Mantid
Loading...
Searching...
No Matches
SavePHX.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(SavePHX)
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 input workspace");
37 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
38 "The filename to use for the saved data");
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 std::ofstream outPHX_file(filename.c_str());
50
51 if (!outPHX_file) {
52 g_log.error("Failed to open (PHX) file:" + filename);
53 throw Kernel::Exception::FileError("Failed to open (PHX) file:", filename);
54 }
55
56 // execute the ChildAlgorithm to calculate the detector's parameters;
57 auto spCalcDetPar = createChildAlgorithm("FindDetectorsPar", 0, 1, true, 1);
58 spCalcDetPar->initialize();
59 spCalcDetPar->setPropertyValue("InputWorkspace", inputWorkspace->getName());
60 spCalcDetPar->setPropertyValue("ReturnLinearRanges", "0");
61 // in test mode, request the ChildAlgortithm to create output workspace and
62 // add it to dataservice
63 if (!det_par_ws_name.empty()) {
64 spCalcDetPar->setPropertyValue("OutputParTable", det_par_ws_name);
65 }
66
67 // let's not do this for the time being
68 /* std::string parFileName = this->getPropertyValue("ParFile");
69 if(!(parFileName.empty()||parFileName=="not_used.par")){
70 spCalcDetPar->setPropertyValue("ParFile",parFileName);
71 }*/
72 spCalcDetPar->execute();
73 //
74 const auto *pCalcDetPar = dynamic_cast<FindDetectorsPar *>(spCalcDetPar.get());
75 if (!pCalcDetPar) { // "can not get pointer to FindDetectorsPar algorithm"
76 throw(std::bad_cast());
77 }
78 const std::vector<double> &azimuthal = pCalcDetPar->getAzimuthal();
79 const std::vector<double> &polar = pCalcDetPar->getPolar();
80 const std::vector<double> &azimuthal_width = pCalcDetPar->getAzimWidth();
81 const std::vector<double> &polar_width = pCalcDetPar->getPolarWidth();
82 const std::vector<double> &secondary_flightpath = pCalcDetPar->getFlightPath();
83 const std::vector<size_t> &det_ID = pCalcDetPar->getDetID();
84
85 size_t nDetectors = pCalcDetPar->getNDetectors();
86
87 // Write the number of detectors to the file.
88 outPHX_file << " " << nDetectors << '\n';
89
90 for (size_t i = 0; i < nDetectors; ++i) {
91 // verify if no detector defined;
92 volatile double NanID = azimuthal[i];
93 if (NanID != azimuthal[i])
94 continue; // skip NaN -s
95
96 // Now write all the detector info.
97 outPHX_file << std::fixed << std::setprecision(3);
98 outPHX_file << " " << secondary_flightpath[i] << "\t 0 \t\t" << polar[i] << " \t" << azimuthal[i] << " \t"
99 << polar_width[i] << " \t" << azimuthal_width[i] << " \t\t" << det_ID[i] << '\n';
100 }
101
102 // Close the file
103 outPHX_file.close();
104}
105
106} // 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.
Kernel::Logger & g_log
Definition Algorithm.h:422
@ 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...
void exec() override
Execution code.
Definition SavePHX.cpp:41
void init() override
Initialisation code.
Definition SavePHX.cpp:33
std::string det_par_ws_name
The name of the table workpsace with detectors positions used in tests.
Definition SavePHX.h:60
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:108
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
Definition Property.h:53