]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDpidESD.cxx
First implementation of neural network PID
[u/mrichter/AliRoot.git] / TRD / AliTRDpidESD.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 //                                                                        //
20 // Implementation of the TRD PID class                                    //
21 //                                                                        //
22 // Assigns the electron and pion likelihoods to each ESD track.           //
23 // The function MakePID(AliESDEvent *event) calculates the probability         //
24 // of having dedx and a maximum timbin at a given                         //
25 // momentum (mom) and particle type k                                     //
26 // from the precalculated distributions.                                  //
27 //                                                                        //
28 // Authors :                                                              //
29 // Prashant Shukla <shukla@pi0.physi.uni-heidelberg.de> (Original version)//
30 // Alex Bercuci (a.bercuci@gsi.de)                                        //
31 //                                                                        //
32 ////////////////////////////////////////////////////////////////////////////
33
34 #include "AliLog.h"
35 #include "AliESDEvent.h"
36 #include "AliESDtrack.h"
37 #include "AliTracker.h"
38
39 #include "AliTRDpidESD.h"
40 #include "AliTRDgeometry.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliRun.h"
43 #include "AliTRDtrack.h"
44 #include "Cal/AliTRDCalPID.h"
45
46
47 ClassImp(AliTRDpidESD)
48
49   Bool_t AliTRDpidESD::fCheckTrackStatus = kTRUE;
50   Bool_t AliTRDpidESD::fCheckKinkStatus  = kFALSE;
51   Int_t AliTRDpidESD::fMinPlane          = 0;
52
53 //_____________________________________________________________________________
54 AliTRDpidESD::AliTRDpidESD()
55   :TObject(), fTrack(0x0)
56 {
57   //
58   // Default constructor
59   //
60
61 }
62
63 //_____________________________________________________________________________
64 AliTRDpidESD::AliTRDpidESD(const AliTRDpidESD &p)
65   :TObject(p), fTrack(0x0)
66 {
67   //
68   // AliTRDpidESD copy constructor
69   //
70
71   ((AliTRDpidESD &) p).Copy(*this);
72
73 }
74
75 //_____________________________________________________________________________
76 AliTRDpidESD::~AliTRDpidESD()
77 {
78   //
79   // Destructor
80   //
81         if(fTrack) delete fTrack;
82 }
83
84 //_____________________________________________________________________________
85 AliTRDpidESD &AliTRDpidESD::operator=(const AliTRDpidESD &p)
86 {
87   //
88   // Assignment operator
89   //
90
91   if (this != &p) ((AliTRDpidESD &) p).Copy(*this);
92   return *this;
93
94 }
95
96 //_____________________________________________________________________________
97 void AliTRDpidESD::Copy(TObject &p) const
98 {
99   //
100   // Copy function
101   //
102
103   ((AliTRDpidESD &) p).fCheckTrackStatus          = fCheckTrackStatus;
104   ((AliTRDpidESD &) p).fCheckKinkStatus           = fCheckKinkStatus;
105   ((AliTRDpidESD &) p).fMinPlane                  = fMinPlane;
106   ((AliTRDpidESD &) p).fTrack                     = 0x0;
107         
108 }
109
110 //_____________________________________________________________________________
111 Int_t AliTRDpidESD::MakePID(AliESDEvent *event)
112 {
113   //
114   // This function calculates the PID probabilities based on TRD signals
115   //
116   // The method produces probabilities based on the charge
117   // and the position of the maximum time bin in each layer.
118   // The dE/dx information can be used as global charge or 2 to 3
119   // slices. Check AliTRDCalPID and AliTRDCalPIDRefMaker for the actual
120   // implementation.
121   //
122   // Author
123   // Alex Bercuci (A.Bercuci@gsi.de) 2nd May 2007
124
125         AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
126         if (!calibration) {
127                 AliErrorGeneral("AliTRDpidESD::MakePID()"
128                         ,"No access to calibration data");
129                 return -1;
130         }
131         
132         // Retrieve the CDB container class with the probability distributions
133         const AliTRDCalPID *pd = calibration->GetPIDObject(1);
134         if (!pd) {
135                 AliErrorGeneral("AliTRDpidESD::MakePID()"
136                         ,"No access to AliTRDCalPID");
137                 return -1;
138         }
139
140         // Loop through all ESD tracks
141         Double_t p[10];
142         AliESDtrack *t = 0x0;
143         Float_t dedx[AliTRDtrack::kNslice], dEdx;
144         Int_t   timebin;
145         Float_t mom, length;
146         Int_t   nPlanePID;
147         for (Int_t i=0; i<event->GetNumberOfTracks(); i++) {
148                 t = event->GetTrack(i);
149                 
150                 // Check track
151                 if(!CheckTrack(t)) continue;
152
153                                                 
154                 // Skip tracks which have no TRD signal at all
155                 if (t->GetTRDsignal() == 0.) continue;
156         
157                 // Loop over detector layers
158                 mom          = 0.;
159                 length       = 0.;
160                 nPlanePID    = 0;
161                 for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) p[iSpecies] = 1./AliPID::kSPECIES;
162                 for (Int_t iPlan = 0; iPlan < AliTRDgeometry::kNplan; iPlan++) {
163                         // read data for track segment
164                         for(int iSlice=0; iSlice<AliTRDtrack::kNslice; iSlice++)
165                                 dedx[iSlice] = t->GetTRDsignals(iPlan, iSlice);
166                         dEdx    = t->GetTRDsignals(iPlan, -1);
167                         timebin = t->GetTRDTimBin(iPlan);
168
169                         // check data
170                         if ((dEdx <=  0.) || (timebin <= -1.)) continue;
171
172                         // retrive kinematic info for this track segment
173                         if(!RecalculateTrackSegmentKine(t, iPlan, mom, length)){
174                                 // information is not fully reliable especialy for length
175                                 // estimation. To be used in the future. 
176                         }
177                         
178                         // this track segment has fulfilled all requierments
179                         nPlanePID++;
180
181                         // Get the probabilities for the different particle species
182                         for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) {
183                                 p[iSpecies] *= pd->GetProbability(iSpecies, mom, dedx, length, iPlan);
184                                 //p[iSpecies] *= pd->GetProbabilityT(iSpecies, mom, timebin);
185                         }
186                 }
187                 if(nPlanePID == 0) continue;
188                 
189                 // normalize probabilities
190                 Double_t probTotal = 0.;
191                 for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) probTotal   += p[iSpecies];
192                 if(probTotal <= 0.){
193                         AliWarningGeneral("AliTRDpidESD::MakePID()", Form("The total probability (%e) over all species <= 0 in ESD track %d. This may be caused by some error in reference data. Calculation continue but results might be corrupted.", probTotal, i));
194                         continue;
195                 }
196                 for(Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) p[iSpecies] /= probTotal;
197
198                 // book PID to the track
199                 t->SetTRDpid(p);
200                 t->SetTRDpidQuality(nPlanePID);
201         }
202         
203         return 0;
204 }
205
206 //_____________________________________________________________________________
207 Bool_t AliTRDpidESD::CheckTrack(AliESDtrack *t)
208 {
209   //
210   // Check if track is eligible for PID calculations
211   //
212         
213         // Check the ESD track status
214         if (fCheckTrackStatus) {
215                 if (((t->GetStatus() & AliESDtrack::kTRDout  ) == 0) &&
216                         ((t->GetStatus() & AliESDtrack::kTRDrefit) == 0)) return kFALSE;
217         }
218
219         // Check for ESD kink tracks
220         if (fCheckKinkStatus && (t->GetKinkIndex(0) != 0)) return kFALSE;
221
222         return kTRUE;
223 }
224
225 //_____________________________________________________________________________
226 Bool_t AliTRDpidESD::RecalculateTrackSegmentKine(AliESDtrack *esd, Int_t plan, Float_t &mom, Float_t &length)
227 {
228   //
229   // Retrive momentum "mom" and track "length" in TRD chamber from plane
230   // "plan" according to information stored in AliESDtrack "t".
231   //
232   // Origin
233   // Alex Bercuci (A.Bercuci@gsi.de)    
234
235         const Float_t kAmHalfWidth = AliTRDgeometry::AmThick() / 2.;
236         const Float_t kDrWidth     = AliTRDgeometry::DrThick();
237         const Float_t kTime0       = AliTRDgeometry::GetTime0(plan);
238
239         // set initial length value to chamber height 
240         length = 2 * kAmHalfWidth + kDrWidth;
241                 
242         // retrive track's outer param
243         const AliExternalTrackParam *op = esd->GetOuterParam();
244         if(!op){
245                 mom    = esd->GetP();
246                 return kFALSE;
247         }
248
249         AliExternalTrackParam *param = 0x0;
250         if(!fTrack){
251                 fTrack = new AliExternalTrackParam(*op);
252                 param = fTrack;
253         } else param = new(fTrack) AliExternalTrackParam(*op);
254         
255         // retrive the magnetic field
256         Double_t xyz0[3];
257         op->GetXYZ(xyz0);
258         Float_t field = AliTracker::GetBz(xyz0); // Bz in kG at point xyz0
259         Double_t s, t;
260
261         // propagate to chamber entrance
262         if(!param->PropagateTo(kTime0-kAmHalfWidth-kDrWidth, field)){
263                 mom    = op->GetP();
264                 s      = op->GetSnp();
265                 t      = op->GetTgl();
266                 if (s < 1.) length /= TMath::Sqrt((1. - s*s) / (1. + t*t));
267                 return kFALSE;
268         }
269         mom        = param->GetP();
270         s = param->GetSnp();
271         t = param->GetTgl();
272         if (s < 1.) length    /= TMath::Sqrt((1. - s*s) / (1. + t*t));
273
274         // check if track is crossing tracking sector by propagating to chamber exit- maybe is too much :)
275         Double_t alpha = param->GetAlpha();
276         if(!param->PropagateTo(kTime0+kAmHalfWidth, field)) return kFALSE;
277                 
278         // mark track segments which are crossing SM boundaries along chamber
279         if(TMath::Abs(alpha-param->GetAlpha())>.01) return kFALSE;
280         
281         return kTRUE;
282 }
283