Mantid
Loading...
Searching...
No Matches
SavePAR.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
17
18#include <cstdio>
19#include <fstream>
20
21namespace Mantid::DataHandling {
22
23// Register the algorithm into the AlgorithmFactory
24DECLARE_ALGORITHM(SavePAR)
25
26using namespace Mantid::Kernel;
27using namespace Mantid::API;
28using namespace Mantid::Geometry;
29
30// A reference to the logger is provided by the base class, it is called g_log.
31// It is used to print out information,
32
34 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
35 std::make_shared<InstrumentValidator>()),
36 "The name of the workspace to save.");
37 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
38 "The name to give to the saved file.");
39}
40
42
43 // Get the input workspace
44 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
45
46 // Retrieve the filename from the properties
47 const std::string filename = getProperty("Filename");
48
49 // Get a pointer to the sample
50 IComponent_const_sptr sample = inputWorkspace->getInstrument()->getSample();
51
52 std::ofstream outPAR_file(filename.c_str());
53
54 if (!outPAR_file) {
55 g_log.error("Failed to open (PAR) file:" + filename);
56 throw Kernel::Exception::FileError("Failed to open (PAR) file:", filename);
57 }
58
59 // execute the ChildAlgorithm to calculate the detector's parameters;
60 auto spCalcDetPar = createChildAlgorithm("FindDetectorsPar", 0, 1, true, 1);
61 spCalcDetPar->initialize();
62 spCalcDetPar->setPropertyValue("InputWorkspace", inputWorkspace->getName());
63 // calculate linear rather then angular detector's sizes;
64 spCalcDetPar->setPropertyValue("ReturnLinearRanges", "1");
65 // in test mode, request the ChildAlgortithm to create output workspace and
66 // add it to dataservice
67 if (!det_par_ws_name.empty()) {
68 spCalcDetPar->setPropertyValue("OutputParTable", det_par_ws_name);
69 }
70
71 // let's not do this for the time being
72 /* std::string parFileName = this->getPropertyValue("ParFile");
73 if(!(parFileName.empty()||parFileName=="not_used.par")){
74 spCalcDetPar->setPropertyValue("ParFile",parFileName);
75 }*/
76 spCalcDetPar->execute();
77 //
78 auto *pCalcDetPar = dynamic_cast<FindDetectorsPar *>(spCalcDetPar.get());
79 if (!pCalcDetPar) { // "can not get pointer to FindDetectorsPar algorithm"
80 throw(std::bad_cast());
81 }
82 const std::vector<double> &azimuthal = pCalcDetPar->getAzimuthal();
83 const std::vector<double> &polar = pCalcDetPar->getPolar();
84 const std::vector<double> &azimuthal_width = pCalcDetPar->getAzimWidth();
85 const std::vector<double> &polar_width = pCalcDetPar->getPolarWidth();
86 const std::vector<double> &secondary_flightpath = pCalcDetPar->getFlightPath();
87 const std::vector<size_t> &det_ID = pCalcDetPar->getDetID();
88
89 size_t nDetectors = pCalcDetPar->getNDetectors();
90
91 // Write the number of detectors to the file.
92 outPAR_file << " " << nDetectors << '\n';
93
94 for (size_t i = 0; i < nDetectors; ++i) {
95 // verify if no detector defined;
96 volatile double NanID = azimuthal[i];
97 if (NanID != azimuthal[i])
98 continue; // skip NaN -s
99
100 // Now write all the detector info.
101 outPAR_file << std::fixed << std::setprecision(3);
102 outPAR_file.width(10);
103 outPAR_file << secondary_flightpath[i];
104 outPAR_file.width(10);
105 outPAR_file << polar[i];
106 outPAR_file.width(10);
107 outPAR_file << (-azimuthal[i]);
108 outPAR_file.width(10);
109 outPAR_file << polar_width[i];
110 outPAR_file.width(10);
111 outPAR_file << azimuthal_width[i];
112 outPAR_file.width(10);
113 outPAR_file << det_ID[i] << '\n';
114 }
115
116 // Close the file
117 outPAR_file.close();
118}
119
120} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
A property class for workspaces.
std::vector< double > const & getAzimuthal() const
the accessors, used to return algorithm results when called as Child Algorithm, without setting the p...
std::string det_par_ws_name
The name of the table workpsace with detectors positions used in tests.
Definition: SavePAR.h:74
void init() override
Initialisation code.
Definition: SavePAR.cpp:33
void exec() override
Execution code.
Definition: SavePAR.cpp:41
Records the filename and the description of failure.
Definition: Exception.h:98
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
Definition: IComponent.h:161
@ Input
An input workspace.
Definition: Property.h:53