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