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