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