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