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