forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rebin.cpp
161 lines (137 loc) · 5.49 KB
/
Rebin.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
// 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 "MantidHistogramData/Rebin.h"
#include "MantidHistogramData/BinEdges.h"
#include "MantidHistogramData/Exception.h"
#include "MantidHistogramData/Histogram.h"
#include <algorithm>
#include <numeric>
using Mantid::HistogramData::BinEdges;
using Mantid::HistogramData::Counts;
using Mantid::HistogramData::CountStandardDeviations;
using Mantid::HistogramData::CountVariances;
using Mantid::HistogramData::Frequencies;
using Mantid::HistogramData::FrequencyStandardDeviations;
using Mantid::HistogramData::Histogram;
using Mantid::HistogramData::Exception::InvalidBinEdgesError;
namespace {
Histogram rebinCounts(const Histogram &input, const BinEdges &binEdges) {
const auto &xold = input.x();
const auto &yold = input.y();
const auto &eold = input.e();
const auto &xnew = binEdges.rawData();
Counts newCounts(xnew.size() - 1);
CountVariances newCountVariances(xnew.size() - 1);
auto &ynew = newCounts.mutableData();
auto &enew = newCountVariances.mutableData();
const auto size_yold = yold.size();
const auto size_ynew = ynew.size();
size_t iold = 0;
size_t inew = 0;
while ((inew < size_ynew) && (iold < size_yold)) {
const auto xo_low = xold[iold];
const auto xo_high = xold[iold + 1];
const auto xn_low = xnew[inew];
const auto xn_high = xnew[inew + 1];
const auto owidth = xo_high - xo_low;
const auto nwidth = xn_high - xn_low;
if (owidth <= 0.0 || nwidth <= 0.0) {
if (xo_high == -DBL_MAX && xo_low == -DBL_MAX) {
throw InvalidBinEdgesError("One or more x-values was unusually low "
"(below -1e100). This usually occurs when a "
"monitor spectrum has not been masked after "
"ConvertUnits has been run on the workspace");
} else {
throw InvalidBinEdgesError("Negative or zero bin widths not allowed.");
}
}
if (xn_high <= xo_low)
inew++; /* old and new bins do not overlap */
else if (xo_high <= xn_low)
iold++; /* old and new bins do not overlap */
else {
// delta is the overlap of the bins on the x axis
auto delta = xo_high < xn_high ? xo_high : xn_high;
delta -= xo_low > xn_low ? xo_low : xn_low;
ynew[inew] += yold[iold] * delta / owidth;
enew[inew] += eold[iold] * eold[iold] * delta / owidth;
if (xn_high > xo_high) {
iold++;
} else {
inew++;
}
}
}
return Histogram(binEdges, newCounts, CountStandardDeviations(std::move(newCountVariances)));
}
Histogram rebinFrequencies(const Histogram &input, const BinEdges &binEdges) {
const auto &xold = input.x();
const auto &yold = input.y();
const auto &eold = input.e();
const auto &xnew = binEdges.rawData();
Frequencies newFrequencies(xnew.size() - 1);
FrequencyStandardDeviations newFrequencyStdDev(xnew.size() - 1);
auto &ynew = newFrequencies.mutableData();
auto &enew = newFrequencyStdDev.mutableData();
const auto size_yold = yold.size();
const auto size_ynew = ynew.size();
size_t iold = 0;
size_t inew = 0;
while ((inew < size_ynew) && (iold < size_yold)) {
const auto xo_low = xold[iold];
const auto xo_high = xold[iold + 1];
const auto xn_low = xnew[inew];
const auto xn_high = xnew[inew + 1];
const auto owidth = xo_high - xo_low;
const auto nwidth = xn_high - xn_low;
if (owidth <= 0.0 || nwidth <= 0.0)
throw InvalidBinEdgesError("Negative or zero bin widths not allowed.");
if (xn_high <= xo_low)
inew++; /* old and new bins do not overlap */
else if (xo_high <= xn_low)
iold++; /* old and new bins do not overlap */
else {
// delta is the overlap of the bins on the x axis
auto delta = xo_high < xn_high ? xo_high : xn_high;
delta -= xo_low > xn_low ? xo_low : xn_low;
ynew[inew] += yold[iold] * delta;
enew[inew] += eold[iold] * eold[iold] * delta * owidth;
if (xn_high > xo_high) {
iold++;
} else {
inew++;
}
}
}
for (size_t i = 0; i < size_ynew; ++i) {
const auto width = xnew[i + 1] - xnew[i];
const auto factor = 1 / width;
ynew[i] *= factor;
enew[i] = sqrt(enew[i]) * factor;
}
return Histogram(binEdges, newFrequencies, newFrequencyStdDev);
}
} // anonymous namespace
namespace Mantid::HistogramData {
/** Rebins data according to a new set of bin edges.
* @param input :: input histogram data to be rebinned.
* @param binEdges :: input will be rebinned according to this set of bin edges.
* @returns The rebinned histogram.
* @throws std::runtime_error if the input histogram xmode is not BinEdges,
* the input yMode is undefined, or for non-positive input/output bin widths
*/
Histogram rebin(const Histogram &input, const BinEdges &binEdges) {
if (input.xMode() != Histogram::XMode::BinEdges)
throw std::runtime_error("XMode must be Histogram::XMode::BinEdges for input histogram");
if (input.yMode() == Histogram::YMode::Counts)
return rebinCounts(input, binEdges);
else if (input.yMode() == Histogram::YMode::Frequencies)
return rebinFrequencies(input, binEdges);
else
throw std::runtime_error("YMode must be defined for input histogram.");
}
} // namespace Mantid::HistogramData