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