-
Notifications
You must be signed in to change notification settings - Fork 122
/
SofQWNormalisedPolygon.cpp
430 lines (368 loc) · 15.4 KB
/
SofQWNormalisedPolygon.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "MantidAlgorithms/SofQWNormalisedPolygon.h"
#include "MantidAlgorithms/SofQW.h"
#include "MantidAPI/BinEdgeAxis.h"
#include "MantidAPI/NearestNeighbourInfo.h"
#include "MantidAPI/SpectrumDetectorMapping.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/FractionalRebinning.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/DetectorGroup.h"
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidGeometry/Objects/BoundingBox.h"
#include "MantidGeometry/Objects/Object.h"
#include "MantidIndexing/IndexInfo.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VectorHelper.h"
namespace Mantid {
namespace Algorithms {
// Setup typedef for later use
typedef std::map<specnum_t, Mantid::Kernel::V3D> SpectraDistanceMap;
typedef Geometry::IDetector_const_sptr DetConstPtr;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SofQWNormalisedPolygon)
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
/// Default constructor
SofQWNormalisedPolygon::SofQWNormalisedPolygon()
: Rebin2D(), m_Qout(), m_thetaWidth(0.0), m_detNeighbourOffset(-1) {}
/**
* @return the name of the Algorithm
*/
const std::string SofQWNormalisedPolygon::name() const {
return "SofQWNormalisedPolygon";
}
/**
* @return the version number of the Algorithm
*/
int SofQWNormalisedPolygon::version() const { return 1; }
/**
* @return the category list for the Algorithm
*/
const std::string SofQWNormalisedPolygon::category() const {
return "Inelastic\\SofQW";
}
/**
* Initialize the algorithm
*/
void SofQWNormalisedPolygon::init() {
SofQW::createCommonInputProperties(*this);
}
/**
* Execute the algorithm.
*/
void SofQWNormalisedPolygon::exec() {
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
// Do the full check for common binning
if (!WorkspaceHelpers::commonBoundaries(*inputWS)) {
throw std::invalid_argument(
"The input workspace must have common binning across all spectra");
}
RebinnedOutput_sptr outputWS =
this->setUpOutputWorkspace(*inputWS, getProperty("QAxisBinning"), m_Qout);
g_log.debug() << "Workspace type: " << outputWS->id() << '\n';
setProperty("OutputWorkspace", outputWS);
const size_t nEnergyBins = inputWS->blocksize();
const size_t nHistos = inputWS->getNumberHistograms();
// Holds the spectrum-detector mapping
std::vector<std::vector<detid_t>> detIDMapping(
outputWS->getNumberHistograms());
// Progress reports & cancellation
const size_t nreports(nHistos * nEnergyBins);
m_progress = boost::shared_ptr<API::Progress>(
new API::Progress(this, 0.0, 1.0, nreports));
// Compute input caches
m_EmodeProperties.initCachedValues(*inputWS, this);
std::vector<double> par =
inputWS->getInstrument()->getNumberParameter("detector-neighbour-offset");
if (par.empty()) {
// Index theta cache
this->initAngularCachesNonPSD(inputWS);
} else {
g_log.debug() << "Offset: " << par[0] << '\n';
this->m_detNeighbourOffset = static_cast<int>(par[0]);
this->initAngularCachesPSD(inputWS);
}
const auto &X = inputWS->x(0);
int emode = m_EmodeProperties.m_emode;
const auto &inputIndices = inputWS->indexInfo();
const auto &spectrumInfo = inputWS->spectrumInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
for (int64_t i = 0; i < static_cast<int64_t>(nHistos);
++i) // signed for openmp
{
PARALLEL_START_INTERUPT_REGION
if (spectrumInfo.isMasked(i) || spectrumInfo.isMonitor(i)) {
continue;
}
double theta = this->m_theta[i];
double phi = this->m_phi[i];
double thetaWidth = this->m_thetaWidths[i];
double phiWidth = this->m_phiWidths[i];
// Compute polygon points
double thetaHalfWidth = 0.5 * thetaWidth;
double phiHalfWidth = 0.5 * phiWidth;
const double thetaLower = theta - thetaHalfWidth;
const double thetaUpper = theta + thetaHalfWidth;
const double phiLower = phi - phiHalfWidth;
const double phiUpper = phi + phiHalfWidth;
const double efixed = m_EmodeProperties.getEFixed(spectrumInfo.detector(i));
const specnum_t specNo = inputIndices.spectrumNumber(i);
std::stringstream logStream;
for (size_t j = 0; j < nEnergyBins; ++j) {
m_progress->report("Computing polygon intersections");
// For each input polygon test where it intersects with
// the output grid and assign the appropriate weights of Y/E
const double dE_j = X[j];
const double dE_jp1 = X[j + 1];
const double lrQ =
this->calculateQ(efixed, emode, dE_jp1, thetaLower, phiLower);
const V2D ll(dE_j,
this->calculateQ(efixed, emode, dE_j, thetaLower, phiLower));
const V2D lr(dE_jp1, lrQ);
const V2D ur(dE_jp1, this->calculateQ(efixed, emode, dE_jp1, thetaUpper,
phiUpper));
const V2D ul(dE_j,
this->calculateQ(efixed, emode, dE_j, thetaUpper, phiUpper));
if (g_log.is(Logger::Priority::PRIO_DEBUG)) {
logStream << "Spectrum=" << specNo << ", theta=" << theta
<< ",thetaWidth=" << thetaWidth << ", phi=" << phi
<< ", phiWidth=" << phiWidth << ". QE polygon: ll=" << ll
<< ", lr=" << lr << ", ur=" << ur << ", ul=" << ul << "\n";
}
Quadrilateral inputQ = Quadrilateral(ll, lr, ur, ul);
FractionalRebinning::rebinToFractionalOutput(inputQ, inputWS, i, j,
outputWS, m_Qout);
// Find which q bin this point lies in
const MantidVec::difference_type qIndex =
std::upper_bound(m_Qout.begin(), m_Qout.end(), lrQ) - m_Qout.begin();
if (qIndex != 0 && qIndex < static_cast<int>(m_Qout.size())) {
// Add this spectra-detector pair to the mapping
PARALLEL_CRITICAL(SofQWNormalisedPolygon_spectramap) {
detIDMapping[qIndex - 1].push_back(spectrumInfo.detector(i).getID());
}
}
}
if (g_log.is(Logger::Priority::PRIO_DEBUG)) {
g_log.debug(logStream.str());
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
outputWS->finalize();
FractionalRebinning::normaliseOutput(outputWS, inputWS, m_progress);
// Set the output spectrum-detector mapping
auto outputIndices = outputWS->indexInfo();
outputIndices.setDetectorIDs(std::move(detIDMapping));
outputWS->setIndexInfo(outputIndices);
// Replace any NaNs in outputWorkspace with zeroes
if (this->getProperty("ReplaceNaNs")) {
auto replaceNans = this->createChildAlgorithm("ReplaceSpecialValues");
replaceNans->setChild(true);
replaceNans->initialize();
replaceNans->setProperty("InputWorkspace", outputWS);
replaceNans->setProperty("OutputWorkspace", outputWS);
replaceNans->setProperty("NaNValue", 0.0);
replaceNans->setProperty("InfinityValue", 0.0);
replaceNans->setProperty("BigNumberThreshold", DBL_MAX);
replaceNans->execute();
}
}
/**
* Calculate the Q value for a given set of energy transfer, scattering
* and azimuthal angle.
* @param efixed :: An fixed energy value
* @param emode :: the energy evaluation mode
* @param deltaE :: The energy change
* @param twoTheta :: The value of the scattering angle
* @param azimuthal :: The value of the azimuthual angle
* @return The value of Q
*/
double SofQWNormalisedPolygon::calculateQ(const double efixed, int emode,
const double deltaE,
const double twoTheta,
const double azimuthal) const {
double ki = 0.0;
double kf = 0.0;
if (emode == 1) {
ki = std::sqrt(efixed * SofQW::energyToK());
kf = std::sqrt((efixed - deltaE) * SofQW::energyToK());
} else if (emode == 2) {
ki = std::sqrt((deltaE + efixed) * SofQW::energyToK());
kf = std::sqrt(efixed * SofQW::energyToK());
}
const double Qx = ki - kf * std::cos(twoTheta);
const double Qy = -kf * std::sin(twoTheta) * std::cos(azimuthal);
const double Qz = -kf * std::sin(twoTheta) * std::sin(azimuthal);
return std::sqrt(Qx * Qx + Qy * Qy + Qz * Qz);
}
/**
* A map detector ID and Q ranges
* This method looks unnecessary as it could be calculated on the fly but
* the parallelization means that lazy instantation slows it down due to the
* necessary CRITICAL sections required to update the cache. The Q range
* values are required very frequently so the total time is more than
* offset by this precaching step
*/
void SofQWNormalisedPolygon::initAngularCachesNonPSD(
const API::MatrixWorkspace_const_sptr &workspace) {
const size_t nhist = workspace->getNumberHistograms();
this->m_theta = std::vector<double>(nhist);
this->m_thetaWidths = std::vector<double>(nhist);
// Force phi widths to zero
this->m_phi = std::vector<double>(nhist, 0.0);
this->m_phiWidths = std::vector<double>(nhist, 0.0);
auto inst = workspace->getInstrument();
const PointingAlong upDir = inst->getReferenceFrame()->pointingUp();
const auto &spectrumInfo = workspace->spectrumInfo();
for (size_t i = 0; i < nhist; ++i) // signed for OpenMP
{
m_progress->report("Calculating detector angles");
this->m_theta[i] = -1.0; // Indicates a detector to skip
this->m_thetaWidths[i] = -1.0;
// If no detector found, skip onto the next spectrum
if (!spectrumInfo.hasDetectors(i) || spectrumInfo.isMonitor(i)) {
continue;
}
const auto &det = spectrumInfo.detector(i);
// Check to see if there is an EFixed, if not skip it
try {
m_EmodeProperties.getEFixed(det);
} catch (std::runtime_error &) {
continue;
}
this->m_theta[i] = spectrumInfo.twoTheta(i);
/**
* Determine width from shape geometry. A group is assumed to contain
* detectors with the same shape & r, theta value, i.e. a ring mapped-group
* The shape is retrieved and rotated to match the rotation of the detector.
* The angular width is computed using the l2 distance from the sample
*/
Kernel::V3D pos;
boost::shared_ptr<const Object>
shape; // Defined in its own reference frame with centre at 0,0,0
Kernel::Quat rot;
if (spectrumInfo.hasUniqueDetector(i)) {
pos = det.getPos();
shape = det.shape();
rot = det.getRotation();
} else {
// assume they all have same shape and same r,theta
const auto &group = dynamic_cast<const Geometry::DetectorGroup &>(det);
const auto &firstDet = group.getDetectors();
pos = firstDet[0]->getPos();
shape = firstDet[0]->shape();
rot = firstDet[0]->getRotation();
}
double l2(0.0), t(0.0), p(0.0);
pos.getSpherical(l2, t, p);
BoundingBox bbox = shape->getBoundingBox();
auto maxPoint(bbox.maxPoint());
rot.rotate(maxPoint);
double boxWidth = maxPoint[upDir];
m_thetaWidths[i] = std::fabs(2.0 * std::atan(boxWidth / l2));
if (g_log.is(Logger::Priority::PRIO_DEBUG)) {
g_log.debug() << "Detector at spectrum ="
<< workspace->getSpectrum(i).getSpectrumNo()
<< ", width=" << m_thetaWidths[i] * 180.0 / M_PI
<< " degrees\n";
}
}
}
/**
* Function that retrieves the two-theta and azimuthal angles from a given
* detector. It then looks up the nearest neighbours. Using those detectors,
* it calculates the two-theta and azimuthal angle widths.
* @param workspace : the workspace containing the needed detector information
*/
void SofQWNormalisedPolygon::initAngularCachesPSD(
const API::MatrixWorkspace_const_sptr &workspace) {
const size_t nHistos = workspace->getNumberHistograms();
g_log.debug() << "Number of Histograms: " << nHistos << '\n';
bool ignoreMasked = true;
const int numNeighbours = 4;
NearestNeighbourInfo neighbourInfo(*workspace, ignoreMasked, numNeighbours);
this->m_theta = std::vector<double>(nHistos);
this->m_thetaWidths = std::vector<double>(nHistos);
this->m_phi = std::vector<double>(nHistos);
this->m_phiWidths = std::vector<double>(nHistos);
const auto &spectrumInfo = workspace->spectrumInfo();
for (size_t i = 0; i < nHistos; ++i) {
m_progress->report("Calculating detector angular widths");
const auto &detector = spectrumInfo.detector(i);
g_log.debug() << "Current histogram: " << i << '\n';
specnum_t inSpec = workspace->getSpectrum(i).getSpectrumNo();
SpectraDistanceMap neighbours = neighbourInfo.getNeighboursExact(inSpec);
g_log.debug() << "Current ID: " << inSpec << '\n';
// Convert from spectrum numbers to workspace indices
double thetaWidth = -DBL_MAX;
double phiWidth = -DBL_MAX;
// Find theta and phi widths
double theta = spectrumInfo.twoTheta(i);
double phi = detector.getPhi();
specnum_t deltaPlus1 = inSpec + 1;
specnum_t deltaMinus1 = inSpec - 1;
specnum_t deltaPlusT = inSpec + this->m_detNeighbourOffset;
specnum_t deltaMinusT = inSpec - this->m_detNeighbourOffset;
for (auto &neighbour : neighbours) {
specnum_t spec = neighbour.first;
g_log.debug() << "Neighbor ID: " << spec << '\n';
if (spec == deltaPlus1 || spec == deltaMinus1 || spec == deltaPlusT ||
spec == deltaMinusT) {
const auto &detector_n = spectrumInfo.detector(spec - 1);
double theta_n = spectrumInfo.twoTheta(spec - 1) * 0.5;
double phi_n = detector_n.getPhi();
double dTheta = std::fabs(theta - theta_n);
double dPhi = std::fabs(phi - phi_n);
if (dTheta > thetaWidth) {
thetaWidth = dTheta;
g_log.information()
<< "Current ThetaWidth: " << thetaWidth * 180 / M_PI << '\n';
}
if (dPhi > phiWidth) {
phiWidth = dPhi;
g_log.information() << "Current PhiWidth: " << phiWidth * 180 / M_PI
<< '\n';
}
}
}
this->m_theta[i] = theta;
this->m_phi[i] = phi;
this->m_thetaWidths[i] = thetaWidth;
this->m_phiWidths[i] = phiWidth;
}
}
/** Creates the output workspace, setting the axes according to the input
* binning parameters
* @param[in] inputWorkspace The input workspace
* @param[in] binParams The bin parameters from the user
* @param[out] newAxis The 'vertical' axis defined by the given
* parameters
* @return A pointer to the newly-created workspace
*/
RebinnedOutput_sptr SofQWNormalisedPolygon::setUpOutputWorkspace(
const API::MatrixWorkspace &inputWorkspace,
const std::vector<double> &binParams, std::vector<double> &newAxis) {
// Create a vector to temporarily hold the vertical ('y') axis and populate
// that
const int yLength = static_cast<int>(
VectorHelper::createAxisFromRebinParams(binParams, newAxis));
// Create output workspace, bin edges are same as in inputWorkspace index 0
auto outputWorkspace = create<RebinnedOutput>(inputWorkspace, yLength - 1);
// Create a binned numeric axis to replace the default vertical one
Axis *const verticalAxis = new BinEdgeAxis(newAxis);
outputWorkspace->replaceAxis(1, verticalAxis);
// Set the axis units
verticalAxis->unit() = UnitFactory::Instance().create("MomentumTransfer");
verticalAxis->title() = "|Q|";
// Set the X axis title (for conversion to MD)
outputWorkspace->getAxis(0)->title() = "Energy transfer";
outputWorkspace->setYUnit("");
outputWorkspace->setYUnitLabel("Intensity");
return std::move(outputWorkspace);
}
} // namespace Mantid
} // namespace Algorithms