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