-
Notifications
You must be signed in to change notification settings - Fork 122
/
LorentzCorrection.cpp
109 lines (90 loc) · 3.42 KB
/
LorentzCorrection.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
#include "MantidAlgorithms/LorentzCorrection.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidKernel/MultiThreaded.h"
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <cmath>
namespace Mantid {
namespace Algorithms {
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using Mantid::API::WorkspaceProperty;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LorentzCorrection)
/// Algorithm's version for identification. @see Algorithm::version
int LorentzCorrection::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string LorentzCorrection::category() const {
return "Crystal\\Corrections";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string LorentzCorrection::summary() const {
return "Performs a white beam Lorentz Correction";
}
const std::string LorentzCorrection::name() const {
return "LorentzCorrection";
}
/** Initialize the algorithm's properties.
*/
void LorentzCorrection::init() {
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input,
PropertyMode::Mandatory,
boost::make_shared<WorkspaceUnitValidator>("Wavelength")),
"Input workspace to correct in Wavelength.");
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"An output workspace.");
}
/** Execute the algorithm.
*/
void LorentzCorrection::exec() {
MatrixWorkspace_sptr inWS = this->getProperty("InputWorkspace");
auto cloneAlg = this->createChildAlgorithm("CloneWorkspace", 0, 0.1);
cloneAlg->initialize();
cloneAlg->setProperty("InputWorkspace", inWS);
cloneAlg->execute();
Workspace_sptr temp = cloneAlg->getProperty("OutputWorkspace");
MatrixWorkspace_sptr outWS =
boost::dynamic_pointer_cast<MatrixWorkspace>(temp);
const auto numHistos = inWS->getNumberHistograms();
const auto &spectrumInfo = inWS->spectrumInfo();
Progress prog(this, 0, 1, numHistos);
PARALLEL_FOR1(inWS)
for (int64_t i = 0; i < int64_t(numHistos); ++i) {
PARALLEL_START_INTERUPT_REGION
if (!spectrumInfo.hasDetectors(i))
continue;
const double twoTheta = spectrumInfo.twoTheta(i);
const double sinTheta = std::sin(twoTheta / 2);
double sinThetaSq = sinTheta * sinTheta;
auto &inY = inWS->y(i);
auto &outY = outWS->mutableY(i);
auto &outE = outWS->mutableE(i);
const auto points = inWS->points(i);
const auto pos = std::find(cbegin(points), cend(points), 0.0);
if (pos != cend(points)) {
std::stringstream buffer;
buffer << "Cannot have zero values Wavelength. At workspace index: "
<< pos - cbegin(points);
throw std::runtime_error(buffer.str());
}
for (size_t j = 0; j < inY.size(); ++j) {
double weight =
sinThetaSq / (points[j] * points[j] * points[j] * points[j]);
outY[j] *= weight;
outE[j] *= weight;
}
prog.report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
this->setProperty("OutputWorkspace", outWS);
}
} // namespace Algorithms
} // namespace Mantid