-
Notifications
You must be signed in to change notification settings - Fork 122
/
EstimateResolutionDiffraction.cpp
255 lines (207 loc) · 9.81 KB
/
EstimateResolutionDiffraction.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
// 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 "MantidAlgorithms/EstimateResolutionDiffraction.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidGeometry/IDetector.h"
#include "MantidGeometry/Instrument/ComponentInfo.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/V3D.h"
#include "MantidTypes/SpectrumDefinition.h"
#include <cmath>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using namespace std;
namespace Mantid::Algorithms {
DECLARE_ALGORITHM(EstimateResolutionDiffraction)
namespace { // hide these constants
///
const double MICROSEC_TO_SEC = 1.0E-6;
///
const double WAVELENGTH_TO_VELOCITY = 1.0E10 * PhysicalConstants::h / PhysicalConstants::NeutronMass;
/// This is an absurd number for even ultra cold neutrons
const double WAVELENGTH_MAX = 1000.;
} // namespace
const std::string EstimateResolutionDiffraction::name() const { return "EstimateResolutionDiffraction"; }
const std::string EstimateResolutionDiffraction::alias() const { return "EstimatePDDetectorResolution"; }
const std::string EstimateResolutionDiffraction::summary() const {
return "Estimate the resolution of each detector pixel for a powder "
"diffractometer";
}
int EstimateResolutionDiffraction::version() const { return 1; }
const std::string EstimateResolutionDiffraction::category() const { return "Diffraction\\Utility"; }
void EstimateResolutionDiffraction::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"Name of the workspace to have detector resolution calculated");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("DivergenceWorkspace", "", Direction::Input,
PropertyMode::Optional),
"Workspace containing the divergence");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
"Name of the output workspace containing delta(d)/d of each "
"detector/spectrum");
auto positiveDeltaTOF = std::make_shared<BoundedValidator<double>>();
positiveDeltaTOF->setLower(0.);
positiveDeltaTOF->setLowerExclusive(true);
declareProperty("DeltaTOF", 0., positiveDeltaTOF, "DeltaT as the resolution of TOF with unit microsecond");
auto positiveWavelength = std::make_shared<BoundedValidator<double>>();
positiveWavelength->setLower(0.);
positiveWavelength->setLowerExclusive(true);
declareProperty("Wavelength", EMPTY_DBL(), positiveWavelength,
"Wavelength setting in Angstroms. This overrides what is in "
"the dataset.");
declareProperty(
std::make_unique<WorkspaceProperty<WorkspaceGroup>>("PartialResolutionWorkspaces", "", Direction::Output),
"Workspaces created showing the various resolution terms");
}
void EstimateResolutionDiffraction::exec() {
processAlgProperties();
retrieveInstrumentParameters();
// create all of the output workspaces
std::string partials_prefix = getPropertyValue("PartialResolutionWorkspaces");
m_resTof = DataObjects::create<DataObjects::Workspace2D>(*m_inputWS, HistogramData::Points(1));
m_resPathLength = DataObjects::create<DataObjects::Workspace2D>(*m_inputWS, HistogramData::Points(1));
m_resAngle = DataObjects::create<DataObjects::Workspace2D>(*m_inputWS, HistogramData::Points(1));
m_outputWS = DataObjects::create<DataObjects::Workspace2D>(*m_inputWS, HistogramData::Points(1));
estimateDetectorResolution();
setProperty("OutputWorkspace", m_outputWS);
// put together the output group
auto partialsGroup = std::make_shared<WorkspaceGroup>();
API::AnalysisDataService::Instance().addOrReplace(partials_prefix + "_tof", m_resTof);
API::AnalysisDataService::Instance().addOrReplace(partials_prefix + "_length", m_resPathLength);
API::AnalysisDataService::Instance().addOrReplace(partials_prefix + "_angle", m_resAngle);
partialsGroup->addWorkspace(m_resTof);
partialsGroup->addWorkspace(m_resPathLength);
partialsGroup->addWorkspace(m_resAngle);
setProperty("PartialResolutionWorkspaces", partialsGroup);
}
void EstimateResolutionDiffraction::processAlgProperties() {
m_inputWS = getProperty("InputWorkspace");
m_divergenceWS = getProperty("DivergenceWorkspace");
m_deltaT = getProperty("DeltaTOF");
m_deltaT *= MICROSEC_TO_SEC; // convert to meter
}
double EstimateResolutionDiffraction::getWavelength() {
double wavelength = getProperty("Wavelength");
if (!isEmpty(wavelength)) {
return wavelength;
}
Property *cwlproperty = m_inputWS->run().getProperty("LambdaRequest");
if (!cwlproperty)
throw runtime_error("Unable to locate property LambdaRequest as central wavelength. ");
auto *cwltimeseries = dynamic_cast<TimeSeriesProperty<double> *>(cwlproperty);
if (!cwltimeseries)
throw runtime_error("LambdaReqeust is not a TimeSeriesProperty in double. ");
string unit = cwltimeseries->units();
if ((unit != "Angstrom") && (unit != "A")) {
throw runtime_error("Unit is not recognized: " + unit);
}
return cwltimeseries->timeAverageValue();
}
void EstimateResolutionDiffraction::retrieveInstrumentParameters() {
double centrewavelength = getWavelength();
g_log.notice() << "Centre wavelength = " << centrewavelength << " Angstrom\n";
if (centrewavelength > WAVELENGTH_MAX) {
throw runtime_error("unphysical wavelength used");
}
// Calculate centre neutron velocity
m_centreVelocity = WAVELENGTH_TO_VELOCITY / centrewavelength;
g_log.notice() << "Centre neutron velocity = " << m_centreVelocity << "\n";
}
void EstimateResolutionDiffraction::estimateDetectorResolution() {
const auto &spectrumInfo = m_inputWS->spectrumInfo();
const auto l1 = spectrumInfo.l1();
const auto &componentInfo = m_inputWS->componentInfo();
const auto &detectorInfo = m_inputWS->detectorInfo();
g_log.notice() << "L1 = " << l1 << "\n";
const auto samplepos = spectrumInfo.samplePosition();
const size_t numspec = m_inputWS->getNumberHistograms();
double mintwotheta = 2. * M_PI; // a bit more than 2*pi
double maxtwotheta = 0.;
double mint3 = 1.;
double maxt3 = 0.;
size_t count_nodetsize = 0;
for (size_t i = 0; i < numspec; ++i) {
const auto &det = spectrumInfo.detector(i);
double detdim;
const auto realdet = dynamic_cast<const Detector *>(&det);
if (realdet) {
const double dy = realdet->getHeight();
const double dx = realdet->getWidth();
detdim = sqrt(dx * dx + dy * dy) * 0.5;
} else {
// Use detector dimension as 0 as no-information
detdim = 0;
++count_nodetsize;
}
// Get the distance from detector to source
const double l2 = spectrumInfo.l2(i);
// Calculate T
const double centraltof = (l1 + l2) / m_centreVelocity;
// Angle
const double twotheta = spectrumInfo.isMonitor(i) ? 0.0 : spectrumInfo.twoTheta(i);
const double theta = 0.5 * twotheta;
double deltatheta = 0.;
if (m_divergenceWS) {
deltatheta = m_divergenceWS->y(i)[0];
} else {
auto &spectrumDefinition = spectrumInfo.spectrumDefinition(i);
const double solidangle =
std::accumulate(spectrumDefinition.cbegin(), spectrumDefinition.cend(), 0.,
[&componentInfo, &detectorInfo, &samplepos](const auto sum, const auto &index) {
if (!detectorInfo.isMasked(index.first)) {
return sum + componentInfo.solidAngle(index.first, samplepos);
} else {
return sum;
}
});
deltatheta = sqrt(solidangle);
}
// Resolution
const double t1 = m_deltaT / centraltof;
const double t2 = detdim / (l1 + l2);
const double t3 = deltatheta / tan(theta);
if (spectrumInfo.isMonitor(i)) {
m_resTof->mutableY(i) = 0.;
m_resPathLength->mutableY(i) = 0.;
m_resAngle->mutableY(i) = 0.;
m_outputWS->mutableY(i) = 0.;
} else { // not a monitor
const double resolution = sqrt(t1 * t1 + t2 * t2 + t3 * t3);
m_resTof->mutableY(i) = t1;
m_resPathLength->mutableY(i) = t2;
m_resAngle->mutableY(i) = t3;
m_outputWS->mutableY(i) = resolution;
}
m_resTof->mutableX(i) = static_cast<double>(i);
m_resPathLength->mutableX(i) = static_cast<double>(i);
m_resAngle->mutableX(i) = static_cast<double>(i);
m_outputWS->mutableX(i) = static_cast<double>(i);
maxtwotheta = std::max(twotheta, maxtwotheta);
mintwotheta = std::min(twotheta, mintwotheta);
if (fabs(t3) < mint3)
mint3 = fabs(t3);
else if (fabs(t3) > maxt3)
maxt3 = fabs(t3);
g_log.debug() << det.type() << " " << i << "\t\t" << twotheta << "\t\tdT/T = " << t1 * t1 << "\t\tdL/L = " << t2
<< "\t\tdTheta*cotTheta = " << t3 << "\n";
}
g_log.notice() << "2theta range: " << mintwotheta << ", " << maxtwotheta << "\n";
g_log.notice() << "t3 range: " << mint3 << ", " << maxt3 << "\n";
g_log.notice() << "Number of detector having NO size information = " << count_nodetsize << "\n";
}
} // namespace Mantid::Algorithms