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