-
Notifications
You must be signed in to change notification settings - Fork 122
/
Rebunch.cpp
324 lines (294 loc) · 9.47 KB
/
Rebunch.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
// 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/Rebunch.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/Workspace_fwd.h"
#include "MantidKernel/BoundedValidator.h"
#include <cmath>
#include <numeric>
#include <sstream>
namespace Mantid {
using namespace HistogramData;
namespace Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(Rebunch)
using namespace Kernel;
using API::MatrixWorkspace;
using API::MatrixWorkspace_const_sptr;
using API::WorkspaceProperty;
/** Initialisation method. Declares properties to be used in algorithm.
*
*/
void Rebunch::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"The input workspace");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The result of rebinning");
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty("NBunch", 1, mustBePositive,
"The number of bins that will be summed in each bunch");
}
/** Executes the rebin algorithm
*
* @throw runtime_error Thrown if
*/
void Rebunch::exec() {
// retrieve the properties
int n_bunch = getProperty("NBunch");
// Get the input workspace
MatrixWorkspace_const_sptr inputW = getProperty("InputWorkspace");
bool dist = inputW->isDistribution();
// workspace independent determination of length
auto histnumber = static_cast<int>(inputW->size() / inputW->blocksize());
auto size_x = static_cast<int>(inputW->x(0).size());
auto size_y = static_cast<int>(inputW->y(0).size());
// signal is the same length for histogram and point data
int ny = (size_y / n_bunch);
if (size_y % n_bunch > 0)
ny += 1;
// default is for hist
int nx = ny + 1;
bool point = false;
if (size_x == size_y) {
point = true;
nx = ny;
}
// 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, nx, ny);
int progress_step = histnumber / 100;
if (progress_step == 0)
progress_step = 1;
PARALLEL_FOR_IF(Kernel::threadSafe(*inputW, *outputW))
for (int hist = 0; hist < histnumber; hist++) {
PARALLEL_START_INTERUPT_REGION
// output data arrays are implicitly filled by function
if (point) {
rebunch_point(inputW->x(hist), inputW->y(hist), inputW->e(hist),
outputW->mutableX(hist), outputW->mutableY(hist),
outputW->mutableE(hist), n_bunch);
} else if (dist) {
rebunch_hist_frequencies(inputW->x(hist), inputW->y(hist),
inputW->e(hist), outputW->mutableX(hist),
outputW->mutableY(hist), outputW->mutableE(hist),
n_bunch);
} else {
rebunch_hist_counts(inputW->x(hist), inputW->y(hist), inputW->e(hist),
outputW->mutableX(hist), outputW->mutableY(hist),
outputW->mutableE(hist), n_bunch);
}
if (hist % progress_step == 0) {
progress(double(hist) / histnumber);
interruption_point();
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
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);
}
/** Rebunches histogram data data according to n_bunch input
*
* @param xold :: old x data
* @param yold :: old y data
* @param eold :: old e data
* @param xnew :: new x data
* @param ynew :: new y data
* @param enew :: new e data
* @param n_bunch :: number of data points to bunch together for each new point
* @throw runtime_error Thrown if algorithm cannot execute
* @throw invalid_argument Thrown if input to function is incorrect
**/
void Rebunch::rebunch_hist_counts(const HistogramX &xold,
const HistogramY &yold,
const HistogramE &eold, HistogramX &xnew,
HistogramY &ynew, HistogramE &enew,
const size_t n_bunch) {
size_t size_x = xold.size();
size_t size_y = yold.size();
double ysum, esum;
size_t hi_index = size_x - 1;
size_t wbins = size_y / n_bunch;
size_t rem = size_y % n_bunch;
size_t i, j;
int i_in = 0;
j = 0;
while (j < wbins) {
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= n_bunch; i++) {
ysum += yold[i_in];
esum += eold[i_in] * eold[i_in];
i_in++;
}
// average contributing x values
ynew[j] = ysum;
enew[j] = sqrt(esum);
j++;
}
if (rem != 0) {
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= rem; i++) {
ysum += yold[i_in];
esum += eold[i_in] * eold[i_in];
i_in++;
}
ynew[j] = ysum;
enew[j] = sqrt(esum);
}
j = 0;
xnew[j] = xold[0];
j++;
for (i = n_bunch; i < hi_index; i += n_bunch) {
xnew[j] = xold[i];
j++;
}
xnew[j] = xold[hi_index];
}
/** Rebunches histogram data data according to n_bunch input
*
* @param xold :: old x data
* @param yold :: old y data
* @param eold :: old e data
* @param xnew :: new x data
* @param ynew :: new y data
* @param enew :: new e data
* @param n_bunch :: number of data points to bunch together for each new point
* @throw runtime_error Thrown if algorithm cannot execute
* @throw invalid_argument Thrown if input to function is incorrect
**/
void Rebunch::rebunch_hist_frequencies(const HistogramX &xold,
const HistogramY &yold,
const HistogramE &eold, HistogramX &xnew,
HistogramY &ynew, HistogramE &enew,
const size_t n_bunch) {
double width;
size_t size_x = xold.size();
size_t size_y = yold.size();
double ysum, esum;
size_t hi_index = size_x - 1;
size_t wbins = size_y / n_bunch;
size_t rem = size_y % n_bunch;
size_t i, j;
int i_in = 0;
j = 0;
while (j < wbins) {
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= n_bunch; i++) {
width = xold[i_in + 1] - xold[i_in];
ysum += yold[i_in] * width;
esum += eold[i_in] * eold[i_in] * width * width;
i_in++;
}
// average contributing x values
ynew[j] = ysum;
enew[j] = sqrt(esum);
j++;
}
if (rem != 0) {
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= rem; i++) {
width = xold[i_in + 1] - xold[i_in];
ysum += yold[i_in] * width;
esum += eold[i_in] * eold[i_in] * width * width;
i_in++;
}
ynew[j] = ysum;
enew[j] = sqrt(esum);
}
j = 0;
xnew[j] = xold[0];
j++;
for (i = n_bunch; i < hi_index; i += n_bunch) {
xnew[j] = xold[i];
j++;
}
xnew[j] = xold[hi_index];
for (i = 0; i < ynew.size(); i++) {
width = xnew[i + 1] - xnew[i];
ynew[i] = ynew[i] / width;
enew[i] = enew[i] / width;
}
}
/** Rebunches point data data according to n_bunch input
*
* @param xold :: old x data
* @param yold :: old y data
* @param eold :: old e data
* @param xnew :: new x data
* @param ynew :: new y data
* @param enew :: new e data
* @param n_bunch :: number of data points to bunch together for each new point
* @throw runtime_error Thrown if algorithm cannot execute
* @throw invalid_argument Thrown if input to function is incorrect
**/
void Rebunch::rebunch_point(const HistogramX &xold, const HistogramY &yold,
const HistogramE &eold, HistogramX &xnew,
HistogramY &ynew, HistogramE &enew,
const size_t n_bunch) {
size_t size_y = yold.size();
double xsum, ysum, esum;
size_t wbins = size_y / n_bunch;
size_t rem = size_y % n_bunch;
size_t i, j;
int i_in = 0;
j = 0;
while (j < wbins) {
xsum = 0.0;
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= n_bunch; i++) {
xsum += xold[i_in];
ysum += yold[i_in];
esum += eold[i_in] * eold[i_in];
i_in++;
}
// average contributing x values
xnew[j] = xsum / static_cast<double>(n_bunch);
ynew[j] = ysum / static_cast<double>(n_bunch);
enew[j] = sqrt(esum) / static_cast<double>(n_bunch);
j++;
}
if (rem != 0) {
xsum = 0.0;
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= rem; i++) {
xsum += xold[i_in];
ysum += yold[i_in];
esum += eold[i_in] * eold[i_in];
i_in++;
}
xnew[j] = xsum / static_cast<double>(rem);
ynew[j] = ysum / static_cast<double>(rem);
enew[j] = sqrt(esum) / static_cast<double>(rem);
}
}
} // namespace Algorithms
} // namespace Mantid