-
Notifications
You must be signed in to change notification settings - Fork 122
/
Rebunch.cpp
274 lines (246 loc) · 8.13 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
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/Rebunch.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Workspace_fwd.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/BoundedValidator.h"
#include <cmath>
#include <numeric>
#include <sstream>
namespace Mantid {
namespace Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(Rebunch)
using namespace Kernel;
using API::WorkspaceProperty;
using API::MatrixWorkspace_const_sptr;
using API::MatrixWorkspace;
/** Initialisation method. Declares properties to be used in algorithm.
*
*/
void Rebunch::init() {
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"The input workspace");
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The result of rebinning");
auto mustBePositive = boost::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
int histnumber = static_cast<int>(inputW->size() / inputW->blocksize());
int size_x = static_cast<int>(inputW->readX(0).size());
int size_y = static_cast<int>(inputW->readY(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_FOR2(inputW, outputW)
for (int hist = 0; hist < histnumber; hist++) {
PARALLEL_START_INTERUPT_REGION
// get const references to input Workspace arrays (no copying)
const MantidVec &XValues = inputW->readX(hist);
const MantidVec &YValues = inputW->readY(hist);
const MantidVec &YErrors = inputW->readE(hist);
// get references to output workspace data (no copying)
MantidVec &XValues_new = outputW->dataX(hist);
MantidVec &YValues_new = outputW->dataY(hist);
MantidVec &YErrors_new = outputW->dataE(hist);
// output data arrays are implicitly filled by function
if (point) {
rebunch_point(XValues, YValues, YErrors, XValues_new, YValues_new,
YErrors_new, n_bunch);
} else {
rebunch_hist(XValues, YValues, YErrors, XValues_new, YValues_new,
YErrors_new, n_bunch, dist);
}
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 array of data
* @param xnew :: new x array of data
* @param yold :: old y array of data
* @param ynew :: new y array of data
* @param eold :: old error array of data
* @param enew :: new error array of data
* @param n_bunch :: number of data points to bunch together for each new point
* @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 Rebunch::rebunch_hist(const std::vector<double> &xold,
const std::vector<double> &yold,
const std::vector<double> &eold,
std::vector<double> &xnew, std::vector<double> &ynew,
std::vector<double> &enew, const size_t n_bunch,
const bool distribution) {
size_t i, j;
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;
int i_in = 0;
j = 0;
while (j < wbins) {
ysum = 0.0;
esum = 0.0;
for (i = 1; i <= n_bunch; i++) {
if (distribution) {
width = xold[i_in + 1] - xold[i_in];
ysum += yold[i_in] * width;
esum += eold[i_in] * eold[i_in] * width * width;
i_in++;
} else {
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++) {
if (distribution) {
width = xold[i_in + 1] - xold[i_in];
ysum += yold[i_in] * width;
esum += eold[i_in] * eold[i_in] * width * width;
i_in++;
} else {
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];
if (distribution)
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 array of data
* @param xnew :: new x array of data
* @param yold :: old y array of data
* @param ynew :: new y array of data
* @param eold :: old error array of data
* @param enew :: new error array of 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 std::vector<double> &xold,
const std::vector<double> &yold,
const std::vector<double> &eold,
std::vector<double> &xnew,
std::vector<double> &ynew,
std::vector<double> &enew, const int n_bunch) {
int i, j;
int size_y = static_cast<int>(yold.size());
double xsum, ysum, esum;
int wbins = size_y / n_bunch;
int rem = size_y % n_bunch;
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 Algorithm
} // namespace Mantid