Mantid
Loading...
Searching...
No Matches
JoinISISPolarizationEfficiencies.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
10#include "MantidAPI/TextAxis.h"
13#include "MantidHistogramData/Histogram.h"
14#include "MantidHistogramData/Interpolate.h"
15#include "MantidHistogramData/LinearGenerator.h"
16
17#include <limits>
18
19using namespace Mantid::API;
20using namespace Mantid::DataObjects;
21using namespace Mantid::HistogramData;
22using namespace Mantid::Kernel;
23
24namespace Mantid::DataHandling {
25
26// Register the algorithm into the AlgorithmFactory
28
29//----------------------------------------------------------------------------------------------
30
31
32const std::string JoinISISPolarizationEfficiencies::name() const { return "JoinISISPolarizationEfficiencies"; }
33
36
39 return "Joins workspaces containing ISIS reflectometry polarization "
40 "efficiency factors into a single workspace ready to be used with "
41 "PolarizationEfficiencyCor.";
42}
43
44const std::vector<std::string> JoinISISPolarizationEfficiencies::seeAlso() const {
45 return {"CreatePolarizationEfficiencies", "LoadISISPolarizationEfficiencies", "PolarizationEfficiencyCor"};
46}
47
48//----------------------------------------------------------------------------------------------
52
55 "A matrix workspaces containing the Pp polarization efficiency.");
56
59 "A matrix workspaces containing the Ap polarization efficiency.");
60
63 "A matrix workspaces containing the Rho polarization efficiency.");
64
67 "A matrix workspaces containing the Alpha polarization efficiency.");
68
71 "A matrix workspaces containing the P1 polarization efficiency.");
72
75 "A matrix workspaces containing the P2 polarization efficiency.");
76
79 "A matrix workspaces containing the F1 polarization efficiency.");
80
83 "A matrix workspaces containing the F2 polarization efficiency.");
84
86}
87
91 std::vector<MatrixWorkspace_sptr> workspaces;
92 for (auto const &propName : props) {
93 MatrixWorkspace_sptr ws = getProperty(propName);
94 if (ws->getNumberHistograms() != 1) {
95 throw std::runtime_error("Loaded workspace must contain a single histogram. Found " +
96 std::to_string(ws->getNumberHistograms()));
97 }
98 workspaces.emplace_back(ws);
99 }
100
101 return createEfficiencies(props, workspaces);
102}
103
109JoinISISPolarizationEfficiencies::createEfficiencies(std::vector<std::string> const &labels,
110 std::vector<MatrixWorkspace_sptr> const &workspaces) {
111 auto interpolatedWorkspaces = interpolateWorkspaces(workspaces);
112
113 auto const &inWS = interpolatedWorkspaces.front();
114 MatrixWorkspace_sptr outWS = DataObjects::create<Workspace2D>(*inWS, labels.size(), inWS->histogram(0));
115 auto axis1 = std::make_unique<TextAxis>(labels.size());
116 auto axis1Raw = axis1.get();
117 outWS->replaceAxis(1, std::move(axis1));
118 outWS->getAxis(0)->setUnit("Wavelength");
119
120 for (size_t i = 0; i < interpolatedWorkspaces.size(); ++i) {
121 auto &ws = interpolatedWorkspaces[i];
122 outWS->setHistogram(i, ws->histogram(0));
123 axis1Raw->setLabel(i, labels[i]);
124 }
125
126 return outWS;
127}
128
132std::vector<MatrixWorkspace_sptr>
133JoinISISPolarizationEfficiencies::interpolateWorkspaces(std::vector<MatrixWorkspace_sptr> const &workspaces) {
134 size_t minSize(std::numeric_limits<size_t>::max());
135 size_t maxSize(0);
136 bool thereAreHistograms = false;
137 bool allAreHistograms = true;
138
139 // Find out if the workspaces need to be interpolated.
140 for (auto const &ws : workspaces) {
141 auto size = ws->blocksize();
142 if (size < minSize) {
143 minSize = size;
144 }
145 if (size > maxSize) {
146 maxSize = size;
147 }
148 thereAreHistograms = thereAreHistograms || ws->isHistogramData();
149 allAreHistograms = allAreHistograms && ws->isHistogramData();
150 }
151
152 if (thereAreHistograms != allAreHistograms) {
153 throw std::invalid_argument("Cannot mix histograms and point data.");
154 }
155
156 // All same size, same type - nothing to do
157 if (minSize == maxSize) {
158 return workspaces;
159 }
160
161 // Interpolate those that need interpolating
162 std::vector<MatrixWorkspace_sptr> interpolatedWorkspaces;
163 for (auto const &ws : workspaces) {
164 if (ws->blocksize() < maxSize) {
165 if (allAreHistograms) {
166 interpolatedWorkspaces.emplace_back(interpolateHistogramWorkspace(ws, maxSize));
167 } else {
168 interpolatedWorkspaces.emplace_back(interpolatePointDataWorkspace(ws, maxSize));
169 }
170 } else {
171 interpolatedWorkspaces.emplace_back(ws);
172 }
173 }
174
175 return interpolatedWorkspaces;
176}
177
179 size_t const maxSize) {
180 auto const &x = ws->x(0);
181 auto const startX = x.front();
182 auto const endX = x.back();
183 Counts yVals(maxSize, 0.0);
184 auto const dX = (endX - startX) / double(maxSize - 1);
185 Points xVals(maxSize, LinearGenerator(startX, dX));
186 auto newHisto = Histogram(xVals, yVals);
187 interpolateLinearInplace(ws->histogram(0), newHisto);
188 auto interpolatedWS = std::make_shared<Workspace2D>();
189 interpolatedWS->initialize(1, newHisto);
190 assert(interpolatedWS->y(0).size() == maxSize);
191 return interpolatedWS;
192}
193
195 size_t const maxSize) {
196 ws->setDistribution(true);
197 auto const &x = ws->x(0);
198 auto const dX = (x.back() - x.front()) / double(maxSize);
199 std::vector<double> params(2 * maxSize + 1);
200 for (size_t i = 0; i < maxSize; ++i) {
201 params[2 * i] = x.front() + dX * double(i);
202 params[2 * i + 1] = dX;
203 }
204 params.back() = x.back();
205 auto alg = createChildAlgorithm("InterpolatingRebin");
206 alg->setProperty("InputWorkspace", ws);
207 alg->setProperty("Params", params);
208 alg->setProperty("OutputWorkspace", "dummy");
209 alg->execute();
210 MatrixWorkspace_sptr interpolatedWS = alg->getProperty("OutputWorkspace");
211 assert(interpolatedWS->y(0).size() == maxSize);
212 assert(interpolatedWS->x(0).size() == maxSize + 1);
213 return interpolatedWS;
214}
215
216} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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.
static std::string const Pp
Names of the efficiency properties.
JoinISISPolarizationEfficiencies : Joins reflectometry polarization efficiency correction factors to ...
API::MatrixWorkspace_sptr interpolateHistogramWorkspace(const API::MatrixWorkspace_sptr &ws, size_t const maxSize)
std::vector< API::MatrixWorkspace_sptr > interpolateWorkspaces(std::vector< API::MatrixWorkspace_sptr > const &workspaces)
Interpolate the workspaces so that all have the same blocksize.
API::MatrixWorkspace_sptr interpolatePointDataWorkspace(const API::MatrixWorkspace_sptr &ws, size_t const maxSize)
API::MatrixWorkspace_sptr createEfficiencies(std::vector< std::string > const &props) override
Load efficientcies from files and put them into a single workspace.
int version() const override
Algorithm's version for identification.
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso (these are not validated) algorithms related to this algorithm....
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::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53