-
Notifications
You must be signed in to change notification settings - Fork 122
/
SofTwoThetaTOF.cpp
250 lines (227 loc) · 9.09 KB
/
SofTwoThetaTOF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidWorkflowAlgorithms/SofTwoThetaTOF.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "boost/filesystem.hpp"
namespace {
/// Constants for the algorithm's property names.
namespace Prop {
std::string const ANGLE_STEP{"AngleStep"};
std::string const FILENAME{"GroupingFilename"};
std::string const INPUT_WS{"InputWorkspace"};
std::string const OUTPUT_WS{"OutputWorkspace"};
} // namespace Prop
/// A temporary file resource lifetime manager
struct RemoveFileAtScopeExit {
std::string name;
~RemoveFileAtScopeExit() {
if (!name.empty()) {
auto const path = boost::filesystem::path(name);
if (boost::filesystem::exists(path)) {
boost::filesystem::remove(path);
}
}
}
};
/// A holder of minimum and maximum values.
struct MinMax {
double min{std::numeric_limits<double>::max()};
double max{std::numeric_limits<double>::lowest()};
};
/**
* Return the minimum and maximum X over all histograms
* @param ws a workspace
* @return the minimum and maximum X
*/
MinMax minMaxX(Mantid::API::MatrixWorkspace const &ws) {
auto const nHisto = ws.getNumberHistograms();
MinMax mm;
for (size_t i = 0; i < nHisto; ++i) {
auto const &x = ws.x(i);
mm.min = std::min(mm.min, x.front());
mm.max = std::max(mm.max, x.back());
}
return mm;
}
/**
* Return the width of the first bin in the first histogram.
* @param ws a workspace
* @return the bin width
*/
double binWidth(Mantid::API::MatrixWorkspace const &ws) {
auto const &x = ws.x(0);
return x[1] - x[0];
}
/**
* Append .xml to filename if needed.
* @param filename a filename
* @return a suitable name for an XML file
*/
std::string ensureXMLExtension(std::string const &filename) {
std::string name = filename;
auto const path = boost::filesystem::path(filename);
if (path.extension() != ".xml") {
name += ".xml";
}
return name;
}
} // namespace
namespace Mantid {
namespace WorkflowAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SofTwoThetaTOF)
/// Algorithms name for identification. @see Algorithm::name
const std::string SofTwoThetaTOF::name() const { return "SofTwoThetaTOF"; }
/// Algorithm's version for identification. @see Algorithm::version
int SofTwoThetaTOF::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SofTwoThetaTOF::category() const {
return "Inelastic;ILL\\Direct";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string SofTwoThetaTOF::summary() const {
return "Calculates the intensity as a function of scattering angle and time "
"of flight.";
}
/** Initialize the algorithm's properties.
*/
void SofTwoThetaTOF::init() {
auto histogrammedTOF = std::make_shared<Kernel::CompositeValidator>();
histogrammedTOF->add(std::make_shared<API::WorkspaceUnitValidator>("TOF"));
histogrammedTOF->add(std::make_shared<API::HistogramValidator>());
histogrammedTOF->add(std::make_shared<API::InstrumentValidator>());
declareProperty(
std::make_unique<API::WorkspaceProperty<>>(
Prop::INPUT_WS, "", Kernel::Direction::Input, histogrammedTOF),
"A workspace to be converted.");
declareProperty(std::make_unique<API::WorkspaceProperty<>>(
Prop::OUTPUT_WS, "", Kernel::Direction::Output),
"A workspace with (2theta, TOF) units.");
auto positiveDouble = std::make_shared<Kernel::BoundedValidator<double>>();
positiveDouble->setLowerExclusive(0.);
declareProperty(Prop::ANGLE_STEP, EMPTY_DBL(), positiveDouble,
"The angle step for detector grouping, in degrees.");
declareProperty(std::make_unique<API::FileProperty>(
Prop::FILENAME, "", API::FileProperty::OptionalSave,
std::vector<std::string>{".xml"}),
"A grouping file that will be created; a corresponding .par "
"file wille be created as well.");
}
/** Execute the algorithm.
*/
void SofTwoThetaTOF::exec() {
API::MatrixWorkspace_sptr in = getProperty(Prop::INPUT_WS);
auto ragged = convertToConstantL2(in);
auto equalBinning = rebinToNonRagged(ragged);
auto ws = maskEmptyBins(equalBinning, ragged);
ws = groupByTwoTheta(ws, clarifyAngleStep(*in));
ws = convertToTwoTheta(ws);
setProperty(Prop::OUTPUT_WS, ws);
}
double SofTwoThetaTOF::clarifyAngleStep(API::MatrixWorkspace const &ws) {
if (!isDefault(Prop::ANGLE_STEP)) {
return getProperty(Prop::ANGLE_STEP);
} else {
auto instrument = ws.getInstrument();
if (!instrument) {
throw std::invalid_argument("Missing " + Prop::ANGLE_STEP);
}
auto const ¶mMap = ws.constInstrumentParameters();
auto const paramValues =
paramMap.getDouble(instrument->getName(), "natural-angle-step");
if (paramValues.empty()) {
throw std::invalid_argument("Missing " + Prop::ANGLE_STEP +
" or 'natural-angle-step' not defined in "
"instrument parameters file.");
}
return paramValues.front();
}
}
API::MatrixWorkspace_sptr
SofTwoThetaTOF::convertToConstantL2(API::MatrixWorkspace_sptr &ws) {
auto toConstantL2 = createChildAlgorithm("ConvertToConstantL2", 0., 0.2);
toConstantL2->setProperty("InputWorkspace", ws);
toConstantL2->setPropertyValue("OutputWorkspace", "out");
toConstantL2->execute();
return toConstantL2->getProperty("OutputWorkspace");
}
API::MatrixWorkspace_sptr
SofTwoThetaTOF::convertToTwoTheta(API::MatrixWorkspace_sptr &ws) {
auto convertAxis = createChildAlgorithm("ConvertSpectrumAxis", 0.9, 1.0);
convertAxis->setProperty("InputWorkspace", ws);
convertAxis->setProperty("OutputWorkspace", "out");
convertAxis->setProperty("Target", "Theta");
convertAxis->execute();
return convertAxis->getProperty("OutputWorkspace");
}
API::MatrixWorkspace_sptr
SofTwoThetaTOF::groupByTwoTheta(API::MatrixWorkspace_sptr &ws,
double const twoThetaStep) {
auto generateGrouping =
createChildAlgorithm("GenerateGroupingPowder", 0.2, 0.5);
generateGrouping->setProperty("InputWorkspace", ws);
generateGrouping->setProperty("AngleStep", twoThetaStep);
std::string filename;
RemoveFileAtScopeExit deleteThisLater;
if (isDefault(Prop::FILENAME)) {
auto tempPath = boost::filesystem::temp_directory_path();
tempPath /= boost::filesystem::unique_path(
"detector-grouping-%%%%-%%%%-%%%%-%%%%.xml");
filename = tempPath.string();
generateGrouping->setProperty("GenerateParFile", false);
// Make sure the file gets deleted at scope exit.
// enable cppcheck-suppress unreadVariable if needed
deleteThisLater.name = filename;
} else {
filename = static_cast<std::string>(getProperty(Prop::FILENAME));
filename = ensureXMLExtension(filename);
}
generateGrouping->setProperty("GroupingFilename", filename);
generateGrouping->execute();
auto groupDetectors = createChildAlgorithm("GroupDetectors", 0.7, 0.9);
groupDetectors->setProperty("InputWorkspace", ws);
groupDetectors->setProperty("OutputWorkspace", "out");
groupDetectors->setProperty("MapFile", filename);
groupDetectors->setProperty("Behaviour", "Average");
groupDetectors->execute();
return groupDetectors->getProperty("OutputWorkspace");
}
API::MatrixWorkspace_sptr
SofTwoThetaTOF::maskEmptyBins(API::MatrixWorkspace_sptr &maskable,
API::MatrixWorkspace_sptr &comparison) {
auto maskNonOverlapping =
createChildAlgorithm("MaskNonOverlappingBins", 0.6, 0.7);
maskNonOverlapping->setProperty("InputWorkspace", maskable);
maskNonOverlapping->setProperty("OutputWorkspace", "out");
maskNonOverlapping->setProperty("ComparisonWorkspace", comparison);
maskNonOverlapping->setProperty("MaskPartiallyOverlapping", true);
maskNonOverlapping->setProperty("RaggedInputs", "Ragged");
maskNonOverlapping->setProperty("CheckSortedX", false);
maskNonOverlapping->execute();
return maskNonOverlapping->getProperty("OutputWorkspace");
}
API::MatrixWorkspace_sptr
SofTwoThetaTOF::rebinToNonRagged(API::MatrixWorkspace_sptr &ws) {
auto const xRange = minMaxX(*ws);
std::vector<double> const rebinParams{xRange.min, binWidth(*ws), xRange.max};
auto rebin = createChildAlgorithm("Rebin", 0.5, 0.6);
rebin->setProperty("InputWorkspace", ws);
rebin->setProperty("OutputWorkspace", "out");
rebin->setProperty("Params", rebinParams);
rebin->setProperty("FullBinsOnly", true);
rebin->execute();
return rebin->getProperty("OutputWorkspace");
}
} // namespace WorkflowAlgorithms
} // namespace Mantid