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