Mantid
Loading...
Searching...
No Matches
ExtractPolarizationEfficiencies.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 +
11#include "MantidHistogramData/Counts.h"
12#include "MantidHistogramData/LinearGenerator.h"
13#include "MantidHistogramData/Points.h"
14#include "MantidKernel/Unit.h"
15
16#include <sstream>
17
18namespace Mantid::DataHandling {
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(ExtractPolarizationEfficiencies)
21
22using namespace Mantid::API;
23using namespace Mantid::DataObjects;
24using namespace Mantid::HistogramData;
25using namespace Mantid::Kernel;
26
27namespace {
28
29std::string const METHOD_FREDRIKZE("Fredrikze");
30std::string const METHOD_WILDES("Wildes");
31std::string const METHOD_PARAMETER("polarization_correction_method");
32std::string const OPTION_PARAMETER("polarization_correction_option");
33std::string const LAMBDA_PARAMETER("efficiency_lambda");
34
35std::map<std::string, std::vector<std::string>> const EFFICIENCIES{{METHOD_FREDRIKZE, {"Pp", "Ap", "Rho", "Alpha"}},
36 {METHOD_WILDES, {"P1", "P2", "F1", "F2"}}};
37
38std::vector<double> parseVector(std::string const &name, std::string const &value) {
39 std::istringstream istr(value);
40 std::vector<double> result;
41 double number;
42 while (istr >> number) {
43 result.emplace_back(number);
44 }
45
46 if (!istr.eof()) {
47 throw std::invalid_argument("Error while parsing instrument vector parameter " + name);
48 }
49
50 return result;
51}
52
53MatrixWorkspace_sptr createWorkspace(std::vector<double> const &x, std::vector<double> const &y,
54 std::vector<double> const &e = std::vector<double>()) {
55 Points xVals(x);
56 Counts yVals(y);
57 CountStandardDeviations eVals(e.empty() ? std::vector<double>(y.size()) : e);
58 auto retVal = std::make_shared<Workspace2D>();
59 retVal->initialize(1, Histogram(xVals, yVals, eVals));
60 return retVal;
61}
62
63} // namespace
64
66const std::string ExtractPolarizationEfficiencies::name() const { return "ExtractPolarizationEfficiencies"; }
67
70
72const std::string ExtractPolarizationEfficiencies::category() const { return "DataHandling;Reflectometry\\ISIS"; }
73
76 return "Extracts polarization efficiencies from instrument's parameter file.";
77}
78
82 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
83 "A workspace with attached instrument whose parameters contain "
84 "polarization efficiencies.");
85 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
86 "The workspace with extracted efficiencies.");
87 declareProperty<std::string>("CorrectionMethod", "", "Correction method: Fredrikze or Wildes.",
89 declareProperty<std::string>("CorrectionOption", "", "Correction option, eg \"PA\" or \"PNR\" for Fredrikze method.",
91}
92
96 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
97
98 auto instrument = inputWS->getInstrument();
99 auto const method = instrument->getParameterAsString(METHOD_PARAMETER);
100 if (method.empty()) {
101 throw std::invalid_argument("Correction method is undefined");
102 }
103 if (method != METHOD_FREDRIKZE && method != METHOD_WILDES) {
104 throw std::invalid_argument("Unknown correction method: " + method);
105 }
106 setProperty("CorrectionMethod", method);
107 auto const lambdaValue = instrument->getParameterAsString(LAMBDA_PARAMETER);
108 if (lambdaValue.empty()) {
109 throw std::invalid_argument("Wavelengths are missing from the correction parameters");
110 }
111 auto const lambda = parseVector(LAMBDA_PARAMETER, lambdaValue);
112 if (lambda.size() < 2) {
113 throw std::runtime_error("Instrument vector parameter \"" + LAMBDA_PARAMETER +
114 "\" must have at least 2 elements but it has " + std::to_string(lambda.size()));
115 }
116
117 auto alg = createChildAlgorithm("JoinISISPolarizationEfficiencies");
118 auto const &efficiencies = EFFICIENCIES.at(method);
119 for (auto const &name : efficiencies) {
120 auto propValue = instrument->getParameterAsString(name);
121 if (propValue.empty()) {
122 throw std::invalid_argument("Parameter " + name + " is missing from the correction parameters");
123 }
124 auto const prop = parseVector(name, propValue);
125 if (lambda.size() != prop.size()) {
126 throw std::runtime_error("Instrument vector parameter \"" + name + "\" is expected to be the same size as \"" +
127 LAMBDA_PARAMETER + "\" but " + std::to_string(prop.size()) +
128 " != " + std::to_string(lambda.size()));
129 }
130 auto const errorName = name + "_Errors";
131 propValue = instrument->getParameterAsString(errorName);
132 auto const errorProp = propValue.empty() ? std::vector<double>() : parseVector(errorName, propValue);
133 auto ws = createWorkspace(lambda, prop, errorProp);
134 alg->setProperty(name, ws);
135 }
136 alg->execute();
137 MatrixWorkspace_sptr result = alg->getProperty("OutputWorkspace");
138 setProperty("OutputWorkspace", result);
139 auto const option = instrument->getParameterAsString(OPTION_PARAMETER);
140 if (option.empty()) {
141 throw std::invalid_argument("Correction option is undefined");
142 }
143 setProperty("CorrectionOption", option);
144}
145
146} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
const std::vector< double > * lambda
double value
The value of the point.
Definition: FitMW.cpp:51
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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.
Definition: Algorithm.cpp:842
A property class for workspaces.
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 name() const override
Algorithm's name for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< T > createWorkspace(InitArgs... args)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54