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