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