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 h.SetEntries(stats.GetNEntries());
73
74 Double_t hStats[4] = {
75 stats.GetSumW(),
76 stats.GetSumW2(),
77 stats.GetDimensionStats(0).fSumWX,
78 stats.GetDimensionStats(0).fSumWX2,
79 };
80 h.PutStats(hStats);
81}
82} // namespace
83
84namespace ROOT {
85namespace Experimental {
86namespace Hist {
87
88std::unique_ptr<TH1C> ConvertToTH1C(const RHistEngine<char> &engine)
89{
90 return ConvertToTH1Impl<TH1C>(engine);
91}
92
93std::unique_ptr<TH1S> ConvertToTH1S(const RHistEngine<short> &engine)
94{
95 return ConvertToTH1Impl<TH1S>(engine);
96}
97
98std::unique_ptr<TH1I> ConvertToTH1I(const RHistEngine<int> &engine)
99{
100 return ConvertToTH1Impl<TH1I>(engine);
101}
102
103std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long> &engine)
104{
105 return ConvertToTH1Impl<TH1L>(engine);
106}
107
108std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long long> &engine)
109{
110 return ConvertToTH1Impl<TH1L>(engine);
111}
112
113std::unique_ptr<TH1F> ConvertToTH1F(const RHistEngine<float> &engine)
114{
115 return ConvertToTH1Impl<TH1F>(engine);
116}
117
118std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<double> &engine)
119{
120 return ConvertToTH1Impl<TH1D>(engine);
121}
122
123std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<RBinWithError> &engine)
124{
125 return ConvertToTH1Impl<TH1D>(engine);
126}
127
128std::unique_ptr<TH1C> ConvertToTH1C(const RHist<char> &hist)
129{
130 auto ret = ConvertToTH1C(hist.GetEngine());
131 ConvertGlobalStatistics(*ret, hist.GetStats());
132 return ret;
133}
134
135std::unique_ptr<TH1S> ConvertToTH1S(const RHist<short> &hist)
136{
137 auto ret = ConvertToTH1S(hist.GetEngine());
138 ConvertGlobalStatistics(*ret, hist.GetStats());
139 return ret;
140}
141
142std::unique_ptr<TH1I> ConvertToTH1I(const RHist<int> &hist)
143{
144 auto ret = ConvertToTH1I(hist.GetEngine());
145 ConvertGlobalStatistics(*ret, hist.GetStats());
146 return ret;
147}
148
149std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long> &hist)
150{
151 auto ret = ConvertToTH1L(hist.GetEngine());
152 ConvertGlobalStatistics(*ret, hist.GetStats());
153 return ret;
154}
155
156std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long long> &hist)
157{
158 auto ret = ConvertToTH1L(hist.GetEngine());
159 ConvertGlobalStatistics(*ret, hist.GetStats());
160 return ret;
161}
162
163std::unique_ptr<TH1F> ConvertToTH1F(const RHist<float> &hist)
164{
165 auto ret = ConvertToTH1F(hist.GetEngine());
166 ConvertGlobalStatistics(*ret, hist.GetStats());
167 return ret;
168}
169
170std::unique_ptr<TH1D> ConvertToTH1D(const RHist<double> &hist)
171{
172 auto ret = ConvertToTH1D(hist.GetEngine());
173 ConvertGlobalStatistics(*ret, hist.GetStats());
174 return ret;
175}
176
177std::unique_ptr<TH1D> ConvertToTH1D(const RHist<RBinWithError> &hist)
178{
179 auto ret = ConvertToTH1D(hist.GetEngine());
180 ConvertGlobalStatistics(*ret, hist.GetStats());
181 return ret;
182}
183
184} // namespace Hist
185} // namespace Experimental
186} // 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.
A special bin content type to compute the bin error in weighted filling.