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