589d963f145a61d8ceb75ad0a139740f2813b27b
[u/mrichter/AliRoot.git] / STEER / 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$ */
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 "AliLog.h"
30 #include "AliPID.h"
31 #include "AliTOFHeader.h"
32 #include "AliESDpid.h"
33 #include "AliESDEvent.h"
34 #include "AliESDtrack.h"
35
36 ClassImp(AliESDpid)
37
38 Int_t AliESDpid::MakePID(AliESDEvent *event, Bool_t TPConly, Float_t timeZeroTOF) const {
39   //
40   //  Calculate probabilities for all detectors, except if TPConly==kTRUE
41   //  and combine PID
42   //  
43   //   Option TPConly==kTRUE is used during reconstruction, 
44   //  because ITS tracking uses TPC pid
45   //  HMPID and TRD pid are done in detector reconstructors
46   //
47
48   /*
49   Float_t timeZeroTOF = 0;
50   if (subtractT0) 
51     timeZeroTOF = event->GetT0();
52   */
53   Int_t nTrk=event->GetNumberOfTracks();
54   for (Int_t iTrk=0; iTrk<nTrk; iTrk++) {  
55     AliESDtrack *track=event->GetTrack(iTrk);
56     MakeTPCPID(track);
57     if (!TPConly) {
58       MakeITSPID(track);
59       MakeTOFPID(track, timeZeroTOF);
60       //MakeHMPIDPID(track);
61       //MakeTRDPID(track);
62     }
63     CombinePID(track);
64   }
65   return 0;
66 }
67 //_________________________________________________________________________
68 void AliESDpid::MakeTPCPID(AliESDtrack *track) const
69 {
70   //
71   //  TPC pid using bethe-bloch and gaussian response
72   //
73   if ((track->GetStatus()&AliESDtrack::kTPCin )==0)
74     if ((track->GetStatus()&AliESDtrack::kTPCout)==0) return;
75
76     Double_t mom = track->GetP();
77     const AliExternalTrackParam *in=track->GetInnerParam();
78     if (in) mom = in->GetP();
79
80     Double_t p[AliPID::kSPECIES];
81     Double_t dedx=track->GetTPCsignal(); 
82     Bool_t mismatch=kTRUE, heavy=kTRUE;
83
84     for (Int_t j=0; j<AliPID::kSPECIES; j++) {
85       AliPID::EParticleType type=AliPID::EParticleType(j);
86       Double_t bethe=fTPCResponse.GetExpectedSignal(mom,type); 
87       Double_t sigma=fTPCResponse.GetExpectedSigma(mom,track->GetTPCsignalN(),type);
88       if (TMath::Abs(dedx-bethe) > fRange*sigma) {
89         p[j]=TMath::Exp(-0.5*fRange*fRange)/sigma;
90       } else {
91         p[j]=TMath::Exp(-0.5*(dedx-bethe)*(dedx-bethe)/(sigma*sigma))/sigma;
92         mismatch=kFALSE;
93       }
94
95       // Check for particles heavier than (AliPID::kSPECIES - 1)
96       if (dedx < (bethe + fRange*sigma)) heavy=kFALSE;
97
98     }
99
100     if (mismatch)
101        for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
102
103     track->SetTPCpid(p);
104
105     if (heavy) track->ResetStatus(AliESDtrack::kTPCpid);
106
107 }
108 //_________________________________________________________________________
109 void AliESDpid::MakeITSPID(AliESDtrack *track) const
110 {
111   //
112   // ITS PID
113   // Two options, depending on fITSPIDmethod:
114   //  1) Truncated mean method
115   //  2) Likelihood, using charges measured in all 4 layers and 
116   //     Landau+gaus response functions
117   //
118
119   if ((track->GetStatus()&AliESDtrack::kITSin)==0 &&
120       (track->GetStatus()&AliESDtrack::kITSout)==0) return;
121
122   Double_t mom=track->GetP();  
123   if (fITSPIDmethod == kITSTruncMean) {
124     Double_t dedx=track->GetITSsignal();
125     Bool_t isSA=kTRUE;
126     Double_t momITS=mom;
127     ULong_t trStatus=track->GetStatus();
128     if(trStatus&AliESDtrack::kTPCin) isSA=kFALSE;
129     UChar_t clumap=track->GetITSClusterMap();
130     Int_t nPointsForPid=0;
131     for(Int_t i=2; i<6; i++){
132       if(clumap&(1<<i)) ++nPointsForPid;
133     }
134
135     if(nPointsForPid<3) { // track not to be used for combined PID purposes
136       track->ResetStatus(AliESDtrack::kITSpid);
137       return;
138     }
139
140     Double_t p[10];
141
142     Bool_t mismatch=kTRUE, heavy=kTRUE;
143     for (Int_t j=0; j<AliPID::kSPECIES; j++) {
144       Double_t mass=AliPID::ParticleMass(j);//GeV/c^2
145       Double_t bethe=fITSResponse.Bethe(momITS,mass);
146       Double_t sigma=fITSResponse.GetResolution(bethe,nPointsForPid,isSA);
147       if (TMath::Abs(dedx-bethe) > fRange*sigma) {
148         p[j]=TMath::Exp(-0.5*fRange*fRange)/sigma;
149       } else {
150         p[j]=TMath::Exp(-0.5*(dedx-bethe)*(dedx-bethe)/(sigma*sigma))/sigma;
151         mismatch=kFALSE;
152       }
153
154       // Check for particles heavier than (AliPID::kSPECIES - 1)
155       if (dedx < (bethe + fRange*sigma)) heavy=kFALSE;
156
157     }
158
159     if (mismatch)
160        for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
161
162     track->SetITSpid(p);
163
164     if (heavy) track->ResetStatus(AliESDtrack::kITSpid);
165   }
166   else {  // Likelihood method
167     Double_t condprobfun[AliPID::kSPECIES];
168     Double_t qclu[4];
169     track->GetITSdEdxSamples(qclu);
170     fITSResponse.GetITSProbabilities(mom,qclu,condprobfun);
171     track->SetITSpid(condprobfun);
172   }
173
174 }
175 //_________________________________________________________________________
176 void AliESDpid::MakeTOFPID(AliESDtrack *track, Float_t /*timeZeroTOF*/) const
177 {
178   //
179   //   TOF PID using gaussian response
180   //
181
182   if ((track->GetStatus()&AliESDtrack::kTOFout)==0) return;
183   if ((track->GetStatus()&AliESDtrack::kTIME)==0) return;
184
185   Int_t ibin = fTOFResponse.GetMomBin(track->GetP());
186   Float_t timezero = fTOFResponse.GetT0bin(ibin);
187
188   Double_t time[AliPID::kSPECIESN];
189   track->GetIntegratedTimes(time);
190
191   Double_t sigma[AliPID::kSPECIES];
192   for (Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++) {
193     sigma[iPart] = fTOFResponse.GetExpectedSigma(track->GetP(),time[iPart],AliPID::ParticleMass(iPart));
194   }
195
196   AliDebugGeneral("AliESDpid::MakeTOFPID",2,
197            Form("Expected TOF signals [ps]: %f %f %f %f %f",
198                   time[AliPID::kElectron],
199                   time[AliPID::kMuon],
200                   time[AliPID::kPion],
201                   time[AliPID::kKaon],
202                   time[AliPID::kProton]));
203
204   AliDebugGeneral("AliESDpid::MakeTOFPID",2,
205            Form("Expected TOF std deviations [ps]: %f %f %f %f %f",
206                   sigma[AliPID::kElectron],
207                   sigma[AliPID::kMuon],
208                   sigma[AliPID::kPion],
209                   sigma[AliPID::kKaon],
210                   sigma[AliPID::kProton]
211                   ));
212
213   Double_t tof = track->GetTOFsignal() - timezero;
214
215   Double_t p[AliPID::kSPECIES];
216   Bool_t mismatch = kTRUE, heavy = kTRUE;
217   for (Int_t j=0; j<AliPID::kSPECIES; j++) {
218     Double_t sig = sigma[j];
219     if (TMath::Abs(tof-time[j]) > (fRange+2)*sig) {
220         p[j] = TMath::Exp(-0.5*(fRange+2)*(fRange+2))/sig;
221     } else
222       p[j] = TMath::Exp(-0.5*(tof-time[j])*(tof-time[j])/(sig*sig))/sig;
223
224     // Check the mismatching
225     Double_t mass = AliPID::ParticleMass(j);
226     Double_t pm = fTOFResponse.GetMismatchProbability(track->GetP(),mass);
227     if (p[j]>pm) mismatch = kFALSE;
228
229     // Check for particles heavier than (AliPID::kSPECIES - 1)
230     if (tof < (time[j] + fRange*sig)) heavy=kFALSE;
231
232   }
233
234   if (mismatch)
235     for (Int_t j=0; j<AliPID::kSPECIES; j++) p[j]=1./AliPID::kSPECIES;
236
237   track->SetTOFpid(p);
238
239   if (heavy) track->ResetStatus(AliESDtrack::kTOFpid);    
240   if (!CheckTOFMatching(track)) track->SetStatus(AliESDtrack::kTOFmismatch);
241   
242 }
243 //_________________________________________________________________________
244 void AliESDpid::MakeTRDPID(AliESDtrack *track) const
245 {
246   //
247   // Method to recalculate the TRD PID probabilities
248   //
249   Double_t prob[AliPID::kSPECIES];
250   ComputeTRDProbability(track, AliPID::kSPECIES, prob);
251   track->SetTRDpid(prob);
252 }
253 //_________________________________________________________________________
254 void AliESDpid::CombinePID(AliESDtrack *track) const
255 {
256   //
257   // Combine the information of various detectors
258   // to determine the Particle Identification
259   //
260   Int_t ns=AliPID::kSPECIES;
261   Double_t p[10]={1.,1.,1.,1.,1.,1.,1.,1.,1.,1.};
262
263   if (track->IsOn(AliESDtrack::kITSpid)) {
264     Double_t d[10];
265     track->GetITSpid(d);
266     for (Int_t j=0; j<ns; j++) p[j]*=d[j];
267   }
268
269   if (track->IsOn(AliESDtrack::kTPCpid)) {
270     Double_t d[10];
271     track->GetTPCpid(d);
272     for (Int_t j=0; j<ns; j++) p[j]*=d[j];
273   }
274
275   if (track->IsOn(AliESDtrack::kTRDpid)) {
276     Double_t d[10];
277     track->GetTRDpid(d);
278     for (Int_t j=0; j<ns; j++) p[j]*=d[j];
279   }
280
281   if (track->IsOn(AliESDtrack::kTOFpid)) {
282     Double_t d[10];
283     track->GetTOFpid(d);
284     for (Int_t j=0; j<ns; j++) p[j]*=d[j];
285   }
286
287   if (track->IsOn(AliESDtrack::kHMPIDpid)) {
288     Double_t d[10];
289     track->GetHMPIDpid(d);
290     for (Int_t j=0; j<ns; j++) p[j]*=d[j];
291   }
292
293   track->SetESDpid(p);
294 }
295 //_________________________________________________________________________
296 Bool_t AliESDpid::CheckTOFMatching(AliESDtrack *track) const{
297   //
298   // Check pid matching of TOF with TPC as reference
299   //
300     Bool_t status = kFALSE;
301     
302     Double_t exptimes[5];
303     track->GetIntegratedTimes(exptimes);
304     
305     Float_t p = track->P();
306     
307     Float_t dedx = track->GetTPCsignal();
308     Float_t time = track->GetTOFsignal() - fTOFResponse.GetStartTime(p);
309     
310     Double_t ptpc[3];
311     track->GetInnerPxPyPz(ptpc);
312     Float_t momtpc=TMath::Sqrt(ptpc[0]*ptpc[0] + ptpc[1]*ptpc[1] + ptpc[2]*ptpc[2]);
313     
314     for(Int_t i=0;i < 5;i++){
315         AliPID::EParticleType type=AliPID::EParticleType(i);
316         
317         Float_t resolutionTOF = fTOFResponse.GetExpectedSigma(p, exptimes[i], AliPID::ParticleMass(i));
318         if(TMath::Abs(exptimes[i] - time) < fRange * resolutionTOF){
319             Float_t dedxExp = fTPCResponse.GetExpectedSignal(momtpc,type);
320             Float_t resolutionTPC = fTPCResponse.GetExpectedSigma(momtpc,track->GetTPCsignalN(),type);
321             
322             if(TMath::Abs(dedx - dedxExp) < fRangeTOFMismatch * resolutionTPC){
323                 status = kTRUE;
324             }
325         }
326     }
327     
328     // for nuclei
329     Float_t resolutionTOFpr = fTOFResponse.GetExpectedSigma(p, exptimes[4], AliPID::ParticleMass(4));
330     if(!status && (exptimes[4] + fRange*resolutionTOFpr < time)) status = kTRUE;
331     
332     
333     return status;
334 }
335 //_________________________________________________________________________
336 void AliESDpid::SetTOFResponse(AliVEvent *vevent,EStartTimeType_t option){
337   //
338   // Set TOF response function
339   // Input option for event_time used
340   //
341
342     AliESDEvent *event=(AliESDEvent*)vevent;
343   
344     Float_t t0spread = 0.; //event->GetEventTimeSpread();
345     if(t0spread < 10) t0spread = 80;
346
347     // T0 from TOF algorithm
348
349     Bool_t flagT0TOF=kFALSE;
350     Bool_t flagT0T0=kFALSE;
351     Float_t *startTime = new Float_t[fTOFResponse.GetNmomBins()];
352     Float_t *startTimeRes = new Float_t[fTOFResponse.GetNmomBins()];
353     Int_t *startTimeMask = new Int_t[fTOFResponse.GetNmomBins()];
354
355     // T0-TOF arrays
356     Float_t *estimatedT0event = new Float_t[fTOFResponse.GetNmomBins()];
357     Float_t *estimatedT0resolution = new Float_t[fTOFResponse.GetNmomBins()];
358     for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
359       estimatedT0event[i]=0.0;
360       estimatedT0resolution[i]=0.0;
361       startTimeMask[i] = 0;
362     }
363
364     Float_t resT0A=75,resT0C=65,resT0AC=55;
365     if(event->GetT0TOF()){ // check if T0 detector information is available
366         flagT0T0=kTRUE;
367     }
368
369
370     AliTOFHeader *tofHeader =(AliTOFHeader*)event->GetTOFHeader();
371
372     if(tofHeader){ // read global info and T0-TOF info from ESD
373       fTOFResponse.SetTimeResolution(tofHeader->GetTOFResolution());
374       t0spread = tofHeader->GetT0spread(); // read t0 sprad
375       if(t0spread < 10) t0spread = 80;
376
377       flagT0TOF=kTRUE;
378       for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){ // read T0-TOF default value
379         startTime[i]=tofHeader->GetDefaultEventTimeVal();
380         startTimeRes[i]=tofHeader->GetDefaultEventTimeRes();
381         if(startTimeRes[i] < 1.e-5) startTimeRes[i] = t0spread;
382       }
383
384       TArrayI *ibin=tofHeader->GetNvalues();
385       TArrayF *t0Bin=tofHeader->GetEventTimeValues();
386       TArrayF *t0ResBin=tofHeader->GetEventTimeRes();
387       for(Int_t j=0;j < tofHeader->GetNbins();j++){ // fill T0-TOF in p-bins
388         Int_t icurrent = (Int_t)ibin->GetAt(j);
389         startTime[icurrent]=t0Bin->GetAt(j);
390         startTimeRes[icurrent]=t0ResBin->GetAt(j);
391         if(startTimeRes[icurrent] < 1.e-5) startTimeRes[icurrent] = t0spread;
392      }
393     }
394
395     // for cut of 3 sigma on t0 spread
396     Float_t t0cut = 3 * t0spread;
397     if(t0cut < 500) t0cut = 500;
398
399     if(option == kFILL_T0){ // T0-FILL is used
400         for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
401           estimatedT0event[i]=0.0;
402           estimatedT0resolution[i]=t0spread;
403         }
404         fTOFResponse.SetT0event(estimatedT0event);
405         fTOFResponse.SetT0resolution(estimatedT0resolution);
406     }
407
408     if(option == kTOF_T0){ // T0-TOF is used when available (T0-FILL otherwise) from ESD
409         if(flagT0TOF){
410             fTOFResponse.SetT0event(startTime);
411             fTOFResponse.SetT0resolution(startTimeRes);
412             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
413               if(startTimeRes[i]<t0spread) startTimeMask[i]=1;
414               fTOFResponse.SetT0binMask(i,startTimeMask[i]);
415             }
416         }
417         else{
418             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
419               estimatedT0event[i]=0.0;
420               estimatedT0resolution[i]=t0spread;
421               fTOFResponse.SetT0binMask(i,startTimeMask[i]);
422             }
423             fTOFResponse.SetT0event(estimatedT0event);
424             fTOFResponse.SetT0resolution(estimatedT0resolution);
425         }
426     }
427     else if(option == kBest_T0){ // T0-T0 or T0-TOF are used when available (T0-FILL otherwise) from ESD
428         Float_t t0AC=-10000;
429         Float_t t0A=-10000;
430         Float_t t0C=-10000;
431         if(flagT0T0){
432             t0AC= event->GetT0TOF()[0];
433             t0A= event->GetT0TOF()[1];
434             t0C= event->GetT0TOF()[2];
435         }
436
437         Float_t t0t0Best = 0;
438         Float_t t0t0BestRes = 9999;
439         Int_t t0used=0;
440         if(TMath::Abs(t0A) < t0cut && TMath::Abs(t0C) < t0cut && TMath::Abs(t0C-t0A) < 500){
441             t0t0Best = t0AC;
442             t0t0BestRes = resT0AC;
443             t0used=6;
444         }
445         else if(TMath::Abs(t0C) < t0cut){
446             t0t0Best = t0C;
447             t0t0BestRes = resT0C;
448             t0used=4;
449         }
450         else if(TMath::Abs(t0A) < t0cut){
451             t0t0Best = t0A;
452             t0t0BestRes = resT0A;
453             t0used=2;
454         }
455
456         if(flagT0TOF){ // if T0-TOF info is available
457             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
458                 if(t0t0BestRes < 999){
459                   if(startTimeRes[i] < t0spread){
460                     Double_t wtot = 1./startTimeRes[i]/startTimeRes[i] + 1./t0t0BestRes/t0t0BestRes;
461                     Double_t t0best = startTime[i]/startTimeRes[i]/startTimeRes[i] + t0t0Best/t0t0BestRes/t0t0BestRes;
462                     estimatedT0event[i]=t0best / wtot;
463                     estimatedT0resolution[i]=1./TMath::Sqrt(wtot);
464                     startTimeMask[i] = t0used+1;
465                   }
466                   else {
467                     estimatedT0event[i]=t0t0Best;
468                     estimatedT0resolution[i]=t0t0BestRes;
469                     startTimeMask[i] = t0used;
470                   }
471                 }
472                 else{
473                   estimatedT0event[i]=startTime[i];
474                   estimatedT0resolution[i]=startTimeRes[i];
475                   if(startTimeRes[i]<t0spread) startTimeMask[i]=1;
476                 }
477                 fTOFResponse.SetT0binMask(i,startTimeMask[i]);
478             }
479             fTOFResponse.SetT0event(estimatedT0event);
480             fTOFResponse.SetT0resolution(estimatedT0resolution);
481         }
482         else{ // if no T0-TOF info is available
483             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
484               fTOFResponse.SetT0binMask(i,t0used);
485               if(t0t0BestRes < 999){
486                 estimatedT0event[i]=t0t0Best;
487                 estimatedT0resolution[i]=t0t0BestRes;
488               }
489               else{
490                 estimatedT0event[i]=0.0;
491                 estimatedT0resolution[i]=t0spread;
492               }
493             }
494             fTOFResponse.SetT0event(estimatedT0event);
495             fTOFResponse.SetT0resolution(estimatedT0resolution);
496         }
497     }
498
499     else if(option == kT0_T0){ // T0-T0 is used when available (T0-FILL otherwise) from ESD
500         Float_t t0AC=-10000;
501         Float_t t0A=-10000;
502         Float_t t0C=-10000;
503         if(flagT0T0){
504             t0AC= event->GetT0TOF()[0];
505             t0A= event->GetT0TOF()[1];
506             t0C= event->GetT0TOF()[2];
507         }
508
509         if(TMath::Abs(t0A) < t0cut && TMath::Abs(t0C) < t0cut && TMath::Abs(t0C-t0A) < 500){
510             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
511               estimatedT0event[i]=t0AC;
512               estimatedT0resolution[i]=resT0AC;
513               fTOFResponse.SetT0binMask(i,6);
514             }
515         }
516         else if(TMath::Abs(t0C) < t0cut){
517             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
518               estimatedT0event[i]=t0C;
519               estimatedT0resolution[i]=resT0C;
520               fTOFResponse.SetT0binMask(i,4);
521             }
522         }
523         else if(TMath::Abs(t0A) < t0cut){
524             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
525               estimatedT0event[i]=t0A;
526               estimatedT0resolution[i]=resT0A;
527               fTOFResponse.SetT0binMask(i,2);
528             }
529         }
530         else{
531             for(Int_t i=0;i<fTOFResponse.GetNmomBins();i++){
532               estimatedT0event[i]=0.0;
533               estimatedT0resolution[i]=t0spread;
534               fTOFResponse.SetT0binMask(i,0);
535             }
536         }
537         fTOFResponse.SetT0event(estimatedT0event);
538         fTOFResponse.SetT0resolution(estimatedT0resolution);
539     }
540     delete [] startTime;
541     delete [] startTimeRes;
542     delete [] startTimeMask;
543     delete [] estimatedT0event;
544     delete [] estimatedT0resolution;
545 }