-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathConvertToTH3.cxx
More file actions
246 lines (212 loc) · 6.36 KB
/
Copy pathConvertToTH3.cxx
File metadata and controls
246 lines (212 loc) · 6.36 KB
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
/// \file
/// \warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
/// Feedback is welcome!
#include <ROOT/Hist/ConversionUtils.hxx>
#include <ROOT/Hist/ConvertToTH3.hxx>
#include <ROOT/RBinIndex.hxx>
#include <ROOT/RHist.hxx>
#include <ROOT/RHistEngine.hxx>
#include <TH3.h>
#include <cassert>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <variant>
#include <vector>
using namespace ROOT::Experimental;
namespace {
template <typename Hist, typename T>
std::unique_ptr<Hist> ConvertToTH3Impl(const RHistEngine<T> &engine)
{
if (engine.GetNDimensions() != 3) {
throw std::invalid_argument("TH3 requires two dimensions");
}
auto ret = std::make_unique<Hist>();
ret->SetDirectory(nullptr);
const auto &axis0 = engine.GetAxes()[0];
ROOT::Experimental::Hist::Internal::ConvertAxis(*ret->GetXaxis(), axis0);
const auto &axis1 = engine.GetAxes()[1];
ROOT::Experimental::Hist::Internal::ConvertAxis(*ret->GetYaxis(), axis1);
const auto &axis2 = engine.GetAxes()[2];
ROOT::Experimental::Hist::Internal::ConvertAxis(*ret->GetZaxis(), axis2);
ret->SetBinsLength();
Double_t *sumw2 = nullptr;
auto copyBinContent = [&ret, &engine, &sumw2](Int_t i, RBinIndex index0, RBinIndex index1, RBinIndex index2) {
if constexpr (std::is_same_v<T, RBinWithError>) {
if (sumw2 == nullptr) {
ret->Sumw2();
sumw2 = ret->GetSumw2()->GetArray();
}
const RBinWithError &c = engine.GetBinContent(index0, index1, index2);
ret->GetArray()[i] = c.fSum;
sumw2[i] = c.fSum2;
} else {
(void)sumw2;
ret->GetArray()[i] = engine.GetBinContent(index0, index1, index2);
}
};
// Copy the bin contents, accounting for TH3 numbering conventions.
for (auto index0 : axis0.GetFullRange()) {
Int_t i0 = 0;
if (index0.IsUnderflow()) {
i0 = 0;
} else if (index0.IsOverflow()) {
i0 = axis0.GetNNormalBins() + 1;
} else {
assert(index0.IsNormal());
i0 = index0.GetIndex() + 1;
}
Int_t n0 = ret->GetXaxis()->GetNbins() + 2;
for (auto index1 : axis1.GetFullRange()) {
Int_t i1 = 0;
if (index1.IsUnderflow()) {
i1 = 0;
} else if (index1.IsOverflow()) {
i1 = axis1.GetNNormalBins() + 1;
} else {
assert(index1.IsNormal());
i1 = index1.GetIndex() + 1;
}
Int_t n1 = ret->GetYaxis()->GetNbins() + 2;
for (auto index2 : axis2.GetFullRange()) {
Int_t i2 = 0;
if (index2.IsUnderflow()) {
i2 = 0;
} else if (index2.IsOverflow()) {
i2 = axis2.GetNNormalBins() + 1;
} else {
assert(index2.IsNormal());
i2 = index2.GetIndex() + 1;
}
Int_t i = i0 + n0 * (i1 + n1 * i2);
copyBinContent(i, index0, index1, index2);
}
}
}
return ret;
}
template <typename Hist>
void ConvertGlobalStatistics(Hist &h, const RHistStats &stats)
{
if (stats.IsTainted()) {
return;
}
h.SetEntries(stats.GetNEntries());
Double_t hStats[11] = {
stats.GetSumW(),
stats.GetSumW2(),
0,
0,
0,
0,
// We do not have sumwxy
0,
0,
0,
// We do not have sumwxz
0,
// We do not have sumwyz
0,
};
if (stats.IsEnabled(0)) {
hStats[2] = stats.GetDimensionStats(0).fSumWX;
hStats[3] = stats.GetDimensionStats(0).fSumWX2;
}
if (stats.IsEnabled(1)) {
hStats[4] = stats.GetDimensionStats(1).fSumWX;
hStats[5] = stats.GetDimensionStats(1).fSumWX2;
}
if (stats.IsEnabled(2)) {
hStats[7] = stats.GetDimensionStats(2).fSumWX;
hStats[8] = stats.GetDimensionStats(2).fSumWX2;
}
h.PutStats(hStats);
}
} // namespace
namespace ROOT {
namespace Experimental {
namespace Hist {
std::unique_ptr<TH3C> ConvertToTH3C(const RHistEngine<char> &engine)
{
return ConvertToTH3Impl<TH3C>(engine);
}
std::unique_ptr<TH3S> ConvertToTH3S(const RHistEngine<short> &engine)
{
return ConvertToTH3Impl<TH3S>(engine);
}
std::unique_ptr<TH3I> ConvertToTH3I(const RHistEngine<int> &engine)
{
return ConvertToTH3Impl<TH3I>(engine);
}
std::unique_ptr<TH3L> ConvertToTH3L(const RHistEngine<long> &engine)
{
return ConvertToTH3Impl<TH3L>(engine);
}
std::unique_ptr<TH3L> ConvertToTH3L(const RHistEngine<long long> &engine)
{
return ConvertToTH3Impl<TH3L>(engine);
}
std::unique_ptr<TH3F> ConvertToTH3F(const RHistEngine<float> &engine)
{
return ConvertToTH3Impl<TH3F>(engine);
}
std::unique_ptr<TH3D> ConvertToTH3D(const RHistEngine<double> &engine)
{
return ConvertToTH3Impl<TH3D>(engine);
}
std::unique_ptr<TH3D> ConvertToTH3D(const RHistEngine<RBinWithError> &engine)
{
return ConvertToTH3Impl<TH3D>(engine);
}
std::unique_ptr<TH3C> ConvertToTH3C(const RHist<char> &hist)
{
auto ret = ConvertToTH3C(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3S> ConvertToTH3S(const RHist<short> &hist)
{
auto ret = ConvertToTH3S(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3I> ConvertToTH3I(const RHist<int> &hist)
{
auto ret = ConvertToTH3I(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3L> ConvertToTH3L(const RHist<long> &hist)
{
auto ret = ConvertToTH3L(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3L> ConvertToTH3L(const RHist<long long> &hist)
{
auto ret = ConvertToTH3L(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3F> ConvertToTH3F(const RHist<float> &hist)
{
auto ret = ConvertToTH3F(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3D> ConvertToTH3D(const RHist<double> &hist)
{
auto ret = ConvertToTH3D(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
std::unique_ptr<TH3D> ConvertToTH3D(const RHist<RBinWithError> &hist)
{
auto ret = ConvertToTH3D(hist.GetEngine());
ConvertGlobalStatistics(*ret, hist.GetStats());
return ret;
}
} // namespace Hist
} // namespace Experimental
} // namespace ROOT