KaliVeda
Toolkit for HIC analysis
KVDataAnalysisTreeHistoHandler.h
1 #pragma once
2 
11 #include <optional>
12 #include "KVHashList.h"
13 #include "TH3.h"
14 #include "TProfile2D.h"
15 #include "TH2.h"
16 #include "TTree.h"
17 
18 template<typename AnalysisClass>
20 {
21  KVHashList fHistoList;
22  KVHashList fTreeList;
23 
24  void FillTH1(TH1* h1, Double_t x, Double_t w)
25  {
26  // \param[in] h1 1D histogram to fill
27  // \param[in] x value for x axis
28  // \param[in] w weight
29 
30  h1->Fill(x, w);
31  }
32  void FillTProfile(TProfile* h1, Double_t x, Double_t y, Double_t w)
33  {
34  // \param[in] h1 TProfile to fill
35  // \param[in] x value for x axis
36  // \param[in] y value for y axis
37  // \param[in] w weight
38 
39  h1->Fill(x, y, w);
40  }
41  void FillTH2(TH2* h2, Double_t x, Double_t y, Double_t w)
42  {
43  // \param[in] h2 TH2 to fill
44  // \param[in] x value for x axis
45  // \param[in] y value for y axis
46  // \param[in] w weight
47 
48  h2->Fill(x, y, w);
49  }
50  void FillTProfile2D(TProfile2D* h2, Double_t x, Double_t y, Double_t z, Double_t w)
51  {
52  // \param[in] h2 TProfile2D to fill
53  // \param[in] x value for x axis
54  // \param[in] y value for y axis
55  // \param[in] z value for z axis
56  // \param[in] w weight
57 
58  h2->Fill(x,y,z,w);
59  }
60  void FillTH3(TH3* h3, Double_t x, Double_t y, Double_t z, Double_t w)
61  {
62  // \param[in] h2 TH3 to fill
63  // \param[in] x value for x axis
64  // \param[in] y value for y axis
65  // \param[in] z value for z axis
66  // \param[in] w weight
67 
68  h3->Fill(x,y,z,w);
69  }
70 
71  protected:
72  KVHashList& GetHistoList()
73  {
74  return fHistoList;
75  }
76  KVHashList& GetTreeList()
77  {
78  return fTreeList;
79  }
80  public:
84  const KVHashList* GetHistoList() const
85  {
86  return &fHistoList;
87  }
92  const KVHashList* GetTreeList() const
93  {
94  return &fTreeList;
95  }
96 
112  template<typename HistoType, typename... Args>
113  HistoType* AddHisto(Args&& ... args)
114  {
115  auto h = new HistoType(std::forward<Args>(args)...);
116  static_cast<AnalysisClass&>(*this).add_histo(h);
117  return h;
118  }
127  TTree* AddTree(const TString& name, const TString& title = "")
128  {
129  auto t = new TTree(name, title);
130  static_cast<AnalysisClass&>(*this).add_tree(t);
131  return t;
132  }
137  TH1* GetHisto(const Char_t* name) const
138  {
139  return fHistoList.get_object<TH1>(name);
140  }
141 
146  TTree* GetTree(const Char_t* name) const
147  {
148  auto t = fTreeList.get_object<TTree>(name);
149  if (!t) KVError::Error(this, "GetTree()", "Tree %s not found: is this the right name?", name);
150  return t;
151  }
152 
161  void FillHisto(const Char_t* histo_name, Double_t x, Double_t y = 1, Double_t z = 1, Double_t w = 1)
162  {
163  TH1* h1 = nullptr;
164  if ((h1 = GetHisto(histo_name))) {
165  if (h1->InheritsFrom("TH3"))
166  FillTH3((TH3*)h1, x, y, z, w);
167  else if (h1->InheritsFrom("TProfile2D"))
168  FillTProfile2D((TProfile2D*)h1, x, y, z, w);
169  else if (h1->InheritsFrom("TH2"))
170  FillTH2((TH2*)h1, x, y, z);
171  else if (h1->InheritsFrom("TProfile"))
172  FillTProfile((TProfile*)h1, x, y, z);
173  else if (h1->InheritsFrom("TH1"))
174  FillTH1(h1, x, y);
175  else
176  KVError::Warning(this, "FillHisto", "%s -> Unhandled class ...", h1->ClassName());
177  }
178  else {
179  KVError::Warning(this, "FillHisto", "Histogram %s has not been defined", histo_name);
180  }
181  }
182 
189  void FillHisto(const Char_t* histo_name, const Char_t* label, Double_t weight = 1)
190  {
191  TH1* h1 = nullptr;
192  if ((h1 = GetHisto(histo_name))) {
193  h1->Fill(label, weight);
194  }
195  else {
196  KVError::Warning(this, "FillHisto", "Histogram %s has not been defined", histo_name);
197  }
198  }
199 
205  void FillTree(std::optional<TString> tree_name = {})
206  {
207  if (!tree_name) {
208  GetTreeList().R__FOR_EACH(TTree, Fill)();
209  }
210  else {
211  TTree* tt = 0;
212  if ((tt = GetTree(*tree_name))) {
213  tt->Fill();
214  }
215  else {
216  KVError::Warning(this, "FillTree", "%s unknown", tree_name->Data());
217  }
218  }
219  }
220  static const Char_t* Class_Name()
221  {
222  return "KVDataAnalysisTreeHistoHandler";
223  }
224 };
char Char_t
double Double_t
winID w
char name[80]
Methods to facilitate creation and use of histograms and TTrees in user analysis.
const KVHashList * GetTreeList() const
GetTreeList.
TH1 * GetHisto(const Char_t *name) const
void FillHisto(const Char_t *histo_name, const Char_t *label, Double_t weight=1)
Fill histogram with given name according to label bin and weight.
TTree * GetTree(const Char_t *name) const
void FillHisto(const Char_t *histo_name, Double_t x, Double_t y=1, Double_t z=1, Double_t w=1)
Fill previously-declared histogram with given name and required arguments.
TTree * AddTree(const TString &name, const TString &title="")
void FillTree(std::optional< TString > tree_name={})
Extended version of ROOT THashList.
Definition: KVHashList.h:29
T * get_object(const TString &name) const
virtual Int_t Fill(const char *name, Double_t w)
virtual Int_t Fill(const char *namex, const char *namey, Double_t w)
virtual Int_t Fill(const char *namex, const char *namey, const char *namez, Double_t w)
virtual const char * ClassName() const
virtual Bool_t InheritsFrom(const char *classname) const
Double_t y[n]
Double_t x[n]
TH1F * h1
TH1 * h
void Error(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:116
void Warning(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:125
str tree_name
auto * tt