]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/hfe/AliHFEvarManager.cxx
Updates + addition of EMCal
[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   // Value derived via GetValue()
154   //
155    AliDebug(1, Form("Var Name: %s", name.Data()));
156
157   if(!name.CompareTo("pt")) 
158     fVariables->AddLast(new AliHFEvariable("pt", "pt", kPt, 44, 0.1, 20, kTRUE));
159   else if(!name.CompareTo("eta"))
160     fVariables->AddLast(new AliHFEvariable("eta", "eta", kEta, 8, -0.8, 0.8));
161   else if(!name.CompareTo("phi"))
162     fVariables->AddLast(new AliHFEvariable("phi", "phi", kPhi, 18, -0, 2*TMath::Pi()));
163   else if(!name.CompareTo("charge"))
164     fVariables->AddLast(new AliHFEvariable("charge", "charge", kCharge, 2, -1.1, 1.1));
165   else if(!name.CompareTo("source"))
166     fVariables->AddLast(new AliHFEvariable("source", "source", kSource, 5, 0, 5));
167   else if(!name.CompareTo("centrality"))
168     fVariables->AddLast(new AliHFEvariable("centrality", "centrality", kCentrality, 11, 0.0, 11.0));
169   else if(!name.CompareTo("species"))
170     fVariables->AddLast(new AliHFEvariable("species", "species", kSpecies, 6, -1, 5));
171
172   // More to come ...
173 }
174
175 //____________________________________________________________
176 Bool_t AliHFEvarManager::IsVariableDefined(TString name){
177   //
178   // Add new variable to the var manager
179   // Value derived via GetValue()
180   //
181    AliDebug(1, Form("Var Name: %s", name.Data()));
182
183    AliHFEvariable *u = (AliHFEvariable *) fVariables->FindObject((const char*)name);
184    if(u) return kTRUE;
185    else return kFALSE;
186   
187    // More to come ...
188 }
189
190 //____________________________________________________________
191 void AliHFEvarManager::DefineVariables(AliHFEcontainer *cont){
192         //
193         // Define Variables
194         //
195   Int_t nVars = fVariables->GetEntriesFast();
196   cont->SetNumberOfVariables(nVars);
197   TIter vars(fVariables);
198   AliHFEvariable *var;
199   Int_t counter = 0;
200   while((var = dynamic_cast<AliHFEvariable *>(vars()))){
201     cont->SetVariableName(counter, var->GetName());
202     if(var->IsLogarithmic())
203       cont->MakeLogarithmicBinning(counter, var->GetNumberOfBins(), var->GetMinimum(), var->GetMaximum());
204     else
205       cont->MakeLinearBinning(counter, var->GetNumberOfBins(), var->GetMinimum(), var->GetMaximum());
206     counter++;
207   }
208         fContent = new Double_t[nVars]; 
209   memset(fContent, 0, sizeof(Double_t) * nVars);
210   fContentMC = new Double_t[nVars]; 
211   memset(fContentMC, 0, sizeof(Double_t) * nVars);
212 }
213
214 //____________________________________________________________
215 void AliHFEvarManager::NewTrack(AliVParticle *recTrack, AliVParticle *mcTrack, Float_t centrality, Int_t aprioriPID, Bool_t signal){
216   //
217   // Cache information for new track pair
218   //
219   AliDebug(1, "Filling new Track");
220   fSignalTrack = signal;
221   FillArray(recTrack, fContent, centrality, aprioriPID);
222   if(mcTrack) FillArray(mcTrack, fContentMC, centrality, aprioriPID);
223   if(fWeighting){
224     Int_t indexpt = -1, indexeta = -1, indexphi = -1, counter = 0;
225     AliHFEvariable *var = NULL;
226     TIter vars(fVariables);
227     while((var = dynamic_cast<AliHFEvariable *>(vars()))){
228       switch(var->GetVarCode()){
229         case kPt:   indexpt = counter;  break;
230         case kEta:  indexeta = counter; break;
231         case kPhi:  indexphi = counter; break;
232       };
233       if(indexpt >= 0 && indexeta >= 0 && indexphi >= 0) // all dimensions found
234         break;
235       counter++;
236     }
237     if(indexpt >= 0 && indexeta >= 0 && indexphi >= 0)
238       fWeightFactor = FindWeight(fContent[indexpt], fContent[indexeta], fContent[indexphi]);
239   }
240 }
241
242 //____________________________________________________________
243 Double_t AliHFEvarManager::GetValue(AliVParticle *track, UInt_t code, Float_t centrality, Int_t aprioriPID) const {
244   //
245   // Definition of the variables
246   //
247   if(!track) return 0.;
248   Double_t value = 0.;
249   switch(code){
250     case kPt:       value = track->Pt();  break;
251     case kEta:      value = track->Eta(); break;
252     case kPhi:      value = track->Phi(); break;
253     case kCharge:{
254       value = track->Charge();
255       if(TString(track->IsA()->GetName()).Contains("MC")) value /= 3;
256       break;
257     }
258     case kSource:{
259       if(fSignal){
260               if(fSignal->IsCharmElectron(track)) value = 0;
261               else if(fSignal->IsBeautyElectron(track)) value = 1;
262               else if(fSignal->IsGammaElectron(track)) value = 2;
263         else if(fSignal->IsNonHFElectron(track)) value = 3;
264               else value = 4;
265       }
266       break;
267     }
268     case kSpecies:  value = aprioriPID; break; 
269     case kCentrality:  value = centrality; break;  
270   };
271   return value;
272 }
273
274 //____________________________________________________________
275 void AliHFEvarManager::FillArray(AliVParticle *track, Double_t* container, Float_t centrality, Int_t aprioriPID) const{
276         //
277         // Fill array with variables
278         //
279   TIter vars(fVariables);
280   AliHFEvariable *var = NULL;
281   Int_t counter = 0;
282   while((var = dynamic_cast<AliHFEvariable *>(vars())))
283     container[counter++] = GetValue(track , var->GetVarCode(), centrality, aprioriPID);
284 }
285
286 //____________________________________________________________
287 void AliHFEvarManager::FillContainer(AliCFContainer *cont, Int_t step, Bool_t useMC) const{
288         //
289         // Fill CF container with defined content
290         //
291
292         // Do reweighting if necessary
293   Double_t *content = fContent;
294   if(useMC) content = fContentMC;
295         cont->Fill(content, step, fWeightFactor);
296 }
297
298 //____________________________________________________________
299 void AliHFEvarManager::FillContainer(const AliHFEcontainer *const cont, const Char_t *contname, UInt_t step, Bool_t useMC, Double_t externalWeight) const {
300         //
301         // Fill CF container with defined content
302         //
303
304   // Do reweighting if necessary
305   Double_t *content = fContent;
306   if(useMC) content = fContentMC;
307         cont->FillCFContainer(contname, step, content, fWeightFactor * externalWeight);
308 }  
309
310 //____________________________________________________________
311 void AliHFEvarManager::FillContainerStepname(const AliHFEcontainer *const cont, const Char_t *contname, const Char_t *step, Bool_t useMC, Double_t externalWeight) const {
312         //
313         // Fill CF container with defined content
314         //
315
316   // Do reweighting if necessary
317   Double_t *content = fContent;
318   if(useMC) content = fContentMC;
319         cont->FillCFContainerStepname(contname, step, content, fWeightFactor * externalWeight);
320 }  
321
322 //____________________________________________________________
323 void AliHFEvarManager::FillCorrelationMatrix(THnSparseF *matrix) const {
324         //
325         // Fill Correlation Matrix
326         //
327
328         // Do reweighting if necessary
329   Int_t nVars = fVariables->GetEntriesFast();
330   Double_t *content = new Double_t[2*nVars];
331   memcpy(&content[0], fContent, sizeof(Double_t) * nVars);
332   memcpy(&content[nVars], fContentMC, sizeof(Double_t) * nVars);
333   matrix->Fill(content, fWeightFactor);
334   if(content) delete[] content;
335
336 }
337
338 //_______________________________________________
339 void AliHFEvarManager::SetWeightFactors(TH3F *weightFactors){
340         //
341         // Set the histos with the weights for the efficiency maps
342         //
343         fWeighting = kTRUE;
344         fWeightFactors = weightFactors;
345 }
346
347 //_______________________________________________
348 void AliHFEvarManager::SetWeightFactorsFunction(TF3 *weightFactorsFunction){
349         //
350         // Set the histos with the weights for the efficiency maps
351         //
352         fWeighting = kTRUE;
353         fWeightFactorsFunction = weightFactorsFunction;
354 }
355
356 //_______________________________________________
357 Double_t AliHFEvarManager::FindWeight(Double_t pt, Double_t eta, Double_t phi) const {
358         //
359         // Find the weight corresponding to pt eta and phi in the TH3D
360         //
361         Double_t weight = 1.0;
362         if(fWeightFactors) {
363
364                 TAxis *ptaxis = fWeightFactors->GetXaxis();
365                 TAxis *etaaxis = fWeightFactors->GetYaxis();
366                 TAxis *phiaxis = fWeightFactors->GetZaxis();
367
368                 Int_t ptbin = ptaxis->FindBin(pt);
369                 Int_t etabin = etaaxis->FindBin(eta);
370                 Int_t phibin = phiaxis->FindBin(phi);
371
372
373                 weight = fWeightFactors->GetBinContent(ptbin,etabin,phibin);
374         }
375         else if(fWeightFactorsFunction) {
376
377                 weight = fWeightFactorsFunction->Eval(pt,eta,phi);
378
379         }
380
381         AliDebug(2, Form("pt %f, eta %f, phi %f, weight %f",pt,eta,phi,weight));
382
383         return weight;
384 }
385
386 //_______________________________________________
387 AliHFEvarManager::AliHFEvariable::AliHFEvariable():
388     TNamed()
389   , fCode(0)
390   , fNBins(0)
391   , fMin(0)
392   , fMax(0)
393   , fIsLogarithmic(kFALSE)
394 {
395   // 
396   // Dummy constructor
397   //
398 }
399
400 //_______________________________________________
401 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):
402     TNamed(name, title)
403   , fCode(code)
404   , fNBins(nBins)
405   , fMin(min)
406   , fMax(max)
407   , fIsLogarithmic(isLogarithmic)
408 {
409   // 
410   // Default constructor
411   //
412 }
413
414 //_______________________________________________
415 AliHFEvarManager::AliHFEvariable::AliHFEvariable(const AliHFEvarManager::AliHFEvariable &ref):
416     TNamed(ref)
417   , fCode(ref.fCode)
418   , fNBins(ref.fNBins)
419   , fMin(ref.fMin)
420   , fMax(ref.fMax)
421   , fIsLogarithmic(ref.fIsLogarithmic)
422 {
423   // 
424   // Copy constructor
425   //
426 }
427
428 //_______________________________________________
429 AliHFEvarManager::AliHFEvariable& AliHFEvarManager::AliHFEvariable::operator=(const AliHFEvarManager::AliHFEvariable &ref){
430   //
431   // Assignment operator
432   //
433   
434   if(&ref != this){
435     TNamed::operator=(ref);
436     fCode = ref.fCode;
437     fNBins = ref.fNBins;
438     fMax = ref.fMax;
439     fMin = ref.fMin;
440     fIsLogarithmic = ref.fIsLogarithmic;
441   }
442   return *this;
443 }
444