]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/ESD/AliESDpid.cxx
Merge branch 'master', remote branch 'origin' into TPCdev
[u/mrichter/AliRoot.git] / STEER / ESD / AliESDpid.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 /* $Id: AliESDpid.cxx 64123 2013-09-05 15:09:53Z morsch $ */
17
18 //-----------------------------------------------------------------
19 //           Implementation of the combined PID class
20 //           For the Event Summary Data Class
21 //           produced by the reconstruction process
22 //           and containing information on the particle identification
23 //      Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
24 //-----------------------------------------------------------------
25
26 #include "TArrayI.h"
27 #include "TArrayF.h"
28
29 #include "TRandom.h"
30 #include "AliLog.h"
31 #include "AliPID.h"
32 #include "AliTOFHeader.h"
33 #include "AliESDpid.h"
34 #include "AliESDEvent.h"
35 #include "AliESDtrack.h"
36 #include "AliMCEvent.h"
37 #include "AliTOFPIDParams.h"
38
39 #include <AliDetectorPID.h>
40
41 ClassImp(AliESDpid)
42
43 Int_t AliESDpid::MakePID(AliESDEvent *event, Bool_t TPConly, Float_t /*timeZeroTOF*/) const {
44   //
45   //  Calculate probabilities for all detectors, except if TPConly==kTRUE
46   //  and combine PID
47   //  
48   //   Option TPConly==kTRUE is used during reconstruction, 
49   //  because ITS tracking uses TPC pid
50   //  HMPID and TRD pid are done in detector reconstructors
51   //
52
53   /*
54   Float_t timeZeroTOF = 0;
55   if (subtractT0) 
56     timeZeroTOF = event->GetT0();
57   */
58   Int_t nTrk=event->GetNumberOfTracks();
59   for (Int_t iTrk=0; iTrk<nTrk; iTrk++) {  
60     AliESDtrack *track=event->GetTrack(iTrk);
61     MakeTPCPID(track);
62     if (!TPConly) {
63       MakeITSPID(track);
64       //MakeTOFPID(track, timeZeroTOF);
65       //MakeHMPIDPID(track);
66       //MakeTRDPID(track);
67     }
68     CombinePID(track);
69   }
70   return 0;
71 }
72 //_________________________________________________________________________
73 Float_t AliESDpid::GetTPCsignalTunedOnData(const AliVTrack *t) const {
74     AliESDtrack *track = (AliESDtrack *) t;
75     Float_t dedx = track->GetTPCsignalTunedOnData();
76
77     if(dedx > 0) return dedx;
78
79     dedx = t->GetTPCsignal();
80     track->SetTPCsignalTunedOnData(dedx);
81     if(dedx < 20) return dedx;
82
83     AliPID::EParticleType type = AliPID::kPion;
84
85     AliMCEventHandler* eventHandler=dynamic_cast<AliMCEventHandler*>(fEventHandler);
86     if (eventHandler) {
87         AliMCEvent* mcEvent = eventHandler->MCEvent();
88         if(mcEvent){
89             Bool_t kGood = kTRUE;
90             AliMCParticle *MCpart = (AliMCParticle *) mcEvent->GetTrack(TMath::Abs(t->GetLabel()));
91             TParticle *part = MCpart->Particle();
92             
93             Int_t iS = TMath::Abs(part->GetPdgCode());
94
95             if(iS==AliPID::ParticleCode(AliPID::kElectron)){
96                 type = AliPID::kElectron;
97             }
98             else if(iS==AliPID::ParticleCode(AliPID::kMuon)){
99                 type = AliPID::kMuon;
100             }
101             else if(iS==AliPID::ParticleCode(AliPID::kPion)){
102                 type = AliPID::kPion;
103             }
104             else if(iS==AliPID::ParticleCode(AliPID::kKaon)){
105                 type = AliPID::kKaon;
106             }
107             else if(iS==AliPID::ParticleCode(AliPID::kProton)){
108                 type = AliPID::kProton;
109             }
110             else if(iS==AliPID::ParticleCode(AliPID::kDeuteron)){ // d
111                 type = AliPID::kDeuteron;
112             }
113             else if(iS==AliPID::ParticleCode(AliPID::kTriton)){ // t
114                 type = AliPID::kTriton;
115             }
116             else if(iS==AliPID::ParticleCode(AliPID::kHe3)){ // 3He
117                 type = AliPID::kHe3;
118             }
119             else if(iS==AliPID::ParticleCode(AliPID::kAlpha)){ // 4He
120                 type = AliPID::kAlpha;
121             }
122             else
123                 kGood = kFALSE;
124
125             if(kGood){
126         //TODO maybe introduce different dEdxSources?
127         Double_t bethe = fTPCResponse.GetExpectedSignal(track, type, AliTPCPIDResponse::kdEdxDefault, this->UseTPCEtaCorrection(),
128                                                         this->UseTPCMultiplicityCorrection());
129         Double_t sigma = fTPCResponse.GetExpectedSigma(track, type, AliTPCPIDResponse::kdEdxDefault, this->UseTPCEtaCorrection(),
130                                                        this->UseTPCMultiplicityCorrection());
131                 dedx = gRandom->Gaus(bethe,sigma);
132 //              if(iS == AliPID::ParticleCode(AliPID::kHe3) || iS == AliPID::ParticleCode(AliPID::kAlpha)) dedx *= 5;
133             }
134         }
135     }
136
137     track->SetTPCsignalTunedOnData(dedx);
138     return dedx;
139 }
140 //_________________________________________________________________________
141 Float_t AliESDpid::GetTOFsignalTunedOnData(const AliVTrack *t) const {
142     AliESDtrack *track = (AliESDtrack *) t;
143     Double_t tofSignal = track->GetTOFsignalTunedOnData();
144
145     if(tofSignal <  99999) return (Float_t)tofSignal; // it has been already set
146     // read additional mismatch fraction
147     Float_t addmism = GetTOFPIDParams()->GetTOFadditionalMismForMC();
148     if(addmism > 1.){
149       Float_t centr = GetCurrentCentrality();
150       if(centr > 50) addmism *= 0.1667;
151       else if(centr > 20) addmism *= 0.33;
152     }
153
154     tofSignal = t->GetTOFsignal() + fTOFResponse.GetTailRandomValue(t->Pt(),t->Eta(),t->GetTOFsignal(),addmism);
155     track->SetTOFsignalTunedOnData(tofSignal);
156     return (Float_t)tofSignal;
157 }
158 //_________________________________________________________________________
159 void AliESDpid::MakeTPCPID(AliESDtrack *track) const
160 {
161   //
162   //  TPC pid using bethe-bloch and gaussian response
163   //
164   if ((track->GetStatus()&AliESDtrack::kTPCin )==0)
165     if ((track->GetStatus()&AliESDtrack::kTPCout)==0) return;
166
167     Double_t mom = track->GetP();
168     const AliExternalTrackParam *in=track->GetInnerParam();
169     if (in) mom = in->GetP();
170
171     Double_t p[AliPID::kSPECIES];
172     Double_t dedx=track->GetTPCsignal(); 
173     Bool_t mismatch=kTRUE, heavy=kTRUE;
174
175     for (Int_t j=0; j<AliPID::kSPECIES; j++) {
176       AliPID::EParticleType type=AliPID::EParticleType(j);
177       Double_t bethe=fTPCResponse.GetExpectedSignal(mom,type); 
178       Double_t sigma=fTPCResponse.GetExpectedSigma(mom,track->GetTPCsignalN(),type);
179       if (TMath::Abs(dedx-bethe) > fRange*sigma) {
180         p[j]=TMath::Exp(-0.5*fRange*fRange)/sigma;
181       } else {
182         p[j]=TMath::Exp(-0.5*(dedx-bethe)*(dedx-bethe)/(sigma*sigma))/sigma;
183         mismatch=kFALSE;
184       }
185
186       // Check for particles heavier than (AliPID::kSPECIES - 1)
187       if (dedx < (bethe + fRange*sigma)) heavy=kFALSE;
188
189     }
190
191     if (mismatch)
192        for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
193
194     track->SetTPCpid(p);
195
196     if (heavy) track->ResetStatus(AliESDtrack::kTPCpid);
197
198 }
199 //_________________________________________________________________________
200 void AliESDpid::MakeITSPID(AliESDtrack *track) const
201 {
202   //
203   // ITS PID
204   // Two options, depending on fITSPIDmethod:
205   //  1) Truncated mean method
206   //  2) Likelihood, using charges measured in all 4 layers and 
207   //     Landau+gaus response functions
208   //
209
210   if ((track->GetStatus()&AliESDtrack::kITSin)==0 &&
211       (track->GetStatus()&AliESDtrack::kITSout)==0) return;
212
213   Double_t mom=track->GetP();  
214   if (fITSPIDmethod == kITSTruncMean) {
215     Double_t dedx=track->GetITSsignal();
216     Bool_t isSA=kTRUE;
217     Double_t momITS=mom;
218     ULong_t trStatus=track->GetStatus();
219     if(trStatus&AliESDtrack::kTPCin) isSA=kFALSE;
220     UChar_t clumap=track->GetITSClusterMap();
221     Int_t nPointsForPid=0;
222     for(Int_t i=2; i<6; i++){
223       if(clumap&(1<<i)) ++nPointsForPid;
224     }
225
226     if(nPointsForPid<3) { // track not to be used for combined PID purposes
227       track->ResetStatus(AliESDtrack::kITSpid);
228       return;
229     }
230
231     Double_t p[AliPID::kSPECIES];
232
233     Bool_t mismatch=kTRUE, heavy=kTRUE;
234     for (Int_t j=0; j<AliPID::kSPECIES; j++) {
235       Double_t mass=AliPID::ParticleMass(j);//GeV/c^2
236       Double_t bethe=fITSResponse.Bethe(momITS,mass);
237       Double_t sigma=fITSResponse.GetResolution(bethe,nPointsForPid,isSA);
238       if (TMath::Abs(dedx-bethe) > fRange*sigma) {
239         p[j]=TMath::Exp(-0.5*fRange*fRange)/sigma;
240       } else {
241         p[j]=TMath::Exp(-0.5*(dedx-bethe)*(dedx-bethe)/(sigma*sigma))/sigma;
242         mismatch=kFALSE;
243       }
244
245       // Check for particles heavier than (AliPID::kSPECIES - 1)
246       if (dedx < (bethe + fRange*sigma)) heavy=kFALSE;
247
248     }
249
250     if (mismatch)
251        for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
252
253     track->SetITSpid(p);
254
255     if (heavy) track->ResetStatus(AliESDtrack::kITSpid);
256   }
257   else {  // Likelihood method
258     Double_t condprobfun[AliPID::kSPECIES];
259     Double_t qclu[4];
260     track->GetITSdEdxSamples(qclu);
261     fITSResponse.GetITSProbabilities(mom,qclu,condprobfun);
262     track->SetITSpid(condprobfun);
263   }
264
265 }
266 //_________________________________________________________________________
267 void AliESDpid::MakeTOFPID(AliESDtrack *track, Float_t /*timeZeroTOF*/) const
268 {
269   //
270   //   TOF PID using gaussian response
271   //
272
273   if ((track->GetStatus()&AliESDtrack::kTOFout)==0) return;
274   if ((track->GetStatus()&AliESDtrack::kTIME)==0) return;
275   if ((track->GetStatus()&AliESDtrack::kITSin)==0) return;
276
277   Int_t ibin = fTOFResponse.GetMomBin(track->GetP());
278   Float_t timezero = fTOFResponse.GetT0bin(ibin);
279
280   Double_t time[AliPID::kSPECIESC];
281   track->GetIntegratedTimes(time);
282
283   Double_t sigma[AliPID::kSPECIES];
284   for (Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++) {
285     sigma[iPart] = fTOFResponse.GetExpectedSigma(track->GetP(),time[iPart],AliPID::ParticleMass(iPart));
286   }
287
288   AliDebugGeneral("AliESDpid::MakeTOFPID",2,
289            Form("Expected TOF signals [ps]: %f %f %f %f %f",
290                   time[AliPID::kElectron],
291                   time[AliPID::kMuon],
292                   time[AliPID::kPion],
293                   time[AliPID::kKaon],
294                   time[AliPID::kProton]));
295
296   AliDebugGeneral("AliESDpid::MakeTOFPID",2,
297            Form("Expected TOF std deviations [ps]: %f %f %f %f %f",
298                   sigma[AliPID::kElectron],
299                   sigma[AliPID::kMuon],
300                   sigma[AliPID::kPion],
301                   sigma[AliPID::kKaon],
302                   sigma[AliPID::kProton]
303                   ));
304
305   Double_t tof = track->GetTOFsignal() - timezero;
306
307   Double_t p[AliPID::kSPECIES];
308 //   Bool_t mismatch = kTRUE;
309   Bool_t heavy = kTRUE;
310   for (Int_t j=0; j<AliPID::kSPECIES; j++) {
311     Double_t sig = sigma[j];
312     if (TMath::Abs(tof-time[j]) > (fRange+2)*sig) {
313         p[j] = TMath::Exp(-0.5*(fRange+2)*(fRange+2))/sig;
314     } else
315       p[j] = TMath::Exp(-0.5*(tof-time[j])*(tof-time[j])/(sig*sig))/sig;
316
317     // Check the mismatching
318     
319 //     Double_t mass = AliPID::ParticleMass(j);
320 //     Double_t pm = fTOFResponse.GetMismatchProbability(track->GetP(),mass);
321 //     if (p[j]>pm) mismatch = kFALSE;
322
323     // Check for particles heavier than (AliPID::kSPECIES - 1)
324     if (tof < (time[j] + fRange*sig)) heavy=kFALSE;
325
326   }
327
328   /*
329     if (mismatch)
330     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
331   */
332
333   track->SetTOFpid(p);
334
335   if (heavy) track->ResetStatus(AliESDtrack::kTOFpid);
336   
337   // kTOFmismatch flas is not set because deprecated from 18/02/2013
338   //  if (!CheckTOFMatching(track)) track->SetStatus(AliESDtrack::kTOFmismatch);
339   //  else track->ResetStatus(AliESDtrack::kTOFmismatch);
340 }
341 //_________________________________________________________________________
342 void AliESDpid::MakeTRDPID(AliESDtrack *track) const
343 {
344   //
345   // Method to recalculate the TRD PID probabilities
346   //
347   Double_t prob[AliPID::kSPECIES];
348   GetComputeTRDProbability(track, AliPID::kSPECIES, prob);
349   track->SetTRDpid(prob);
350 }
351 //_________________________________________________________________________
352 void AliESDpid::CombinePID(AliESDtrack *track) const
353 {
354   //
355   // Combine the information of various detectors
356   // to determine the Particle Identification
357   //
358   Double_t p[AliPID::kSPECIES]={1.};
359
360   if (track->IsOn(AliESDtrack::kITSpid)) {
361     Double_t d[AliPID::kSPECIES];
362     track->GetITSpid(d);
363     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]*=d[j];
364   }
365
366   if (track->IsOn(AliESDtrack::kTPCpid)) {
367     Double_t d[AliPID::kSPECIES];
368     track->GetTPCpid(d);
369     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]*=d[j];
370   }
371
372   if (track->IsOn(AliESDtrack::kTRDpid)) {
373     Double_t d[AliPID::kSPECIES];
374     track->GetTRDpid(d);
375     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]*=d[j];
376   }
377
378   if (track->IsOn(AliESDtrack::kTOFpid)) {
379     Double_t d[AliPID::kSPECIES];
380     track->GetTOFpid(d);
381     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]*=d[j];
382   }
383
384   if (track->IsOn(AliESDtrack::kHMPIDpid)) {
385     Double_t d[AliPID::kSPECIES];
386     track->GetHMPIDpid(d);
387     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]*=d[j];
388   }
389
390   track->SetESDpid(p);
391 }
392 //_________________________________________________________________________
393 Bool_t AliESDpid::CheckTOFMatching(AliESDtrack *track) const{
394   //
395   // Check pid matching of TOF with TPC as reference
396   //
397     Bool_t status = kFALSE;
398     
399     Double_t exptimes[AliPID::kSPECIESC];
400     track->GetIntegratedTimes(exptimes);
401     
402     Float_t p = track->P();
403     
404     Float_t dedx = track->GetTPCsignal();
405     Float_t time = track->GetTOFsignal() - fTOFResponse.GetStartTime(p);
406     
407     Double_t ptpc[3];
408     track->GetInnerPxPyPz(ptpc);
409     Float_t momtpc=TMath::Sqrt(ptpc[0]*ptpc[0] + ptpc[1]*ptpc[1] + ptpc[2]*ptpc[2]);
410     
411     for(Int_t i=0;i < AliPID::kSPECIES;i++){
412         AliPID::EParticleType type=AliPID::EParticleType(i);
413         
414         Float_t resolutionTOF = fTOFResponse.GetExpectedSigma(p, exptimes[i], AliPID::ParticleMass(i));
415         if(TMath::Abs(exptimes[i] - time) < fRange * resolutionTOF){
416             Float_t dedxExp = fTPCResponse.GetExpectedSignal(momtpc,type);
417             Float_t resolutionTPC = fTPCResponse.GetExpectedSigma(momtpc,track->GetTPCsignalN(),type);
418             
419             if(TMath::Abs(dedx - dedxExp) < fRangeTOFMismatch * resolutionTPC){
420                 status = kTRUE;
421             }
422         }
423     }
424     
425     // for nuclei
426     Float_t resolutionTOFpr = fTOFResponse.GetExpectedSigma(p, exptimes[4], AliPID::ParticleMass(4));
427     if(!status && (exptimes[4] + fRange*resolutionTOFpr < time)) status = kTRUE;
428     
429     
430     return status;
431 }
432
433 //_________________________________________________________________________
434 Float_t AliESDpid::GetSignalDeltaTOFold(const AliVParticle *track, AliPID::EParticleType type, Bool_t ratio/*=kFALSE*/) const
435 {
436   //
437   // TOF signal - expected
438   //
439   AliVTrack *vtrack=(AliVTrack*)track;
440   
441   const Double_t expTime = fTOFResponse.GetExpectedSignal(vtrack,type);
442   Double_t tofTime = 99999;
443   if (fTuneMConData && ((fTuneMConDataMask & kDetTOF) == kDetTOF) ) tofTime = (Double_t)this->GetTOFsignalTunedOnData((AliVTrack*)vtrack);
444   else tofTime=vtrack->GetTOFsignal();
445   tofTime = tofTime  - fTOFResponse.GetStartTime(vtrack->P());
446   Double_t delta=-9999.;
447
448   if (!ratio) delta=tofTime-expTime;
449   else if (expTime>1.e-20) delta=tofTime/expTime;
450   
451   return delta;
452 }
453
454 //_________________________________________________________________________
455 Float_t AliESDpid::GetNumberOfSigmasTOFold(const AliVParticle *track, AliPID::EParticleType type) const
456 {
457   //
458   // Number of sigma implementation for the TOF
459   //
460
461   AliVTrack *vtrack=(AliVTrack*)track;
462   Double_t tofTime = 99999;
463   if (fTuneMConData && ((fTuneMConDataMask & kDetTOF) == kDetTOF) ) tofTime = (Double_t)this->GetTOFsignalTunedOnData((AliVTrack*)vtrack);
464   else tofTime=vtrack->GetTOFsignal();
465   
466   Double_t expTime = fTOFResponse.GetExpectedSignal(vtrack,type);
467   return (tofTime - fTOFResponse.GetStartTime(vtrack->P()) - expTime)/fTOFResponse.GetExpectedSigma(vtrack->P(),expTime,AliPID::ParticleMassZ(type));
468 }
469
470 //_________________________________________________________________________
471 void AliESDpid::SetPIDForTracking(AliESDtrack *esdtr) const
472 {
473   // assign mass for tracking
474   //
475
476   // in principle AliPIDCombined could be used to also set priors
477   //AliPIDCombined pidProb;
478   //pidProb.SetDetectorMask(kDetTPC);              // use only TPC information, couls also be changed
479   //pidProb.SetSelectedSpecies(AliPID::kSPECIESC); // number of species to use
480   //pidProb.SetDefaultTPCPriors();                 // load default priors
481
482   Double_t prob[AliPID::kSPECIESC]={0.};
483   EDetPidStatus pidStatus=ComputePIDProbability(kTPC, esdtr, AliPID::kSPECIESC, prob);
484   // check if a valid signal was found, otherwise return pion mass
485   if (pidStatus!=kDetPidOk) {
486     esdtr->SetPIDForTracking(AliPID::kPion);
487     return;
488   }
489
490   // or with AliPIDCombined
491   // pidProb.ComputeProbabilities(esdtr, this, p);
492
493   // find max probability
494   Float_t max=0.;
495   Int_t pid=-1;
496   for (Int_t i=0; i<AliPID::kSPECIESC; ++i) if (prob[i]>max) {pid=i; max=prob[i];}
497
498   //int pid = AliPID::kPion; // this should be substituted by real most probable TPC pid (e,mu -> pion) or poin if no PID possible
499
500   //
501   if (pid>AliPID::kSPECIESC-1) pid = AliPID::kPion;
502   //
503   esdtr->SetPIDForTracking( pid );
504   //
505 }
506
507
508 //_________________________________________________________________________
509 void AliESDpid::MakePIDForTracking(AliESDEvent *event) const
510 {
511   // assign masses using for tracking
512   Int_t nTrk=event->GetNumberOfTracks();
513   for (Int_t iTrk=nTrk; iTrk--;) {  
514     AliESDtrack *track = event->GetTrack(iTrk);
515     SetPIDForTracking(track);
516   }
517
518 }