Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <cstddef>
16#include <vector>
17
18#include "TAxis.h"
19#include "TH1.h"
20#include "TH2.h"
21#include "TH3.h"
22#include "THn.h"
23
24/**
25 * \class ROOT::RDF::TH1DModel
26 * \ingroup dataframe
27 * \brief A struct which stores some basic parameters of a TH1D
28 * \note It stores only basic settings such as name, title, bins, bin edges,
29 * but not others such as fSumw2.
30 *
31 * \class ROOT::RDF::TH2DModel
32 * \ingroup dataframe
33 * \brief A struct which stores some basic parameters of a TH2D
34 * \note It stores only basic settings such as name, title, bins, bin edges,
35 * but not others such as fSumw2.
36 *
37 * \class ROOT::RDF::TH3DModel
38 * \ingroup dataframe
39 * \brief A struct which stores some basic parameters of a TH3D
40 * \note It stores only basic settings such as name, title, bins, bin edges,
41 * but not others such as fSumw2.
42 *
43 * \class ROOT::RDF::THnDModel
44 * \ingroup dataframe
45 * \brief A struct which stores some basic parameters of a THnD
46 * \note It stores only basic settings such as name, title, bins, bin edges,
47 * but not others such as fSumw2.
48 *
49 * \class ROOT::RDF::TProfile1DModel
50 * \ingroup dataframe
51 * \brief A struct which stores some basic parameters of a TProfile
52 * \note It stores only basic settings such as name, title, bins, bin edges,
53 * but not others such as fSumw2.
54 *
55 * \class ROOT::RDF::TProfile2DModel
56 * \ingroup dataframe
57 * \brief A struct which stores some basic parameters of a TProfile2D
58 * \note It stores only basic settings such as name, title, bins, bin edges,
59 * but not others such as fSumw2.
60 */
61
62template <typename T>
63inline void FillVector(std::vector<double> &v, int size, T *a)
64{
65 v.reserve(size);
66 for (auto i : ROOT::TSeq<int>(size + 1))
67 v.push_back(a[i]);
68}
69
70template <>
71inline void FillVector<double>(std::vector<double> &v, int size, double *a)
72{
73 v.assign(a, a + (size_t)(size + 1));
74}
75
76inline void SetAxisProperties(const TAxis *axis, double &low, double &up, std::vector<double> &edges)
77{
78 // Check if this histo has fixed binning
79 // Same technique of "Int_t TAxis::FindBin(Double_t)"
80 if (!axis->GetXbins()->fN) {
81 low = axis->GetXmin();
82 up = axis->GetXmax();
83 } else {
84 // This histo has variable binning
85 const auto size = axis->GetNbins() + 1;
86 edges.reserve(size);
87 for (auto i : ROOT::TSeq<int>(1, size))
88 edges.push_back(axis->GetBinLowEdge(i));
89 edges.push_back(axis->GetBinUpEdge(size - 1));
90 }
91}
92
93namespace ROOT {
94
95namespace RDF {
96
97TH1DModel::TH1DModel(const ::TH1D &h) : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX())
98{
100}
101TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup)
102 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup)
103{
104}
105TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const float *xbins)
106 : fName(name), fTitle(title), fNbinsX(nbinsx)
107{
109}
110TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const double *xbins)
111 : fName(name), fTitle(title), fNbinsX(nbinsx)
112{
114}
115std::shared_ptr<::TH1D> TH1DModel::GetHistogram() const
116{
117 std::shared_ptr<::TH1D> h;
118 if (fBinXEdges.empty())
119 h = std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fXLow, fXUp);
120 else
121 h = std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fBinXEdges.data());
122
123 h->SetDirectory(nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
124 return h;
125}
126TH1DModel::~TH1DModel()
127{
128}
129
130TH2DModel::TH2DModel(const ::TH2D &h)
131 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY())
132{
133 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
134 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
135}
136TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
137 double yup)
138 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
139{
140}
141TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy, double ylow,
142 double yup)
143 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
144{
146}
147TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
148 const double *ybins)
149 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy)
150{
152}
153TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
154 const double *ybins)
155 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
156{
159}
160TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
161 const float *ybins)
162 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
163{
166}
167std::shared_ptr<::TH2D> TH2DModel::GetHistogram() const
168{
169 auto xEdgesEmpty = fBinXEdges.empty();
170 auto yEdgesEmpty = fBinYEdges.empty();
171 std::shared_ptr<::TH2D> h;
173 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp);
174 else if (!xEdgesEmpty && yEdgesEmpty)
175 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fYLow, fYUp);
176 else if (xEdgesEmpty && !yEdgesEmpty)
177 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fBinYEdges.data());
178 else
179 h = std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data());
180
181 h->SetDirectory(nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
182 return h;
183}
184TH2DModel::~TH2DModel()
185{
186}
187
188TH3DModel::TH3DModel(const ::TH3D &h)
189 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY()), fNbinsZ(h.GetNbinsZ())
190{
191 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
192 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
193 SetAxisProperties(h.GetZaxis(), fZLow, fZUp, fBinZEdges);
194}
195TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
196 double yup, int nbinsz, double zlow, double zup)
197 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
198 fNbinsZ(nbinsz), fZLow(zlow), fZUp(zup)
199{
200}
201TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
202 const double *ybins, int nbinsz, const double *zbins)
203 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
204{
208}
209TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
210 const float *ybins, int nbinsz, const float *zbins)
211 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
212{
216}
217std::shared_ptr<::TH3D> TH3DModel::GetHistogram() const
218{
219 std::shared_ptr<::TH3D> h;
220 if (fBinXEdges.empty() && fBinYEdges.empty() && fBinZEdges.empty())
221 h = std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fNbinsZ, fZLow, fZUp);
222 else
223 h = std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data(), fNbinsZ,
224 fBinZEdges.data());
225
226 h->SetDirectory(nullptr);
227 return h;
228}
229TH3DModel::~TH3DModel()
230{
231}
232
233THnDModel::THnDModel(const ::THnD &h)
234 : fName(h.GetName()), fTitle(h.GetTitle()), fDim(h.GetNdimensions()), fNbins(fDim), fXmin(fDim), fXmax(fDim),
235 fBinEdges(fDim)
236{
237 for (int idim = 0; idim < fDim; ++idim) {
238 fNbins[idim] = h.GetAxis(idim)->GetNbins();
240 }
241}
242
243THnDModel::THnDModel(const char *name, const char *title, int dim, const int *nbins, const double *xmin,
244 const double *xmax)
245 : fName(name), fTitle(title), fDim(dim), fBinEdges(dim)
246{
247 fNbins.reserve(fDim);
248 fXmin.reserve(fDim);
249 fXmax.reserve(fDim);
250 for (int idim = 0; idim < fDim; ++idim) {
251 fNbins.push_back(nbins[idim]);
252 fXmin.push_back(xmin[idim]);
253 fXmax.push_back(xmax[idim]);
254 }
255}
256
257THnDModel::THnDModel(const char *name, const char *title, int dim, const std::vector<int> &nbins,
258 const std::vector<double> &xmin, const std::vector<double> &xmax)
259 : fName(name), fTitle(title), fDim(dim), fNbins(nbins), fXmin(xmin), fXmax(xmax), fBinEdges(dim)
260{
261}
262
263THnDModel::THnDModel(const char *name, const char *title, int dim, const int *nbins,
264 const std::vector<std::vector<double>> &xbins)
265 : fName(name), fTitle(title), fDim(dim), fXmin(dim, 0.), fXmax(dim, 64.), fBinEdges(xbins)
266{
267 fNbins.reserve(fDim);
268 for (int idim = 0; idim < fDim; ++idim) {
269 fNbins.push_back(nbins[idim]);
270 }
271}
272
273THnDModel::THnDModel(const char *name, const char *title, int dim, const std::vector<int> &nbins,
274 const std::vector<std::vector<double>> &xbins)
275 : fName(name), fTitle(title), fDim(dim), fNbins(nbins), fXmin(dim, 0.), fXmax(dim, 64.), fBinEdges(xbins)
276{
277}
278
279std::shared_ptr<::THnD> THnDModel::GetHistogram() const
280{
281 bool varbinning = false;
282 for (const auto &bins : fBinEdges) {
283 if (!bins.empty()) {
284 varbinning = true;
285 break;
286 }
287 }
288 std::shared_ptr<::THnD> h;
289 if (varbinning) {
290 h = std::make_shared<::THnD>(fName, fTitle, fDim, fNbins.data(), fBinEdges);
291 } else {
292 h = std::make_shared<::THnD>(fName, fTitle, fDim, fNbins.data(), fXmin.data(), fXmax.data());
293 }
294 return h;
295}
296THnDModel::~THnDModel() {}
297
298// Profiles
299
300TProfile1DModel::TProfile1DModel(const ::TProfile &h)
301 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
302 fXUp(h.GetXaxis()->GetXmax()), fYLow(h.GetYmin()), fYUp(h.GetYmax()), fOption(h.GetErrorOption())
303{
304 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
305}
306TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup,
307 const char *option)
308 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fOption(option)
309{
310}
311
312TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, double ylow,
313 double yup, const char *option)
314 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fYLow(ylow), fYUp(yup), fOption(option)
315{
316}
317
318TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const float *xbins,
319 const char *option)
320 : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
321{
323}
324TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins,
325 const char *option)
326 : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
327{
329}
330TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins, double ylow,
331 double yup, const char *option)
332 : fName(name), fTitle(title), fNbinsX(nbinsx), fYLow(ylow), fYUp(yup), fOption(option)
333{
335}
336std::shared_ptr<::TProfile> TProfile1DModel::GetProfile() const
337{
338 std::shared_ptr<::TProfile> prof;
339
340 if (fBinXEdges.empty())
341 prof = std::make_shared<::TProfile>(fName, fTitle, fNbinsX, fXLow, fXUp, fYLow, fYUp, fOption);
342 else
343 prof = std::make_shared<::TProfile>(fName, fTitle, fNbinsX, fBinXEdges.data(), fYLow, fYUp, fOption);
344
345 prof->SetDirectory(nullptr); // lifetime is managed by the shared_ptr, detach from ROOT's memory management
346 return prof;
347}
348TProfile1DModel::~TProfile1DModel()
349{
350}
351
352TProfile2DModel::TProfile2DModel(const ::TProfile2D &h)
353 : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
354 fXUp(h.GetXaxis()->GetXmax()), fNbinsY(h.GetNbinsY()), fYLow(h.GetYaxis()->GetXmin()),
355 fYUp(h.GetYaxis()->GetXmax()), fZLow(h.GetZmin()), fZUp(h.GetZmax()), fOption(h.GetErrorOption())
356{
357 SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
358 SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
359}
360TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
361 double ylow, double yup, const char *option)
362 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
363 fOption(option)
364{
365}
366
367TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
368 double ylow, double yup, double zlow, double zup, const char *option)
369 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
370 fZLow(zlow), fZUp(zup), fOption(option)
371{
372}
373
374TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
375 double ylow, double yup, const char *option)
376 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup), fOption(option)
377{
379}
380
381TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
382 const double *ybins, const char *option)
383 : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fOption(option)
384{
386}
387
388TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
389 const double *ybins, const char *option)
390 : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fOption(option)
391{
394}
395
396std::shared_ptr<::TProfile2D> TProfile2DModel::GetProfile() const
397{
398 // In this method we decide how to build the profile based on the input given in the constructor of the model.
399 // There are 4 cases:
400 // 1. No custom binning for both the x and y axes: we return a profile with equally spaced binning
401 // 2./3. Custom binning only for x(y): we return a profile with custom binning for x(y) and equally spaced for y(x).
402 // 4. No custom binning: we return a profile with equally spaced bins on both axes
403 auto xEdgesEmpty = fBinXEdges.empty();
404 auto yEdgesEmpty = fBinYEdges.empty();
405 std::shared_ptr<::TProfile2D> prof;
406 if (xEdgesEmpty && yEdgesEmpty) {
407 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fZLow, fZUp,
408 fOption);
409 } else if (!xEdgesEmpty && yEdgesEmpty) {
410 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fYLow, fYUp, fOption);
411 } else if (xEdgesEmpty && !yEdgesEmpty) {
412 prof = std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fBinYEdges.data(), fOption);
413 } else {
414 prof =
415 std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data(), fOption);
416 }
417
418 prof->SetDirectory(
419 nullptr); // object's lifetime is managed by the shared_ptr, detach it from ROOT's memory management
420 return prof;
421}
422
423TProfile2DModel::~TProfile2DModel()
424{
425}
426
427} // ns RDF
428
429} // 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 a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
A pseudo container class which is a generator of indices.
Definition TSeq.hxx:67
Class to manage histogram axis.
Definition TAxis.h:32
const TArrayD * GetXbins() const
Definition TAxis.h:138
Double_t GetXmax() const
Definition TAxis.h:142
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:522
Double_t GetXmin() const
Definition TAxis.h:141
Int_t GetNbins() const
Definition TAxis.h:127
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:532
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::vector< double > fBinXEdges
std::vector< double > fBinYEdges
std::vector< double > fBinXEdges
std::vector< double > fBinZEdges
std::vector< double > fBinYEdges
std::vector< double > fBinXEdges
std::vector< double > fXmax
std::vector< std::vector< double > > fBinEdges
std::vector< double > fXmin
std::vector< int > fNbins
std::vector< double > fBinXEdges
std::vector< double > fBinXEdges
std::vector< double > fBinYEdges