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