-
Notifications
You must be signed in to change notification settings - Fork 122
/
EstimateResolutionDiffraction.cpp
236 lines (188 loc) · 7.13 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
#include "MantidAlgorithms/EstimateResolutionDiffraction.h"
#include "MantidAPI/DetectorInfo.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidGeometry/IDetector.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/V3D.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include <cmath>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using namespace std;
namespace Mantid {
namespace 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.;
}
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 for a powder "
"diffractometer. ";
}
int EstimateResolutionDiffraction::version() const { return 1; }
const std::string EstimateResolutionDiffraction::category() const {
return "Diffraction\\Utility";
}
void EstimateResolutionDiffraction::init() {
declareProperty(
Kernel::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"Name of the workspace to have detector resolution calculated. ");
declareProperty(Kernel::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"Name of the output workspace containing delta(d)/d of each "
"detector/spectrum.");
auto positiveDeltaTOF = boost::make_shared<BoundedValidator<double>>();
positiveDeltaTOF->setLower(0.);
positiveDeltaTOF->setLowerExclusive(true);
declareProperty(
"DeltaTOF", 0., positiveDeltaTOF,
"DeltaT as the resolution of TOF with unit microsecond (10^-6m).");
auto positiveWavelength = boost::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.");
}
/**
*/
void EstimateResolutionDiffraction::exec() {
processAlgProperties();
retrieveInstrumentParameters();
m_outputWS = DataObjects::create<DataObjects::Workspace2D>(
*m_inputWS, HistogramData::Points(1));
estimateDetectorResolution();
setProperty("OutputWorkspace", m_outputWS);
}
/**
*/
void EstimateResolutionDiffraction::processAlgProperties() {
m_inputWS = getProperty("InputWorkspace");
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. ");
TimeSeriesProperty<double> *cwltimeseries =
dynamic_cast<TimeSeriesProperty<double> *>(cwlproperty);
if (!cwltimeseries)
throw runtime_error(
"LambdaReqeust is not a TimeSeriesProperty in double. ");
string unit = cwltimeseries->units();
if (unit.compare("Angstrom") != 0) {
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 &detectorInfo = m_inputWS->detectorInfo();
const auto l1 = spectrumInfo.l1();
g_log.notice() << "L1 = " << l1 << "\n";
const V3D samplepos = spectrumInfo.samplePosition();
size_t numspec = m_inputWS->getNumberHistograms();
double mintwotheta = 10000;
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) {
double dy = realdet->getHeight();
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
double l2 = spectrumInfo.l2(i);
// Calculate T
double centraltof = (l1 + l2) / m_centreVelocity;
// Angle
double twotheta =
spectrumInfo.isMonitor(i) ? 0.0 : spectrumInfo.twoTheta(i);
double theta = 0.5 * twotheta;
double solidangle = 0.0;
for (const auto detID : m_inputWS->getSpectrum(i).getDetectorIDs()) {
const auto index = detectorInfo.indexOf(detID);
if (!detectorInfo.isMasked(index))
solidangle += detectorInfo.detector(index).solidAngle(samplepos);
}
double deltatheta = sqrt(solidangle);
// Resolution
double t1 = m_deltaT / centraltof;
double t2 = detdim / (l1 + l2);
double t3 = deltatheta * (cos(theta) / sin(theta));
double resolution = sqrt(t1 * t1 + t2 * t2 + t3 * t3);
if (spectrumInfo.isMonitor(i))
resolution = 0.0;
m_outputWS->mutableX(i)[0] = static_cast<double>(i);
m_outputWS->mutableY(i)[0] = resolution;
if (twotheta > maxtwotheta)
maxtwotheta = twotheta;
else if (twotheta < mintwotheta)
mintwotheta = twotheta;
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 Algorithms
} // namespace Mantid