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