/
SANSSolidAngleCorrection.cpp
228 lines (198 loc) · 8.23 KB
/
SANSSolidAngleCorrection.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
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidWorkflowAlgorithms/SANSSolidAngleCorrection.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidKernel/PropertyManagerDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidGeometry/IDetector.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/PropertyManager.h"
namespace Mantid {
namespace WorkflowAlgorithms {
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SANSSolidAngleCorrection)
/// Returns the angle between the sample-to-pixel vector and its
/// projection on the X-Z plane.
static double getYTubeAngle(IDetector_const_sptr det,
MatrixWorkspace_const_sptr workspace) {
// Get the sample position
Geometry::IComponent_const_sptr sample =
workspace->getInstrument()->getSample();
const V3D samplePos = sample->getPos();
// Get the vector from the sample position to the detector pixel
V3D sampleDetVec = det->getPos() - samplePos;
// Get the projection of that vector on the X-Z plane
V3D inPlane = V3D(sampleDetVec);
inPlane.setY(0.0);
// This is the angle between the sample-to-detector vector
// and its project on the X-Z plane.
return sampleDetVec.angle(inPlane);
}
void SANSSolidAngleCorrection::init() {
auto wsValidator = boost::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("Wavelength");
wsValidator->add<HistogramValidator>();
declareProperty(make_unique<WorkspaceProperty<>>(
"InputWorkspace", "", Direction::Input, wsValidator));
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output));
declareProperty("DetectorTubes", false, "If true, the algorithm will assume "
"that the detectors are tubes in the "
"Y direction.");
declareProperty("OutputMessage", "", Direction::Output);
declareProperty("ReductionProperties", "__sans_reduction_properties",
Direction::Input);
}
void SANSSolidAngleCorrection::exec() {
// Reduction property manager
const std::string reductionManagerName = getProperty("ReductionProperties");
boost::shared_ptr<PropertyManager> reductionManager;
if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
reductionManager =
PropertyManagerDataService::Instance().retrieve(reductionManagerName);
} else {
reductionManager = boost::make_shared<PropertyManager>();
PropertyManagerDataService::Instance().addOrReplace(reductionManagerName,
reductionManager);
}
// If the solid angle algorithm isn't in the reduction properties, add it
if (!reductionManager->existsProperty("SolidAngleAlgorithm")) {
auto algProp = make_unique<AlgorithmProperty>("SolidAngleAlgorithm");
algProp->setValue(toString());
reductionManager->declareProperty(std::move(algProp));
}
MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
DataObjects::EventWorkspace_const_sptr inputEventWS =
boost::dynamic_pointer_cast<const EventWorkspace>(inputWS);
if (inputEventWS)
return execEvent();
// Now create the output workspace
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
if (outputWS != inputWS) {
outputWS = WorkspaceFactory::Instance().create(inputWS);
outputWS->setDistribution(true);
outputWS->setYUnit("");
outputWS->setYUnitLabel("Steradian");
setProperty("OutputWorkspace", outputWS);
}
const int numHists = static_cast<int>(inputWS->getNumberHistograms());
Progress progress(this, 0.0, 1.0, numHists);
// Number of X bins
const int xLength = static_cast<int>(inputWS->readY(0).size());
PARALLEL_FOR2(outputWS, inputWS)
for (int i = 0; i < numHists; ++i) {
PARALLEL_START_INTERUPT_REGION
outputWS->dataX(i) = inputWS->readX(i);
IDetector_const_sptr det;
try {
det = inputWS->getDetector(i);
} catch (Exception::NotFoundError &) {
g_log.warning() << "Workspace index " << i
<< " has no detector assigned to it - discarding"
<< std::endl;
// Catch if no detector. Next line tests whether this happened - test
// placed
// outside here because Mac Intel compiler doesn't like 'continue' in a
// catch
// in an openmp block.
}
// If no detector found, skip onto the next spectrum
if (!det)
continue;
// Skip if we have a monitor or if the detector is masked.
if (det->isMonitor() || det->isMasked())
continue;
const MantidVec &YIn = inputWS->readY(i);
const MantidVec &EIn = inputWS->readE(i);
MantidVec &YOut = outputWS->dataY(i);
MantidVec &EOut = outputWS->dataE(i);
// Compute solid angle correction factor
const bool is_tube = getProperty("DetectorTubes");
const double tanTheta = tan(inputWS->detectorTwoTheta(*det));
const double theta_term = sqrt(tanTheta * tanTheta + 1.0);
double corr;
if (is_tube) {
const double tanAlpha = tan(getYTubeAngle(det, inputWS));
const double alpha_term = sqrt(tanAlpha * tanAlpha + 1.0);
corr = alpha_term * theta_term * theta_term;
} else {
corr = theta_term * theta_term * theta_term;
}
// Correct data for all X bins
for (int j = 0; j < xLength; j++) {
YOut[j] = YIn[j] * corr;
EOut[j] = fabs(EIn[j] * corr);
}
progress.report("Solid Angle Correction");
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
setProperty("OutputMessage", "Solid angle correction applied");
}
void SANSSolidAngleCorrection::execEvent() {
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
// generate the output workspace pointer
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
if (outputWS != inputWS) {
outputWS = inputWS->clone();
setProperty("OutputWorkspace", outputWS);
}
auto outputEventWS = boost::dynamic_pointer_cast<EventWorkspace>(outputWS);
const int numberOfSpectra =
static_cast<int>(outputEventWS->getNumberHistograms());
Progress progress(this, 0.0, 1.0, numberOfSpectra);
progress.report("Solid Angle Correction");
PARALLEL_FOR1(outputEventWS)
for (int i = 0; i < numberOfSpectra; i++) {
PARALLEL_START_INTERUPT_REGION
IDetector_const_sptr det;
try {
det = outputEventWS->getDetector(i);
} catch (Exception::NotFoundError &) {
g_log.warning() << "Workspace index " << i
<< " has no detector assigned to it - discarding"
<< std::endl;
// Catch if no detector. Next line tests whether this happened - test
// placed
// outside here because Mac Intel compiler doesn't like 'continue' in a
// catch
// in an openmp block.
}
if (!det)
continue;
// Skip if we have a monitor or if the detector is masked.
if (det->isMonitor() || det->isMasked())
continue;
// Compute solid angle correction factor
const bool is_tube = getProperty("DetectorTubes");
const double tanTheta = tan(outputEventWS->detectorTwoTheta(*det));
const double theta_term = sqrt(tanTheta * tanTheta + 1.0);
double corr;
if (is_tube) {
const double tanAlpha = tan(getYTubeAngle(det, inputWS));
const double alpha_term = sqrt(tanAlpha * tanAlpha + 1.0);
corr = alpha_term * theta_term * theta_term;
} else {
corr = theta_term * theta_term * theta_term;
}
EventList &el = outputEventWS->getEventList(i);
el *= corr;
progress.report("Solid Angle Correction");
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
setProperty("OutputMessage", "Solid angle correction applied");
}
} // namespace WorkflowAlgorithms
} // namespace Mantid