]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/hfe/AliHFEvarManager.cxx
some updates for e v2 by TPC+EMCal EP
[u/mrichter/AliRoot.git] / PWGHF / hfe / AliHFEvarManager.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 // Class AliHFEvarManager:
17 // Contains definition of the variables which are filled into the 
18 // Correction framework container. The class handles both AliCFContainer and
19 // AliHFEcontainer.
20 // Defining a new variable which has to be monitored can be done via the
21 // function AddVariable. This function also defines a new dimension for the
22 // particle container and appends it to the list of variables. For a new
23 // track the function NewTrack caches the values to be filled into the 
24 // correction framework container. With FillContainer the specified 
25 // correction framework container is filled. The VarManager also handles
26 // the filling of the correlation matrices
27 //
28 // Author:
29 //   Markus Fasel <M.Fasel@gsi.de>
30 //
31 #include <TClass.h>
32 #include <TH3.h>
33 #include <TF3.h>
34 #include <TMath.h>
35 #include <THnSparse.h>
36 #include <TString.h>
37
38 #include "AliCFContainer.h"
39 #include "AliLog.h"
40 #include "AliVParticle.h"
41
42 #include "AliHFEcontainer.h"
43 #include "AliHFEsignalCuts.h"
44 #include "AliHFEtools.h"
45 #include "AliHFEvarManager.h"
46
47 ClassImp(AliHFEvarManager)
48 ClassImp(AliHFEvarManager::AliHFEvariable);
49
50 //____________________________________________________________
51 AliHFEvarManager::AliHFEvarManager():
52   TNamed(),
53   fVariables(NULL),
54   fContent(NULL),
55   fContentMC(NULL),
56   fWeightFactor(1.),
57   fSignalTrack(kTRUE),
58   fWeighting(kFALSE),
59   fSignal(NULL),
60   fWeightFactors(NULL),
61   fWeightFactorsFunction(NULL)
62 {
63         //
64         // Dummy constructor
65   //
66   SetOwner();
67 }
68
69 //____________________________________________________________
70 AliHFEvarManager::AliHFEvarManager(const Char_t *name):
71   TNamed(name, ""),
72   fVariables(NULL),
73   fContent(NULL),
74   fContentMC(NULL),
75   fWeightFactor(1.),
76   fSignalTrack(kTRUE),
77   fWeighting(kFALSE),
78   fSignal(NULL),
79   fWeightFactors(NULL),
80   fWeightFactorsFunction(NULL)
81 {
82         //
83         // Default constructor
84   //
85   fVariables = new TObjArray;
86   SetOwner();
87 }
88
89 //____________________________________________________________
90 AliHFEvarManager::AliHFEvarManager(const AliHFEvarManager &ref):
91   TNamed(ref),
92   fVariables(NULL),
93   fContent(NULL),
94   fContentMC(NULL),
95   fWeightFactor(ref.fWeightFactor),
96   fSignalTrack(ref.fSignalTrack),
97   fWeighting(ref.fWeighting),
98   fSignal(NULL),
99   fWeightFactors(NULL),
100   fWeightFactorsFunction(NULL)
101 {
102   //
103   // Copy Constructor
104   //
105   ref.Copy(*this);
106 }
107
108 //____________________________________________________________
109 AliHFEvarManager &AliHFEvarManager::operator=(const AliHFEvarManager &ref){
110   //
111   // Assignment operator
112   //
113   if(&ref != this){ 
114     this->~AliHFEvarManager();
115     ref.Copy(*this);
116   }
117   return *this;
118 }
119
120 //____________________________________________________________
121 AliHFEvarManager::~AliHFEvarManager(){
122         //
123         // Destructor
124         //
125   if(IsOwner()){
126     if(fVariables) delete fVariables;
127   }
128   if(fContent) delete[] fContent;
129   if(fContentMC) delete[] fContentMC;
130 }
131
132 //____________________________________________________________
133 void AliHFEvarManager::Copy(TObject &o) const{
134   //
135   // Make Copy
136   //
137   AliHFEvarManager &target = dynamic_cast<AliHFEvarManager &>(o);
138   target.fVariables = fVariables;
139   target.fContent = new Double_t[sizeof(fContent)/sizeof(Double_t)]; 
140   target.fContentMC = new Double_t[sizeof(fContentMC)/sizeof(Double_t)];
141   target.fWeightFactor = fWeightFactor;
142   target.fSignalTrack = fSignalTrack;
143   target.fWeighting = fWeighting;
144   target.fSignal = fSignal;
145   target.fWeightFactors = fWeightFactors;
146   target.fWeightFactorsFunction = fWeightFactorsFunction; 
147   target.SetOwner(kFALSE);
148 }
149
150 //____________________________________________________________
151 void AliHFEvarManager::AddVariable(TString name){
152   //
153   // Add new variable to the var manager
154   // Use default binning
155   // Value derived via GetValue()
156   //
157    AliDebug(1, Form("Var Name: %s", name.Data()));
158
159   if(!name.CompareTo("pt")) 
160     fVariables->AddLast(new AliHFEvariable("pt", "pt", kPt, 44, 0.1, 20, kTRUE));
161   else if(!name.CompareTo("eta"))
162     fVariables->AddLast(new AliHFEvariable("eta", "eta", kEta, 8, -0.8, 0.8));
163   else if(!name.CompareTo("phi"))
164     fVariables->AddLast(new AliHFEvariable("phi", "phi", kPhi, 18, -0, 2*TMath::Pi()));
165   else if(!name.CompareTo("charge"))
166     fVariables->AddLast(new AliHFEvariable("charge", "charge", kCharge, 2, -1.1, 1.1));
167   else if(!name.CompareTo("source"))
168     fVariables->AddLast(new AliHFEvariable("source", "source", kSource, 8, 0, 8));
169   else if(!name.CompareTo("centrality"))
170     fVariables->AddLast(new AliHFEvariable("centrality", "centrality", kCentrality, 11, 0.0, 11.0));
171   else if(!name.CompareTo("species"))
172     fVariables->AddLast(new AliHFEvariable("species", "species", kSpecies, 6, -1, 5));
173
174   // More to come ...
175 }
176
177 //____________________________________________________________
178 void AliHFEvarManager::AddVariable(TString name, Int_t nBins, Double_t min, Double_t max, Bool_t isLogarithmic){
179   //
180   // Add new variable to the var manager
181   // User can define minimum and maximum
182   // Function can be used to copy variables to a new var manager
183   // Value derived via GetValue()
184   //
185   AliDebug(1, Form("Var Name: %s", name.Data()));
186
187   UInt_t varcode = 1000; 
188   if(!name.CompareTo("pt")){
189     varcode = kPt;
190   } else if(!name.CompareTo("eta")){
191     varcode = kEta;
192   } else if(!name.CompareTo("phi")){
193     varcode = kPhi;
194   } else if(!name.CompareTo("charge")){
195     varcode = kCharge;
196   } else if(!name.CompareTo("source")){
197     varcode = kSource;
198   } else if(!name.CompareTo("species")){
199     varcode = kSpecies;
200   } else if(!name.CompareTo("centrality")) {
201     varcode = kCentrality;
202   } else {
203     AliError("Variable not defined or not supposed to have a user-defined binning.");
204   }
205
206   if(varcode < 1000) fVariables->AddLast(new AliHFEvariable(name.Data(), name.Data(), varcode, nBins, min, max, isLogarithmic));
207   // More to come ...
208 }
209
210 //____________________________________________________________
211 void AliHFEvarManager::AddVariable(TString name, Int_t nBins, const Double_t *binning){
212   //
213   // Add new variable to the var manager
214   // Not to be applied for Variables which have a limited amount of possible values like charge, species, source
215   // Value derived via GetValue()
216   //
217   AliDebug(1, Form("Var Name: %s", name.Data()));
218
219   UInt_t varcode = 1000; 
220   if(!name.CompareTo("pt")){
221     varcode = kPt;
222   } else if(!name.CompareTo("eta")){
223     varcode = kEta;
224   } else if(!name.CompareTo("phi")){
225     varcode = kPhi;
226   } else if(!name.CompareTo("centrality")) {
227     varcode = kCentrality;
228   } else {
229     AliError("Variable not defined or not supposed to have a user-defined binning.");
230   }
231
232   if(varcode < 1000) fVariables->AddLast(new AliHFEvariable(name.Data(), name.Data(), varcode, nBins, binning));
233   // More to come ...
234 }
235
236 //____________________________________________________________
237 Bool_t AliHFEvarManager::IsVariableDefined(TString name){
238   //
239   // Add new variable to the var manager
240   // Value derived via GetValue()
241   //
242    AliDebug(1, Form("Var Name: %s", name.Data()));
243
244    AliHFEvariable *u = (AliHFEvariable *) fVariables->FindObject((const char*)name);
245    if(u) return kTRUE;
246    else return kFALSE;
247   
248    // More to come ...
249 }
250
251 //____________________________________________________________
252 void AliHFEvarManager::DefineVariables(AliHFEcontainer *cont){
253         //
254         // Define Variables
255         //
256   Int_t nVars = fVariables->GetEntriesFast();
257   cont->SetNumberOfVariables(nVars);
258   TIter vars(fVariables);
259   AliHFEvariable *var;
260   Int_t counter = 0;
261   while((var = dynamic_cast<AliHFEvariable *>(vars()))){
262     cont->SetVariableName(counter, var->GetName());
263     if(var->IsLogarithmic())
264       cont->MakeLogarithmicBinning(counter, var->GetNumberOfBins(), var->GetMinimum(), var->GetMaximum());
265     else if(var->HasUserDefinedBinning())
266       cont->MakeUserDefinedBinning(counter, var->GetNumberOfBins(), var->GetBinning());
267     else
268       cont->MakeLinearBinning(counter, var->GetNumberOfBins(), var->GetMinimum(), var->GetMaximum());
269     counter++;
270   }
271         fContent = new Double_t[nVars]; 
272   memset(fContent, 0, sizeof(Double_t) * nVars);
273   fContentMC = new Double_t[nVars]; 
274   memset(fContentMC, 0, sizeof(Double_t) * nVars);
275 }
276
277 //____________________________________________________________
278 void AliHFEvarManager::NewTrack(AliVParticle *recTrack, AliVParticle *mcTrack, Float_t centrality, Int_t aprioriPID, Bool_t signal){
279   //
280   // Cache information for new track pair
281   //
282   AliDebug(1, "Filling new Track");
283   fSignalTrack = signal;
284   FillArray(recTrack, fContent, centrality, aprioriPID);
285   if(mcTrack) FillArray(mcTrack, fContentMC, centrality, aprioriPID);
286   if(fWeighting){
287     Int_t indexpt = -1, indexeta = -1, indexphi = -1, counter = 0;
288     AliHFEvariable *var = NULL;
289     TIter vars(fVariables);
290     while((var = dynamic_cast<AliHFEvariable *>(vars()))){
291       switch(var->GetVarCode()){
292         case kPt:   indexpt = counter;  break;
293         case kEta:  indexeta = counter; break;
294         case kPhi:  indexphi = counter; break;
295       };
296       if(indexpt >= 0 && indexeta >= 0 && indexphi >= 0) // all dimensions found
297         break;
298       counter++;
299     }
300     if(indexpt >= 0 && indexeta >= 0 && indexphi >= 0)
301       fWeightFactor = FindWeight(fContent[indexpt], fContent[indexeta], fContent[indexphi]);
302   }
303 }
304
305 //____________________________________________________________
306 Double_t AliHFEvarManager::GetValue(AliVParticle *track, UInt_t code, Float_t centrality, Int_t aprioriPID) const {
307   //
308   // Definition of the variables
309   //
310   if(!track) return 0.;
311   Double_t value = 0.;
312   switch(code){
313     case kPt:       value = track->Pt();  break;
314     case kEta:      value = track->Eta(); break;
315     case kPhi:      value = track->Phi(); break;
316     case kCharge:{
317       value = track->Charge();
318       if(TString(track->IsA()->GetName()).Contains("MC")) value /= 3;
319       break;
320     }
321     case kSource:{
322       if(fSignal){
323         value = static_cast<Int_t>(fSignal->GetSignalSource(track));
324         /*
325               if(fSignal->IsCharmElectron(track)) value = 0;
326               else if(fSignal->IsBeautyElectron(track)) value = 1;
327               else if(fSignal->IsGammaElectron(track)) value = 2;
328         else if(fSignal->IsNonHFElectron(track)) value = 3;
329         else if(fSignal->IsJpsiElectron(track)) value = 4;
330         else if(fSignal->IsB2JpsiElectron(track)) value = 5;
331         else if(fSignal->IsKe3Electron(track)) value = 6;
332               else value = 7;
333         */
334       }
335       AliDebug(2, Form("source: %f", value));
336       break;
337     }
338     case kSpecies:  value = aprioriPID; break; 
339     case kCentrality:  value = centrality; break;  
340   };
341   return value;
342 }
343
344 //____________________________________________________________
345 void AliHFEvarManager::FillArray(AliVParticle *track, Double_t* container, Float_t centrality, Int_t aprioriPID) const{
346         //
347         // Fill array with variables
348         //
349   TIter vars(fVariables);
350   AliHFEvariable *var = NULL;
351   Int_t counter = 0;
352   while((var = dynamic_cast<AliHFEvariable *>(vars())))
353     container[counter++] = GetValue(track , var->GetVarCode(), centrality, aprioriPID);
354 }
355
356 //____________________________________________________________
357 void AliHFEvarManager::FillContainer(AliCFContainer *cont, Int_t step, Bool_t useMC) const{
358         //
359         // Fill CF container with defined content
360         //
361
362         // Do reweighting if necessary
363   Double_t *content = fContent;
364   if(useMC) content = fContentMC;
365         cont->Fill(content, step, fWeightFactor);
366 }
367
368 //____________________________________________________________
369 void AliHFEvarManager::FillContainer(const AliHFEcontainer *const cont, const Char_t *contname, UInt_t step, Bool_t useMC, Double_t externalWeight) const {
370         //
371         // Fill CF container with defined content
372         //
373
374   // Do reweighting if necessary
375   Double_t *content = fContent;
376   if(useMC) content = fContentMC;
377         cont->FillCFContainer(contname, step, content, fWeightFactor * externalWeight);
378 }  
379
380 //____________________________________________________________
381 void AliHFEvarManager::FillContainerStepname(const AliHFEcontainer *const cont, const Char_t *contname, const Char_t *step, Bool_t useMC, Double_t externalWeight) const {
382         //
383         // Fill CF container with defined content
384         //
385
386   // Do reweighting if necessary
387   Double_t *content = fContent;
388   if(useMC) content = fContentMC;
389         cont->FillCFContainerStepname(contname, step, content, fWeightFactor * externalWeight);
390 }  
391
392 //____________________________________________________________
393 void AliHFEvarManager::FillCorrelationMatrix(THnSparseF *matrix) const {
394         //
395         // Fill Correlation Matrix
396         //
397
398         // Do reweighting if necessary
399   Int_t nVars = fVariables->GetEntriesFast();
400   Double_t *content = new Double_t[2*nVars];
401   memcpy(&content[0], fContent, sizeof(Double_t) * nVars);
402   memcpy(&content[nVars], fContentMC, sizeof(Double_t) * nVars);
403   matrix->Fill(content, fWeightFactor);
404   delete[] content;
405
406 }
407
408 //_______________________________________________
409 void AliHFEvarManager::SetWeightFactors(TH3F *weightFactors){
410         //
411         // Set the histos with the weights for the efficiency maps
412         //
413         fWeighting = kTRUE;
414         fWeightFactors = weightFactors;
415 }
416
417 //_______________________________________________
418 void AliHFEvarManager::SetWeightFactorsFunction(TF3 *weightFactorsFunction){
419         //
420         // Set the histos with the weights for the efficiency maps
421         //
422         fWeighting = kTRUE;
423         fWeightFactorsFunction = weightFactorsFunction;
424 }
425
426 //_______________________________________________
427 Double_t AliHFEvarManager::FindWeight(Double_t pt, Double_t eta, Double_t phi) const {
428         //
429         // Find the weight corresponding to pt eta and phi in the TH3D
430         //
431         Double_t weight = 1.0;
432         if(fWeightFactors) {
433
434                 TAxis *ptaxis = fWeightFactors->GetXaxis();
435                 TAxis *etaaxis = fWeightFactors->GetYaxis();
436                 TAxis *phiaxis = fWeightFactors->GetZaxis();
437
438                 Int_t ptbin = ptaxis->FindBin(pt);
439                 Int_t etabin = etaaxis->FindBin(eta);
440                 Int_t phibin = phiaxis->FindBin(phi);
441
442
443                 weight = fWeightFactors->GetBinContent(ptbin,etabin,phibin);
444         }
445         else if(fWeightFactorsFunction) {
446
447                 weight = fWeightFactorsFunction->Eval(pt,eta,phi);
448
449         }
450
451         AliDebug(2, Form("pt %f, eta %f, phi %f, weight %f",pt,eta,phi,weight));
452
453         return weight;
454 }
455
456 //_______________________________________________
457 AliHFEvarManager::AliHFEvariable::AliHFEvariable():
458     TNamed()
459   , fCode(0)
460   , fNBins(0)
461   , fMin(0)
462   , fMax(0)
463   , fBinning()
464   , fIsLogarithmic(kFALSE)
465   , fUserDefinedBinning(kFALSE)
466 {
467   // 
468   // Dummy constructor
469   //
470 }
471
472 //_______________________________________________
473 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const Char_t *name, const Char_t *title, UInt_t code, UInt_t nBins, const Double_t *binning):
474     TNamed(name, title)
475   , fCode(code)
476   , fNBins(nBins)
477   , fMin(0.)
478   , fMax(0.)
479   , fBinning()
480   , fIsLogarithmic(kFALSE)
481   , fUserDefinedBinning(kTRUE)
482 {
483   // 
484   // Default constructor
485   //
486   fBinning.Set(nBins+1);
487   memcpy(fBinning.GetArray(), binning, sizeof(Double_t) * (nBins+1));
488 }
489
490 //_______________________________________________
491 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const Char_t *name, const Char_t *title, UInt_t code, UInt_t nBins, Double_t min, Double_t max, Bool_t isLogarithmic):
492     TNamed(name, title)
493   , fCode(code)
494   , fNBins(nBins)
495   , fMin(min)
496   , fMax(max)
497   , fBinning()
498   , fIsLogarithmic(isLogarithmic)
499   , fUserDefinedBinning(kFALSE)
500 {
501   // 
502   // Default constructor
503   //
504 }
505
506 //_______________________________________________
507 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const AliHFEvarManager::AliHFEvariable &ref):
508     TNamed(ref)
509   , fCode(ref.fCode)
510   , fNBins(ref.fNBins)
511   , fMin(ref.fMin)
512   , fMax(ref.fMax)
513   , fBinning(ref.fBinning)
514   , fIsLogarithmic(ref.fIsLogarithmic)
515   , fUserDefinedBinning(ref.fUserDefinedBinning)
516 {
517   // 
518   // Copy constructor
519   //
520 }
521
522 //_______________________________________________
523 AliHFEvarManager::AliHFEvariable& AliHFEvarManager::AliHFEvariable::operator=(const AliHFEvarManager::AliHFEvariable &ref){
524   //
525   // Assignment operator
526   //
527   
528   if(&ref != this){
529     TNamed::operator=(ref);
530     fBinning = ref.fBinning;
531     fCode = ref.fCode;
532     fNBins = ref.fNBins;
533     fMax = ref.fMax;
534     fMin = ref.fMin;
535     fIsLogarithmic = ref.fIsLogarithmic;
536     fUserDefinedBinning = ref.fUserDefinedBinning;
537   }
538   return *this;
539 }
540
541 //_______________________________________________
542 AliHFEvarManager::AliHFEvariable::~AliHFEvariable(){
543   //
544   // Destruktor
545   //
546 }
547
548 //_______________________________________________
549 Double_t* AliHFEvarManager::AliHFEvariable::GetBinning() { 
550   if(fUserDefinedBinning) return fBinning.GetArray(); 
551
552   // No User defined Binning - create one and store in fBinning
553   if(fBinning.GetSize() != 0) return fBinning.GetArray();
554   else{
555     Double_t *binning;
556     if(fIsLogarithmic) binning = AliHFEtools::MakeLogarithmicBinning(fNBins, fMin, fMax);
557     else binning = AliHFEtools::MakeLogarithmicBinning(fNBins, fMin, fMax);
558     fBinning.Set(fNBins+1, binning);
559     delete []binning;
560     return fBinning.GetArray();
561   }
562 }
563