]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/hfe/AliHFEvarManager.cxx
a4a4d68a6fdbac856cf97fc920cae839765c8162
[u/mrichter/AliRoot.git] / PWG3 / 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       break;
329     }
330     case kSpecies:  value = aprioriPID; break; 
331     case kCentrality:  value = centrality; break;  
332   };
333   return value;
334 }
335
336 //____________________________________________________________
337 void AliHFEvarManager::FillArray(AliVParticle *track, Double_t* container, Float_t centrality, Int_t aprioriPID) const{
338         //
339         // Fill array with variables
340         //
341   TIter vars(fVariables);
342   AliHFEvariable *var = NULL;
343   Int_t counter = 0;
344   while((var = dynamic_cast<AliHFEvariable *>(vars())))
345     container[counter++] = GetValue(track , var->GetVarCode(), centrality, aprioriPID);
346 }
347
348 //____________________________________________________________
349 void AliHFEvarManager::FillContainer(AliCFContainer *cont, Int_t step, Bool_t useMC) const{
350         //
351         // Fill CF container with defined content
352         //
353
354         // Do reweighting if necessary
355   Double_t *content = fContent;
356   if(useMC) content = fContentMC;
357         cont->Fill(content, step, fWeightFactor);
358 }
359
360 //____________________________________________________________
361 void AliHFEvarManager::FillContainer(const AliHFEcontainer *const cont, const Char_t *contname, UInt_t step, Bool_t useMC, Double_t externalWeight) const {
362         //
363         // Fill CF container with defined content
364         //
365
366   // Do reweighting if necessary
367   Double_t *content = fContent;
368   if(useMC) content = fContentMC;
369         cont->FillCFContainer(contname, step, content, fWeightFactor * externalWeight);
370 }  
371
372 //____________________________________________________________
373 void AliHFEvarManager::FillContainerStepname(const AliHFEcontainer *const cont, const Char_t *contname, const Char_t *step, Bool_t useMC, Double_t externalWeight) const {
374         //
375         // Fill CF container with defined content
376         //
377
378   // Do reweighting if necessary
379   Double_t *content = fContent;
380   if(useMC) content = fContentMC;
381         cont->FillCFContainerStepname(contname, step, content, fWeightFactor * externalWeight);
382 }  
383
384 //____________________________________________________________
385 void AliHFEvarManager::FillCorrelationMatrix(THnSparseF *matrix) const {
386         //
387         // Fill Correlation Matrix
388         //
389
390         // Do reweighting if necessary
391   Int_t nVars = fVariables->GetEntriesFast();
392   Double_t *content = new Double_t[2*nVars];
393   memcpy(&content[0], fContent, sizeof(Double_t) * nVars);
394   memcpy(&content[nVars], fContentMC, sizeof(Double_t) * nVars);
395   matrix->Fill(content, fWeightFactor);
396   delete[] content;
397
398 }
399
400 //_______________________________________________
401 void AliHFEvarManager::SetWeightFactors(TH3F *weightFactors){
402         //
403         // Set the histos with the weights for the efficiency maps
404         //
405         fWeighting = kTRUE;
406         fWeightFactors = weightFactors;
407 }
408
409 //_______________________________________________
410 void AliHFEvarManager::SetWeightFactorsFunction(TF3 *weightFactorsFunction){
411         //
412         // Set the histos with the weights for the efficiency maps
413         //
414         fWeighting = kTRUE;
415         fWeightFactorsFunction = weightFactorsFunction;
416 }
417
418 //_______________________________________________
419 Double_t AliHFEvarManager::FindWeight(Double_t pt, Double_t eta, Double_t phi) const {
420         //
421         // Find the weight corresponding to pt eta and phi in the TH3D
422         //
423         Double_t weight = 1.0;
424         if(fWeightFactors) {
425
426                 TAxis *ptaxis = fWeightFactors->GetXaxis();
427                 TAxis *etaaxis = fWeightFactors->GetYaxis();
428                 TAxis *phiaxis = fWeightFactors->GetZaxis();
429
430                 Int_t ptbin = ptaxis->FindBin(pt);
431                 Int_t etabin = etaaxis->FindBin(eta);
432                 Int_t phibin = phiaxis->FindBin(phi);
433
434
435                 weight = fWeightFactors->GetBinContent(ptbin,etabin,phibin);
436         }
437         else if(fWeightFactorsFunction) {
438
439                 weight = fWeightFactorsFunction->Eval(pt,eta,phi);
440
441         }
442
443         AliDebug(2, Form("pt %f, eta %f, phi %f, weight %f",pt,eta,phi,weight));
444
445         return weight;
446 }
447
448 //_______________________________________________
449 AliHFEvarManager::AliHFEvariable::AliHFEvariable():
450     TNamed()
451   , fCode(0)
452   , fNBins(0)
453   , fMin(0)
454   , fMax(0)
455   , fBinning(NULL)
456   , fIsLogarithmic(kFALSE)
457 {
458   // 
459   // Dummy constructor
460   //
461 }
462
463 //_______________________________________________
464 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const Char_t *name, const Char_t *title, UInt_t code, UInt_t nBins, const Double_t *binning):
465     TNamed(name, title)
466   , fCode(code)
467   , fNBins(nBins)
468   , fMin(0.)
469   , fMax(0.)
470   , fBinning(NULL)
471   , fIsLogarithmic(kFALSE)
472 {
473   // 
474   // Default constructor
475   //
476   fBinning = new Double_t[nBins+1];
477   memcpy(fBinning, binning, sizeof(Double_t) * (nBins+1));
478 }
479
480 //_______________________________________________
481 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):
482     TNamed(name, title)
483   , fCode(code)
484   , fNBins(nBins)
485   , fMin(min)
486   , fMax(max)
487   , fBinning(NULL)
488   , fIsLogarithmic(isLogarithmic)
489 {
490   // 
491   // Default constructor
492   //
493 }
494
495 //_______________________________________________
496 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const AliHFEvarManager::AliHFEvariable &ref):
497     TNamed(ref)
498   , fCode(ref.fCode)
499   , fNBins(ref.fNBins)
500   , fMin(ref.fMin)
501   , fMax(ref.fMax)
502   , fBinning(NULL)
503   , fIsLogarithmic(ref.fIsLogarithmic)
504 {
505   // 
506   // Copy constructor
507   //
508   if(ref.fBinning){
509     fBinning = new Double_t[ref.fNBins + 1];
510     memcpy(fBinning, ref.fBinning, sizeof(Double_t) * (ref.fNBins + 1));
511   }
512 }
513
514 //_______________________________________________
515 AliHFEvarManager::AliHFEvariable& AliHFEvarManager::AliHFEvariable::operator=(const AliHFEvarManager::AliHFEvariable &ref){
516   //
517   // Assignment operator
518   //
519   
520   if(&ref != this){
521     TNamed::operator=(ref);
522     fCode = ref.fCode;
523     fNBins = ref.fNBins;
524     fMax = ref.fMax;
525     fMin = ref.fMin;
526     if(ref.fBinning){
527       fBinning = new Double_t[ref.fNBins + 1];
528       memcpy(fBinning, ref.fBinning, sizeof(Double_t) * (ref.fNBins + 1));
529     } else fBinning = NULL;
530     fIsLogarithmic = ref.fIsLogarithmic;
531   }
532   return *this;
533 }
534
535 //_______________________________________________
536 AliHFEvarManager::AliHFEvariable::~AliHFEvariable(){
537   //
538   // Destruktor
539   //
540   if(fBinning) delete fBinning;
541 }
542