/
SANSSolidAngleCorrection.cpp
211 lines (178 loc) · 7.88 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
// 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 "MantidWorkflowAlgorithms/SANSSolidAngleCorrection.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidKernel/PropertyManagerDataService.h"
namespace Mantid::WorkflowAlgorithms {
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SANSSolidAngleCorrection)
namespace {
/// Returns the angle between the sample-to-pixel vector and its
/// projection on the X-Z plane.
static double getYTubeAngle(const SpectrumInfo &spectrumInfo, size_t i) {
const V3D samplePos = spectrumInfo.samplePosition();
// Get the vector from the sample position to the detector pixel
V3D sampleDetVec = spectrumInfo.position(i) - 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);
}
} // namespace
//----------------------------------------------------------------------------------------------
void SANSSolidAngleCorrection::init() {
auto wsValidator = std::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("Wavelength");
wsValidator->add<HistogramValidator>();
declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator));
declareProperty(std::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("DetectorWing", false,
"If true, the algorithm will assume "
"that the detector is curved around the sample. E.g. BIOSANS "
"Wing detector.");
declareProperty("OutputMessage", "", Direction::Output);
declareProperty("ReductionProperties", "__sans_reduction_properties", Direction::Input);
}
void SANSSolidAngleCorrection::exec() {
// Reduction property manager
const std::string reductionManagerName = getProperty("ReductionProperties");
std::shared_ptr<PropertyManager> reductionManager;
if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
} else {
reductionManager = std::make_shared<PropertyManager>();
PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, reductionManager);
}
// If the solid angle algorithm isn't in the reduction properties, add it
if (!reductionManager->existsProperty("SANSSolidAngleCorrection")) {
auto algProp = std::make_unique<AlgorithmProperty>("SANSSolidAngleCorrection");
algProp->setValue(toString());
reductionManager->declareProperty(std::move(algProp));
}
MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
DataObjects::EventWorkspace_const_sptr inputEventWS = std::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 auto numHists = static_cast<int>(inputWS->getNumberHistograms());
Progress progress(this, 0.0, 1.0, numHists);
// Number of X bins
const auto xLength = static_cast<int>(inputWS->y(0).size());
const auto &spectrumInfo = inputWS->spectrumInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(*outputWS, *inputWS))
for (int i = 0; i < numHists; ++i) {
PARALLEL_START_INTERRUPT_REGION
outputWS->setSharedX(i, inputWS->sharedX(i));
if (!spectrumInfo.hasDetectors(i)) {
g_log.warning() << "Workspace index " << i << " has no detector assigned to it - discarding\n";
continue;
}
// Skip if we have a monitor or if the detector is masked.
if (spectrumInfo.isMonitor(i) || spectrumInfo.isMasked(i))
continue;
const auto &YIn = inputWS->y(i);
const auto &EIn = inputWS->e(i);
auto &YOut = outputWS->mutableY(i);
auto &EOut = outputWS->mutableE(i);
// Compute solid angle correction factor
const bool is_tube = getProperty("DetectorTubes");
const bool is_wing = getProperty("DetectorWing");
const double tanTheta = tan(spectrumInfo.twoTheta(i));
const double theta_term = sqrt(tanTheta * tanTheta + 1.0);
double corr;
if (is_tube || is_wing) {
const double tanAlpha = tan(getYTubeAngle(spectrumInfo, i));
const double alpha_term = sqrt(tanAlpha * tanAlpha + 1.0);
if (is_tube)
corr = alpha_term * theta_term * theta_term;
else { // is_wing
corr = alpha_term * alpha_term * alpha_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_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_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 = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
const auto numberOfSpectra = static_cast<int>(outputEventWS->getNumberHistograms());
Progress progress(this, 0.0, 1.0, numberOfSpectra);
progress.report("Solid Angle Correction");
const auto &spectrumInfo = outputEventWS->spectrumInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(*outputEventWS))
for (int i = 0; i < numberOfSpectra; i++) {
PARALLEL_START_INTERRUPT_REGION
if (!spectrumInfo.hasDetectors(i)) {
g_log.warning() << "Workspace index " << i << " has no detector assigned to it - discarding\n";
continue;
}
// Skip if we have a monitor or if the detector is masked.
if (spectrumInfo.isMonitor(i) || spectrumInfo.isMasked(i))
continue;
// Compute solid angle correction factor
const bool is_tube = getProperty("DetectorTubes");
const double tanTheta = tan(spectrumInfo.twoTheta(i));
const double theta_term = sqrt(tanTheta * tanTheta + 1.0);
double corr;
if (is_tube) {
const double tanAlpha = tan(getYTubeAngle(spectrumInfo, i));
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->getSpectrum(i);
el *= corr;
progress.report("Solid Angle Correction");
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
setProperty("OutputMessage", "Solid angle correction applied");
}
} // namespace Mantid::WorkflowAlgorithms