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