Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ConvertToTH1.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 <TH1.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> ConvertToTH1Impl(const RHistEngine<T> &engine)
26{
27 if (engine.GetNDimensions() != 1) {
28 throw std::invalid_argument("TH1 requires one dimension");
29 }
30
31 auto ret = std::make_unique<Hist>();
32 ret->SetDirectory(nullptr);
33
34 ROOT::Experimental::Hist::Internal::ConvertAxis(*ret->GetXaxis(), engine.GetAxes()[0]);
35 ret->SetBinsLength();
36
37 Double_t *sumw2 = nullptr;
38 auto copyBinContent = [&ret, &engine, &sumw2](Int_t i, RBinIndex index) {
39 if constexpr (std::is_same_v<T, RBinWithError>) {
40 if (sumw2 == nullptr) {
41 ret->Sumw2();
42 sumw2 = ret->GetSumw2()->GetArray();
43 }
44 const RBinWithError &c = engine.GetBinContent(index);
45 ret->GetArray()[i] = c.fSum;
46 sumw2[i] = c.fSum2;
47 } else {
48 (void)sumw2;
49 ret->GetArray()[i] = engine.GetBinContent(index);
50 }
51 };
52
53 // Copy the bin contents, accounting for TH1 numbering conventions.
54 const auto &axis = engine.GetAxes()[0];
55 for (auto index : axis.GetFullRange()) {
56 if (index.IsUnderflow()) {
58 } else if (index.IsOverflow()) {
59 copyBinContent(axis.GetNNormalBins() + 1, index);
60 } else {
61 assert(index.IsNormal());
62 copyBinContent(index.GetIndex() + 1, index);
63 }
64 }
65
66 return ret;
67}
68
69template <typename Hist>
70void ConvertGlobalStatistics(Hist &h, const RHistStats &stats)
71{
72 if (stats.IsTainted()) {
73 return;
74 }
75
76 h.SetEntries(stats.GetNEntries());
77
78 Double_t hStats[4] = {
79 stats.GetSumW(),
80 stats.GetSumW2(),
81 stats.GetDimensionStats(0).fSumWX,
82 stats.GetDimensionStats(0).fSumWX2,
83 };
84 h.PutStats(hStats);
85}
86} // namespace
87
88namespace ROOT {
89namespace Experimental {
90namespace Hist {
91
92std::unique_ptr<TH1C> ConvertToTH1C(const RHistEngine<char> &engine)
93{
94 return ConvertToTH1Impl<TH1C>(engine);
95}
96
97std::unique_ptr<TH1S> ConvertToTH1S(const RHistEngine<short> &engine)
98{
99 return ConvertToTH1Impl<TH1S>(engine);
100}
101
102std::unique_ptr<TH1I> ConvertToTH1I(const RHistEngine<int> &engine)
103{
104 return ConvertToTH1Impl<TH1I>(engine);
105}
106
107std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long> &engine)
108{
109 return ConvertToTH1Impl<TH1L>(engine);
110}
111
112std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long long> &engine)
113{
114 return ConvertToTH1Impl<TH1L>(engine);
115}
116
117std::unique_ptr<TH1F> ConvertToTH1F(const RHistEngine<float> &engine)
118{
119 return ConvertToTH1Impl<TH1F>(engine);
120}
121
122std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<double> &engine)
123{
124 return ConvertToTH1Impl<TH1D>(engine);
125}
126
127std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<RBinWithError> &engine)
128{
129 return ConvertToTH1Impl<TH1D>(engine);
130}
131
132std::unique_ptr<TH1C> ConvertToTH1C(const RHist<char> &hist)
133{
134 auto ret = ConvertToTH1C(hist.GetEngine());
135 ConvertGlobalStatistics(*ret, hist.GetStats());
136 return ret;
137}
138
139std::unique_ptr<TH1S> ConvertToTH1S(const RHist<short> &hist)
140{
141 auto ret = ConvertToTH1S(hist.GetEngine());
142 ConvertGlobalStatistics(*ret, hist.GetStats());
143 return ret;
144}
145
146std::unique_ptr<TH1I> ConvertToTH1I(const RHist<int> &hist)
147{
148 auto ret = ConvertToTH1I(hist.GetEngine());
149 ConvertGlobalStatistics(*ret, hist.GetStats());
150 return ret;
151}
152
153std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long> &hist)
154{
155 auto ret = ConvertToTH1L(hist.GetEngine());
156 ConvertGlobalStatistics(*ret, hist.GetStats());
157 return ret;
158}
159
160std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long long> &hist)
161{
162 auto ret = ConvertToTH1L(hist.GetEngine());
163 ConvertGlobalStatistics(*ret, hist.GetStats());
164 return ret;
165}
166
167std::unique_ptr<TH1F> ConvertToTH1F(const RHist<float> &hist)
168{
169 auto ret = ConvertToTH1F(hist.GetEngine());
170 ConvertGlobalStatistics(*ret, hist.GetStats());
171 return ret;
172}
173
174std::unique_ptr<TH1D> ConvertToTH1D(const RHist<double> &hist)
175{
176 auto ret = ConvertToTH1D(hist.GetEngine());
177 ConvertGlobalStatistics(*ret, hist.GetStats());
178 return ret;
179}
180
181std::unique_ptr<TH1D> ConvertToTH1D(const RHist<RBinWithError> &hist)
182{
183 auto ret = ConvertToTH1D(hist.GetEngine());
184 ConvertGlobalStatistics(*ret, hist.GetStats());
185 return ret;
186}
187
188} // namespace Hist
189} // namespace Experimental
190} // 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.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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
void ConvertAxis(TAxis &dst, const RAxisVariant &src)
Convert a single axis object to TAxis.
std::unique_ptr< TH1I > ConvertToTH1I(const RHistEngine< int > &engine)
Convert a one-dimensional histogram to TH1I.
std::unique_ptr< TH1S > ConvertToTH1S(const RHistEngine< short > &engine)
Convert a one-dimensional histogram to TH1S.
std::unique_ptr< TH1L > ConvertToTH1L(const RHistEngine< long > &engine)
Convert a one-dimensional histogram to TH1L.
std::unique_ptr< TH1F > ConvertToTH1F(const RHistEngine< float > &engine)
Convert a one-dimensional histogram to TH1F.
std::unique_ptr< TH1C > ConvertToTH1C(const RHistEngine< char > &engine)
Convert a one-dimensional histogram to TH1C.
std::unique_ptr< TH1D > ConvertToTH1D(const RHistEngine< double > &engine)
Convert a one-dimensional histogram to TH1D.