Logo ROOT  
Reference Guide
RDFHistoModels.cxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 09/2017
2
3/*************************************************************************
4 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
12#include <ROOT/TSeq.hxx>
13#include <TProfile.h>
14#include <TProfile2D.h>
15#include <stddef.h>
16#include <vector>
17
18#include "TAxis.h"
19#include "TH1.h"
20#include "TH2.h"
21#include "TH3.h"
22
23/**
24* \class ROOT::RDF::TH1DModel
25* \ingroup dataframe
26* \brief A struct which stores the parameters of a TH1D
27*
28* \class ROOT::RDF::TH2DModel
29* \ingroup dataframe
30* \brief A struct which stores the parameters of a TH2D
31*
32* \class ROOT::RDF::TH3DModel
33* \ingroup dataframe
34* \brief A struct which stores the parameters of a TH3D
35*
36* \class ROOT::RDF::TProfile1DModel
37* \ingroup dataframe
38* \brief A struct which stores the parameters of a TProfile
39*
40* \class ROOT::RDF::TProfile2DModel
41* \ingroup dataframe
42* \brief A struct which stores the parameters of a TProfile2D
43*/
44
45template <typename T>
46inline void FillVector(std::vector<double> &v, int size, T *a)
47{
48 v.reserve(size);
49 for (auto i : ROOT::TSeq<int>(size + 1))
50 v.push_back(a[i]);
51}
52
53template <>
54inline void FillVector<double>(std::vector<double> &v, int size, double *a)
55{
56 v.assign(a, a + (size_t)(size + 1));
57}
58
59inline void SetAxisProperties(const TAxis *axis, double &low, double &up, std::vector<double> &edges)
60{
61 // Check if this histo has fixed binning
62 // Same technique of "Int_t TAxis::FindBin(Double_t)"
63 if (!axis->GetXbins()->fN) {
64 low = axis->GetXmin();
65 up = axis->GetXmax();
66 } else {
67 // This histo has variable binning
68 const auto size = axis->GetNbins() + 1;
69 edges.reserve(size);
70 for (auto i : ROOT::TSeq<int>(1, size))
71 edges.push_back(axis->GetBinLowEdge(i));
72 edges.push_back(axis->GetBinUpEdge(size - 1));
73 }
74}
75
76namespace ROOT {
77
78namespace RDF {
79
80TH1DModel::TH1DModel(const ::TH1D &h) : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX())
81{
83}
84TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup)
85 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup)
86{
87}
88TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const float *xbins)
89 : fName(name), fTitle(title), fNbinsX(nbinsx)
90{
91 FillVector(fBinXEdges, nbinsx, xbins);
92}
93TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const double *xbins)
94 : fName(name), fTitle(title), fNbinsX(nbinsx)
95{
96 FillVector(fBinXEdges, nbinsx, xbins);
97}
98std::shared_ptr<::TH1D> TH1DModel::GetHistogram() const
99{
100 std::shared_ptr<::TH1D> h;
101 if (fBinXEdges.empty())
102 h = std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fXLow, fXUp);
103 else
104 h = std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fBinXEdges.data());
105
106 h->SetDirectory(nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
107 return h;
108}
110{
111}
112
114 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY())
115{
116 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
117 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
118}
119TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
120 double yup)
121 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
122{
123}
124TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy, double ylow,
125 double yup)
126 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
127{
128 FillVector(fBinXEdges, nbinsx, xbins);
129}
130TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
131 const double *ybins)
132 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy)
133{
134 FillVector(fBinYEdges, nbinsy, ybins);
135}
136TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
137 const double *ybins)
138 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
139{
140 FillVector(fBinXEdges, nbinsx, xbins);
141 FillVector(fBinYEdges, nbinsy, ybins);
142}
143TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
144 const float *ybins)
145 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
146{
147 FillVector(fBinXEdges, nbinsx, xbins);
148 FillVector(fBinYEdges, nbinsy, ybins);
149}
150std::shared_ptr<::TH2D> TH2DModel::GetHistogram() const
151{
152 auto xEdgesEmpty = fBinXEdges.empty();
153 auto yEdgesEmpty = fBinYEdges.empty();
154 std::shared_ptr<::TH2D> h;
155 if (xEdgesEmpty && yEdgesEmpty)
156 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp);
157 else if (!xEdgesEmpty && yEdgesEmpty)
158 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fYLow, fYUp);
159 else if (xEdgesEmpty && !yEdgesEmpty)
160 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fBinYEdges.data());
161 else
162 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data());
163
164 h->SetDirectory(nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
165 return h;
166}
168{
169}
170
172 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY()), fNbinsZ(h.GetNbinsZ())
173{
174 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
175 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
176 SetAxisProperties(h.GetZaxis(), fZLow, fZUp, fBinZEdges);
177}
178TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
179 double yup, int nbinsz, double zlow, double zup)
180 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
181 fNbinsZ(nbinsz), fZLow(zlow), fZUp(zup)
182{
183}
184TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
185 const double *ybins, int nbinsz, const double *zbins)
186 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
187{
188 FillVector(fBinXEdges, nbinsx, xbins);
189 FillVector(fBinYEdges, nbinsy, ybins);
190 FillVector(fBinZEdges, nbinsz, zbins);
191}
192TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
193 const float *ybins, int nbinsz, const float *zbins)
194 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
195{
196 FillVector(fBinXEdges, nbinsx, xbins);
197 FillVector(fBinYEdges, nbinsy, ybins);
198 FillVector(fBinZEdges, nbinsz, zbins);
199}
200std::shared_ptr<::TH3D> TH3DModel::GetHistogram() const
201{
202 std::shared_ptr<::TH3D> h;
203 if (fBinXEdges.empty() && fBinYEdges.empty() && fBinZEdges.empty())
204 h = std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fNbinsZ, fZLow, fZUp);
205 else
206 h = std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data(), fNbinsZ,
207 fBinZEdges.data());
208
209 h->SetDirectory(nullptr);
210 return h;
211}
213{
214}
215
216// Profiles
217
219 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
220 fXUp(h.GetXaxis()->GetXmax()), fYLow(h.GetYmin()), fYUp(h.GetYmax()), fOption(h.GetErrorOption())
221{
222 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
223}
224TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup,
225 const char *option)
226 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fOption(option)
227{
228}
229
230TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, double ylow,
231 double yup, const char *option)
232 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fYLow(ylow), fYUp(yup), fOption(option)
233{
234}
235
236TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const float *xbins,
237 const char *option)
238 : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
239{
240 FillVector(fBinXEdges, nbinsx, xbins);
241}
242TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins,
243 const char *option)
244 : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
245{
246 FillVector(fBinXEdges, nbinsx, xbins);
247}
248TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins, double ylow,
249 double yup, const char *option)
250 : fName(name), fTitle(title), fNbinsX(nbinsx), fYLow(ylow), fYUp(yup), fOption(option)
251{
252 FillVector(fBinXEdges, nbinsx, xbins);
253}
254std::shared_ptr<::TProfile> TProfile1DModel::GetProfile() const
255{
256 std::shared_ptr<::TProfile> prof;
257
258 if (fBinXEdges.empty())
259 prof = std::make_shared<::TProfile>(fName, fTitle, fNbinsX, fXLow, fXUp, fYLow, fYUp, fOption);
260 else
261 prof = std::make_shared<::TProfile>(fName, fTitle, fNbinsX, fBinXEdges.data(), fYLow, fYUp, fOption);
262
263 prof->SetDirectory(nullptr); // lifetime is managed by the shared_ptr, detach from ROOT's memory management
264 return prof;
265}
267{
268}
269
271 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
272 fXUp(h.GetXaxis()->GetXmax()), fNbinsY(h.GetNbinsY()), fYLow(h.GetYaxis()->GetXmin()),
273 fYUp(h.GetYaxis()->GetXmax()), fZLow(h.GetZmin()), fZUp(h.GetZmax()), fOption(h.GetErrorOption())
274{
275 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
276 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
277}
278TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
279 double ylow, double yup, const char *option)
280 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
281 fOption(option)
282{
283}
284
285TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
286 double ylow, double yup, double zlow, double zup, const char *option)
287 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
288 fZLow(zlow), fZUp(zup), fOption(option)
289{
290}
291
292TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
293 double ylow, double yup, const char *option)
294 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup), fOption(option)
295{
296 FillVector(fBinXEdges, nbinsx, xbins);
297}
298
299TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
300 const double *ybins, const char *option)
301 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fOption(option)
302{
303 FillVector(fBinYEdges, nbinsy, ybins);
304}
305
306TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
307 const double *ybins, const char *option)
308 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fOption(option)
309{
310 FillVector(fBinXEdges, nbinsx, xbins);
311 FillVector(fBinYEdges, nbinsy, ybins);
312}
313
314std::shared_ptr<::TProfile2D> TProfile2DModel::GetProfile() const
315{
316 // In this method we decide how to build the profile based on the input given in the constructor of the model.
317 // There are 4 cases:
318 // 1. No custom binning for both the x and y axes: we return a profile with equally spaced binning
319 // 2./3. Custom binning only for x(y): we return a profile with custom binning for x(y) and equally spaced for y(x).
320 // 4. No custom binning: we return a profile with equally spaced bins on both axes
321 auto xEdgesEmpty = fBinXEdges.empty();
322 auto yEdgesEmpty = fBinYEdges.empty();
323 std::shared_ptr<::TProfile2D> prof;
324 if (xEdgesEmpty && yEdgesEmpty) {
325 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fZLow, fZUp,
326 fOption);
327 } else if (!xEdgesEmpty && yEdgesEmpty) {
328 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fYLow, fYUp, fOption);
329 } else if (xEdgesEmpty && !yEdgesEmpty) {
330 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fBinYEdges.data(), fOption);
331 } else {
332 prof =
333 std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data(), fOption);
334 }
335
336 prof->SetDirectory(
337 nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
338 return prof;
339}
340
342{
343}
344
345} // ns RDF
346
347} // ns ROOT
void FillVector< double >(std::vector< double > &v, int size, double *a)
void FillVector(std::vector< double > &v, int size, T *a)
void SetAxisProperties(const TAxis *axis, double &low, double &up, std::vector< double > &edges)
#define h(i)
Definition: RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
char name[80]
Definition: TGX11.cxx:110
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
Int_t fN
Definition: TArray.h:38
Class to manage histogram axis.
Definition: TAxis.h:30
const TArrayD * GetXbins() const
Definition: TAxis.h:130
Double_t GetXmax() const
Definition: TAxis.h:134
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition: TAxis.cxx:518
Double_t GetXmin() const
Definition: TAxis.h:133
Int_t GetNbins() const
Definition: TAxis.h:121
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition: TAxis.cxx:528
double T(double x)
Definition: ChebyshevPol.h:34
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
const double xbins[xbins_n]
std::shared_ptr<::TH1D > GetHistogram() const
std::vector< double > fBinXEdges
Definition: HistoModels.hxx:33
std::vector< double > fBinYEdges
Definition: HistoModels.hxx:55
std::vector< double > fBinXEdges
Definition: HistoModels.hxx:54
std::shared_ptr<::TH2D > GetHistogram() const
std::vector< double > fBinZEdges
Definition: HistoModels.hxx:84
std::vector< double > fBinYEdges
Definition: HistoModels.hxx:83
std::vector< double > fBinXEdges
Definition: HistoModels.hxx:82
std::shared_ptr<::TH3D > GetHistogram() const
std::shared_ptr<::TProfile > GetProfile() const
std::vector< double > fBinXEdges
std::vector< double > fBinXEdges
std::shared_ptr<::TProfile2D > GetProfile() const
std::vector< double > fBinYEdges
auto * a
Definition: textangle.C:12