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
6#include <ROOT/RBinIndex.hxx>
7#include <ROOT/RHist.hxx>
9
10#include <TH1.h>
11
12#include <cassert>
13#include <cstddef>
14#include <memory>
15#include <stdexcept>
16#include <type_traits>
17#include <variant>
18#include <vector>
19
20using namespace ROOT::Experimental;
21
22namespace {
23template <typename Hist, typename T>
24std::unique_ptr<Hist> ConvertToTH1Impl(const RHistEngine<T> &engine)
25{
26 if (engine.GetNDimensions() != 1) {
27 throw std::invalid_argument("TH1 requires one dimension");
28 }
29
30 std::unique_ptr<Hist> ret;
31 Double_t *sumw2 = nullptr;
32 auto copyBinContent = [&ret, &engine, &sumw2](Int_t i, RBinIndex index) {
33 if constexpr (std::is_same_v<T, RBinWithError>) {
34 if (sumw2 == nullptr) {
35 ret->Sumw2();
36 sumw2 = ret->GetSumw2()->GetArray();
37 }
38 const RBinWithError &c = engine.GetBinContent(index);
39 ret->GetArray()[i] = c.fSum;
40 sumw2[i] = c.fSum2;
41 } else {
42 (void)sumw2;
43 ret->GetArray()[i] = engine.GetBinContent(index);
44 }
45 };
46
47 const auto &axes = engine.GetAxes();
49 if (auto *regular = std::get_if<RRegularAxis>(&axes[0])) {
50 const std::size_t nNormalBins = regular->GetNNormalBins();
51 ret.reset(new Hist("", "", nNormalBins, regular->GetLow(), regular->GetHigh()));
52
53 // Convert the flow bins, if enabled.
54 if (regular->HasFlowBins()) {
57 }
58
59 // Get the range of normal bins for the loop below.
60 range = regular->GetNormalRange();
61 } else {
62 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
63 }
64
65 assert(ret);
66 ret->SetDirectory(nullptr);
67
68 // Convert the normal bins, accounting for TH1 numbering conventions.
69 for (auto index : range) {
70 copyBinContent(index.GetIndex() + 1, index);
71 }
72
73 return ret;
74}
75
76template <typename Hist>
77void ConvertGlobalStatistics(Hist &h, const RHistStats &stats)
78{
79 h.SetEntries(stats.GetNEntries());
80
81 Double_t hStats[4] = {
82 stats.GetSumW(),
83 stats.GetSumW2(),
84 stats.GetDimensionStats(0).fSumWX,
85 stats.GetDimensionStats(0).fSumWX2,
86 };
87 h.PutStats(hStats);
88}
89} // namespace
90
91namespace ROOT {
92namespace Experimental {
93namespace Hist {
94
95std::unique_ptr<TH1C> ConvertToTH1C(const RHistEngine<char> &engine)
96{
97 return ConvertToTH1Impl<TH1C>(engine);
98}
99
100std::unique_ptr<TH1S> ConvertToTH1S(const RHistEngine<short> &engine)
101{
102 return ConvertToTH1Impl<TH1S>(engine);
103}
104
105std::unique_ptr<TH1I> ConvertToTH1I(const RHistEngine<int> &engine)
106{
107 return ConvertToTH1Impl<TH1I>(engine);
108}
109
110std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long> &engine)
111{
112 return ConvertToTH1Impl<TH1L>(engine);
113}
114
115std::unique_ptr<TH1L> ConvertToTH1L(const RHistEngine<long long> &engine)
116{
117 return ConvertToTH1Impl<TH1L>(engine);
118}
119
120std::unique_ptr<TH1F> ConvertToTH1F(const RHistEngine<float> &engine)
121{
122 return ConvertToTH1Impl<TH1F>(engine);
123}
124
125std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<double> &engine)
126{
127 return ConvertToTH1Impl<TH1D>(engine);
128}
129
130std::unique_ptr<TH1D> ConvertToTH1D(const RHistEngine<RBinWithError> &engine)
131{
132 return ConvertToTH1Impl<TH1D>(engine);
133}
134
135std::unique_ptr<TH1C> ConvertToTH1C(const RHist<char> &hist)
136{
137 auto ret = ConvertToTH1C(hist.GetEngine());
138 ConvertGlobalStatistics(*ret, hist.GetStats());
139 return ret;
140}
141
142std::unique_ptr<TH1S> ConvertToTH1S(const RHist<short> &hist)
143{
144 auto ret = ConvertToTH1S(hist.GetEngine());
145 ConvertGlobalStatistics(*ret, hist.GetStats());
146 return ret;
147}
148
149std::unique_ptr<TH1I> ConvertToTH1I(const RHist<int> &hist)
150{
151 auto ret = ConvertToTH1I(hist.GetEngine());
152 ConvertGlobalStatistics(*ret, hist.GetStats());
153 return ret;
154}
155
156std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long> &hist)
157{
158 auto ret = ConvertToTH1L(hist.GetEngine());
159 ConvertGlobalStatistics(*ret, hist.GetStats());
160 return ret;
161}
162
163std::unique_ptr<TH1L> ConvertToTH1L(const RHist<long long> &hist)
164{
165 auto ret = ConvertToTH1L(hist.GetEngine());
166 ConvertGlobalStatistics(*ret, hist.GetStats());
167 return ret;
168}
169
170std::unique_ptr<TH1F> ConvertToTH1F(const RHist<float> &hist)
171{
172 auto ret = ConvertToTH1F(hist.GetEngine());
173 ConvertGlobalStatistics(*ret, hist.GetStats());
174 return ret;
175}
176
177std::unique_ptr<TH1D> ConvertToTH1D(const RHist<double> &hist)
178{
179 auto ret = ConvertToTH1D(hist.GetEngine());
180 ConvertGlobalStatistics(*ret, hist.GetStats());
181 return ret;
182}
183
184std::unique_ptr<TH1D> ConvertToTH1D(const RHist<RBinWithError> &hist)
185{
186 auto ret = ConvertToTH1D(hist.GetEngine());
187 ConvertGlobalStatistics(*ret, hist.GetStats());
188 return ret;
189}
190
191} // namespace Hist
192} // namespace Experimental
193} // 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:22
static RBinIndex Overflow()
static RBinIndex Underflow()
Histogram statistics of unbinned values.
const RDimensionStats & GetDimensionStats(std::size_t dim=0) const
std::uint64_t GetNEntries() const
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.