]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFtracker.cxx
bug fixed
[u/mrichter/AliRoot.git] / TOF / AliTOFtracker.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 // -- Authors : S. Arcelli, C. Zampolli (Bologna University and INFN) //
23 // -- Contacts: Annalisa.De.Caro@cern.ch                              //
24 // --         : Chiara.Zampolli@bo.infn.it                            //
25 // --         : Silvia.Arcelli@bo.infn.it                             //
26 //                                                                    //
27 //--------------------------------------------------------------------//
28
29 #include <Rtypes.h>
30 #include <TROOT.h>
31
32 #include <TSeqCollection.h>
33 #include <TClonesArray.h>
34 #include <TGeoManager.h>
35 #include <TTree.h>
36 #include <TFile.h>
37 #include <TH2F.h>
38
39 #include "AliGeomManager.h"
40 #include "AliESDtrack.h"
41 #include "AliESDEvent.h"
42 #include "AliLog.h"
43 #include "AliTrackPointArray.h"
44 #include "AliCDBManager.h"
45
46 #include "AliTOFpidESD.h"
47 #include "AliTOFRecoParam.h"
48 #include "AliTOFReconstructor.h"
49 #include "AliTOFcluster.h"
50 #include "AliTOFGeometry.h"
51 #include "AliTOFtracker.h"
52 #include "AliTOFtrack.h"
53
54 extern TGeoManager *gGeoManager;
55 extern TROOT *gROOT;
56
57 ClassImp(AliTOFtracker)
58
59 //_____________________________________________________________________________
60 AliTOFtracker::AliTOFtracker():
61   fRecoParam(0x0),
62   fGeom(0x0),
63   fPid(0x0),
64   fN(0),
65   fNseeds(0),
66   fNseedsTOF(0),
67   fngoodmatch(0),
68   fnbadmatch(0),
69   fnunmatch(0),
70   fnmatch(0),
71   fTracks(new TClonesArray("AliTOFtrack")),
72   fSeeds(new TClonesArray("AliESDtrack")),
73   fHDigClusMap(0x0),
74   fHDigNClus(0x0),
75   fHDigClusTime(0x0),
76   fHDigClusToT(0x0),
77   fHRecNClus(0x0),
78   fHRecDist(0x0),
79   fHRecSigYVsP(0x0),
80   fHRecSigZVsP(0x0),
81   fHRecSigYVsPWin(0x0),
82   fHRecSigZVsPWin(0x0),
83   fCalTree(0x0),
84   fIch(-1),
85   fToT(-1.),
86   fTime(-1.),
87   fExpTimePi(-1.),
88   fExpTimeKa(-1.),
89   fExpTimePr(-1.)
90  { 
91   //AliTOFtracker main Ctor
92    
93    // Gettimg the geometry 
94    fGeom= new AliTOFGeometry();
95
96    InitCheckHists();
97
98 }
99 //_____________________________________________________________________________
100 AliTOFtracker::~AliTOFtracker() {
101   //
102   // Dtor
103   //
104
105   SaveCheckHists();
106
107   if(!(AliCDBManager::Instance()->GetCacheFlag())){
108     delete fRecoParam;
109   }
110   delete fGeom; 
111   delete fPid; 
112   delete fHDigClusMap;
113   delete fHDigNClus;
114   delete fHDigClusTime;
115   delete fHDigClusToT;
116   delete fHRecNClus;
117   delete fHRecDist;
118   delete fHRecSigYVsP;
119   delete fHRecSigZVsP;
120   delete fHRecSigYVsPWin;
121   delete fHRecSigZVsPWin;
122   delete fCalTree;
123   if (fTracks){
124     fTracks->Delete();
125     delete fTracks;
126     fTracks=0x0;
127   }
128   if (fSeeds){
129     fSeeds->Delete();
130     delete fSeeds;
131     fSeeds=0x0;
132   }
133
134 }
135 //_____________________________________________________________________________
136 Int_t AliTOFtracker::PropagateBack(AliESDEvent* event) {
137   //
138   // Gets seeds from ESD event and Match with TOF Clusters
139   //
140
141   // initialize RecoParam for current event
142
143   AliInfo("Initializing params for TOF... ");
144
145   fRecoParam = AliTOFReconstructor::GetRecoParam();  // instantiate reco param from STEER...
146
147   if (fRecoParam == 0x0) { 
148     AliFatal("No Reco Param found for TOF!!!");
149   }
150   //fRecoParam->Dump();
151   //if(fRecoParam->GetApplyPbPbCuts())fRecoParam=fRecoParam->GetPbPbparam();
152   //fRecoParam->PrintParameters();
153
154   Double_t parPID[2];   
155   parPID[0]=fRecoParam->GetTimeResolution();
156   parPID[1]=fRecoParam->GetTimeNSigma();
157   fPid=new AliTOFpidESD(parPID);
158
159   //Initialise some counters
160
161   fNseeds=0;
162   fNseedsTOF=0;
163   fngoodmatch=0;
164   fnbadmatch=0;
165   fnunmatch=0;
166   fnmatch=0;
167
168   Int_t ntrk=event->GetNumberOfTracks();
169   fNseeds = ntrk;
170   TClonesArray &aESDTrack = *fSeeds;
171
172
173   //Load ESD tracks into a local Array of ESD Seeds
174
175   for (Int_t i=0; i<fNseeds; i++) {
176     AliESDtrack *t=event->GetTrack(i);
177     new(aESDTrack[i]) AliESDtrack(*t);
178   }
179
180   //Prepare ESD tracks candidates for TOF Matching
181   CollectESD();
182
183   //First Step with Strict Matching Criterion
184   MatchTracks(kFALSE);
185
186   //Second Step with Looser Matching Criterion
187   MatchTracks(kTRUE);
188
189   AliInfo(Form("Number of matched tracks: %d",fnmatch));
190   AliInfo(Form("Number of good matched tracks: %d",fngoodmatch));
191   AliInfo(Form("Number of bad  matched tracks: %d",fnbadmatch));
192
193   //Update the matched ESD tracks
194
195   for (Int_t i=0; i<ntrk; i++) {
196     AliESDtrack *t=event->GetTrack(i);
197     AliESDtrack *seed =(AliESDtrack*)fSeeds->UncheckedAt(i);
198     if(seed->GetTOFsignal()>0){
199       t->SetTOFsignal(seed->GetTOFsignal());
200       t->SetTOFcluster(seed->GetTOFcluster());
201       t->SetTOFsignalToT(seed->GetTOFsignalToT());
202       t->SetTOFsignalRaw(seed->GetTOFsignalRaw());
203       t->SetTOFsignalDz(seed->GetTOFsignalDz());
204       t->SetTOFCalChannel(seed->GetTOFCalChannel());
205       Int_t tlab[3]; seed->GetTOFLabel(tlab);    
206       t->SetTOFLabel(tlab);
207       AliTOFtrack *track = new AliTOFtrack(*seed); 
208       t->UpdateTrackParams(track,AliESDtrack::kTOFout);   
209       delete track;
210     }
211   }
212
213   //Handle Time Zero information
214
215   Double_t timeZero=0.;
216   Double_t timeZeroMax=99999.;
217   Bool_t usetimeZero     = fRecoParam->UseTimeZero();
218   Bool_t timeZeroFromT0  = fRecoParam->GetTimeZerofromT0();
219   Bool_t timeZeroFromTOF = fRecoParam->GetTimeZerofromTOF();
220
221   AliDebug(1,Form("Use Time Zero?: %d",usetimeZero));
222   AliDebug(1,Form("Time Zero from T0? : %d",timeZeroFromT0));
223   AliDebug(1,Form("Time Zero From TOF? : %d",timeZeroFromTOF));
224
225   if(usetimeZero){
226     if(timeZeroFromT0){
227       timeZero=GetTimeZerofromT0(event); 
228     }
229     if(timeZeroFromTOF && (timeZero>timeZeroMax || !timeZeroFromT0)){
230       timeZero=GetTimeZerofromTOF(event); 
231     }
232   }
233   AliDebug(2,Form("time Zero used in PID: %f",timeZero));
234   //Make TOF PID
235   fPid->MakePID(event,timeZero);
236
237   fSeeds->Clear();
238   fTracks->Clear();
239   return 0;
240   
241 }
242 //_________________________________________________________________________
243 void AliTOFtracker::CollectESD() {
244    //prepare the set of ESD tracks to be matched to clusters in TOF
245
246   Int_t seedsTOF1=0;
247   Int_t seedsTOF2=0;
248  
249   TClonesArray &aTOFTrack = *fTracks;
250   for (Int_t i=0; i<fNseeds; i++) {
251
252     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(i);
253     if ((t->GetStatus()&AliESDtrack::kTPCout)==0)continue;
254
255     // TRD 'good' tracks, already propagated at 372 cm
256
257     AliTOFtrack *track = new AliTOFtrack(*t); // New
258     Double_t x = track->GetX(); //New
259     if (((t->GetStatus()&AliESDtrack::kTRDout)!=0 ) && 
260          ( x >= AliTOFGeometry::RinTOF()) ){
261       track->SetSeedIndex(i);
262       t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
263       new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
264       fNseedsTOF++;
265       seedsTOF1++;
266       delete track;
267     }
268
269     // Propagate the rest of TPCbp  
270
271     else {
272       if(track->PropagateToInnerTOF()){ 
273         track->SetSeedIndex(i);
274         t->UpdateTrackParams(track,AliESDtrack::kTOFout);    
275         new(aTOFTrack[fNseedsTOF]) AliTOFtrack(*track);
276         fNseedsTOF++;
277         seedsTOF2++;
278       }
279       delete track;
280     }
281   }
282
283   AliInfo(Form("Number of TOF seeds %i",fNseedsTOF));
284   AliInfo(Form("Number of TOF seeds Type 1 %i",seedsTOF1));
285   AliInfo(Form("Number of TOF seeds Type 2 %i",seedsTOF2));
286
287   // Sort according uncertainties on track position 
288   fTracks->Sort();
289
290 }
291 //_________________________________________________________________________
292 void AliTOFtracker::MatchTracks( Bool_t mLastStep){
293
294
295   // Parameters used/regulating the reconstruction
296
297   static Float_t corrLen=0.75;
298   static Float_t detDepth=15.3;
299   static Float_t padDepth=0.5;
300
301   Float_t dY=AliTOFGeometry::XPad(); 
302   Float_t dZ=AliTOFGeometry::ZPad(); 
303
304   Float_t sensRadius = fRecoParam->GetSensRadius();
305   Float_t stepSize   = fRecoParam->GetStepSize();
306   Float_t scaleFact   = fRecoParam->GetWindowScaleFact();
307   Float_t dyMax=fRecoParam->GetWindowSizeMaxY(); 
308   Float_t dzMax=fRecoParam->GetWindowSizeMaxZ();
309   Float_t dCut=fRecoParam->GetDistanceCut();
310   Double_t maxChi2=fRecoParam->GetMaxChi2TRD();
311   Bool_t timeWalkCorr    = fRecoParam->GetTimeWalkCorr();
312   if(!mLastStep){
313     AliDebug(1,"++++++++++++++TOF Reconstruction Parameters:++++++++++++ \n");
314     AliDebug(1,Form("TOF sens radius: %f",sensRadius));
315     AliDebug(1,Form("TOF step size: %f",stepSize));
316     AliDebug(1,Form("TOF Window scale factor: %f",scaleFact));
317     AliDebug(1,Form("TOF Window max dy: %f",dyMax));
318     AliDebug(1,Form("TOF Window max dz: %f",dzMax));
319     AliDebug(1,Form("TOF distance Cut: %f",dCut));
320     AliDebug(1,Form("TOF Max Chi2: %f",maxChi2));
321     AliDebug(1,Form("Time Walk Correction? : %d",timeWalkCorr));   
322   }
323   //Match ESD tracks to clusters in TOF
324
325
326   // Get the number of propagation steps
327
328   Int_t nSteps=(Int_t)(detDepth/stepSize);
329
330   //PH Arrays (moved outside of the loop)
331
332   Float_t * trackPos[4];
333   for (Int_t ii=0; ii<4; ii++) trackPos[ii] = new Float_t[nSteps];
334   Int_t * clind = new Int_t[fN];
335   
336   for (Int_t iseed=0; iseed<fNseedsTOF; iseed++) {
337
338     AliTOFtrack *track =(AliTOFtrack*)fTracks->UncheckedAt(iseed);
339     AliESDtrack *t =(AliESDtrack*)fSeeds->UncheckedAt(track->GetSeedIndex());
340     if(t->GetTOFsignal()>0. ) continue;
341     AliTOFtrack *trackTOFin =new AliTOFtrack(*track);
342
343     // Some init
344
345     Int_t         index[10000];
346     Float_t        dist[10000];
347     Float_t       distZ[10000];
348     Float_t       cxpos[10000];
349     Float_t       crecL[10000];
350     TGeoHMatrix   global[1000];
351      
352     // Determine a window around the track
353
354     Double_t x,par[5]; 
355     trackTOFin->GetExternalParameters(x,par);
356     Double_t cov[15]; 
357     trackTOFin->GetExternalCovariance(cov);
358
359     Double_t dphi=
360       scaleFact*
361       ((5*TMath::Sqrt(cov[0]) + 0.5*dY + 2.5*TMath::Abs(par[2]))/sensRadius); 
362     Double_t dz=
363        scaleFact*
364        (5*TMath::Sqrt(cov[2]) + 0.5*dZ + 2.5*TMath::Abs(par[3]));
365
366     Double_t phi=TMath::ATan2(par[0],x) + trackTOFin->GetAlpha();
367     if (phi<-TMath::Pi())phi+=2*TMath::Pi();
368     if (phi>=TMath::Pi())phi-=2*TMath::Pi();
369     Double_t z=par[1];   
370
371     //upper limit on window's size.
372
373     if(dz> dzMax) dz=dzMax;
374     if(dphi*sensRadius> dyMax) dphi=dyMax/sensRadius;
375
376
377     Int_t nc=0;
378
379     // find the clusters in the window of the track
380
381     for (Int_t k=FindClusterIndex(z-dz); k<fN; k++) {
382
383       AliTOFcluster *c=fClusters[k];
384       if (c->GetZ() > z+dz) break;
385       if (c->IsUsed()) continue;
386
387       if (!c->GetStatus()) {
388               AliDebug(1,"Cluster in channel declared bad!");
389               continue; // skip bad channels as declared in OCDB
390       }
391
392       Double_t dph=TMath::Abs(c->GetPhi()-phi);
393       if (dph>TMath::Pi()) dph-=2.*TMath::Pi();
394       if (TMath::Abs(dph)>dphi) continue;
395
396       Double_t yc=(c->GetPhi() - trackTOFin->GetAlpha())*c->GetR();
397       Double_t p[2]={yc, c->GetZ()};
398       Double_t cov2[3]= {dY*dY/12., 0., dZ*dZ/12.};
399       if (trackTOFin->AliExternalTrackParam::GetPredictedChi2(p,cov2) > maxChi2)continue;
400
401       clind[nc] = k;      
402       Char_t path[100];
403       Int_t ind[5];
404       ind[0]=c->GetDetInd(0);
405       ind[1]=c->GetDetInd(1);
406       ind[2]=c->GetDetInd(2);
407       ind[3]=c->GetDetInd(3);
408       ind[4]=c->GetDetInd(4);
409       fGeom->GetVolumePath(ind,path);
410       gGeoManager->cd(path);
411       global[nc] = *gGeoManager->GetCurrentMatrix();
412       nc++;
413     }
414
415     //start fine propagation 
416
417     Int_t nStepsDone = 0;
418     for( Int_t istep=0; istep<nSteps; istep++){ 
419
420       Float_t xs=AliTOFGeometry::RinTOF()+istep*0.1;
421       Double_t ymax=xs*TMath::Tan(0.5*AliTOFGeometry::GetAlpha());
422
423       Bool_t skip=kFALSE;
424       Double_t ysect=trackTOFin->GetYat(xs,skip);
425       if (skip) break;
426       if (ysect > ymax) {
427         if (!trackTOFin->Rotate(AliTOFGeometry::GetAlpha())) {
428           break;
429         }
430       } else if (ysect <-ymax) {
431         if (!trackTOFin->Rotate(AliTOFGeometry::GetAlpha())) {
432           break;
433         }
434       }
435
436       if(!trackTOFin->PropagateTo(xs)) {
437         break;
438       }
439
440       nStepsDone++;
441
442       // store the running point (Globalrf) - fine propagation     
443
444       Double_t r[3];
445       trackTOFin->GetXYZ(r);
446       trackPos[0][istep]= (Float_t) r[0];
447       trackPos[1][istep]= (Float_t) r[1];
448       trackPos[2][istep]= (Float_t) r[2];   
449       trackPos[3][istep]= trackTOFin->GetIntegratedLength();
450     }
451
452
453     Int_t nfound = 0;
454     Bool_t accept = kFALSE;
455     Bool_t isInside =kFALSE;
456     for (Int_t istep=0; istep<nStepsDone; istep++) {
457
458       Float_t ctrackPos[3];     
459
460       ctrackPos[0]= trackPos[0][istep];
461       ctrackPos[1]= trackPos[1][istep];
462       ctrackPos[2]= trackPos[2][istep];
463
464       //now see whether the track matches any of the TOF clusters            
465
466       Float_t dist3d[3];
467       accept=kFALSE;
468       for (Int_t i=0; i<nc; i++){
469         isInside=fGeom->IsInsideThePad(global[i],ctrackPos,dist3d);
470
471         if( mLastStep){
472           Float_t xLoc=dist3d[0];
473           Float_t rLoc=TMath::Sqrt(dist3d[1]*dist3d[1]+dist3d[2]*dist3d[2]);
474           accept = (TMath::Abs(xLoc)<padDepth*0.5 && rLoc<dCut);
475         }
476         else{
477           accept = isInside;
478         }
479         if(accept){
480           dist[nfound]=TMath::Sqrt(dist3d[0]*dist3d[0]+dist3d[1]*dist3d[1]+dist3d[2]*dist3d[2]);
481           distZ[nfound]=dist3d[2];
482           crecL[nfound]=trackPos[3][istep];
483           index[nfound]=clind[i]; // store cluster id       
484           cxpos[nfound]=AliTOFGeometry::RinTOF()+istep*0.1; //store prop.radius
485           nfound++;
486           if(accept &&!mLastStep)break;
487         }//end if accept
488       } //end for on the clusters
489       if(accept &&!mLastStep)break;
490     } //end for on the steps     
491
492
493
494     if (nfound == 0 ) {
495       fnunmatch++;
496       delete trackTOFin;
497       continue;
498     }
499     
500     fnmatch++;
501
502     // now choose the cluster to be matched with the track.
503
504     Int_t idclus=-1;
505     Float_t  recL = 0.;
506     Float_t  xpos=0.;
507     Float_t  mindist=1000.;
508     Float_t  mindistZ=0.;
509     for (Int_t iclus= 0; iclus<nfound;iclus++){
510       if (dist[iclus]< mindist){
511         mindist = dist[iclus];
512         mindistZ = distZ[iclus];
513         xpos = cxpos[iclus];
514         idclus =index[iclus]; 
515         recL=crecL[iclus]+corrLen*0.5;
516       }
517     }
518
519
520     AliTOFcluster *c=fClusters[idclus];
521
522     AliDebug(2, Form("%7i     %7i     %10i     %10i  %10i  %10i      %7i",
523                      iseed,
524                      fnmatch-1,
525                      TMath::Abs(trackTOFin->GetLabel()),
526                      c->GetLabel(0), c->GetLabel(1), c->GetLabel(2),
527                      idclus)); // AdC
528
529     c->Use(); 
530
531     // Track length correction for matching Step 2 
532
533     if(mLastStep){
534       Float_t rc=TMath::Sqrt(c->GetR()*c->GetR() + c->GetZ()*c->GetZ());
535       Float_t rt=TMath::Sqrt(trackPos[0][70]*trackPos[0][70]
536                              +trackPos[1][70]*trackPos[1][70]
537                              +trackPos[2][70]*trackPos[2][70]);
538       Float_t dlt=rc-rt;      
539       recL=trackPos[3][70]+dlt;
540     }    
541
542     if (
543         (c->GetLabel(0)==TMath::Abs(trackTOFin->GetLabel()))
544         ||
545         (c->GetLabel(1)==TMath::Abs(trackTOFin->GetLabel()))
546         ||
547         (c->GetLabel(2)==TMath::Abs(trackTOFin->GetLabel()))
548         ) {
549       fngoodmatch++;
550
551        AliDebug(2,Form(" track label good %5i",trackTOFin->GetLabel()));
552
553     }
554     else{
555       fnbadmatch++;
556
557       AliDebug(2,Form(" track label  bad %5i",trackTOFin->GetLabel()));
558
559     }
560
561     delete trackTOFin;
562
563     //  Store quantities to be used in the TOF Calibration
564     Float_t tToT=AliTOFGeometry::ToTBinWidth()*c->GetToT()*1E-3; // in ns
565     t->SetTOFsignalToT(tToT);
566     Float_t rawTime=AliTOFGeometry::TdcBinWidth()*c->GetTDCRAW()+32; // RAW time,in ps
567     t->SetTOFsignalRaw(rawTime);
568     t->SetTOFsignalDz(mindistZ);
569     AliDebug(2,Form(" Setting TOF raw time: %f, z distance: %f time: %f = ",rawTime,mindistZ));    
570     Int_t ind[5];
571     ind[0]=c->GetDetInd(0);
572     ind[1]=c->GetDetInd(1);
573     ind[2]=c->GetDetInd(2);
574     ind[3]=c->GetDetInd(3);
575     ind[4]=c->GetDetInd(4);
576     Int_t calindex = AliTOFGeometry::GetIndex(ind);
577     t->SetTOFCalChannel(calindex);
578
579     // keep track of the track labels in the matched cluster
580     Int_t tlab[3];
581     tlab[0]=c->GetLabel(0);
582     tlab[1]=c->GetLabel(1);
583     tlab[2]=c->GetLabel(2);
584     AliDebug(2,Form(" tdc time of the matched track %i = ",c->GetTDC()));    
585     Double_t tof=AliTOFGeometry::TdcBinWidth()*c->GetTDC()+32; // in ps
586     AliDebug(2,Form(" tof time of the matched track: %f = ",tof));    
587     Double_t tofcorr=tof;
588     if(timeWalkCorr)tofcorr=CorrectTimeWalk(mindistZ,tof);
589     AliDebug(2,Form(" tof time of the matched track, after TW corr: %f = ",tofcorr));    
590     //Set TOF time signal and pointer to the matched cluster
591     t->SetTOFsignal(tofcorr);
592     t->SetTOFcluster(idclus); // pointing to the recPoints tree
593
594     //Tracking info
595     Double_t time[AliPID::kSPECIES]; t->GetIntegratedTimes(time);
596     Double_t mom=t->GetP();
597     for(Int_t j=0;j<AliPID::kSPECIES;j++){
598       Double_t mass=AliPID::ParticleMass(j);
599       time[j]+=(recL-trackPos[3][0])/3e-2*TMath::Sqrt(mom*mom+mass*mass)/mom;
600     }
601
602     AliTOFtrack *trackTOFout = new AliTOFtrack(*t); 
603     trackTOFout->PropagateTo(xpos);
604     t->UpdateTrackParams(trackTOFout,AliESDtrack::kTOFout);    
605     t->SetIntegratedLength(recL);
606     t->SetIntegratedTimes(time);
607     t->SetTOFLabel(tlab);
608
609  
610     // Fill Reco-QA histos for Reconstruction
611     fHRecNClus->Fill(nc);
612     fHRecDist->Fill(mindist);
613     fHRecSigYVsP->Fill(mom,TMath::Sqrt(cov[0]));
614     fHRecSigZVsP->Fill(mom,TMath::Sqrt(cov[2]));
615     fHRecSigYVsPWin->Fill(mom,dphi*sensRadius);
616     fHRecSigZVsPWin->Fill(mom,dz);
617
618     // Fill Tree for on-the-fly offline Calibration
619
620     if ( !((t->GetStatus() & AliESDtrack::kTIME)==0 )){    
621       fIch=calindex;
622       fToT=tToT;
623       fTime=rawTime;
624       fExpTimePi=time[2];
625       fExpTimeKa=time[3];
626       fExpTimePr=time[4];
627       fCalTree->Fill();
628     }
629     delete trackTOFout;
630   }
631   for (Int_t ii=0; ii<4; ii++) delete [] trackPos[ii];
632   delete [] clind;
633  
634 }
635 //_________________________________________________________________________
636 Int_t AliTOFtracker::LoadClusters(TTree *cTree) {
637   //--------------------------------------------------------------------
638   //This function loads the TOF clusters
639   //--------------------------------------------------------------------
640
641   Int_t npadX = AliTOFGeometry::NpadX();
642   Int_t npadZ = AliTOFGeometry::NpadZ();
643   Int_t nStripA = AliTOFGeometry::NStripA();
644   Int_t nStripB = AliTOFGeometry::NStripB();
645   Int_t nStripC = AliTOFGeometry::NStripC();
646
647   TBranch *branch=cTree->GetBranch("TOF");
648   if (!branch) { 
649     AliError("can't get the branch with the TOF clusters !");
650     return 1;
651   }
652
653   static TClonesArray dummy("AliTOFcluster",10000);
654   dummy.Clear();
655   TClonesArray *clusters=&dummy;
656   branch->SetAddress(&clusters);
657
658   cTree->GetEvent(0);
659   Int_t nc=clusters->GetEntriesFast();
660   fHDigNClus->Fill(nc);
661
662   AliInfo(Form("Number of clusters: %d",nc));
663
664   for (Int_t i=0; i<nc; i++) {
665     AliTOFcluster *c=(AliTOFcluster*)clusters->UncheckedAt(i);
666     fClusters[i]=new AliTOFcluster(*c); fN++;
667
668   // Fill Digits QA histos
669  
670     Int_t isector = c->GetDetInd(0);
671     Int_t iplate = c->GetDetInd(1);
672     Int_t istrip = c->GetDetInd(2);
673     Int_t ipadX = c->GetDetInd(4);
674     Int_t ipadZ = c->GetDetInd(3);
675
676     Float_t time =(AliTOFGeometry::TdcBinWidth()*c->GetTDC())*1E-3; // in ns
677     Float_t tot = (AliTOFGeometry::TdcBinWidth()*c->GetToT())*1E-3;//in ns
678  
679     Int_t stripOffset = 0;
680     switch (iplate) {
681     case 0:
682       stripOffset = 0;
683       break;
684     case 1:
685       stripOffset = nStripC;
686       break;
687     case 2:
688       stripOffset = nStripC+nStripB;
689       break;
690     case 3:
691       stripOffset = nStripC+nStripB+nStripA;
692       break;
693     case 4:
694       stripOffset = nStripC+nStripB+nStripA+nStripB;
695       break;
696     default:
697       AliError(Form("Wrong plate number in TOF (%d) !",iplate));
698       break;
699     };
700     Int_t zindex=npadZ*(istrip+stripOffset)+(ipadZ+1);
701     Int_t phiindex=npadX*isector+ipadX+1;
702     fHDigClusMap->Fill(zindex,phiindex);
703     fHDigClusTime->Fill(time);
704     fHDigClusToT->Fill(tot);
705
706   }
707
708
709   return 0;
710 }
711 //_________________________________________________________________________
712 void AliTOFtracker::UnloadClusters() {
713   //--------------------------------------------------------------------
714   //This function unloads TOF clusters
715   //--------------------------------------------------------------------
716   for (Int_t i=0; i<fN; i++) {
717     delete fClusters[i];
718     fClusters[i] = 0x0;
719   }
720   fN=0;
721 }
722
723 //_________________________________________________________________________
724 Int_t AliTOFtracker::FindClusterIndex(Double_t z) const {
725   //--------------------------------------------------------------------
726   // This function returns the index of the nearest cluster 
727   //--------------------------------------------------------------------
728   if (fN==0) return 0;
729   if (z <= fClusters[0]->GetZ()) return 0;
730   if (z > fClusters[fN-1]->GetZ()) return fN;
731   Int_t b=0, e=fN-1, m=(b+e)/2;
732   for (; b<e; m=(b+e)/2) {
733     if (z > fClusters[m]->GetZ()) b=m+1;
734     else e=m; 
735   }
736   return m;
737 }
738
739 //_________________________________________________________________________
740 Bool_t AliTOFtracker::GetTrackPoint(Int_t index, AliTrackPoint& p) const
741 {
742   // Get track space point with index i
743   // Coordinates are in the global system
744   AliTOFcluster *cl = fClusters[index];
745   Float_t xyz[3];
746   xyz[0] = cl->GetR()*TMath::Cos(cl->GetPhi());
747   xyz[1] = cl->GetR()*TMath::Sin(cl->GetPhi());
748   xyz[2] = cl->GetZ();
749   Float_t phiangle = (Int_t(cl->GetPhi()*TMath::RadToDeg()/20.)+0.5)*20.*TMath::DegToRad();
750   Float_t sinphi = TMath::Sin(phiangle), cosphi = TMath::Cos(phiangle);
751   Float_t tiltangle = AliTOFGeometry::GetAngles(cl->GetDetInd(1),cl->GetDetInd(2))*TMath::DegToRad();
752   Float_t sinth = TMath::Sin(tiltangle), costh = TMath::Cos(tiltangle);
753   Float_t sigmay2 = AliTOFGeometry::XPad()*AliTOFGeometry::XPad()/12.;
754   Float_t sigmaz2 = AliTOFGeometry::ZPad()*AliTOFGeometry::ZPad()/12.;
755   Float_t cov[6];
756   cov[0] = sinphi*sinphi*sigmay2 + cosphi*cosphi*sinth*sinth*sigmaz2;
757   cov[1] = -sinphi*cosphi*sigmay2 + sinphi*cosphi*sinth*sinth*sigmaz2;
758   cov[2] = -cosphi*sinth*costh*sigmaz2;
759   cov[3] = cosphi*cosphi*sigmay2 + sinphi*sinphi*sinth*sinth*sigmaz2;
760   cov[4] = -sinphi*sinth*costh*sigmaz2;
761   cov[5] = costh*costh*sigmaz2;
762   p.SetXYZ(xyz[0],xyz[1],xyz[2],cov);
763
764   // Detector numbering scheme
765   Int_t nSector = AliTOFGeometry::NSectors();
766   Int_t nPlate  = AliTOFGeometry::NPlates();
767   Int_t nStripA = AliTOFGeometry::NStripA();
768   Int_t nStripB = AliTOFGeometry::NStripB();
769   Int_t nStripC = AliTOFGeometry::NStripC();
770
771   Int_t isector = cl->GetDetInd(0);
772   if (isector >= nSector)
773     AliError(Form("Wrong sector number in TOF (%d) !",isector));
774   Int_t iplate = cl->GetDetInd(1);
775   if (iplate >= nPlate)
776     AliError(Form("Wrong plate number in TOF (%d) !",iplate));
777   Int_t istrip = cl->GetDetInd(2);
778
779   Int_t stripOffset = 0;
780   switch (iplate) {
781   case 0:
782     stripOffset = 0;
783     break;
784   case 1:
785     stripOffset = nStripC;
786     break;
787   case 2:
788     stripOffset = nStripC+nStripB;
789     break;
790   case 3:
791     stripOffset = nStripC+nStripB+nStripA;
792     break;
793   case 4:
794     stripOffset = nStripC+nStripB+nStripA+nStripB;
795     break;
796   default:
797     AliError(Form("Wrong plate number in TOF (%d) !",iplate));
798     break;
799   };
800
801   Int_t idet = (2*(nStripC+nStripB)+nStripA)*isector +
802                stripOffset +
803                istrip;
804   UShort_t volid = AliGeomManager::LayerToVolUID(AliGeomManager::kTOF,idet);
805   p.SetVolumeID((UShort_t)volid);
806   return kTRUE;
807 }
808 //_________________________________________________________________________
809 void AliTOFtracker::InitCheckHists() {
810
811   //Init histos for Digits/Reco QA and Calibration
812
813
814   TDirectory *dir = gDirectory;
815   TFile *logFileTOF = 0;
816
817   TSeqCollection *list = gROOT->GetListOfFiles();
818   int n = list->GetEntries();
819   Bool_t isThere=kFALSE;
820   for(int i=0; i<n; i++) {
821     logFileTOF = (TFile*)list->At(i);
822     if (strstr(logFileTOF->GetName(), "TOFQA.root")){
823       isThere=kTRUE;
824       break;
825     } 
826   }
827
828   if(!isThere)logFileTOF = new TFile( "TOFQA.root","RECREATE");
829   logFileTOF->cd(); 
830
831   fCalTree = new TTree("CalTree", "Tree for TOF calibration");
832   fCalTree->Branch("TOFchannelindex",&fIch,"iTOFch/I");
833   fCalTree->Branch("ToT",&fToT,"TOFToT/F");
834   fCalTree->Branch("TOFtime",&fTime,"TOFtime/F");
835   fCalTree->Branch("PionExpTime",&fExpTimePi,"PiExpTime/F");
836   fCalTree->Branch("KaonExpTime",&fExpTimeKa,"KaExpTime/F");
837   fCalTree->Branch("ProtonExpTime",&fExpTimePr,"PrExpTime/F");
838
839   //Digits "QA" 
840   fHDigClusMap = new TH2F("TOFDig_ClusMap", "",182,0.5,182.5,864, 0.5,864.5);  
841   fHDigNClus = new TH1F("TOFDig_NClus", "",200,0.5,200.5);  
842   fHDigClusTime = new TH1F("TOFDig_ClusTime", "",2000,0.,200.);  
843   fHDigClusToT = new TH1F("TOFDig_ClusToT", "",500,0.,100);  
844
845   //Reco "QA"
846   fHRecNClus =new TH1F("TOFRec_NClusW", "",50,0.5,50.5);
847   fHRecDist=new TH1F("TOFRec_Dist", "",50,0.5,10.5);
848   fHRecSigYVsP=new TH2F("TOFDig_SigYVsP", "",40,0.,4.,100, 0.,5.);
849   fHRecSigZVsP=new TH2F("TOFDig_SigZVsP", "",40,0.,4.,100, 0.,5.);
850   fHRecSigYVsPWin=new TH2F("TOFDig_SigYVsPWin", "",40,0.,4.,100, 0.,50.);
851   fHRecSigZVsPWin=new TH2F("TOFDig_SigZVsPWin", "",40,0.,4.,100, 0.,50.);
852
853   dir->cd();
854
855 }
856
857 //_________________________________________________________________________
858 void AliTOFtracker::SaveCheckHists() {
859
860   //write histos for Digits/Reco QA and Calibration
861
862   TDirectory *dir = gDirectory;
863   TFile *logFileTOF = 0;
864
865   TSeqCollection *list = gROOT->GetListOfFiles();
866   int n = list->GetEntries();
867   Bool_t isThere=kFALSE;
868   for(int i=0; i<n; i++) {
869     logFileTOF = (TFile*)list->At(i);
870     if (strstr(logFileTOF->GetName(), "TOFQA.root")){
871       isThere=kTRUE;
872       break;
873     } 
874   }
875    
876   if(!isThere) {
877           AliError(Form("File TOFQA.root not found!! not wring histograms...."));
878           return;
879   }
880   logFileTOF->cd(); 
881   fHDigClusMap->Write(fHDigClusMap->GetName(), TObject::kOverwrite);
882   fHDigNClus->Write(fHDigNClus->GetName(), TObject::kOverwrite);
883   fHDigClusTime->Write(fHDigClusTime->GetName(), TObject::kOverwrite);
884   fHDigClusToT->Write(fHDigClusToT->GetName(), TObject::kOverwrite);
885   fHRecNClus->Write(fHRecNClus->GetName(), TObject::kOverwrite);
886   fHRecDist->Write(fHRecDist->GetName(), TObject::kOverwrite);
887   fHRecSigYVsP->Write(fHRecSigYVsP->GetName(), TObject::kOverwrite);
888   fHRecSigZVsP->Write(fHRecSigZVsP->GetName(), TObject::kOverwrite);
889   fHRecSigYVsPWin->Write(fHRecSigYVsPWin->GetName(), TObject::kOverwrite);
890   fHRecSigZVsPWin->Write(fHRecSigZVsPWin->GetName(), TObject::kOverwrite);
891   fCalTree->Write(fCalTree->GetName(),TObject::kOverwrite);
892   logFileTOF->Flush();  
893
894   dir->cd();
895   }
896 //_________________________________________________________________________
897 Float_t AliTOFtracker::CorrectTimeWalk( Float_t dist, Float_t tof) {
898
899   //dummy, for the moment
900   Float_t tofcorr=0.;
901   if(dist<AliTOFGeometry::ZPad()*0.5){
902     tofcorr=tof;
903     //place here the actual correction
904   }else{
905     tofcorr=tof; 
906   } 
907   return tofcorr;
908 }
909 //_________________________________________________________________________
910 Float_t AliTOFtracker::GetTimeZerofromT0(AliESDEvent *event) const {
911
912   //Returns TimeZero as measured by T0 detector
913
914   return event->GetT0();
915 }
916 //_________________________________________________________________________
917 Float_t AliTOFtracker::GetTimeZerofromTOF(AliESDEvent * /*event*/) const {
918
919   //dummy, for the moment. T0 algorithm using tracks on TOF
920   {
921     //place T0 algo here...
922   }
923   return 0.;
924 }
925 //_________________________________________________________________________
926
927 void AliTOFtracker::FillClusterArray(TObjArray* arr) const
928 {
929   //
930   // Returns the TOF cluster array
931   //
932
933   if (fN==0)
934     arr = 0x0;
935   else
936     for (Int_t i=0; i<fN; ++i) arr->Add(fClusters[i]);
937
938 }