Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ConvertToTH2.cxx
Go to the documentation of this file.
1/// \file
2/// \warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
3/// Feedback is welcome!
4
7#include <ROOT/RBinIndex.hxx>
8#include <ROOT/RHist.hxx>
10
11#include <TH2.h>
12
13#include <cassert>
14#include <cstddef>
15#include <memory>
16#include <stdexcept>
17#include <type_traits>
18#include <variant>
19#include <vector>
20
21using namespace ROOT::Experimental;
22
23namespace {
24template <typename Hist, typename T>
25std::unique_ptr<Hist> ConvertToTH2Impl(const RHistEngine<T> &engine)
26{
27 if (engine.GetNDimensions() != 2) {
28 throw std::invalid_argument("TH2 requires two dimensions");
29 }
30
31 auto ret = std::make_unique<Hist>();
32 ret->SetDirectory(nullptr);
33
34 const auto &axis0 = engine.GetAxes()[0];
36 const auto &axis1 = engine.GetAxes()[1];
38 ret->SetBinsLength();
39
40 Double_t *sumw2 = nullptr;
41 auto copyBinContent = [&ret, &engine, &sumw2](Int_t i, RBinIndex index0, RBinIndex index1) {
42 if constexpr (std::is_same_v<T, RBinWithError>) {
43 if (sumw2 == nullptr) {
44 ret->Sumw2();
45 sumw2 = ret->GetSumw2()->GetArray();
46 }
47 const RBinWithError &c = engine.GetBinContent(index0, index1);
48 ret->GetArray()[i] = c.fSum;
49 sumw2[i] = c.fSum2;
50 } else {
51 (void)sumw2;
52 ret->GetArray()[i] = engine.GetBinContent(index0, index1);
53 }
54 };
55
56 // Copy the bin contents, accounting for TH2 numbering conventions.
57 for (auto index0 : axis0.GetFullRange()) {
58 Int_t i0 = 0;
59 if (index0.IsUnderflow()) {
60 i0 = 0;
61 } else if (index0.IsOverflow()) {
62 i0 = axis0.GetNNormalBins() + 1;
63 } else {
64 assert(index0.IsNormal());
65 i0 = index0.GetIndex() + 1;
66 }
67 Int_t n0 = ret->GetXaxis()->GetNbins() + 2;
68
69 for (auto index1 : axis1.GetFullRange()) {
70 if (index1.IsUnderflow()) {
72 } else if (index1.IsOverflow()) {
73 copyBinContent(i0 + n0 * (axis1.GetNNormalBins() + 1), index0, index1);
74 } else {
75 assert(index1.IsNormal());
76 copyBinContent(i0 + n0 * (index1.GetIndex() + 1), index0, index1);
77 }
78 }
79 }
80
81 return ret;
82}
83
84template <typename Hist>
85void ConvertGlobalStatistics(Hist &h, const RHistStats &stats)
86{
87 if (stats.IsTainted()) {
88 return;
89 }
90
91 h.SetEntries(stats.GetNEntries());
92
93 Double_t hStats[7] = {
94 stats.GetSumW(),
95 stats.GetSumW2(),
96 0,
97 0,
98 0,
99 0,
100 // We do not have sumwxy
101 0,
102 };
103 if (stats.IsEnabled(0)) {
104 hStats[2] = stats.GetDimensionStats(0).fSumWX;
105 hStats[3] = stats.GetDimensionStats(0).fSumWX2;
106 }
107 if (stats.IsEnabled(1)) {
108 hStats[4] = stats.GetDimensionStats(1).fSumWX;
109 hStats[5] = stats.GetDimensionStats(1).fSumWX2;
110 }
111 h.PutStats(hStats);
112}
113} // namespace
114
115namespace ROOT {
116namespace Experimental {
117namespace Hist {
118
119std::unique_ptr<TH2C> ConvertToTH2C(const RHistEngine<char> &engine)
120{
121 return ConvertToTH2Impl<TH2C>(engine);
122}
123
124std::unique_ptr<TH2S> ConvertToTH2S(const RHistEngine<short> &engine)
125{
126 return ConvertToTH2Impl<TH2S>(engine);
127}
128
129std::unique_ptr<TH2I> ConvertToTH2I(const RHistEngine<int> &engine)
130{
131 return ConvertToTH2Impl<TH2I>(engine);
132}
133
134std::unique_ptr<TH2L> ConvertToTH2L(const RHistEngine<long> &engine)
135{
136 return ConvertToTH2Impl<TH2L>(engine);
137}
138
139std::unique_ptr<TH2L> ConvertToTH2L(const RHistEngine<long long> &engine)
140{
141 return ConvertToTH2Impl<TH2L>(engine);
142}
143
144std::unique_ptr<TH2F> ConvertToTH2F(const RHistEngine<float> &engine)
145{
146 return ConvertToTH2Impl<TH2F>(engine);
147}
148
149std::unique_ptr<TH2D> ConvertToTH2D(const RHistEngine<double> &engine)
150{
151 return ConvertToTH2Impl<TH2D>(engine);
152}
153
154std::unique_ptr<TH2D> ConvertToTH2D(const RHistEngine<RBinWithError> &engine)
155{
156 return ConvertToTH2Impl<TH2D>(engine);
157}
158
159std::unique_ptr<TH2C> ConvertToTH2C(const RHist<char> &hist)
160{
161 auto ret = ConvertToTH2C(hist.GetEngine());
162 ConvertGlobalStatistics(*ret, hist.GetStats());
163 return ret;
164}
165
166std::unique_ptr<TH2S> ConvertToTH2S(const RHist<short> &hist)
167{
168 auto ret = ConvertToTH2S(hist.GetEngine());
169 ConvertGlobalStatistics(*ret, hist.GetStats());
170 return ret;
171}
172
173std::unique_ptr<TH2I> ConvertToTH2I(const RHist<int> &hist)
174{
175 auto ret = ConvertToTH2I(hist.GetEngine());
176 ConvertGlobalStatistics(*ret, hist.GetStats());
177 return ret;
178}
179
180std::unique_ptr<TH2L> ConvertToTH2L(const RHist<long> &hist)
181{
182 auto ret = ConvertToTH2L(hist.GetEngine());
183 ConvertGlobalStatistics(*ret, hist.GetStats());
184 return ret;
185}
186
187std::unique_ptr<TH2L> ConvertToTH2L(const RHist<long long> &hist)
188{
189 auto ret = ConvertToTH2L(hist.GetEngine());
190 ConvertGlobalStatistics(*ret, hist.GetStats());
191 return ret;
192}
193
194std::unique_ptr<TH2F> ConvertToTH2F(const RHist<float> &hist)
195{
196 auto ret = ConvertToTH2F(hist.GetEngine());
197 ConvertGlobalStatistics(*ret, hist.GetStats());
198 return ret;
199}
200
201std::unique_ptr<TH2D> ConvertToTH2D(const RHist<double> &hist)
202{
203 auto ret = ConvertToTH2D(hist.GetEngine());
204 ConvertGlobalStatistics(*ret, hist.GetStats());
205 return ret;
206}
207
208std::unique_ptr<TH2D> ConvertToTH2D(const RHist<RBinWithError> &hist)
209{
210 auto ret = ConvertToTH2D(hist.GetEngine());
211 ConvertGlobalStatistics(*ret, hist.GetStats());
212 return ret;
213}
214
215} // namespace Hist
216} // namespace Experimental
217} // namespace ROOT
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:23
Histogram statistics of unbinned values.
const RDimensionStats & GetDimensionStats(std::size_t dim=0) const
Get the statistics object for one dimension.
std::uint64_t GetNEntries() const
bool IsEnabled(std::size_t dim) const
void ConvertAxis(TAxis &dst, const RAxisVariant &src)
Convert a single axis object to TAxis.
std::unique_ptr< TH2D > ConvertToTH2D(const RHistEngine< double > &engine)
Convert a two-dimensional histogram to TH2D.
std::unique_ptr< TH2F > ConvertToTH2F(const RHistEngine< float > &engine)
Convert a two-dimensional histogram to TH2F.
std::unique_ptr< TH2I > ConvertToTH2I(const RHistEngine< int > &engine)
Convert a two-dimensional histogram to TH2I.
std::unique_ptr< TH2C > ConvertToTH2C(const RHistEngine< char > &engine)
Convert a two-dimensional histogram to TH2C.
std::unique_ptr< TH2L > ConvertToTH2L(const RHistEngine< long > &engine)
Convert a two-dimensional histogram to TH2L.
std::unique_ptr< TH2S > ConvertToTH2S(const RHistEngine< short > &engine)
Convert a two-dimensional histogram to TH2S.
Namespace for ROOT features in testing.
Definition TROOT.h:100