]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGDQ/dielectron/AliDielectronHFhelper.cxx
-updates on the configs and HF incl.helper
[u/mrichter/AliRoot.git] / PWGDQ / dielectron / AliDielectronHFhelper.cxx
CommitLineData
5e2cf960 1/*************************************************************************
2* Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
3* *
4* Author: The ALICE Off-line Project. *
5* Contributors are mentioned in the code where appropriate. *
6* *
7* Permission to use, copy, modify and distribute this software and its *
8* documentation strictly for non-commercial purposes is hereby granted *
9* without fee, provided that the above copyright notice appears in all *
10* copies and that both the copyright notice and this permission notice *
11* appear in the supporting documentation. The authors make no claims *
12* about the suitability of this software for any purpose. It is *
13* provided "as is" without express or implied warranty. *
14**************************************************************************/
15
16///////////////////////////////////////////////////////////////////////////
17// Dielectron Histogram framework helper //
18// //
19/*
20
21
22
23
24
25
26
27
28
29*/
30// //
31///////////////////////////////////////////////////////////////////////////
32
33#include <TObjArray.h>
34#include <TKey.h>
35#include <TList.h>
36#include <TClass.h>
37#include <TObject.h>
38#include <TFile.h>
39#include <TString.h>
40#include <TObjString.h>
41#include <TVectorD.h>
42#include <TMath.h>
43#include <TH1.h>
44
45#include <AliLog.h>
46
443a091c 47#include "AliDielectron.h"
5e2cf960 48#include "AliDielectronHFhelper.h"
49#include "AliDielectronHF.h"
50
51//ClassImp(AliDielectronHFhelper)
52
53//const char* AliDielectronHFhelper::fCutVars[AliDielectronHFhelper::kMaxCuts] = {"","","","","","","","","",""};
54
55//________________________________________________________________
56AliDielectronHFhelper::AliDielectronHFhelper(const char* filename) :
57 TNamed(),
58 fArrPairType(0x0),
59 fCutVars(0x0),
60 fCutLowLimits(0),
61 fCutUpLimits(0)
62{
63 //
64 // get HF container(s) from file 'filename'
65 //
66 SetHFArray(filename);
187de573 67
5e2cf960 68}
69
70//________________________________________________________________
71AliDielectronHFhelper::~AliDielectronHFhelper()
72{
73 //
74 // dtor
75 //
76 if(fArrPairType) delete fArrPairType;
77 if(fCutVars) delete fCutVars;
187de573 78
5e2cf960 79}
80
81//________________________________________________________________
82void AliDielectronHFhelper::SetHFArray(const char* filename)
83{
84 //
85 // get HF containers from file
86 //
87
88 TFile *f = TFile::Open(filename);
89
90 TList *l=f->GetListOfKeys();
91 TIter nextKey(l);
92 TKey *k=0x0;
93 while ( (k=static_cast<TKey*>(nextKey())) ){
187de573 94
5e2cf960 95 TObject *o=k->ReadObj();
96 if (o->IsA()==TList::Class()){
187de573 97
5e2cf960 98 TList *tlist=(TList*)o;
187de573 99
5e2cf960 100 TIter next(tlist);
101 TObject *obj=0x0;
102 while ((obj = next())) {
103 TString objname(obj->GetName());
104
105 if( objname.Contains("_HF") && obj->IsA()==TObjArray::Class()) {
106 fArrPairType = new TObjArray( *(dynamic_cast<TObjArray*>(obj)) );
107 //fArrPairType->Print();
108 return;
109 }
110 }
111 }
112 }
113
114}
115//________________________________________________________________
116void AliDielectronHFhelper::SetRangeUser(const char *varname, Double_t min, Double_t max, Bool_t leg)
117{
118 //
119 // Set range from variable name
120 //
121 // Int_t size=sizeof(fCutVars)/sizeof(const char*);
187de573 122
5e2cf960 123 Int_t size=fCutLowLimits.GetNrows();
124
187de573 125 // check if cut is already set
126 for(Int_t icut=0; icut<size; icut++) {
127 TString cutName = fCutVars->At(icut)->GetName();
128 if(!cutName.CompareTo(Form("%s%s",(leg?"Leg":""),varname))) {
129 UnsetRangeUser(varname,leg);
130 SetRangeUser(varname, min, max, leg);
131 return;
132 }
133 }
134
5e2cf960 135 if(size>=kMaxCuts) return;
136
137 // arrays
138 if(!fCutVars) {
139 fCutVars = new TObjArray();
140 fCutVars->SetOwner();
141 }
142 fCutLowLimits.ResizeTo(size+1);
143 fCutUpLimits.ResizeTo(size+1);
144
145 // fill
5e2cf960 146 TObjString *str = new TObjString(Form("%s%s",(leg?"Leg":""),varname));
147 fCutVars->Add(str);
148 fCutLowLimits(size) = min;
149 fCutUpLimits(size) = max;
150 AliWarning(Form(" %s [%.2f,%.2f]",fCutVars->At(size)->GetName(),fCutLowLimits(size),fCutUpLimits(size)));
151}
152
153//________________________________________________________________
154void AliDielectronHFhelper::SetRangeUser(AliDielectronVarManager::ValueTypes type, Double_t min, Double_t max, Bool_t leg)
155{
156 //
187de573 157 // Set range from AliDielectronVarManager
5e2cf960 158 //
159 SetRangeUser(AliDielectronVarManager::GetValueName(type), min, max, leg);
160}
161
4d7704c5 162//________________________________________________________________
163void AliDielectronHFhelper::UnsetRangeUser(const char *varname, Bool_t leg)
164{
165 //
166 // unset range from variable name
167 //
168 Int_t size=fCutLowLimits.GetNrows();
187de573 169 // PrintCuts();
4d7704c5 170 TVectorD newlow;
171 TVectorD newup;
172
187de573 173 // find cut and build new vectors w/o it
4d7704c5 174 Int_t ientries = 0;
175 for(Int_t icut=0; icut<size; icut++) {
176
177 TString cutName = fCutVars->At(icut)->GetName();
187de573 178 if(!cutName.CompareTo(Form("%s%s",(leg?"Leg":""),varname))) {
4d7704c5 179 fCutVars->AddAt(0x0,icut);
180 continue;
181 }
187de573 182
4d7704c5 183 // fill new vectors
184 newlow.ResizeTo(ientries+1);
185 newup.ResizeTo(ientries+1);
186 newlow(ientries) = fCutLowLimits(icut);
187 newup(ientries) = fCutUpLimits(icut);
187de573 188
4d7704c5 189 ientries++;
190 }
187de573 191
4d7704c5 192 // adapt new arrays/vectors
193 fCutVars->Compress();
194
195 fCutLowLimits.ResizeTo(ientries);
196 fCutUpLimits.ResizeTo(ientries);
197 for(Int_t icut=0; icut<ientries; icut++) {
198 fCutLowLimits(icut) = newlow(icut);
199 fCutUpLimits(icut) = newup(icut);
200 }
187de573 201 // PrintCuts();
202
4d7704c5 203}
204
205//________________________________________________________________
206void AliDielectronHFhelper::UnsetRangeUser(AliDielectronVarManager::ValueTypes type, Bool_t leg)
207{
208 //
187de573 209 // Unset range from AliDielectronVarManager
4d7704c5 210 //
211 UnsetRangeUser(AliDielectronVarManager::GetValueName(type), leg);
212}
213
443a091c 214//________________________________________________________________
187de573 215TObjArray* AliDielectronHFhelper::CollectHistos()
443a091c 216{
217 //
218 // collect histograms for all kind of pair types or sources
219 //
187de573 220
443a091c 221 TObjArray *collection = new TObjArray(AliDielectron::kEv1PMRot+1);
222
187de573 223 TObjArray *histArr = (TObjArray*) fArrPairType->Clone("tmpArr");
224 histArr->SetOwner(kTRUE);
225
443a091c 226 // loop over max. available pair types
227 for(Int_t i=0; i<AliDielectron::kEv1PMRot+1; i++) {
228
187de573 229 collection->AddAt(GetHistogram(AliDielectron::PairClassName(i),histArr), i);
230
231 }
232
233 // clean up the clone
234 if(histArr) {
235 delete histArr;
236 histArr=0;
443a091c 237 }
238
239 return collection;
240}
241
5e2cf960 242//________________________________________________________________
187de573 243TH1F* AliDielectronHFhelper::GetHistogram(const char *step, TObjArray *histArr)
5e2cf960 244{
245 //
187de573 246 // main function to recieve a single histogram
247 // TODO: check memory
248
249 AliDebug(1,Form(" Step %s selected",step));
250
251 TObjArray *histos= 0x0;
252 TH1F *hist = 0x0;
253 if(!histArr) {
254 histos = (TObjArray*) fArrPairType->FindObject(step)->Clone("tmpArr");
255 }
256 else {
257 histos = (TObjArray*) histArr->FindObject(step);
258 }
5e2cf960 259
187de573 260 if(histos) hist = FindHistograms(histos);
5e2cf960 261 return hist;
187de573 262
5e2cf960 263}
264
265//________________________________________________________________
266TH1F* AliDielectronHFhelper::FindHistograms(TObjArray *histos)
267{
268 //
269 // exclude histograms
270 //
187de573 271
5e2cf960 272 // debug
273 // TString title = histos->At(0)->GetTitle();
274 // TObjArray* vars = title.Tokenize(":");
187de573 275 // AliDebug(1,Form(" number of cuts/vars: %d/%d",fCutLowLimits.GetNrows(),vars->GetEntriesFast()));
276
5e2cf960 277 // check for missing cuts
278 CheckCuts(histos);
187de573 279
5e2cf960 280 // loop over all cuts
281 for(Int_t icut=0; icut<fCutLowLimits.GetNrows(); icut++) {
282
283 Bool_t bFndBin = kFALSE; // exact bin found
284 const char *cutvar = fCutVars->At(icut)->GetName();
285 Double_t min = fCutLowLimits(icut);
286 Double_t max = fCutUpLimits(icut);
187de573 287 AliDebug(5,Form(" Cut %d: %s [%.2f,%.2f]",icut,cutvar,min,max));
5e2cf960 288
289 // loop over all histograms
290 for(Int_t i=0; i<histos->GetEntriesFast(); i++) {
291
292 // continue if already empty
293 if(!histos->At(i)) continue;
294
295 // collect binning from histo title
296 TString title = histos->At(i)->GetTitle();
297 if(title.IsNull()) continue;
187de573 298 AliDebug(10,Form(" histo title: %s",title.Data()));
5e2cf960 299
300 TObjArray *vars = title.Tokenize(":");
301 for(Int_t ivar=0; ivar<vars->GetEntriesFast(); ivar++) {
302 TString binvar = vars->At(ivar)->GetName();
187de573 303 AliDebug(10,Form(" Check ivar %d binvar %s",ivar,binvar.Data()));
304
5e2cf960 305 // check for cuts and ranges by the user
306 if(binvar.Contains(cutvar)) {
307 TObjArray *limits = binvar.Tokenize("#");
187de573 308
5e2cf960 309 Double_t binmin = atof(limits->At(1)->GetName());
310 Double_t binmax = atof(limits->At(2)->GetName());
187de573 311 AliDebug(10,Form(" bin %s var %s [%.2f,%.2f]",binvar.Data(),limits->At(0)->GetName(),binmin,binmax));
312
5e2cf960 313 // remove histogram from array
314 if(binmin < min || binmax < min || binmin > max || binmax > max ) {
187de573 315 AliDebug(10,Form(" removed, out of range min %.2f,%.2f max %.2f,%.2f",binmin,min,binmax,max));
5e2cf960 316 histos->AddAt(0x0,i);
317 }
318 if(bFndBin && !(binmin == min && binmax == max)) {
319 histos->AddAt(0x0,i);
187de573 320 AliDebug(10,Form(" removed, within range min %.2f,%.2f max %.2f,%.2f",binmin,min,binmax,max));
5e2cf960 321 }
322 // clean up
323 if(limits) delete limits;
324
325 // do we have found an exact bin
326 if(binmin==min && binmax==max) bFndBin=kTRUE;
327
328 }
329
330 }
331 // clean up
332 if(vars) delete vars;
187de573 333
5e2cf960 334 }
187de573 335
5e2cf960 336 }
187de573 337
5e2cf960 338 // compress the array by removing all empty histos
187de573 339 histos->Compress();
5e2cf960 340 AliDebug(1,Form(" Compression: %d histograms left",histos->GetEntriesFast()));
341
342 // merge histograms
343 TH1F* hist = MergeHistos(histos);
344 return hist;
345}
346
347//________________________________________________________________
348TH1F* AliDielectronHFhelper::MergeHistos(TObjArray *arr)
349{
350 //
351 // merge histos to one single histogram
352 //
5e2cf960 353
187de573 354 if(arr->GetEntriesFast()<1) { AliError(" No more histosgrams left!"); return 0x0; }
355
356 TH1F *final=(TH1F*) arr->At(0)->Clone();
5e2cf960 357 if(!final) return 0x0;
187de573 358
5e2cf960 359 final->Reset("CE");
360 final->SetTitle(""); //TODO: change in future
361 for(Int_t i=0; i<arr->GetEntriesFast(); i++) {
362 final->Add((TH1F*)arr->At(i));
363 }
364 arr->Clear();
365
366 return final;
367}
368
369//________________________________________________________________
370void AliDielectronHFhelper::CheckCuts(TObjArray *arr)
371{
372 //
373 // Compare histo binning and cut variables. Add necessary cuts (largest limit)
374 //
375
376
377 // build array with bin variable, minimum and maximum bin values
378 TString titleFIRST = arr->First()->GetTitle();
379 TString titleLAST = arr->Last()->GetTitle();
380 TObjArray* binvarsF = titleFIRST.Tokenize(":#");
381 TObjArray* binvarsL = titleLAST.Tokenize(":#");
382 Double_t binmin[kMaxCuts]= {0.0};
383 Double_t binmax[kMaxCuts]= {0.0};
384 for(Int_t ivar=0; ivar<binvarsF->GetEntriesFast(); ivar++) {
385
386 TString elementF=binvarsF->At(ivar)->GetName();
387 TString elementL=binvarsL->At(ivar)->GetName();
388 AliDebug(1,Form(" binvar %d: %s,%s",ivar,elementF.Data(),elementL.Data()));
187de573 389
5e2cf960 390 switch(ivar%3) {
391 case 0: continue; break;
392 case 1: binmin[(int)ivar/3]=atof(elementF.Data()); break;
393 case 2: binmax[(int)ivar/3]=atof(elementL.Data()); break;
394 }
187de573 395
5e2cf960 396 binvarsF->AddAt(0x0,ivar);
397 }
398 binvarsF->Compress();
187de573 399
5e2cf960 400 // loop over all vars and cuts, check for missing stuff
401 for(Int_t ivar=0; ivar<binvarsF->GetEntriesFast(); ivar++) {
187de573 402
5e2cf960 403 TString binvar=binvarsF->At(ivar)->GetName();
404 Bool_t selected=kFALSE;
405
406 AliDebug(1,Form(" check cuts %d %s [%.2f,%.2f]",ivar,binvar.Data(),binmin[ivar],binmax[ivar]));
407 // loop over all cuts and check for missing stuff
408 for(Int_t icut=0; icut<fCutLowLimits.GetNrows(); icut++) {
409 if(binvar.Contains(fCutVars->At(icut)->GetName())) { selected=kTRUE; break; }
410 // else break;
411 }
187de573 412
5e2cf960 413 // add missing cut with max limits
414 if(!selected) {
187de573 415 AliWarning(Form(" Bin variable %s not covered. Add cut!",binvar.Data()));
416 Bool_t leg = binvar.BeginsWith("Leg");
417 if(leg) binvar.Remove(0,3);
418 SetRangeUser(binvar.Data(),binmin[ivar],binmax[ivar], leg);
5e2cf960 419 }
187de573 420
5e2cf960 421 }
187de573 422
423 // clean up
5e2cf960 424 if(binvarsF) delete binvarsF;
425 if(binvarsL) delete binvarsL;
426}
427
428//________________________________________________________________
443a091c 429void AliDielectronHFhelper::Print(const Option_t* /*option*/) const
5e2cf960 430{
431
432 //
433 // Print out object contents
434 //
435
436 // pairtypes, steps and sources
187de573 437 AliInfo(Form(" Number of filled steps: %d",fArrPairType->GetEntries()));
438 for(Int_t istep=0; istep<fArrPairType->GetEntriesFast(); istep++) {
439 if(fArrPairType->At(istep))
440 AliInfo(Form(" step %d: %s",istep,fArrPairType->At(istep)->GetName()));
441 }
442
443 AliInfo(Form(" Number of histograms: %d",((TObjArray*)fArrPairType->At(0))->GetEntriesFast()));
444
445 TString title = ((TObjArray*)fArrPairType->At(0))->First()->GetTitle();
446 TObjArray* binvars = title.Tokenize(":");
447 AliInfo(Form(" Number of variables: %d",binvars->GetEntriesFast()));
448
449 TObjArray* binvars2 = title.Tokenize(":#");
450 for(Int_t ivar=0; ivar<binvars2->GetEntriesFast(); ivar++) {
451 if(ivar%3) continue;
452 AliInfo(Form(" variable %.0f: %s",((Double_t)ivar)/3+1,binvars2->At(ivar)->GetName()));
5e2cf960 453 }
454
455}
456
457//________________________________________________________________
458void AliDielectronHFhelper::PrintCuts()
459{
460
461 //
462 // Print out object contents
463 //
464
465 // loop over all cuts
466 AliInfo(" Selected cuts:");
467 for(Int_t icut=0; icut<fCutLowLimits.GetNrows(); icut++)
468 AliInfo(Form(" %d: %s [%.2f,%.2f]",icut,fCutVars->At(icut)->GetName(),fCutLowLimits(icut),fCutUpLimits(icut)));
187de573 469
5e2cf960 470}
471