PartCorr split in 2 Base and Dep; coding violations corrected; PHOS geometry can...
[u/mrichter/AliRoot.git] / PWG4 / PartCorrBase / AliCaloPID.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 /* $Id: AliCaloPID.cxx 21839 2007-10-29 13:49:42Z gustavo $ */
16
17 //_________________________________________________________________________
18 // Class for track/cluster acceptance selection
19 // Selection in Central barrel, EMCAL and PHOS
20 //                
21 //*-- Author: Gustavo Conesa (LNF-INFN) 
22 //////////////////////////////////////////////////////////////////////////////
23
24
25 // --- ROOT system ---
26 #include <TMath.h>
27 #include <TLorentzVector.h>
28 #include <TString.h>
29 #include <TFormula.h>
30
31 //---- ANALYSIS system ----
32 #include "AliLog.h"
33 #include "AliCaloPID.h"
34 #include "AliAODCaloCluster.h"
35 #include "AliAODPWG4Particle.h"
36 #include "AliStack.h"
37 #include "TParticle.h"
38
39 ClassImp(AliCaloPID)
40
41
42 //________________________________________________
43 AliCaloPID::AliCaloPID() : 
44 TObject(), fEMCALPhotonWeight(0.), fEMCALPi0Weight(0.),  
45 fEMCALElectronWeight(0.),  fEMCALChargeWeight(0.),
46 fEMCALNeutralWeight(0.),
47 fPHOSPhotonWeight(0.), fPHOSPi0Weight(0.),  
48 fPHOSElectronWeight(0.), fPHOSChargeWeight(0.) , 
49 fPHOSNeutralWeight(0.), fPHOSWeightFormula(0), 
50 fPHOSPhotonWeightFormula(0x0), fPHOSPi0WeightFormula(0x0),
51 fDispCut(0.),fTOFCut(0.), fDebug(-1)
52 {
53         //Ctor
54         
55         //Initialize parameters
56         InitParameters();
57 }
58
59 //____________________________________________________________________________
60 AliCaloPID::AliCaloPID(const AliCaloPID & pid) :   
61 TObject(pid), fEMCALPhotonWeight(pid.fEMCALPhotonWeight), 
62 fEMCALPi0Weight(pid.fEMCALPi0Weight), 
63 fEMCALElectronWeight(pid.fEMCALElectronWeight), 
64 fEMCALChargeWeight(pid.fEMCALChargeWeight), 
65 fEMCALNeutralWeight(pid.fEMCALNeutralWeight), 
66 fPHOSPhotonWeight(pid.fPHOSPhotonWeight),
67 fPHOSPi0Weight(pid.fPHOSPi0Weight),
68 fPHOSElectronWeight(pid.fPHOSElectronWeight), 
69 fPHOSChargeWeight(pid.fPHOSChargeWeight),
70 fPHOSNeutralWeight(pid.fPHOSNeutralWeight),
71 fPHOSWeightFormula(pid.fPHOSWeightFormula), 
72 fPHOSPhotonWeightFormula(pid.fPHOSPhotonWeightFormula), 
73 fPHOSPi0WeightFormula(pid.fPHOSPi0WeightFormula), 
74 fDispCut(pid.fDispCut),fTOFCut(pid.fTOFCut),
75 fDebug(pid.fDebug)
76 {
77         // cpy ctor
78         
79 }
80
81 //_________________________________________________________________________
82 AliCaloPID & AliCaloPID::operator = (const AliCaloPID & pid)
83 {
84         // assignment operator
85         
86         if(&pid == this) return *this;
87         
88         fEMCALPhotonWeight = pid. fEMCALPhotonWeight ;
89         fEMCALPi0Weight = pid.fEMCALPi0Weight ;
90         fEMCALElectronWeight = pid.fEMCALElectronWeight; 
91         fEMCALChargeWeight = pid.fEMCALChargeWeight;
92         fEMCALNeutralWeight = pid.fEMCALNeutralWeight;
93         
94         fPHOSPhotonWeight = pid.fPHOSPhotonWeight ;
95         fPHOSPi0Weight = pid.fPHOSPi0Weight ;
96         fPHOSElectronWeight = pid.fPHOSElectronWeight; 
97         fPHOSChargeWeight = pid.fPHOSChargeWeight;
98         fPHOSNeutralWeight = pid.fPHOSNeutralWeight;
99         
100         fPHOSWeightFormula       = pid.fPHOSWeightFormula; 
101         fPHOSPhotonWeightFormula = pid.fPHOSPhotonWeightFormula; 
102         fPHOSPi0WeightFormula    = pid.fPHOSPi0WeightFormula;
103         
104         fDispCut  = pid.fDispCut;
105         fTOFCut   = pid.fTOFCut;
106         fDebug    = pid.fDebug;
107         
108         return *this;
109         
110 }
111
112 //_________________________________
113 AliCaloPID::~AliCaloPID() {
114         //Dtor
115         
116         if(fPHOSPhotonWeightFormula) delete  fPHOSPhotonWeightFormula ;
117         if(fPHOSPi0WeightFormula) delete  fPHOSPi0WeightFormula ;
118         
119 }
120
121
122 //_________________________________________________________________________
123 Int_t AliCaloPID::CheckOrigin(const Int_t label, AliStack * stack) const {
124         //Play with the MC stack if available
125         //Check origin of the candidates, good for PYTHIA
126         
127         if(!stack) AliFatal("Stack is not available, check analysis settings in configuration file, STOP!!");
128         
129         if(label >= 0 && label <  stack->GetNtrack()){
130                 //Mother
131                 TParticle * mom = stack->Particle(label);
132                 Int_t mPdg = TMath::Abs(mom->GetPdgCode());
133                 Int_t mStatus =  mom->GetStatusCode() ;
134                 Int_t iParent =  mom->GetFirstMother() ;
135                 if(fDebug > 0 && label < 8 ) printf("AliCaloPID::CheckOrigin: Mother is parton %d\n",iParent);
136                 
137                 //GrandParent
138                 TParticle * parent = new TParticle ;
139                 Int_t pPdg = -1;
140                 Int_t pStatus =-1;
141                 if(iParent > 0){
142                         parent = stack->Particle(iParent);
143                         pPdg = TMath::Abs(parent->GetPdgCode());
144                         pStatus = parent->GetStatusCode();  
145                 }
146                 else if(fDebug > 0 ) printf("AliCaloPID::CheckOrigin: Parent with label %d\n",iParent);
147                 
148                 //return tag
149                 if(mPdg == 22){
150                         if(mStatus == 1){
151                                 if(iParent < 8) {
152                                         if(pPdg == 22) return kMCPrompt;
153                                         else  return kMCFragmentation;
154                                 }
155                                 else if(pStatus == 11){
156                                         if(pPdg == 111) return kMCPi0Decay ;
157                                         else if (pPdg == 321)  return kMCEtaDecay ;
158                                         else  return kMCOtherDecay ;
159                                 }
160                         }//Status 1 : Pythia generated
161                         else if(mStatus == 0){
162                                 if(pPdg ==22 || pPdg ==11) return kMCConversion ;
163                                 if(pPdg == 111) return kMCPi0Decay ;
164                                 else if (pPdg == 221)  return kMCEtaDecay ;
165                                 else  return kMCOtherDecay ;
166                         }//status 0 : geant generated
167                 }//Mother Photon
168                 else if(mPdg == 111)  return kMCPi0 ;
169                 else if(mPdg == 221)  return kMCEta ;
170                 else if(mPdg ==11){
171                         if(mStatus == 0) return kMCConversion ;
172                         else return kMCElectron ;
173                 }
174                 else return kMCUnknown;
175         }//Good label value
176         else{
177                 if(label < 0 ) printf("AliCaloPID::CheckOrigin: *** bad label or no stack ***:  label %d \n", label);
178                 if(label >=  stack->GetNtrack()) printf("AliCaloPID::CheckOrigin: *** large label ***:  label %d, n tracks %d \n", label, stack->GetNtrack());
179                 return kMCUnknown;
180         }//Bad label
181         
182         return kMCUnknown;
183         
184 }
185
186 //_______________________________________________________________
187 void AliCaloPID::InitParameters()
188 {
189         //Initialize the parameters of the PID.
190         
191         fEMCALPhotonWeight   = 0.8 ;
192         fEMCALPi0Weight      = 0.5 ;
193         fEMCALElectronWeight = 0.8 ;
194         fEMCALChargeWeight   = 0.5 ;
195         fEMCALNeutralWeight  = 0.5 ;
196         
197         fPHOSPhotonWeight    = 0.75 ;
198         fPHOSPi0Weight       = 0.8 ;
199         fPHOSElectronWeight  = 0.5 ;
200         fPHOSChargeWeight    = 0.5 ;
201         fPHOSNeutralWeight   = 0.5 ;
202         
203         //Formula to set the PID weight threshold for photon or pi0
204         fPHOSWeightFormula = kTRUE;
205         fPHOSPhotonWeightFormula = 
206     new TFormula("photonWeight","0.98*(x<40)+ 0.68*(x>=100)+(x>=40 && x<100)*(0.98+x*(6e-3)-x*x*(2e-04)+x*x*x*(1.1e-06))");
207         fPHOSPi0WeightFormula = 
208     new TFormula("pi0Weight","0.98*(x<65)+ 0.915*(x>=100)+(x>=65 && x-x*(1.95e-3)-x*x*(4.31e-05)+x*x*x*(3.61e-07))");
209         
210     fDispCut  = 1.5;
211         fTOFCut   = 5.e-9;
212         fDebug = -1;
213 }
214
215 //_______________________________________________________________
216 Int_t AliCaloPID::GetPdg(const TString calo, const Double_t * pid, const Float_t energy) const {
217         //Return most probable identity of the particle.
218         
219         if(!pid) AliFatal("pid pointer not initialized!!!");
220         
221         Float_t wPh =  fPHOSPhotonWeight ;
222         Float_t wPi0 =  fPHOSPi0Weight ;
223         Float_t wE =  fPHOSElectronWeight ;
224         Float_t wCh =  fPHOSChargeWeight ;
225         Float_t wNe =  fPHOSNeutralWeight ;
226         
227         
228         if(calo == "PHOS" && fPHOSWeightFormula){
229                 wPh  = fPHOSPhotonWeightFormula->Eval(energy) ;
230                 wPi0 = fPHOSPi0WeightFormula->Eval(energy);
231         }
232         
233         if(calo == "EMCAL"){
234                 
235                 wPh  =  fEMCALPhotonWeight ;
236                 wPi0 =  fEMCALPi0Weight ;
237                 wE   =  fEMCALElectronWeight ;
238                 wCh  =  fEMCALChargeWeight ;
239                 wNe  =  fEMCALNeutralWeight ;
240                 
241         }
242         
243         if(fDebug > 0)  printf("AliCaloPID::GetPdg: calo %s, ph %0.2f, pi0 %0.2f, el %0.2f, conv el %0.2f, hadrons: pion %0.2f, kaon %0.2f, proton %0.2f , neutron %0.2f, kaon %0.2f \n",
244                                                    calo.Data(),pid[AliAODCluster::kPhoton], pid[AliAODCluster::kPi0],
245                                                    pid[AliAODCluster::kElectron], pid[AliAODCluster::kEleCon],
246                                                    pid[AliAODCluster::kPion], pid[AliAODCluster::kKaon], pid[AliAODCluster::kProton],
247                                                    pid[AliAODCluster::kNeutron], pid[AliAODCluster::kKaon0]);
248         
249         Int_t pdg = kNeutralUnknown ;
250         Float_t chargedHadronWeight = pid[AliAODCluster::kProton]+pid[AliAODCluster::kKaon]+
251     pid[AliAODCluster::kPion]+pid[AliAODCluster::kMuon];
252         Float_t neutralHadronWeight = pid[AliAODCluster::kNeutron]+pid[AliAODCluster::kKaon0];
253         Float_t allChargedWeight    = pid[AliAODCluster::kElectron]+pid[AliAODCluster::kEleCon]+ chargedHadronWeight;
254         Float_t allNeutralWeight    = pid[AliAODCluster::kPhoton]+pid[AliAODCluster::kPi0]+ neutralHadronWeight;
255         
256         //Select most probable ID
257         if(calo=="PHOS"){
258                 if(pid[AliAODCluster::kPhoton] > wPh) pdg = kPhoton ;
259                 else if(pid[AliAODCluster::kPi0] > wPi0) pdg = kPi0 ; 
260                 else if(pid[AliAODCluster::kElectron] > wE)  pdg = kElectron ;
261                 else if(pid[AliAODCluster::kEleCon] >  wE) pdg = kEleCon ;
262                 else if(chargedHadronWeight > wCh) pdg = kChargedHadron ;  
263                 else if(neutralHadronWeight > wNe) pdg = kNeutralHadron ; 
264                 else if(allChargedWeight >  allNeutralWeight)
265                         pdg = kChargedUnknown ; 
266                 else 
267                         pdg = kNeutralUnknown ;
268         }
269         else{//EMCAL
270                 //Temporal solution, electrons and photons not differenciated
271                 if(pid[AliAODCluster::kPhoton] + pid[AliAODCluster::kElectron]  > wPh) pdg = kPhoton ;
272                 else if(pid[AliAODCluster::kPi0] > wPi0) pdg = kPi0 ; 
273                 else if(chargedHadronWeight + neutralHadronWeight > wCh) pdg = kChargedHadron ;  
274                 else if(neutralHadronWeight + chargedHadronWeight > wNe) pdg = kNeutralHadron ; 
275                 else pdg =  kNeutralUnknown ;
276                 
277         }
278         
279         
280         if(fDebug > 0)printf("AliCaloPID::GetPdg:Final Pdg: %d \n", pdg);
281         
282         
283         
284         return pdg ;
285         
286 }
287
288 //_______________________________________________________________
289 Int_t AliCaloPID::GetPdg(const TString calo,const TLorentzVector mom, const AliAODCaloCluster * cluster) const {
290         //Recalculated PID with all parameters
291         if(fDebug > 0)printf("AliCaloPID::GetPdg: Calorimeter %s, E %3.2f, l0 %3.2f, l1 %3.2f, disp %3.2f, tof %1.11f, distCPV %3.2f, distToBC %1.1f, NMax %d\n",
292                                                  calo.Data(),mom.E(),cluster->GetM02(),cluster->GetM20(),cluster->GetDispersion(),cluster->GetTOF(), 
293                                                  cluster->GetEmcCpvDistance(), cluster->GetDistToBadChannel(),cluster->GetNExMax());
294         
295         if(calo == "EMCAL") {
296                 if(cluster->GetM02()< 0.25) return kPhoton ;
297                 else return  kNeutralHadron ; 
298         }
299         
300         //   if(calo == "PHOS") {
301         //    if(cluster->GetM02()< 0.25) return kPhoton ;
302         //    else return  kNeutralHadron ; 
303         //  }
304         
305         return  kNeutralHadron ; 
306         
307 }
308
309 //__________________________________________________
310 TString  AliCaloPID::GetPIDParametersList()  {
311         //Put data member values in string to keep in output container
312         
313         TString parList ; //this will be list of parameters used for this analysis.
314         char onePar[255] ;
315         sprintf(onePar,"--- AliCaloPID ---\n") ;
316         parList+=onePar ;       
317         sprintf(onePar,"fDispCut =%2.2f (Cut on dispersion, used in PID evaluation) \n",fDispCut) ;
318         parList+=onePar ;
319         sprintf(onePar,"fTOFCut  =%e (Cut on TOF, used in PID evaluation) \n",fTOFCut) ;
320         parList+=onePar ;
321         sprintf(onePar,"fEMCALPhotonWeight =%2.2f (EMCAL bayesian weight for photons)\n",fEMCALPhotonWeight) ;
322         parList+=onePar ;
323         sprintf(onePar,"fEMCALPi0Weight =%2.2f (EMCAL bayesian weight for pi0)\n",fEMCALPi0Weight) ;
324         parList+=onePar ;
325         sprintf(onePar,"fEMCALElectronWeight =%2.2f(EMCAL bayesian weight for electrons)\n",fEMCALElectronWeight) ;
326         parList+=onePar ;
327         sprintf(onePar,"fEMCALChargeWeight =%2.2f (EMCAL bayesian weight for charged hadrons)\n",fEMCALChargeWeight) ;
328         parList+=onePar ;
329         sprintf(onePar,"fEMCALNeutralWeight =%2.2f (EMCAL bayesian weight for neutral hadrons)\n",fEMCALNeutralWeight) ;
330         parList+=onePar ;
331         sprintf(onePar,"fPHOSPhotonWeight =%2.2f (PHOS bayesian weight for photons)\n",fPHOSPhotonWeight) ;
332         parList+=onePar ;
333         sprintf(onePar,"fPHOSPi0Weight =%2.2f (PHOS bayesian weight for pi0)\n",fPHOSPi0Weight) ;
334         parList+=onePar ;
335         sprintf(onePar,"fPHOSElectronWeight =%2.2f(PHOS bayesian weight for electrons)\n",fPHOSElectronWeight) ;
336         parList+=onePar ;
337         sprintf(onePar,"fPHOSChargeWeight =%2.2f (PHOS bayesian weight for charged hadrons)\n",fPHOSChargeWeight) ;
338         parList+=onePar ;
339         sprintf(onePar,"fPHOSNeutralWeight =%2.2f (PHOS bayesian weight for neutral hadrons)\n",fPHOSNeutralWeight) ;
340         parList+=onePar ;
341         
342         if(fPHOSWeightFormula){
343                 parList+="PHOS Photon Weight Formula: "+(fPHOSPhotonWeightFormula->GetExpFormula("p"));
344                 parList+="PHOS Pi0    Weight Formula: "+(fPHOSPi0WeightFormula->GetExpFormula("p"));
345         }
346         
347         return parList; 
348         
349 }
350
351 //________________________________________________________________
352 void AliCaloPID::Print(const Option_t * opt) const
353 {
354         
355         //Print some relevant parameters set for the analysis
356         if(! opt)
357                 return;
358         
359         printf("***** Print: %s %s ******\n", GetName(), GetTitle() ) ;
360         
361         printf("PHOS PID weight , photon %0.2f, pi0 %0.2f, e %0.2f, charge %0.2f, neutral %0.2f \n",  
362                    fPHOSPhotonWeight,  fPHOSPi0Weight, 
363                    fPHOSElectronWeight,  fPHOSChargeWeight,   fPHOSNeutralWeight) ; 
364         printf("EMCAL PID weight, photon %0.2f, pi0 %0.2f, e %0.2f, charge %0.2f, neutral %0.2f\n",   
365                    fEMCALPhotonWeight,  fEMCALPi0Weight, 
366                    fEMCALElectronWeight,  fEMCALChargeWeight,  fEMCALNeutralWeight) ; 
367         
368         printf("PHOS Parametrized weight on?  =     %d\n",  fPHOSWeightFormula) ; 
369         if(fPHOSWeightFormula){
370                 printf("Photon weight formula = %s\n", (fPHOSPhotonWeightFormula->GetExpFormula("p")).Data());
371                 printf("Pi0    weight formula = %s\n", (fPHOSPhotonWeightFormula->GetExpFormula("p")).Data());
372     }
373         
374         printf("TOF cut        = %e\n",fTOFCut);
375         printf("Dispersion cut = %2.2f\n",fDispCut);
376         printf("Debug level    = %d\n",fDebug);
377         
378         printf(" \n");
379         
380
381
382 //_______________________________________________________________
383 void AliCaloPID::SetPIDBits(const TString calo, const AliAODCaloCluster * cluster, AliAODPWG4Particle * ph) {
384         //Set Bits for PID selection
385         
386         //Dispersion/lambdas
387     Double_t disp=cluster->GetDispersion()  ;
388         //    Double_t m20=calo->GetM20() ;
389         //    Double_t m02=calo->GetM02() ; 
390     ph->SetDispBit(disp<fDispCut) ;  
391         
392     //TOF
393     Double_t tof=cluster->GetTOF()  ;
394     ph->SetTOFBit(TMath::Abs(tof)<fTOFCut) ; 
395         
396     //Charged veto
397         //    Double_t cpvR=calo->GetEmcCpvDistance() ; 
398     Int_t ntr=cluster->GetNTracksMatched();  //number of track matched
399     ph->SetChargedBit(ntr>0) ;  //Temporary cut, should we evaluate distance?
400         
401     //Set PID pdg
402         ph->SetPdg(GetPdg(calo,cluster->PID(),ph->E()));
403         
404         if(fDebug > 0){ 
405                 printf("AliCaloPID::SetPIDBits: TOF %e, Dispersion %2.2f, NTracks %d\n",tof , disp, ntr);       
406                 printf("AliCaloPID::SetPIDBits: pdg %d, bits: TOF %d, Dispersion %d, Charge %d\n",
407                            ph->GetPdg(), ph->GetTOFBit() , ph->GetDispBit() , ph->GetChargedBit()); 
408         }
409 }
410
411