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