]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFClusterFinder.cxx
7eee3c245fc6e6bcc730e5450d654f2a9d24a0f7
[u/mrichter/AliRoot.git] / TOF / AliTOFClusterFinder.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 $Log: AliTOFClusterFinder.cxx,v $
18 Revision 1.31  2007/11/24 14:53:19  zampolli
19 Status flag implemented as UChar_t
20
21 Revision 1.30  2007/10/04 13:08:52  arcelli
22 updates to comply with AliTOFGeometryV5 becoming AliTOFGeometry
23
24 Revision 1.29  2007/10/03 10:42:33  arcelli
25 updates to handle new AliTOFcluster, inheriting form AliCluster3D
26
27 Revision 1.28  2007/05/31 16:06:05  arcelli
28 move instance of AliRawStream outside loop on DDL
29
30 Revision 1.27  2007/05/02 16:31:49  arcelli
31 Add methods to handle single event reconstruction.  retrieval of Calib
32 info moved to AliTOFReconstructor ctor, and passed via a pointer to
33 AliTOFcalib
34
35 Revision 1.26  2007/04/30 19:02:24  arcelli
36 hopefully the last refinements for correct type conversion in calibration
37
38 Revision 1.25  2007/04/30 15:22:17  arcelli
39 Change TOF digit Time, Tot etc to int type
40
41 Revision 1.24  2007/04/27 11:19:31  arcelli
42 updates for the new decoder
43
44 Revision 1.23  2007/04/23 16:51:39  decaro
45 Digits-to-raw_data conversion: correction for a more real description
46 (A.De Caro, R.Preghenella)
47
48 Revision 1.22  2007/04/19 17:26:32  arcelli
49 Fix a bug (add some debug printout
50
51 Revision 1.21  2007/04/18 17:28:12  arcelli
52 Set the ToT bin width to the one actually used...
53
54 Revision 1.20  2007/03/09 09:57:23  arcelli
55  Remove a forgotten include of Riostrem
56
57 Revision 1.19  2007/03/08 15:41:20  arcelli
58 set uncorrected times when filling RecPoints
59
60 Revision 1.18  2007/03/06 16:31:20  arcelli
61 Add Uncorrected TOF Time signal
62
63 Revision 1.17  2007/02/28 18:09:11  arcelli
64 Add protection against failed retrieval of the CDB cal object,
65 now Reconstruction exits with AliFatal
66
67 Revision 1.16  2007/02/20 15:57:00  decaro
68 Raw data update: to read the TOF raw data defined in UNPACKED mode
69
70
71 Revision 0.03  2005/07/28 A. De Caro
72          Implement public method
73          Raw2Digits(Int_t, AliRawReader *)
74          to convert digits from raw data in MC digits
75          (temporary solution)
76
77 Revision 0.02  2005/07/27 A. De Caro
78          Implement public method
79          Digits2RecPoint(Int_t)
80          to convert digits in clusters
81
82 Revision 0.02  2005/07/26 A. De Caro
83          Implement private methods
84          InsertCluster(AliTOFcluster *)
85          FindClusterIndex(Double_t)
86          originally implemented in AliTOFtracker
87          by S. Arcelli and C. Zampolli
88
89 Revision 0.01  2005/07/25 A. De Caro
90          Implement public methods
91          Digits2RecPoint(AliRawReader *, TTree *)
92          Digits2RecPoint(Int_t, AliRawReader *)
93          to convert raw data in clusters
94  */
95
96 ////////////////////////////////////////////////////////////////
97 //                                                            //
98 //         Class for TOF cluster finder                       //
99 //                                                            //
100 // Starting from Raw Data, create rec points,                 //
101 //                         fill TreeR for TOF,                //
102 //                         write TOF.RecPoints.root file      //
103 //                                                            //
104 ////////////////////////////////////////////////////////////////
105
106 #include "Riostream.h"
107
108 #include "TClonesArray.h"
109 #include "TStopwatch.h"
110 #include "TTree.h"
111 //#include <TGeoManager.h>
112 #include <TGeoMatrix.h>
113 //#include <TGeoPhysicalNode.h>
114
115 #include "AliDAQ.h"
116 #include "AliLoader.h"
117 #include "AliLog.h"
118 #include "AliRawReader.h"
119 #include "AliRunLoader.h"
120 //#include "AliAlignObj.h"
121 #include <AliGeomManager.h>
122
123 #include "AliTOFcalib.h"
124 #include "AliTOFChannelOnlineArray.h"
125 #include "AliTOFChannelOnlineStatusArray.h"
126 #include "AliTOFChannelOffline.h"
127 #include "AliTOFClusterFinder.h"
128 #include "AliTOFcluster.h"
129 #include "AliTOFdigit.h"
130 #include "AliTOFGeometry.h"
131 #include "AliTOFrawData.h"
132 //#include "AliTOFRawStream.h"
133
134 #include "AliTOFDeltaBCOffset.h"
135 #include "AliTOFCTPLatency.h"
136 #include "AliTOFRunParams.h"
137
138 //extern TFile *gFile;
139
140 ClassImp(AliTOFClusterFinder)
141
142 AliTOFClusterFinder::AliTOFClusterFinder(AliTOFcalib *calib):
143   TTask("AliTOFClusterFinder",""),
144   fRunLoader(0),
145   fTOFLoader(0),
146   fTreeD(0),
147   fTreeR(0),
148   fDigits(new TClonesArray("AliTOFdigit", 4000)),
149   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
150   fNumberOfTofClusters(0),
151   fVerbose(0),
152   fDecoderVersion(0),
153   fTOFcalib(calib),
154   fTOFRawStream(AliTOFRawStream())
155 {
156 //
157 // Constructor
158 //
159
160   TString validity = (TString)fTOFcalib->GetOfflineValidity();
161   if (validity.CompareTo("valid")==0) {
162     AliInfo(Form(" validity = %s - Using offline calibration parameters",validity.Data()));
163   } else {
164     AliInfo(Form(" validity = %s - Using online calibration parameters",validity.Data()));
165   }
166
167 }
168
169 //______________________________________________________________________________
170
171 AliTOFClusterFinder::AliTOFClusterFinder(AliRunLoader* runLoader, AliTOFcalib *calib):
172   TTask("AliTOFClusterFinder",""),
173   fRunLoader(runLoader),
174   fTOFLoader(runLoader->GetLoader("TOFLoader")),
175   fTreeD(0),
176   fTreeR(0),
177   fDigits(new TClonesArray("AliTOFdigit", 4000)),
178   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
179   fNumberOfTofClusters(0),
180   fVerbose(0),
181   fDecoderVersion(0),
182   fTOFcalib(calib),
183   fTOFRawStream(AliTOFRawStream())
184 {
185 //
186 // Constructor
187 //
188
189   TString validity = (TString)fTOFcalib->GetOfflineValidity();
190   if (validity.CompareTo("valid")==0) {
191     AliInfo(Form(" validity = %s - Using offline calibration parameters",validity.Data()));
192   } else {
193     AliInfo(Form(" validity = %s - Using online calibration parameters",validity.Data()));
194   }
195
196 }
197
198 //------------------------------------------------------------------------
199 AliTOFClusterFinder::AliTOFClusterFinder(const AliTOFClusterFinder &source)
200   :TTask(source),
201   fRunLoader(0),
202   fTOFLoader(0),
203   fTreeD(0),
204   fTreeR(0),
205   fDigits(source.fDigits),
206   fRecPoints(source.fRecPoints),
207   fNumberOfTofClusters(0),
208   fVerbose(0),
209   fDecoderVersion(source.fDecoderVersion),
210   fTOFcalib(source.fTOFcalib),
211   fTOFRawStream(source.fTOFRawStream)
212 {
213   // copy constructor
214 }
215
216 //------------------------------------------------------------------------
217 AliTOFClusterFinder& AliTOFClusterFinder::operator=(const AliTOFClusterFinder &source)
218 {
219   // ass. op.
220
221   if (this == &source)
222     return *this;
223
224   TTask::operator=(source);  
225   fDigits=source.fDigits;
226   fRecPoints=source.fRecPoints;
227   fVerbose=source.fVerbose;
228   fDecoderVersion=source.fDecoderVersion;
229   fTOFcalib=source.fTOFcalib;
230   fTOFRawStream=source.fTOFRawStream;
231   return *this;
232
233 }
234 //______________________________________________________________________________
235
236 AliTOFClusterFinder::~AliTOFClusterFinder()
237 {
238
239   //
240   // Destructor
241   //
242
243   if (fDigits)
244     {
245       fDigits->Delete();
246       delete fDigits;
247       fDigits=0;
248     }
249   if (fRecPoints)
250     {
251       fRecPoints->Delete();
252       delete fRecPoints;
253       fRecPoints=0;
254     }
255
256   if (fTofClusters || fNumberOfTofClusters) {
257     for (Int_t ii=0; ii<fNumberOfTofClusters; ii++)
258       if (fTofClusters[ii]) fTofClusters[ii]->Delete();
259     fNumberOfTofClusters=0;
260    }
261
262 }
263 //______________________________________________________________________________
264
265 void AliTOFClusterFinder::Digits2RecPoints(Int_t iEvent)
266 {
267   //
268   // Converts digits to recpoints for TOF
269   //
270
271   TStopwatch stopwatch;
272   stopwatch.Start();
273
274   Int_t inholes = 0;
275
276   fRunLoader->GetEvent(iEvent);
277
278   fTreeD = fTOFLoader->TreeD();
279   if (fTreeD == 0x0)
280     {
281       AliFatal("AliTOFClusterFinder: Can not get TreeD");
282     }
283
284   TBranch *branch = fTreeD->GetBranch("TOF");
285   if (!branch) { 
286     AliError("can't get the branch with the TOF digits !");
287     return;
288   }
289
290   TClonesArray staticdigits("AliTOFdigit",10000);
291   staticdigits.Clear();
292   TClonesArray *digits =&staticdigits;
293   branch->SetAddress(&digits);
294
295   ResetRecpoint();
296
297   fTreeR = fTOFLoader->TreeR();
298   if (fTreeR == 0x0)
299     {
300       fTOFLoader->MakeTree("R");
301       fTreeR = fTOFLoader->TreeR();
302     }
303
304   Int_t bufsize = 32000;
305   fTreeR->Branch("TOF", &fRecPoints, bufsize);
306
307   fTreeD->GetEvent(0);
308   Int_t nDigits = digits->GetEntriesFast();
309   AliDebug(2,Form("Number of TOF digits: %d",nDigits));
310
311   Int_t ii;
312   Int_t dig[5]; //cluster detector indeces
313   Int_t  parTOF[7]; //The TOF signal parameters
314   Bool_t status=kTRUE; // assume all sim channels ok in the beginning...
315   for (ii=0; ii<nDigits; ii++) {
316     AliTOFdigit *d = (AliTOFdigit*)digits->UncheckedAt(ii);
317     dig[0]=d->GetSector();
318     dig[1]=d->GetPlate();
319     dig[2]=d->GetStrip();
320     dig[3]=d->GetPadz();
321     dig[4]=d->GetPadx();
322
323     /* check valid index */
324     if (dig[0]==-1||dig[1]==-1||dig[2]==-1||dig[3]==-1||dig[4]==-1) continue;
325
326     // Do not reconstruct anything in the holes
327     if (dig[0]==13 || dig[0]==14 || dig[0]==15 ) { // sectors with holes
328       if (dig[1]==2) { // plate with holes
329         inholes++;
330         continue;
331       }
332     }
333
334     AliDebug(2,Form(" %2d  %1d  %2d  %1d  %2d ",dig[0],dig[1],dig[2],dig[3],dig[4]));
335
336     parTOF[0] = d->GetTdc(); //the TDC signal
337     parTOF[1] = d->GetToT(); //the ToT signal
338     parTOF[2] = d->GetAdc(); // the adc charge
339     parTOF[3] = d->GetTdcND(); // non decalibrated sim time
340     parTOF[4] = d->GetTdc(); // raw time, == Tdc time for the moment
341     parTOF[5] = 0; // deltaBC
342     parTOF[6] = 0; // L0-L1 latency
343     Double_t posClus[3];
344     Double_t covClus[6];
345     UShort_t volIdClus=GetClusterVolIndex(dig);
346     GetClusterPars(dig, posClus,covClus);
347     AliTOFcluster *tofCluster = new AliTOFcluster(volIdClus,posClus[0],posClus[1],posClus[2],covClus[0],covClus[1],covClus[2],covClus[3],covClus[4],covClus[5],d->GetTracks(),dig,parTOF,status,ii);
348     InsertCluster(tofCluster);
349
350   }
351
352   AliDebug(1,Form("Number of found clusters: %d for event: %d", fNumberOfTofClusters, iEvent));
353
354   CalibrateRecPoint();
355   FillRecPoint();
356
357   fTreeR->Fill();
358   ResetRecpoint();
359
360   fTOFLoader = fRunLoader->GetLoader("TOFLoader");  
361   fTOFLoader->WriteRecPoints("OVERWRITE");
362
363   AliDebug(1,Form("Execution time to read TOF digits and to write TOF clusters : R:%.4fs C:%.4fs",
364                   stopwatch.RealTime(),stopwatch.CpuTime()));
365   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
366
367 }
368
369 //______________________________________________________________________________
370
371 void AliTOFClusterFinder::Digits2RecPoints(TTree* digitsTree, TTree* clusterTree)
372 {
373   //
374   // Converts digits to recpoints for TOF
375   //
376
377   TStopwatch stopwatch;
378   stopwatch.Start();
379
380   Int_t inholes = 0;
381
382   ///  fRunLoader->GetEvent(iEvent);
383
384   if (digitsTree == 0x0)
385     {
386       AliFatal("AliTOFClusterFinder: Can not get TreeD");
387     }
388
389   TBranch *branch = digitsTree->GetBranch("TOF");
390   if (!branch) { 
391     AliError("can't get the branch with the TOF digits !");
392     return;
393   }
394
395   TClonesArray staticdigits("AliTOFdigit",10000);
396   staticdigits.Clear();
397   TClonesArray *digits = & staticdigits;
398   branch->SetAddress(&digits);
399
400   ResetRecpoint();
401
402   fTreeR=clusterTree;
403   Int_t bufsize = 32000;
404   fTreeR->Branch("TOF", &fRecPoints, bufsize);
405
406   digitsTree->GetEvent(0);
407   Int_t nDigits = digits->GetEntriesFast();
408   AliDebug(2,Form("Number of TOF digits: %d",nDigits));
409
410   Int_t ii;
411   Int_t dig[5]; //cluster detector indeces
412   Int_t  parTOF[7]; //The TOF signal parameters
413   Bool_t status=kTRUE; // assume all sim channels ok in the beginning...
414   for (ii=0; ii<nDigits; ii++) {
415     AliTOFdigit *d = (AliTOFdigit*)digits->UncheckedAt(ii);
416     dig[0]=d->GetSector();
417     dig[1]=d->GetPlate();
418     dig[2]=d->GetStrip();
419     dig[3]=d->GetPadz();
420     dig[4]=d->GetPadx();
421
422     /* check valid index */
423     if (dig[0]==-1||dig[1]==-1||dig[2]==-1||dig[3]==-1||dig[4]==-1) continue;
424
425     // Do not reconstruct anything in the holes
426     if (dig[0]==13 || dig[0]==14 || dig[0]==15 ) { // sectors with holes
427       if (dig[1]==2) { // plate with holes
428         inholes++;
429         continue;
430       }
431     }
432
433     //    AliDebug(2,Form(" %2d  %1d  %2d  %1d  %2d ",dig[0],dig[1],dig[2],dig[3],dig[4]));
434
435     parTOF[0] = d->GetTdc(); //the TDC signal
436     parTOF[1] = d->GetToT(); //the ToT signal
437     parTOF[2] = d->GetAdc(); // the adc charge
438     parTOF[3] = d->GetTdcND(); // non decalibrated sim time
439     parTOF[4] = d->GetTdc(); // raw time, == Tdc time for the moment
440     parTOF[5] = 0; // deltaBC
441     parTOF[6] = 0; // L0-L1 latency
442     
443     Double_t posClus[3];
444     Double_t covClus[6];
445     UShort_t volIdClus=GetClusterVolIndex(dig);
446     GetClusterPars(dig,posClus,covClus);
447     AliTOFcluster *tofCluster = new AliTOFcluster(volIdClus,posClus[0],posClus[1],posClus[2],covClus[0],covClus[1],covClus[2],covClus[3],covClus[4],covClus[5],d->GetTracks(),dig,parTOF,status,ii);
448     InsertCluster(tofCluster);
449
450   }
451
452   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
453
454   CalibrateRecPoint();
455   FillRecPoint();
456
457   clusterTree->Fill();
458   ResetRecpoint();
459
460   AliDebug(1,Form("Execution time to read TOF digits and to write TOF clusters : R:%.4fs C:%.4fs",
461                   stopwatch.RealTime(),stopwatch.CpuTime()));
462   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
463
464 }
465 //______________________________________________________________________________
466
467 void AliTOFClusterFinder::Digits2RecPoints(AliRawReader *rawReader,
468                                            TTree *clustersTree)
469 {
470   //
471   // Converts RAW data to recpoints for TOF
472   //
473
474   TStopwatch stopwatch;
475   stopwatch.Start();
476
477   Int_t inholes = 0;
478
479   const Int_t kDDL = AliDAQ::NumberOfDdls("TOF");
480
481   ResetRecpoint();
482
483   Int_t bufsize = 32000;
484   clustersTree->Branch("TOF", &fRecPoints, bufsize);
485
486   TClonesArray * clonesRawData;
487
488   Int_t dummy = -1;
489
490   Int_t detectorIndex[5];
491   Int_t parTOF[7];
492
493   ofstream ftxt;
494   if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
495
496   //AliTOFRawStream tofInput(rawReader);
497   fTOFRawStream.Clear();
498   fTOFRawStream.SetRawReader(rawReader);
499
500   if (fDecoderVersion == 1) {
501     AliInfo("Using New Decoder");
502   }
503   else if (fDecoderVersion == 2) {
504     AliInfo("Using Enhanced Decoder");
505   }
506   else {
507     AliInfo("Using Old Decoder");
508   }
509
510   Int_t indexDDL = 0;
511   for (indexDDL = 0; indexDDL < kDDL; indexDDL++) {
512
513     rawReader->Reset();
514     if (fDecoderVersion == 1) {
515       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
516     }
517     else if (fDecoderVersion == 2) {
518       fTOFRawStream.LoadRawDataBuffersV2(indexDDL,fVerbose);
519     }
520     else  {
521       fTOFRawStream.LoadRawData(indexDDL);
522     }
523     
524     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
525     
526     for (Int_t iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
527
528       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
529
530       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
531       if (tofRawDatum->GetTOF()==-1) continue;
532
533       if (fVerbose==2) {
534         if (indexDDL<10) ftxt << "  " << indexDDL;
535         else         ftxt << " " << indexDDL;
536         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
537         else         ftxt << " " << tofRawDatum->GetTRM();
538         ftxt << "  " << tofRawDatum->GetTRMchain();
539         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
540         else         ftxt << " " << tofRawDatum->GetTDC();
541         ftxt << "  " << tofRawDatum->GetTDCchannel();
542       }
543
544       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
545                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
546       dummy = detectorIndex[3];
547       detectorIndex[3] = detectorIndex[4];
548       detectorIndex[4] = dummy;
549
550       if (fVerbose==2) {
551         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
552         else              ftxt  << "  -> " << detectorIndex[0];
553         ftxt << "  " << detectorIndex[1];
554         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
555         else              ftxt << " " << detectorIndex[2];
556         ftxt << "  " << detectorIndex[3];
557         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[4];
558         else              ftxt << " " << detectorIndex[4];
559       }
560
561     /* check valid index */
562     if (detectorIndex[0]==-1||detectorIndex[1]==-1||detectorIndex[2]==-1||detectorIndex[3]==-1||detectorIndex[4]==-1) continue;
563
564       // Do not reconstruct anything in the holes
565       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
566         if (detectorIndex[1]==2) { // plate with holes
567           inholes++;
568           continue;
569         }
570       }
571
572       parTOF[0] = tofRawDatum->GetTOF(); //TDC
573       parTOF[1] = tofRawDatum->GetTOT(); // TOT
574       parTOF[2] = tofRawDatum->GetTOT(); //ADC==TOF
575       parTOF[3] = -1;//raw data: no track of undecalib sim time
576       parTOF[4] = tofRawDatum->GetTOF(); // RAW time
577       parTOF[5] = tofRawDatum->GetDeltaBC(); // deltaBC
578       parTOF[6] = tofRawDatum->GetL0L1Latency(); // L0-L1 latency
579       Double_t posClus[3];
580       Double_t covClus[6];
581       UShort_t volIdClus=GetClusterVolIndex(detectorIndex);
582       Int_t lab[3]={-1,-1,-1};
583       Bool_t status=kTRUE;
584       GetClusterPars(detectorIndex,posClus,covClus);
585       AliTOFcluster *tofCluster = new AliTOFcluster(volIdClus,posClus[0],posClus[1],posClus[2],covClus[0],covClus[1],covClus[2],covClus[3],covClus[4],covClus[5],lab,detectorIndex,parTOF,status,-1);
586       InsertCluster(tofCluster);
587
588       if (fVerbose==2) {
589         if (parTOF[1]<10)ftxt << "        " << parTOF[1];
590         else if (parTOF[1]>=10 && parTOF[1]<100) ftxt << "      " << parTOF[1];
591         else ftxt << "      " << parTOF[1];
592         if (parTOF[0]<10) ftxt << "      " << parTOF[0] << endl;
593         else if (parTOF[0]>=10 && parTOF[0]<100)   ftxt << "    " << parTOF[0] << endl;
594         else if (parTOF[0]>=100 && parTOF[0]<1000) ftxt << "    " << parTOF[0] << endl;
595         else ftxt << "   " << parTOF[3] << endl;
596       }
597
598     } // closed loop on TOF raw data per current DDL file
599
600     clonesRawData->Clear();
601
602   } // closed loop on DDL index
603
604   if (fVerbose==2) ftxt.close();
605
606   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
607
608   CalibrateRecPoint(rawReader->GetTimestamp());
609   FillRecPoint();
610
611   clustersTree->Fill();
612
613   ResetRecpoint();
614
615   AliDebug(1, Form("Execution time to read TOF raw data and to write TOF clusters : R:%.4fs C:%.4fs",
616                    stopwatch.RealTime(),stopwatch.CpuTime()));
617   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
618
619 }
620 //______________________________________________________________________________
621
622 void AliTOFClusterFinder::Digits2RecPoints(Int_t iEvent, AliRawReader *rawReader)
623 {
624   //
625   // Converts RAW data to recpoints for TOF
626   //
627
628   TStopwatch stopwatch;
629   stopwatch.Start();
630
631   Int_t inholes = 0;
632
633   const Int_t kDDL = AliDAQ::NumberOfDdls("TOF");
634
635   fRunLoader->GetEvent(iEvent);
636
637   AliDebug(2,Form(" Event number %2d ", iEvent));
638
639   fTreeR = fTOFLoader->TreeR();
640
641   if (fTreeR == 0x0){
642     fTOFLoader->MakeTree("R");
643     fTreeR = fTOFLoader->TreeR();
644   }
645
646   Int_t bufsize = 32000;
647   fTreeR->Branch("TOF", &fRecPoints, bufsize);
648
649   TClonesArray * clonesRawData;
650
651   Int_t dummy = -1;
652
653   Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
654   Int_t parTOF[7];
655   ofstream ftxt;
656   if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
657
658   //AliTOFRawStream tofInput(rawReader);
659   fTOFRawStream.Clear();
660   fTOFRawStream.SetRawReader(rawReader);
661
662   if (fDecoderVersion == 1) {
663     AliInfo("Using New Decoder");
664   }
665   else if (fDecoderVersion == 2) {
666     AliInfo("Using Enhanced Decoder");
667   }
668   else {
669     AliInfo("Using Old Decoder");
670   }
671
672   Int_t indexDDL = 0;
673   for (indexDDL = 0; indexDDL < kDDL; indexDDL++) {
674
675     rawReader->Reset();
676     if (fDecoderVersion == 1) {
677       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
678     }
679     else if (fDecoderVersion == 2) {
680       fTOFRawStream.LoadRawDataBuffersV2(indexDDL,fVerbose);
681     }
682     else {
683       fTOFRawStream.LoadRawData(indexDDL);
684     }
685
686     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
687
688     for (Int_t iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
689
690       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
691
692       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
693       if (tofRawDatum->GetTOF()==-1) continue;
694
695       if (fVerbose==2) {
696         if (indexDDL<10) ftxt << "  " << indexDDL;
697         else         ftxt << " " << indexDDL;
698         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
699         else         ftxt << " " << tofRawDatum->GetTRM();
700         ftxt << "  " << tofRawDatum->GetTRMchain();
701         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
702         else         ftxt << " " << tofRawDatum->GetTDC();
703         ftxt << "  " << tofRawDatum->GetTDCchannel();
704       }
705
706       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
707                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
708       dummy = detectorIndex[3];
709       detectorIndex[3] = detectorIndex[4];
710       detectorIndex[4] = dummy;
711
712       if (fVerbose==2) {
713         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
714         else              ftxt  << "  -> " << detectorIndex[0];
715         ftxt << "  " << detectorIndex[1];
716         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
717         else              ftxt << " " << detectorIndex[2];
718         ftxt << "  " << detectorIndex[3];
719         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[4];
720         else              ftxt << " " << detectorIndex[4];
721       }
722
723     /* check valid index */
724     if (detectorIndex[0]==-1||detectorIndex[1]==-1||detectorIndex[2]==-1||detectorIndex[3]==-1||detectorIndex[4]==-1) continue;
725
726       // Do not reconstruct anything in the holes
727       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
728         if (detectorIndex[1]==2) { // plate with holes
729           inholes++;
730           continue;
731         }
732       }
733
734       parTOF[0] = tofRawDatum->GetTOF(); // TDC
735       parTOF[1] = tofRawDatum->GetTOT(); // TOT
736       parTOF[2] = tofRawDatum->GetTOT(); // raw data have ADC=TOT
737       parTOF[3] = -1; //raw data: no track of the undecalib sim time
738       parTOF[4] = tofRawDatum->GetTOF(); // Raw time == TDC
739       parTOF[5] = tofRawDatum->GetDeltaBC(); // deltaBC
740       parTOF[6] = tofRawDatum->GetL0L1Latency(); // L0-L1 latency
741       Double_t posClus[3];
742       Double_t covClus[6];
743       UShort_t volIdClus=GetClusterVolIndex(detectorIndex);
744       Int_t lab[3]={-1,-1,-1};
745       Bool_t status=kTRUE;
746       GetClusterPars(detectorIndex,posClus,covClus);
747       AliTOFcluster *tofCluster = new AliTOFcluster(volIdClus,posClus[0],posClus[1],posClus[2],covClus[0],covClus[1],covClus[2],covClus[3],covClus[4],covClus[5],lab,detectorIndex,parTOF,status,-1);
748       InsertCluster(tofCluster);
749
750       if (fVerbose==2) {
751         if (parTOF[1]<10)ftxt << "        " << parTOF[1];
752         else if (parTOF[1]>=10 && parTOF[1]<100) ftxt << "      " << parTOF[1];
753         else ftxt << "      " << parTOF[1];
754         if (parTOF[0]<10) ftxt << "      " << parTOF[0] << endl;
755         else if (parTOF[0]>=10 && parTOF[0]<100)   ftxt << "    " << parTOF[0] << endl;
756         else if (parTOF[0]>=100 && parTOF[0]<1000) ftxt << "    " << parTOF[0] << endl;
757         else ftxt << "   " << parTOF[3] << endl;
758       }
759
760     } // closed loop on TOF raw data per current DDL file
761
762     clonesRawData->Clear();
763
764   } // closed loop on DDL index
765
766   if (fVerbose==2) ftxt.close();
767
768   AliDebug(1,Form("Number of found clusters: %d for event: %d", fNumberOfTofClusters, iEvent));
769
770   CalibrateRecPoint(rawReader->GetTimestamp());
771   FillRecPoint();
772
773   fTreeR->Fill();
774   ResetRecpoint();
775
776   fTOFLoader = fRunLoader->GetLoader("TOFLoader");
777   fTOFLoader->WriteRecPoints("OVERWRITE");
778   
779   AliDebug(1, Form("Execution time to read TOF raw data and to write TOF clusters : R:%.4fs C:%.4fs",
780                stopwatch.RealTime(),stopwatch.CpuTime()));
781   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
782
783 }
784 //______________________________________________________________________________
785
786 void AliTOFClusterFinder::Raw2Digits(Int_t iEvent, AliRawReader *rawReader)
787 {
788   //
789   // Converts RAW data to MC digits for TOF
790   //
791   //             (temporary solution)
792   //
793
794   TStopwatch stopwatch;
795   stopwatch.Start();
796
797   const Int_t kDDL = AliTOFGeometry::NDDL()*AliTOFGeometry::NSectors();
798
799   fRunLoader->GetEvent(iEvent);
800
801   fTreeD = fTOFLoader->TreeD();
802   if (fTreeD)
803     {
804     AliInfo("TreeD re-creation");
805     fTreeD = 0x0;
806     fTOFLoader->MakeTree("D");
807     fTreeD = fTOFLoader->TreeD();
808     }
809
810   TClonesArray *tofDigits = new TClonesArray("AliTOFdigit",10000);
811   Int_t bufsize = 32000;
812   fTreeD->Branch("TOF", &tofDigits, bufsize);
813
814   fRunLoader->GetEvent(iEvent);
815
816   AliDebug(2,Form(" Event number %2d ", iEvent));
817
818   TClonesArray * clonesRawData;
819
820   Int_t dummy = -1;
821
822   Int_t detectorIndex[5];
823   Int_t digit[4];
824
825   //AliTOFRawStream tofInput(rawReader);
826   fTOFRawStream.Clear();
827   fTOFRawStream.SetRawReader(rawReader);
828
829   if (fDecoderVersion == 1) {
830     AliInfo("Using New Decoder");
831   }
832   else if (fDecoderVersion == 2) {
833     AliInfo("Using Enhanced Decoder");
834   }
835   else {
836     AliInfo("Using Old Decoder");
837   }
838
839   Int_t indexDDL = 0;
840   for (indexDDL = 0; indexDDL < kDDL; indexDDL++) {
841
842     rawReader->Reset();
843     if (fDecoderVersion == 1) {
844       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
845     }
846     else if (fDecoderVersion == 2) {
847       fTOFRawStream.LoadRawDataBuffersV2(indexDDL,fVerbose);
848     }
849     else {
850       fTOFRawStream.LoadRawData(indexDDL);
851     }
852
853     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
854
855     for (Int_t iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
856
857       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
858
859       //if (!tofRawDatum->GetTOT() || !tofRawDatum->GetTOF()) continue;
860       if (tofRawDatum->GetTOF()==-1) continue;
861
862       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
863                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
864       dummy = detectorIndex[3];
865       detectorIndex[3] = detectorIndex[4];
866       detectorIndex[4] = dummy;
867
868       digit[0] = fTOFRawStream.GetTofBin();
869       digit[1] = fTOFRawStream.GetToTbin();
870       digit[2] = fTOFRawStream.GetToTbin();
871       digit[3] = -1;
872
873       Int_t tracknum[3]={-1,-1,-1};
874
875       TClonesArray &aDigits = *tofDigits;
876       Int_t last=tofDigits->GetEntriesFast();
877       new (aDigits[last]) AliTOFdigit(tracknum, detectorIndex, digit);
878
879     } // while loop
880
881     clonesRawData->Clear();
882
883   } // DDL Loop
884
885   fTreeD->Fill();
886
887   fTOFLoader = fRunLoader->GetLoader("TOFLoader");
888   fTOFLoader->WriteDigits("OVERWRITE");
889
890   delete tofDigits;
891
892   AliDebug(1, Form("Execution time to read TOF raw data and to write TOF clusters : R:%.2fs C:%.2fs",
893                    stopwatch.RealTime(),stopwatch.CpuTime()));
894
895 }
896
897 //______________________________________________________________________________
898
899 void AliTOFClusterFinder::Raw2Digits(AliRawReader *rawReader, TTree* digitsTree)
900 {
901   //
902   // Converts RAW data to MC digits for TOF for the current event
903   //
904   //
905
906   TStopwatch stopwatch;
907   stopwatch.Start();
908
909   const Int_t kDDL = AliTOFGeometry::NDDL()*AliTOFGeometry::NSectors();
910
911   if (!digitsTree)
912     {
913     AliError("No input digits Tree");
914     return;
915     }
916
917   TClonesArray *tofDigits = new TClonesArray("AliTOFdigit",10000);
918   Int_t bufsize = 32000;
919   digitsTree->Branch("TOF", &tofDigits, bufsize);
920
921   TClonesArray * clonesRawData;
922
923   Int_t dummy = -1;
924
925   Int_t detectorIndex[5];
926   Int_t digit[4];
927
928   //AliTOFRawStream tofInput(rawReader);
929   fTOFRawStream.Clear();
930   fTOFRawStream.SetRawReader(rawReader);
931
932   if (fDecoderVersion == 1) {
933     AliInfo("Using New Decoder");
934   }
935   else if (fDecoderVersion == 2) {
936     AliInfo("Using Enhanced Decoder");
937   }
938   else {
939     AliInfo("Using Old Decoder");
940   }
941
942   Int_t indexDDL = 0;
943   for (indexDDL = 0; indexDDL < kDDL; indexDDL++) {
944
945     rawReader->Reset();
946     if (fDecoderVersion == 1) {
947       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
948     }
949     else if (fDecoderVersion == 2) {
950       fTOFRawStream.LoadRawDataBuffersV2(indexDDL,fVerbose);
951     }
952     else {
953       fTOFRawStream.LoadRawData(indexDDL);
954     }
955
956     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
957
958     for (Int_t iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
959
960       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
961
962       //if (!tofRawDatum->GetTOT() || !tofRawDatum->GetTOF()) continue;
963       if (tofRawDatum->GetTOF()==-1) continue;
964
965       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
966                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
967       dummy = detectorIndex[3];
968       detectorIndex[3] = detectorIndex[4];
969       detectorIndex[4] = dummy;
970
971       digit[0] = fTOFRawStream.GetTofBin();
972       digit[1] = fTOFRawStream.GetToTbin();
973       digit[2] = fTOFRawStream.GetToTbin();
974       digit[3] = -1;
975
976       Int_t tracknum[3]={-1,-1,-1};
977
978       TClonesArray &aDigits = *tofDigits;
979       Int_t last=tofDigits->GetEntriesFast();
980       new (aDigits[last]) AliTOFdigit(tracknum, detectorIndex, digit);
981
982     } // while loop
983
984     clonesRawData->Clear();
985
986   } // DDL Loop
987
988   digitsTree->Fill();
989
990   delete tofDigits;
991
992   AliDebug(1, Form("Got %d digits: ", fDigits->GetEntries()));
993   AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
994                    stopwatch.RealTime(),stopwatch.CpuTime()));
995
996 }
997 //______________________________________________________________________________
998
999 Int_t AliTOFClusterFinder::InsertCluster(AliTOFcluster *tofCluster) {
1000   //---------------------------------------------------------------------------//
1001   // This function adds a TOF cluster to the array of TOF clusters sorted in Z //
1002   //---------------------------------------------------------------------------//
1003   if (fNumberOfTofClusters==kTofMaxCluster) {
1004     AliError("Too many clusters !");
1005     return 1;
1006   }
1007
1008   if (fNumberOfTofClusters==0) {
1009     fTofClusters[fNumberOfTofClusters++] = tofCluster;
1010     return 0;
1011   }
1012
1013   Int_t ii = FindClusterIndex(tofCluster->GetZ());
1014   memmove(fTofClusters+ii+1 ,fTofClusters+ii,(fNumberOfTofClusters-ii)*sizeof(AliTOFcluster*));
1015   fTofClusters[ii] = tofCluster;
1016   fNumberOfTofClusters++;
1017   
1018   return 0;
1019
1020 }
1021 //_________________________________________________________________________
1022
1023 Int_t AliTOFClusterFinder::FindClusterIndex(Double_t z) const {
1024   //--------------------------------------------------------------------
1025   // This function returns the index of the nearest cluster in z
1026   //--------------------------------------------------------------------
1027   if (fNumberOfTofClusters==0) return 0;
1028   if (z <= fTofClusters[0]->GetZ()) return 0;
1029   if (z > fTofClusters[fNumberOfTofClusters-1]->GetZ()) return fNumberOfTofClusters;
1030   Int_t b = 0, e = fNumberOfTofClusters-1, m = (b+e)/2;
1031   for (; b<e; m=(b+e)/2) {
1032     if (z > fTofClusters[m]->GetZ()) b=m+1;
1033     else e=m;
1034   }
1035
1036   return m;
1037
1038 }
1039 //_________________________________________________________________________
1040
1041 void AliTOFClusterFinder::FillRecPoint()
1042 {
1043   //
1044   // Copy the global array of AliTOFcluster, i.e. fTofClusters (sorted
1045   // in Z) in the global TClonesArray of AliTOFcluster,
1046   // i.e. fRecPoints.
1047   //
1048
1049   Int_t ii, jj;
1050
1051   Int_t detectorIndex[5];
1052   Int_t parTOF[7];
1053   Int_t trackLabels[3];
1054   Int_t digitIndex = -1;
1055   Bool_t status=kTRUE;
1056
1057   TClonesArray &lRecPoints = *fRecPoints;
1058   
1059   for (ii=0; ii<fNumberOfTofClusters; ii++) {
1060
1061     digitIndex = fTofClusters[ii]->GetIndex();
1062     for(jj=0; jj<5; jj++) detectorIndex[jj] = fTofClusters[ii]->GetDetInd(jj);
1063     for(jj=0; jj<3; jj++) trackLabels[jj] = fTofClusters[ii]->GetLabel(jj);
1064     parTOF[0] = fTofClusters[ii]->GetTDC(); // TDC
1065     parTOF[1] = fTofClusters[ii]->GetToT(); // TOT
1066     parTOF[2] = fTofClusters[ii]->GetADC(); // ADC=TOT
1067     parTOF[3] = fTofClusters[ii]->GetTDCND(); // TDCND
1068     parTOF[4] = fTofClusters[ii]->GetTDCRAW();//RAW
1069     parTOF[5] = fTofClusters[ii]->GetDeltaBC();//deltaBC
1070     parTOF[6] = fTofClusters[ii]->GetL0L1Latency();//L0-L1 latency
1071     status=fTofClusters[ii]->GetStatus();
1072     Double_t posClus[3];
1073     Double_t covClus[6];
1074     UShort_t volIdClus=GetClusterVolIndex(detectorIndex);
1075     GetClusterPars(detectorIndex,posClus,covClus);
1076     new(lRecPoints[ii]) AliTOFcluster(volIdClus,posClus[0],posClus[1],posClus[2],covClus[0],covClus[1],covClus[2],covClus[3],covClus[4],covClus[5],trackLabels,detectorIndex, parTOF,status,digitIndex);
1077
1078     AliDebug(2, Form(" %4d  %4d  %f %f %f  %f %f %f %f %f %f  %3d %3d %3d  %2d %1d %2d %1d %2d  %4d %3d %3d %4d %4d  %1d  %4d", 
1079                      ii, volIdClus, posClus[0], posClus[1], posClus[2],
1080                      fTofClusters[ii]->GetSigmaX2(),
1081                      fTofClusters[ii]->GetSigmaXY(),
1082                      fTofClusters[ii]->GetSigmaXZ(),
1083                      fTofClusters[ii]->GetSigmaY2(),
1084                      fTofClusters[ii]->GetSigmaYZ(),
1085                      fTofClusters[ii]->GetSigmaZ2(),
1086                      trackLabels[0], trackLabels[1], trackLabels[2],
1087                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[3], detectorIndex[4],
1088                      parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
1089                      status, digitIndex));
1090
1091   } // loop on clusters
1092
1093 }
1094
1095 //_________________________________________________________________________
1096
1097 /*
1098  * OLD CALIBRATE REC POINTS FUNCTION
1099  */
1100
1101 #if 0
1102 void AliTOFClusterFinder::CalibrateRecPoint(UInt_t timestamp)
1103 {
1104   //
1105   // Copy the global array of AliTOFcluster, i.e. fTofClusters (sorted
1106   // in Z) in the global TClonesArray of AliTOFcluster,
1107   // i.e. fRecPoints.
1108   //
1109
1110   Int_t ii, jj;
1111
1112   Int_t detectorIndex[5];
1113   Int_t digitIndex = -1;
1114   Double_t tToT;
1115   Double_t timeCorr;
1116   Int_t   tdcCorr;
1117   Float_t tdcLatencyWindow;
1118   AliDebug(1," Calibrating TOF Clusters");
1119
1120   AliTOFChannelOnlineArray *calDelay = fTOFcalib->GetTOFOnlineDelay();  
1121   AliTOFChannelOnlineStatusArray *calStatus = fTOFcalib->GetTOFOnlineStatus();  
1122   TObjArray *calTOFArrayOffline = fTOFcalib->GetTOFCalArrayOffline();
1123   
1124   AliTOFDeltaBCOffset *deltaBCOffsetObj = fTOFcalib->GetDeltaBCOffset();
1125   Int_t deltaBCOffset = deltaBCOffsetObj->GetDeltaBCOffset();
1126   AliTOFCTPLatency *ctpLatencyObj = fTOFcalib->GetCTPLatency();
1127   Float_t ctpLatency = ctpLatencyObj->GetCTPLatency();
1128   AliTOFRunParams *runParamsObj = fTOFcalib->GetRunParams();
1129   Float_t t0 = runParamsObj->EvalT0(timestamp);
1130
1131   TString validity = (TString)fTOFcalib->GetOfflineValidity();
1132   Int_t calibration = -1;
1133   if (validity.CompareTo("valid")==0) {
1134     //AliInfo(Form(" validity = %s - Using offline calibration parameters",validity.Data()));
1135     calibration = 1;
1136   } else {
1137     //AliInfo(Form(" validity = %s - Using online calibration parameters",validity.Data()));
1138     calibration = 0 ;
1139   }
1140
1141   for (ii=0; ii<fNumberOfTofClusters; ii++) {
1142     digitIndex = fTofClusters[ii]->GetIndex();
1143     for(jj=0; jj<5; jj++) detectorIndex[jj] = fTofClusters[ii]->GetDetInd(jj);
1144
1145     Int_t index = AliTOFGeometry::GetIndex(detectorIndex);
1146      
1147     UChar_t statusPulser=calStatus->GetPulserStatus(index);
1148     UChar_t statusNoise=calStatus->GetNoiseStatus(index);
1149     UChar_t statusHW=calStatus->GetHWStatus(index);
1150     UChar_t status=calStatus->GetStatus(index);
1151     tdcLatencyWindow = calStatus->GetLatencyWindow(index) * 1.e3; /* ns -> ps */
1152     
1153     //check the status, also unknown is fine!!!!!!!
1154
1155     AliDebug(2, Form(" Status for channel %d = %d",index, (Int_t)status));
1156     if((statusPulser & AliTOFChannelOnlineStatusArray::kTOFPulserBad)==(AliTOFChannelOnlineStatusArray::kTOFPulserBad)||(statusNoise & AliTOFChannelOnlineStatusArray::kTOFNoiseBad)==(AliTOFChannelOnlineStatusArray::kTOFNoiseBad)||(statusHW & AliTOFChannelOnlineStatusArray::kTOFHWBad)==(AliTOFChannelOnlineStatusArray::kTOFHWBad)){
1157       AliDebug(2, Form(" Bad Status for channel %d",index));
1158       fTofClusters[ii]->SetStatus(kFALSE); //odd convention, to avoid conflict with calibration objects currently in the db (temporary solution).
1159     }
1160     else {
1161       AliDebug(2, Form(" Good Status for channel %d",index));
1162     }
1163     // Get Rough channel online equalization 
1164     Double_t roughDelay=(Double_t)calDelay->GetDelay(index);  // in ns
1165     AliDebug(2,Form(" channel delay (ns) = %f", roughDelay));
1166     // Get Refined channel offline calibration parameters
1167     if (calibration ==1){
1168       AliTOFChannelOffline * calChannelOffline = (AliTOFChannelOffline*)calTOFArrayOffline->At(index);
1169       Double_t par[6];
1170       for (Int_t j = 0; j<6; j++){
1171         par[j]=(Double_t)calChannelOffline->GetSlewPar(j);
1172      } 
1173       AliDebug(2,Form(" Calib Pars = %f, %f, %f, %f, %f, %f ",par[0],par[1],par[2],par[3],par[4],par[5]));
1174       AliDebug(2,Form(" The ToT and Time, uncorr (counts) = %d , %d", fTofClusters[ii]->GetToT(),fTofClusters[ii]->GetTDC()));
1175       tToT = (Double_t)(fTofClusters[ii]->GetToT()*AliTOFGeometry::ToTBinWidth());    
1176       tToT*=1.E-3; //ToT in ns
1177
1178       /* check TOT limits and set new TOT in case */
1179       if (tToT < AliTOFGeometry::SlewTOTMin()) tToT = AliTOFGeometry::SlewTOTMin();
1180       if (tToT > AliTOFGeometry::SlewTOTMax()) tToT = AliTOFGeometry::SlewTOTMax();
1181
1182       AliDebug(2,Form(" The ToT and Time, uncorr (ns)= %e, %e",fTofClusters[ii]->GetTDC()*AliTOFGeometry::TdcBinWidth()*1.E-3,tToT));
1183       timeCorr=par[0]+par[1]*tToT+par[2]*tToT*tToT+par[3]*tToT*tToT*tToT+par[4]*tToT*tToT*tToT*tToT+par[5]*tToT*tToT*tToT*tToT*tToT; // the time correction (ns)
1184     }
1185     else {
1186       timeCorr = roughDelay; // correction in ns
1187     }
1188     AliDebug(2,Form(" The ToT and Time, uncorr (ns)= %e, %e",fTofClusters[ii]->GetTDC()*AliTOFGeometry::TdcBinWidth()*1.E-3,fTofClusters[ii]->GetToT()*AliTOFGeometry::ToTBinWidth()));
1189     AliDebug(2,Form(" The time correction (ns) = %f", timeCorr));
1190     timeCorr=(Double_t)(fTofClusters[ii]->GetTDC())*AliTOFGeometry::TdcBinWidth()*1.E-3-timeCorr;//redefine the time
1191     timeCorr*=1.E3;
1192     AliDebug(2,Form(" The channel time, corr (ps)= %e",timeCorr ));
1193
1194     /* here timeCorr should be already corrected for calibration. 
1195      * we now go into further corrections keeping in mind that timeCorr
1196      * is in ps.
1197      *
1198      * the following corrections are performed in this way:
1199      *
1200      *    time = timeRaw - deltaBC + L0L1Latency + CTPLatency - TDCLatencyWindow - T0
1201      *
1202      */
1203
1204     AliDebug(2, Form("applying further corrections (DeltaBC): DeltaBC=%d (BC bins), DeltaBCoffset=%d (BC bins)", fTofClusters[ii]->GetDeltaBC(), deltaBCOffset));
1205     AliDebug(2, Form("applying further corrections (L0L1Latency): L0L1Latency=%d (BC bins)", fTofClusters[ii]->GetL0L1Latency()));
1206     AliDebug(2, Form("applying further corrections (CTPLatency): CTPLatency=%f (ps)", ctpLatency));
1207     AliDebug(2, Form("applying further corrections (TDCLatencyWindow): TDCLatencyWindow=%f (ps)", tdcLatencyWindow));
1208     AliDebug(2, Form("applying further corrections (T0): T0=%f (ps)", t0));
1209
1210     /* deltaBC correction (inhibited for the time being) */
1211     //    timeCorr -= (fTofClusters[ii]->GetDeltaBC() - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
1212     /* L0L1-latency correction */
1213     timeCorr += fTofClusters[ii]->GetL0L1Latency() * AliTOFGeometry::BunchCrossingBinWidth();
1214     /* CTP-latency correction (from OCDB) */
1215     timeCorr += ctpLatency;
1216     /* TDC latency-window correction (from OCDB) */
1217     timeCorr -= tdcLatencyWindow;
1218     /* T0 correction (from OCDB) */
1219     timeCorr -= t0;
1220
1221     /*
1222      * end of furhter corrections
1223      */
1224
1225     tdcCorr=(Int_t)(timeCorr/AliTOFGeometry::TdcBinWidth()); //the corrected time (tdc counts)
1226     fTofClusters[ii]->SetTDC(tdcCorr);
1227   } // loop on clusters
1228
1229 }
1230 #endif
1231
1232 //_________________________________________________________________________
1233
1234 void AliTOFClusterFinder::CalibrateRecPoint(UInt_t timestamp)
1235 {
1236   //
1237   // Copy the global array of AliTOFcluster, i.e. fTofClusters (sorted
1238   // in Z) in the global TClonesArray of AliTOFcluster,
1239   // i.e. fRecPoints.
1240   //
1241
1242   Int_t detectorIndex[5];
1243   Double_t time, tot, corr;
1244   Int_t deltaBC, l0l1, tdcBin;
1245   for (Int_t ii = 0; ii < fNumberOfTofClusters; ii++) {
1246     for(Int_t jj = 0; jj < 5; jj++) detectorIndex[jj] = fTofClusters[ii]->GetDetInd(jj);
1247
1248     Int_t index = AliTOFGeometry::GetIndex(detectorIndex);
1249
1250     /* check channel enabled */
1251     if (!fTOFcalib->IsChannelEnabled(index)) fTofClusters[ii]->SetStatus(kFALSE);
1252     
1253     /* get cluster info */
1254     time = fTofClusters[ii]->GetTDC() * AliTOFGeometry::TdcBinWidth(); /* ps */
1255     tot = fTofClusters[ii]->GetToT() * AliTOFGeometry::ToTBinWidth() * 1.e-3; /* ns */
1256     deltaBC = fTofClusters[ii]->GetDeltaBC();
1257     l0l1 = fTofClusters[ii]->GetL0L1Latency();
1258
1259     /* get correction */
1260     corr = fTOFcalib->GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp); /* ps */
1261     AliDebug(2, Form("calibrate index %d: time=%f (ps) tot=%f (ns) deltaBC=%d l0l1=%d timestamp=%d corr=%f (ps)", index, time, tot, deltaBC, l0l1, timestamp, corr));
1262
1263     /* apply time correction */
1264     time -= corr;
1265
1266     /* convert in TDC bins and set cluster */
1267     tdcBin = (Int_t)(time / AliTOFGeometry::TdcBinWidth()); //the corrected time (tdc counts)
1268     fTofClusters[ii]->SetTDC(tdcBin);
1269
1270   } // loop on clusters
1271
1272 }
1273
1274 //______________________________________________________________________________
1275
1276 void AliTOFClusterFinder::ResetRecpoint()
1277 {
1278   //
1279   // Clear the list of reconstructed points
1280   //
1281
1282   fNumberOfTofClusters = 0;
1283   if (fRecPoints) fRecPoints->Clear();
1284
1285 }
1286 //______________________________________________________________________________
1287
1288 void AliTOFClusterFinder::Load()
1289 {
1290   //
1291   // Load TOF.Digits.root and TOF.RecPoints.root files
1292   //
1293
1294   fTOFLoader->LoadDigits("READ");
1295   fTOFLoader->LoadRecPoints("recreate");
1296
1297 }
1298 //______________________________________________________________________________
1299
1300 void AliTOFClusterFinder::LoadClusters()
1301 {
1302   //
1303   // Load TOF.RecPoints.root file
1304   //
1305
1306   fTOFLoader->LoadRecPoints("recreate");
1307
1308 }
1309 //______________________________________________________________________________
1310
1311 void AliTOFClusterFinder::UnLoad()
1312 {
1313   //
1314   // Unload TOF.Digits.root and TOF.RecPoints.root files
1315   //
1316
1317   fTOFLoader->UnloadDigits();
1318   fTOFLoader->UnloadRecPoints();
1319
1320 }
1321 //______________________________________________________________________________
1322
1323 void AliTOFClusterFinder::UnLoadClusters()
1324 {
1325   //
1326   // Unload TOF.RecPoints.root file
1327   //
1328
1329   fTOFLoader->UnloadRecPoints();
1330
1331 }
1332 //-------------------------------------------------------------------------
1333 UShort_t AliTOFClusterFinder::GetClusterVolIndex(const Int_t * const ind) const {
1334
1335   //First of all get the volume ID to retrieve the l2t transformation...
1336   //
1337   // Detector numbering scheme
1338   Int_t nSector = 18;
1339   Int_t nPlate  = 5;
1340   Int_t nStripA = 15;
1341   Int_t nStripB = 19;
1342   Int_t nStripC = 19;
1343
1344   Int_t isector =ind[0];
1345   if (isector >= nSector)
1346     AliError(Form("Wrong sector number in TOF (%d) !",isector));
1347   Int_t iplate = ind[1];
1348   if (iplate >= nPlate)
1349     AliError(Form("Wrong plate number in TOF (%d) !",iplate));
1350   Int_t istrip = ind[2];
1351
1352   Int_t stripOffset = 0;
1353   switch (iplate) {
1354   case 0:
1355     stripOffset = 0;
1356     break;
1357   case 1:
1358     stripOffset = nStripC;
1359     break;
1360   case 2:
1361     stripOffset = nStripC+nStripB;
1362     break;
1363   case 3:
1364     stripOffset = nStripC+nStripB+nStripA;
1365     break;
1366   case 4:
1367     stripOffset = nStripC+nStripB+nStripA+nStripB;
1368     break;
1369   default:
1370     AliError(Form("Wrong plate number in TOF (%d) !",iplate));
1371     break;
1372   };
1373
1374   Int_t index= (2*(nStripC+nStripB)+nStripA)*isector +
1375                stripOffset +
1376                istrip;
1377
1378   UShort_t volIndex = AliGeomManager::LayerToVolUID(AliGeomManager::kTOF,index);
1379   return volIndex;
1380 }
1381 //
1382 //-------------------------------------------------------------------------
1383 void AliTOFClusterFinder::GetClusterPars(Int_t *ind, Double_t* pos,Double_t* cov) const {
1384
1385   //First of all get the volume ID to retrieve the l2t transformation...
1386   //
1387   UShort_t volIndex = GetClusterVolIndex(ind);
1388   //
1389   //
1390   //we now go in the system of the strip: determine the local coordinates
1391   //
1392   //
1393   // 47---------------------------------------------------0  ^ z
1394   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 1 |
1395   // -----------------------------------------------------   | y going outwards
1396   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 0 |  par[0]=0;
1397
1398   // -----------------------------------------------------   |
1399   // x <-----------------------------------------------------
1400
1401   /*
1402   Float_t localX = (ind[4]-23.5)*AliTOFGeometry::XPad();
1403   Float_t localY = 0;
1404   Float_t localZ = (ind[3]- 0.5)*AliTOFGeometry::ZPad();
1405   */
1406   Float_t localX = (ind[4]-AliTOFGeometry::NpadX()/2)*AliTOFGeometry::XPad()+AliTOFGeometry::XPad()/2.;
1407   Float_t localY = 0;
1408   Float_t localZ = (ind[3]-AliTOFGeometry::NpadZ()/2)*AliTOFGeometry::ZPad()+AliTOFGeometry::ZPad()/2.;
1409   //move to the tracking ref system
1410
1411   Double_t lpos[3];
1412   lpos[0] = localX;
1413   lpos[1] = localY;
1414   lpos[2] = localZ; 
1415
1416   const TGeoHMatrix *l2t= AliGeomManager::GetTracking2LocalMatrix(volIndex);
1417   // Get The position in the track ref system
1418   Double_t tpos[3];
1419   l2t->MasterToLocal(lpos,tpos);
1420   pos[0] = tpos[0];
1421   pos[1] = tpos[1];
1422   pos[2] = tpos[2];
1423
1424   //Get The cluster covariance in the track ref system
1425   Double_t lcov[9];
1426
1427   //cluster covariance in the local system:
1428   // sx2   0   0
1429   // 0     0   0
1430   // 0     0   sz2
1431
1432   lcov[0] = AliTOFGeometry::XPad()*AliTOFGeometry::XPad()/12.;
1433   lcov[1] = 0;
1434   lcov[2] = 0;
1435   lcov[3] = 0;
1436   lcov[4] = 0;
1437   lcov[5] = 0;
1438   lcov[6] = 0;
1439   lcov[7] = 0;
1440   lcov[8] = AliTOFGeometry::ZPad()*AliTOFGeometry::ZPad()/12.;
1441
1442   //cluster covariance in the tracking system:
1443   TGeoHMatrix m;
1444   m.SetRotation(lcov);
1445   m.Multiply(l2t);
1446   m.MultiplyLeft(&l2t->Inverse());
1447   Double_t *tcov = m.GetRotationMatrix();
1448   cov[0] = tcov[0]; cov[1] = tcov[1]; cov[2] = tcov[2];
1449   cov[3] = tcov[4]; cov[4] = tcov[5];
1450   cov[5] = tcov[8];
1451
1452   return;
1453
1454 }