/
Regroup.cpp
224 lines (195 loc) · 7.66 KB
/
Regroup.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
// 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 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/Regroup.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/RebinParamsValidator.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <numeric>
namespace Mantid {
using HistogramData::HistogramE;
using HistogramData::HistogramX;
using HistogramData::HistogramY;
namespace Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(Regroup)
using namespace Kernel;
using API::MatrixWorkspace;
using API::MatrixWorkspace_const_sptr;
using API::MatrixWorkspace_sptr;
using API::WorkspaceProperty;
/// Initialisation method. Declares properties to be used in algorithm.
void Regroup::init() {
auto wsVal = std::make_shared<CompositeValidator>();
wsVal->add<API::HistogramValidator>();
wsVal->add<API::CommonBinsValidator>();
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input, wsVal),
"The input workspace.");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
"The result of regrouping.");
declareProperty(std::make_unique<ArrayProperty<double>>("Params", std::make_shared<RebinParamsValidator>()),
"The new approximate bin boundaries in the form: x1,dx1,x2,dx2,...,xn");
}
/** Executes the regroup algorithm
*/
void Regroup::exec() {
// retrieve the properties
std::vector<double> rb_params = getProperty("Params");
// Get the input workspace
MatrixWorkspace_const_sptr inputW = getProperty("InputWorkspace");
const bool dist = inputW->isDistribution();
const auto histnumber = static_cast<int>(inputW->getNumberHistograms());
HistogramData::BinEdges XValues_new(0);
auto &XValues_old = inputW->x(0);
std::vector<int> xoldIndex; // indeces of new x in XValues_old
// create new output X axis
int ntcnew = newAxis(rb_params, XValues_old.rawData(), XValues_new.mutableRawData(), xoldIndex);
// make output Workspace the same type is the input, but with new length of
// signal array
API::MatrixWorkspace_sptr outputW = API::WorkspaceFactory::Instance().create(inputW, histnumber, ntcnew, ntcnew - 1);
int progress_step = histnumber / 100;
if (progress_step == 0)
progress_step = 1;
for (int hist = 0; hist < histnumber; hist++) {
// get const references to input Workspace arrays (no copying)
auto &XValues = inputW->x(hist);
auto &YValues = inputW->y(hist);
auto &YErrors = inputW->e(hist);
// get references to output workspace data (no copying)
auto &YValues_new = outputW->mutableY(hist);
auto &YErrors_new = outputW->mutableE(hist);
// output data arrays are implicitly filled by function
rebin(XValues, YValues, YErrors, xoldIndex, YValues_new, YErrors_new, dist);
outputW->setBinEdges(hist, XValues_new);
if (hist % progress_step == 0) {
progress(double(hist) / histnumber);
interruption_point();
}
}
outputW->setDistribution(dist);
// Copy units
if (outputW->getAxis(0)->unit().get())
outputW->getAxis(0)->unit() = inputW->getAxis(0)->unit();
try {
if (inputW->getAxis(1)->unit().get())
outputW->getAxis(1)->unit() = inputW->getAxis(1)->unit();
} catch (Exception::IndexError &) {
// OK, so this isn't a Workspace2D
}
// Assign it to the output workspace property
setProperty("OutputWorkspace", outputW);
}
/** Regroup the data according to new output X array
*
* @param xold :: old x array of data
* @param yold :: old y array of data
* @param eold :: old error array of data
* @param xoldIndex :: indeces of new x in XValues_old
* @param ynew :: new y array of data
* @param enew :: new error array of data
* @param distribution :: flag defining if distribution data (1) or not (0)
* @throw runtime_error Thrown if algorithm cannot execute
* @throw invalid_argument Thrown if input to function is incorrect
**/
void Regroup::rebin(const HistogramX &xold, const HistogramY &yold, const HistogramE &eold,
const std::vector<int> &xoldIndex, HistogramY &ynew, HistogramE &enew, bool distribution) {
for (int i = 0; i < int(xoldIndex.size() - 1); i++) {
int n = xoldIndex[i]; // start the group
int m = xoldIndex[i + 1]; // end the group
double width = xold[m] - xold[n]; // width of the group
if (width == 0.) {
g_log.error("Zero bin width");
throw std::runtime_error("Zero bin width");
}
/*
* yold contains counts/unit time, ynew contains counts
* enew contains counts**2
*/
if (distribution) {
ynew[i] = 0.;
enew[i] = 0.;
for (int j = n; j < m; j++) {
double wdt = xold[j + 1] - xold[j]; // old bin width
ynew[i] += yold[j] * wdt;
enew[i] += eold[j] * eold[j] * wdt * wdt;
}
ynew[i] /= width;
enew[i] = sqrt(enew[i]) / width;
} else // yold,eold data is not distribution but counts
{
ynew[i] = 0.;
enew[i] = 0.;
for (int j = n; j < m; j++) {
ynew[i] += yold[j];
enew[i] += eold[j] * eold[j];
}
enew[i] = sqrt(enew[i]);
}
}
}
/** Creates a new output X array according to specific boundary defnitions
*
* @param params :: rebin parameters input [x_1, delta_1,x_2, ...
*,x_n-1,delta_n-1,x_n)
* @param xold :: the current x array
* @param xnew :: new output workspace x array
* @param xoldIndex :: indeces of new x in XValues_old
* @return The number of bin boundaries in the new X array
**/
int Regroup::newAxis(const std::vector<double> ¶ms, const std::vector<double> &xold, std::vector<double> &xnew,
std::vector<int> &xoldIndex) {
double xcurr, xs;
int ibound(2), istep(1), inew(0);
auto ibounds = static_cast<int>(params.size()); // highest index in params array containing a bin boundary
int isteps = ibounds - 1; // highest index in params array containing a step
xcurr = params[0];
using std::placeholders::_1;
auto iup = std::find_if(xold.cbegin(), xold.cend(), std::bind(std::greater_equal<double>(), _1, xcurr));
if (iup != xold.end()) {
xcurr = *iup;
xnew.emplace_back(xcurr);
xoldIndex.emplace_back(inew);
inew++;
} else
return 0;
while ((ibound <= ibounds) && (istep <= isteps)) {
// if step is negative then it is logarithmic step
if (params[istep] >= 0.0)
xs = params[istep];
else
xs = xcurr * fabs(params[istep]);
// xcurr += xs;
// find nearest x_i that is >= xcurr
iup = std::find_if(xold.begin(), xold.end(), std::bind(std::greater_equal<double>(), _1, xcurr + xs));
if (iup != xold.end()) {
if (*iup <= params[ibound]) {
xcurr = *iup;
xnew.emplace_back(xcurr);
xoldIndex.emplace_back(inew);
inew++;
} else {
ibound += 2;
istep += 2;
}
} else
return inew;
}
// returns length of new x array or -1 if failure
return inew;
// return( (ibound == ibounds) && (istep == isteps) ? inew : -1 );
}
} // namespace Algorithms
} // namespace Mantid