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