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