-
Notifications
You must be signed in to change notification settings - Fork 121
/
Bin2DPowderDiffraction.cpp
361 lines (319 loc) · 14.5 KB
/
Bin2DPowderDiffraction.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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/Bin2DPowderDiffraction.h"
#include "MantidAPI/BinEdgeAxis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/SpectraAxisValidator.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/EventList.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/RebinParamsValidator.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VectorHelper.h"
#include <algorithm>
#include <fstream>
#include <stdexcept>
namespace Mantid {
namespace Algorithms {
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
using namespace Mantid::HistogramData;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(Bin2DPowderDiffraction)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string Bin2DPowderDiffraction::name() const { return "Bin2DPowderDiffraction"; }
/// Algorithm's version for identification. @see Algorithm::version
int Bin2DPowderDiffraction::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string Bin2DPowderDiffraction::category() const { return "Diffraction\\Focussing"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string Bin2DPowderDiffraction::summary() const {
return "Bins TOF powder diffraction event data in 2D space.";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void Bin2DPowderDiffraction::init() {
auto wsValidator = std::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("Wavelength");
wsValidator->add<SpectraAxisValidator>();
wsValidator->add<InstrumentValidator>();
wsValidator->add<HistogramValidator>();
declareProperty(
std::make_unique<WorkspaceProperty<EventWorkspace>>("InputWorkspace", "", Direction::Input, wsValidator),
"An input EventWorkspace must be a Histogram workspace, not Point data. "
"X-axis units must be wavelength.");
declareProperty(std::make_unique<WorkspaceProperty<API::Workspace>>("OutputWorkspace", "", Direction::Output),
"An output workspace.");
const std::string docString = "A comma separated list of first bin boundary, width, last bin boundary. "
"Optionally "
"this can be followed by a comma and more widths and last boundary "
"pairs. "
"Negative width values indicate logarithmic binning.";
auto rebinValidator = std::make_shared<RebinParamsValidator>(true);
declareProperty(std::make_unique<ArrayProperty<double>>("dSpaceBinning", rebinValidator), docString);
declareProperty(std::make_unique<ArrayProperty<double>>("dPerpendicularBinning", rebinValidator), docString);
const std::vector<std::string> exts{".txt", ".dat"};
declareProperty(std::make_unique<FileProperty>("BinEdgesFile", "", FileProperty::OptionalLoad, exts),
"Optional: The ascii file containing the list of bin edges. "
"Either this or Axis1- and dPerpendicularBinning need to be specified.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("NormalizeByBinArea", true),
"Normalize the binned workspace by the bin area.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void Bin2DPowderDiffraction::exec() {
m_inputWS = this->getProperty("InputWorkspace");
m_numberOfSpectra = static_cast<int>(m_inputWS->getNumberHistograms());
g_log.debug() << "Number of spectra in input workspace: " << m_numberOfSpectra << "\n";
MatrixWorkspace_sptr outputWS = createOutputWorkspace();
const bool normalizeByBinArea = this->getProperty("NormalizeByBinArea");
if (normalizeByBinArea)
normalizeToBinArea(outputWS);
setProperty("OutputWorkspace", outputWS);
}
//----------------------------------------------------------------------------------------------
/**
* @brief Bin2DPowderDiffraction::validateInputs Validate inputs
* @return
*/
std::map<std::string, std::string> Bin2DPowderDiffraction::validateInputs() {
std::map<std::string, std::string> result;
const auto useBinFile = !getPointerToProperty("BinEdgesFile")->isDefault();
const auto useBinning1 = !getPointerToProperty("dSpaceBinning")->isDefault();
const auto useBinning2 = !getPointerToProperty("dPerpendicularBinning")->isDefault();
if (!useBinFile && !useBinning1 && !useBinning2) {
const std::string msg = "You must specify either dSpaceBinning and "
"dPerpendicularBinning, or a BinEdgesFile.";
result["dSpaceBinning"] = msg;
result["dPerpendicularBinning"] = msg;
result["BinEdgesFile"] = msg;
} else if (useBinFile && (useBinning1 || useBinning2)) {
const std::string msg = "You must specify either dSpaceBinning and "
"dPerpendicularBinning, or a BinEdgesFile, but not both.";
result["BinEdgesFile"] = msg;
}
return result;
}
//----------------------------------------------------------------------------------------------
/**
* @brief createOutputWorkspace create an output workspace and setup axis
* @throw std::runtime_error If theta=0 or cos(theta)<=0
* @return MatrixWorkspace with binned events
*/
MatrixWorkspace_sptr Bin2DPowderDiffraction::createOutputWorkspace() {
using VectorHelper::createAxisFromRebinParams;
bool binsFromFile(false);
size_t dPerpSize = 0;
size_t dSize = 0;
MatrixWorkspace_sptr outputWS;
const auto &spectrumInfo = m_inputWS->spectrumInfo();
const std::string beFileName = getProperty("BinEdgesFile");
if (!beFileName.empty())
binsFromFile = true;
const auto &oldXEdges = m_inputWS->x(0);
BinEdges dBins(oldXEdges.size());
BinEdges dPerpBins(oldXEdges.size());
auto &dPerp = dPerpBins.mutableRawData();
std::vector<std::vector<double>> fileXbins;
// First create the output Workspace filled with zeros
if (binsFromFile) {
dPerp.clear();
ReadBinsFromFile(dPerp, fileXbins);
dPerpSize = dPerp.size();
// unify xbins
dSize = UnifyXBins(fileXbins);
g_log.debug() << "Maximal size of Xbins = " << dSize;
outputWS = WorkspaceFactory::Instance().create(m_inputWS, dPerpSize - 1, dSize, dSize - 1);
g_log.debug() << "Outws has " << outputWS->getNumberHistograms() << " histograms and " << outputWS->blocksize()
<< " bins." << std::endl;
size_t idx = 0;
for (const auto &Xbins : fileXbins) {
g_log.debug() << "Xbins size: " << Xbins.size() << std::endl;
BinEdges binEdges(Xbins);
outputWS->setBinEdges(idx, binEdges);
idx++;
}
} else {
static_cast<void>(createAxisFromRebinParams(getProperty("dSpaceBinning"), dBins.mutableRawData()));
HistogramData::BinEdges binEdges(dBins);
dPerpSize = createAxisFromRebinParams(getProperty("dPerpendicularBinning"), dPerp);
dSize = binEdges.size();
outputWS = WorkspaceFactory::Instance().create(m_inputWS, dPerpSize - 1, dSize, dSize - 1);
for (size_t idx = 0; idx < dPerpSize - 1; idx++)
outputWS->setBinEdges(idx, binEdges);
auto abscissa = std::make_unique<BinEdgeAxis>(dBins.mutableRawData());
outputWS->replaceAxis(0, std::move(abscissa));
}
outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("dSpacing");
auto verticalAxis = std::make_unique<BinEdgeAxis>(dPerp);
auto verticalAxisRaw = verticalAxis.get();
// Meta data
verticalAxis->unit() = UnitFactory::Instance().create("dSpacingPerpendicular");
verticalAxis->title() = "d_p";
outputWS->replaceAxis(1, std::move(verticalAxis));
Progress prog(this, 0.0, 1.0, m_numberOfSpectra);
auto numSpectra = static_cast<int64_t>(m_numberOfSpectra);
std::vector<std::vector<double>> newYValues(dPerpSize - 1, std::vector<double>(dSize - 1, 0.0));
std::vector<std::vector<double>> newEValues(dPerpSize - 1, std::vector<double>(dSize - 1, 0.0));
// fill the workspace with data
g_log.debug() << "newYSize = " << dPerpSize << std::endl;
g_log.debug() << "newXSize = " << dSize << std::endl;
std::vector<double> dp_vec(verticalAxisRaw->getValues());
PARALLEL_FOR_IF(Kernel::threadSafe(*m_inputWS, *outputWS))
for (int64_t snum = 0; snum < numSpectra; ++snum) {
PARALLEL_START_INTERUPT_REGION
if (!spectrumInfo.isMasked(snum)) {
double theta = 0.5 * spectrumInfo.twoTheta(snum);
double sin_theta = sin(theta);
if (sin_theta == 0) {
throw std::runtime_error("Spectrum " + std::to_string(snum) + " has sin(theta)=0. Cannot calculate d-Spacing!");
}
if (cos(theta) <= 0) {
throw std::runtime_error("Spectrum " + std::to_string(snum) +
" has cos(theta) <= 0. Cannot calculate d-SpacingPerpendicular!");
}
double log_cos_theta = log(cos(theta));
EventList &evList = m_inputWS->getSpectrum(snum);
// Switch to weighted if needed.
if (evList.getEventType() == TOF)
evList.switchTo(WEIGHTED);
std::vector<WeightedEvent> events = evList.getWeightedEvents();
for (const auto &ev : events) {
auto d = calcD(ev.tof(), sin_theta);
auto dp = calcDPerp(ev.tof(), log_cos_theta);
const auto lowy = std::lower_bound(dp_vec.begin(), dp_vec.end(), dp);
if ((lowy == dp_vec.end()) || (lowy == dp_vec.begin()))
continue;
int64_t dp_index = std::distance(dp_vec.begin(), lowy) - 1;
auto xs = binsFromFile ? fileXbins[dp_index] : dBins.rawData();
const auto lowx = std::lower_bound(xs.begin(), xs.end(), d);
if ((lowx == xs.end()) || lowx == xs.begin())
continue;
int64_t d_index = std::distance(xs.begin(), lowx) - 1;
// writing to the same vectors is not thread-safe
PARALLEL_CRITICAL(newValues) {
newYValues[dp_index][d_index] += ev.weight();
newEValues[dp_index][d_index] += ev.errorSquared();
}
}
}
prog.report("Binning event data...");
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
size_t idx = 0;
for (const auto &yVec : newYValues) {
outputWS->setCounts(idx, yVec);
idx++;
}
idx = 0;
for (auto &eVec : newEValues) {
std::transform(eVec.begin(), eVec.end(), eVec.begin(), static_cast<double (*)(double)>(sqrt));
outputWS->setCountStandardDeviations(idx, eVec);
idx++;
}
return outputWS;
}
//----------------------------------------------------------------------------------------------
/**
* @brief Bin2DPowderDiffraction::ReadBinsFromFile
* @param[out] Ybins vector of doubles to save the dSpacingPerpendicular bin
* edges
* @param[out] Xbins vector of vectors of doubles to save the dSpacing bin edges
*/
void Bin2DPowderDiffraction::ReadBinsFromFile(std::vector<double> &Ybins,
std::vector<std::vector<double>> &Xbins) const {
const std::string beFileName = getProperty("BinEdgesFile");
std::ifstream file(beFileName);
std::string line;
std::string::size_type n;
std::string::size_type sz;
std::vector<double> tmp;
int dpno = 0;
while (getline(file, line)) {
n = line.find("dp =");
if (n != std::string::npos) {
if (!tmp.empty()) {
Xbins.emplace_back(tmp);
tmp.clear();
}
double dp1 = std::stod(line.substr(4), &sz); // 4 is needed to crop 'dp='
double dp2 = std::stod(line.substr(sz + 4));
if (dpno < 1) {
Ybins.emplace_back(dp1);
Ybins.emplace_back(dp2);
} else {
Ybins.emplace_back(dp2);
}
dpno++;
} else if (line.find("#") == std::string::npos) {
std::stringstream ss(line);
double d;
while (ss >> d) {
tmp.emplace_back(d);
}
}
}
Xbins.emplace_back(tmp);
g_log.information() << "Number of Ybins: " << Ybins.size() << std::endl;
g_log.information() << "Number of Xbins sets: " << Xbins.size() << std::endl;
}
//----------------------------------------------------------------------------------------------
/**
* @brief Bin2DPowderDiffraction::UnifyXBins unifies size of the vectors in
*Xbins.
* Fills the last vector element at the end of the shorter bins.
* Required to avoid garbage values in the X values after ws->setHistogram.
* returns the maximal size
*
* @param[in] Xbins --- bins to unify. Will be overwritten.
*/
size_t Bin2DPowderDiffraction::UnifyXBins(std::vector<std::vector<double>> &Xbins) const {
// get maximal vector size
size_t max_size = 0;
for (const auto &v : Xbins) {
max_size = std::max(v.size(), max_size);
}
// resize all vectors to maximum size, fill last vector element at the end
for (auto &v : Xbins) {
if (v.size() < max_size)
v.resize(max_size, v.back());
}
return max_size;
}
void Bin2DPowderDiffraction::normalizeToBinArea(const MatrixWorkspace_sptr &outWS) {
NumericAxis *verticalAxis = dynamic_cast<NumericAxis *>(outWS->getAxis(1));
const std::vector<double> &yValues = verticalAxis->getValues();
auto nhist = outWS->getNumberHistograms();
g_log.debug() << "Number of hists: " << nhist << " Length of YAxis: " << verticalAxis->length() << std::endl;
for (size_t idx = 0; idx < nhist; ++idx) {
double factor = 1.0 / (yValues[idx + 1] - yValues[idx]);
// divide by the xBinWidth
outWS->convertToFrequencies(idx);
auto &freqs = outWS->mutableY(idx);
using std::placeholders::_1;
std::transform(freqs.begin(), freqs.end(), freqs.begin(), std::bind(std::multiplies<double>(), factor, _1));
auto &errors = outWS->mutableE(idx);
std::transform(errors.begin(), errors.end(), errors.begin(), std::bind(std::multiplies<double>(), factor, _1));
}
}
double calcD(double wavelength, double sintheta) { return wavelength * 0.5 / sintheta; }
double calcDPerp(double wavelength, double logcostheta) { return sqrt(wavelength * wavelength - 2.0 * logcostheta); }
} // namespace Algorithms
} // namespace Mantid