-
Notifications
You must be signed in to change notification settings - Fork 122
/
GetQsInQENSData.cpp
133 lines (108 loc) · 3.93 KB
/
GetQsInQENSData.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
// 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 "MantidAlgorithms/GetQsInQENSData.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidGeometry/IDetector.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitConversion.h"
#include <stdexcept>
namespace {
Mantid::Kernel::Logger g_log("ConvolutionFitSequential");
}
namespace Mantid::Algorithms {
using namespace API;
using namespace Kernel;
using namespace Geometry;
// Register the Algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(GetQsInQENSData)
// Initializes the Algorithm
void GetQsInQENSData::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"Input QENS data as MatrixWorkspace");
declareProperty("RaiseMode", false,
"Set to True if an Exception, instead of "
"any empty list of Q values, is "
"desired.");
declareProperty(std::make_unique<ArrayProperty<double>>("Qvalues", Direction::Output));
}
/*
* Validates the input properties
*/
std::map<std::string, std::string> GetQsInQENSData::validateInputs() {
std::map<std::string, std::string> issues;
MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
// Check whether the input workspace could be found
if (!inputWs) {
issues["InputWorkspace"] = "InputWorkspace is not a MatrixWorkspace";
}
return issues;
}
/*
* Executes the Algorithm
*/
void GetQsInQENSData::exec() {
MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
try {
setProperty("Qvalues", extractQValues(inputWs));
} catch (std::exception &e) {
g_log.error(e.what());
// If the RaiseMode property has been set to true, raise any
// exception which is thrown.
bool inRaiseMode = getProperty("RaiseMode");
if (inRaiseMode) {
throw;
}
setProperty("Qvalues", std::vector<double>());
}
}
/*
* Extracts Q-values from the specified workspace.
*
* @param workspace The workspace from which to extract Q-values.
* @return The extracted Q-values as a vector.
*/
MantidVec GetQsInQENSData::extractQValues(const Mantid::API::MatrixWorkspace_sptr &workspace) {
size_t numSpectra = workspace->getNumberHistograms();
Axis *qAxis;
try {
qAxis = workspace->getAxis(1);
} catch (std::exception &) {
throw std::runtime_error("Vertical axis is empty");
}
MantidVec qValues(qAxis->length());
// Check if the specified workspace is already in Q-space.
if (qAxis->unit()->unitID() == "MomentumTransfer") {
// Add axis values to vector of Q-values
for (size_t i = 0; i < qAxis->length(); i++) {
qValues[i] = qAxis->getValue(i);
}
// Check if the Q-values are stored as histogram data.
if (qValues.size() == numSpectra + 1) {
// Convert Q-values to point values.
qValues.pop_back();
qValues.erase(qValues.begin());
using std::placeholders::_1;
std::transform(qValues.begin(), qValues.end(), qValues.begin(), std::bind(std::divides<double>(), _1, 2.0));
}
} else {
// Iterate over all spectrum in the specified workspace.
try {
for (size_t i = 0; i < numSpectra; i++) {
IDetector_const_sptr detector = workspace->getDetector(i);
double efixed = workspace->getEFixed(detector);
double theta = 0.5 * workspace->detectorTwoTheta(*detector);
qValues[i] = UnitConversion::convertToElasticQ(theta, efixed);
}
} catch (std::exception &) {
throw std::runtime_error("Detectors are missing from the input workspace");
}
}
return qValues;
}
} // namespace Mantid::Algorithms