Mantid
Loading...
Searching...
No Matches
BaseConvertToDiffractionMDWorkspace.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 +
8
10
14
18
22
23#include <algorithm>
24#include <limits>
25
26using namespace Mantid::API;
27using namespace Mantid::Kernel;
28using namespace Mantid::DataObjects;
29using namespace Mantid::Geometry;
30
31namespace Mantid::MDAlgorithms {
32
35public:
36 DisabledProperty() : EnabledWhenProperty("NonExistingProperty", IS_DEFAULT) {}
37 bool checkCriterion(const IPropertyManager * /*algo*/) const override { return false; }
38};
39
40//----------------------------------------------------------------------------------------------
44
45 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
46 "An input workspace.");
47
48 declareProperty(std::make_unique<WorkspaceProperty<IMDEventWorkspace>>("OutputWorkspace", "", Direction::Output),
49 "Name of the output MDEventWorkspace. If the workspace "
50 "already exists, then the events will be added to it.");
51 declareProperty(std::make_unique<PropertyWithValue<bool>>("Append", false, Direction::Input),
52 "Append events to the output workspace. The workspace is replaced if "
53 "unchecked.");
54
55 // Disabled for this version
56 declareProperty(std::make_unique<PropertyWithValue<bool>>("ClearInputWorkspace", false, Direction::Input),
57 "Clearing the events from the input workspace during "
58 "conversion (to save memory) is not supported by algorithm "
59 "v2");
60 // disable property on interface
61 this->setPropertySettings("ClearInputWorkspace", std::make_unique<DisabledProperty>());
62
63 declareProperty(std::make_unique<PropertyWithValue<bool>>("OneEventPerBin", true, Direction::Input),
64 "Use the histogram representation (event for event workspaces).\n"
65 "One MDEvent will be created for each histogram bin (even empty ones).\n"
66 "Warning! This can use significantly more memory!");
67
68 frameOptions.emplace_back("Q (sample frame)");
69 frameOptions.emplace_back("Q (lab frame)");
70 frameOptions.emplace_back("HKL");
71 declareProperty("OutputDimensions", "Q (lab frame)", std::make_shared<StringListValidator>(frameOptions),
72 "What will be the dimensions of the output workspace?\n"
73 " Q (lab frame): Wave-vector change of the lattice in the lab frame.\n"
74 " Q (sample frame): Wave-vector change of the lattice in the frame of "
75 "the sample (taking out goniometer rotation).\n"
76 " HKL: Use the sample's UB matrix to convert to crystal's HKL indices.");
77
78 declareProperty(std::make_unique<PropertyWithValue<bool>>("LorentzCorrection", false, Direction::Input),
79 "Correct the weights of events by multiplying by the Lorentz "
80 "formula: sin(theta)^2 / lambda^4");
81
82 // Box controller properties. These are the defaults
83 this->initBoxControllerProps("2" /*SplitInto*/, 1500 /*SplitThreshold*/, 20 /*MaxRecursionDepth*/);
84
85 declareProperty(std::make_unique<PropertyWithValue<int>>("MinRecursionDepth", 1),
86 "Optional. If specified, then all the boxes will be split to this "
87 "minimum recursion depth. 1 = one level of splitting, etc.\n"
88 "Be careful using this since it can quickly create a huge number of "
89 "boxes = (SplitInto ^ (MinRercursionDepth * NumDimensions)).\n"
90 "But setting this property equal to MaxRecursionDepth property is "
91 "necessary if one wants to generate multiple file based workspaces in "
92 "order to merge them later\n");
93 setPropertyGroup("MinRecursionDepth", getBoxSettingsGroupName());
94}
95
108 std::string &TargFrameName,
109 std::string &ScalingName) {
110 // ----------------- Handle the type of output
111 // -------------------------------------
112
113 MDAlgorithms::MDWSTransform QSclAndFrames;
114
115 if (TargFrame == frameOptions[0]) // "Q (sample frame)"
116 {
117 TargFrameName = QSclAndFrames.getTargetFrame(MDAlgorithms::CnvrtToMD::SampleFrame);
118 ScalingName = QSclAndFrames.getQScaling(MDAlgorithms::CnvrtToMD::NoScaling); //< momentums in A^-1
119 } else if (TargFrame == frameOptions[1]) // "Q (lab frame)"
120 {
121 TargFrameName = QSclAndFrames.getTargetFrame(MDAlgorithms::CnvrtToMD::LabFrame);
122 ScalingName = QSclAndFrames.getQScaling(MDAlgorithms::CnvrtToMD::NoScaling); //< momentums in A^-1
123 } else if (TargFrame == frameOptions[2]) // "HKL"
124 {
125 TargFrameName = QSclAndFrames.getTargetFrame(MDAlgorithms::CnvrtToMD::HKLFrame);
126 ScalingName = QSclAndFrames.getQScaling(MDAlgorithms::CnvrtToMD::HKLScale); //< momentums in A^-1
127 } else {
128 throw std::invalid_argument("BaseConvertToDiffractionMDWorkspace::Unknown target frame: " + TargFrame);
129 }
130}
131
132//----------------------------------------------------------------------------------------------
135
136 Mantid::API::Algorithm_sptr Convert = createChildAlgorithm("ConvertToMD", 0., 1.);
137 Convert->initialize();
138
139 Convert->setRethrows(true);
140 Convert->initialize();
141
142 Convert->setProperty<MatrixWorkspace_sptr>("InputWorkspace", this->getProperty("InputWorkspace"));
143 Convert->setProperty("OutputWorkspace", this->getPropertyValue("OutputWorkspace"));
144 Convert->setProperty("OverwriteExisting", !this->getProperty("Append"));
145
146 if (!MDTransfFactory::Instance().exists("Q3D")) {
147 throw std::runtime_error(" ConvertToMD Q3D plugin used to transform into "
148 "DiffractionWorkspaced has not been registered "
149 "with the MDTransformation factory");
150 }
151 Convert->setPropertyValue("QDimensions", "Q3D");
152
153 std::vector<std::string> dE_modes = Kernel::DeltaEMode::availableTypes();
154 Convert->setPropertyValue("dEAnalysisMode", dE_modes[Kernel::DeltaEMode::Elastic]);
155
156 std::string TargetFrame, Scaling;
157 this->convertFramePropertyNames(this->getPropertyValue("OutputDimensions"), TargetFrame, Scaling);
158 Convert->setProperty("Q3DFrames", TargetFrame);
159 Convert->setProperty("QConversionScales", Scaling);
160
161 Convert->setProperty("OtherDimensions", "");
162 Convert->setProperty("PreprocDetectorsWS", "-");
163
164 bool lorCorr = this->getProperty("LorentzCorrection");
165 Convert->setProperty("LorentzCorrection", lorCorr);
166
167 bool ignoreZeros = !this->getProperty("OneEventPerBin");
168 Convert->setProperty("IgnoreZeroSignals", ignoreZeros);
169 // set extents
170 std::vector<double> extents = this->getProperty("Extents");
171 std::vector<double> minVal, maxVal;
172 convertExtents(extents, minVal, maxVal);
173 Convert->setProperty("MinValues", minVal);
174 Convert->setProperty("MaxValues", maxVal);
175
176 // Box controller properties. Has defaults
177 Convert->setProperty("SplitInto", this->getPropertyValue("SplitInto"));
178 Convert->setProperty("SplitThreshold", this->getPropertyValue("SplitThreshold"));
179 Convert->setProperty("MaxRecursionDepth", this->getPropertyValue("MaxRecursionDepth"));
180 std::string depth = this->getPropertyValue("MinRecursionDepth");
181 if (depth == "0")
182 depth = "1"; // ConvertToMD does not understand 0 depth
183 Convert->setProperty("MinRecursionDepth", depth);
184
185 Convert->executeAsChildAlg();
186
187 IMDEventWorkspace_sptr iOut = Convert->getProperty("OutputWorkspace");
188 this->setProperty("OutputWorkspace", iOut);
189}
190
191} // namespace Mantid::MDAlgorithms
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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
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
void initBoxControllerProps(const std::string &SplitInto="5", int SplitThreshold=1000, int MaxRecursionDepth=5)
Initialise the properties.
A property class for workspaces.
Interface to PropertyManager.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
void init() override
Initialize the algorithm's properties.
void convertFramePropertyNames(const std::string &TargFrame, std::string &TargFrameName, std::string &ScalingName)
method to convert the value of the target frame specified for the ConvertToDiffractionMDWorksapce int...
virtual void convertExtents(const std::vector< double > &Extents, std::vector< double > &minVal, std::vector< double > &maxVal)=0
Small class to diable propery on interface.
bool checkCriterion(const IPropertyManager *) const override
Checks that the specified property matches the criteria given.
CnvrtToMD::CoordScaling getQScaling(const std::string &ScID) const
function which convert input string representing coordinate scaling to correspondent enum
CnvrtToMD::TargetFrame getTargetFrame(const std::string &FrameID) const
converts the target frame string representation into the frame ID
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition: Algorithm.h:61
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
TargetFrame
enum describes availible target coordinate systems for Q3D mode
Definition: MDWSTransform.h:40
static const std::vector< std::string > availableTypes()
Returns the string list of available modes.
Definition: DeltaEMode.cpp:35
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54