]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFtrackerMI.cxx
reducing macro to minimum AliReconstruction functionality, disable QA and TriggerESD...
[u/mrichter/AliRoot.git] / TOF / AliTOFtrackerMI.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 //-----------------------------------------------------------------//
17 //                                                                 //
18 //   AliTOFtracker Class                                           //
19 //   Task: Perform association of the ESD tracks to TOF Clusters   //
20 //   and Update ESD track with associated TOF Cluster parameters   //
21 //                                                                 //
22 //-----------------------------------------------------------------//
23
24 #include <Rtypes.h>
25
26 #include "TClonesArray.h"
27 //#include "TGeoManager.h"
28 #include "TTree.h"
29 #include "TTreeStream.h"
30
31 //#include "AliRun.h"
32 #include "AliESDEvent.h"
33 #include "AliESDtrack.h"
34
35 #include "AliTOFRecoParam.h"
36 #include "AliTOFcalib.h"
37 #include "AliTOFcluster.h"
38 #include "AliTOFGeometry.h"
39 #include "AliTOFtrackerMI.h"
40 #include "AliTOFtrack.h"
41 #include "AliTOFpidESD.h"
42
43 class TGeoManager;
44
45 extern TGeoManager *gGeoManager;
46
47 ClassImp(AliTOFtrackerMI)
48
49 //_____________________________________________________________________________
50 AliTOFtrackerMI::AliTOFtrackerMI():
51   fRecoParam(0x0),
52   fGeom(0x0),
53   fPid(0x0),
54   fN(0),
55   fNseeds(0),
56   fNseedsTOF(0),
57   fngoodmatch(0),
58   fnbadmatch(0),
59   fnunmatch(0),
60   fnmatch(0),
61   fR(379.), 
62   fTOFHeigth(15.3),  
63   fdCut(3.), 
64   fDx(1.5), 
65   fDy(0), 
66   fDz(0), 
67   fTracks(0x0),
68   fSeeds(0x0),
69   fDebugStreamer(0x0)
70  { 
71   //AliTOFtrackerMI main Ctor
72
73    fRecoParam=new AliTOFRecoParam();
74    fGeom=new AliTOFGeometry();
75    Double_t parPID[2];   
76    parPID[0]=fRecoParam->GetTimeResolution();
77    parPID[1]=fRecoParam->GetTimeNSigma();
78    fPid=new AliTOFpidESD(parPID);
79    fDy=AliTOFGeometry::XPad(); 
80    fDz=AliTOFGeometry::ZPad(); 
81    fDebugStreamer = new TTreeSRedirector("TOFdebug.root");   
82 }
83 //_____________________________________________________________________________
84 AliTOFtrackerMI::AliTOFtrackerMI(const AliTOFtrackerMI &t):
85   AliTracker(),
86   fRecoParam(0x0),
87   fGeom(0x0),
88   fPid(0x0),
89   fN(0),
90   fNseeds(0),
91   fNseedsTOF(0),
92   fngoodmatch(0),
93   fnbadmatch(0),
94   fnunmatch(0),
95   fnmatch(0),
96   fR(379.), 
97   fTOFHeigth(15.3),  
98   fdCut(3.), 
99   fDx(1.5), 
100   fDy(0), 
101   fDz(0), 
102   fTracks(0x0),
103   fSeeds(0x0),
104   fDebugStreamer(0x0)
105  { 
106   //AliTOFtrackerMI copy Ctor
107
108   fNseeds=t.fNseeds;
109   fNseedsTOF=t.fNseedsTOF;
110   fngoodmatch=t.fngoodmatch;
111   fnbadmatch=t.fnbadmatch;
112   fnunmatch=t.fnunmatch;
113   fnmatch=t.fnmatch;
114   fRecoParam = t.fRecoParam;
115   fGeom=t.fGeom;
116   fPid = t.fPid;
117   fR=t.fR; 
118   fTOFHeigth=t.fTOFHeigth;  
119   fdCut=t.fdCut; 
120   fDy=t.fDy; 
121   fDz=t.fDz; 
122   fDx=1.5; 
123   fSeeds=t.fSeeds;
124   fTracks=t.fTracks;
125   fN=t.fN;
126 }
127
128 //_________________________________________________________________________________
129 AliTOFtrackerMI& AliTOFtrackerMI::operator=(const AliTOFtrackerMI &t)
130
131   //AliTOFtrackerMI assignment operator
132
133   this->fNseeds=t.fNseeds;
134   this->fNseedsTOF=t.fNseedsTOF;
135   this->fngoodmatch=t.fngoodmatch;
136   this->fnbadmatch=t.fnbadmatch;
137   this->fnunmatch=t.fnunmatch;
138   this->fnmatch=t.fnmatch;
139   this->fRecoParam = t.fRecoParam;
140   this->fGeom = t.fGeom;
141   this->fPid = t.fPid;
142   this->fR=t.fR; 
143   this->fTOFHeigth=t.fTOFHeigth;  
144   this->fdCut=t.fdCut; 
145   this->fDy=t.fDy;
146   this->fDz=t.fDz;
147   this->fDx=t.fDx;
148   this->fSeeds=t.fSeeds;
149   this->fTracks=t.fTracks;
150   this->fN=t.fN;
151   return *this;
152
153 }
154
155 //_____________________________________________________________________________
156 AliTOFtrackerMI::~AliTOFtrackerMI(){
157   //
158   //
159   //
160   if (fDebugStreamer) {    
161     //fDebugStreamer->Close();
162     delete fDebugStreamer;
163   }
164   delete fRecoParam;
165   delete fGeom;
166   delete fPid;
167 }
168
169 //_____________________________________________________________________________
170 Int_t AliTOFtrackerMI::PropagateBack(AliESDEvent* event) {
171   //
172   // Gets seeds from ESD event and Match with TOF Clusters
173   //
174
175
176   //Initialise some counters
177
178   fNseeds=0;
179   fNseedsTOF=0;
180   fngoodmatch=0;
181   fnbadmatch=0;
182   fnunmatch=0;
183   fnmatch=0;
184
185   Int_t ntrk=event->GetNumberOfTracks();
186   fNseeds = ntrk;
187   fSeeds= new TClonesArray("AliESDtrack");
188   TClonesArray &aESDTrack = *fSeeds;
189
190
191   //Load ESD tracks into a local Array of ESD Seeds
192
193   for (Int_t i=0; i<fNseeds; i++) {
194     AliESDtrack *t=event->GetTrack(i);
195     new(aESDTrack[i]) AliESDtrack(*t);
196   }
197
198   //Prepare ESD tracks candidates for TOF Matching
199   CollectESD();
200
201   //First Step with Strict Matching Criterion
202   //MatchTracks(kFALSE);
203
204   //Second Step with Looser Matching Criterion
205   //MatchTracks(kTRUE);
206   MatchTracksMI(kFALSE);  // assign track to clusters
207   MatchTracksMI(kTRUE);   // assign clusters to esd
208   
209   Info("PropagateBack","Number of matched tracks: %d",fnmatch);
210   Info("PropagateBack","Number of good matched tracks: %d",fngoodmatch);
211   Info("PropagateBack","Number of bad  matched tracks: %d",fnbadmatch);
212
213   //Update the matched ESD tracks
214
215   for (Int_t i=0; i<ntrk; i++) {
216     AliESDtrack *t=event->GetTrack(i);
217     AliESDtrack *seed =(AliESDtrack*)fSeeds->UncheckedAt(i);
218     if(seed->GetTOFsignal()>0){
219       t->SetTOFsignal(seed->GetTOFsignal());
220       t->SetTOFcluster(seed->GetTOFcluster());
221       Int_t tlab[3];
222       seed->GetTOFLabel(tlab);    
223       t->SetTOFLabel(tlab);
224       AliTOFtrack *track = new AliTOFtrack(*seed);
225       Float_t info[10];
226       Double_t times[10];
227       seed->GetTOFInfo(info);
228       seed->GetIntegratedTimes(times);
229       t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
230       t->SetIntegratedLength(seed->GetIntegratedLength());
231       t->SetIntegratedTimes(times);
232       t->SetTOFsignalToT(seed->GetTOFsignalToT());
233       t->SetTOFCalChannel(seed->GetTOFCalChannel());
234       //
235       t->SetTOFInfo(info);
236       delete track;
237     }
238   }
239
240
241   //Make TOF PID
242   fPid->MakePID(event);
243
244   if (fSeeds) {
245     fSeeds->Delete();
246     delete fSeeds;
247     fSeeds = 0x0;
248   }
249   if (fTracks) {
250     fTracks->Delete();
251     delete fTracks;
252     fTracks = 0x0;
253   }
254   return 0;
255   
256 }
257 //_________________________________________________________________________
258 void AliTOFtrackerMI::CollectESD() {
259    //prepare the set of ESD tracks to be matched to clusters in TOF
260  
261   fTracks= new TClonesArray("AliTOFtrack");
262   TClonesArray &aTOFTrack = *fTracks;
263   Int_t c0=0;
264   Int_t c1=0;
265   for (Int_t i=0; i<fNseeds; i++) {
266
267     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(i);
268     if ((t->GetStatus()&AliESDtrack::kTPCout)==0)continue;
269
270     // TRD good tracks, already propagated at 372 cm
271
272     AliTOFtrack *track = new AliTOFtrack(*t); // New
273     Double_t x = track->GetX(); //New
274
275     if (((t->GetStatus()&AliESDtrack::kTRDout)!=0 ) && 
276          ( x >= AliTOFGeometry::RinTOF()) ){
277       track->SetSeedIndex(i);
278       t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
279       new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
280       fNseedsTOF++;
281       c0++;
282       delete track;
283     }
284
285     // Propagate the rest of TPCbp  
286
287     else {
288       if(track->PropagateToInnerTOF()){ // temporary solution
289         track->SetSeedIndex(i);
290         t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
291         new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
292         fNseedsTOF++;
293         c1++;
294       }
295       delete track;
296     }
297   }
298   //
299   //
300   printf("TRD\tOn\t%d\tOff\t%d\n",c0,c1);
301
302   // Sort according uncertainties on track position 
303   fTracks->Sort();
304
305 }
306
307
308
309
310
311
312
313 //
314 //
315 //_________________________________________________________________________
316 void AliTOFtrackerMI::MatchTracks( Bool_t /*mLastStep*/){
317   return;
318 }
319 //
320 //
321 //_________________________________________________________________________
322 void AliTOFtrackerMI::MatchTracksMI(Bool_t mLastStep){
323
324   //Match ESD tracks to clusters in TOF
325   const Float_t kTofOffset = 26;  // time offset
326   const Float_t kMinQuality        = -6.; // minimal quality
327   const Float_t kMaxQualityD       = 1.;  // max delta quality if cluster used
328   const Float_t kForbiddenR        = 0.1;  // minimal PID according TPC
329
330   static const Double_t kMasses[]={
331     0.000511, 0.105658, 0.139570, 0.493677, 0.938272, 1.875613
332   };
333   
334   Int_t nSteps=(Int_t)(fTOFHeigth/0.1);
335
336   AliTOFcalib *calib = new AliTOFcalib();
337
338   //PH Arrays (moved outside of the loop)
339   Float_t * trackPos[4];
340   for (Int_t ii=0; ii<4; ii++) trackPos[ii] = new Float_t[nSteps];
341   Int_t * clind[6];
342   for (Int_t ii=0;ii<6;ii++) clind[ii] = new Int_t[fN];
343
344   // Some init 
345   
346   Int_t         index[1000];
347   Float_t       dist3D[1000][6];
348   Double_t      times[1000][6];
349   Float_t       mintimedist[1000];
350   Float_t       likelihood[1000];
351   Float_t       length[1000];
352   AliTOFcluster *clusters[1000];
353   Double_t       tpcpid[5];
354   dist3D[0][0]=1;
355   
356   for (Int_t i=0; i<fNseedsTOF; i++) {
357
358     AliTOFtrack *track =(AliTOFtrack*)fTracks->UncheckedAt(i);
359     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(track->GetSeedIndex());
360     Bool_t hasTime = ( (t->GetStatus()& AliESDtrack::kTIME)>0) ? kTRUE:kFALSE;   // did we integrate time
361     Float_t trdquality = t->GetTRDQuality();
362     //
363     // Normalize tpc pid
364     //
365     t->GetTPCpid(tpcpid);
366     Double_t sumpid=0;
367     for (Int_t ipid=0;ipid<5;ipid++){
368       sumpid+=tpcpid[ipid];
369     }
370     for (Int_t ipid=0;ipid<5;ipid++){
371       if (sumpid>0) tpcpid[ipid]/=sumpid;
372       else{
373         tpcpid[ipid]=0.2;
374       }
375     }
376
377     if (trdquality<0) continue; // no chance 
378     //
379     AliTOFtrack *trackTOFin =new AliTOFtrack(*track);     
380     //
381     //propagat track to the middle of TOF
382     //
383     Float_t xs = 379.2;  // should be defined in the TOF geometry
384     Double_t ymax=xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());  
385     Bool_t skip=kFALSE;
386     Double_t ysect=trackTOFin->GetYat(xs,skip);
387     if (skip){
388       xs = 373.;
389       ymax=xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
390       ysect=trackTOFin->GetYat(xs,skip);
391     }
392     if (ysect > ymax) {
393       if (!trackTOFin->Rotate(AliTOFGeometry::GetAlpha())) {
394         continue;
395       }
396     } else if (ysect <-ymax) {
397       if (!trackTOFin->Rotate(-AliTOFGeometry::GetAlpha())) {
398         continue;
399       }
400     }    
401     if(!trackTOFin->PropagateTo(xs)) {
402       continue;
403     }
404     //
405     // Determine a window around the track
406     //
407     Double_t x,par[5]; 
408     trackTOFin->GetExternalParameters(x,par);
409     Double_t cov[15]; 
410     trackTOFin->GetExternalCovariance(cov);
411     Float_t scalefact=3.;    
412     Double_t dphi=
413       scalefact*
414       ((5*TMath::Sqrt(cov[0]) + 3.*fDy +10.*TMath::Abs(par[2]))/fR); 
415     Double_t dz=
416       scalefact*
417       (5*TMath::Sqrt(cov[2]) + 3.*fDz  +10.*TMath::Abs(par[3]));
418     
419     Double_t phi=TMath::ATan2(par[0],x) + trackTOFin->GetAlpha();
420     if (phi<-TMath::Pi())phi+=2*TMath::Pi();
421     if (phi>=TMath::Pi())phi-=2*TMath::Pi();
422     Double_t z=par[1];   
423
424     Int_t nc     =0;
425     Int_t nfound =0;
426     // find the clusters in the window of the track
427
428     for (Int_t k=FindClusterIndex(z-dz); k<fN; k++) {
429       AliTOFcluster *c=fClusters[k];
430       if (c->GetZ() > z+dz) break;
431       //      if (c->IsUsed()) continue;
432       
433       Double_t dph=TMath::Abs(c->GetPhi()-phi);
434       if (dph>TMath::Pi()) dph-=2.*TMath::Pi();
435       if (TMath::Abs(dph)>dphi) continue;
436     
437       clind[0][nc] = c->GetDetInd(0);
438       clind[1][nc] = c->GetDetInd(1);
439       clind[2][nc] = c->GetDetInd(2);
440       clind[3][nc] = c->GetDetInd(3);
441       clind[4][nc] = c->GetDetInd(4);
442       clind[5][nc] = k;      
443       nc++;
444     }
445
446     //
447     // select close clusters
448     //
449     Double_t mom=t->GetP();
450     //    Bool_t dump = kTRUE;
451     for (Int_t icl=0; icl<nc; icl++){
452       Float_t distances[5];
453       if (nfound>=1000) break;
454       index[nfound]=clind[5][icl];
455       AliTOFcluster *cluster = fClusters[clind[5][icl]];
456       GetLinearDistances(trackTOFin,cluster, distances);
457       dist3D[nfound][0] = distances[4];
458       dist3D[nfound][1] = distances[1];
459       dist3D[nfound][2] = distances[2];
460       // cut on distance
461       if (TMath::Abs(dist3D[nfound][1])>20 || TMath::Abs(dist3D[nfound][2])>20) continue;
462       //
463       GetLikelihood(distances[1],distances[2],cov,trackTOFin, dist3D[nfound][3], dist3D[nfound][4]);   
464       //
465       // cut on likelihood      
466       if (dist3D[nfound][3]*dist3D[nfound][4]<0.00000000000001) continue;  // log likelihood
467       if (TMath::Log(dist3D[nfound][3]*dist3D[nfound][4])<-9) continue;  // log likelihood
468       //
469       clusters[nfound] = cluster;
470       //
471       //length  and TOF updates 
472       trackTOFin->GetIntegratedTimes(times[nfound]);
473       length[nfound] = trackTOFin->GetIntegratedLength();
474       length[nfound]+=distances[4];
475       mintimedist[nfound]=1000; 
476       Double_t tof2=AliTOFGeometry::TdcBinWidth()*cluster->GetTDC()+kTofOffset; // in ps
477       // Float_t tgamma = TMath::Sqrt(cluster->GetR()*cluster->GetR()+cluster->GetZ()*cluster->GetZ())/0.03;  //time for "primary" gamma
478       //if (trackTOFin->GetPt()<0.7 && TMath::Abs(tgamma-tof2)<350) continue;  // gamma conversion candidate - TEMPORARY
479       for(Int_t j=0;j<=5;j++){
480         Double_t mass=kMasses[j];
481         times[nfound][j]+=distances[4]/3e-2*TMath::Sqrt(mom*mom+mass*mass)/mom;   // add time distance
482         if ( TMath::Abs(times[nfound][j]-tof2)<mintimedist[nfound] && tpcpid[j]>kForbiddenR){
483           mintimedist[nfound]=TMath::Abs(times[nfound][j]-tof2);
484         }
485       }
486       //
487       Float_t   liketime =  TMath::Exp(-mintimedist[nfound]/90.)+0.25*TMath::Exp(-mintimedist[nfound]/180.);
488       if (!hasTime)  liketime=0.2;
489       likelihood[nfound] = TMath::Log(dist3D[nfound][3]*dist3D[nfound][4]*liketime);
490       
491       if (TMath::Log(dist3D[nfound][3]*dist3D[nfound][4])<-1){
492         if (likelihood[nfound]<-9.) continue;
493       }
494       //
495       nfound++;
496     }   
497     if (nfound == 0 ) {
498       fnunmatch++;
499       delete trackTOFin;
500       continue;
501     } 
502     //
503     //choose the best cluster
504     //
505     Float_t quality[1000];
506     Int_t   index[1000];
507     //
508     AliTOFcluster * cgold=0;
509     Int_t igold =-1;
510     for (Int_t icl=0;icl<nfound;icl++){
511       quality[icl] = dist3D[icl][3]*dist3D[icl][4];
512     }
513     TMath::Sort(nfound,quality,index,kTRUE);
514     igold =  index[0];
515     cgold =  clusters[igold];
516     if (nfound>1 &&likelihood[index[1]]>likelihood[index[0]]){
517       if ( quality[index[0]]<quality[index[1]]+0.5){
518         igold = index[1];
519         cgold =  clusters[igold];
520       }
521     }
522     //
523     //
524     Float_t qualityGold = TMath::Log(0.0000001+(quality[igold]*(0.1+TMath::Exp(-mintimedist[igold]/80.))*(0.2+trdquality)));
525     if (!mLastStep){
526       if (cgold->GetQuality()<qualityGold) cgold->SetQuality(qualityGold);
527       continue;
528     }
529     //
530     if (mLastStep){
531       //signed better cluster
532       if (cgold->GetQuality()>qualityGold+kMaxQualityD) continue;
533       if (2.*qualityGold-cgold->GetQuality()<kMinQuality) continue;
534     }
535  
536     Int_t inonfake=-1;
537     
538     for (Int_t icl=0;icl<nfound;icl++){
539       if (
540           (clusters[index[icl]]->GetLabel(0)==TMath::Abs(trackTOFin->GetLabel()))
541           ||
542           (clusters[index[icl]]->GetLabel(1)==TMath::Abs(trackTOFin->GetLabel()))
543           ||
544           (clusters[index[icl]]->GetLabel(2)==TMath::Abs(trackTOFin->GetLabel()))
545           ) {
546         inonfake = icl;
547         break;
548       }
549     }    
550     fnmatch++;;
551     if (inonfake==0) fngoodmatch++;
552     else{
553       fnbadmatch++;
554     }
555
556     Int_t tlab[3];
557     tlab[0]=cgold->GetLabel(0);
558     tlab[1]=cgold->GetLabel(1);
559     tlab[2]=cgold->GetLabel(2);
560     //    Double_t tof2=25.*cgold->GetTDC()-350; // in ps
561     Double_t tof2=AliTOFGeometry::TdcBinWidth()*cgold->GetTDC()+kTofOffset; // in ps
562     Float_t tgamma = TMath::Sqrt(cgold->GetR()*cgold->GetR()+cgold->GetZ()*cgold->GetZ())/0.03;
563     Float_t info[11]={dist3D[igold][0],dist3D[igold][1],dist3D[igold][2],dist3D[igold][3],dist3D[igold][4],mintimedist[igold],
564                       -1,tgamma, qualityGold,cgold->GetQuality(),0};
565     //    GetLinearDistances(trackTOFin,cgold,&info[6]);
566     if (inonfake>=0){
567       info[6] = inonfake;
568       //      info[7] = mintimedist[index[inonfake]];
569     }
570     //
571     //  Store quantities to be used for TOF Calibration
572     Float_t tToT=cgold->GetToT(); // in ps
573     t->SetTOFsignalToT(tToT);
574     Int_t ind[5];
575     ind[0]=cgold->GetDetInd(0);
576     ind[1]=cgold->GetDetInd(1);
577     ind[2]=cgold->GetDetInd(2);
578     ind[3]=cgold->GetDetInd(3);
579     ind[4]=cgold->GetDetInd(4);
580     Int_t calindex = AliTOFGeometry::GetIndex(ind);
581     t->SetTOFCalChannel(calindex);
582
583     t->SetTOFInfo(info);
584     t->SetTOFsignal(tof2);
585     t->SetTOFcluster(cgold->GetIndex());  
586     AliTOFtrack *trackTOFout = new AliTOFtrack(*t); 
587     trackTOFout->PropagateTo(379.);
588     t->UpdateTrackParams(trackTOFout,AliESDtrack::kTOFout);    
589     t->SetIntegratedLength(length[igold]);
590     t->SetIntegratedTimes(times[igold]);
591     t->SetTOFLabel(tlab);
592     //
593     delete trackTOFin;
594     delete trackTOFout;
595     //
596   }
597   //
598   //
599   //
600   for (Int_t ii=0; ii<4; ii++) delete [] trackPos[ii];
601   for (Int_t ii=0;ii<6;ii++) delete [] clind[ii];
602   delete calib;
603 }
604
605
606
607
608 // //_________________________________________________________________________
609 // Int_t AliTOFtrackerMI::LoadClusters(TTree *dTree) {
610 //   //--------------------------------------------------------------------
611 //   //This function loads the TOF clusters
612 //   //--------------------------------------------------------------------
613
614 //   TBranch *branch=dTree->GetBranch("TOF");
615 //   if (!branch) { 
616 //     AliError(" can't get the branch with the TOF digits !");
617 //     return 1;
618 //   }
619
620 //   TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;
621 //   branch->SetAddress(&digits);
622
623 //   dTree->GetEvent(0);
624 //   Int_t nd=digits->GetEntriesFast();
625 //   Info("LoadClusters","number of digits: %d",nd);
626
627 //   for (Int_t i=0; i<nd; i++) {
628 //     AliTOFdigit *d=(AliTOFdigit*)digits->UncheckedAt(i);
629 //     Int_t dig[5]; Float_t g[3];
630 //     dig[0]=d->GetSector();
631 //     dig[1]=d->GetPlate();
632 //     dig[2]=d->GetStrip();
633 //     dig[3]=d->GetPadz();
634 //     dig[4]=d->GetPadx();
635
636 //     fGeom->GetPos(dig,g);
637
638 //     Double_t h[5];
639 //     h[0]=TMath::Sqrt(g[0]*g[0]+g[1]*g[1]);
640 //     h[1]=TMath::ATan2(g[1],g[0]); h[2]=g[2]; 
641 //     h[3]=d->GetTdc(); h[4]=d->GetAdc();
642
643 //     AliTOFcluster *cl=new AliTOFcluster(h,d->GetTracks(),dig,i);
644 //     InsertCluster(cl);
645 //   }  
646
647 //   return 0;
648 // }
649 // //_________________________________________________________________________
650 // void AliTOFtrackerMI::UnloadClusters() {
651 //   //--------------------------------------------------------------------
652 //   //This function unloads TOF clusters
653 //   //--------------------------------------------------------------------
654 //   for (Int_t i=0; i<fN; i++) delete fClusters[i];
655 //   fN=0;
656 // }
657
658
659
660 //_________________________________________________________________________
661 Int_t AliTOFtrackerMI::LoadClusters(TTree *cTree) {
662   //--------------------------------------------------------------------
663   //This function loads the TOF clusters
664   //--------------------------------------------------------------------
665
666   TBranch *branch=cTree->GetBranch("TOF");
667   if (!branch) { 
668     AliError("can't get the branch with the TOF clusters !");
669     return 1;
670   }
671
672   TClonesArray dummy("AliTOFcluster",10000), *clusters=&dummy;
673   branch->SetAddress(&clusters);
674
675   cTree->GetEvent(0);
676   Int_t nc=clusters->GetEntriesFast();
677   AliInfo(Form("Number of clusters: %d",nc));
678
679   for (Int_t i=0; i<nc; i++) {
680     AliTOFcluster *c=(AliTOFcluster*)clusters->UncheckedAt(i);
681     /*
682     for (Int_t jj=0; jj<5; jj++) dig[jj]=c->GetDetInd(jj);
683
684     Double_t h[5];
685     h[0]=c->GetR();
686     h[1]=c->GetPhi();
687     h[2]=c->GetZ();
688     h[3]=c->GetTDC();
689     h[4]=c->GetADC();
690
691     Int_t indexDig[3];
692     for (Int_t jj=0; jj<3; jj++) indexDig[jj] = c->GetLabel(jj);
693
694     AliTOFcluster *cl=new AliTOFcluster(h,c->GetTracks(),dig,i);
695     */
696
697     //    fClusters[i]=c; fN++;
698     fClusters[i]=new AliTOFcluster(*c); fN++;
699
700     //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)));
701     //AliInfo(Form("%i %f",i, fClusters[i]->GetZ()));
702   }
703
704   //AliInfo(Form("Number of clusters: %d",fN));
705
706   return 0;
707 }
708 //_________________________________________________________________________
709 void AliTOFtrackerMI::UnloadClusters() {
710   //--------------------------------------------------------------------
711   //This function unloads TOF clusters
712   //--------------------------------------------------------------------
713   for (Int_t i=0; i<fN; i++) {
714     delete fClusters[i];
715     fClusters[i] = 0x0;
716   }
717   fN=0;
718 }
719
720
721
722
723 //_________________________________________________________________________
724 Int_t AliTOFtrackerMI::InsertCluster(AliTOFcluster *c) {
725   //--------------------------------------------------------------------
726   //This function adds a cluster to the array of clusters sorted in Z
727   //--------------------------------------------------------------------
728   if (fN==kMaxCluster) {
729     AliError("Too many clusters !");
730     return 1;
731   }
732
733   if (fN==0) {fClusters[fN++]=c; return 0;}
734   Int_t i=FindClusterIndex(c->GetZ());
735   memmove(fClusters+i+1 ,fClusters+i,(fN-i)*sizeof(AliTOFcluster*));
736   fClusters[i]=c; fN++;
737
738   return 0;
739 }
740
741 //_________________________________________________________________________
742 Int_t AliTOFtrackerMI::FindClusterIndex(Double_t z) const {
743   //--------------------------------------------------------------------
744   // This function returns the index of the nearest cluster 
745   //--------------------------------------------------------------------
746   if (fN==0) return 0;
747   if (z <= fClusters[0]->GetZ()) return 0;
748   if (z > fClusters[fN-1]->GetZ()) return fN;
749   Int_t b=0, e=fN-1, m=(b+e)/2;
750   for (; b<e; m=(b+e)/2) {
751     if (z > fClusters[m]->GetZ()) b=m+1;
752     else e=m; 
753   }
754   return m;
755 }
756
757
758
759 Float_t AliTOFtrackerMI::GetLinearDistances(AliTOFtrack * track, AliTOFcluster *cluster, Float_t distances[5])
760 {
761   //
762   // calclates distance between cluster and track
763   // use linear aproximation
764   //
765   const Float_t kRaddeg = 180/3.14159265358979312;
766   //
767   //  Float_t tiltangle  = fGeom->GetAngles(cluster->fdetIndex[1],cluster->fdetIndex[2])/kRaddeg;  //tiltangle  
768   Int_t cind[5];
769   cind[0]= cluster->GetDetInd(0);
770   cind[1]= cluster->GetDetInd(1);
771   cind[2]= cluster->GetDetInd(2);
772   cind[3]= cluster->GetDetInd(3);
773   cind[4]= cluster->GetDetInd(4);
774   Float_t tiltangle  = AliTOFGeometry::GetAngles(cluster->GetDetInd(1),cluster->GetDetInd(2))/kRaddeg;  //tiltangle  
775
776   Float_t cpos[3];  //cluster position
777   Float_t cpos0[3];  //cluster position
778   //  fGeom->GetPos(cluster->fdetIndex,cpos);  
779   //fGeom->GetPos(cluster->fdetIndex,cpos0);  
780   //
781   fGeom->GetPos(cind,cpos);  
782   fGeom->GetPos(cind,cpos0);  
783
784   Float_t phi = TMath::ATan2(cpos[1],cpos[0]);  
785   if(phi<0) phi=2.*TMath::Pi()+phi;
786   //  Get the local angle in the sector philoc
787   Float_t phiangle   = (Int_t (phi*kRaddeg/20.) + 0.5)*20./kRaddeg;
788   //
789   Double_t v0[3];
790   Double_t dir[3];
791   track->GetXYZ(v0);
792   track->GetPxPyPz(dir);
793   dir[0]/=track->GetP();
794   dir[1]/=track->GetP();
795   dir[2]/=track->GetP();
796   //
797   //
798   //rotate 0
799   Float_t sinphi = TMath::Sin(phiangle);
800   Float_t cosphi = TMath::Cos(phiangle);
801   Float_t sinth  = TMath::Sin(tiltangle);
802   Float_t costh  = TMath::Cos(tiltangle);
803   //
804   Float_t temp;
805   temp    =  cpos[0]*cosphi+cpos[1]*sinphi;
806   cpos[1] = -cpos[0]*sinphi+cpos[1]*cosphi;
807   cpos[0] = temp;
808   temp    =  v0[0]*cosphi+v0[1]*sinphi;
809   v0[1] = -v0[0]*sinphi+v0[1]*cosphi;
810   v0[0] = temp;
811   //  
812   temp    =  cpos[0]*costh+cpos[2]*sinth;
813   cpos[2] = -cpos[0]*sinth+cpos[2]*costh;
814   cpos[0] = temp;
815   temp    =  v0[0]*costh+v0[2]*sinth;
816   v0[2]   = -v0[0]*sinth+v0[2]*costh;
817   v0[0]   = temp;
818   //
819   //
820   //rotate direction vector
821   //
822   temp    =  dir[0]*cosphi+dir[1]*sinphi;
823   dir[1] = -dir[0]*sinphi+dir[1]*cosphi;
824   dir[0] = temp;
825   //
826   temp    =  dir[0]*costh+dir[2]*sinth;
827   dir[2]  = -dir[0]*sinth+dir[2]*costh;
828   dir[0]  = temp;
829   //
830   Float_t v3[3];
831   Float_t k = (cpos[0]-v0[0])/dir[0];
832   v3[0] = v0[0]+k*dir[0];
833   v3[1] = v0[1]+k*dir[1];
834   v3[2] = v0[2]+k*dir[2];
835   //
836   distances[0] = v3[0]-cpos[0];
837   distances[1] = v3[1]-cpos[1];
838   distances[2] = v3[2]-cpos[2];
839   distances[3] = TMath::Sqrt( distances[0]*distances[0]+distances[1]*distances[1]+distances[2]*distances[2]); //distance
840   distances[4] = k;  //length
841
842   //
843   // Debuging part of the matching
844   //
845   if (track->GetLabel()==cluster->GetLabel(0) ||track->GetLabel()==cluster->GetLabel(1) ){
846     TTreeSRedirector& cstream = *fDebugStreamer;
847     Float_t tdc = cluster->GetTDC();
848     cstream<<"Tracks"<<
849       "TOF.="<<track<<
850       "Cx="<<cpos0[0]<<
851       "Cy="<<cpos0[1]<<
852       "Cz="<<cpos0[2]<<
853       "Dist="<<k<<
854       "Dist0="<<distances[0]<<
855       "Dist1="<<distances[1]<<
856       "Dist2="<<distances[2]<<
857       "TDC="<<tdc<<
858       "\n";
859   }
860   return distances[3];
861 }
862
863
864
865 void    AliTOFtrackerMI::GetLikelihood(Float_t dy, Float_t dz, const Double_t *cov, AliTOFtrack * /*track*/, Float_t & py, Float_t &pz)
866 {
867   //
868   //  get likelihood - track covariance taken
869   //  75 % of gauss with expected sigma
870   //  25 % of gauss with extended sigma
871   
872   Double_t kMaxSigmaY  = 0.6;  // ~ 90% of TRD tracks  
873   Double_t kMaxSigmaZ  = 1.2;  // ~ 90% of TRD tracks 
874   Double_t kMeanSigmaY = 0.25; // mean TRD sigma  
875   Double_t kMeanSigmaZ = 0.5;  // mean TRD sigma 
876
877   
878   Float_t normwidth, normd, p0,p1;  
879   Float_t sigmay = TMath::Max(TMath::Sqrt(cov[0]+kMeanSigmaY*kMeanSigmaY),kMaxSigmaY);
880   Float_t sigmaz = TMath::Max(TMath::Sqrt(cov[2]+kMeanSigmaZ*kMeanSigmaZ),kMaxSigmaZ);
881
882   py=0;
883   pz=0;  
884   // 
885   // py calculation   - 75% admixture of original sigma - 25% tails 
886   //
887   normwidth = fDy/sigmay;
888   normd     = dy/sigmay;
889   p0 = 0.5*(1+TMath::Erf(normd-normwidth*0.5));
890   p1 = 0.5*(1+TMath::Erf(normd+normwidth*0.5));  
891   py+= 0.75*(p1-p0);
892   //
893   normwidth = fDy/(3.*sigmay);
894   normd     = dy/(3.*sigmay);
895   p0 = 0.5*(1+TMath::Erf(normd-normwidth*0.5));
896   p1 = 0.5*(1+TMath::Erf(normd+normwidth*0.5));  
897   py+= 0.25*(p1-p0);
898   // 
899   // pz calculation   - 75% admixture of original sigma - 25% tails 
900   //
901   normwidth = fDz/sigmaz;
902   normd     = dz/sigmaz;
903   p0 = 0.5*(1+TMath::Erf(normd-normwidth*0.5));
904   p1 = 0.5*(1+TMath::Erf(normd+normwidth*0.5));  
905   pz+= 0.75*(p1-p0);
906   //
907   normwidth = fDz/(3.*sigmaz);
908   normd     = dz/(3.*sigmaz);
909   p0 = 0.5*(1+TMath::Erf(normd-normwidth*0.5));
910   p1 = 0.5*(1+TMath::Erf(normd+normwidth*0.5));  
911   pz+= 0.25*(p1-p0);
912 }