Mantid
Loading...
Searching...
No Matches
SofTwoThetaTOF.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
18
19#include <filesystem>
20
22
23namespace {
25namespace Prop {
26std::string const ANGLE_STEP{"AngleStep"};
27std::string const FILENAME{"GroupingFilename"};
28std::string const INPUT_WS{"InputWorkspace"};
29std::string const OUTPUT_WS{"OutputWorkspace"};
30} // namespace Prop
31
33struct RemoveFileAtScopeExit {
34 std::string name;
35 ~RemoveFileAtScopeExit() {
36 if (!name.empty()) {
37 auto const path = std::filesystem::path(name);
38 if (std::filesystem::exists(path)) {
39 std::filesystem::remove(path);
40 }
41 }
42 }
43};
44
46struct MinMax {
47 double min{std::numeric_limits<double>::max()};
48 double max{std::numeric_limits<double>::lowest()};
49};
50
56MinMax minMaxX(Mantid::API::MatrixWorkspace const &ws) {
57 auto const nHisto = ws.getNumberHistograms();
58 MinMax mm;
59 for (size_t i = 0; i < nHisto; ++i) {
60 auto const &x = ws.x(i);
61 mm.min = std::min(mm.min, x.front());
62 mm.max = std::max(mm.max, x.back());
63 }
64 return mm;
65}
66
72double binWidth(Mantid::API::MatrixWorkspace const &ws) {
73 auto const &x = ws.x(0);
74 return x[1] - x[0];
75}
76
82std::string ensureXMLExtension(std::string const &filename) {
83 std::string name = filename;
84 auto const path = std::filesystem::path(filename);
85 if (path.extension() != ".xml") {
86 name += ".xml";
87 }
88 return name;
89}
90} // namespace
91
93
94// Register the algorithm into the AlgorithmFactory
95DECLARE_ALGORITHM(SofTwoThetaTOF)
96
97
98const std::string SofTwoThetaTOF::name() const { return "SofTwoThetaTOF"; }
99
101int SofTwoThetaTOF::version() const { return 1; }
102
104const std::string SofTwoThetaTOF::category() const { return "Inelastic;ILL\\Direct"; }
105
107const std::string SofTwoThetaTOF::summary() const {
108 return "Calculates the intensity as a function of scattering angle and time "
109 "of flight.";
110}
111
115 auto histogrammedTOF = std::make_shared<Kernel::CompositeValidator>();
116 histogrammedTOF->add(std::make_shared<API::WorkspaceUnitValidator>("TOF"));
117 histogrammedTOF->add(std::make_shared<API::HistogramValidator>());
118 histogrammedTOF->add(std::make_shared<API::InstrumentValidator>());
119 declareProperty(
120 std::make_unique<API::WorkspaceProperty<>>(Prop::INPUT_WS, "", Kernel::Direction::Input, histogrammedTOF),
121 "A workspace to be converted.");
122 declareProperty(std::make_unique<API::WorkspaceProperty<>>(Prop::OUTPUT_WS, "", Kernel::Direction::Output),
123 "A workspace with (2theta, TOF) units.");
124 auto positiveDouble = std::make_shared<Kernel::BoundedValidator<double>>();
125 positiveDouble->setLowerExclusive(0.);
126 declareProperty(Prop::ANGLE_STEP, EMPTY_DBL(), positiveDouble, "The angle step for detector grouping, in degrees.");
127 declareProperty(std::make_unique<API::FileProperty>(Prop::FILENAME, "", API::FileProperty::OptionalSave,
128 std::vector<std::string>{".xml"}),
129 "A grouping file that will be created; a corresponding .par "
130 "file wille be created as well.");
131}
132
136 API::MatrixWorkspace_sptr in = getProperty(Prop::INPUT_WS);
137 auto ragged = convertToConstantL2(in);
138 auto equalBinning = rebinToNonRagged(ragged);
139 auto ws = maskEmptyBins(equalBinning, ragged);
140 ws = groupByTwoTheta(ws, clarifyAngleStep(*in));
141 ws = convertToTwoTheta(ws);
142 setProperty(Prop::OUTPUT_WS, ws);
143}
144
146 if (!isDefault(Prop::ANGLE_STEP)) {
147 return getProperty(Prop::ANGLE_STEP);
148 } else {
149 auto instrument = ws.getInstrument();
150 if (!instrument) {
151 throw std::invalid_argument("Missing " + Prop::ANGLE_STEP);
152 }
153 auto const &paramMap = ws.constInstrumentParameters();
154 auto const paramValues = paramMap.getDouble(instrument->getName(), "natural-angle-step");
155 if (paramValues.empty()) {
156 throw std::invalid_argument("Missing " + Prop::ANGLE_STEP +
157 " or 'natural-angle-step' not defined in "
158 "instrument parameters file.");
159 }
160 return paramValues.front();
161 }
162}
163
165 auto toConstantL2 = createChildAlgorithm("ConvertToConstantL2", 0., 0.2);
166 toConstantL2->setProperty("InputWorkspace", ws);
167 toConstantL2->setPropertyValue("OutputWorkspace", "out");
168 toConstantL2->execute();
169 return toConstantL2->getProperty("OutputWorkspace");
170}
171
173 auto convertAxis = createChildAlgorithm("ConvertSpectrumAxis", 0.9, 1.0);
174 convertAxis->setProperty("InputWorkspace", ws);
175 convertAxis->setProperty("OutputWorkspace", "out");
176 convertAxis->setProperty("Target", "Theta");
177 convertAxis->execute();
178 return convertAxis->getProperty("OutputWorkspace");
179}
180
182 const std::string GROUP_WS("softwothetatof_groupWS");
183 auto generateGrouping = createChildAlgorithm("GenerateGroupingPowder", 0.2, 0.5);
184 generateGrouping->setProperty("InputWorkspace", ws);
185 generateGrouping->setProperty("AngleStep", twoThetaStep);
186 generateGrouping->setProperty("GroupingWorkspace", GROUP_WS);
187 std::string filename;
188 RemoveFileAtScopeExit deleteThisLater;
189 if (isDefault(Prop::FILENAME)) {
190 const std::string shortname = "detector-grouping-" + randomString(4) + "-" + randomString(4) + "-" +
191 randomString(4) + "-" + randomString(4) + ".xml";
192 const auto tempPath = std::filesystem::temp_directory_path() / shortname;
193 filename = tempPath.string();
194 generateGrouping->setProperty("GenerateParFile", false);
195 // Make sure the file gets deleted at scope exit.
196 deleteThisLater.name = filename;
197 } else {
198 filename = static_cast<std::string>(getProperty(Prop::FILENAME));
199 filename = ensureXMLExtension(filename);
200 }
201 generateGrouping->setProperty("GroupingFilename", filename);
202 generateGrouping->execute();
203
204 auto groupDetectors = createChildAlgorithm("GroupDetectors", 0.7, 0.9);
205 groupDetectors->setProperty("InputWorkspace", ws);
206 groupDetectors->setProperty("OutputWorkspace", "out");
207 // TODO make this algo work with the GroupingWorkspace from GenerateGroupingPowder
208 // Mantid::DataObjects::GroupingWorkspace_sptr gws = generateGrouping->getProperty("GroupingWorkspace");
209 // groupDetectors->setProperty("CopyGroupingFromWorkspace", gws);
210 groupDetectors->setProperty("MapFile", filename);
211 groupDetectors->setProperty("Behaviour", "Average");
212 groupDetectors->execute();
213 return groupDetectors->getProperty("OutputWorkspace");
214}
215
217 API::MatrixWorkspace_sptr &comparison) {
218 auto maskNonOverlapping = createChildAlgorithm("MaskNonOverlappingBins", 0.6, 0.7);
219 maskNonOverlapping->setProperty("InputWorkspace", maskable);
220 maskNonOverlapping->setProperty("OutputWorkspace", "out");
221 maskNonOverlapping->setProperty("ComparisonWorkspace", comparison);
222 maskNonOverlapping->setProperty("MaskPartiallyOverlapping", true);
223 maskNonOverlapping->setProperty("RaggedInputs", "Ragged");
224 maskNonOverlapping->setProperty("CheckSortedX", false);
225 maskNonOverlapping->execute();
226 return maskNonOverlapping->getProperty("OutputWorkspace");
227}
228
230 auto const xRange = minMaxX(*ws);
231 std::vector<double> const rebinParams{xRange.min, binWidth(*ws), xRange.max};
232 auto rebin = createChildAlgorithm("Rebin", 0.5, 0.6);
233 rebin->setProperty("InputWorkspace", ws);
234 rebin->setProperty("OutputWorkspace", "out");
235 rebin->setProperty("Params", rebinParams);
236 rebin->setProperty("FullBinsOnly", true);
237 rebin->execute();
238 return rebin->getProperty("OutputWorkspace");
239}
240
241} // namespace Mantid::WorkflowAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
const Geometry::ParameterMap & constInstrumentParameters() const
Const version.
Geometry::Instrument_const_sptr getInstrument() const
Returns the parameterized instrument.
@ OptionalSave
to specify a file to write to but an empty string is
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) override
Create a Child Algorithm.
Kernel::IPropertyManager::TypedValue getProperty(const std::string &name) const override
Get the property held by this object.
Base MatrixWorkspace Abstract Class.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
const HistogramData::HistogramX & x(const size_t index) const
A property class for workspaces.
std::vector< double > getDouble(const std::string &compName, const std::string &name) const
Returns a double parameter as vector's first element if exists and an empty vector if it doesn't.
SofTwoThetaTOF : Convert a S(spectrum number, TOF) workspace to S(twoTheta, TOF) workspace.
API::MatrixWorkspace_sptr convertToTwoTheta(API::MatrixWorkspace_sptr &ws)
const std::string category() const override
Algorithm's category for identification.
API::MatrixWorkspace_sptr maskEmptyBins(API::MatrixWorkspace_sptr &maskable, API::MatrixWorkspace_sptr &comparison)
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void exec() override
Execute the algorithm.
API::MatrixWorkspace_sptr groupByTwoTheta(API::MatrixWorkspace_sptr &ws, double const twoThetaStep)
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
API::MatrixWorkspace_sptr rebinToNonRagged(API::MatrixWorkspace_sptr &ws)
API::MatrixWorkspace_sptr convertToConstantL2(API::MatrixWorkspace_sptr &ws)
double clarifyAngleStep(API::MatrixWorkspace const &ws)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_KERNEL_DLL std::string randomString(const size_t len)
Generates random alpha-numeric string.
Definition Strings.cpp:1190
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
String constants for algorithm's properties.
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54