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
13
14namespace Mantid::DataHandling {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(SavePAR)
18
19using namespace Mantid::Kernel;
20using namespace Mantid::API;
21using namespace Mantid::Geometry;
22
23// A reference to the logger is provided by the base class, it is called g_log.
24// It is used to print out information,
25
27 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
28 std::make_shared<InstrumentValidator>()),
29 "The name of the workspace to save.");
30 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
31 "The name to give to the saved file.");
32}
33
35 // Get the input workspace
36 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
37
38 // Retrieve the filename from the properties
39 const std::string filename = getProperty("Filename");
40
41 // execute the ChildAlgorithm to calculate the detector's parameters;
42 auto spCalcDetPar = createChildAlgorithm("FindDetectorsPar", 0, 1, true, 1);
43 spCalcDetPar->initialize();
44 spCalcDetPar->setPropertyValue("InputWorkspace", inputWorkspace->getName());
45 // calculate linear rather then angular detector's sizes;
46 spCalcDetPar->setPropertyValue("ReturnLinearRanges", "1");
47 // in test mode, request the ChildAlgortithm to create output workspace and
48 // add it to dataservice
49 if (!det_par_ws_name.empty()) {
50 spCalcDetPar->setPropertyValue("OutputParTable", det_par_ws_name);
51 }
52
53 spCalcDetPar->execute();
54 //
55 const auto *pCalcDetPar = dynamic_cast<FindDetectorsPar *>(spCalcDetPar.get());
56 if (!pCalcDetPar) { // "can not get pointer to FindDetectorsPar algorithm"
57 throw(std::bad_cast());
58 }
59 const std::vector<double> &azimuthal = pCalcDetPar->getAzimuthal();
60 const std::vector<double> &polar = pCalcDetPar->getPolar();
61 const std::vector<double> &azimuthal_width = pCalcDetPar->getAzimWidth();
62 const std::vector<double> &polar_width = pCalcDetPar->getPolarWidth();
63 const std::vector<double> &secondary_flightpath = pCalcDetPar->getFlightPath();
64 const std::vector<size_t> &det_ID = pCalcDetPar->getDetID();
65
66 size_t nDetectors = pCalcDetPar->getNDetectors();
67
68 writePAR(filename, azimuthal, polar, azimuthal_width, polar_width, secondary_flightpath, det_ID, nDetectors);
69}
70
71void SavePAR::writePAR(const std::string &filename, const std::vector<double> &azimuthal,
72 const std::vector<double> &polar, const std::vector<double> &azimuthal_width,
73 const std::vector<double> &polar_width, const std::vector<double> &secondary_flightpath,
74 const std::vector<size_t> &det_ID, const size_t nDetectors) {
75 std::ofstream outPAR_file(filename.c_str());
76
77 if (!outPAR_file) {
78 throw Kernel::Exception::FileError("Failed to open (PAR) file:", filename);
79 }
80 // Write the number of detectors to the file.
81 outPAR_file << " " << nDetectors << '\n';
82
83 for (size_t i = 0; i < nDetectors; ++i) {
84 // verify if no detector defined;
85 volatile double NanID = azimuthal[i];
86 if (NanID != azimuthal[i])
87 continue; // skip NaN -s
88
89 // Now write all the detector info.
90 outPAR_file << std::fixed << std::setprecision(3);
91 outPAR_file.width(10);
92 outPAR_file << secondary_flightpath[i];
93 outPAR_file.width(10);
94 outPAR_file << polar[i];
95 outPAR_file.width(10);
96 outPAR_file << (-azimuthal[i]);
97 outPAR_file.width(10);
98 outPAR_file << polar_width[i];
99 outPAR_file.width(10);
100 outPAR_file << azimuthal_width[i];
101 outPAR_file.width(10);
102 outPAR_file << det_ID[i] << '\n';
103 }
104
105 // Close the file
106 outPAR_file.close();
107}
108
109} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
@ Save
to specify a file to write to, the file may or may not exist
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:79
static void writePAR(const std::string &filename, const std::vector< double > &azimuthal, const std::vector< double > &polar, const std::vector< double > &azimuthal_width, const std::vector< double > &polar_width, const std::vector< double > &secondary_flightpath, const std::vector< size_t > &det_ID, const size_t nDetectors)
Definition SavePAR.cpp:71
void init() override
Initialisation code.
Definition SavePAR.cpp:26
void exec() override
Execution code.
Definition SavePAR.cpp:34
Records the filename and the description of failure.
Definition Exception.h:98
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
Definition Property.h:53