-
Notifications
You must be signed in to change notification settings - Fork 122
/
CreatePolarizationEfficiencies.cpp
129 lines (99 loc) · 4.94 KB
/
CreatePolarizationEfficiencies.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
// 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 "MantidDataHandling/CreatePolarizationEfficiencies.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceHistory.h"
#include "MantidDataObjects/WorkspaceSingleValue.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Unit.h"
#include <memory>
#include <algorithm>
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
namespace {
double calculatePolynomial(std::vector<double> const &coefficients, double x) {
double polynomial = coefficients[0];
double xPow = 1.0;
// Build up the polynomial in ascending powers of x
for (size_t i = 1; i < coefficients.size(); ++i) {
xPow *= x;
polynomial += coefficients[i] * xPow;
}
return polynomial;
}
} // namespace
namespace Mantid::DataHandling {
DECLARE_ALGORITHM(CreatePolarizationEfficiencies)
const std::string CreatePolarizationEfficiencies::name() const { return "CreatePolarizationEfficiencies"; }
int CreatePolarizationEfficiencies::version() const { return 1; }
const std::string CreatePolarizationEfficiencies::summary() const {
return "Converts polynomial factors to histograms with polarization "
"efficiencies.";
}
const std::vector<std::string> CreatePolarizationEfficiencies::seeAlso() const {
return {"JoinISISPolarizationEfficiencies", "LoadISISPolarizationEfficiencies", "PolarizationEfficiencyCor"};
}
void CreatePolarizationEfficiencies::init() {
declareProperty(
std::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"An input workspace to use the x-values from.");
declareProperty(std::make_unique<ArrayProperty<double>>(Pp, Direction::Input),
"Effective polarizing power of the polarizing system. "
"Expressed as a ratio 0 < Pp < 1");
declareProperty(std::make_unique<ArrayProperty<double>>(Ap, Direction::Input),
"Effective polarizing power of the analyzing system. "
"Expressed as a ratio 0 < Ap < 1");
declareProperty(std::make_unique<ArrayProperty<double>>(Rho, Direction::Input),
"Ratio of efficiencies of polarizer spin-down to polarizer "
"spin-up. This is characteristic of the polarizer flipper. "
"Values are constants for each term in a polynomial "
"expression.");
declareProperty(std::make_unique<ArrayProperty<double>>(Alpha, Direction::Input),
"Ratio of efficiencies of analyzer spin-down to analyzer "
"spin-up. This is characteristic of the analyzer flipper. "
"Values are factors for each term in a polynomial "
"expression.");
declareProperty(std::make_unique<ArrayProperty<double>>(P1, Direction::Input), "Polarizer efficiency.");
declareProperty(std::make_unique<ArrayProperty<double>>(P2, Direction::Input), "Analyzer efficiency.");
declareProperty(std::make_unique<ArrayProperty<double>>(F1, Direction::Input), "Polarizer flipper efficiency.");
declareProperty(std::make_unique<ArrayProperty<double>>(F2, Direction::Input), "Analyzer flipper efficiency.");
initOutputWorkspace();
}
/// Create the efficiencies workspace given names of input properties.
/// @param labels :: Names of efficiencies which to include in the output
/// workspace.
MatrixWorkspace_sptr CreatePolarizationEfficiencies::createEfficiencies(std::vector<std::string> const &labels) {
std::vector<std::vector<double>> polynomialCoefficients;
for (auto const &label : labels) {
polynomialCoefficients.emplace_back<std::vector<double>>(getProperty(label));
}
MatrixWorkspace_sptr inWS = getProperty("InputWorkspace");
auto sharedInX = inWS->sharedX(0);
MatrixWorkspace_sptr outWS =
WorkspaceFactory::Instance().create(inWS, labels.size(), sharedInX->size(), inWS->blocksize());
auto axis1 = std::make_unique<TextAxis>(labels.size());
auto axis1Raw = axis1.get();
outWS->replaceAxis(1, std::move(axis1));
outWS->getAxis(0)->setUnit(inWS->getAxis(0)->unit()->unitID());
auto const x = inWS->points(0);
std::vector<double> y(x.size());
for (size_t i = 0; i < labels.size(); ++i) {
outWS->setSharedX(i, sharedInX);
auto const &coefficients = polynomialCoefficients[i];
std::transform(x.begin(), x.end(), y.begin(),
[&coefficients](double v) { return calculatePolynomial(coefficients, v); });
outWS->mutableY(i) = y;
axis1Raw->setLabel(i, labels[i]);
}
return outWS;
}
} // namespace Mantid::DataHandling