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
17
18#include "boost/filesystem.hpp"
19
20namespace {
22namespace Prop {
23std::string const ANGLE_STEP{"AngleStep"};
24std::string const FILENAME{"GroupingFilename"};
25std::string const INPUT_WS{"InputWorkspace"};
26std::string const OUTPUT_WS{"OutputWorkspace"};
27} // namespace Prop
28
30struct RemoveFileAtScopeExit {
31 std::string name;
32 ~RemoveFileAtScopeExit() {
33 if (!name.empty()) {
34 auto const path = boost::filesystem::path(name);
35 if (boost::filesystem::exists(path)) {
36 boost::filesystem::remove(path);
37 }
38 }
39 }
40};
41
43struct MinMax {
44 double min{std::numeric_limits<double>::max()};
45 double max{std::numeric_limits<double>::lowest()};
46};
47
53MinMax minMaxX(Mantid::API::MatrixWorkspace const &ws) {
54 auto const nHisto = ws.getNumberHistograms();
55 MinMax mm;
56 for (size_t i = 0; i < nHisto; ++i) {
57 auto const &x = ws.x(i);
58 mm.min = std::min(mm.min, x.front());
59 mm.max = std::max(mm.max, x.back());
60 }
61 return mm;
62}
63
69double binWidth(Mantid::API::MatrixWorkspace const &ws) {
70 auto const &x = ws.x(0);
71 return x[1] - x[0];
72}
73
79std::string ensureXMLExtension(std::string const &filename) {
80 std::string name = filename;
81 auto const path = boost::filesystem::path(filename);
82 if (path.extension() != ".xml") {
83 name += ".xml";
84 }
85 return name;
86}
87} // namespace
88
90
91// Register the algorithm into the AlgorithmFactory
92DECLARE_ALGORITHM(SofTwoThetaTOF)
93
94
95const std::string SofTwoThetaTOF::name() const { return "SofTwoThetaTOF"; }
96
98int SofTwoThetaTOF::version() const { return 1; }
99
101const std::string SofTwoThetaTOF::category() const { return "Inelastic;ILL\\Direct"; }
102
104const std::string SofTwoThetaTOF::summary() const {
105 return "Calculates the intensity as a function of scattering angle and time "
106 "of flight.";
107}
108
112 auto histogrammedTOF = std::make_shared<Kernel::CompositeValidator>();
113 histogrammedTOF->add(std::make_shared<API::WorkspaceUnitValidator>("TOF"));
114 histogrammedTOF->add(std::make_shared<API::HistogramValidator>());
115 histogrammedTOF->add(std::make_shared<API::InstrumentValidator>());
116 declareProperty(
117 std::make_unique<API::WorkspaceProperty<>>(Prop::INPUT_WS, "", Kernel::Direction::Input, histogrammedTOF),
118 "A workspace to be converted.");
119 declareProperty(std::make_unique<API::WorkspaceProperty<>>(Prop::OUTPUT_WS, "", Kernel::Direction::Output),
120 "A workspace with (2theta, TOF) units.");
121 auto positiveDouble = std::make_shared<Kernel::BoundedValidator<double>>();
122 positiveDouble->setLowerExclusive(0.);
123 declareProperty(Prop::ANGLE_STEP, EMPTY_DBL(), positiveDouble, "The angle step for detector grouping, in degrees.");
124 declareProperty(std::make_unique<API::FileProperty>(Prop::FILENAME, "", API::FileProperty::OptionalSave,
125 std::vector<std::string>{".xml"}),
126 "A grouping file that will be created; a corresponding .par "
127 "file wille be created as well.");
128}
129
133 API::MatrixWorkspace_sptr in = getProperty(Prop::INPUT_WS);
134 auto ragged = convertToConstantL2(in);
135 auto equalBinning = rebinToNonRagged(ragged);
136 auto ws = maskEmptyBins(equalBinning, ragged);
137 ws = groupByTwoTheta(ws, clarifyAngleStep(*in));
138 ws = convertToTwoTheta(ws);
139 setProperty(Prop::OUTPUT_WS, ws);
140}
141
143 if (!isDefault(Prop::ANGLE_STEP)) {
144 return getProperty(Prop::ANGLE_STEP);
145 } else {
146 auto instrument = ws.getInstrument();
147 if (!instrument) {
148 throw std::invalid_argument("Missing " + Prop::ANGLE_STEP);
149 }
150 auto const &paramMap = ws.constInstrumentParameters();
151 auto const paramValues = paramMap.getDouble(instrument->getName(), "natural-angle-step");
152 if (paramValues.empty()) {
153 throw std::invalid_argument("Missing " + Prop::ANGLE_STEP +
154 " or 'natural-angle-step' not defined in "
155 "instrument parameters file.");
156 }
157 return paramValues.front();
158 }
159}
160
162 auto toConstantL2 = createChildAlgorithm("ConvertToConstantL2", 0., 0.2);
163 toConstantL2->setProperty("InputWorkspace", ws);
164 toConstantL2->setPropertyValue("OutputWorkspace", "out");
165 toConstantL2->execute();
166 return toConstantL2->getProperty("OutputWorkspace");
167}
168
170 auto convertAxis = createChildAlgorithm("ConvertSpectrumAxis", 0.9, 1.0);
171 convertAxis->setProperty("InputWorkspace", ws);
172 convertAxis->setProperty("OutputWorkspace", "out");
173 convertAxis->setProperty("Target", "Theta");
174 convertAxis->execute();
175 return convertAxis->getProperty("OutputWorkspace");
176}
177
179 auto generateGrouping = createChildAlgorithm("GenerateGroupingPowder", 0.2, 0.5);
180 generateGrouping->setProperty("InputWorkspace", ws);
181 generateGrouping->setProperty("AngleStep", twoThetaStep);
182 std::string filename;
183 RemoveFileAtScopeExit deleteThisLater;
184 if (isDefault(Prop::FILENAME)) {
185 auto tempPath = boost::filesystem::temp_directory_path();
186 tempPath /= boost::filesystem::unique_path("detector-grouping-%%%%-%%%%-%%%%-%%%%.xml");
187 filename = tempPath.string();
188 generateGrouping->setProperty("GenerateParFile", false);
189 // Make sure the file gets deleted at scope exit.
190 // enable cppcheck-suppress unreadVariable if needed
191 deleteThisLater.name = filename;
192 } else {
193 filename = static_cast<std::string>(getProperty(Prop::FILENAME));
194 filename = ensureXMLExtension(filename);
195 }
196 generateGrouping->setProperty("GroupingFilename", filename);
197 generateGrouping->execute();
198 auto groupDetectors = createChildAlgorithm("GroupDetectors", 0.7, 0.9);
199 groupDetectors->setProperty("InputWorkspace", ws);
200 groupDetectors->setProperty("OutputWorkspace", "out");
201 groupDetectors->setProperty("MapFile", filename);
202 groupDetectors->setProperty("Behaviour", "Average");
203 groupDetectors->execute();
204 return groupDetectors->getProperty("OutputWorkspace");
205}
206
208 API::MatrixWorkspace_sptr &comparison) {
209 auto maskNonOverlapping = createChildAlgorithm("MaskNonOverlappingBins", 0.6, 0.7);
210 maskNonOverlapping->setProperty("InputWorkspace", maskable);
211 maskNonOverlapping->setProperty("OutputWorkspace", "out");
212 maskNonOverlapping->setProperty("ComparisonWorkspace", comparison);
213 maskNonOverlapping->setProperty("MaskPartiallyOverlapping", true);
214 maskNonOverlapping->setProperty("RaggedInputs", "Ragged");
215 maskNonOverlapping->setProperty("CheckSortedX", false);
216 maskNonOverlapping->execute();
217 return maskNonOverlapping->getProperty("OutputWorkspace");
218}
219
221 auto const xRange = minMaxX(*ws);
222 std::vector<double> const rebinParams{xRange.min, binWidth(*ws), xRange.max};
223 auto rebin = createChildAlgorithm("Rebin", 0.5, 0.6);
224 rebin->setProperty("InputWorkspace", ws);
225 rebin->setProperty("OutputWorkspace", "out");
226 rebin->setProperty("Params", rebinParams);
227 rebin->setProperty("FullBinsOnly", true);
228 rebin->execute();
229 return rebin->getProperty("OutputWorkspace");
230}
231
232} // namespace Mantid::WorkflowAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
Definition: FileProperty.h:50
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.
Definition: ParameterMap.h:237
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
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
String constants for algorithm's properties.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54