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