CbmRoot
Loading...
Searching...
No Matches
PairAnalysisHistos.h
Go to the documentation of this file.
1#ifndef PAIRANALYSISHISTOS_H
2#define PAIRANALYSISHISTOS_H
3/* Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
4 * See cxx source for full Copyright notice */
5
7// //
8// Generic Histogram container with support for groups and filling of groups by passing //
9// a vector of data //
10// //
11// Authors: //
12// Julian Book <Julian.Book@cern.ch> //
13// //
15
16#include <iostream>
17
18#include <stdint.h>
19
20#include <Rtypes.h>
21#include <TNamed.h>
22// #include <TCollection.h>
23#include <TBits.h>
24#include <TFormula.h>
25#include <TH1.h> //new
26#include <THashList.h>
27#include <THnBase.h>
28#include <THnSparse.h>
29#include <TList.h> //new
30#include <TVectorD.h> //Dfwd
31
32#include "PairAnalysisHelper.h"
35
36class TH1;
37class TString;
38class TList;
39// class TVectorT<double>;
40
42class PairAnalysisHn : public THnSparseF {
43public:
44 TList* GetListOfFunctions() const { return fFunctions; }
45 inline PairAnalysisHn() : THnSparseF(), fFunctions(new TList) {}
46 inline PairAnalysisHn(const char* name, const char* title, Int_t dim, const Int_t* nbins, const Double_t* xmin = 0,
47 const Double_t* xmax = 0, Int_t chunksize = 1024 * 16)
48 : THnSparseF(name, title, dim, nbins, xmin, xmax, chunksize)
49 , fFunctions(new TList)
50 {
51 }
52
54 {
55 if (fFunctions) {
56 fFunctions->SetBit(kInvalidObject);
57 TObject* obj = 0;
58 while ((obj = fFunctions->First())) {
59 while (fFunctions->Remove(obj)) {}
60 if (!obj->TestBit(kNotDeleted)) { break; }
61 delete obj;
62 obj = 0;
63 }
64 delete fFunctions;
65 fFunctions = 0;
66 }
67 }
68
69protected:
70 TList* fFunctions; //->Pointer to list of functions (fits and user)
71
72private:
75 ClassDef(PairAnalysisHn,
76 2) // (temporary) class to hold functions in THnSparse
77};
78
81
82 class PairAnalysisHistos : public TNamed {
83public:
84 PairAnalysisHistos();
85 PairAnalysisHistos(const char* name, const char* title);
86 virtual ~PairAnalysisHistos();
87
88 enum class Eoption
89 {
90 kNoAutoFill = 1000000000,
91 kNoProfile = 999,
92 kNoWeights = 998,
93 kNo = 997
94 };
95 enum class Eprecision
96 {
97 kFloat = 0,
98 kDouble
99 };
100
101 // functions for object creation
102 void SetPrecision(Eprecision precision) { fPrecision = precision; }
103 TBits* GetUsedVars() const { return fUsedVars; }
104 void SetReservedWords(const char* words);
105 void AddClass(const char* histClass);
106 TString UserHistogram(const char* histClass, TObject* hist);
107 static void AdaptNameTitle(TH1* hist, const char* histClass);
108 static void AdaptNameTitle(THnBase* hist, const char* histClass);
109 static void StoreVariables(TObject* obj, UInt_t valType[20]);
110 static void StoreVariables(TH1* obj, UInt_t valType[20]);
111 static void StoreVariables(THnBase* obj, UInt_t valType[20]);
112
113 void UserHistogram(const char* histClass, Int_t ndim, TObjArray* limits, UInt_t* vars,
114 UInt_t valTypeW = static_cast<UInt_t>(Eoption::kNoWeights));
115 void AddSparse(const char* histClass, Int_t ndim, TObjArray* limits, UInt_t* vars,
116 UInt_t valTypeW = static_cast<UInt_t>(Eoption::kNoWeights));
117 void AddSparse(const char* histClass, Int_t ndim, TObjArray* limits, TFormula** vars,
118 UInt_t valTypeW = static_cast<UInt_t>(Eoption::kNoWeights));
119
120 // templates
121 template<typename valX, typename valY, typename valZ, typename valP, typename valW>
122 TString UserObject(const char* histClass, const char* name, const char* title, const TVectorD* const binsX,
123 valX valTypeX, const TVectorD* const binsY, valY valTypeY, const TVectorD* const binsZ,
124 valZ valTypeZ, valP valTypeP, valW valTypeW, TString option);
125
126 // 1D
127 template<typename valX, typename valW>
128 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX, valW valTypeW)
129 {
130 return UserObject(histClass, "", "", binsX, valTypeX, 0x0, static_cast<UInt_t>(Eoption::kNo), 0x0,
131 static_cast<UInt_t>(Eoption::kNo), static_cast<UInt_t>(Eoption::kNoProfile), valTypeW, "");
132 }
133
134 template<typename valX>
135 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX)
136 {
137 return AddHistogram(histClass, binsX, valTypeX, static_cast<UInt_t>(Eoption::kNoWeights));
138 }
139
140 template<typename valX, typename valP, typename valW>
141 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, valP valTypeP, TString option,
142 valW valTypeW)
143 {
144 return UserObject(histClass, "", "", binsX, valTypeX, 0x0, valTypeP, 0x0, static_cast<UInt_t>(Eoption::kNo),
145 valTypeP, valTypeW, option);
146 }
147
148 template<typename valX, typename valP>
149 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, valP valTypeP, TString option)
150 {
151 return AddProfile(histClass, binsX, valTypeX, valTypeP, option, static_cast<UInt_t>(Eoption::kNoWeights));
152 }
153
154 // 2D
155 template<typename valX, typename valY, typename valW>
156 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
157 valY valTypeY, valW valTypeW)
158 {
159 return UserObject(histClass, "", "", binsX, valTypeX, binsY, valTypeY, 0x0, static_cast<UInt_t>(Eoption::kNo),
160 static_cast<UInt_t>(Eoption::kNoProfile), valTypeW, "");
161 }
162
163 template<typename valX, typename valY>
164 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
165 valY valTypeY)
166 {
167 return AddHistogram(histClass, binsX, valTypeX, binsY, valTypeY, static_cast<UInt_t>(Eoption::kNoWeights));
168 }
169
170 template<typename valX, typename valY, typename valP, typename valW>
171 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
172 valY valTypeY, valP valTypeP, TString option, valW valTypeW)
173 {
174 return UserObject(histClass, "", "", binsX, valTypeX, binsY, valTypeY, 0x0, valTypeP, valTypeP, valTypeW, option);
175 }
176
177 template<typename valX, typename valY, typename valP>
178 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
179 valY valTypeY, valP valTypeP, TString option)
180 {
181 return AddProfile(histClass, binsX, valTypeX, binsY, valTypeY, valTypeP, option,
182 static_cast<UInt_t>(Eoption::kNoWeights));
183 }
184
185 // 3D
186 template<typename valX, typename valY, typename valZ, typename valW>
187 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
188 valY valTypeY, const TVectorD* const binsZ, valZ valTypeZ, valW valTypeW)
189 {
190 return UserObject(histClass, "", "", binsX, valTypeX, binsY, valTypeY, binsZ, valTypeZ,
191 static_cast<UInt_t>(Eoption::kNoProfile), valTypeW, "");
192 }
193
194 template<typename valX, typename valY, typename valZ>
195 TString AddHistogram(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
196 valY valTypeY, const TVectorD* const binsZ, valZ valTypeZ)
197 {
198 return AddHistogram(histClass, binsX, valTypeX, binsY, valTypeY, binsZ, valTypeZ,
199 static_cast<UInt_t>(Eoption::kNoWeights));
200 }
201
202 //profs
203 template<typename valX, typename valY, typename valZ, typename valP, typename valW>
204 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
205 valY valTypeY, const TVectorD* const binsZ, valZ valTypeZ, valP valTypeP, TString option,
206 valW valTypeW)
207 {
208 return UserObject(histClass, "", "", binsX, valTypeX, binsY, valTypeY, binsZ, valTypeZ, valTypeP, valTypeW, option);
209 }
210
211 template<typename valX, typename valY, typename valZ, typename valP>
212 TString AddProfile(const char* histClass, const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
213 valY valTypeY, const TVectorD* const binsZ, valZ valTypeZ, valP valTypeP, TString option)
214 {
215 return AddProfile(histClass, binsX, valTypeX, binsY, valTypeY, binsZ, valTypeZ, valTypeP, option,
216 static_cast<UInt_t>(Eoption::kNoWeights));
217 }
218
219 // functions to fill objects
220 // void FillClass(const char* histClass, const Double_t *values);
221 void FillClass(TString histClass, const Double_t* values);
222 static void FillValues(TObject* obj, const Double_t* values);
223 static void FillValues(TH1* obj, const Double_t* values);
224 static void FillValues(THnBase* obj, const Double_t* values);
225
226 // functions to get and draw histograms
227 void ReadFromFile(const char* file = "histos.root", const char* task = "", const char* config = "");
228 void DumpToFile(const char* file = "histos.root");
229 void SetList(TList* const list) { fList = list; }
230 TList* GetList() const { return fList; }
231 Bool_t SetCutClass(const char* cutClass);
232 Bool_t HasHistClass(TString histClass) const { return fHistoList.FindObject(histClass.Data()); }
233 void SetHistogramList(THashList& list, Bool_t setOwner = kTRUE);
234 void ResetHistogramList() { fHistoList.Clear(); }
235 const THashList* GetHistogramList() const { return &fHistoList; }
236
237 TObject* GetHist(const char* histClass, const char* name) const;
238 TH1* GetHistogram(const char* histClass, const char* name) const;
239 TObject* GetHist(const char* cutClass, const char* histClass, const char* name) const;
240 TH1* GetHistogram(const char* cutClass, const char* histClass, const char* name) const;
241
242
243 PairAnalysisMetaData* GetMetaData() const { return fMetaData; }
244
245 virtual void Print(const Option_t* option = "") const;
246 virtual void Draw(const Option_t* option = "");
247 virtual TObjArray* DrawSame(TString histName, TString option = "leg can", TString histClassDenom = "",
248 THashList* listDenom = 0x0);
249 virtual TObjArray* DrawTaskSame(TString histName, TString opt = "leg can", TString histClassDenom = "",
250 TString taskDenom = "");
251
252
253protected:
254 THashList fHistoList; //-> list of histograms
255
256
257 //private:
258
259 TFormula* GetFormula(const char* name, const char* formula);
260 TH1* GetTHist(const char* histClass, const char* name, const char* title, const TVectorD* const binsX,
261 const TVectorD* const binsY, const TVectorD* const binsZ);
262 TH1* GetTProf(const char* histClass, const char* name, const char* title, const TVectorD* const binsX,
263 const TVectorD* const binsY, const TVectorD* const binsZ, TString option = "i");
264
265 void FillVarArray(TObject* obj, UInt_t* valType);
266
267 PairAnalysisMetaData* fMetaData;
268 // THashList fHistoList; //-> list of histograms
269 TList* fList;
270 TBits* fUsedVars; // list of used variables
271
272 TString* fReservedWords;
273 void UserHistogramReservedWords(const char* histClass, const TObject* hist);
274 void FillClass(THashTable* classTable, Int_t nValues, Double_t* values);
275
276 void PrintPDF(Option_t* opt);
277 void PrintStructure() const;
278
279 Bool_t IsHistogramOk(const char* classTable, const char* name);
280 Eprecision fPrecision;
281
282 PairAnalysisHistos(const PairAnalysisHistos& hist);
283 PairAnalysisHistos& operator=(const PairAnalysisHistos& hist);
284
285 ClassDef(PairAnalysisHistos, 3) // Histogram management
286};
287
288template<typename valX, typename valY, typename valZ, typename valP, typename valW>
289TString PairAnalysisHistos::UserObject(const char* histClass, const char* name, const char* title,
290 const TVectorD* const binsX, valX valTypeX, const TVectorD* const binsY,
291 valY valTypeY, const TVectorD* const binsZ, valZ valTypeZ, valP valTypeP,
292 valW valTypeW, TString option)
293{
294 //
295 // main function to setup the histogram with given variables, binning and dimensions
296 //
297 TH1* hist = 0x0;
298 TString err = "err";
299 //profile or histogram
300 if (typeid(valTypeP) == typeid(UInt_t) && (uintptr_t) valTypeP == static_cast<UInt_t>(Eoption::kNoProfile))
301 hist = GetTHist(histClass, name, title, binsX, binsY, binsZ);
302 else
303 hist = GetTProf(histClass, name, title, binsX, binsY, binsZ, option);
304 if (!hist) {
305 delete binsX;
306 return err;
307 }
308
309 // add formulas to the histograms list of functions
310 // and store variales in uniqueIDs
311 UInt_t valType[20] = {0};
312 TString func = "";
313 func.Form("%d", valTypeX);
314 if (!func.Atoi()) std::cout << func << std::endl; //hist->GetListOfFunctions()->Add( GetFormula("xFormula",func) );
315 else
316 valType[0] = func.Atoi();
317
318 func = "";
319 func.Form("%d", valTypeY);
320 if (!func.Atoi()) std::cout << func << std::endl; //hist->GetListOfFunctions()->Add( GetFormula("yFormula",func) );
321 else
322 valType[1] = func.Atoi();
323
324 func = "";
325 func.Form("%d", valTypeZ);
326 if (!func.Atoi()) std::cout << func << std::endl; //hist->GetListOfFunctions()->Add( GetFormula("zFormula",func) );
327 else
328 valType[2] = func.Atoi();
329
330 func = "";
331 func.Form("%d", valTypeP);
332 if (!func.Atoi()) std::cout << func << std::endl; //hist->GetListOfFunctions()->Add( GetFormula("pFormula",func) );
333 else
334 valType[3] = func.Atoi();
335
336 TString func2 = "";
337 func2.Form("%d", valTypeX);
338 func = "";
339 func.Form("%d", valTypeW); //func+=valTypeW;
340 if (!func.Atoi()) std::cout << func << std::endl; //hist->GetListOfFunctions()->Add( GetFormula("wFormula",func) );
341 else if (func2.Atoi() != static_cast<UInt_t>(Eoption::kNoWeights)) {
342 hist->SetUniqueID(func.Atoi()); // store weighting variable
343 fUsedVars->SetBitNumber(func.Atoi(), kTRUE);
344 }
345
346 // store variables and their usage
347 StoreVariables(hist, valType);
348 for (Int_t i = 0; i < 4; i++)
349 fUsedVars->SetBitNumber(valType[i], kTRUE);
350
351 // adapt the name and title of the histogram in case they are empty
352 AdaptNameTitle(hist, histClass);
353
354 // save histname
355 TString key = hist->GetName();
356 // set pdg code labels
357 /*
358 if(hist->GetDimension()==1) {
359 func=""; func+=valTypeX;
360 if(func.Contains("PdgCode") ||
361 func.Atoi()==PairAnalysisVarManager::kPdgCode ||
362 func.Atoi()==PairAnalysisVarManager::kPdgCodeMother ||
363 func.Atoi()==PairAnalysisVarManager::kPdgCodeGrandMother ||
364 ) PairAnalysisHelper::SetPDGBinLabels(hist);
365 }
366 */
367
368 Bool_t isReserved = fReservedWords->Contains(histClass);
369 if (func2.Atoi() && func2.Atoi() == static_cast<UInt_t>(Eoption::kNoAutoFill)) hist->SetUniqueID(func2.Atoi());
370 if (isReserved) UserHistogramReservedWords(histClass, hist);
371 else
372 UserHistogram(histClass, hist);
373
374 delete binsX;
375 delete binsY;
376 delete binsZ;
377
378 return key;
379}
380
381#endif
ClassImp(CbmConverterManager)
TList * GetListOfFunctions() const
PairAnalysisHn(const PairAnalysisHn &hist)
PairAnalysisHn(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin=0, const Double_t *xmax=0, Int_t chunksize=1024 *16)
PairAnalysisHn & operator=(const PairAnalysisHn &hist)
TFormula * GetFormula(const char *name, const char *formula)
T ReadFromFile(fs::path path)
Definition Yaml.h:51