Logo ROOT   6.12/07
Reference Guide
TDFHistoModels.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 
11 #include <ROOT/TSeq.hxx>
12 #include <ROOT/TDFHistoModels.hxx>
13 
14 #include <TH1D.h>
15 #include <TH2D.h>
16 #include <TH3D.h>
17 #include <TProfile.h>
18 #include <TProfile2D.h>
19 
20 /**
21 * \class ROOT::Experimental::TDF::TH1DModel
22 * \ingroup dataframe
23 * \brief A struct which stores the parameters of a TH1D
24 *
25 * \class ROOT::Experimental::TDF::TH2DModel
26 * \ingroup dataframe
27 * \brief A struct which stores the parameters of a TH2D
28 *
29 * \class ROOT::Experimental::TDF::TH3DModel
30 * \ingroup dataframe
31 * \brief A struct which stores the parameters of a TH3D
32 *
33 * \class ROOT::Experimental::TDF::TProfile1DModel
34 * \ingroup dataframe
35 * \brief A struct which stores the parameters of a TProfile
36 *
37 * \class ROOT::Experimental::TDF::TProfile2DModel
38 * \ingroup dataframe
39 * \brief A struct which stores the parameters of a TProfile2D
40 */
41 
42 template <typename T>
43 inline void FillVector(std::vector<double> &v, int size, T *a)
44 {
45  v.reserve(size);
46  for (auto i : ROOT::TSeq<int>(size + 1))
47  v.push_back(a[i]);
48 }
49 
50 template <>
51 inline void FillVector<double>(std::vector<double> &v, int size, double *a)
52 {
53  v.assign(a, a + (size_t)(size + 1));
54 }
55 
56 inline void SetAxisProperties(const TAxis *axis, double &low, double &up, std::vector<double> &edges)
57 {
58  // Check if this histo has fixed binning
59  // Same technique of "Int_t TAxis::FindBin(Double_t)"
60  if (!axis->GetXbins()->fN) {
61  low = axis->GetXmin();
62  up = axis->GetXmax();
63  } else {
64  // This histo has variable binning
65  const auto size = axis->GetNbins() + 1;
66  edges.reserve(size);
67  for (auto i : ROOT::TSeq<int>(1, size))
68  edges.push_back(axis->GetBinLowEdge(i));
69  edges.push_back(axis->GetBinUpEdge(size - 1));
70  }
71 }
72 
73 namespace ROOT {
74 namespace Experimental {
75 namespace TDF {
76 
77 TH1DModel::TH1DModel(const ::TH1D &h) : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX())
78 {
79  SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
80 }
81 TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup)
82  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup)
83 {
84 }
85 TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const float *xbins)
86  : fName(name), fTitle(title), fNbinsX(nbinsx)
87 {
88  FillVector(fBinXEdges, nbinsx, xbins);
89 }
90 TH1DModel::TH1DModel(const char *name, const char *title, int nbinsx, const double *xbins)
91  : fName(name), fTitle(title), fNbinsX(nbinsx)
92 {
93  FillVector(fBinXEdges, nbinsx, xbins);
94 }
95 std::shared_ptr<::TH1D> TH1DModel::GetHistogram() const
96 {
97  if (fBinXEdges.empty()) {
98  return std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fXLow, fXUp);
99  } else {
100  return std::make_shared<::TH1D>(fName, fTitle, fNbinsX, fBinXEdges.data());
101  }
102 }
104 {
105 }
106 
108  : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY())
109 {
110  SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
111  SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
112 }
113 TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
114  double yup)
115  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
116 {
117 }
118 TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy, double ylow,
119  double yup)
120  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup)
121 {
122  FillVector(fBinXEdges, nbinsx, xbins);
123 }
124 TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
125  const double *ybins)
126  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy)
127 {
128  FillVector(fBinYEdges, nbinsy, ybins);
129 }
130 TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
131  const double *ybins)
132  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
133 {
134  FillVector(fBinXEdges, nbinsx, xbins);
135  FillVector(fBinYEdges, nbinsy, ybins);
136 }
137 TH2DModel::TH2DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
138  const float *ybins)
139  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy)
140 {
141  FillVector(fBinXEdges, nbinsx, xbins);
142  FillVector(fBinYEdges, nbinsy, ybins);
143 }
144 std::shared_ptr<::TH2D> TH2DModel::GetHistogram() const
145 {
146  auto xEdgesEmpty = fBinXEdges.empty();
147  auto yEdgesEmpty = fBinYEdges.empty();
148  if (xEdgesEmpty && yEdgesEmpty) {
149  return std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp);
150  } else if (!xEdgesEmpty && yEdgesEmpty) {
151  return std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fYLow, fYUp);
152  } else if (xEdgesEmpty && !yEdgesEmpty) {
153  return std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fBinYEdges.data());
154  } else {
155  return std::make_shared<::TH2D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data());
156  }
157 }
159 {
160 }
161 
163  : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fNbinsY(h.GetNbinsY()), fNbinsZ(h.GetNbinsZ())
164 {
165  SetAxisProperties(h.GetXaxis(), fXLow, fXUp, fBinXEdges);
166  SetAxisProperties(h.GetYaxis(), fYLow, fYUp, fBinYEdges);
167  SetAxisProperties(h.GetZaxis(), fZLow, fZUp, fBinZEdges);
168 }
169 TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow,
170  double yup, int nbinsz, double zlow, double zup)
171  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
172  fNbinsZ(nbinsz), fZLow(zlow), fZUp(zup)
173 {
174 }
175 TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
176  const double *ybins, int nbinsz, const double *zbins)
177  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
178 {
179  FillVector(fBinXEdges, nbinsx, xbins);
180  FillVector(fBinYEdges, nbinsy, ybins);
181  FillVector(fBinZEdges, nbinsz, zbins);
182 }
183 TH3DModel::TH3DModel(const char *name, const char *title, int nbinsx, const float *xbins, int nbinsy,
184  const float *ybins, int nbinsz, const float *zbins)
185  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fNbinsZ(nbinsz)
186 {
187  FillVector(fBinXEdges, nbinsx, xbins);
188  FillVector(fBinYEdges, nbinsy, ybins);
189  FillVector(fBinZEdges, nbinsz, zbins);
190 }
191 std::shared_ptr<::TH3D> TH3DModel::GetHistogram() const
192 {
193  if (fBinXEdges.empty() && fBinYEdges.empty() && fBinZEdges.empty()) {
194  return std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fNbinsZ, fZLow, fZUp);
195  } else {
196  return std::make_shared<::TH3D>(fName, fTitle, fNbinsX, fBinXEdges.data(), fNbinsY, fBinYEdges.data(), fNbinsZ,
197  fBinZEdges.data());
198  }
199 }
201 {
202 }
203 
204 // Profiles
205 
207  : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
208  fXUp(h.GetXaxis()->GetXmax()), fYLow(h.GetYmin()), fYUp(h.GetYmax()), fOption(h.GetErrorOption())
209 {
210 }
211 TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup,
212  const char *option)
213  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fOption(option)
214 {
215 }
216 
217 TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, double ylow,
218  double yup, const char *option)
219  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fYLow(ylow), fYUp(yup), fOption(option)
220 {
221 }
222 
223 TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const float *xbins,
224  const char *option)
225  : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
226 {
227  FillVector(fBinXEdges, nbinsx, xbins);
228 }
229 TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins,
230  const char *option)
231  : fName(name), fTitle(title), fNbinsX(nbinsx), fOption(option)
232 {
233  FillVector(fBinXEdges, nbinsx, xbins);
234 }
235 TProfile1DModel::TProfile1DModel(const char *name, const char *title, int nbinsx, const double *xbins, double ylow,
236  double yup, const char *option)
237  : fName(name), fTitle(title), fNbinsX(nbinsx), fYLow(ylow), fYUp(yup), fOption(option)
238 {
239  FillVector(fBinXEdges, nbinsx, xbins);
240 }
241 std::shared_ptr<::TProfile> TProfile1DModel::GetProfile() const
242 {
243  return std::make_shared<::TProfile>(fName, fTitle, fNbinsX, fXLow, fXUp, fYLow, fYUp, fOption);
244 }
246 {
247 }
248 
250  : fName(h.GetName()), fTitle(h.GetTitle()), fNbinsX(h.GetNbinsX()), fXLow(h.GetXaxis()->GetXmin()),
251  fXUp(h.GetXaxis()->GetXmax()), fNbinsY(h.GetNbinsY()), fYLow(h.GetYaxis()->GetXmin()),
252  fYUp(h.GetYaxis()->GetXmax()), fZLow(h.GetZmin()), fZUp(h.GetZmax()), fOption(h.GetErrorOption())
253 {
254 }
255 TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
256  double ylow, double yup, const char *option)
257  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
258  fOption(option)
259 {
260 }
261 
262 TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
263  double ylow, double yup, double zlow, double zup, const char *option)
264  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup),
265  fZLow(zlow), fZUp(zup), fOption(option)
266 {
267 }
268 
269 TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
270  double ylow, double yup, const char *option)
271  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fYLow(ylow), fYUp(yup), fOption(option)
272 {
273  FillVector(fBinXEdges, nbinsx, xbins);
274 }
275 
276 TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy,
277  const double *ybins, const char *option)
278  : fName(name), fTitle(title), fNbinsX(nbinsx), fXLow(xlow), fXUp(xup), fNbinsY(nbinsy), fOption(option)
279 {
280  FillVector(fBinYEdges, nbinsy, ybins);
281 }
282 
283 TProfile2DModel::TProfile2DModel(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy,
284  const double *ybins, const char *option)
285  : fName(name), fTitle(title), fNbinsX(nbinsx), fNbinsY(nbinsy), fOption(option)
286 {
287  FillVector(fBinXEdges, nbinsx, xbins);
288  FillVector(fBinYEdges, nbinsy, ybins);
289 }
290 
291 std::shared_ptr<::TProfile2D> TProfile2DModel::GetProfile() const
292 {
293  return std::make_shared<::TProfile2D>(fName, fTitle, fNbinsX, fXLow, fXUp, fNbinsY, fYLow, fYUp, fZLow, fZUp,
294  fOption);
295 }
296 
298 {
299 }
300 
301 } // ns TDF
302 } // ns Experimental
303 } // ns ROOT
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:145
std::shared_ptr<::TH1D > GetHistogram() const
void FillVector< double >(std::vector< double > &v, int size, double *a)
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
std::shared_ptr<::TH3D > GetHistogram() const
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition: TAxis.cxx:504
double T(double x)
Definition: ChebyshevPol.h:34
TH1 * h
Definition: legend2.C:5
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition: TAxis.cxx:514
Double_t GetXmin() const
Definition: TAxis.h:133
void SetAxisProperties(const TAxis *axis, double &low, double &up, std::vector< double > &edges)
void FillVector(std::vector< double > &v, int size, T *a)
THist< 3, double, THistStatContent, THistStatUncertainty > TH3D
Definition: THist.hxx:296
Int_t fN
Definition: TArray.h:38
Class to manage histogram axis.
Definition: TAxis.h:30
SVector< double, 2 > v
Definition: Dict.h:5
auto * a
Definition: textangle.C:12
std::shared_ptr<::TProfile > GetProfile() const
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:290
std::shared_ptr<::TProfile2D > GetProfile() const
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
std::shared_ptr<::TH2D > GetHistogram() const
Int_t GetNbins() const
Definition: TAxis.h:121
Double_t GetXmax() const
Definition: TAxis.h:134
char name[80]
Definition: TGX11.cxx:109
const TArrayD * GetXbins() const
Definition: TAxis.h:130