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 // Get a pointer to the sample
50 IComponent_const_sptr sample = inputWorkspace->getInstrument()->getSample();
51
52 std::ofstream outPHX_file(filename.c_str());
53
54 if (!outPHX_file) {
55 g_log.error("Failed to open (PHX) file:" + filename);
56 throw Kernel::Exception::FileError("Failed to open (PHX) 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 spCalcDetPar->setPropertyValue("ReturnLinearRanges", "0");
64 // in test mode, request the ChildAlgortithm to create output workspace and
65 // add it to dataservice
66 if (!det_par_ws_name.empty()) {
67 spCalcDetPar->setPropertyValue("OutputParTable", det_par_ws_name);
68 }
69
70 // let's not do this for the time being
71 /* std::string parFileName = this->getPropertyValue("ParFile");
72 if(!(parFileName.empty()||parFileName=="not_used.par")){
73 spCalcDetPar->setPropertyValue("ParFile",parFileName);
74 }*/
75 spCalcDetPar->execute();
76 //
77 auto *pCalcDetPar = dynamic_cast<FindDetectorsPar *>(spCalcDetPar.get());
78 if (!pCalcDetPar) { // "can not get pointer to FindDetectorsPar algorithm"
79 throw(std::bad_cast());
80 }
81 const std::vector<double> &azimuthal = pCalcDetPar->getAzimuthal();
82 const std::vector<double> &polar = pCalcDetPar->getPolar();
83 const std::vector<double> &azimuthal_width = pCalcDetPar->getAzimWidth();
84 const std::vector<double> &polar_width = pCalcDetPar->getPolarWidth();
85 const std::vector<double> &secondary_flightpath = pCalcDetPar->getFlightPath();
86 const std::vector<size_t> &det_ID = pCalcDetPar->getDetID();
87
88 size_t nDetectors = pCalcDetPar->getNDetectors();
89
90 // Write the number of detectors to the file.
91 outPHX_file << " " << nDetectors << '\n';
92
93 for (size_t i = 0; i < nDetectors; ++i) {
94 // verify if no detector defined;
95 volatile double NanID = azimuthal[i];
96 if (NanID != azimuthal[i])
97 continue; // skip NaN -s
98
99 // Now write all the detector info.
100 outPHX_file << std::fixed << std::setprecision(3);
101 outPHX_file << " " << secondary_flightpath[i] << "\t 0 \t\t" << polar[i] << " \t" << azimuthal[i] << " \t"
102 << polar_width[i] << " \t" << azimuthal_width[i] << " \t\t" << det_ID[i] << '\n';
103 }
104
105 // Close the file
106 outPHX_file.close();
107}
108
109} // 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...
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: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