Mantid
Loading...
Searching...
No Matches
GenerateGroupingPowder2.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2024 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 +
8
14
15using namespace Mantid::API;
16using namespace Mantid::DataObjects;
17
18namespace Mantid::DataHandling {
19
20// Register the algorithm into the AlgorithmFactory
22
23
24int GenerateGroupingPowder2::version() const { return 2; }
25
30
31 // This version will determine the file format from the extension of the GroupingFilename property
32 removeProperty("FileFormat");
33}
34
41
43
44 // save if a filename was specified
45 if (!isDefault("GroupingFilename")) {
46
47 std::string filename = this->getProperty("GroupingFilename");
48 std::string ext = filename.substr(filename.length() - 3);
49 if (ext == "xml") {
50 this->saveAsXML();
51 } else if (ext == "nxs" || ext == "nx5") {
52 this->saveAsNexus();
53 } else {
54 throw std::invalid_argument("that file format doesn't exist: must be xml, nxs, nx5\n");
55 }
56
57 if (getProperty("GenerateParFile")) {
58 this->saveAsPAR();
59 }
60 }
61}
62
63// XML file
65 const std::string filename = this->getProperty("GroupingFilename");
66 auto saveDetectorsGrouping = createChildAlgorithm("SaveDetectorsGrouping");
67 saveDetectorsGrouping->setProperty("InputWorkspace", this->m_groupWS);
68 saveDetectorsGrouping->setProperty("OutputFile", filename);
69 saveDetectorsGrouping->setProperty("SaveUngroupedDetectors", false);
70 saveDetectorsGrouping->executeAsChildAlg();
71}
72
73// PAR file
75 std::string PARfilename = getPropertyValue("GroupingFilename");
76 PARfilename = parFilenameFromXmlFilename(PARfilename);
77
78 // create an empty workspace based on input to perform GroupDetectors with
79 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
80 inputWorkspace = WorkspaceFactory::Instance().create(inputWorkspace, -1, 1, 1);
81
82 auto groupDetectors = createChildAlgorithm("GroupDetectors", 0, 1, true, 2);
83 groupDetectors->initialize();
84 groupDetectors->setProperty("InputWorkspace", inputWorkspace);
85 groupDetectors->setProperty("CopyGroupingFromWorkspace", this->m_groupWS);
86 groupDetectors->execute();
87
88 const MatrixWorkspace_sptr groupedWorkspace = groupDetectors->getProperty("OutputWorkspace");
89
90 auto spCalcDetPar = createChildAlgorithm("FindDetectorsPar", 0, 1, true, 1);
91 spCalcDetPar->initialize();
92 spCalcDetPar->setProperty("InputWorkspace", groupedWorkspace);
93 spCalcDetPar->setProperty("ReturnLinearRanges", true);
94 spCalcDetPar->execute();
95
96 auto *pCalcDetPar = dynamic_cast<FindDetectorsPar *>(spCalcDetPar.get());
97 const size_t nDetectors = pCalcDetPar->getNDetectors();
98 const std::vector<double> &secondary_flightpath = pCalcDetPar->getFlightPath();
99
100 double stepSize = getProperty("AngleStep");
101 stepSize *= Geometry::deg2rad;
102 std::vector<double> polar_width;
103
104 std::transform(secondary_flightpath.cbegin(), secondary_flightpath.cend(), std::back_inserter(polar_width),
105 [stepSize](const double r) { return r * stepSize; });
106
107 SavePAR::writePAR(PARfilename, std::vector<double>(nDetectors, -0.), pCalcDetPar->getPolar(),
108 std::vector<double>(nDetectors, 0.01), polar_width, secondary_flightpath, pCalcDetPar->getDetID(),
109 nDetectors);
110}
111} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void removeProperty(const std::string &name, const bool delproperty=true) override
Removes the property from management.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
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.
bool isDefault(const std::string &name) const
size_t getNDetectors() const
number of real detectors, calculated by algorithm
GenerateGroupingPowder2 : Generate grouping file and par file, for powder scattering.
void init() override
Initialize the algorithm's properties.
void init() override
Initialize the algorithm's properties.
static std::string parFilenameFromXmlFilename(const std::string &filename)
DataObjects::GroupingWorkspace_sptr m_groupWS
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
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
constexpr double deg2rad
Defines units/enum for Crystal work.
Definition AngleUnits.h:20