- Reshuffling of the particle codes in AliPID. Now the light nuclei are between the
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliPIDCombined.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
17 //-----------------------------------------------------------------
18 //        Base class for combining PID of different detectors    //
19 //        (user selected) and compute Bayesian probabilities     //
20 //                                                               //
21 //                                                               //
22 //   Origin: Pietro Antonioli, INFN-BO Pietro.Antonioli@cern.ch  //
23 //                                                               //
24 //-----------------------------------------------------------------
25
26 #include <TH1.h>
27
28 #include <AliVTrack.h>
29 #include <AliLog.h>
30 #include <AliPID.h>
31 #include <AliPIDResponse.h>
32
33 #include "AliPIDCombined.h"
34
35 #include "TMath.h"
36 #include "TFile.h"
37
38 #include "AliOADBContainer.h"
39
40 // initialize static members
41 TH2F* AliPIDCombined::fDefaultPriorsTPC[]={0x0};
42
43 ClassImp(AliPIDCombined);
44
45 AliPIDCombined::AliPIDCombined():
46         TNamed("CombinedPID","CombinedPID"),
47         fDetectorMask(0),
48         fRejectMismatchMask(0x7F),
49         fEnablePriors(kTRUE),
50         fSelectedSpecies(AliPID::kSPECIES),
51         fUseDefaultTPCPriors(kFALSE)
52 {       
53   //
54   // default constructor
55   //    
56   for (Int_t i=0;i<AliPID::kSPECIESC;i++) fPriorsDistributions[i]=NULL;
57   AliLog::SetClassDebugLevel("AliPIDCombined",10);
58 }
59
60 //-------------------------------------------------------------------------------------------------     
61 AliPIDCombined::AliPIDCombined(const TString& name,const TString& title):
62         TNamed(name,title),
63         fDetectorMask(0),
64         fRejectMismatchMask(0x7F),
65         fEnablePriors(kTRUE),
66         fSelectedSpecies(AliPID::kSPECIES),
67         fUseDefaultTPCPriors(kFALSE)
68 {
69   //
70   // default constructor with name and title
71   //
72   for (Int_t i=0;i<AliPID::kSPECIESC;i++) fPriorsDistributions[i]=NULL;
73   AliLog::SetClassDebugLevel("AliPIDCombined",10);
74
75 }
76
77 //-------------------------------------------------------------------------------------------------     
78 AliPIDCombined::~AliPIDCombined() {
79
80   for(Int_t i=0;i < AliPID::kSPECIESC;i++){
81     if(fPriorsDistributions[i])
82       delete fPriorsDistributions[i];
83   }
84 }
85
86 //-------------------------------------------------------------------------------------------------     
87 void AliPIDCombined::SetPriorDistribution(AliPID::EParticleType type,TH1F *prior) {
88   if ( (type < 0) || ( type >= ((AliPID::EParticleType)AliPID::kSPECIESC) ) ){
89     AliError(Form("Invalid EParticleType setting prior (offending type: %d)",type));
90     return;
91   }
92   if(prior) {
93     Int_t i = (Int_t)type;
94     if (fPriorsDistributions[i]) {
95       delete fPriorsDistributions[i]; 
96     }
97     fPriorsDistributions[i]=new TH1F(*prior);
98   }
99 }
100
101 //-------------------------------------------------------------------------------------------------     
102 UInt_t AliPIDCombined::ComputeProbabilities(const AliVTrack *track, const AliPIDResponse *response, Double_t* bayesProbabilities) const {
103         Double_t pITS[fSelectedSpecies];
104         Double_t pTPC[fSelectedSpecies];
105         Double_t pTRD[fSelectedSpecies];
106         Double_t pTOF[fSelectedSpecies];
107         Double_t pHMPID[fSelectedSpecies];
108         Double_t pEMCAL[fSelectedSpecies];
109         Double_t pPHOS[fSelectedSpecies];
110         for (Int_t i=0;i<fSelectedSpecies;i++) {
111          pITS[i]=1.;
112          pTPC[i]=1.;
113          pTRD[i]=1.;
114          pTOF[i]=1.;
115          pHMPID[i]=1.;
116          pEMCAL[i]=1.;
117          pPHOS[i]=1.;
118         }
119         UInt_t usedMask=0;
120         AliPIDResponse::EDetPidStatus status=AliPIDResponse::kDetNoSignal;
121         Double_t p[fSelectedSpecies];
122         memset(p,0,sizeof(Double_t)*fSelectedSpecies);
123
124         // getting probability distributions for selected detectors only
125         if (fDetectorMask & AliPIDResponse::kDetITS) {
126           status = response->ComputeITSProbability(track, fSelectedSpecies, pITS);
127           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetITS,pITS);
128         }
129
130         if (fDetectorMask & AliPIDResponse::kDetTPC) { 
131           status = response->ComputeTPCProbability(track, fSelectedSpecies, pTPC);
132           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetTPC,pTPC);
133         }
134
135
136         if (fDetectorMask & AliPIDResponse::kDetTRD) { 
137           status = response->ComputeTRDProbability(track, fSelectedSpecies, pTRD);
138           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetTRD,pTRD);
139         }
140
141         if (fDetectorMask &  AliPIDResponse::kDetTOF) { 
142           status = response->ComputeTOFProbability(track, fSelectedSpecies, pTOF);
143           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetTOF,pTOF);
144         }
145
146         if (fDetectorMask & AliPIDResponse::kDetHMPID) { 
147           status = response->ComputeHMPIDProbability(track, fSelectedSpecies, pHMPID);
148           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetHMPID,pHMPID);
149         }
150
151
152         if (fDetectorMask & AliPIDResponse::kDetEMCAL) { 
153           status = response->ComputeEMCALProbability(track, fSelectedSpecies, pEMCAL);
154           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetEMCAL,pEMCAL);
155         }
156
157
158         if (fDetectorMask & AliPIDResponse::kDetPHOS) { 
159           status = response->ComputePHOSProbability(track, fSelectedSpecies, pPHOS);
160           SetCombinedStatus(status,&usedMask,AliPIDResponse::kDetPHOS,pPHOS);
161         }
162
163
164         for (Int_t i =0; i<fSelectedSpecies; i++){
165           p[i] = pITS[i]*pTPC[i]*pTRD[i]*pTOF[i]*pHMPID[i]*pEMCAL[i]*pPHOS[i];
166         }
167         Double_t priors[fSelectedSpecies];
168         memset(priors,0,fSelectedSpecies*sizeof(Double_t));
169         if (fEnablePriors){
170           GetPriors(track,priors,response->GetCurrentCentrality());
171           
172           // for the moment we have three cases
173           // TPC+TRD      --> apply TRD propagation factors
174           // TPC+TOF      --> apply TOF propagation factors
175           // TPC+TRD+TOF  --> apply TOF propagation factors
176           // apply tof matching efficiency to priors if TOF joined PID for this track
177           if(fUseDefaultTPCPriors) {
178              Double_t pt=TMath::Abs(track->Pt());
179              if ( (usedMask & AliPIDResponse::kDetTOF) == AliPIDResponse::kDetTOF ){ // TOF is the outer having prop. factors for the moment
180                Float_t kaonTOFfactor = 0.1;
181                if(pt > 0.35) kaonTOFfactor = 1 - TMath::Exp(-TMath::Power(pt,4.19618E-07)/5.68017E-01)*TMath::Power(pt,-1.50705);
182                Float_t protonTOFfactor = 0.1;
183                if(pt > 0.4) protonTOFfactor = 1 - TMath::Exp(-TMath::Power(pt,3.30978)/8.57396E-02)*TMath::Power(pt,-4.42661E-01);
184                
185                if(track->Charge() < 0){ // for negative tracks
186                  kaonTOFfactor *= 1 - TMath::Exp(-TMath::Power(pt,4.87912E-07)/3.26431E-01)*TMath::Power(pt,-1.22893);
187                  protonTOFfactor *= 1 - TMath::Exp(-TMath::Power(pt,2.00575E-07)/4.95605E-01)*TMath::Power(pt,-6.71305E-01);
188                }
189                p[3] *= kaonTOFfactor;
190                p[4] *= protonTOFfactor;
191              }
192              else {
193                if ( (usedMask & AliPIDResponse::kDetTRD)==AliPIDResponse::kDetTRD ) {
194                  Float_t electronTRDfactor=0.1;
195                  Float_t kaonTRDfactor = 0.1;
196                  Float_t protonTRDfactor = 0.1;
197                  
198                  if(track->Charge() > 0){
199                    // positiv charge
200                    if(pt > 0.25) electronTRDfactor =  1 - TMath::Exp(-TMath::Power(pt,5.13315e-03)/2.11145e-01)*TMath::Power(pt,-2.97659e+00);
201                    if(pt > 0.35) kaonTRDfactor = 1 - TMath::Exp(-TMath::Power(pt,-4.29549e-02)/4.87989e-01)*TMath::Power(pt,-1.54270e+00);
202                    if(pt > 0.35) protonTRDfactor = 1 - TMath::Exp(-TMath::Power(pt,2.81238e+00)/7.57082e-02)*TMath::Power(pt,-8.12595e-01);
203                  }
204                  
205                  if(track->Charge() < 0){
206                    // negative charge
207                    if(pt > 0.25) electronTRDfactor =  1 - TMath::Exp(-TMath::Power(pt,2.45537e-02)/1.90397e-01)*TMath::Power(pt,-3.33121e+00);
208                    if(pt > 0.35) kaonTRDfactor = 1 - TMath::Exp(-TMath::Power(pt, -3.42831e-03)/5.57013e-01)*TMath::Power(pt,-1.39202e+00);
209                    if(pt > 0.35) protonTRDfactor = 1 - TMath::Exp(-TMath::Power(pt,3.36631e+00)/7.18819e-02)*TMath::Power(pt,-2.00577e-01);
210                  }
211                  // what about electrons
212                  p[0] *= electronTRDfactor;
213                  p[3] *= kaonTRDfactor;
214                  p[4] *= protonTRDfactor;             
215                } // end of TRD case
216              } // end of detectors inner than TOF
217           } // end of fUseDefaultTPCPriors
218         }   // end of use priors
219         else { for (Int_t i=0;i<fSelectedSpecies;i++) priors[i]=1.;}
220         ComputeBayesProbabilities(bayesProbabilities,p,priors);
221         return usedMask;
222 }
223
224
225 //-------------------------------------------------------------------------------------------------
226 void AliPIDCombined::GetPriors(const AliVTrack *track, Double_t* p,Float_t centrality) const {
227         
228         //
229         // get priors from distributions
230         //
231         
232         Double_t pt=TMath::Abs(track->Pt());
233
234         if(fUseDefaultTPCPriors){ // use default priors if requested
235           Float_t usedCentr = centrality+5*(centrality>0);
236           if(usedCentr < -0.99) usedCentr = -0.99;
237           else if(usedCentr > 99) usedCentr = 99;
238           if(pt > 9.99) pt = 9.99;
239           else if(pt < 0.01)  pt = 0.01;
240           
241           for(Int_t i=0;i<5;i++) p[i] = fDefaultPriorsTPC[i]->Interpolate(usedCentr,pt);
242
243           return;
244         }
245         
246         Double_t sumPriors = 0;
247         for (Int_t i=0;i<fSelectedSpecies;++i){
248                 if (fPriorsDistributions[i]){
249                         p[i]=fPriorsDistributions[i]->Interpolate(pt);
250                 }
251                 else {
252                         p[i]=0.;
253                 }
254                 sumPriors+=p[i];                
255         }
256
257         // normalizing priors
258         if (sumPriors == 0) return;
259         for (Int_t i=0;i<fSelectedSpecies;++i){
260            p[i]=p[i]/sumPriors;
261         }
262         return;
263 }
264 //-------------------------------------------------------------------------------------------------
265 void AliPIDCombined::GetPriors(const AliVTrack *track,Double_t* p,const AliPIDResponse *response,UInt_t detUsed) const{
266         
267         //
268         // get priors from distributions
269         //
270         Double_t pt=TMath::Abs(track->Pt());
271         
272         if(fUseDefaultTPCPriors){ // use default priors if requested
273           Float_t usedCentr = response->GetCurrentCentrality()+5*(response->GetCurrentCentrality()>0);
274           if(usedCentr < -0.99) usedCentr = -0.99;
275           else if(usedCentr > 99) usedCentr = 99;
276           if(pt > 9.99) pt = 9.99;
277           else if(pt < 0.01)  pt = 0.01;
278           
279           for(Int_t i=0;i<5;i++) p[i] = fDefaultPriorsTPC[i]->Interpolate(usedCentr,pt);
280
281           // Extra factor if TOF matching was required
282           if(detUsed & AliPIDResponse::kDetTOF){
283             Float_t kaonTOFfactor = 0.1;
284             if(pt > 0.35) kaonTOFfactor = 1 - TMath::Exp(-TMath::Power(pt,4.19618E-07)/5.68017E-01)*TMath::Power(pt,-1.50705);
285             Float_t protonTOFfactor = 0.1;
286             if(pt > 0.4) protonTOFfactor = 1 - TMath::Exp(-TMath::Power(pt,3.30978)/8.57396E-02)*TMath::Power(pt,-4.42661E-01);
287             
288             if(track->Charge() < 0){ // for negative tracks
289               kaonTOFfactor *= 1 - TMath::Exp(-TMath::Power(pt,4.87912E-07)/3.26431E-01)*TMath::Power(pt,-1.22893);
290               protonTOFfactor *= 1 - TMath::Exp(-TMath::Power(pt,2.00575E-07)/4.95605E-01)*TMath::Power(pt,-6.71305E-01);
291             }
292             
293             p[3] *= kaonTOFfactor;
294             p[4] *= protonTOFfactor;
295           }
296
297           return;
298         }
299
300
301         Double_t sumPriors = 0;
302         for (Int_t i=0;i<fSelectedSpecies;++i){
303                 if (fPriorsDistributions[i]){
304                         p[i]=fPriorsDistributions[i]->Interpolate(pt);
305                 }
306                 else {
307                         p[i]=0.;
308                 }
309                 sumPriors+=p[i];                
310         }
311
312         // normalizing priors
313         if (sumPriors == 0) return;
314         for (Int_t i=0;i<fSelectedSpecies;++i){
315            p[i]=p[i]/sumPriors;
316         }
317         return;
318 }
319 //-------------------------------------------------------------------------------------------------     
320 void AliPIDCombined::ComputeBayesProbabilities(Double_t* probabilities, const Double_t* probDensity, const Double_t* prior) const {
321
322
323   //
324   // calculate Bayesian probabilities
325   //
326   Double_t sum = 0.;
327   for (Int_t i = 0; i < fSelectedSpecies; i++) {
328     sum += probDensity[i] * prior[i];
329   }
330   if (sum <= 0) {
331
332     AliError("Invalid probability densities or priors");
333     for (Int_t i = 0; i < fSelectedSpecies; i++) probabilities[i] = -1;
334     return;
335   }
336   for (Int_t i = 0; i < fSelectedSpecies; i++) {
337     probabilities[i] = probDensity[i] * prior[i] / sum;
338   }
339
340
341 }
342
343 //----------------------------------------------------------------------------------------
344 void AliPIDCombined::SetCombinedStatus(AliPIDResponse::EDetPidStatus status, UInt_t *maskDetIn, AliPIDResponse::EDetCode bit, Double_t* p) const {
345   switch (status) {
346   case AliPIDResponse::kDetNoSignal:
347     break;
348   case AliPIDResponse::kDetPidOk:
349     *maskDetIn = *maskDetIn | bit;
350     break;
351   case AliPIDResponse::kDetMismatch:
352     if ( fRejectMismatchMask & bit) for (Int_t j=0;j<fSelectedSpecies;j++) p[j]=1./fSelectedSpecies;
353     else *maskDetIn = *maskDetIn | bit;
354     break;
355   }
356
357 }
358
359 //----------------------------------------------------------------------------------------
360 void AliPIDCombined::SetDefaultTPCPriors(){
361   fEnablePriors=kTRUE;
362   fUseDefaultTPCPriors = kTRUE;
363
364   //check if priors are already initialized
365   if (fDefaultPriorsTPC[0]) return;
366   
367   TString oadbfilename("$ALICE_ROOT/OADB/COMMON/PID/data/");
368   oadbfilename += "/PIDdefaultPriors.root";
369   TFile * foadb = TFile::Open(oadbfilename.Data());
370   if(!foadb || !foadb->IsOpen()) {
371     delete foadb;
372     AliFatal(Form("Cannot open OADB file %s", oadbfilename.Data()));
373     return;
374   }
375   
376   AliOADBContainer * priorsContainer = (AliOADBContainer*) foadb->Get("priorsTPC");
377   if (!priorsContainer) AliFatal("Cannot fetch OADB container for Priors");
378   
379   const char *namespecies[AliPID::kSPECIES] = {"El","Mu","Pi","Ka","Pr"};
380   char name[100];
381
382   for(Int_t i=0;i < AliPID::kSPECIES;i++){
383     snprintf(name,99,"hDefault%sPriors",namespecies[i]);
384     fDefaultPriorsTPC[i] = (TH2F*) priorsContainer->GetDefaultObject(name);
385     if (!fDefaultPriorsTPC[i]) AliFatal(Form("Cannot find priors for %s", namespecies[i]));
386   }
387
388   delete foadb;
389 }