Mantid
Loading...
Searching...
No Matches
LoadILLPolarizationFactors.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#include "MantidAPI/TextAxis.h"
14#include "MantidHistogramData/Histogram.h"
15#include "MantidHistogramData/Interpolate.h"
16
17#include <fstream>
18
19namespace {
21namespace Prop {
22const static std::string FILENAME{"Filename"};
23const static std::string OUT_WS{"OutputWorkspace"};
24const static std::string REF_WS{"WavelengthReference"};
25} // namespace Prop
26
28struct FactorDefinition {
30 std::vector<double> limits;
32 std::vector<double> fitFactors;
33};
34
36enum class Factor { F1, F2, Phi, P1, P2 };
37
39Factor factor(const std::string &l) {
40 const auto id = l.substr(0, 2);
41 if (id == "F1")
42 return Factor::F1;
43 if (id == "F2")
44 return Factor::F2;
45 if (id == "Ph")
46 return Factor::Phi;
47 if (id == "P1")
48 return Factor::P1;
49 if (id == "P2")
50 return Factor::P2;
51 throw std::runtime_error("Syntax error.");
52}
53
55const std::array<Factor, 5> factor_list() { return {{Factor::F1, Factor::F2, Factor::P1, Factor::P2, Factor::Phi}}; }
56
58std::string to_string(const Factor f) {
59 switch (f) {
60 case Factor::F1:
61 return "F1";
62 case Factor::F2:
63 return "F2";
64 case Factor::P1:
65 return "P1";
66 case Factor::P2:
67 return "P2";
68 case Factor::Phi:
69 return "Phi";
70 }
71 throw std::runtime_error("Unknown polarization correction factor tag.");
72}
73
75std::string cleanse_comments(const std::string &l) {
76 const auto commentBegin = l.find(';');
77 // commentBegin == npos is OK.
78 return l.substr(0, commentBegin);
79}
80
82void cleanse_whitespace(std::string &l) { l.erase(std::remove_if(l.begin(), l.end(), isspace), l.end()); }
83
85bool contains_limits(const std::string &l) { return l.find("_limits") != std::string::npos; }
86
88std::vector<double> extract_values(const std::string &l) {
89 const auto valBegin = l.find('[');
90 const auto valEnd = l.find(']');
91 if (valBegin == std::string::npos || valEnd == std::string::npos)
92 throw std::runtime_error("Syntax error.");
93 std::vector<double> values;
94 std::string valStr;
95 for (size_t i = valBegin + 1; i != valEnd; i++) {
96 const auto c = l[i];
97 if (c != ',') {
98 valStr.push_back(c);
99 if (i != valEnd - 1) {
100 continue;
101 }
102 }
103 double v;
104 try {
105 v = std::stod(valStr);
106 } catch (std::exception &) {
107 throw std::runtime_error("Syntax error");
108 }
109 values.emplace_back(v);
110 valStr.clear();
111 }
112 return values;
113}
114
116Mantid::HistogramData::Histogram make_histogram(const std::vector<double> &points, const double maxWavelength) {
117 Mantid::HistogramData::Points p(points.size() + 2);
118 p.mutableRawData().front() = 0;
119 p.mutableRawData().back() = maxWavelength > points.back() ? maxWavelength : 2 * points.back();
120 for (size_t i = 1; i != p.size() - 1; ++i) {
121 p.mutableData()[i] = points[i - 1];
122 }
123 const Mantid::HistogramData::Counts c(p.size());
124 return Mantid::HistogramData::Histogram(p, c);
125}
126
128void calculate_factors_in_place(Mantid::HistogramData::Histogram &h, const std::vector<double> &piecewiseFactors) {
129 const auto &xs = h.x();
130 auto &ys = h.mutableY();
131 ys[0] = piecewiseFactors.front();
132 for (size_t i = 1; i != h.size(); ++i) {
133 ys[i] = ys[i - 1] + piecewiseFactors[i] * (xs[i] - xs[i - 1]);
134 }
135}
136
138std::map<Factor, FactorDefinition> parse(std::istream &in) {
139 std::map<Factor, FactorDefinition> factors;
140 std::string l;
141 while (!in.eof()) {
142 try {
143 std::getline(in, l);
144 } catch (std::exception &e) {
145 throw std::runtime_error(std::string("Unknown exception: ") + e.what());
146 }
147 cleanse_whitespace(l);
148 l = cleanse_comments(l);
149 if (l.empty())
150 continue;
151 auto &fDef = factors[factor(l)];
152 const auto values = extract_values(l);
153 if (contains_limits(l)) {
154 fDef.limits = values;
155 } else {
156 fDef.fitFactors = values;
157 }
158 }
159 return factors;
160}
161
163void definition_map_sanity_check(const std::map<Factor, FactorDefinition> &m) {
164 const auto factors = factor_list();
165 for (const auto f : factors) {
166 const auto i = m.find(f);
167 if (i == m.end()) {
168 throw std::runtime_error("One of the factors is missing.");
169 }
170 const auto &fDef = (*i).second;
171 if (fDef.limits.empty()) {
172 throw std::runtime_error("No limits defined for a factor.");
173 }
174 if (fDef.fitFactors.empty()) {
175 throw std::runtime_error("No fitting information defined for a factor.");
176 }
177 if (fDef.limits.size() + 2 != fDef.fitFactors.size()) {
178 throw std::runtime_error("Size mismatch between limits and fitting information.");
179 }
180 }
181}
182
184void addErrors(Mantid::HistogramData::Histogram &h, const Factor tag) {
185 // The error estimates are taken from the LAMP/COSMOS software.
186 const auto f = [tag]() {
187 switch (tag) {
188 case Factor::F1:
189 case Factor::F2:
190 return 1. / 3000.;
191 case Factor::P1:
192 case Factor::P2:
193 case Factor::Phi:
194 return 1. / 500.;
195 default:
196 throw std::logic_error("Logic error: unknown efficiency factor tag.");
197 }
198 }();
199 h.mutableE() = (h.y() * f).rawData();
200}
201
203void setUnits(Mantid::API::MatrixWorkspace &ws) {
204 auto xAxis = ws.getAxis(0);
205 xAxis->setUnit("Wavelength");
206 ws.setYUnit("Polarization efficiency");
207}
208} // namespace
209
210namespace Mantid::DataHandling {
211
214
215// Register the algorithm into the AlgorithmFactory
216DECLARE_ALGORITHM(LoadILLPolarizationFactors)
217
218//----------------------------------------------------------------------------------------------
219
220
221const std::string LoadILLPolarizationFactors::name() const { return "LoadILLPolarizationFactors"; }
222
224int LoadILLPolarizationFactors::version() const { return 1; }
225
227const std::string LoadILLPolarizationFactors::category() const { return "DataHandling\\Text;ILL\\Reflectometry"; }
228
230const std::string LoadILLPolarizationFactors::summary() const {
231 return "Loads ILL formatted reflectometry polarization efficiency factors.";
232}
233
234//----------------------------------------------------------------------------------------------
238 declareProperty(std::make_unique<API::FileProperty>(Prop::FILENAME, "", API::FileProperty::Load),
239 "Path to the polarization efficiency file.");
240 const auto refWSValidator = std::make_shared<API::IncreasingAxisValidator>();
242 std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>(Prop::OUT_WS, "", Direction::Output, refWSValidator),
243 "An output workspace containing the efficiencies at the "
244 "reference workspace's wavelength points.");
246 "A reference workspace to get the wavelength axis from.");
247}
248
249//----------------------------------------------------------------------------------------------
253 API::MatrixWorkspace_const_sptr refWS = getProperty(Prop::REF_WS);
254 HistogramData::Histogram tmplHist{refWS->histogram(0).points()};
255 API::MatrixWorkspace_sptr outWS = DataObjects::create<DataObjects::Workspace2D>(5, tmplHist);
256 auto outVertAxis = std::make_unique<API::TextAxis>(5);
257 const auto maxWavelength = tmplHist.x().back();
258
259 const std::string filename = getProperty(Prop::FILENAME);
260 std::ifstream in(filename);
261 if (in.bad()) {
262 throw std::runtime_error("Couldn't open file " + filename);
263 }
264 const auto fittingData = [&in, &filename]() {
265 try {
266 const auto data = parse(in);
267 definition_map_sanity_check(data);
268 return data;
269 } catch (std::exception &e) {
270 throw std::runtime_error("Error while reading " + filename + ": " + e.what());
271 }
272 }();
273 const auto factorTags = factor_list();
275 for (int i = 0; i < static_cast<int>(factorTags.size()); ++i) {
277 const auto tag = factorTags[i];
278 const auto &fDef = fittingData.at(tag);
279 auto source = make_histogram(fDef.limits, maxWavelength);
280 calculate_factors_in_place(source, fDef.fitFactors);
281 auto target = outWS->histogram(i);
282 HistogramData::interpolateLinearInplace(source, target);
283 addErrors(target, tag);
284 HistogramData::Histogram outH{refWS->histogram(0)};
285 outH.setSharedY(target.sharedY());
286 outH.setSharedE(target.sharedE());
287 outWS->setHistogram(i, outH);
288 outVertAxis->setLabel(i, to_string(tag));
290 }
292 outWS->replaceAxis(1, std::move(outVertAxis));
293 setUnits(*outWS);
294 outWS->setTitle("Polarization efficiency factors");
295 setProperty(Prop::OUT_WS, outWS);
296}
297
301std::map<std::string, std::string> LoadILLPolarizationFactors::validateInputs() {
302 std::map<std::string, std::string> issues;
303 API::MatrixWorkspace_const_sptr refWS = getProperty(Prop::REF_WS);
304 if (refWS->getNumberHistograms() == 0) {
305 issues[Prop::REF_WS] = "The reference workspace does not contain any histograms.";
306 return issues;
307 }
308 const auto &xs = refWS->x(0);
309 // A validator should have checked that xs is ordered.
310 if (xs.front() < 0) {
311 issues[Prop::REF_WS] = "The reference workspace contains negative X values.";
312 }
313 return issues;
314}
315
316} // namespace Mantid::DataHandling
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual const std::shared_ptr< Kernel::Unit > & setUnit(const std::string &unitName)
Set the unit on the Axis.
Definition Axis.cpp:39
@ Load
allowed here which will be passed to the algorithm
Base MatrixWorkspace Abstract Class.
virtual Axis * getAxis(const std::size_t &axisIndex) const
Get a non owning pointer to a workspace axis.
void setYUnit(const std::string &newUnit)
Sets a new unit for the data (Y axis) in the workspace.
A property class for workspaces.
LoadILLPolarizationFactors : Load reflectometry polarization efficiency correction factors from disk.
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
void init() override
Initialize the algorithm's properties.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
std::map< std::string, std::string > validateInputs() override
Validates the algorithm's inputs.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
String constants for algorithm's properties.
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54