-
Notifications
You must be signed in to change notification settings - Fork 122
/
CorrectKiKf.cpp
307 lines (272 loc) · 10.6 KB
/
CorrectKiKf.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 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/CorrectKiKf.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidGeometry/IDetector.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/UnitFactory.h"
namespace Mantid {
namespace Algorithms {
// Register with the algorithm factory
DECLARE_ALGORITHM(CorrectKiKf)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
using namespace Geometry;
using std::size_t;
/// Initialisation method
void CorrectKiKf::init() {
auto wsValidator = std::make_shared<WorkspaceUnitValidator>("DeltaE");
this->declareProperty(
std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input, wsValidator),
"Name of the input workspace");
this->declareProperty(
std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
"Name of the output workspace, can be the same as the input");
std::vector<std::string> propOptions{"Direct", "Indirect"};
this->declareProperty("EMode", "Direct", std::make_shared<StringListValidator>(propOptions),
"The energy mode (default: Direct)");
auto mustBePositive = std::make_shared<BoundedValidator<double>>();
mustBePositive->setLower(0.0);
this->declareProperty("EFixed", EMPTY_DBL(), mustBePositive,
"Value of fixed energy in meV : EI (EMode=Direct) or "
"EF (EMode=Indirect) .");
}
void CorrectKiKf::exec() {
// Get the workspaces
MatrixWorkspace_const_sptr inputWS = this->getProperty("InputWorkspace");
MatrixWorkspace_sptr outputWS = this->getProperty("OutputWorkspace");
// Check if it is an event workspace
EventWorkspace_const_sptr eventW = std::dynamic_pointer_cast<const EventWorkspace>(inputWS);
if (eventW != nullptr) {
this->execEvent();
return;
}
// If input and output workspaces are not the same, create a new workspace for
// the output using the clone method to account for different workspace types
if (outputWS != inputWS) {
outputWS = inputWS->clone();
}
const size_t size = inputWS->blocksize();
// Calculate the number of spectra in this workspace
const auto numberOfSpectra = static_cast<int>(inputWS->size() / size);
API::Progress prog(this, 0.0, 1.0, numberOfSpectra);
bool negativeEnergyWarning = false;
const std::string emodeStr = getProperty("EMode");
double efixedProp = getProperty("EFixed");
if (efixedProp == EMPTY_DBL()) {
if (emodeStr == "Direct") {
// Check if it has been store on the run object for this workspace
if (inputWS->run().hasProperty("Ei")) {
efixedProp = inputWS->run().getPropertyValueAsType<double>("Ei");
g_log.debug() << "Using stored Ei value " << efixedProp << "\n";
} else {
throw std::invalid_argument("No Ei value has been set or stored within the run information.");
}
} else {
// If not specified, will try to get Ef from the parameter file for
// indirect geometry,
// but it will be done for each spectrum separately, in case of different
// analyzer crystals
}
}
// Get the parameter map
const ParameterMap &pmap = outputWS->constInstrumentParameters();
const auto &spectrumInfo = inputWS->spectrumInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) {
PARALLEL_START_INTERUPT_REGION
double Efi = 0;
// Now get the detector object for this histogram to check if monitor
// or to get Ef for indirect geometry
if (emodeStr == "Indirect") {
if (efixedProp != EMPTY_DBL())
Efi = efixedProp;
// If a DetectorGroup is present should provide a value as a property
// instead
else if (spectrumInfo.hasUniqueDetector(i)) {
getEfixedFromParameterMap(Efi, i, spectrumInfo, pmap);
} else {
g_log.information() << "Workspace Index " << i << ": cannot find detector"
<< "\n";
}
}
auto &yOut = outputWS->mutableY(i);
auto &eOut = outputWS->mutableE(i);
const auto &xIn = inputWS->points(i);
auto &yIn = inputWS->y(i);
auto &eIn = inputWS->e(i);
// Copy the energy transfer axis
outputWS->setSharedX(i, inputWS->sharedX(i));
for (unsigned int j = 0; j < size; ++j) {
const double deltaE = xIn[j];
double Ei = 0.;
double Ef = 0.;
double kioverkf = 1.;
if (emodeStr == "Direct") // Ei=Efixed
{
Ei = efixedProp;
Ef = Ei - deltaE;
} else // Ef=Efixed
{
Ef = Efi;
Ei = Efi + deltaE;
}
// if Ei or Ef is negative, it should be a warning
// however, if the intensity is 0 (histogram goes to energy transfer
// higher than Ei) it is still ok, so no warning.
if ((Ei <= 0) || (Ef <= 0)) {
kioverkf = 0.;
if (yIn[j] != 0)
negativeEnergyWarning = true;
} else
kioverkf = std::sqrt(Ei / Ef);
yOut[j] = yIn[j] * kioverkf;
eOut[j] = eIn[j] * kioverkf;
}
prog.report();
PARALLEL_END_INTERUPT_REGION
} // end for i
PARALLEL_CHECK_INTERUPT_REGION
if (negativeEnergyWarning)
g_log.information() << "Ef <= 0 or Ei <= 0 in at least one spectrum!!!!\n";
if ((negativeEnergyWarning) && (efixedProp == EMPTY_DBL()))
g_log.information() << "Try to set fixed energy\n";
this->setProperty("OutputWorkspace", outputWS);
}
/**
* Execute CorrectKiKf for event workspaces
*
*/
void CorrectKiKf::execEvent() {
g_log.information("Processing event workspace");
const MatrixWorkspace_const_sptr matrixInputWS = getProperty("InputWorkspace");
auto inputWS = std::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS);
// generate the output workspace pointer
API::MatrixWorkspace_sptr matrixOutputWS = getProperty("OutputWorkspace");
if (matrixOutputWS != matrixInputWS) {
matrixOutputWS = matrixInputWS->clone();
setProperty("OutputWorkspace", matrixOutputWS);
}
auto outputWS = std::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS);
const std::string emodeStr = getProperty("EMode");
double efixedProp = getProperty("EFixed"), efixed;
if (efixedProp == EMPTY_DBL()) {
if (emodeStr == "Direct") {
// Check if it has been store on the run object for this workspace
if (inputWS->run().hasProperty("Ei")) {
efixedProp = inputWS->run().getPropertyValueAsType<double>("Ei");
g_log.debug() << "Using stored Ei value " << efixedProp << "\n";
} else {
throw std::invalid_argument("No Ei value has been set or stored within the run information.");
}
} else {
// If not specified, will try to get Ef from the parameter file for
// indirect geometry,
// but it will be done for each spectrum separately, in case of different
// analyzer crystals
}
}
// Get the parameter map
const ParameterMap &pmap = outputWS->constInstrumentParameters();
auto numHistograms = static_cast<int64_t>(inputWS->getNumberHistograms());
const auto &spectrumInfo = inputWS->spectrumInfo();
API::Progress prog = API::Progress(this, 0.0, 1.0, numHistograms);
PARALLEL_FOR_IF(Kernel::threadSafe(*outputWS))
for (int64_t i = 0; i < numHistograms; ++i) {
PARALLEL_START_INTERUPT_REGION
// Now get the detector object for this histogram to check if monitor
// or to get Ef for indirect geometry
if (emodeStr == "Indirect") {
double Efi = 0;
if (efixedProp != EMPTY_DBL()) {
Efi = efixedProp;
// If a DetectorGroup is present should provide a value as a property
// instead
} else if (spectrumInfo.hasUniqueDetector(i)) {
getEfixedFromParameterMap(Efi, i, spectrumInfo, pmap);
} else {
g_log.information() << "Workspace Index " << i << ": cannot find detector"
<< "\n";
}
efixed = Efi;
} else
efixed = efixedProp;
// Do the correction
auto &evlist = outputWS->getSpectrum(i);
switch (evlist.getEventType()) {
case TOF:
// Switch to weights if needed.
evlist.switchTo(WEIGHTED);
/* no break */
// Fall through
case WEIGHTED:
correctKiKfEventHelper(evlist.getWeightedEvents(), efixed, emodeStr);
break;
case WEIGHTED_NOTIME:
correctKiKfEventHelper(evlist.getWeightedEventsNoTime(), efixed, emodeStr);
break;
}
prog.report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
outputWS->clearMRU();
if (inputWS->getNumberEvents() != outputWS->getNumberEvents()) {
g_log.information() << "Ef <= 0 or Ei <= 0 for " << inputWS->getNumberEvents() - outputWS->getNumberEvents()
<< " events, out of " << inputWS->getNumberEvents() << '\n';
if (efixedProp == EMPTY_DBL())
g_log.information() << "Try to set fixed energy\n";
}
}
template <class T>
void CorrectKiKf::correctKiKfEventHelper(std::vector<T> &wevector, double efixed, const std::string &emodeStr) {
double Ei, Ef;
float kioverkf;
typename std::vector<T>::iterator it;
for (it = wevector.begin(); it < wevector.end();) {
if (emodeStr == "Direct") // Ei=Efixed
{
Ei = efixed;
Ef = Ei - it->tof();
} else // Ef=Efixed
{
Ef = efixed;
Ei = Ef + it->tof();
}
// if Ei or Ef is negative, delete the event
if ((Ei <= 0) || (Ef <= 0)) {
it = wevector.erase(it);
} else {
kioverkf = static_cast<float>(std::sqrt(Ei / Ef));
it->m_weight *= kioverkf;
it->m_errorSquared *= kioverkf * kioverkf;
++it;
}
}
}
void CorrectKiKf::getEfixedFromParameterMap(double &Efi, int64_t i, const SpectrumInfo &spectrumInfo,
const ParameterMap &pmap) {
Efi = 0;
if (spectrumInfo.isMonitor(i))
return;
const auto &det = spectrumInfo.detector(i);
Parameter_sptr par = pmap.getRecursive(&det, "Efixed");
if (par) {
Efi = par->value<double>();
g_log.debug() << "Detector: " << det.getID() << " EFixed: " << Efi << "\n";
}
}
} // namespace Algorithms
} // namespace Mantid