-
Notifications
You must be signed in to change notification settings - Fork 122
/
ConvertHFIRSCDtoMDE.cpp
264 lines (232 loc) · 11.2 KB
/
ConvertHFIRSCDtoMDE.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// 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 "MantidMDAlgorithms/ConvertHFIRSCDtoMDE.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidAPI/Run.h"
#include "MantidDataObjects/MDBoxBase.h"
#include "MantidDataObjects/MDEventFactory.h"
#include "MantidDataObjects/MDEventInserter.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidGeometry/MDGeometry/QSample.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/UnitLabelTypes.h"
#include "Eigen/Dense"
#include "boost/math/constants/constants.hpp"
namespace Mantid::MDAlgorithms {
using Mantid::API::WorkspaceProperty;
using Mantid::Kernel::Direction;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ConvertHFIRSCDtoMDE)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string ConvertHFIRSCDtoMDE::name() const { return "ConvertHFIRSCDtoMDE"; }
/// Algorithm's version for identification. @see Algorithm::version
int ConvertHFIRSCDtoMDE::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string ConvertHFIRSCDtoMDE::category() const { return "MDAlgorithms\\Creation"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string ConvertHFIRSCDtoMDE::summary() const {
return "Convert from the detector vs scan index MDHistoWorkspace into a "
"MDEventWorkspace with units in Q_sample.";
}
std::map<std::string, std::string> ConvertHFIRSCDtoMDE::validateInputs() {
std::map<std::string, std::string> result;
API::IMDHistoWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
std::stringstream inputWSmsg;
if (inputWS->getNumDims() != 3) {
inputWSmsg << "Incorrect number of dimensions";
} else if (inputWS->getDimension(0)->getName() != "y" || inputWS->getDimension(1)->getName() != "x" ||
inputWS->getDimension(2)->getName() != "scanIndex") {
inputWSmsg << "Wrong dimensions";
} else if (inputWS->getNumExperimentInfo() == 0) {
inputWSmsg << "Missing experiment info";
} else if (inputWS->getExperimentInfo(0)->getInstrument()->getName() != "HB3A" &&
inputWS->getExperimentInfo(0)->getInstrument()->getName() != "WAND") {
inputWSmsg << "This only works for DEMAND (HB3A) or WAND (HB2C)";
} else if (inputWS->getDimension(2)->getNBins() != inputWS->getExperimentInfo(0)->run().getNumGoniometers()) {
inputWSmsg << "goniometers not set correctly, did you run SetGoniometer "
"with Average=False";
} else {
std::string instrument = inputWS->getExperimentInfo(0)->getInstrument()->getName();
const auto run = inputWS->getExperimentInfo(0)->run();
size_t number_of_runs = inputWS->getDimension(2)->getNBins();
std::vector<std::string> logs;
if (instrument == "HB3A")
logs = {"monitor", "time"};
else
logs = {"duration", "monitor_count"};
for (auto log : logs) {
if (run.hasProperty(log)) {
if (static_cast<size_t>(run.getLogData(log)->size()) != number_of_runs)
inputWSmsg << "Log " << log << " has incorrect length, ";
} else {
inputWSmsg << "Missing required log " << log << ", ";
}
}
}
if (!inputWSmsg.str().empty())
result["InputWorkspace"] = inputWSmsg.str();
std::vector<double> minVals = this->getProperty("MinValues");
std::vector<double> maxVals = this->getProperty("MaxValues");
if (minVals.size() != 3 || maxVals.size() != 3) {
std::stringstream msg;
msg << "Must provide 3 values, 1 for every dimension";
result["MinValues"] = msg.str();
result["MaxValues"] = msg.str();
} else {
std::stringstream msg;
size_t rank = minVals.size();
for (size_t i = 0; i < rank; ++i) {
if (minVals[i] >= maxVals[i]) {
if (msg.str().empty())
msg << "max not bigger than min ";
else
msg << ", ";
msg << "at index=" << (i + 1) << " (" << minVals[i] << ">=" << maxVals[i] << ")";
}
}
if (!msg.str().empty()) {
result["MinValues"] = msg.str();
result["MaxValues"] = msg.str();
}
}
return result;
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ConvertHFIRSCDtoMDE::init() {
declareProperty(std::make_unique<WorkspaceProperty<API::IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
declareProperty(
std::make_unique<PropertyWithValue<double>>(
"Wavelength", DBL_MAX, std::make_shared<BoundedValidator<double>>(0.0, 100.0, true), Direction::Input),
"Wavelength");
declareProperty(std::make_unique<ArrayProperty<double>>("MinValues", "-10,-10,-10"),
"It has to be 3 comma separated values, one for each dimension in "
"q_sample."
"Values smaller then specified here will not be added to "
"workspace.");
declareProperty(std::make_unique<ArrayProperty<double>>("MaxValues", "10,10,10"),
"A list of the same size and the same units as MinValues "
"list. Values higher or equal to the specified by "
"this list will be ignored");
// Box controller properties. These are the defaults
this->initBoxControllerProps("5" /*SplitInto*/, 1000 /*SplitThreshold*/, 20 /*MaxRecursionDepth*/);
declareProperty(std::make_unique<PropertyWithValue<double>>("ObliquityParallaxCoefficient", 1.0, Direction::Input),
"Geometrical correction for shift in vertical beam position due to wide beam.");
declareProperty(std::make_unique<WorkspaceProperty<API::IMDEventWorkspace>>("OutputWorkspace", "", Direction::Output),
"An output workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ConvertHFIRSCDtoMDE::exec() {
double wavelength = this->getProperty("Wavelength");
API::IMDHistoWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
auto &expInfo = *(inputWS->getExperimentInfo(static_cast<uint16_t>(0)));
std::string instrument = expInfo.getInstrument()->getName();
std::vector<double> twotheta, azimuthal;
if (instrument == "HB3A") {
const auto &di = expInfo.detectorInfo();
for (size_t x = 0; x < 512; x++) {
for (size_t y = 0; y < 512 * 3; y++) {
size_t n = x + y * 512;
if (!di.isMonitor(n)) {
twotheta.push_back(di.twoTheta(n));
azimuthal.push_back(di.azimuthal(n));
}
}
}
} else { // HB2C
azimuthal = (*(dynamic_cast<Kernel::PropertyWithValue<std::vector<double>> *>(expInfo.getLog("azimuthal"))))();
twotheta = (*(dynamic_cast<Kernel::PropertyWithValue<std::vector<double>> *>(expInfo.getLog("twotheta"))))();
}
auto outputWS = DataObjects::MDEventFactory::CreateMDWorkspace(3, "MDEvent");
Mantid::Geometry::QSample frame;
std::vector<double> minVals = this->getProperty("MinValues");
std::vector<double> maxVals = this->getProperty("MaxValues");
outputWS->addDimension(std::make_shared<Geometry::MDHistoDimension>(
"Q_sample_x", "Q_sample_x", frame, static_cast<coord_t>(minVals[0]), static_cast<coord_t>(maxVals[0]), 1));
outputWS->addDimension(std::make_shared<Geometry::MDHistoDimension>(
"Q_sample_y", "Q_sample_y", frame, static_cast<coord_t>(minVals[1]), static_cast<coord_t>(maxVals[1]), 1));
outputWS->addDimension(std::make_shared<Geometry::MDHistoDimension>(
"Q_sample_z", "Q_sample_z", frame, static_cast<coord_t>(minVals[2]), static_cast<coord_t>(maxVals[2]), 1));
outputWS->setCoordinateSystem(Mantid::Kernel::QSample);
outputWS->initialize();
BoxController_sptr bc = outputWS->getBoxController();
this->setBoxController(bc);
outputWS->splitBox();
auto mdws_mdevt_3 = std::dynamic_pointer_cast<MDEventWorkspace<MDEvent<3>, 3>>(outputWS);
MDEventInserter<MDEventWorkspace<MDEvent<3>, 3>::sptr> inserter(mdws_mdevt_3);
double cop = this->getProperty("ObliquityParallaxCoefficient");
float coeff = static_cast<float>(cop);
float k = boost::math::float_constants::two_pi / static_cast<float>(wavelength);
// check convention to determine the sign of k
std::string convention = Kernel::ConfigService::Instance().getString("Q.convention");
if (convention == "Crystallography") {
k *= -1.f;
}
std::vector<Eigen::Vector3f> q_lab_pre;
q_lab_pre.reserve(azimuthal.size());
for (size_t m = 0; m < azimuthal.size(); ++m) {
auto twotheta_f = static_cast<float>(twotheta[m]);
auto azimuthal_f = static_cast<float>(azimuthal[m]);
q_lab_pre.push_back({-std::sin(twotheta_f) * std::cos(azimuthal_f) * k,
-std::sin(twotheta_f) * std::sin(azimuthal_f) * k * coeff, (1.f - std::cos(twotheta_f)) * k});
}
const auto run = inputWS->getExperimentInfo(0)->run();
for (size_t n = 0; n < inputWS->getDimension(2)->getNBins(); n++) {
auto gon = run.getGoniometerMatrix(n);
Eigen::Matrix3f goniometer;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
goniometer(i, j) = static_cast<float>(gon[i][j]);
goniometer = goniometer.inverse().eval();
auto goniometerIndex = static_cast<uint16_t>(n);
for (size_t m = 0; m < azimuthal.size(); m++) {
size_t idx = n * azimuthal.size() + m;
coord_t signal = static_cast<coord_t>(inputWS->getSignalAt(idx));
if (signal > 0.f && std::isfinite(signal)) {
Eigen::Vector3f q_sample = goniometer * q_lab_pre[m];
inserter.insertMDEvent(signal, signal, 0, goniometerIndex, 0, q_sample.data());
}
}
}
auto *ts = new ThreadSchedulerFIFO();
ThreadPool tp(ts);
outputWS->splitAllIfNeeded(ts);
tp.joinAll();
outputWS->refreshCache();
outputWS->copyExperimentInfos(*inputWS);
auto &outRun = outputWS->getExperimentInfo(0)->mutableRun();
if (outRun.hasProperty("wavelength")) {
outRun.removeLogData("wavelength");
}
outRun.addLogData(new PropertyWithValue<double>("wavelength", wavelength));
outRun.getProperty("wavelength")->setUnits("Angstrom");
auto user_convention = Kernel::ConfigService::Instance().getString("Q.convention");
auto ws_convention = outputWS->getConvention();
if (user_convention != ws_convention) {
auto convention_alg = createChildAlgorithm("ChangeQConvention");
convention_alg->setProperty("InputWorkspace", outputWS);
convention_alg->executeAsChildAlg();
}
setProperty("OutputWorkspace", outputWS);
}
} // namespace Mantid::MDAlgorithms