]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFClusterFinderV1.cxx
Changed class inheritance (TObject->TTask) and fixed bug
[u/mrichter/AliRoot.git] / TOF / AliTOFClusterFinderV1.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: AliTOFClusterFinderV1.cxx,v $
18 Revision 2009/04/20 A. De Caro
19     - added two new global variables, called fTOFGeometry and fTOFdigits;
20     - added a new method, called FindClustersWithoutTOT,
21       to transform TOF digits with fTOT=0 in one pad clusters;
22     - update of the covariance matrix elements for the TOF clusters
23
24 Revision 0.01  2008/05/10 A. De Caro
25  */
26
27 /////////////////////////////////////////
28 //                                     //
29 //  Class for TOF cluster finder (V1)  //
30 //                                     //
31 //  Input data: Raw Data or Digits;    //
32 //  Output data: Digits or Rec Points  //
33 //                                     //
34 /////////////////////////////////////////
35
36 #include "Riostream.h"
37
38 #include "TClonesArray.h"
39 #include "TStopwatch.h"
40 #include "TTree.h"
41 #include "TGeoMatrix.h"
42 #include "TString.h"
43
44 #include "AliDAQ.h"
45 #include "AliLog.h"
46 #include "AliRawReader.h"
47 #include "AliRunLoader.h"
48 #include "AliGeomManager.h"
49
50 #include "AliTOFcalib.h"
51 #include "AliTOFChannelOnlineArray.h"
52 #include "AliTOFChannelOnlineStatusArray.h"
53 #include "AliTOFChannelOffline.h"
54 #include "AliTOFClusterFinderV1.h"
55 #include "AliTOFcluster.h"
56 #include "AliTOFdigit.h"
57 #include "AliTOFDigitMap.h"
58 #include "AliTOFrawData.h"
59 #include "AliTOFReconstructor.h"
60 #include "AliTOFRecoParam.h"
61
62 ClassImp(AliTOFClusterFinderV1)
63
64 //_____________________________________________________________________________
65 AliTOFClusterFinderV1::AliTOFClusterFinderV1(AliTOFcalib *calib):
66   TTask("AliTOFClusterFinderV1",""),
67   fRunLoader(0),
68   fDigits(new TClonesArray("AliTOFdigit", 4000)),
69   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
70   fNumberOfTofClusters(0),
71   fNumberOfTofDigits(0),
72   fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
73   fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
74   fVerbose(0),
75   fDecoderVersion(0),
76   fTOFcalib(calib),
77   fTOFdigitMap(new AliTOFDigitMap()),
78   fTOFGeometry(new AliTOFGeometry()),
79   fTOFdigits(new TTree()),
80   fTOFRawStream(AliTOFRawStream()),
81   fCalibrateTOFtimes(1)
82 {
83 //
84 // Constructor
85 //
86
87   if (AliTOFReconstructor::GetRecoParam()) {
88     fkRecoParam = AliTOFReconstructor::GetRecoParam();
89     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
90   }
91   else
92     fMaxDeltaTime = 2;
93
94   TString validity = (TString)fTOFcalib->GetOfflineValidity();
95   if (validity.CompareTo("valid")==0) {
96     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
97   } else {
98     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
99   }
100
101 }
102
103 //_____________________________________________________________________________
104 AliTOFClusterFinderV1::AliTOFClusterFinderV1(AliRunLoader* runLoader, AliTOFcalib *calib):
105   TTask("AliTOFClusterFinderV1",""),
106   fRunLoader(runLoader),
107   fDigits(new TClonesArray("AliTOFdigit", 4000)),
108   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
109   fNumberOfTofClusters(0),
110   fNumberOfTofDigits(0),
111   fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
112   fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
113   fVerbose(0),
114   fDecoderVersion(0),
115   fTOFcalib(calib),
116   fTOFdigitMap(new AliTOFDigitMap()),
117   fTOFGeometry(new AliTOFGeometry()),
118   fTOFdigits(new TTree()),
119   fTOFRawStream(AliTOFRawStream()),
120   fCalibrateTOFtimes(1)
121 {
122 //
123 // Constructor
124 //
125
126   if (AliTOFReconstructor::GetRecoParam()) {
127     fkRecoParam = AliTOFReconstructor::GetRecoParam();
128     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
129   }
130   else
131     fMaxDeltaTime = 2;
132
133   TString validity = (TString)fTOFcalib->GetOfflineValidity();
134   if (validity.CompareTo("valid")==0) {
135     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
136   } else {
137     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
138   }
139
140 }
141 //_____________________________________________________________________________
142
143 AliTOFClusterFinderV1::AliTOFClusterFinderV1(const AliTOFClusterFinderV1 &source)
144   :TTask(source),
145    fRunLoader(0),
146    fDigits(source.fDigits),
147    fRecPoints(source.fRecPoints),
148    fNumberOfTofClusters(0),
149    fNumberOfTofDigits(0),
150    fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
151    fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
152    fVerbose(0),
153    fDecoderVersion(source.fDecoderVersion),
154    fTOFcalib(source.fTOFcalib),
155    fTOFdigitMap(new AliTOFDigitMap()),
156    fTOFGeometry(new AliTOFGeometry()),
157    fTOFdigits(source.fTOFdigits),
158    fTOFRawStream(source.fTOFRawStream),
159    fCalibrateTOFtimes(1)
160 {
161   // copy constructor
162
163   if (AliTOFReconstructor::GetRecoParam()) {
164     fkRecoParam = AliTOFReconstructor::GetRecoParam();
165     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
166   }
167   else
168     fMaxDeltaTime = 2;
169
170 }
171 //_____________________________________________________________________________
172
173 AliTOFClusterFinderV1& AliTOFClusterFinderV1::operator=(const AliTOFClusterFinderV1 &source)
174 {
175   // ass. op.
176
177   if (this == &source)
178     return *this;
179
180   TObject::operator=(source);
181   fDigits=source.fDigits;
182   fRecPoints=source.fRecPoints;
183   fVerbose=source.fVerbose;
184   fDecoderVersion=source.fDecoderVersion;
185   fTOFcalib=source.fTOFcalib;
186   fTOFdigitMap=source.fTOFdigitMap;
187   fTOFGeometry=source.fTOFGeometry;
188   fTOFdigits=source.fTOFdigits;
189   fTOFRawStream=source.fTOFRawStream;
190   fCalibrateTOFtimes=source.fCalibrateTOFtimes;
191   return *this;
192
193 }
194 //_____________________________________________________________________________
195
196 AliTOFClusterFinderV1::~AliTOFClusterFinderV1()
197 {
198
199   //
200   // Destructor
201   //
202
203   if (fDigits)
204     {
205       fDigits->Delete();
206       delete fDigits;
207       fDigits=0;
208     }
209   if (fRecPoints)
210     {
211       fRecPoints->Delete();
212       delete fRecPoints;
213       fRecPoints=0;
214     }
215
216   delete fTOFdigitMap;
217
218   delete fTOFGeometry;
219
220   delete fTOFdigits;
221
222   if (fTofClusters || fNumberOfTofClusters) {
223     for (Int_t ii=0; ii<fNumberOfTofClusters; ii++)
224       if (fTofClusters[ii]) fTofClusters[ii]->Delete();
225     fNumberOfTofClusters = 0;
226   }
227
228 }
229 //_____________________________________________________________________________
230
231 void AliTOFClusterFinderV1::Digits2RecPoints(TTree* digitsTree, TTree* clusterTree)
232 {
233   //
234   // Converts digits to recPoints for TOF
235   //
236
237   TStopwatch stopwatch;
238   stopwatch.Start();
239
240   Int_t inholes = 0;
241
242   fDigits->Clear();
243   TClonesArray &aDigits = *fDigits;
244
245   if (digitsTree == 0x0)
246     AliFatal("Can not get TreeD for TOF");
247
248   TBranch *branch = digitsTree->GetBranch("TOF");
249   if (!branch) {
250     AliError("Can not get branch with the TOF digits !");
251     return;
252   }
253
254   TClonesArray staticDigits("AliTOFdigit",10000);
255   staticDigits.Clear();
256   TClonesArray *digits = &staticDigits;
257   branch->SetAddress(&digits);
258   digitsTree->GetEvent(0);
259   AliDebug(1,Form("Number of TOF digits: %d", digits->GetEntriesFast()));
260
261   AliTOFdigit *tofDigit;
262
263   Int_t jj = 0;
264   Int_t detectorIndex[5];
265   for (jj=0; jj<5; jj++) detectorIndex[jj] = -1;
266   Int_t info[4];
267   for (jj=0; jj<4; jj++) info[jj] = -1;
268   Int_t *tracks;
269   Int_t tdcCorr;
270   Int_t dummy = -1;
271   Int_t last = -1;
272
273   Bool_t status = kTRUE;
274
275   AliDebug(1," Calibrating TOF Digits");
276   /*
277   TString validity = (TString)fTOFcalib->GetOfflineValidity();
278   if (validity.CompareTo("valid")==0) {
279     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
280   } else
281     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
282   */
283
284   Int_t ii = 0;
285   for (ii=0; ii<digits->GetEntriesFast(); ii++) {
286     tofDigit = (AliTOFdigit*)digits->UncheckedAt(ii);
287     detectorIndex[0] = tofDigit->GetSector();
288     detectorIndex[1] = tofDigit->GetPlate();
289     detectorIndex[2] = tofDigit->GetStrip();
290     detectorIndex[3] = tofDigit->GetPadz();
291     detectorIndex[4] = tofDigit->GetPadx();
292
293     if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
294       if (detectorIndex[1]==2) { // plate with holes
295         inholes++;
296         continue;
297       }
298     }
299
300     tdcCorr = tofDigit->GetTdc();
301     status = MakeSlewingCorrection(detectorIndex, tofDigit->GetToT(), tofDigit->GetTdc(), tdcCorr);
302
303     for (jj=0; jj<4; jj++) info[jj] = -1;
304     info[0] = tdcCorr;//tofDigit->GetTdc();
305     info[1] = tofDigit->GetAdc();
306     info[2] = tofDigit->GetToT();
307     info[3] = tofDigit->GetTdcND();//tofDigit->GetTdc();//
308     tracks  = tofDigit->GetTracks();
309
310     dummy = detectorIndex[3];
311     detectorIndex[3] = detectorIndex[4];//padx
312     detectorIndex[4] = dummy;//padz
313     last = fDigits->GetEntriesFast();
314     new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, info);
315     if (status) fTOFdigitMap->AddDigit(detectorIndex, last);
316
317     AliDebug(2, Form(" Digits reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
318                      last,
319                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[3], detectorIndex[4],
320                      info[0], info[1], info[3]));
321
322   }
323
324   fNumberOfTofDigits = fDigits->GetEntriesFast();
325
326   ResetRecpoint();
327
328   Int_t bufsize = 32000;
329   clusterTree->Branch("TOF", &fRecPoints, bufsize);
330
331   FillRecPoint();
332   clusterTree->Fill();
333
334   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
335
336   ResetRecpoint();
337
338   fTOFdigitMap->Clear();
339
340   ResetDigits();
341
342   AliDebug(1,Form("Execution time to read TOF digits and to write TOF clusters : R:%.4fs C:%.4fs",
343                   stopwatch.RealTime(),stopwatch.CpuTime()));
344
345   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
346
347 }
348 //_____________________________________________________________________________
349
350 void AliTOFClusterFinderV1::Digits2RecPoints(AliRawReader *rawReader, TTree *clustersTree)
351 {
352   //
353   // Converts raw data to recPoints for TOF
354   //
355
356   TStopwatch stopwatch;
357   stopwatch.Start();
358
359
360   AliDebug(2, "TreeD re-creation");
361   //TTree *digitsTree = new TTree();
362   //Raw2Digits(rawReader, digitsTree);
363
364   Raw2Digits(rawReader, fTOFdigits);
365
366   AliDebug(1,Form("Number of TOF digits: %d", fNumberOfTofDigits));
367   ResetRecpoint();
368
369   Int_t bufsize = 32000;
370   clustersTree->Branch("TOF", &fRecPoints, bufsize);
371   FillRecPoint();
372
373   clustersTree->Fill();
374
375   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
376
377   ResetRecpoint();
378
379   fTOFdigitMap->Clear();
380
381   ResetDigits();
382
383   AliDebug(1,Form("Execution time to read TOF raw data and to write TOF clusters : R:%.4fs C:%.4fs",
384                   stopwatch.RealTime(),stopwatch.CpuTime()));
385
386 }
387
388 //_____________________________________________________________________________
389
390 void AliTOFClusterFinderV1::Raw2Digits(AliRawReader *rawReader, TTree* digitsTree)
391 {
392   //
393   // Converts raw data to digits for TOF
394   //
395
396   TStopwatch stopwatch;
397   stopwatch.Start();
398
399   Int_t inholes = 0;
400
401   const Int_t kMaxNumberOfTracksPerDigit = 3;
402   const Int_t kDDL = AliDAQ::NumberOfDdls("TOF");
403
404   digitsTree->Branch("TOF", &fDigits);
405   TClonesArray &aDigits = *fDigits;
406
407   fTOFRawStream.Clear();
408   fTOFRawStream.SetRawReader(rawReader);
409
410   ofstream ftxt;
411   if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
412
413   TClonesArray staticRawData("AliTOFrawData",10000);
414   staticRawData.Clear();
415   TClonesArray * clonesRawData = &staticRawData;
416
417   Int_t dummy = -1;
418   Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
419   Int_t digit[4];
420   Int_t tracks[kMaxNumberOfTracksPerDigit];
421   for (Int_t ii=0; ii<kMaxNumberOfTracksPerDigit; ii++)
422     tracks[ii] = -1;
423   Int_t last = -1;
424   Int_t tdcCorr = 0;
425
426   Bool_t status = kTRUE;
427
428   /*
429   TString validity = (TString)fTOFcalib->GetOfflineValidity();
430   if (validity.CompareTo("valid")==0) {
431     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
432   } else
433     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
434   */
435
436   if (fDecoderVersion)
437     AliInfo("Using New Decoder");
438
439   Int_t indexDDL = 0;
440   Int_t iRawData = 0;
441   for (indexDDL=0; indexDDL<kDDL; indexDDL++) {
442
443     rawReader->Reset();
444     if (fDecoderVersion)
445       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
446     else fTOFRawStream.LoadRawData(indexDDL);
447
448     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
449     if (clonesRawData->GetEntriesFast()!=0) AliDebug(2,Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
450     for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
451
452       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
453
454       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
455       if (tofRawDatum->GetTOF()==-1) continue;
456
457       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
458                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
459
460       tdcCorr = 0;
461       dummy = detectorIndex[3];
462       detectorIndex[3] = detectorIndex[4];//padz
463       detectorIndex[4] = dummy;//padx
464
465       tdcCorr = tofRawDatum->GetTOF();
466       status = MakeSlewingCorrection(detectorIndex, tofRawDatum->GetTOT(), tofRawDatum->GetTOF(), tdcCorr);
467
468       digit[0] = tdcCorr;
469       digit[1] = tofRawDatum->GetTOT();
470       digit[2] = tofRawDatum->GetTOT();
471       digit[3] = -1;//tofRawDatum->GetTOF(); //tofND
472
473       dummy = detectorIndex[3];
474       detectorIndex[3] = detectorIndex[4];//padx
475       detectorIndex[4] = dummy;//padz
476
477       /* check valid index */
478       if (detectorIndex[0]==-1||detectorIndex[1]==-1||detectorIndex[2]==-1||detectorIndex[3]==-1||detectorIndex[4]==-1) continue;
479
480       // Do not reconstruct anything in the holes
481       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
482         if (detectorIndex[1]==2) { // plate with holes
483           inholes++;
484           continue;
485         }
486       }
487
488       last = fDigits->GetEntriesFast();
489       new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, digit);
490       if (status) fTOFdigitMap->AddDigit(detectorIndex, last);
491
492       if (fVerbose==2) {
493         if (indexDDL<10) ftxt << "  " << indexDDL;
494         else         ftxt << " " << indexDDL;
495         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
496         else         ftxt << " " << tofRawDatum->GetTRM();
497         ftxt << "  " << tofRawDatum->GetTRMchain();
498         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
499         else         ftxt << " " << tofRawDatum->GetTDC();
500         ftxt << "  " << tofRawDatum->GetTDCchannel();
501
502         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
503         else              ftxt  << "  -> " << detectorIndex[0];
504         ftxt << "  " << detectorIndex[1];
505         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
506         else              ftxt << " " << detectorIndex[2];
507         ftxt << "  " << detectorIndex[4];
508         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
509         else              ftxt << " " << detectorIndex[3];
510
511         if (digit[1]<10)ftxt << "        " << digit[1];
512         else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
513         else ftxt << "      " << digit[1];
514         if (digit[0]<10) ftxt << "      " << digit[0] << endl;
515         else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
516         else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
517         else ftxt << "   " << digit[3] << endl;
518       }
519
520       AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
521                        last,
522                        detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
523                        digit[0], digit[1], digit[3]));
524
525     } // while loop
526
527     clonesRawData->Clear();
528
529   } // DDL Loop
530
531   if (fVerbose==2) ftxt.close();
532
533   digitsTree->Fill();
534
535   fNumberOfTofDigits = fDigits->GetEntries();
536
537   AliDebug(1, Form("Got %d TOF digits", fNumberOfTofDigits));
538   AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
539                    stopwatch.RealTime(),stopwatch.CpuTime()));
540
541   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
542
543 }
544 //_____________________________________________________________________________
545
546 void AliTOFClusterFinderV1::FillRecPoint()
547 {
548   //
549   // Fill the global TClonesArray of AliTOFcluster objects,
550   // i.e. fRecPoints
551   //
552
553   Int_t dummy4 = -1;
554   Int_t dummy3 = -1;
555   Int_t dummy2 = -1;
556   Int_t dummy  = -1;
557
558   for(Int_t iPlate=AliTOFGeometry::NPlates()-1; iPlate>=0; iPlate--) {
559     for(Int_t iStrip=AliTOFGeometry::NStrip(iPlate)-1; iStrip>=0; iStrip--) {
560       //for (Int_t iSector=AliTOFGeometry::NSectors()-1; iSector>=0; iSector--) {
561       for (Int_t iSector=0; iSector<AliTOFGeometry::NSectors(); iSector++) {
562
563
564         if (fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))
565           AliDebug(1,Form(" Number of TOF digits in (%2d,%1d,%2d) -> %d",
566                           iSector,iPlate,iStrip,fTOFdigitMap->FilledCellsInStrip(iSector,iPlate,iStrip)));
567
568         if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
569         FindClustersWithoutTOT(iSector, iPlate, iStrip); // clusters coming from digits without TOT measurement
570
571         if (fMaxDeltaTime>0) {
572
573           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
574           //if (fTOFdigitMap->FilledCellsInStrip(iSector,iPlate,iStrip)>=4)
575           FindClustersPerStrip(iSector, iPlate, iStrip, 4); // 4 pads clusters
576           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
577
578           dummy4 = fNumberOfTofClusters;
579           FindClustersPerStrip(iSector, iPlate, iStrip, 4); // 4 pads clusters
580           if (fNumberOfTofClusters!=dummy4)
581             AliDebug(2, Form(" (4): n1= %5d, n2 = %5", dummy4, fNumberOfTofClusters));
582
583
584           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
585           FindClustersPerStrip(iSector, iPlate, iStrip, 3); // 3 pads clusters
586           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
587
588           dummy3 = fNumberOfTofClusters;
589           FindClustersPerStrip(iSector, iPlate, iStrip, 3); // 3 pads clusters
590           if (fNumberOfTofClusters!=dummy3)
591             AliDebug(2, Form(" (3): n1= %5d, n2 = %5", dummy3, fNumberOfTofClusters));
592
593
594           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
595           FindClustersPerStrip(iSector, iPlate, iStrip, 2); // 2 pads clusters
596           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
597
598           dummy2 = fNumberOfTofClusters;
599           FindClustersPerStrip(iSector, iPlate, iStrip, 2); // 2 pads clusters
600           if (fNumberOfTofClusters!=dummy2)
601             AliDebug(2, Form(" (2): n1= %5d, n2 =%5", dummy2, fNumberOfTofClusters));
602
603
604           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
605           dummy = fNumberOfTofClusters;
606           FindClusters34(iSector, iPlate, iStrip); // 3 pads clusters between 4 hit pads
607           if (fNumberOfTofClusters!=dummy)
608             AliDebug(2, Form(" (3 between 4): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
609
610
611           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
612           dummy = fNumberOfTofClusters;
613           FindClusters23(iSector, iPlate, iStrip); // 2 pads clusters between 3 hit pads
614           if (fNumberOfTofClusters!=dummy)
615             AliDebug(2, Form(" (2 between 3): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
616
617           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
618           dummy = fNumberOfTofClusters;
619           FindClusters24(iSector, iPlate, iStrip); // 2 pads clusters between 4 hit pads
620           if (fNumberOfTofClusters!=dummy)
621             AliDebug(2, Form(" (2 between 4): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
622
623
624           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
625           dummy = fNumberOfTofClusters;
626           FindOnePadClusterPerStrip(iSector, iPlate, iStrip); // 1 pad clusters
627           if (fNumberOfTofClusters!=dummy)
628             AliDebug(2,Form(" (1): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
629
630           if (fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)>0)
631             AliDebug(2, Form(" (1): number of clusters = %5d (remaining digit %2d), -%2d %1d %2d-",
632                              fNumberOfTofClusters, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip),
633                              iSector, iPlate, iStrip));
634
635         }
636         else {
637           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
638           dummy = fNumberOfTofClusters;
639           FindOnePadClusterPerStrip(iSector, iPlate, iStrip); // 1 pad clusters
640           if (fNumberOfTofClusters!=dummy)
641             AliDebug(2,Form(" (1): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
642
643           if (fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)>0)
644             AliDebug(2, Form(" (1): number of clusters = %5d (remaining digit %2d), -%2d %1d %2d-",
645                              fNumberOfTofClusters, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip),
646                              iSector, iPlate, iStrip));
647
648         }
649
650
651       }
652     }
653   }
654
655
656   TClonesArray &lRecPoints = *fRecPoints;
657   
658   Int_t ii, jj;
659
660   Int_t detectorIndex[5];
661   for (jj=0; jj<5; jj++) detectorIndex[jj] = -1;
662   Int_t parTOF[5];
663   for (jj=0; jj<5; jj++) parTOF[jj] = -1;
664   Int_t trackLabels[3];
665   for (jj=0; jj<3; jj++) trackLabels[jj] = -1;
666   Int_t digitIndex = -1;
667   Bool_t status = kTRUE;
668   Float_t posClus[3];
669   for (ii=0; ii<3; ii++) posClus[ii] = 0.;
670   //Float_t covClus[6];
671   //for (ii=0; ii<6; ii++) covClus[ii] = 0.;
672   UShort_t volIdClus;
673
674   for (ii=0; ii<fNumberOfTofClusters; ii++) {
675
676     digitIndex = fTofClusters[ii]->GetIndex();
677     for(jj=0; jj<5; jj++) detectorIndex[jj] = fTofClusters[ii]->GetDetInd(jj);
678     volIdClus = fTOFGeometry->GetAliSensVolIndex(detectorIndex[0],detectorIndex[1],detectorIndex[2]);
679     //volIdClus = GetClusterVolIndex(detectorIndex);
680     for(jj=0; jj<3; jj++) trackLabels[jj] = fTofClusters[ii]->GetLabel(jj);
681     parTOF[0] = fTofClusters[ii]->GetTDC(); // TDC
682     parTOF[1] = fTofClusters[ii]->GetToT(); // TOT
683     parTOF[2] = fTofClusters[ii]->GetADC(); // ADC=TOT
684     parTOF[3] = fTofClusters[ii]->GetTDCND(); // TDCND
685     parTOF[4] = fTofClusters[ii]->GetTDCRAW();//RAW
686     status = fTofClusters[ii]->GetStatus();
687
688     posClus[0] = fTofClusters[ii]->GetX();
689     posClus[1] = fTofClusters[ii]->GetY();
690     posClus[2] = fTofClusters[ii]->GetZ();
691
692     //for (jj=0; jj<6; jj++) covClus[jj] = 0.;
693     //((AliCluster*)fTofClusters[ii])->GetGlobalCov(covClus);
694
695     new(lRecPoints[ii]) AliTOFcluster(volIdClus, (Double_t)posClus[0], (Double_t)posClus[1], (Double_t)posClus[2],
696                                       (Double_t)(fTofClusters[ii]->GetSigmaX2()),
697                                       (Double_t)(fTofClusters[ii]->GetSigmaXY()),
698                                       (Double_t)(fTofClusters[ii]->GetSigmaXZ()),
699                                       (Double_t)(fTofClusters[ii]->GetSigmaY2()),
700                                       (Double_t)(fTofClusters[ii]->GetSigmaYZ()),
701                                       (Double_t)(fTofClusters[ii]->GetSigmaZ2()),
702                                       //(Double_t)covClus[0], (Double_t)covClus[1], (Double_t)covClus[2],
703                                       //(Double_t)covClus[3], (Double_t)covClus[4], (Double_t)covClus[5],
704                                       trackLabels, detectorIndex, parTOF, status, digitIndex);
705
706     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", 
707                      ii, volIdClus, posClus[0], posClus[1], posClus[2],
708                      fTofClusters[ii]->GetSigmaX2(),
709                      fTofClusters[ii]->GetSigmaXY(),
710                      fTofClusters[ii]->GetSigmaXZ(),
711                      fTofClusters[ii]->GetSigmaY2(),
712                      fTofClusters[ii]->GetSigmaYZ(),
713                      fTofClusters[ii]->GetSigmaZ2(),
714                      trackLabels[0], trackLabels[1], trackLabels[2],
715                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[3], detectorIndex[4],
716                      parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
717                      status, digitIndex));
718
719
720   }
721
722   for (Int_t iSector=0; iSector<AliTOFGeometry::NSectors(); iSector++)
723     for(Int_t iPlate=0; iPlate<AliTOFGeometry::NPlates(); iPlate++) {
724       for(Int_t iStrip=0; iStrip<AliTOFGeometry::NStrip(iPlate); iStrip++) {
725         if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
726         AliDebug(2, Form(" END %2d %1d %2d   %5d",
727                          iSector, iPlate, iStrip, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)));
728       }
729     }
730
731 }
732 //_____________________________________________________________________________
733
734 void AliTOFClusterFinderV1::FindOnePadClusterPerStrip(Int_t nSector,
735                                                       Int_t nPlate,
736                                                       Int_t nStrip)
737 {
738   //
739   // This function searches the isolated digits (stored in the fDigits object),
740   // to perform clusters (stored in the fTofClusters array).
741   // This research has been made by checking the fTOFdigitMap object,
742   // filled at digits/raw-data reading time.
743   //
744
745   const Int_t kMaxNumberOfTracksPerDigit = 3;
746   const Int_t kMaxNumberOfDigitsPerVolume = 10;
747
748   Int_t jj = 0;
749
750   Int_t det[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padZ,padX
751   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padX,padZ
752   UShort_t volIdClus = 0;
753
754   Float_t pos[3];
755   for (jj=0; jj<3; jj++) pos[jj] = 0.;
756   Double_t posClus[3];
757   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
758
759   Double_t covClus[6];
760   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
761
762   Int_t parTOF[5];
763   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
764
765   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
766
767   Int_t tracks[kMaxNumberOfTracksPerDigit];
768   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
769
770   Int_t dummyCounter=-1;
771
772   AliTOFdigit *digitInteresting;
773
774   Int_t iPadX = -1;
775   Int_t iPadZ = -1;
776   for (iPadX=0; iPadX<AliTOFGeometry::NpadX(); iPadX++) {
777     for (iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++) {
778       vol[4] = iPadZ  , vol[3]  = iPadX;
779
780       AliDebug(3, Form(" %1d %2d\n", iPadZ, iPadX));
781
782       if (fTOFdigitMap->GetNumberOfDigits(vol)==0) continue;
783
784       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
785         if (fTOFdigitMap->GetDigitIndex(vol,digIndex)<0) continue;
786         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(fTOFdigitMap->GetDigitIndex(vol,digIndex));
787
788         AliDebug(2, Form(" %3d  %5d    %2d %1d %2d %1d %2d  %d %d %d  %5d  %5d %5d %5d",
789                          fTOFdigitMap->GetNumberOfDigits(vol), digIndex,
790                          vol[0], vol[1], vol[2] ,vol[4], vol[3],
791                          digitInteresting->GetTdc(), digitInteresting->GetAdc(),
792                          digitInteresting->GetToT(),
793                          fTOFdigitMap->GetDigitIndex(vol,digIndex),
794                          digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
795         
796         det[3] = vol[4]; // padz
797         det[4] = vol[3]; // padx
798         fTOFGeometry->GetPosPar(det,pos);
799         AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
800
801         //insert cluster
802         for (jj=0; jj<3; jj++) posClus[jj] = pos[jj];
803
804         parTOF[0] = Int_t(digitInteresting->GetTdc());
805         parTOF[1] = Int_t(digitInteresting->GetToT());
806         parTOF[2] = Int_t(digitInteresting->GetAdc());
807         parTOF[3] = Int_t(digitInteresting->GetTdcND());
808         parTOF[4] = Int_t(digitInteresting->GetTdc());
809
810         volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
811         //volIdClus = GetClusterVolIndex(det);
812
813         for (jj=0; jj<6; jj++) covClus[jj] = 0.;
814         GetClusterPars(det, posClus, covClus);
815
816         // To fill the track index array
817         dummyCounter=-1;
818         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
819         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) { // three is the max number of tracks associated to one digit
820           if (digitInteresting->GetTrack(jj)==-1) continue;
821           else {
822             dummyCounter++;
823             tracks[dummyCounter] = digitInteresting->GetTrack(jj);
824           }
825         }
826
827         AliTOFcluster *tofCluster =
828           new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
829                             covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
830                             tracks, det, parTOF, status, fTOFdigitMap->GetDigitIndex(vol,digIndex));
831         InsertCluster(tofCluster);
832
833         AliDebug(2, Form("       %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", 
834                          volIdClus, posClus[0], posClus[1], posClus[2],
835                          covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
836                          tracks[0], tracks[1], tracks[2],
837                          det[0], det[1], det[2], det[3], det[4],
838                          parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
839                          status, fTOFdigitMap->GetDigitIndex(vol,digIndex)));
840
841         AliDebug(2, Form("        %f %f %f", pos[0], pos[1], pos[2]));
842         AliDebug(2, Form("           %d %d", parTOF[0], parTOF[2]));
843
844         fTOFdigitMap->ResetDigit(vol, digIndex);
845
846       }
847
848     }
849   }
850
851 }
852 //_____________________________________________________________________________
853
854 void AliTOFClusterFinderV1::FindClustersWithoutTOT(Int_t nSector,
855                                                    Int_t nPlate,
856                                                    Int_t nStrip)
857 {
858   //
859   // This function searches the isolated digits without TOT
860   // measurement (stored in the fDigits object), to perform clusters
861   // (stored in the fTofClusters array). This research has been made
862   // by checking the fTOFdigitMap object, filled at digits/raw-data
863   // reading time.
864   //
865
866   const Int_t kMaxNumberOfTracksPerDigit = 3;
867   const Int_t kMaxNumberOfDigitsPerVolume = 10;
868
869   Int_t jj = 0;
870
871   Int_t det[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padZ,padX
872   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padX,padZ
873   UShort_t volIdClus = 0;
874
875   Float_t pos[3];
876   for (jj=0; jj<3; jj++) pos[jj] = 0.;
877   Double_t posClus[3];
878   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
879
880   Double_t covClus[6];
881   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
882
883   Int_t parTOF[5];
884   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
885
886   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
887   Int_t tracks[kMaxNumberOfTracksPerDigit];
888   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
889
890   Int_t dummyCounter=-1;
891
892   AliTOFdigit *digitInteresting;
893
894   Int_t iPadX = -1;
895   Int_t iPadZ = -1;
896   for (iPadX=0; iPadX<AliTOFGeometry::NpadX(); iPadX++) {
897     for (iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++) {
898       vol[4] = iPadZ  , vol[3]  = iPadX;
899
900       AliDebug(3, Form(" %1d %2d\n", iPadZ, iPadX));
901
902       if (fTOFdigitMap->GetNumberOfDigits(vol)==0) continue;
903
904       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
905         if (fTOFdigitMap->GetDigitIndex(vol,digIndex)<0) continue;
906         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(fTOFdigitMap->GetDigitIndex(vol,digIndex));
907         if (digitInteresting->GetToT()>0) continue; // AdC
908
909         AliDebug(2, Form(" %3d  %5d    %2d %1d %2d %1d %2d  %d %d %d  %5d  %5d %5d %5d",
910                          fTOFdigitMap->GetNumberOfDigits(vol), digIndex,
911                          vol[0], vol[1], vol[2] ,vol[4], vol[3],
912                          digitInteresting->GetTdc(), digitInteresting->GetAdc(),
913                          digitInteresting->GetToT(),
914                          fTOFdigitMap->GetDigitIndex(vol,digIndex),
915                          digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
916         
917         det[3] = vol[4]; // padz
918         det[4] = vol[3]; // padx
919         fTOFGeometry->GetPosPar(det,pos);
920         AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
921
922         //insert cluster
923         for (jj=0; jj<3; jj++) posClus[jj] = pos[jj];
924
925         parTOF[0] = Int_t(digitInteresting->GetTdc());
926         parTOF[1] = Int_t(digitInteresting->GetToT());
927         parTOF[2] = Int_t(digitInteresting->GetAdc());
928         parTOF[3] = Int_t(digitInteresting->GetTdcND());
929         parTOF[4] = Int_t(digitInteresting->GetTdc());
930
931         volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
932         //volIdClus = GetClusterVolIndex(det);
933
934         for (jj=0; jj<6; jj++) covClus[jj] = 0.;
935         GetClusterPars(det, posClus, covClus);
936
937         // To fill the track index array
938         dummyCounter=-1;
939         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
940         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) { // three is the max number of tracks associated to one digit
941           if (digitInteresting->GetTrack(jj)==-1) continue;
942           else {
943             dummyCounter++;
944             tracks[dummyCounter] = digitInteresting->GetTrack(jj);
945           }
946         }
947
948         AliTOFcluster *tofCluster =
949           new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
950                             covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
951                             tracks, det, parTOF, status, fTOFdigitMap->GetDigitIndex(vol,digIndex));
952         InsertCluster(tofCluster);
953
954         AliDebug(2, Form("       %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", 
955                          volIdClus, posClus[0], posClus[1], posClus[2],
956                          covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
957                          tracks[0], tracks[1], tracks[2],
958                          det[0], det[1], det[2], det[3], det[4],
959                          parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
960                          status, fTOFdigitMap->GetDigitIndex(vol,digIndex)));
961
962         AliDebug(2, Form("        %f %f %f", pos[0], pos[1], pos[2]));
963         AliDebug(2, Form("           %d %d", parTOF[0], parTOF[2]));
964
965         fTOFdigitMap->ResetDigit(vol, digIndex);
966
967       }
968
969     }
970   }
971
972 }
973 //_____________________________________________________________________________
974
975 void AliTOFClusterFinderV1::FindClusters34(Int_t nSector,
976                                            Int_t nPlate,
977                                            Int_t nStrip)
978 {
979   //
980   // This function searches the neighbouring digits (stored in the fDigits object),
981   // to perform clusters (stored in the fTofClusters array).
982   //
983   // This research has been made by checking the fTOFdigitMap object,
984   // filled at digits/raw-data reading time.
985   //
986
987   const Int_t kMaxNumberOfInterestingPads = 4;
988   const Int_t kMaxNumberOfTracksPerDigit = 3;
989   const Int_t kMaxNumberOfDigitsPerVolume = 10;
990
991   Int_t ii = 0;
992
993   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
994   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
995     digitsInVolumeIndices[ii] = -1;
996
997   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
998
999   Float_t pos[3] = {0.,0.,0.};
1000
1001   Int_t jj = 0;
1002   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1003   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1004   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1005   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1006   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1007   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1008   Double_t interestingADC[kMaxNumberOfInterestingPads];
1009   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1010   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1011   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1012   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1013   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1014
1015   Float_t interestingX[kMaxNumberOfInterestingPads];
1016   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1017   Float_t interestingY[kMaxNumberOfInterestingPads];
1018   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1019   Float_t interestingZ[kMaxNumberOfInterestingPads];
1020   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1021
1022   Float_t interDigit[kMaxNumberOfInterestingPads];
1023   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1024
1025   Int_t padsCluster[11];
1026   padsCluster[0] = nSector;
1027   padsCluster[1] = nPlate;
1028   padsCluster[2] = nStrip;
1029   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1030
1031   Int_t interestingCounter=-1;
1032   Int_t  digitIndexLocal=-1; // AdC
1033   Int_t iPad  = -1;
1034   Int_t iPadX = -1;
1035   Int_t iPadZ = -1;
1036
1037   Int_t parTOF[5];
1038   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
1039   Double_t posClus[3];
1040   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1041   Int_t det[5];
1042   for (jj=0; jj<5; jj++) det[jj] = -1;
1043   Float_t posF[3];
1044   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1045   UShort_t volIdClus = 0;
1046   Bool_t check = kFALSE;
1047   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1048   Double_t covClus[6];
1049   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1050   Int_t tracks[kMaxNumberOfTracksPerDigit];
1051   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1052   Int_t dummyCounter=-1;
1053   Bool_t alreadyStored = kFALSE;
1054
1055   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1056   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1057     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1058
1059   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1060     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1061
1062   AliTOFdigit *digitInteresting;
1063
1064   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1065
1066     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1067     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1068
1069     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1070
1071
1072
1073
1074
1075
1076     interestingCounter=-1;
1077
1078     vol[4] = iPadZ  , vol[3]  = iPadX;
1079     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1080       interestingCounter++;
1081
1082     vol[4] = iPadZ, vol[3] = iPadX+1;
1083     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1084       interestingCounter++;
1085
1086     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1087     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1088       interestingCounter++;
1089
1090     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1091     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1092       interestingCounter++;
1093
1094     if (interestingCounter+1!=4) continue; // the hit pads have to be 4
1095     else interestingCounter=-1;
1096
1097
1098     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1099       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1100         selectedDigit[ii][jj] = 0x0;
1101
1102
1103     vol[4] = iPadZ, vol[3] = iPadX;
1104
1105     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1106       interestingCounter++;
1107       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1108         digitsInVolumeIndices[ii] = -1;
1109       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1110       digitIndexLocal=-1; // AdC
1111       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1112         if (digitsInVolumeIndices[digIndex]<0) continue;
1113         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1114         if (digitInteresting->GetToT()<=0) continue; // AdC
1115         digitIndexLocal++; // AdC
1116
1117         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1118                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1119                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1120                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1121                         digitsInVolumeIndices[digIndex],
1122                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1123
1124
1125         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1126           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1127                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1128                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1129                               digitsInVolumeIndices[digIndex],
1130                               digitInteresting->GetTracks());
1131       }
1132       if (digitIndexLocal==-1) interestingCounter--; // AdC
1133     }
1134
1135
1136     vol[4] = iPadZ, vol[3] = iPadX+1;
1137
1138     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1139       interestingCounter++;
1140       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1141         digitsInVolumeIndices[ii] = -1;
1142       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1143       digitIndexLocal=-1; // AdC
1144       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1145         if (digitsInVolumeIndices[digIndex]<0) continue;
1146         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1147         if (digitInteresting->GetToT()<=0) continue; // AdC
1148         digitIndexLocal++; // AdC
1149
1150         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1151                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1152                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1153                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1154                         digitsInVolumeIndices[digIndex],
1155                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1156
1157
1158         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1159           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1160                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1161                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1162                               digitsInVolumeIndices[digIndex],
1163                               digitInteresting->GetTracks());
1164       }
1165       if (digitIndexLocal==-1) interestingCounter--; // AdC
1166     }
1167
1168
1169     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1170
1171     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1172       interestingCounter++;
1173       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1174         digitsInVolumeIndices[ii] = -1;
1175       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1176       digitIndexLocal=-1; // AdC
1177       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1178         if (digitsInVolumeIndices[digIndex]<0) continue;
1179         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1180         if (digitInteresting->GetToT()<=0) continue; // AdC
1181         digitIndexLocal++; // AdC
1182
1183         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1184                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1185                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1186                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1187                         digitsInVolumeIndices[digIndex],
1188                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1189
1190
1191         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1192           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1193                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1194                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1195                               digitsInVolumeIndices[digIndex],
1196                               digitInteresting->GetTracks());
1197       }
1198       if (digitIndexLocal==-1) interestingCounter--; // AdC
1199     }
1200
1201
1202     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1203
1204     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1205       interestingCounter++;
1206       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1207         digitsInVolumeIndices[ii] = -1;
1208       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1209       digitIndexLocal=-1;
1210       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1211         if (digitsInVolumeIndices[digIndex]<0) continue;
1212         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1213         if (digitInteresting->GetToT()<=0) continue; // AdC
1214         digitIndexLocal++; // AdC
1215
1216         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1217                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1218                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1219                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1220                         digitsInVolumeIndices[digIndex],
1221                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1222
1223         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1224           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1225                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1226                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1227                               digitsInVolumeIndices[digIndex],
1228                               digitInteresting->GetTracks());
1229       }
1230       if (digitIndexLocal==-1) interestingCounter--; // AdC
1231     }
1232
1233     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
1234
1235     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
1236       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
1237         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
1238
1239         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
1240           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
1241             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
1242
1243             for (Int_t adesso3=adesso2+1; adesso3<interestingCounter+1; adesso3++) {
1244               for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
1245                 if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
1246
1247
1248                 if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
1249                     ||
1250                     TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
1251                     ||
1252                     TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime) continue;
1253
1254                 interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
1255                 interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
1256                 interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
1257                 interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
1258                 Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
1259                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
1260                 Int_t volDum = vol1[3];
1261                 vol1[3] = vol1[4];
1262                 vol1[4] = volDum;
1263                 fTOFGeometry->GetPosPar(vol1,pos);
1264                 interestingX[0] = pos[0];
1265                 interestingY[0] = pos[1];
1266                 interestingZ[0] = pos[2];
1267
1268                 interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
1269                 interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
1270                 interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
1271                 interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
1272                 Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
1273                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
1274                 volDum = vol2[3];
1275                 vol2[3] = vol2[4];
1276                 vol2[4] = volDum;
1277                 fTOFGeometry->GetPosPar(vol2,pos);
1278                 interestingX[1] = pos[0];
1279                 interestingY[1] = pos[1];
1280                 interestingZ[1] = pos[2];
1281
1282                 interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
1283                 interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
1284                 interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
1285                 interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
1286                 Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
1287                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
1288                 volDum = vol3[3];
1289                 vol3[3] = vol3[4];
1290                 vol3[4] = volDum;
1291                 fTOFGeometry->GetPosPar(vol3,pos);
1292                 interestingX[2] = pos[0];
1293                 interestingY[2] = pos[1];
1294                 interestingZ[2] = pos[2];
1295
1296
1297                 AverageCalculations(3, interestingX, interestingY, interestingZ,
1298                                     interestingTOF, interestingTOT, interestingADC,
1299                                     interestingWeight,
1300                                     parTOF, posClus, check);
1301
1302
1303                 for (jj=0; jj<5; jj++) det[jj] = -1;
1304                 for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
1305                 fTOFGeometry->GetDetID(posF, det);
1306
1307                 volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
1308                 //volIdClus = GetClusterVolIndex(det);
1309
1310                 for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1311                 padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
1312                 padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
1313                 padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
1314                 padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
1315                 padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
1316                 padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
1317
1318                 for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1319                 Int_t ** indDet = new Int_t*[3];
1320                 for (jj=0; jj<3; jj++) indDet[jj] = new Int_t [5];
1321                 for (jj=0; jj<3; jj++) indDet[jj][0] = nSector;
1322                 for (jj=0; jj<3; jj++) indDet[jj][1] = nPlate;
1323                 for (jj=0; jj<3; jj++) indDet[jj][2] = nStrip;
1324                 for (jj=0; jj<3; jj++) indDet[jj][3] = padsCluster[2*jj+3];
1325                 for (jj=0; jj<3; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
1326                 GetClusterPars(/*check,*/ 3, indDet, interestingWeight, posClus, covClus);
1327                 for (jj=0; jj<3; jj++) delete [] indDet[jj];
1328
1329                 // To fill the track index array
1330                 dummyCounter=-1;
1331                 for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1332                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1333                   if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
1334                   else {
1335                     dummyCounter++;
1336                     tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
1337                   }
1338                 }
1339                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1340                   if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
1341                   else {
1342
1343                     alreadyStored = kFALSE;
1344                     for (jj=0; jj<dummyCounter+1; jj++)
1345                       alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
1346
1347                     if (alreadyStored) continue;
1348                     if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1349                       AliWarning("  Siamo al limite!");
1350                       continue;
1351                     }
1352
1353                     dummyCounter++;
1354                     tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
1355
1356                   }
1357
1358                 }
1359                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1360                   if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
1361                   else {
1362
1363                     alreadyStored = kFALSE;
1364                     for (jj=0; jj<dummyCounter+1; jj++)
1365                       alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
1366
1367                     if (alreadyStored) continue;
1368                     if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1369                       AliWarning("  Siamo al limite!");
1370                       continue;
1371                     }
1372
1373                     dummyCounter++;
1374                     tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
1375
1376                   }
1377
1378                 }
1379
1380
1381                 AliTOFcluster *tofCluster =
1382                   new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
1383                                     covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1384                                     tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
1385                 InsertCluster(tofCluster);
1386
1387                 AliDebug(2, Form("       %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", 
1388                                  volIdClus, posClus[0], posClus[1], posClus[2],
1389                                  covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1390                                  tracks[0], tracks[1], tracks[2],
1391                                  det[0], det[1], det[2], det[3], det[4],
1392                                  parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
1393                                  status, selectedDigit[adesso1][firstIndex]->GetIndex()));
1394
1395
1396                 volDum = vol1[3];
1397                 vol1[3] = vol1[4];
1398                 vol1[4] = volDum;
1399                 fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
1400                 volDum = vol2[3];
1401                 vol2[3] = vol2[4];
1402                 vol2[4] = volDum;
1403                 fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
1404                 volDum = vol3[3];
1405                 vol3[3] = vol3[4];
1406                 vol3[4] = volDum;
1407                 fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
1408
1409
1410               } // close loop on third digit
1411             } // close loop on adesso3
1412
1413           } // close loop on second digit
1414         } // close loop on adesso2
1415
1416       } // close loop on first digit
1417     } // close loop on adesso1
1418
1419     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1420       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1421         selectedDigit[ii][jj] = 0x0;
1422
1423   } // loop on iPad
1424
1425   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
1426     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
1427       delete [] selectedDigit[ii][jj];
1428       selectedDigit[ii][jj] = 0x0;
1429     }
1430     delete [] selectedDigit[ii];
1431     selectedDigit[ii] = 0x0;
1432   }
1433   delete selectedDigit;
1434   selectedDigit = 0x0;
1435
1436 }
1437 //_____________________________________________________________________________
1438
1439 void AliTOFClusterFinderV1::FindClusters23(Int_t nSector,
1440                                            Int_t nPlate,
1441                                            Int_t nStrip)
1442 {
1443   //
1444   // This function searches the neighbouring digits (stored in the fDigits object),
1445   // to perform clusters (stored in the fTofClusters array).
1446   //
1447   // This research has been made by checking the fTOFdigitMap object,
1448   // filled at digits/raw-data reading time.
1449   //
1450
1451   const Int_t kMaxNumberOfInterestingPads = 4;
1452   const Int_t kMaxNumberOfTracksPerDigit = 3;
1453   const Int_t kMaxNumberOfDigitsPerVolume = 10;
1454
1455   Int_t ii = 0;
1456
1457   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
1458   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1459     digitsInVolumeIndices[ii] = -1;
1460
1461   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
1462
1463   Float_t pos[3] = {0.,0.,0.};
1464
1465   Int_t jj = 0;
1466   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1467   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1468   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1469   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1470   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1471   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1472   Double_t interestingADC[kMaxNumberOfInterestingPads];
1473   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1474   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1475   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1476   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1477   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1478
1479   Float_t interestingX[kMaxNumberOfInterestingPads];
1480   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1481   Float_t interestingY[kMaxNumberOfInterestingPads];
1482   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1483   Float_t interestingZ[kMaxNumberOfInterestingPads];
1484   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1485
1486   Float_t interDigit[kMaxNumberOfInterestingPads];
1487   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1488
1489   Int_t padsCluster[11];
1490   padsCluster[0] = nSector;
1491   padsCluster[1] = nPlate;
1492   padsCluster[2] = nStrip;
1493   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1494
1495   Int_t interestingCounter=-1;
1496   Int_t digitIndexLocal = -1;
1497   Int_t iPad  = -1;
1498   Int_t iPadX = -1;
1499   Int_t iPadZ = -1;
1500
1501   Bool_t check = kFALSE;
1502   Int_t parTOF[5];
1503   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
1504   Double_t posClus[3];
1505   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1506   Int_t det[5];
1507   for (jj=0; jj<5; jj++) det[jj] = -1;
1508   Float_t posF[3];
1509   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1510   UShort_t volIdClus = 0;
1511   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1512   Double_t covClus[6];
1513   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1514   Int_t tracks[kMaxNumberOfTracksPerDigit];
1515   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1516   Int_t dummyCounter=-1;
1517   Bool_t alreadyStored = kFALSE;
1518
1519   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1520   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1521     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1522
1523   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1524     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1525
1526   AliTOFdigit *digitInteresting;
1527
1528   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1529
1530     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1531     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1532
1533     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1534
1535
1536
1537
1538
1539
1540     interestingCounter=-1;
1541
1542     vol[4] = iPadZ  , vol[3]  = iPadX;
1543     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1544       interestingCounter++;
1545
1546     vol[4] = iPadZ, vol[3] = iPadX+1;
1547     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1548       interestingCounter++;
1549
1550     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1551     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1552       interestingCounter++;
1553
1554     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1555     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1556       interestingCounter++;
1557
1558     if (interestingCounter+1!=3) continue; // the hit pads have to be 3
1559     else interestingCounter=-1;
1560
1561
1562     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1563       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1564         selectedDigit[ii][jj] = 0x0;
1565
1566
1567     vol[4] = iPadZ, vol[3] = iPadX;
1568
1569     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1570       interestingCounter++;
1571       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1572         digitsInVolumeIndices[ii] = -1;
1573       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1574       digitIndexLocal=-1;
1575       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1576         if (digitsInVolumeIndices[digIndex]<0) continue;
1577         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1578         if (digitInteresting->GetToT()<=0) continue; // AdC
1579         digitIndexLocal++; // AdC
1580
1581         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1582                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1583                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1584                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1585                         digitsInVolumeIndices[digIndex],
1586                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1587
1588         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1589           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1590                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1591                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1592                               digitsInVolumeIndices[digIndex],
1593                               digitInteresting->GetTracks());
1594       }
1595       if (digitIndexLocal==-1) interestingCounter--; // AdC
1596     }
1597
1598
1599     vol[4] = iPadZ, vol[3] = iPadX+1;
1600
1601     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1602       interestingCounter++;
1603       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1604         digitsInVolumeIndices[ii] = -1;
1605       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1606       digitIndexLocal=-1; // AdC
1607       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1608         if (digitsInVolumeIndices[digIndex]<0) continue;
1609         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1610         if (digitInteresting->GetToT()<=0) continue; // AdC
1611         digitIndexLocal++; // AdC
1612
1613         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1614                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1615                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1616                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1617                         digitsInVolumeIndices[digIndex],
1618                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1619
1620         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1621           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1622                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1623                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1624                               digitsInVolumeIndices[digIndex],
1625                               digitInteresting->GetTracks());
1626       }
1627       if (digitIndexLocal==-1) interestingCounter--; // AdC
1628     }
1629
1630
1631     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1632
1633     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1634       interestingCounter++;
1635       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1636         digitsInVolumeIndices[ii] = -1;
1637       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1638       digitIndexLocal=-1; // AdC
1639       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1640         if (digitsInVolumeIndices[digIndex]<0) continue;
1641         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1642         if (digitInteresting->GetToT()<=0) continue; // AdC
1643         digitIndexLocal++; // AdC
1644
1645         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1646                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1647                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1648                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1649                         digitsInVolumeIndices[digIndex],
1650                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1651
1652         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1653           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1654                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1655                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1656                               digitsInVolumeIndices[digIndex],
1657                               digitInteresting->GetTracks());
1658       }
1659       if (digitIndexLocal==-1) interestingCounter--; // AdC
1660     }
1661
1662
1663     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1664
1665     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1666       interestingCounter++;
1667       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1668         digitsInVolumeIndices[ii] = -1;
1669       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1670       digitIndexLocal=-1; // AdC
1671       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1672         if (digitsInVolumeIndices[digIndex]<0) continue;
1673         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1674         if (digitInteresting->GetToT()<=0) continue; // AdC
1675         digitIndexLocal++; // AdC
1676
1677         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1678                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1679                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1680                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1681                         digitsInVolumeIndices[digIndex],
1682                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1683
1684         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1685           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1686                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1687                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1688                               digitsInVolumeIndices[digIndex],
1689                               digitInteresting->GetTracks());
1690       }
1691       if (digitIndexLocal==-1) interestingCounter--; // AdC
1692     }
1693
1694     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
1695
1696     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
1697       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
1698         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
1699
1700         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
1701           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
1702             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
1703
1704             if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) continue;
1705
1706             interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
1707             interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
1708             interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
1709             interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
1710             Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
1711             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
1712             Int_t volDum = vol1[3];
1713             vol1[3] = vol1[4];
1714             vol1[4] = volDum;
1715             fTOFGeometry->GetPosPar(vol1,pos);
1716             interestingX[0] = pos[0];
1717             interestingY[0] = pos[1];
1718             interestingZ[0] = pos[2];
1719
1720             interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
1721             interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
1722             interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
1723             interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
1724             Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
1725             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
1726             volDum = vol2[3];
1727             vol2[3] = vol2[4];
1728             vol2[4] = volDum;
1729             fTOFGeometry->GetPosPar(vol2,pos);
1730             interestingX[1] = pos[0];
1731             interestingY[1] = pos[1];
1732             interestingZ[1] = pos[2];
1733
1734             AverageCalculations(2, interestingX, interestingY, interestingZ,
1735                                 interestingTOF, interestingTOT, interestingADC,
1736                                 interestingWeight,
1737                                 parTOF, posClus, check);
1738
1739             for (jj=0; jj<5; jj++) det[jj] = -1;
1740             for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
1741             fTOFGeometry->GetDetID(posF, det);
1742
1743             volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
1744             //volIdClus = GetClusterVolIndex(det);
1745
1746             for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1747             padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
1748             padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
1749             padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
1750             padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
1751
1752             for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1753             Int_t ** indDet = new Int_t*[2];
1754             for (jj=0; jj<2; jj++) indDet[jj] = new Int_t [5];
1755             for (jj=0; jj<2; jj++) indDet[jj][0] = nSector;
1756             for (jj=0; jj<2; jj++) indDet[jj][1] = nPlate;
1757             for (jj=0; jj<2; jj++) indDet[jj][2] = nStrip;
1758             for (jj=0; jj<2; jj++) indDet[jj][3] = padsCluster[2*jj+3];
1759             for (jj=0; jj<2; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
1760             GetClusterPars(/*check,*/ 2, indDet, interestingWeight, posClus, covClus);
1761             for (jj=0; jj<2; jj++) delete [] indDet[jj];
1762
1763             // To fill the track index array
1764             dummyCounter=-1;
1765             for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1766             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1767               if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
1768               else {
1769                 dummyCounter++;
1770                 tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
1771               }
1772             }
1773             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1774               if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
1775               else {
1776
1777                 alreadyStored = kFALSE;
1778                 for (jj=0; jj<dummyCounter+1; jj++)
1779                   alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
1780
1781                 if (alreadyStored) continue;
1782                 if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1783                   AliWarning("  Siamo al limite!");
1784                   continue;
1785                 }
1786
1787                 dummyCounter++;
1788                 tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
1789
1790               }
1791
1792             }
1793
1794
1795             AliTOFcluster *tofCluster =
1796               new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
1797                                 covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1798                                 tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
1799             InsertCluster(tofCluster);
1800
1801             AliDebug(2, Form("       %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", 
1802                              volIdClus, posClus[0], posClus[1], posClus[2],
1803                              covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1804                              tracks[0], tracks[1], tracks[2],
1805                              det[0], det[1], det[2], det[3], det[4],
1806                              parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
1807                              status, selectedDigit[adesso1][firstIndex]->GetIndex()));
1808
1809             volDum = vol1[3];
1810             vol1[3] = vol1[4];
1811             vol1[4] = volDum;
1812             fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
1813             volDum = vol2[3];
1814             vol2[3] = vol2[4];
1815             vol2[4] = volDum;
1816             fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
1817
1818
1819           } // close loop on second digit
1820         } // close loop on adesso2
1821
1822       } // close loop on first digit
1823     } // close loop on adesso1
1824
1825     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1826       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1827         selectedDigit[ii][jj] = 0x0;
1828
1829   } // loop on iPad
1830
1831   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
1832     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
1833       delete [] selectedDigit[ii][jj];
1834       selectedDigit[ii][jj] = 0x0;
1835     }
1836     delete [] selectedDigit[ii];
1837     selectedDigit[ii] = 0x0;
1838   }
1839   delete selectedDigit;
1840   selectedDigit = 0x0;
1841
1842 }
1843 //_____________________________________________________________________________
1844
1845 void AliTOFClusterFinderV1::FindClusters24(Int_t nSector,
1846                                            Int_t nPlate,
1847                                            Int_t nStrip)
1848 {
1849   //
1850   // This function searches the neighbouring digits (stored in the fDigits object),
1851   // to perform clusters (stored in the fTofClusters array).
1852   //
1853   // This research has been made by checking the fTOFdigitMap object,
1854   // filled at digits/raw-data reading time.
1855   //
1856
1857   const Int_t kMaxNumberOfInterestingPads = 4;
1858   const Int_t kMaxNumberOfTracksPerDigit = 3;
1859   const Int_t kMaxNumberOfDigitsPerVolume = 10;
1860
1861   Int_t ii = 0;
1862
1863   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
1864   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1865     digitsInVolumeIndices[ii] = -1;
1866
1867   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
1868
1869   Float_t pos[3] = {0.,0.,0.};
1870
1871   Int_t jj = 0;
1872   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1873   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1874   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1875   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1876   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1877   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1878   Double_t interestingADC[kMaxNumberOfInterestingPads];
1879   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1880   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1881   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1882   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1883   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1884
1885   Float_t interestingX[kMaxNumberOfInterestingPads];
1886   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1887   Float_t interestingY[kMaxNumberOfInterestingPads];
1888   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1889   Float_t interestingZ[kMaxNumberOfInterestingPads];
1890   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1891
1892   Float_t interDigit[kMaxNumberOfInterestingPads];
1893   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1894
1895   Int_t padsCluster[11];
1896   padsCluster[0] = nSector;
1897   padsCluster[1] = nPlate;
1898   padsCluster[2] = nStrip;
1899   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1900
1901   Int_t interestingCounter=-1;
1902   Int_t digitIndexLocal = -1;
1903   Int_t iPad  = -1;
1904   Int_t iPadX = -1;
1905   Int_t iPadZ = -1;
1906
1907   Bool_t check = kFALSE;
1908   Int_t parTOF[5];
1909   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
1910   Double_t posClus[3];
1911   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1912   Int_t det[5];
1913   for (jj=0; jj<5; jj++) det[jj] = -1;
1914   Float_t posF[3];
1915   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1916   UShort_t volIdClus = 0;
1917   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1918   Double_t covClus[6];
1919   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1920   Int_t tracks[kMaxNumberOfTracksPerDigit];
1921   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1922   Int_t dummyCounter=-1;
1923   Bool_t alreadyStored = kFALSE;
1924
1925   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1926   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1927     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1928
1929   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1930     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1931
1932   AliTOFdigit *digitInteresting;
1933
1934   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1935
1936     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1937     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1938
1939     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1940
1941
1942
1943
1944
1945
1946     interestingCounter=-1;
1947
1948     vol[4] = iPadZ  , vol[3]  = iPadX;
1949     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1950       interestingCounter++;
1951
1952     vol[4] = iPadZ, vol[3] = iPadX+1;
1953     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1954       interestingCounter++;
1955
1956     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1957     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1958       interestingCounter++;
1959
1960     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1961     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1962       interestingCounter++;
1963
1964     if (interestingCounter+1!=4) continue; // the hit pads have to be 4
1965     else interestingCounter=-1;
1966
1967
1968     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1969       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1970         selectedDigit[ii][jj] = 0x0;
1971
1972
1973     vol[4] = iPadZ, vol[3] = iPadX;
1974
1975     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1976       interestingCounter++;
1977       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1978         digitsInVolumeIndices[ii] = -1;
1979       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1980       digitIndexLocal=-1; // AdC
1981       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1982         if (digitsInVolumeIndices[digIndex]<0) continue;
1983         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1984         if (digitInteresting->GetToT()<=0) continue; // AdC
1985         digitIndexLocal++; // AdC
1986
1987         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
1988                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1989                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1990                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1991                         digitsInVolumeIndices[digIndex],
1992                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1993
1994         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1995           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1996                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1997                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1998                               digitsInVolumeIndices[digIndex],
1999                               digitInteresting->GetTracks());
2000       }
2001       if (digitIndexLocal==-1) interestingCounter--; // AdC
2002     }
2003
2004
2005     vol[4] = iPadZ, vol[3] = iPadX+1;
2006
2007     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2008       interestingCounter++;
2009       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2010         digitsInVolumeIndices[ii] = -1;
2011       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2012       digitIndexLocal=-1; // AdC
2013       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2014         if (digitsInVolumeIndices[digIndex]<0) continue;
2015         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2016         if (digitInteresting->GetToT()<=0) continue; // AdC
2017         digitIndexLocal++; // AdC
2018
2019         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
2020                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2021                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2022                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2023                         digitsInVolumeIndices[digIndex],
2024                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2025
2026         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2027           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2028                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2029                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2030                               digitsInVolumeIndices[digIndex],
2031                               digitInteresting->GetTracks());
2032       }
2033       if (digitIndexLocal==-1) interestingCounter--; // AdC
2034     }
2035
2036
2037     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2038
2039     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2040       interestingCounter++;
2041       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2042         digitsInVolumeIndices[ii] = -1;
2043       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2044       digitIndexLocal=-1; // AdC
2045       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2046         if (digitsInVolumeIndices[digIndex]<0) continue;
2047         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2048         if (digitInteresting->GetToT()<=0) continue; // AdC
2049         digitIndexLocal++; // AdC
2050
2051         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
2052                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2053                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2054                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2055                         digitsInVolumeIndices[digIndex],
2056                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2057
2058         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2059           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2060                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2061                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2062                               digitsInVolumeIndices[digIndex],
2063                               digitInteresting->GetTracks());
2064       }
2065       if (digitIndexLocal==-1) interestingCounter--; // AdC
2066     }
2067
2068
2069     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2070
2071     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2072       interestingCounter++;
2073       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2074         digitsInVolumeIndices[ii] = -1;
2075       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2076       digitIndexLocal=-1; // AdC
2077       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2078         if (digitsInVolumeIndices[digIndex]<0) continue;
2079         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2080         if (digitInteresting->GetToT()<=0) continue; // AdC
2081         digitIndexLocal++; // AdC
2082
2083         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %f %f %f %f  %5d  %5d %5d %5d",
2084                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2085                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2086                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2087                         digitsInVolumeIndices[digIndex],
2088                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2089
2090         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2091           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2092                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2093                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2094                               digitsInVolumeIndices[digIndex],
2095                               digitInteresting->GetTracks());
2096       }
2097       if (digitIndexLocal==-1) interestingCounter--; // AdC
2098     }
2099
2100     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
2101
2102     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2103       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2104         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2105
2106         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2107           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2108             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2109
2110             if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) continue;
2111
2112             interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2113             interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2114             interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2115             interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2116             Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2117             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2118             Int_t volDum = vol1[3];
2119             vol1[3] = vol1[4];
2120             vol1[4] = volDum;
2121             fTOFGeometry->GetPosPar(vol1,pos);
2122             interestingX[0] = pos[0];
2123             interestingY[0] = pos[1];
2124             interestingZ[0] = pos[2];
2125
2126             interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2127             interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2128             interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2129             interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2130             Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2131             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2132             volDum = vol2[3];
2133             vol2[3] = vol2[4];
2134             vol2[4] = volDum;
2135             fTOFGeometry->GetPosPar(vol2,pos);
2136             interestingX[1] = pos[0];
2137             interestingY[1] = pos[1];
2138             interestingZ[1] = pos[2];
2139
2140
2141             AverageCalculations(2, interestingX, interestingY, interestingZ,
2142                                 interestingTOF, interestingTOT, interestingADC,
2143                                 interestingWeight,
2144                                 parTOF, posClus, check);
2145
2146             for (jj=0; jj<5; jj++) det[jj] = -1;
2147             for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2148             fTOFGeometry->GetDetID(posF, det);
2149
2150             volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2151             //volIdClus = GetClusterVolIndex(det);
2152
2153             for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2154             padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2155             padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2156             padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2157             padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2158
2159             for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2160             Int_t ** indDet = new Int_t*[2];
2161             for (jj=0; jj<2; jj++) indDet[jj] = new Int_t [5];
2162             for (jj=0; jj<2; jj++) indDet[jj][0] = nSector;
2163             for (jj=0; jj<2; jj++) indDet[jj][1] = nPlate;
2164             for (jj=0; jj<2; jj++) indDet[jj][2] = nStrip;
2165             for (jj=0; jj<2; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2166             for (jj=0; jj<2; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2167             GetClusterPars(/*check,*/ 2, indDet, interestingWeight, posClus, covClus);
2168             for (jj=0; jj<2; jj++) delete [] indDet[jj];
2169
2170             // To fill the track index array
2171             dummyCounter=-1;
2172             for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2173             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2174               if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2175               else {
2176                 dummyCounter++;
2177                 tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2178               }
2179             }
2180             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2181               if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2182               else {
2183
2184                 alreadyStored = kFALSE;
2185                 for (jj=0; jj<dummyCounter+1; jj++)
2186                   alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2187
2188                 if (alreadyStored) continue;
2189                 if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2190                   AliWarning("  Siamo al limite!");
2191                   continue;
2192                 }
2193
2194                 dummyCounter++;
2195                 tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2196
2197               }
2198
2199             }
2200
2201
2202             AliTOFcluster *tofCluster =
2203               new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2204                                 covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2205                                 tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
2206             InsertCluster(tofCluster);
2207
2208             AliDebug(2, Form("       %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", 
2209                              volIdClus, posClus[0], posClus[1], posClus[2],
2210                              covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2211                              tracks[0], tracks[1], tracks[2],
2212                              det[0], det[1], det[2], det[3], det[4],
2213                              parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2214                              status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2215
2216             volDum = vol1[3];
2217             vol1[3] = vol1[4];
2218             vol1[4] = volDum;
2219             fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2220             volDum = vol2[3];
2221             vol2[3] = vol2[4];
2222             vol2[4] = volDum;
2223             fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2224
2225
2226           } // close loop on second digit
2227         } // close loop on adesso2
2228
2229       } // close loop on first digit
2230     } // close loop on adesso1
2231
2232     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2233       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
2234         selectedDigit[ii][jj] = 0x0;
2235
2236   } // loop on iPad
2237
2238   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
2239     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
2240       delete [] selectedDigit[ii][jj];
2241       selectedDigit[ii][jj] = 0x0;
2242     }
2243     delete [] selectedDigit[ii];
2244     selectedDigit[ii] = 0x0;
2245   }
2246   delete selectedDigit;
2247   selectedDigit = 0x0;
2248
2249 }
2250 //_____________________________________________________________________________
2251
2252 void AliTOFClusterFinderV1::FindClustersPerStrip(Int_t nSector,
2253                                                  Int_t nPlate,
2254                                                  Int_t nStrip,
2255                                                  Int_t group)
2256 {
2257   //
2258   // This function searches the neighbouring digits (stored in the fDigits object),
2259   // to perform clusters (stored in the fTofClusters array).
2260   //
2261   // Each strip is read four times:
2262   //  - 1st time: it searches possible clusters formed by four
2263   //              neighbouring digits;
2264   //  - 2nd time: it searches possible clusters formed by three
2265   //              neighbouring digits;
2266   //  - 3rd time: it searches possible clusters formed by two
2267   //              neighbouring digits;
2268   //  - 4th time: the remaining isolated digits have been transformed
2269   //              in clusters.
2270   // This research has been made by checking the fTOFdigitMap object,
2271   // filled at digits/raw-data reading time.
2272   //
2273
2274   const Int_t kMaxNumberOfInterestingPads = 4;
2275   const Int_t kMaxNumberOfTracksPerDigit = 3;
2276   const Int_t kMaxNumberOfDigitsPerVolume = 10;
2277
2278   Int_t ii = 0;
2279
2280   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
2281   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2282     digitsInVolumeIndices[ii] = -1;
2283
2284   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
2285
2286   Float_t pos[3] = {0.,0.,0.};
2287
2288   Int_t jj = 0;
2289   Int_t interestingPadX[kMaxNumberOfInterestingPads];
2290   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
2291   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
2292   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
2293   Double_t interestingTOT[kMaxNumberOfInterestingPads];
2294   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
2295   Double_t interestingADC[kMaxNumberOfInterestingPads];
2296   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
2297   Double_t interestingTOF[kMaxNumberOfInterestingPads];
2298   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
2299   Double_t interestingWeight[kMaxNumberOfInterestingPads];
2300   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
2301
2302   Float_t interestingX[kMaxNumberOfInterestingPads];
2303   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
2304   Float_t interestingY[kMaxNumberOfInterestingPads];
2305   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
2306   Float_t interestingZ[kMaxNumberOfInterestingPads];
2307   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
2308
2309   Float_t interDigit[kMaxNumberOfInterestingPads];
2310   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
2311
2312   Int_t padsCluster[11];
2313   padsCluster[0] = nSector;
2314   padsCluster[1] = nPlate;
2315   padsCluster[2] = nStrip;
2316   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2317
2318   Int_t interestingCounter=-1;
2319   Int_t digitIndexLocal = -1;
2320   Int_t iPad  = -1;
2321   Int_t iPadX = -1;
2322   Int_t iPadZ = -1;
2323
2324   Bool_t check = kFALSE;
2325   Int_t parTOF[5];
2326   for (jj=0; jj<5; jj++) parTOF[jj] = 0;
2327   Double_t posClus[3];
2328   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
2329   Int_t det[5];
2330   for (jj=0; jj<5; jj++) det[jj] = -1;
2331   Float_t posF[3];
2332   for (jj=0; jj<3; jj++) posF[jj] = 0.;
2333   UShort_t volIdClus = 0;
2334   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
2335   Double_t covClus[6];
2336   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2337   Int_t tracks[kMaxNumberOfTracksPerDigit];
2338   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2339   Int_t dummyCounter=-1;
2340   Bool_t alreadyStored = kFALSE;
2341
2342   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
2343   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2344     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
2345
2346   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2347     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
2348
2349   AliTOFdigit *digitInteresting;
2350
2351   group = group-1;
2352
2353   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
2354
2355     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
2356     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
2357
2358     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
2359
2360
2361
2362
2363
2364
2365     interestingCounter=-1;
2366
2367     vol[4] = iPadZ  , vol[3]  = iPadX;
2368     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2369       interestingCounter++;
2370
2371     vol[4] = iPadZ, vol[3] = iPadX+1;
2372     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2373       interestingCounter++;
2374
2375     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2376     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2377       interestingCounter++;
2378
2379     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2380     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2381       interestingCounter++;
2382
2383     if (interestingCounter!=group) continue;
2384     else interestingCounter=-1;
2385
2386
2387     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2388       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
2389         selectedDigit[ii][jj] = 0x0;
2390
2391
2392     vol[4] = iPadZ, vol[3] = iPadX;
2393
2394     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2395       interestingCounter++;
2396       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2397         digitsInVolumeIndices[ii] = -1;
2398       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2399       digitIndexLocal=-1; // AdC
2400       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2401         if (digitsInVolumeIndices[digIndex]<0) continue;
2402         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2403         if (digitInteresting->GetToT()<=0) continue; // AdC
2404         digitIndexLocal++; // AdC
2405
2406         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2407                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2408                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2409                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2410                         digitsInVolumeIndices[digIndex],
2411                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2412         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2413
2414         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2415           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2416                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2417                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2418                               digitsInVolumeIndices[digIndex],
2419                               digitInteresting->GetTracks());
2420       }
2421       if (digitIndexLocal==-1) interestingCounter--; // AdC
2422     }
2423
2424
2425     vol[4] = iPadZ, vol[3] = iPadX+1;
2426
2427     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2428       interestingCounter++;
2429       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2430         digitsInVolumeIndices[ii] = -1;
2431       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2432       digitIndexLocal=-1; // AdC
2433       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2434         if (digitsInVolumeIndices[digIndex]<0) continue;
2435         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2436         if (digitInteresting->GetToT()<=0) continue; // AdC
2437         digitIndexLocal++; // AdC
2438
2439         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2440                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2441                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2442                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2443                         digitsInVolumeIndices[digIndex],
2444                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2445         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2446
2447         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2448           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2449                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2450                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2451                               digitsInVolumeIndices[digIndex],
2452                               digitInteresting->GetTracks());
2453       }
2454       if (digitIndexLocal==-1) interestingCounter--; // AdC
2455     }
2456
2457
2458     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2459
2460     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2461       interestingCounter++;
2462       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2463         digitsInVolumeIndices[ii] = -1;
2464       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2465       digitIndexLocal=-1; // AdC
2466       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2467         if (digitsInVolumeIndices[digIndex]<0) continue;
2468         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2469         if (digitInteresting->GetToT()<=0) continue; // AdC
2470         digitIndexLocal++; // AdC
2471
2472         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2473                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2474                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2475                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2476                         digitsInVolumeIndices[digIndex],
2477                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2478         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2479
2480         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2481           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2482                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2483                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2484                               digitsInVolumeIndices[digIndex],
2485                               digitInteresting->GetTracks());
2486       }
2487       if (digitIndexLocal==-1) interestingCounter--; // AdC
2488     }
2489
2490
2491     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2492
2493     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2494       interestingCounter++;
2495       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2496         digitsInVolumeIndices[ii] = -1;
2497       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2498       digitIndexLocal=-1; // AdC
2499       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2500         if (digitsInVolumeIndices[digIndex]<0) continue;
2501         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2502         if (digitInteresting->GetToT()<=0) continue; // AdC
2503         digitIndexLocal++; // AdC
2504
2505         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2506                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2507                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2508                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2509                         digitsInVolumeIndices[digIndex],
2510                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2511         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2512
2513         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2514           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2515                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2516                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2517                               digitsInVolumeIndices[digIndex],
2518                               digitInteresting->GetTracks());
2519       }
2520       if (digitIndexLocal==-1) interestingCounter--; // AdC
2521     }
2522
2523     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
2524
2525     Int_t adesso1 = -1;
2526     Int_t adesso2 = -1;
2527     Int_t adesso3 = -1;
2528     Int_t adesso4 = -1;
2529
2530     switch(interestingCounter+1) {
2531
2532     case 2:
2533
2534       //for (adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2535       adesso1 = 0;
2536         for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2537           if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2538
2539           //for (adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2540           adesso2 = 1;
2541             for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2542               if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2543
2544               if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) {
2545                 AliDebug(1,Form(" selD1[%d][%d]->GetTDC()=%d selD2[%d][%d]->GetTDC()=%d -- %d ",
2546                                 adesso1,firstIndex,(Int_t)selectedDigit[adesso1][firstIndex]->GetTDC(),
2547                                 adesso2,secondIndex,(Int_t)selectedDigit[adesso2][secondIndex]->GetTDC(),
2548                                 fMaxDeltaTime));
2549                 continue;
2550               }
2551
2552               AliDebug(1, Form(" %1d %1d (0x%x) %1d %1d (0x%x)", adesso1, firstIndex,selectedDigit[adesso1][firstIndex],
2553                                adesso2, secondIndex,selectedDigit[adesso2][secondIndex]));
2554
2555               interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2556               interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2557               interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2558               interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2559               Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2560               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2561               Int_t volDum = vol1[3];
2562               vol1[3] = vol1[4];
2563               vol1[4] = volDum;
2564               fTOFGeometry->GetPosPar(vol1,pos);
2565               AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
2566               interestingX[0] = pos[0];
2567               interestingY[0] = pos[1];
2568               interestingZ[0] = pos[2];
2569
2570               interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2571               interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2572               interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2573               interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2574               Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2575               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2576               volDum = vol2[3];
2577               vol2[3] = vol2[4];
2578               vol2[4] = volDum;
2579               fTOFGeometry->GetPosPar(vol2,pos);
2580               AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
2581               interestingX[1] = pos[0];
2582               interestingY[1] = pos[1];
2583               interestingZ[1] = pos[2];
2584
2585
2586               AverageCalculations(interestingCounter+1,
2587                                   interestingX, interestingY, interestingZ,
2588                                   interestingTOF, interestingTOT, interestingADC,
2589                                   interestingWeight,
2590                                   parTOF, posClus, check);
2591
2592               for (jj=0; jj<5; jj++) det[jj] = -1;
2593               for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2594
2595               AliDebug(1,Form(" %f %f %f", posF[0], posF[1], posF[2]));
2596               fTOFGeometry->GetDetID(posF, det);
2597               AliDebug(1,Form(" %2d %1d %2d %1d %2d", det[0], det[1], det[2], det[3], det[4]));
2598
2599               volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2600               //volIdClus = GetClusterVolIndex(det);
2601
2602               for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2603               padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2604               padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2605               padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2606               padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2607
2608               for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2609               Int_t ** indDet = new Int_t*[interestingCounter+1];
2610               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
2611               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
2612               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
2613               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
2614               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2615               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2616               GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
2617               for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
2618
2619               // To fill the track index array
2620               dummyCounter=-1;
2621               for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2622               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2623                 if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2624                 else {
2625                   dummyCounter++;
2626                   tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2627                 }
2628               }
2629               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2630                 if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2631                 else {
2632
2633                   alreadyStored = kFALSE;
2634                   for (jj=0; jj<dummyCounter+1; jj++)
2635                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2636
2637                   if (alreadyStored) continue;
2638                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2639                     AliWarning("  Siamo al limite!");
2640                     continue;
2641                   }
2642
2643                   dummyCounter++;
2644                   tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2645
2646                 }
2647
2648               }
2649
2650
2651               AliTOFcluster *tofCluster =
2652                 new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2653                                   covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2654                                   tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); //to updated
2655               InsertCluster(tofCluster);
2656
2657               AliDebug(2, Form("       %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", 
2658                                volIdClus, posClus[0], posClus[1], posClus[2],
2659                                covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2660                                tracks[0], tracks[1], tracks[2],
2661                                det[0], det[1], det[2], det[3], det[4],
2662                                parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2663                                status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2664
2665               volDum = vol1[3];
2666               vol1[3] = vol1[4];
2667               vol1[4] = volDum;
2668               fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2669               volDum = vol2[3];
2670               vol2[3] = vol2[4];
2671               vol2[4] = volDum;
2672               fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2673
2674
2675             } // close loop on second digit
2676             //} // close loop on adesso2
2677
2678         } // close loop on first digit
2679           //} // close loop on adesso1
2680
2681
2682       break;
2683
2684     case 3:
2685
2686       //for (adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2687       adesso1 = 0;
2688         for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2689           if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2690
2691           //for (adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2692           adesso2 = 1;
2693             for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2694               if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2695
2696               //for (adesso3=adesso2+1; adesso3<interestingCounter+1; adesso3++) {
2697               adesso3 = 2;
2698                 for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
2699                   if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
2700
2701
2702                   if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
2703                       ||
2704                       TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2705                       ||
2706                       TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime) continue;
2707
2708                   interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2709                   interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2710                   interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2711                   interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2712                   Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2713                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2714                   Int_t volDum = vol1[3];
2715                   vol1[3] = vol1[4];
2716                   vol1[4] = volDum;
2717                   fTOFGeometry->GetPosPar(vol1,pos);
2718                   interestingX[0] = pos[0];
2719                   interestingY[0] = pos[1];
2720                   interestingZ[0] = pos[2];
2721
2722                   interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2723                   interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2724                   interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2725                   interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2726                   Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2727                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2728                   volDum = vol2[3];
2729                   vol2[3] = vol2[4];
2730                   vol2[4] = volDum;
2731                   fTOFGeometry->GetPosPar(vol2,pos);
2732                   interestingX[1] = pos[0];
2733                   interestingY[1] = pos[1];
2734                   interestingZ[1] = pos[2];
2735
2736                   interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
2737                   interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
2738                   interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
2739                   interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
2740                   Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
2741                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
2742                   volDum = vol3[3];
2743                   vol3[3] = vol3[4];
2744                   vol3[4] = volDum;
2745                   fTOFGeometry->GetPosPar(vol3,pos);
2746                   interestingX[2] = pos[0];
2747                   interestingY[2] = pos[1];
2748                   interestingZ[2] = pos[2];
2749
2750
2751                   AverageCalculations(interestingCounter+1,
2752                                       interestingX, interestingY, interestingZ,
2753                                       interestingTOF, interestingTOT, interestingADC,
2754                                       interestingWeight,
2755                                       parTOF, posClus, check);
2756
2757                   for (jj=0; jj<5; jj++) det[jj] = -1;
2758                   for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2759                   fTOFGeometry->GetDetID(posF, det);
2760
2761                   volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2762                   //volIdClus = GetClusterVolIndex(det);
2763
2764                   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2765                   padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2766                   padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2767                   padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2768                   padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2769                   padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
2770                   padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
2771
2772                   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2773                   Int_t ** indDet = new Int_t*[interestingCounter+1];
2774                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
2775                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
2776                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
2777                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
2778                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2779                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2780                   GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
2781                   for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
2782
2783                   // To fill the track index array
2784                   dummyCounter=-1;
2785                   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2786                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2787                     if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2788                     else {
2789                       dummyCounter++;
2790                       tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2791                     }
2792                   }
2793                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2794                     if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2795                     else {
2796
2797                       alreadyStored = kFALSE;
2798                       for (jj=0; jj<dummyCounter+1; jj++)
2799                         alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2800
2801                       if (alreadyStored) continue;
2802                       if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2803                         AliWarning("  Siamo al limite!");
2804                         continue;
2805                       }
2806
2807                       dummyCounter++;
2808                       tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2809
2810                     }
2811
2812                   }
2813                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2814                     if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
2815                     else {
2816
2817                       alreadyStored = kFALSE;
2818                       for (jj=0; jj<dummyCounter+1; jj++)
2819                         alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
2820
2821                       if (alreadyStored) continue;
2822                       if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2823                         AliWarning("  Siamo al limite!");
2824                         continue;
2825                       }
2826
2827                       dummyCounter++;
2828                       tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
2829
2830                     }
2831
2832                   }
2833
2834
2835                   AliTOFcluster *tofCluster =
2836                     new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2837                                       covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2838                                       tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
2839                   InsertCluster(tofCluster);
2840
2841                   AliDebug(2, Form("       %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", 
2842                                    volIdClus, posClus[0], posClus[1], posClus[2],
2843                                    covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2844                                    tracks[0], tracks[1], tracks[2],
2845                                    det[0], det[1], det[2], det[3], det[4],
2846                                    parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2847                                    status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2848
2849                   volDum = vol1[3];
2850                   vol1[3] = vol1[4];
2851                   vol1[4] = volDum;
2852                   fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2853                   volDum = vol2[3];
2854                   vol2[3] = vol2[4];
2855                   vol2[4] = volDum;
2856                   fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2857                   volDum = vol3[3];
2858                   vol3[3] = vol3[4];
2859                   vol3[4] = volDum;
2860                   fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
2861
2862
2863                 } // close loop on third digit
2864                 //} // close loop on adesso3
2865
2866             } // close loop on second digit
2867             //} // close loop on adesso2
2868
2869         } // close loop on first digit
2870         //} // close loop on adesso1
2871
2872
2873       break;
2874
2875     case 4:
2876
2877       adesso1 = 0;
2878       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2879         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2880
2881         adesso2 = 1;
2882         for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2883           if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2884
2885           adesso3 = 2;
2886           for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
2887             if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
2888
2889             adesso4 = 3;
2890             for (Int_t fourthIndex=0; fourthIndex<kMaxNumberOfDigitsPerVolume; fourthIndex++) {
2891               if (selectedDigit[adesso4][fourthIndex]==0x0) continue;
2892
2893
2894               if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
2895                   ||
2896                   TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2897                   ||
2898                   TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime
2899                   ||
2900                   TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2901                   ||
2902                   TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime
2903                   ||
2904                   TMath::Abs(selectedDigit[adesso3][thirdIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime) continue;
2905
2906               interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2907               interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2908               interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2909               interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2910               Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2911               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2912               Int_t volDum = vol1[3];
2913               vol1[3] = vol1[4];
2914               vol1[4] = volDum;
2915               fTOFGeometry->GetPosPar(vol1,pos);
2916               interestingX[0] = pos[0];
2917               interestingY[0] = pos[1];
2918               interestingZ[0] = pos[2];
2919
2920               interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2921               interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2922               interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2923               interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2924               Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2925               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2926               volDum = vol2[3];
2927               vol2[3] = vol2[4];
2928               vol2[4] = volDum;
2929               fTOFGeometry->GetPosPar(vol2,pos);
2930               interestingX[1] = pos[0];
2931               interestingY[1] = pos[1];
2932               interestingZ[1] = pos[2];
2933
2934               interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
2935               interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
2936               interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
2937               interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
2938               Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
2939               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
2940               volDum = vol3[3];
2941               vol3[3] = vol3[4];
2942               vol3[4] = volDum;
2943               fTOFGeometry->GetPosPar(vol3,pos);
2944               interestingX[2] = pos[0];
2945               interestingY[2] = pos[1];
2946               interestingZ[2] = pos[2];
2947
2948               interestingTOF[3] = selectedDigit[adesso4][fourthIndex]->GetTDC();
2949               interestingTOT[3] = selectedDigit[adesso4][fourthIndex]->GetTOT();
2950               interestingADC[3] = selectedDigit[adesso4][fourthIndex]->GetADC();
2951               interestingWeight[3] = selectedDigit[adesso4][fourthIndex]->GetWeight();
2952               Int_t vol4[5]; for(jj=0; jj<5; jj++) vol4[jj] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(jj);
2953               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol4[0], vol4[1], vol4[2], vol4[4], vol4[3]));
2954               volDum = vol4[3];
2955               vol4[3] = vol4[4];
2956               vol4[4] = volDum;
2957               fTOFGeometry->GetPosPar(vol4,pos);
2958               interestingX[3] = pos[0];
2959               interestingY[3] = pos[1];
2960               interestingZ[3] = pos[2];
2961
2962
2963               AverageCalculations(interestingCounter+1,
2964                                   interestingX, interestingY, interestingZ,
2965                                   interestingTOF, interestingTOT, interestingADC,
2966                                   interestingWeight,
2967                                   parTOF, posClus, check);
2968
2969               for (jj=0; jj<5; jj++) det[jj] = -1;
2970               for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2971               fTOFGeometry->GetDetID(posF, det);
2972
2973               volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2974               //volIdClus = GetClusterVolIndex(det);
2975
2976               for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2977               padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2978               padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2979               padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2980               padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2981               padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
2982               padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
2983               padsCluster[9] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(4);
2984               padsCluster[10] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(3);
2985
2986               for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2987               Int_t ** indDet = new Int_t*[interestingCounter+1];
2988               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
2989               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
2990               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
2991               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
2992               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2993               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2994               GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
2995               for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
2996
2997               // To fill the track index array
2998               dummyCounter=-1;
2999               for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
3000               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3001                 if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
3002                 else {
3003                   dummyCounter++;
3004                   tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
3005                 }
3006               }
3007               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3008                 if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
3009                 else {
3010
3011                   alreadyStored = kFALSE;
3012                   for (jj=0; jj<dummyCounter+1; jj++)
3013                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
3014
3015                   if (alreadyStored) continue;
3016                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3017                     AliWarning("  Siamo al limite!");
3018                     continue;
3019                   }
3020
3021                   dummyCounter++;
3022                   tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
3023
3024                 }
3025
3026               }
3027               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3028                 if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
3029                 else {
3030
3031                   alreadyStored = kFALSE;
3032                   for (jj=0; jj<dummyCounter+1; jj++)
3033                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
3034
3035                   if (alreadyStored) continue;
3036                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3037                     AliWarning("  Siamo al limite!");
3038                     continue;
3039                   }
3040
3041                   dummyCounter++;
3042                   tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
3043
3044                 }
3045
3046               }
3047               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3048                 if (selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk)==-1) continue;
3049                 else {
3050
3051                   alreadyStored = kFALSE;
3052                   for (jj=0; jj<dummyCounter+1; jj++)
3053                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk));
3054
3055                   if (alreadyStored) continue;
3056                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3057                     AliWarning("  Siamo al limite!");
3058                     continue;
3059                   }
3060
3061                   dummyCounter++;
3062                   tracks[dummyCounter] = selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk);
3063
3064                 }
3065
3066               }
3067
3068
3069               AliTOFcluster *tofCluster =
3070                 new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
3071                                   covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
3072                                   tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
3073               InsertCluster(tofCluster);
3074
3075               AliDebug(2, Form("       %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", 
3076                                volIdClus, posClus[0], posClus[1], posClus[2],
3077                                covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
3078                                tracks[0], tracks[1], tracks[2],
3079                                det[0], det[1], det[2], det[3], det[4],
3080                                parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
3081                                status, selectedDigit[adesso1][firstIndex]->GetIndex()));
3082
3083               volDum = vol1[3];
3084               vol1[3] = vol1[4];
3085               vol1[4] = volDum;
3086               fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
3087               volDum = vol2[3];
3088               vol2[3] = vol2[4];
3089               vol2[4] = volDum;
3090               fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
3091               volDum = vol3[3];
3092               vol3[3] = vol3[4];
3093               vol3[4] = volDum;
3094               fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
3095               volDum = vol4[3];
3096               vol4[3] = vol4[4];
3097               vol4[4] = volDum;
3098               fTOFdigitMap->ResetDigitNumber(vol4,selectedDigit[adesso4][fourthIndex]->GetIndex());
3099
3100
3101             } // close loop on fourth digit
3102
3103           } // close loop on third digit
3104
3105         } // close loop on second digit
3106
3107       } // close loop on first digit
3108
3109       break;
3110
3111     }
3112
3113     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
3114       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
3115         selectedDigit[ii][jj] = 0x0;
3116
3117   } // loop on iPad
3118
3119   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
3120     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
3121       delete [] selectedDigit[ii][jj];
3122       selectedDigit[ii][jj] = 0x0;
3123     }
3124     delete [] selectedDigit[ii];
3125     selectedDigit[ii] = 0x0;
3126   }
3127   delete selectedDigit;
3128   selectedDigit = 0x0;
3129
3130 }
3131 //_____________________________________________________________________________
3132
3133 Int_t AliTOFClusterFinderV1::InsertCluster(AliTOFcluster *tofCluster)
3134 {
3135   //
3136   // This function adds a TOF cluster to the array of TOF clusters
3137   // sorted in Z, i.e. fTofClusters
3138   //
3139
3140   if (fNumberOfTofClusters==kTofMaxCluster) {
3141     AliError("Too many clusters !");
3142     return 1;
3143   }
3144
3145   if (fNumberOfTofClusters==0) {
3146     fTofClusters[fNumberOfTofClusters++] = tofCluster;
3147     return 0;
3148   }
3149
3150   Int_t ii = FindClusterIndex(tofCluster->GetZ());
3151   memmove(fTofClusters+ii+1 ,fTofClusters+ii,(fNumberOfTofClusters-ii)*sizeof(AliTOFcluster*));
3152   fTofClusters[ii] = tofCluster;
3153   fNumberOfTofClusters++;
3154   
3155   return 0;
3156
3157 }
3158 //_____________________________________________________________________________
3159
3160 Int_t AliTOFClusterFinderV1::FindClusterIndex(Double_t z) const
3161 {
3162   //
3163   // This function returns the index of the nearest cluster in z
3164   //
3165
3166   if (fNumberOfTofClusters==0) return 0;
3167   if (z <= fTofClusters[0]->GetZ()) return 0;
3168   if (z > fTofClusters[fNumberOfTofClusters-1]->GetZ()) return fNumberOfTofClusters;
3169   Int_t b = 0, e = fNumberOfTofClusters-1, m = (b+e)/2;
3170   for (; b<e; m=(b+e)/2) {
3171     if (z > fTofClusters[m]->GetZ()) b=m+1;
3172     else e=m;
3173   }
3174
3175   return m;
3176
3177 }
3178 //_____________________________________________________________________________
3179
3180 void AliTOFClusterFinderV1::ResetRecpoint()
3181 {
3182   //
3183   // Clear the list of reconstructed points
3184   //
3185
3186   fNumberOfTofClusters = 0;
3187   if (fRecPoints) fRecPoints->Clear();
3188
3189 }
3190 //_____________________________________________________________________________
3191
3192 void AliTOFClusterFinderV1::ResetDigits()
3193 {
3194   //
3195   // Clear the list of digits
3196   //
3197
3198   fNumberOfTofDigits = 0;
3199   if (fDigits) fDigits->Clear();
3200
3201 }
3202 //_____________________________________________________________________________
3203 //UShort_t AliTOFClusterFinderV1::GetClusterVolIndex(Int_t *ind) const
3204 //{
3205   //
3206   // Get the volume ID to retrieve the l2t transformation
3207   //
3208
3209   // Detector numbering scheme
3210 /*
3211   Int_t nSector = AliTOFGeometry::NSectors();
3212   Int_t nPlate  = AliTOFGeometry::NPlates();
3213   Int_t nStripA = AliTOFGeometry::NStripA();
3214   Int_t nStripB = AliTOFGeometry::NStripB();
3215   Int_t nStripC = AliTOFGeometry::NStripC();
3216
3217   Int_t isector =ind[0];
3218   if (isector >= nSector)
3219     AliError(Form("Wrong sector number in TOF (%d) !", isector));
3220   Int_t iplate = ind[1];
3221   if (iplate >= nPlate)
3222     AliError(Form("Wrong plate number in TOF (%d) !", iplate));
3223   Int_t istrip = ind[2];
3224
3225   Int_t stripOffset = 0;
3226   switch (iplate) {
3227   case 0:
3228     stripOffset = 0;
3229     break;
3230   case 1:
3231     stripOffset = nStripC;
3232     break;
3233   case 2:
3234     stripOffset = nStripC+nStripB;
3235     break;
3236   case 3:
3237     stripOffset = nStripC+nStripB+nStripA;
3238     break;
3239   case 4:
3240     stripOffset = nStripC+nStripB+nStripA+nStripB;
3241     break;
3242   default:
3243     AliError(Form("Wrong plate number in TOF (%d) !", iplate));
3244     break;
3245   };
3246
3247   Int_t index= (2*(nStripC+nStripB)+nStripA)*isector +
3248                stripOffset +
3249                istrip;
3250
3251   UShort_t volIndex = AliGeomManager::LayerToVolUID(AliGeomManager::kTOF, index);
3252   return volIndex;
3253
3254 }
3255 */
3256 //_____________________________________________________________________________
3257
3258 void AliTOFClusterFinderV1::GetClusterPars(Int_t *ind, Double_t* pos, Double_t* cov) const
3259 {
3260   //
3261   // Starting from the volume indices (ind[5]), for a cluster coming from
3262   // a isolated digits, this function returns:
3263   //   the cluster position (pos),
3264   //   the cluster covariance matrix elements (cov)
3265   // 
3266
3267   //
3268   //we now go in the system of the strip: determine the local coordinates
3269   //
3270   //
3271   // 47---------------------------------------------------0  ^ z
3272   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 1 |
3273   // -----------------------------------------------------   | y going outwards
3274   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 0 |  par[0]=0;
3275
3276   // -----------------------------------------------------   |
3277   // x <-----------------------------------------------------
3278
3279   //move to the tracking ref system
3280   Double_t lpos[3] = { (ind[4]-23.5)*AliTOFGeometry::XPad(),
3281                        0.,
3282                        (ind[3]- 0.5)*AliTOFGeometry::ZPad() };   
3283   AliDebug(1, Form(" %f %f %f", lpos[0], lpos[1], lpos[2]));
3284
3285   // Volume ID
3286   UShort_t volIndex = fTOFGeometry->GetAliSensVolIndex(ind[0],ind[1],ind[2]);
3287   //UShort_t volIndex = GetClusterVolIndex(ind);
3288   const TGeoHMatrix *l2t = AliGeomManager::GetTracking2LocalMatrix(volIndex);
3289
3290   // Get the position in the track ref system
3291   Double_t tpos[3];
3292   l2t->MasterToLocal(lpos,tpos);
3293   pos[0] = tpos[0];
3294   pos[1] = tpos[1];
3295   pos[2] = tpos[2];
3296
3297   //Get the cluster covariance in the track ref system
3298   Double_t lcov[9];
3299   for (Int_t ii=0; ii<9; ii++) lcov[ii] = 0.;
3300
3301   //cluster covariance in the local system:
3302   // sx2   0   0
3303   // 0     0   0
3304   // 0     0 sz2
3305   /*
3306   lcov[4] = 0.42*0.42/3.;
3307                        // = ( 5*0.025 (gas gaps thikness)
3308                        //   + 4*0.040 (internal glasses thickness)
3309                        //   + 0.5*0.160 (internl PCB)
3310                        //   + 1*0.055 (external red glass))
3311   */
3312
3313   lcov[0] = 0.499678;//AliTOFGeometry::XPad()*AliTOFGeometry::XPad()/12.;
3314   lcov[8] = 0.992429;//AliTOFGeometry::ZPad()*AliTOFGeometry::ZPad()/12.;
3315
3316   //cluster covariance in the tracking system:
3317   TGeoHMatrix m;
3318   m.SetRotation(lcov);
3319   m.Multiply(l2t);
3320   m.MultiplyLeft(&l2t->Inverse());
3321   Double_t *tcov = m.GetRotationMatrix();
3322   cov[0] = tcov[0]; cov[1] = tcov[1]; cov[2] = tcov[2];
3323   cov[3] = tcov[4]; cov[4] = tcov[5]; cov[5] = tcov[8];
3324
3325   return;
3326
3327 }
3328 //_____________________________________________________________________________
3329
3330 void AliTOFClusterFinderV1::GetClusterPars(/*Bool_t check,*/ Int_t counter,
3331                                            Int_t **ind, Double_t *weight,
3332                                            Double_t *pos, Double_t *cov) const
3333 {
3334   //
3335   // Starting from:
3336   //               the volumes indices (ind[counter][5]), for a
3337   //                  cluster coming from a collection of 'counter'
3338   //                  digits,
3339   //               the volumes weights (weight[counter]), -controlled
3340   //                  by the 'check' variable control-, for a cluster
3341   //                  coming from a collection of 'counter' digits,
3342   //               the cluster position (pos),
3343   // this function returns:
3344   //   the covariance matrix elements (cov) for the found cluster
3345   //
3346
3347   //
3348   // we now go in the system of the strip: determine the local coordinates
3349   //
3350   // 47---------------------------------------------------0  ^ z
3351   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 1 |
3352   // -----------------------------------------------------   | y going outwards
3353   // | | | | | | | | | | | | | | | | | | | | | | | | | | | 0 |  par[0]=0;
3354
3355   // -----------------------------------------------------   |
3356   // x <-----------------------------------------------------
3357
3358   for (Int_t ii=0; ii<counter; ii++)
3359     AliDebug(1, Form(" %2d  %2d %1d %2d %1d %2d ",
3360                      ii, ind[ii][0], ind[ii][1], ind[ii][2], ind[ii][3], ind[ii][4]));
3361
3362   Float_t posF[3]; for (Int_t ii=0; ii<3; ii++) posF[ii] = (Float_t)pos[ii];
3363   AliDebug(1, Form(" %f %f %f", pos[0], pos[1], pos[2]));
3364
3365   Int_t detClus[5] = {-1, -1, -1, -1, -1};
3366   fTOFGeometry->GetDetID(posF, detClus);
3367
3368   // Volume ID
3369   UShort_t volIndex = fTOFGeometry->GetAliSensVolIndex(detClus[0],detClus[1],detClus[2]);
3370   //UShort_t volIndex = GetClusterVolIndex(detClus);
3371   AliDebug(1, Form(" %2d %1d %2d %1d %2d  %7i",
3372                    detClus[0], detClus[1], detClus[2], detClus[3], detClus[4], volIndex));
3373
3374   // Get the position in the TOF strip ref system
3375   const TGeoHMatrix *alice2strip = AliGeomManager::GetOrigGlobalMatrix(volIndex);
3376   Double_t ppos[3] = {-1, -1, -1};
3377   alice2strip->MasterToLocal(pos,ppos);
3378   AliDebug(1, Form(" %f %f %f", ppos[0], ppos[1], ppos[2]));
3379
3380
3381   // Get the position in the tracking ref system
3382   const TGeoHMatrix *g2l = AliGeomManager::GetTracking2LocalMatrix(volIndex);
3383   Double_t lpos[3] = {-1, -1, -1};
3384   g2l->MasterToLocal(ppos,lpos);
3385   AliDebug(1, Form(" %f %f %f", lpos[0], lpos[1], lpos[2]));
3386   for (Int_t ii=0; ii<3; ii++) pos[ii] = lpos[ii];
3387
3388   //Get the cluster covariance in the track ref system
3389   Double_t lcov[9];
3390   for (Int_t ii=0; ii<9; ii++) lcov[ii] = 0.;
3391
3392   //cluster covariance in the local system:
3393   // sx2   0   0
3394   // 0     0   0
3395   // 0     0 sz2
3396
3397   // Evaluation of the ovariance matrix elements
3398   TOFclusterError(/*check,*/ counter, ind, weight, ppos, lcov);
3399
3400   AliDebug(1, Form("lcov[0] = %f, lcov[8] = %f", lcov[0], lcov[8]));
3401
3402   //cluster covariance in the tracking system:
3403   TGeoHMatrix m;
3404   m.SetRotation(lcov);
3405   m.Multiply(g2l);
3406   m.MultiplyLeft(&g2l->Inverse());
3407   Double_t *tcov = m.GetRotationMatrix();
3408   cov[0] = tcov[0]; cov[1] = tcov[1]; cov[2] = tcov[2];
3409   cov[3] = tcov[4]; cov[4] = tcov[5]; cov[5] = tcov[8];
3410
3411   return;
3412
3413 }
3414 //_____________________________________________________________________________
3415
3416 void AliTOFClusterFinderV1::TOFclusterError(/*Bool_t check,*/ Int_t counter,
3417                                             Int_t **ind, Double_t *weight,
3418                                             Double_t ppos[], Double_t lcov[]) const
3419 {
3420   //
3421   //
3422   //
3423
3424   //lcov[4] = 0.42*0.42/3.; // cm2
3425                        // = ( 5*0.025 (gas gaps thikness)
3426                        //   + 4*0.040 (internal glasses thickness)
3427                        //   + 0.5*0.160 (internl PCB)
3428                        //   + 1*0.055 (external red glass))
3429
3430
3431   Float_t *delta2X = new Float_t[counter];
3432   for (Int_t ii=0; ii<counter; ii++)
3433     delta2X[ii] =
3434       ((ind[ii][4]-23.5)*AliTOFGeometry::XPad() - ppos[0])*((ind[ii][4]-23.5)*AliTOFGeometry::XPad() - ppos[0]);
3435
3436   Float_t *delta2Z = new Float_t[counter];
3437   for (Int_t ii=0; ii<counter; ii++)
3438     delta2Z[ii] =
3439       ((ind[ii][3]- 0.5)*AliTOFGeometry::ZPad() - ppos[2])*((ind[ii][3]- 0.5)*AliTOFGeometry::ZPad() - ppos[2]);
3440
3441   for (Int_t ii=0; ii<counter; ii++)
3442     AliDebug(1, Form("x[%d] = %f, z[%d] = %f, weight[%d] = %f",
3443                      ii, (ind[ii][4]-23.5)*AliTOFGeometry::XPad(),
3444                      ii, (ind[ii][3]- 0.5)*AliTOFGeometry::ZPad(),
3445                      ii, weight[ii]
3446                      ));
3447   AliDebug(1, Form("xMean = %f, zMean = %f",ppos[0], ppos[2]));
3448
3449
3450   switch (counter)
3451     {
3452
3453     case 2:
3454
3455       if (ind[0][3]==ind[1][3] && TMath::Abs(ind[0][4]-ind[1][4])==1) { //
3456
3457         lcov[8] = 1.02039; // cm2
3458         lcov[0] = 0.0379409; // cm2
3459         /*
3460         if (check)
3461           lcov[0] = 0.5*0.5; // cm2
3462         else {
3463           if (weight[0]==weight[1])
3464             lcov[0] = 0.0379409; // cm2
3465           else
3466             lcov[0] = TMath::Mean(counter, delta2X, weight); // cm2
3467         }
3468         */
3469
3470       }
3471
3472       else if (ind[0][4]==ind[1][4] && TMath::Abs(ind[0][3]-ind[1][3])==1) {//
3473
3474         lcov[0] = 0.505499; // cm2
3475         lcov[8] = 0.0422046; // cm2
3476         /*
3477         if (check)
3478           lcov[8] = 0.5*0.5; // cm2
3479         else {
3480           if (weight[0]==weight[1])
3481             lcov[8] = 0.0422046; // cm2
3482           else
3483             lcov[8] = TMath::Mean(counter, delta2Z, weight); // cm2
3484         }
3485         */
3486
3487       }
3488
3489       break;
3490
3491     case 3:
3492       lcov[0] = 0.0290677; // cm2
3493       lcov[8] = 0.0569726; // cm2
3494       /*
3495       if (check) {
3496         lcov[0] = 0.5*0.5; // cm2
3497         lcov[8] = 0.5*0.5; // cm2
3498       }
3499       else {
3500       if (weight[0]==weight[1] && weight[0]==weight[2]) {
3501           lcov[0] = 0.0290677; // cm2
3502           lcov[8] = 0.0569726; // cm2
3503           }
3504         else {
3505           lcov[0] = TMath::Mean(counter, delta2X, weight); // cm2
3506           lcov[8] = TMath::Mean(counter, delta2Z, weight); // cm2
3507           }
3508
3509         }
3510       */
3511
3512       break;
3513
3514     case 4:
3515       lcov[0] = 0.0223807; // cm2
3516       lcov[8] = 0.0438662; // cm2
3517       /*
3518       if (check) {
3519         lcov[0] = 0.5*0.5; // cm2
3520         lcov[8] = 0.5*0.5; // cm2
3521       }
3522       else {
3523       if (weight[0]==weight[1] && weight[0]==weight[2] && weight[0]==weight[3]) {
3524           lcov[0] = 0.0223807; // cm2
3525           lcov[8] = 0.0438662; // cm2
3526           }
3527         else {
3528           lcov[0] = TMath::Mean(counter, delta2X, weight); // cm2
3529           lcov[8] = TMath::Mean(counter, delta2Z, weight); // cm2
3530           }
3531
3532         }
3533       */
3534
3535       break;
3536
3537     }
3538
3539   delete [] delta2Z;
3540   delete [] delta2X;
3541
3542 }
3543 //_____________________________________________________________________________
3544
3545 Bool_t AliTOFClusterFinderV1::MakeSlewingCorrection(Int_t *detectorIndex,
3546                                                     Int_t tofDigitToT,
3547                                                     Int_t tofDigitTdc,
3548                                                     Int_t &tdcCorr)
3549 {
3550   //
3551   // This funtion makes the following:
3552   //
3553   //      - if at least one of the three status (Pulser/Noise/HW) is
3554   //        bad, is sets the status of electronic channel, corresponding to the
3555   //        volume identified by detectorIndex, as kFALSE;
3556   //      - if offline calibration is in the valid status, it performs the
3557   //        slewing correction. In particular, by taking into account:
3558   //          * the measured tot and tof values (tofDigitToT and tofDigitTdc,
3559   //            respectively);
3560   //          * the six parameters of 5th order polynomial used
3561   //            to fit the tofVStot scatter plot,
3562   //         it returns the corrected tof value, i.e. tdcCorr value.
3563   //
3564
3565   Bool_t output = kTRUE;
3566
3567   Double_t timeCorr;
3568   Int_t jj;
3569
3570   //AliInfo(" Calibrating TOF Digits: ");
3571   
3572   AliTOFChannelOnlineArray *calDelay = fTOFcalib->GetTOFOnlineDelay();
3573   AliTOFChannelOnlineStatusArray *calStatus = fTOFcalib->GetTOFOnlineStatus();
3574
3575   TObjArray *calTOFArrayOffline = fTOFcalib->GetTOFCalArrayOffline();
3576
3577   Int_t index = AliTOFGeometry::GetIndex(detectorIndex);
3578
3579   UChar_t statusPulser = calStatus->GetPulserStatus(index);
3580   UChar_t statusNoise  = calStatus->GetNoiseStatus(index);
3581   UChar_t statusHW     = calStatus->GetHWStatus(index);
3582   UChar_t status       = calStatus->GetStatus(index);
3583
3584   //check the status, also unknown is fine!!!!!!!
3585
3586   AliDebug(2, Form(" Status for channel %d = %d",index, (Int_t)status));
3587   if((statusPulser & AliTOFChannelOnlineStatusArray::kTOFPulserBad)==(AliTOFChannelOnlineStatusArray::kTOFPulserBad)||(statusNoise & AliTOFChannelOnlineStatusArray::kTOFNoiseBad)==(AliTOFChannelOnlineStatusArray::kTOFNoiseBad)||(statusHW & AliTOFChannelOnlineStatusArray::kTOFHWBad)==(AliTOFChannelOnlineStatusArray::kTOFHWBad)){
3588     AliDebug(2, Form(" Bad Status for channel %d",index));
3589     //fTofClusters[ii]->SetStatus(kFALSE); //odd convention, to avoid conflict with calibration objects currently in the db (temporary solution).
3590     output = kFALSE;
3591   }
3592   else
3593     AliDebug(2, Form(" Good Status for channel %d",index));
3594
3595
3596   if (fCalibrateTOFtimes) { // AdC
3597
3598   // Get Rough channel online equalization 
3599   Double_t roughDelay = (Double_t)calDelay->GetDelay(index);  // in ns
3600   AliDebug(2,Form(" channel delay (ns) = %f", roughDelay));
3601   // Get Refined channel offline calibration parameters
3602   TString validity = (TString)fTOFcalib->GetOfflineValidity();
3603   if (validity.CompareTo("valid")==0) {
3604     AliTOFChannelOffline * calChannelOffline = (AliTOFChannelOffline*)calTOFArrayOffline->At(index);
3605     Double_t par[6];
3606     for (jj = 0; jj<6; jj++)
3607       par[jj] = (Double_t)calChannelOffline->GetSlewPar(jj);
3608
3609     AliDebug(2,Form(" Calib Pars = %f, %f, %f, %f, %f, %f ",par[0],par[1],par[2],par[3],par[4],par[5]));
3610     AliDebug(2,Form(" The ToT and Time, uncorr (counts) = %d , %d", tofDigitToT, tofDigitTdc));
3611     Double_t tToT = (Double_t)(tofDigitToT*AliTOFGeometry::ToTBinWidth());    
3612     tToT*=1.E-3; //ToT in ns
3613     AliDebug(2,Form(" The ToT and Time, uncorr (ns)= %e, %e",tofDigitTdc*AliTOFGeometry::TdcBinWidth()*1.E-3,tToT));
3614     timeCorr = par[0] + tToT*(par[1] + tToT*(par[2] + tToT*(par[3] + tToT*(par[4] + tToT*par[5])))); // the time correction (ns)
3615   }
3616   else
3617     timeCorr = roughDelay; // correction in ns
3618
3619   AliDebug(2,Form(" The ToT and Time, uncorr (ns)= %e, %e",tofDigitTdc*AliTOFGeometry::TdcBinWidth()*1.E-3,tofDigitToT*AliTOFGeometry::ToTBinWidth()));
3620   AliDebug(2,Form(" The time correction (ns) = %f", timeCorr));
3621   timeCorr = (Double_t)(tofDigitTdc)*AliTOFGeometry::TdcBinWidth()*1.E-3-timeCorr;//redefine the time
3622   timeCorr *= 1.E3;
3623   AliDebug(2,Form(" The channel time, corr (ps)= %e",timeCorr ));
3624   tdcCorr = (Int_t)(timeCorr/AliTOFGeometry::TdcBinWidth()); //the corrected time (tdc counts)
3625   
3626   } // AdC
3627
3628   return output;
3629
3630 }
3631 //______________________________________________________________________________
3632
3633 void AliTOFClusterFinderV1::Digits2RecPoints(Int_t iEvent)
3634 {
3635   //
3636   // Converts digits to recpoints for TOF
3637   //
3638
3639   TStopwatch stopwatch;
3640   stopwatch.Start();
3641
3642   fRunLoader->GetEvent(iEvent);
3643
3644   AliLoader *localTOFLoader = (AliLoader*)fRunLoader->GetLoader("TOFLoader");
3645
3646   TTree * localTreeD = (TTree*)localTOFLoader->TreeD();
3647   if (localTreeD == 0x0)
3648     AliFatal("Can not get TreeD");
3649
3650   TBranch *branch = localTreeD->GetBranch("TOF");
3651   if (!branch) {
3652     AliError("Can't get the branch with the TOF digits !");
3653     return;
3654   }
3655
3656   TTree *localTreeR = (TTree*)localTOFLoader->TreeR();
3657   if (localTreeR == 0x0)
3658     {
3659       localTOFLoader->MakeTree("R");
3660       localTreeR = localTOFLoader->TreeR();
3661     }
3662
3663   Digits2RecPoints(localTreeD, localTreeR);
3664
3665   //localTOFLoader = fRunLoader->GetLoader("TOFLoader");  
3666   localTOFLoader->WriteRecPoints("OVERWRITE");
3667
3668   AliDebug(1, Form("Execution time to read TOF digits and to write TOF clusters for the event number %d: R:%.4fs C:%.4fs",
3669                    iEvent, stopwatch.RealTime(),stopwatch.CpuTime()));
3670
3671 }
3672 //______________________________________________________________________________
3673
3674 void AliTOFClusterFinderV1::Digits2RecPoints(Int_t iEvent, AliRawReader *rawReader)
3675 {
3676   //
3677   // Converts RAW data to recpoints for TOF
3678   //
3679
3680   TStopwatch stopwatch;
3681   stopwatch.Start();
3682
3683   fRunLoader->GetEvent(iEvent);
3684
3685   AliDebug(2,Form(" Event number %2d ", iEvent));
3686
3687   AliLoader *localTOFLoader = (AliLoader*)fRunLoader->GetLoader("TOFLoader");
3688
3689   TTree *localTreeR = localTOFLoader->TreeR();
3690
3691   if (localTreeR == 0x0){
3692     localTOFLoader->MakeTree("R");
3693     localTreeR = localTOFLoader->TreeR();
3694   }
3695
3696   Digits2RecPoints(rawReader, localTreeR);
3697
3698   AliDebug(1, Form("Execution time to read TOF raw data and to write TOF clusters for the event number %d: R:%.4fs C:%.4fs",
3699                    iEvent, stopwatch.RealTime(),stopwatch.CpuTime()));
3700
3701 }
3702 //______________________________________________________________________________
3703
3704 void AliTOFClusterFinderV1::Raw2Digits(Int_t iEvent, AliRawReader *rawReader)
3705 {
3706   //
3707   // Converts RAW data to MC digits for TOF
3708   //
3709
3710
3711   TStopwatch stopwatch;
3712   stopwatch.Start();
3713
3714   fRunLoader->GetEvent(iEvent);
3715
3716   AliDebug(2,Form(" Event number %2d ", iEvent));
3717
3718   AliLoader *localTOFLoader = (AliLoader*)fRunLoader->GetLoader("TOFLoader");
3719
3720   TTree *localTreeD = localTOFLoader->TreeD();
3721
3722   if (localTreeD == 0x0){
3723     localTOFLoader->MakeTree("D");
3724     localTreeD = localTOFLoader->TreeD();
3725   }
3726
3727   Raw2Digits(rawReader, localTreeD);
3728
3729   AliDebug(1, Form("Execution time to read TOF raw data and to write TOF clusters for the event number %d: R:%.4fs C:%.4fs",
3730                    iEvent, stopwatch.RealTime(),stopwatch.CpuTime()));
3731
3732 }
3733 //______________________________________________________________________________
3734
3735 void AliTOFClusterFinderV1::AverageCalculations(Int_t number, Float_t *interestingX,
3736                                                 Float_t *interestingY, Float_t *interestingZ,
3737                                                 Double_t *interestingTOF, Double_t *interestingTOT,
3738                                                 Double_t *interestingADC, Double_t *interestingWeight,
3739                                                 Int_t *parTOF, Double_t *posClus, Bool_t &check)
3740 {
3741   //
3742   // Calculates the mean values for cluster position (x,y,z),
3743   //  TOF charge and time
3744   //
3745
3746   Double_t tofAverage = 0.;
3747   Double_t totAverage = 0.;
3748   Double_t adcAverage = 0.;
3749
3750   check = kFALSE;
3751   Int_t ii=-1;
3752   for (ii=number-1; ii>=0; ii--) check=check||(interestingWeight[ii]==0 || interestingWeight[ii]==-1);
3753
3754   if (check) {
3755                   
3756     posClus[0] = TMath::Mean(number, interestingX);
3757     posClus[1] = TMath::Mean(number, interestingY);
3758     posClus[2] = TMath::Mean(number, interestingZ);
3759     tofAverage = TMath::Mean(number, interestingTOF);
3760     totAverage = TMath::Mean(number, interestingTOT);
3761     adcAverage = TMath::Mean(number, interestingADC);
3762
3763   }
3764   else {
3765
3766     posClus[0] = TMath::Mean(number, interestingX, interestingWeight);
3767     posClus[1] = TMath::Mean(number, interestingY, interestingWeight);
3768     posClus[2] = TMath::Mean(number, interestingZ, interestingWeight);
3769     tofAverage = TMath::Mean(number, interestingTOF, interestingWeight);
3770     totAverage = TMath::Mean(number, interestingTOT, interestingWeight);
3771     adcAverage = TMath::Mean(number, interestingADC, interestingWeight);
3772
3773   }
3774
3775   parTOF[0] = Int_t(tofAverage);
3776   parTOF[1] = Int_t(totAverage);
3777   parTOF[2] = Int_t(adcAverage);
3778   parTOF[3] = Int_t(tofAverage);//tofND
3779   parTOF[4] = Int_t(tofAverage);//tofRAW
3780
3781 }