]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFtracker.cxx
Track reconstruction code for TOF: updating
[u/mrichter/AliRoot.git] / TOF / AliTOFtracker.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 // AliTOFtracker Class
16 // Task: Perform association of the ESD tracks to TOF Clusters
17 // and Update ESD track with associated TOF Cluster parameters 
18 //
19 // -- Authors : S. Arcelli, C. Zampolli (Bologna University and INFN) 
20 // -- Contacts: Annalisa.De.Caro@cern.ch
21 // --         : Chiara.Zampolli@bo.infn.it
22 // --         : Silvia.Arcelli@bo.infn.it
23 //--------------------------------------------------------------------
24
25 #include "AliTOFtracker.h"
26 #include "AliTOFtrack.h"
27 #include "TClonesArray.h"
28 #include "TError.h"
29 #include "AliTOFdigit.h"
30 #include "AliTOFGeometry.h"
31 #include "AliTOF.h"
32 #include "AliRun.h"
33 #include "AliModule.h"
34
35 ClassImp(AliTOFtracker)
36
37 //_____________________________________________________________________________
38 AliTOFtracker::AliTOFtracker(AliTOFGeometry * geom, Double_t parPID[2]) { 
39   //AliTOFtracker main Ctor
40
41   fHoles=true;
42   fNseeds=0;
43   fNseedsTOF=0;
44   fngoodmatch=0;
45   fnbadmatch=0;
46   fnunmatch=0;
47   fnmatch=0;
48   fGeom = geom;
49   fTOFpid = new AliTOFpidESD(parPID);
50   fR=378.; 
51   fTOFHeigth=15.3;  
52   fdCut=3.; 
53   fDy=AliTOFGeometry::XPad(); 
54   fDz=AliTOFGeometry::ZPad(); 
55   fDx=1.5; 
56   fSeeds=0x0;
57   fTracks=0x0;
58   fN=0;
59   Init(); // temporary solution to know about Holes/no Holes
60 }
61 //_____________________________________________________________________________
62 AliTOFtracker::AliTOFtracker(const AliTOFtracker &t):AliTracker() { 
63   //AliTOFtracker copy Ctor
64
65   fHoles=t.fHoles;
66   fNseeds=t.fNseeds;
67   fNseedsTOF=t.fNseedsTOF;
68   fngoodmatch=t.fngoodmatch;
69   fnbadmatch=t.fnbadmatch;
70   fnunmatch=t.fnunmatch;
71   fnmatch=t.fnmatch;
72   fGeom = t.fGeom;
73   fTOFpid = t.fTOFpid;
74   fR=t.fR; 
75   fTOFHeigth=t.fTOFHeigth;  
76   fdCut=t.fdCut; 
77   fDy=t.fDy; 
78   fDz=t.fDz; 
79   fDx=1.5; 
80   fSeeds=t.fSeeds;
81   fTracks=t.fTracks;
82   fN=t.fN;
83 }
84 //_____________________________________________________________________________
85 void AliTOFtracker::Init() { 
86
87 // temporary solution to know about Holes/no Holes, will be implemented as 
88 // an AliTOFGeometry getter
89
90   AliModule* frame=gAlice->GetModule("FRAME"); 
91
92   if(!frame) {
93     Error("Init","Could Not load FRAME! Assume Frame with Holes \n");
94     fHoles=true;
95   } else{
96     if(frame->IsVersion()==1) {fHoles=false;}    
97     else {fHoles=true;}      
98   }
99 }
100 //_____________________________________________________________________________
101 Int_t AliTOFtracker::PropagateBack(AliESD* event) {
102   //
103   // Gets seeds from ESD event and Match with TOF Clusters
104   //
105
106
107   //Initialise some counters
108
109   fNseeds=0;
110   fNseedsTOF=0;
111   fngoodmatch=0;
112   fnbadmatch=0;
113   fnunmatch=0;
114   fnmatch=0;
115
116   Int_t ntrk=event->GetNumberOfTracks();
117   fNseeds = ntrk;
118   fSeeds= new TClonesArray("AliESDtrack");
119   TClonesArray &aESDTrack = *fSeeds;
120
121
122   //Load ESD tracks into a local Array of ESD Seeds
123
124   for (Int_t i=0; i<fNseeds; i++) {
125     AliESDtrack *t=event->GetTrack(i);
126     new(aESDTrack[i]) AliESDtrack(*t);
127   }
128
129   //Prepare ESD tracks candidates for TOF Matching
130   CollectESD();
131
132   //First Step with Strict Matching Criterion
133   MatchTracks(kFALSE);
134
135   //Second Step with Looser Matching Criterion
136   MatchTracks(kTRUE);
137
138   Info("PropagateBack","Number of matched tracks: %d",fnmatch);
139   Info("PropagateBack","Number of good matched tracks: %d",fngoodmatch);
140   Info("PropagateBack","Number of bad  matched tracks: %d",fnbadmatch);
141
142   //Update the matched ESD tracks
143
144   for (Int_t i=0; i<ntrk; i++) {
145     AliESDtrack *t=event->GetTrack(i);
146     AliESDtrack *seed =(AliESDtrack*)fSeeds->UncheckedAt(i);
147     if(seed->GetTOFsignal()>0){
148       t->SetTOFsignal(seed->GetTOFsignal());
149       t->SetTOFcluster(seed->GetTOFcluster());
150       AliTOFtrack *track = new AliTOFtrack(*seed); 
151       t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
152       delete track;
153     }
154   }
155
156
157   //Make TOF PID
158   fTOFpid->MakePID(event);
159
160   delete fSeeds;
161   delete fTracks;
162   return 0;
163   
164 }
165 //_________________________________________________________________________
166 void AliTOFtracker::CollectESD() {
167    //prepare the set of ESD tracks to be matched to clusters in TOF
168  
169   fTracks= new TClonesArray("AliTOFtrack");
170   TClonesArray &aTOFTrack = *fTracks;
171   for (Int_t i=0; i<fNseeds; i++) {
172
173     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(i);
174     if ((t->GetStatus()&AliESDtrack::kTPCout)==0)continue;
175
176     // TRD good tracks, already propagated at 371 cm
177
178     if (
179         ((t->GetStatus()&AliESDtrack::kTRDout)!=0) && 
180         ((t->GetStatus()&AliESDtrack::kTRDStop)==0)){
181       AliTOFtrack *track = new AliTOFtrack(*t); 
182       track->SetSeedIndex(i);
183       t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
184       new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
185       fNseedsTOF++;
186       delete track;
187     }
188
189     // Propagate the rest of TPCbp  
190
191     else {
192       AliTOFtrack *track = new AliTOFtrack(*t);
193       if(track->PropagateToInnerTOF(fHoles)){ // temporary solution
194         //      if(track->PropagateToInnerTOF(fGeom->GetHoles())){
195         track->SetSeedIndex(i);
196         t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
197         new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
198         fNseedsTOF++;
199       }
200       delete track;
201     }
202   }
203
204   // Sort according uncertainties on track position 
205   fTracks->Sort();
206
207 }
208 //_________________________________________________________________________
209 void AliTOFtracker::MatchTracks( Bool_t mLastStep){
210
211   //Match ESD tracks to clusters in TOF
212
213   static const Double_t kMasses[]={
214     0.000511, 0.105658, 0.139570, 0.493677, 0.938272, 1.875613
215   };
216   
217   Int_t nSteps=(Int_t)(fTOFHeigth/0.1);
218   
219   for (Int_t i=0; i<fNseedsTOF; i++) {
220
221     AliTOFtrack *track =(AliTOFtrack*)fTracks->UncheckedAt(i);
222     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(track->GetSeedIndex());
223     AliTOFtrack *trackTOFin =new AliTOFtrack(*track);
224     if(t->GetTOFsignal()>0. )continue;
225
226     // Some init 
227
228     Int_t         index[10000];
229     Float_t        dist[10000];
230     Float_t       cxpos[10000];
231     Float_t       crecL[10000];
232     Float_t trackPos[4][nSteps];
233      
234     // Determine a window around the track
235
236     Double_t x,par[5]; 
237     trackTOFin->GetExternalParameters(x,par);
238     Double_t cov[15]; 
239     trackTOFin->GetExternalCovariance(cov);
240     Float_t scalefact=3.;    
241     Double_t dphi=
242       scalefact*
243       ((5*TMath::Sqrt(cov[0]) + 0.5*fDy + 2.5*TMath::Abs(par[2]))/fR); 
244     Double_t dz=
245       scalefact*
246       (5*TMath::Sqrt(cov[2]) + 0.5*fDz + 2.5*TMath::Abs(par[3]));
247     
248     Double_t phi=TMath::ATan2(par[0],x) + trackTOFin->GetAlpha();
249     if (phi<-TMath::Pi())phi+=2*TMath::Pi();
250     if (phi>=TMath::Pi())phi-=2*TMath::Pi();
251     Double_t z=par[1];   
252
253     Int_t clind[6][fN];
254     Int_t nc=0;
255     
256     // find the clusters in the window of the track
257
258     for (Int_t k=FindClusterIndex(z-dz); k<fN; k++) {
259       AliTOFcluster *c=fClusters[k];
260       if (c->GetZ() > z+dz) break;
261       if (c->IsUsed()) continue;
262       
263       Double_t dph=TMath::Abs(c->GetPhi()-phi);
264       if (dph>TMath::Pi()) dph-=2.*TMath::Pi();
265       if (fabs(dph)>dphi) continue;
266     
267       clind[0][nc] = c->GetDetInd(0);
268       clind[1][nc] = c->GetDetInd(1);
269       clind[2][nc] = c->GetDetInd(2);
270       clind[3][nc] = c->GetDetInd(3);
271       clind[4][nc] = c->GetDetInd(4);
272       clind[5][nc] = k;      
273       nc++;
274     }
275
276     //start fine propagation 
277
278     Int_t nStepsDone = 0;
279     for( Int_t istep=0; istep<nSteps; istep++){ 
280
281       Float_t xs=AliTOFGeometry::RinTOF()+istep*0.1;
282       Double_t ymax=xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
283
284       Bool_t skip=kFALSE;
285       Double_t ysect=trackTOFin->GetYat(xs,skip);
286       if(skip)break;
287       if (ysect > ymax) {
288         if (!trackTOFin->Rotate(AliTOFGeometry::GetAlpha())) {
289           break;
290         }
291       } else if (ysect <-ymax) {
292         if (!trackTOFin->Rotate(-AliTOFGeometry::GetAlpha())) {
293           break;
294         }
295       }
296
297       if(!trackTOFin->PropagateTo(xs)) {
298         break;
299       }
300
301       nStepsDone++;
302
303       // store the running point (Globalrf) - fine propagation     
304
305       Double_t x,y,z;
306       trackTOFin->GetGlobalXYZ(x,y,z);
307       trackPos[0][istep]= (Float_t) x;
308       trackPos[1][istep]= (Float_t) y;
309       trackPos[2][istep]= (Float_t) z;   
310       trackPos[3][istep]= trackTOFin->GetIntegratedLength();
311     }
312
313
314     Int_t nfound = 0;
315     for (Int_t istep=0; istep<nStepsDone; istep++) {
316
317       Bool_t isInside =kFALSE;
318       Float_t ctrackPos[3];     
319
320       ctrackPos[0]= trackPos[0][istep];
321       ctrackPos[1]= trackPos[1][istep];
322       ctrackPos[2]= trackPos[2][istep];
323
324       //now see whether the track matches any of the TOF clusters            
325
326       for (Int_t i=0; i<nc; i++){
327         Int_t cind[5];
328         cind[0]= clind[0][i];
329         cind[1]= clind[1][i];
330         cind[2]= clind[2][i];
331         cind[3]= clind[3][i];
332         cind[4]= clind[4][i];
333         Bool_t accept = kFALSE;
334         if( mLastStep)accept = (fGeom->DistanceToPad(cind,ctrackPos)<fdCut);
335         if(!mLastStep)accept = (fGeom->IsInsideThePad(cind,ctrackPos));
336         if(accept){
337           if(!mLastStep)isInside=kTRUE;
338           dist[nfound]=fGeom->DistanceToPad(cind,ctrackPos);
339           crecL[nfound]=trackPos[3][istep];
340           index[nfound]=clind[5][i]; // store cluster id            
341           cxpos[nfound]=AliTOFGeometry::RinTOF()+istep*0.1; //store prop.radius
342           nfound++;
343           if(isInside)break;
344         }//end if accept
345       } //end for on the clusters
346       if(isInside)break;
347     } //end for on the steps     
348
349     if (nfound == 0 ) {
350       fnunmatch++;
351       continue;
352     }
353     
354     fnmatch++;
355
356     // now choose the cluster to be matched with the track.
357
358     Int_t idclus=0;
359     Float_t  recL = 0.;
360     Float_t  xpos=0.;
361     Float_t  mindist=1000.;
362     for (Int_t iclus= 0; iclus<nfound;iclus++){
363       if (dist[iclus]< mindist){
364         mindist = dist[iclus];
365         xpos = cxpos[iclus];
366         idclus =index[iclus]; 
367         recL=crecL[iclus]+fDx*0.5;
368       }
369     }
370
371     AliTOFcluster *c=fClusters[idclus];
372     c->Use();
373
374     // Track length correction for matching Step 2 
375
376     if(mLastStep){
377       Float_t rc=TMath::Sqrt(c->GetR()*c->GetR() + c->GetZ()*c->GetZ());
378       Float_t rt=TMath::Sqrt(trackPos[0][70]*trackPos[0][70]
379                              +trackPos[1][70]*trackPos[1][70]
380                              +trackPos[2][70]*trackPos[2][70]);
381       Float_t dlt=rc-rt;      
382       recL=trackPos[3][70]+dlt;
383     }    
384
385     if (
386         (c->GetLabel(0)==TMath::Abs(trackTOFin->GetLabel()))
387         ||
388         (c->GetLabel(1)==TMath::Abs(trackTOFin->GetLabel()))
389         ||
390         (c->GetLabel(2)==TMath::Abs(trackTOFin->GetLabel()))
391         ) {
392       fngoodmatch++;
393     }
394     else{
395       fnbadmatch++;
396     }
397
398     delete trackTOFin;
399
400     Double_t tof=50*c->GetTDC()+32; // in ps
401     t->SetTOFsignal(tof);
402     t->SetTOFcluster(c->GetIndex());
403     Double_t time[10]; t->GetIntegratedTimes(time);
404     Double_t mom=t->GetP();
405     for(Int_t j=0;j<=5;j++){
406       Double_t mass=kMasses[j];
407       time[j]+=(recL-trackPos[3][0])/3e-2*TMath::Sqrt(mom*mom+mass*mass)/mom;
408     }
409
410     AliTOFtrack *trackTOFout = new AliTOFtrack(*t); 
411     trackTOFout->PropagateTo(xpos);
412     t->UpdateTrackParams(trackTOFout,AliESDtrack::kTOFout);    
413     t->SetIntegratedLength(recL);
414     t->SetIntegratedTimes(time);
415
416     delete trackTOFout;
417   }
418 }
419 //_________________________________________________________________________
420 Int_t AliTOFtracker::LoadClusters(TTree *dTree) {
421   //--------------------------------------------------------------------
422   //This function loads the TOF clusters
423   //--------------------------------------------------------------------
424
425   TBranch *branch=dTree->GetBranch("TOF");
426   if (!branch) { 
427     Error("LoadClusters"," can't get the branch with the TOF digits !\n");
428     return 1;
429   }
430
431   TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;
432   branch->SetAddress(&digits);
433
434   dTree->GetEvent(0);
435   Int_t nd=digits->GetEntriesFast();
436   Info("LoadClusters","number of digits: %d",nd);
437
438   for (Int_t i=0; i<nd; i++) {
439     AliTOFdigit *d=(AliTOFdigit*)digits->UncheckedAt(i);
440     Int_t dig[5]; Float_t g[3];
441     dig[0]=d->GetSector();
442     dig[1]=d->GetPlate();
443     dig[2]=d->GetStrip();
444     dig[3]=d->GetPadz();
445     dig[4]=d->GetPadx();
446
447     fGeom->GetPos(dig,g);
448
449     Double_t h[5];
450     h[0]=TMath::Sqrt(g[0]*g[0]+g[1]*g[1]);
451     h[1]=TMath::ATan2(g[1],g[0]); h[2]=g[2]; 
452     h[3]=d->GetTdc(); h[4]=d->GetAdc();
453
454     AliTOFcluster *cl=new AliTOFcluster(h,d->GetTracks(),dig,i);
455     InsertCluster(cl);
456   }  
457
458   return 0;
459 }
460 //_________________________________________________________________________
461 void AliTOFtracker::UnloadClusters() {
462   //--------------------------------------------------------------------
463   //This function unloads TOF clusters
464   //--------------------------------------------------------------------
465   for (Int_t i=0; i<fN; i++) delete fClusters[i];
466   fN=0;
467 }
468
469 //_________________________________________________________________________
470 Int_t AliTOFtracker::InsertCluster(AliTOFcluster *c) {
471   //--------------------------------------------------------------------
472   //This function adds a cluster to the array of clusters sorted in Z
473   //--------------------------------------------------------------------
474   if (fN==kMaxCluster) {
475     Error("InsertCluster","Too many clusters !\n");
476     return 1;
477   }
478
479   if (fN==0) {fClusters[fN++]=c; return 0;}
480   Int_t i=FindClusterIndex(c->GetZ());
481   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTOFcluster*));
482   fClusters[i]=c; fN++;
483
484   return 0;
485 }
486
487 //_________________________________________________________________________
488 Int_t AliTOFtracker::FindClusterIndex(Double_t z) const {
489   //--------------------------------------------------------------------
490   // This function returns the index of the nearest cluster 
491   //--------------------------------------------------------------------
492   if (fN==0) return 0;
493   if (z <= fClusters[0]->GetZ()) return 0;
494   if (z > fClusters[fN-1]->GetZ()) return fN;
495   Int_t b=0, e=fN-1, m=(b+e)/2;
496   for (; b<e; m=(b+e)/2) {
497     if (z > fClusters[m]->GetZ()) b=m+1;
498     else e=m; 
499   }
500   return m;
501 }
502