First implementation of EMCAL trigger QA from Nicola Arbor
[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 "AliLoader.h"
48 #include "AliRunLoader.h"
49 #include "AliGeomManager.h"
50
51 #include "AliTOFcalib.h"
52 #include "AliTOFChannelOnlineArray.h"
53 #include "AliTOFChannelOnlineStatusArray.h"
54 #include "AliTOFChannelOffline.h"
55 #include "AliTOFClusterFinderV1.h"
56 #include "AliTOFcluster.h"
57 #include "AliTOFdigit.h"
58 #include "AliTOFDigitMap.h"
59 #include "AliTOFrawData.h"
60 #include "AliTOFReconstructor.h"
61 #include "AliTOFRecoParam.h"
62
63 ClassImp(AliTOFClusterFinderV1)
64
65 //_____________________________________________________________________________
66 AliTOFClusterFinderV1::AliTOFClusterFinderV1(AliTOFcalib *calib):
67   TTask("AliTOFClusterFinderV1",""),
68   fRunLoader(0),
69   fDigits(new TClonesArray("AliTOFdigit", 4000)),
70   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
71   fNumberOfTofClusters(0),
72   fNumberOfTofDigits(0),
73   fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
74   fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
75   fVerbose(0),
76   fDecoderVersion(0),
77   fTOFcalib(calib),
78   fTOFdigitMap(new AliTOFDigitMap()),
79   fTOFGeometry(new AliTOFGeometry()),
80   fTOFdigits(new TTree()),
81   fTOFRawStream(AliTOFRawStream()),
82   fCalibrateTOFtimes(1)
83 {
84 //
85 // Constructor
86 //
87
88   for (Int_t ii=0; ii<kTofMaxCluster; ii++) fTofClusters[ii]=0x0;
89
90   if (AliTOFReconstructor::GetRecoParam()) {
91     fkRecoParam = AliTOFReconstructor::GetRecoParam();
92     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
93   }
94   else
95     fMaxDeltaTime = 2;
96
97   TString validity = (TString)fTOFcalib->GetOfflineValidity();
98   if (validity.CompareTo("valid")==0) {
99     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
100   } else {
101     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
102   }
103
104 }
105
106 //_____________________________________________________________________________
107 AliTOFClusterFinderV1::AliTOFClusterFinderV1(AliRunLoader* runLoader, AliTOFcalib *calib):
108   TTask("AliTOFClusterFinderV1",""),
109   fRunLoader(runLoader),
110   fDigits(new TClonesArray("AliTOFdigit", 4000)),
111   fRecPoints(new TClonesArray("AliTOFcluster", 4000)),
112   fNumberOfTofClusters(0),
113   fNumberOfTofDigits(0),
114   fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
115   fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
116   fVerbose(0),
117   fDecoderVersion(0),
118   fTOFcalib(calib),
119   fTOFdigitMap(new AliTOFDigitMap()),
120   fTOFGeometry(new AliTOFGeometry()),
121   fTOFdigits(new TTree()),
122   fTOFRawStream(AliTOFRawStream()),
123   fCalibrateTOFtimes(1)
124 {
125 //
126 // Constructor
127 //
128
129   for (Int_t ii=0; ii<kTofMaxCluster; ii++) fTofClusters[ii]=0x0;
130
131   if (AliTOFReconstructor::GetRecoParam()) {
132     fkRecoParam = AliTOFReconstructor::GetRecoParam();
133     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
134   }
135   else
136     fMaxDeltaTime = 2;
137
138   TString validity = (TString)fTOFcalib->GetOfflineValidity();
139   if (validity.CompareTo("valid")==0) {
140     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
141   } else {
142     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
143   }
144
145 }
146 //_____________________________________________________________________________
147
148 AliTOFClusterFinderV1::AliTOFClusterFinderV1(const AliTOFClusterFinderV1 &source)
149   :TTask(source),
150    fRunLoader(0),
151    fDigits(source.fDigits),
152    fRecPoints(source.fRecPoints),
153    fNumberOfTofClusters(0),
154    fNumberOfTofDigits(0),
155    fkRecoParam(0),//AliTOFReconstructor::GetRecoParam()),
156    fMaxDeltaTime(0),//fkRecoParam->GetMaxDeltaTime()),
157    fVerbose(0),
158    fDecoderVersion(source.fDecoderVersion),
159    fTOFcalib(source.fTOFcalib),
160    fTOFdigitMap(new AliTOFDigitMap()),
161    fTOFGeometry(new AliTOFGeometry()),
162    fTOFdigits(source.fTOFdigits),
163    fTOFRawStream(source.fTOFRawStream),
164    fCalibrateTOFtimes(1)
165 {
166   // copy constructor
167
168   for (Int_t ii=0; ii<kTofMaxCluster; ii++) fTofClusters[ii]=source.fTofClusters[ii];
169
170   if (AliTOFReconstructor::GetRecoParam()) {
171     fkRecoParam = AliTOFReconstructor::GetRecoParam();
172     fMaxDeltaTime = fkRecoParam->GetMaxDeltaTime();
173   }
174   else
175     fMaxDeltaTime = 2;
176
177 }
178 //_____________________________________________________________________________
179
180 AliTOFClusterFinderV1& AliTOFClusterFinderV1::operator=(const AliTOFClusterFinderV1 &source)
181 {
182   // ass. op.
183
184   if (this == &source)
185     return *this;
186
187   TObject::operator=(source);
188   for (Int_t ii=0; ii<kTofMaxCluster; ii++) fTofClusters[ii]=source.fTofClusters[ii];
189   fDigits=source.fDigits;
190   fRecPoints=source.fRecPoints;
191   fVerbose=source.fVerbose;
192   fDecoderVersion=source.fDecoderVersion;
193   fTOFcalib=source.fTOFcalib;
194   fTOFdigitMap=source.fTOFdigitMap;
195   fTOFGeometry=source.fTOFGeometry;
196   fTOFdigits=source.fTOFdigits;
197   fTOFRawStream=source.fTOFRawStream;
198   fCalibrateTOFtimes=source.fCalibrateTOFtimes;
199   return *this;
200
201 }
202 //_____________________________________________________________________________
203
204 AliTOFClusterFinderV1::~AliTOFClusterFinderV1()
205 {
206
207   //
208   // Destructor
209   //
210
211   if (fDigits)
212     {
213       fDigits->Delete();
214       delete fDigits;
215       fDigits=0;
216     }
217   if (fRecPoints)
218     {
219       fRecPoints->Delete();
220       delete fRecPoints;
221       fRecPoints=0;
222     }
223
224   delete fTOFdigitMap;
225
226   delete fTOFGeometry;
227
228   delete fTOFdigits;
229
230   //if (fTofClusters || fNumberOfTofClusters) {
231   if (fNumberOfTofClusters) {
232     for (Int_t ii=0; ii<fNumberOfTofClusters; ii++)
233       if (fTofClusters[ii]) fTofClusters[ii]->Delete();
234     fNumberOfTofClusters = 0;
235   }
236
237 }
238 //_____________________________________________________________________________
239
240 void AliTOFClusterFinderV1::Digits2RecPoints(TTree* digitsTree, TTree* clusterTree)
241 {
242   //
243   // Converts digits to recPoints for TOF
244   //
245
246   TStopwatch stopwatch;
247   stopwatch.Start();
248
249   Int_t inholes = 0;
250
251   fDigits->Clear();
252   TClonesArray &aDigits = *fDigits;
253
254   if (digitsTree == 0x0) {
255     AliFatal("Can not get TreeD for TOF");
256     return;
257   }
258
259   TBranch *branch = digitsTree->GetBranch("TOF");
260   if (!branch) {
261     AliError("Can not get branch with the TOF digits !");
262     return;
263   }
264
265   TClonesArray staticDigits("AliTOFdigit",10000);
266   staticDigits.Clear();
267   TClonesArray *digits = &staticDigits;
268   branch->SetAddress(&digits);
269   digitsTree->GetEvent(0);
270   AliDebug(1,Form("Number of TOF digits: %d", digits->GetEntriesFast()));
271
272   AliTOFdigit *tofDigit;
273
274   Int_t jj = 0;
275   Int_t detectorIndex[5];
276   for (jj=0; jj<5; jj++) detectorIndex[jj] = -1;
277   Int_t info[4];
278   for (jj=0; jj<4; jj++) info[jj] = -1;
279   Int_t *tracks;
280   Int_t tdcCorr;
281   Int_t dummy = -1;
282   Int_t last = -1;
283
284   Bool_t status = kTRUE;
285
286   AliDebug(1," Calibrating TOF Digits");
287   /*
288   TString validity = (TString)fTOFcalib->GetOfflineValidity();
289   if (validity.CompareTo("valid")==0) {
290     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
291   } else
292     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
293   */
294
295   Int_t ii = 0;
296   for (ii=0; ii<digits->GetEntriesFast(); ii++) {
297     tofDigit = (AliTOFdigit*)digits->UncheckedAt(ii);
298     detectorIndex[0] = tofDigit->GetSector();
299     detectorIndex[1] = tofDigit->GetPlate();
300     detectorIndex[2] = tofDigit->GetStrip();
301     detectorIndex[3] = tofDigit->GetPadz();
302     detectorIndex[4] = tofDigit->GetPadx();
303
304     if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
305       if (detectorIndex[1]==2) { // plate with holes
306         inholes++;
307         continue;
308       }
309     }
310
311     tdcCorr = tofDigit->GetTdc();
312     status = MakeSlewingCorrection(detectorIndex, tofDigit->GetToT(), tofDigit->GetTdc(), tdcCorr);
313
314     for (jj=0; jj<4; jj++) info[jj] = -1;
315     info[0] = tdcCorr;//tofDigit->GetTdc();
316     info[1] = tofDigit->GetAdc();
317     info[2] = tofDigit->GetToT();
318     info[3] = tofDigit->GetTdcND();//tofDigit->GetTdc();//
319     tracks  = tofDigit->GetTracks();
320
321     dummy = detectorIndex[3];
322     detectorIndex[3] = detectorIndex[4];//padx
323     detectorIndex[4] = dummy;//padz
324     last = fDigits->GetEntriesFast();
325     new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, info);
326     if (status) fTOFdigitMap->AddDigit(detectorIndex, last);
327
328     AliDebug(2, Form(" Digits reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
329                      last,
330                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[3], detectorIndex[4],
331                      info[0], info[1], info[3]));
332
333   }
334
335   fNumberOfTofDigits = fDigits->GetEntriesFast();
336
337   ResetRecpoint();
338
339   Int_t bufsize = 32000;
340   clusterTree->Branch("TOF", &fRecPoints, bufsize);
341
342   FillRecPoint();
343   clusterTree->Fill();
344
345   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
346
347   ResetRecpoint();
348
349   fTOFdigitMap->Clear();
350
351   ResetDigits();
352
353   AliDebug(1,Form("Execution time to read TOF digits and to write TOF clusters : R:%.4fs C:%.4fs",
354                   stopwatch.RealTime(),stopwatch.CpuTime()));
355
356   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
357
358 }
359 //_____________________________________________________________________________
360
361 void AliTOFClusterFinderV1::Digits2RecPoints(AliRawReader *rawReader, TTree *clustersTree)
362 {
363   //
364   // Converts raw data to recPoints for TOF
365   //
366
367   TStopwatch stopwatch;
368   stopwatch.Start();
369
370
371   AliDebug(2, "TreeD re-creation");
372   //TTree *digitsTree = new TTree();
373   //Raw2Digits(rawReader, digitsTree);
374
375   Raw2Digits(rawReader, fTOFdigits);
376
377   AliDebug(1,Form("Number of TOF digits: %d", fNumberOfTofDigits));
378   ResetRecpoint();
379
380   Int_t bufsize = 32000;
381   clustersTree->Branch("TOF", &fRecPoints, bufsize);
382   FillRecPoint();
383
384   clustersTree->Fill();
385
386   AliDebug(1,Form("Number of found clusters: %d", fNumberOfTofClusters));
387
388   ResetRecpoint();
389
390   fTOFdigitMap->Clear();
391
392   ResetDigits();
393
394   AliDebug(1,Form("Execution time to read TOF raw data and to write TOF clusters : R:%.4fs C:%.4fs",
395                   stopwatch.RealTime(),stopwatch.CpuTime()));
396
397 }
398
399 //_____________________________________________________________________________
400
401 void AliTOFClusterFinderV1::Raw2Digits(AliRawReader *rawReader, TTree* digitsTree)
402 {
403   //
404   // Converts raw data to digits for TOF
405   //
406
407   TStopwatch stopwatch;
408   stopwatch.Start();
409
410   Int_t inholes = 0;
411
412   const Int_t kMaxNumberOfTracksPerDigit = 3;
413   const Int_t kDDL = AliDAQ::NumberOfDdls("TOF");
414
415   digitsTree->Branch("TOF", &fDigits);
416   TClonesArray &aDigits = *fDigits;
417
418   fTOFRawStream.Clear();
419   fTOFRawStream.SetRawReader(rawReader);
420
421   ofstream ftxt;
422   if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
423
424   TClonesArray staticRawData("AliTOFrawData",10000);
425   staticRawData.Clear();
426   TClonesArray * clonesRawData = &staticRawData;
427
428   Int_t dummy = -1;
429   Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
430   Int_t digit[4];
431   Int_t tracks[kMaxNumberOfTracksPerDigit];
432   for (Int_t ii=0; ii<kMaxNumberOfTracksPerDigit; ii++)
433     tracks[ii] = -1;
434   Int_t last = -1;
435   Int_t tdcCorr = 0;
436
437   Bool_t status = kTRUE;
438
439   /*
440   TString validity = (TString)fTOFcalib->GetOfflineValidity();
441   if (validity.CompareTo("valid")==0) {
442     AliInfo(Form(" validity = %s - Using offline calibration parameters", validity.Data()));
443   } else
444     AliInfo(Form(" validity = %s - Using online calibration parameters", validity.Data()));
445   */
446
447   if (fDecoderVersion)
448     AliInfo("Using New Decoder");
449
450   Int_t indexDDL = 0;
451   Int_t iRawData = 0;
452   for (indexDDL=0; indexDDL<kDDL; indexDDL++) {
453
454     rawReader->Reset();
455     if (fDecoderVersion)
456       fTOFRawStream.LoadRawDataBuffers(indexDDL,fVerbose);
457     else fTOFRawStream.LoadRawData(indexDDL);
458
459     clonesRawData = (TClonesArray*)fTOFRawStream.GetRawData();
460     if (clonesRawData->GetEntriesFast()!=0) AliDebug(2,Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
461     for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
462
463       AliTOFrawData *tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
464
465       //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
466       if (tofRawDatum->GetTOF()==-1) continue;
467
468       fTOFRawStream.EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
469                                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
470
471       tdcCorr = 0;
472       dummy = detectorIndex[3];
473       detectorIndex[3] = detectorIndex[4];//padz
474       detectorIndex[4] = dummy;//padx
475
476       tdcCorr = tofRawDatum->GetTOF();
477       status = MakeSlewingCorrection(detectorIndex, tofRawDatum->GetTOT(), tofRawDatum->GetTOF(), tdcCorr);
478
479       digit[0] = tdcCorr;
480       digit[1] = tofRawDatum->GetTOT();
481       digit[2] = tofRawDatum->GetTOT();
482       digit[3] = -1;//tofRawDatum->GetTOF(); //tofND
483
484       dummy = detectorIndex[3];
485       detectorIndex[3] = detectorIndex[4];//padx
486       detectorIndex[4] = dummy;//padz
487
488       /* check valid index */
489       if (detectorIndex[0]==-1||detectorIndex[1]==-1||detectorIndex[2]==-1||detectorIndex[3]==-1||detectorIndex[4]==-1) continue;
490
491       // Do not reconstruct anything in the holes
492       if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
493         if (detectorIndex[1]==2) { // plate with holes
494           inholes++;
495           continue;
496         }
497       }
498
499       last = fDigits->GetEntriesFast();
500       new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, digit);
501       if (status) fTOFdigitMap->AddDigit(detectorIndex, last);
502
503       if (fVerbose==2) {
504         if (indexDDL<10) ftxt << "  " << indexDDL;
505         else         ftxt << " " << indexDDL;
506         if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
507         else         ftxt << " " << tofRawDatum->GetTRM();
508         ftxt << "  " << tofRawDatum->GetTRMchain();
509         if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
510         else         ftxt << " " << tofRawDatum->GetTDC();
511         ftxt << "  " << tofRawDatum->GetTDCchannel();
512
513         if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
514         else              ftxt  << "  -> " << detectorIndex[0];
515         ftxt << "  " << detectorIndex[1];
516         if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
517         else              ftxt << " " << detectorIndex[2];
518         ftxt << "  " << detectorIndex[4];
519         if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
520         else              ftxt << " " << detectorIndex[3];
521
522         if (digit[1]<10)ftxt << "        " << digit[1];
523         else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
524         else ftxt << "      " << digit[1];
525         if (digit[0]<10) ftxt << "      " << digit[0] << endl;
526         else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
527         else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
528         else ftxt << "   " << digit[3] << endl;
529       }
530
531       AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
532                        last,
533                        detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
534                        digit[0], digit[1], digit[3]));
535
536     } // while loop
537
538     clonesRawData->Clear();
539
540   } // DDL Loop
541
542   if (fVerbose==2) ftxt.close();
543
544   digitsTree->Fill();
545
546   fNumberOfTofDigits = fDigits->GetEntries();
547
548   AliDebug(1, Form("Got %d TOF digits", fNumberOfTofDigits));
549   AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
550                    stopwatch.RealTime(),stopwatch.CpuTime()));
551
552   if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
553
554 }
555 //_____________________________________________________________________________
556
557 void AliTOFClusterFinderV1::FillRecPoint()
558 {
559   //
560   // Fill the global TClonesArray of AliTOFcluster objects,
561   // i.e. fRecPoints
562   //
563
564   Int_t dummy4 = -1;
565   Int_t dummy3 = -1;
566   Int_t dummy2 = -1;
567   Int_t dummy  = -1;
568
569   for(Int_t iPlate=AliTOFGeometry::NPlates()-1; iPlate>=0; iPlate--) {
570     for(Int_t iStrip=AliTOFGeometry::NStrip(iPlate)-1; iStrip>=0; iStrip--) {
571       //for (Int_t iSector=AliTOFGeometry::NSectors()-1; iSector>=0; iSector--) {
572       for (Int_t iSector=0; iSector<AliTOFGeometry::NSectors(); iSector++) {
573
574
575         if (fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))
576           AliDebug(1,Form(" Number of TOF digits in (%2d,%1d,%2d) -> %d",
577                           iSector,iPlate,iStrip,fTOFdigitMap->FilledCellsInStrip(iSector,iPlate,iStrip)));
578
579         if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
580         FindClustersWithoutTOT(iSector, iPlate, iStrip); // clusters coming from digits without TOT measurement
581
582         if (fMaxDeltaTime>0) {
583
584           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
585           //if (fTOFdigitMap->FilledCellsInStrip(iSector,iPlate,iStrip)>=4)
586           FindClustersPerStrip(iSector, iPlate, iStrip, 4); // 4 pads clusters
587           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
588
589           dummy4 = fNumberOfTofClusters;
590           FindClustersPerStrip(iSector, iPlate, iStrip, 4); // 4 pads clusters
591           if (fNumberOfTofClusters!=dummy4)
592             AliDebug(2, Form(" (4): n1= %5d, n2 = %5d", dummy4, fNumberOfTofClusters));
593
594
595           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
596           FindClustersPerStrip(iSector, iPlate, iStrip, 3); // 3 pads clusters
597           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
598
599           dummy3 = fNumberOfTofClusters;
600           FindClustersPerStrip(iSector, iPlate, iStrip, 3); // 3 pads clusters
601           if (fNumberOfTofClusters!=dummy3)
602             AliDebug(2, Form(" (3): n1= %5d, n2 = %5d", dummy3, fNumberOfTofClusters));
603
604
605           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
606           FindClustersPerStrip(iSector, iPlate, iStrip, 2); // 2 pads clusters
607           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
608
609           dummy2 = fNumberOfTofClusters;
610           FindClustersPerStrip(iSector, iPlate, iStrip, 2); // 2 pads clusters
611           if (fNumberOfTofClusters!=dummy2)
612             AliDebug(2, Form(" (2): n1= %5d, n2 =%5d", dummy2, fNumberOfTofClusters));
613
614
615           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
616           dummy = fNumberOfTofClusters;
617           FindClusters34(iSector, iPlate, iStrip); // 3 pads clusters between 4 hit pads
618           if (fNumberOfTofClusters!=dummy)
619             AliDebug(2, Form(" (3 between 4): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
620
621
622           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
623           dummy = fNumberOfTofClusters;
624           FindClusters23(iSector, iPlate, iStrip); // 2 pads clusters between 3 hit pads
625           if (fNumberOfTofClusters!=dummy)
626             AliDebug(2, Form(" (2 between 3): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
627
628           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
629           dummy = fNumberOfTofClusters;
630           FindClusters24(iSector, iPlate, iStrip); // 2 pads clusters between 4 hit pads
631           if (fNumberOfTofClusters!=dummy)
632             AliDebug(2, Form(" (2 between 4): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
633
634
635           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
636           dummy = fNumberOfTofClusters;
637           FindOnePadClusterPerStrip(iSector, iPlate, iStrip); // 1 pad clusters
638           if (fNumberOfTofClusters!=dummy)
639             AliDebug(2,Form(" (1): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
640
641           if (fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)>0)
642             AliDebug(2, Form(" (1): number of clusters = %5d (remaining digit %2d), -%2d %1d %2d-",
643                              fNumberOfTofClusters, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip),
644                              iSector, iPlate, iStrip));
645
646         }
647         else {
648           if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
649           dummy = fNumberOfTofClusters;
650           FindOnePadClusterPerStrip(iSector, iPlate, iStrip); // 1 pad clusters
651           if (fNumberOfTofClusters!=dummy)
652             AliDebug(2,Form(" (1): n1 = %5d, n2 = %5d", fNumberOfTofClusters, dummy));
653
654           if (fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)>0)
655             AliDebug(2, Form(" (1): number of clusters = %5d (remaining digit %2d), -%2d %1d %2d-",
656                              fNumberOfTofClusters, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip),
657                              iSector, iPlate, iStrip));
658
659         }
660
661
662       }
663     }
664   }
665
666
667   TClonesArray &lRecPoints = *fRecPoints;
668   
669   Int_t ii, jj;
670
671   Int_t detectorIndex[5];
672   for (jj=0; jj<5; jj++) detectorIndex[jj] = -1;
673   Int_t parTOF[7];
674   for (jj=0; jj<7; jj++) parTOF[jj] = -1;
675   Int_t trackLabels[3];
676   for (jj=0; jj<3; jj++) trackLabels[jj] = -1;
677   Int_t digitIndex = -1;
678   Bool_t status = kTRUE;
679   Float_t posClus[3];
680   for (ii=0; ii<3; ii++) posClus[ii] = 0.;
681   //Float_t covClus[6];
682   //for (ii=0; ii<6; ii++) covClus[ii] = 0.;
683   UShort_t volIdClus;
684
685   for (ii=0; ii<fNumberOfTofClusters; ii++) {
686
687     digitIndex = fTofClusters[ii]->GetIndex();
688     for(jj=0; jj<5; jj++) detectorIndex[jj] = fTofClusters[ii]->GetDetInd(jj);
689     volIdClus = fTOFGeometry->GetAliSensVolIndex(detectorIndex[0],detectorIndex[1],detectorIndex[2]);
690     //volIdClus = GetClusterVolIndex(detectorIndex);
691     for(jj=0; jj<3; jj++) trackLabels[jj] = fTofClusters[ii]->GetLabel(jj);
692     parTOF[0] = fTofClusters[ii]->GetTDC(); // TDC
693     parTOF[1] = fTofClusters[ii]->GetToT(); // TOT
694     parTOF[2] = fTofClusters[ii]->GetADC(); // ADC=TOT
695     parTOF[3] = fTofClusters[ii]->GetTDCND(); // TDCND
696     parTOF[4] = fTofClusters[ii]->GetTDCRAW();//RAW
697     parTOF[5] = 0;
698     parTOF[6] = 0;
699     status = fTofClusters[ii]->GetStatus();
700
701     posClus[0] = fTofClusters[ii]->GetX();
702     posClus[1] = fTofClusters[ii]->GetY();
703     posClus[2] = fTofClusters[ii]->GetZ();
704
705     //for (jj=0; jj<6; jj++) covClus[jj] = 0.;
706     //((AliCluster*)fTofClusters[ii])->GetGlobalCov(covClus);
707
708     new(lRecPoints[ii]) AliTOFcluster(volIdClus, (Double_t)posClus[0], (Double_t)posClus[1], (Double_t)posClus[2],
709                                       (Double_t)(fTofClusters[ii]->GetSigmaX2()),
710                                       (Double_t)(fTofClusters[ii]->GetSigmaXY()),
711                                       (Double_t)(fTofClusters[ii]->GetSigmaXZ()),
712                                       (Double_t)(fTofClusters[ii]->GetSigmaY2()),
713                                       (Double_t)(fTofClusters[ii]->GetSigmaYZ()),
714                                       (Double_t)(fTofClusters[ii]->GetSigmaZ2()),
715                                       //(Double_t)covClus[0], (Double_t)covClus[1], (Double_t)covClus[2],
716                                       //(Double_t)covClus[3], (Double_t)covClus[4], (Double_t)covClus[5],
717                                       trackLabels, detectorIndex, parTOF, status, digitIndex);
718
719     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", 
720                      ii, volIdClus, posClus[0], posClus[1], posClus[2],
721                      fTofClusters[ii]->GetSigmaX2(),
722                      fTofClusters[ii]->GetSigmaXY(),
723                      fTofClusters[ii]->GetSigmaXZ(),
724                      fTofClusters[ii]->GetSigmaY2(),
725                      fTofClusters[ii]->GetSigmaYZ(),
726                      fTofClusters[ii]->GetSigmaZ2(),
727                      trackLabels[0], trackLabels[1], trackLabels[2],
728                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[3], detectorIndex[4],
729                      parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
730                      status, digitIndex));
731
732
733   }
734
735   for (Int_t iSector=0; iSector<AliTOFGeometry::NSectors(); iSector++)
736     for(Int_t iPlate=0; iPlate<AliTOFGeometry::NPlates(); iPlate++) {
737       for(Int_t iStrip=0; iStrip<AliTOFGeometry::NStrip(iPlate); iStrip++) {
738         if (!(fTOFdigitMap->StripDigitCheck(iSector,iPlate,iStrip))) continue;
739         AliDebug(2, Form(" END %2d %1d %2d   %5d",
740                          iSector, iPlate, iStrip, fTOFdigitMap->DigitInStrip(iSector,iPlate,iStrip)));
741       }
742     }
743
744 }
745 //_____________________________________________________________________________
746
747 void AliTOFClusterFinderV1::FindOnePadClusterPerStrip(Int_t nSector,
748                                                       Int_t nPlate,
749                                                       Int_t nStrip)
750 {
751   //
752   // This function searches the isolated digits (stored in the fDigits object),
753   // to perform clusters (stored in the fTofClusters array).
754   // This research has been made by checking the fTOFdigitMap object,
755   // filled at digits/raw-data reading time.
756   //
757
758   const Int_t kMaxNumberOfTracksPerDigit = 3;
759   const Int_t kMaxNumberOfDigitsPerVolume = 10;
760
761   Int_t jj = 0;
762
763   Int_t det[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padZ,padX
764   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padX,padZ
765   UShort_t volIdClus = 0;
766
767   Float_t pos[3];
768   for (jj=0; jj<3; jj++) pos[jj] = 0.;
769   Double_t posClus[3];
770   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
771
772   Double_t covClus[6];
773   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
774
775   Int_t parTOF[7];
776   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
777
778   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
779
780   Int_t tracks[kMaxNumberOfTracksPerDigit];
781   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
782
783   Int_t dummyCounter=-1;
784
785   AliTOFdigit *digitInteresting;
786
787   Int_t iPadX = -1;
788   Int_t iPadZ = -1;
789   for (iPadX=0; iPadX<AliTOFGeometry::NpadX(); iPadX++) {
790     for (iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++) {
791       vol[4] = iPadZ  , vol[3]  = iPadX;
792
793       AliDebug(3, Form(" %1d %2d\n", iPadZ, iPadX));
794
795       if (fTOFdigitMap->GetNumberOfDigits(vol)==0) continue;
796
797       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
798         if (fTOFdigitMap->GetDigitIndex(vol,digIndex)<0) continue;
799         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(fTOFdigitMap->GetDigitIndex(vol,digIndex));
800
801         AliDebug(2, Form(" %3d  %5d    %2d %1d %2d %1d %2d  %d %d %d  %5d  %5d %5d %5d",
802                          fTOFdigitMap->GetNumberOfDigits(vol), digIndex,
803                          vol[0], vol[1], vol[2] ,vol[4], vol[3],
804                          digitInteresting->GetTdc(), digitInteresting->GetAdc(),
805                          digitInteresting->GetToT(),
806                          fTOFdigitMap->GetDigitIndex(vol,digIndex),
807                          digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
808         
809         det[3] = vol[4]; // padz
810         det[4] = vol[3]; // padx
811         fTOFGeometry->GetPosPar(det,pos);
812         AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
813
814         //insert cluster
815         for (jj=0; jj<3; jj++) posClus[jj] = pos[jj];
816
817         parTOF[0] = Int_t(digitInteresting->GetTdc());
818         parTOF[1] = Int_t(digitInteresting->GetToT());
819         parTOF[2] = Int_t(digitInteresting->GetAdc());
820         parTOF[3] = Int_t(digitInteresting->GetTdcND());
821         parTOF[4] = Int_t(digitInteresting->GetTdc());
822         parTOF[5] = 0;
823         parTOF[6] = 0;
824
825         volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
826         //volIdClus = GetClusterVolIndex(det);
827
828         for (jj=0; jj<6; jj++) covClus[jj] = 0.;
829         GetClusterPars(det, posClus, covClus);
830
831         // To fill the track index array
832         dummyCounter=-1;
833         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
834         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) { // three is the max number of tracks associated to one digit
835           if (digitInteresting->GetTrack(jj)==-1) continue;
836           else {
837             dummyCounter++;
838             tracks[dummyCounter] = digitInteresting->GetTrack(jj);
839           }
840         }
841
842         AliTOFcluster *tofCluster =
843           new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
844                             covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
845                             tracks, det, parTOF, status, fTOFdigitMap->GetDigitIndex(vol,digIndex));
846         InsertCluster(tofCluster);
847
848         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", 
849                          volIdClus, posClus[0], posClus[1], posClus[2],
850                          covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
851                          tracks[0], tracks[1], tracks[2],
852                          det[0], det[1], det[2], det[3], det[4],
853                          parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
854                          status, fTOFdigitMap->GetDigitIndex(vol,digIndex)));
855
856         AliDebug(2, Form("        %f %f %f", pos[0], pos[1], pos[2]));
857         AliDebug(2, Form("           %d %d", parTOF[0], parTOF[2]));
858
859         fTOFdigitMap->ResetDigit(vol, digIndex);
860
861       }
862
863     }
864   }
865
866 }
867 //_____________________________________________________________________________
868
869 void AliTOFClusterFinderV1::FindClustersWithoutTOT(Int_t nSector,
870                                                    Int_t nPlate,
871                                                    Int_t nStrip)
872 {
873   //
874   // This function searches the isolated digits without TOT
875   // measurement (stored in the fDigits object), to perform clusters
876   // (stored in the fTofClusters array). This research has been made
877   // by checking the fTOFdigitMap object, filled at digits/raw-data
878   // reading time.
879   //
880
881   const Int_t kMaxNumberOfTracksPerDigit = 3;
882   const Int_t kMaxNumberOfDigitsPerVolume = 10;
883
884   Int_t jj = 0;
885
886   Int_t det[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padZ,padX
887   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};//sector,plate,strip,padX,padZ
888   UShort_t volIdClus = 0;
889
890   Float_t pos[3];
891   for (jj=0; jj<3; jj++) pos[jj] = 0.;
892   Double_t posClus[3];
893   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
894
895   Double_t covClus[6];
896   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
897
898   Int_t parTOF[7];
899   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
900
901   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
902   Int_t tracks[kMaxNumberOfTracksPerDigit];
903   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
904
905   Int_t dummyCounter=-1;
906
907   AliTOFdigit *digitInteresting;
908
909   Int_t iPadX = -1;
910   Int_t iPadZ = -1;
911   for (iPadX=0; iPadX<AliTOFGeometry::NpadX(); iPadX++) {
912     for (iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++) {
913       vol[4] = iPadZ  , vol[3]  = iPadX;
914
915       AliDebug(3, Form(" %1d %2d\n", iPadZ, iPadX));
916
917       if (fTOFdigitMap->GetNumberOfDigits(vol)==0) continue;
918
919       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
920         if (fTOFdigitMap->GetDigitIndex(vol,digIndex)<0) continue;
921         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(fTOFdigitMap->GetDigitIndex(vol,digIndex));
922         if (digitInteresting->GetToT()>0) continue; // AdC
923
924         AliDebug(2, Form(" %3d  %5d    %2d %1d %2d %1d %2d  %d %d %d  %5d  %5d %5d %5d",
925                          fTOFdigitMap->GetNumberOfDigits(vol), digIndex,
926                          vol[0], vol[1], vol[2] ,vol[4], vol[3],
927                          digitInteresting->GetTdc(), digitInteresting->GetAdc(),
928                          digitInteresting->GetToT(),
929                          fTOFdigitMap->GetDigitIndex(vol,digIndex),
930                          digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
931         
932         det[3] = vol[4]; // padz
933         det[4] = vol[3]; // padx
934         fTOFGeometry->GetPosPar(det,pos);
935         AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
936
937         //insert cluster
938         for (jj=0; jj<3; jj++) posClus[jj] = pos[jj];
939
940         parTOF[0] = Int_t(digitInteresting->GetTdc());
941         parTOF[1] = Int_t(digitInteresting->GetToT());
942         parTOF[2] = Int_t(digitInteresting->GetAdc());
943         parTOF[3] = Int_t(digitInteresting->GetTdcND());
944         parTOF[4] = Int_t(digitInteresting->GetTdc());
945         parTOF[5] = 0;
946         parTOF[6] = 0;
947
948         volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
949         //volIdClus = GetClusterVolIndex(det);
950
951         for (jj=0; jj<6; jj++) covClus[jj] = 0.;
952         GetClusterPars(det, posClus, covClus);
953
954         // To fill the track index array
955         dummyCounter=-1;
956         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
957         for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) { // three is the max number of tracks associated to one digit
958           if (digitInteresting->GetTrack(jj)==-1) continue;
959           else {
960             dummyCounter++;
961             tracks[dummyCounter] = digitInteresting->GetTrack(jj);
962           }
963         }
964
965         AliTOFcluster *tofCluster =
966           new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
967                             covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
968                             tracks, det, parTOF, status, fTOFdigitMap->GetDigitIndex(vol,digIndex));
969         InsertCluster(tofCluster);
970
971         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", 
972                          volIdClus, posClus[0], posClus[1], posClus[2],
973                          covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
974                          tracks[0], tracks[1], tracks[2],
975                          det[0], det[1], det[2], det[3], det[4],
976                          parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
977                          status, fTOFdigitMap->GetDigitIndex(vol,digIndex)));
978
979         AliDebug(2, Form("        %f %f %f", pos[0], pos[1], pos[2]));
980         AliDebug(2, Form("           %d %d", parTOF[0], parTOF[2]));
981
982         fTOFdigitMap->ResetDigit(vol, digIndex);
983
984       }
985
986     }
987   }
988
989 }
990 //_____________________________________________________________________________
991
992 void AliTOFClusterFinderV1::FindClusters34(Int_t nSector,
993                                            Int_t nPlate,
994                                            Int_t nStrip)
995 {
996   //
997   // This function searches the neighbouring digits (stored in the fDigits object),
998   // to perform clusters (stored in the fTofClusters array).
999   //
1000   // This research has been made by checking the fTOFdigitMap object,
1001   // filled at digits/raw-data reading time.
1002   //
1003
1004   const Int_t kMaxNumberOfInterestingPads = 4;
1005   const Int_t kMaxNumberOfTracksPerDigit = 3;
1006   const Int_t kMaxNumberOfDigitsPerVolume = 10;
1007
1008   Int_t ii = 0;
1009
1010   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
1011   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1012     digitsInVolumeIndices[ii] = -1;
1013
1014   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
1015
1016   Float_t pos[3] = {0.,0.,0.};
1017
1018   Int_t jj = 0;
1019   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1020   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1021   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1022   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1023   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1024   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1025   Double_t interestingADC[kMaxNumberOfInterestingPads];
1026   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1027   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1028   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1029   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1030   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1031
1032   Float_t interestingX[kMaxNumberOfInterestingPads];
1033   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1034   Float_t interestingY[kMaxNumberOfInterestingPads];
1035   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1036   Float_t interestingZ[kMaxNumberOfInterestingPads];
1037   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1038
1039   Float_t interDigit[kMaxNumberOfInterestingPads];
1040   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1041
1042   Int_t padsCluster[11];
1043   padsCluster[0] = nSector;
1044   padsCluster[1] = nPlate;
1045   padsCluster[2] = nStrip;
1046   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1047
1048   Int_t interestingCounter=-1;
1049   Int_t  digitIndexLocal=-1; // AdC
1050   Int_t iPad  = -1;
1051   Int_t iPadX = -1;
1052   Int_t iPadZ = -1;
1053
1054   Int_t parTOF[7];
1055   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
1056   Double_t posClus[3];
1057   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1058   Int_t det[5];
1059   for (jj=0; jj<5; jj++) det[jj] = -1;
1060   Float_t posF[3];
1061   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1062   UShort_t volIdClus = 0;
1063   Bool_t check = kFALSE;
1064   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1065   Double_t covClus[6];
1066   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1067   Int_t tracks[kMaxNumberOfTracksPerDigit];
1068   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1069   Int_t dummyCounter=-1;
1070   Bool_t alreadyStored = kFALSE;
1071
1072   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1073   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1074     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1075
1076   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1077     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1078
1079   AliTOFdigit *digitInteresting;
1080
1081   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1082
1083     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1084     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1085
1086     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1087
1088
1089
1090
1091
1092
1093     interestingCounter=-1;
1094
1095     vol[4] = iPadZ  , vol[3]  = iPadX;
1096     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1097       interestingCounter++;
1098
1099     vol[4] = iPadZ, vol[3] = iPadX+1;
1100     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1101       interestingCounter++;
1102
1103     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1104     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1105       interestingCounter++;
1106
1107     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1108     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1109       interestingCounter++;
1110
1111     if (interestingCounter+1!=4) continue; // the hit pads have to be 4
1112     else interestingCounter=-1;
1113
1114
1115     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1116       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1117         selectedDigit[ii][jj] = 0x0;
1118
1119
1120     vol[4] = iPadZ, vol[3] = iPadX;
1121
1122     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1123       interestingCounter++;
1124       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1125         digitsInVolumeIndices[ii] = -1;
1126       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1127       digitIndexLocal=-1; // AdC
1128       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1129         if (digitsInVolumeIndices[digIndex]<0) continue;
1130         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1131         if (digitInteresting->GetToT()<=0) continue; // AdC
1132         digitIndexLocal++; // AdC
1133
1134         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1135                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1136                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1137                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1138                         digitsInVolumeIndices[digIndex],
1139                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1140
1141
1142         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1143           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1144                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1145                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1146                               digitsInVolumeIndices[digIndex],
1147                               digitInteresting->GetTracks());
1148       }
1149       if (digitIndexLocal==-1) interestingCounter--; // AdC
1150     }
1151
1152
1153     vol[4] = iPadZ, vol[3] = iPadX+1;
1154
1155     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1156       interestingCounter++;
1157       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1158         digitsInVolumeIndices[ii] = -1;
1159       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1160       digitIndexLocal=-1; // AdC
1161       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1162         if (digitsInVolumeIndices[digIndex]<0) continue;
1163         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1164         if (digitInteresting->GetToT()<=0) continue; // AdC
1165         digitIndexLocal++; // AdC
1166
1167         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1168                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1169                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1170                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1171                         digitsInVolumeIndices[digIndex],
1172                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1173
1174
1175         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1176           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1177                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1178                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1179                               digitsInVolumeIndices[digIndex],
1180                               digitInteresting->GetTracks());
1181       }
1182       if (digitIndexLocal==-1) interestingCounter--; // AdC
1183     }
1184
1185
1186     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1187
1188     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1189       interestingCounter++;
1190       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1191         digitsInVolumeIndices[ii] = -1;
1192       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1193       digitIndexLocal=-1; // AdC
1194       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1195         if (digitsInVolumeIndices[digIndex]<0) continue;
1196         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1197         if (digitInteresting->GetToT()<=0) continue; // AdC
1198         digitIndexLocal++; // AdC
1199
1200         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1201                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1202                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1203                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1204                         digitsInVolumeIndices[digIndex],
1205                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1206
1207
1208         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1209           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1210                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1211                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1212                               digitsInVolumeIndices[digIndex],
1213                               digitInteresting->GetTracks());
1214       }
1215       if (digitIndexLocal==-1) interestingCounter--; // AdC
1216     }
1217
1218
1219     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1220
1221     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1222       interestingCounter++;
1223       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1224         digitsInVolumeIndices[ii] = -1;
1225       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1226       digitIndexLocal=-1;
1227       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1228         if (digitsInVolumeIndices[digIndex]<0) continue;
1229         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1230         if (digitInteresting->GetToT()<=0) continue; // AdC
1231         digitIndexLocal++; // AdC
1232
1233         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1234                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1235                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1236                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1237                         digitsInVolumeIndices[digIndex],
1238                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1239
1240         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1241           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1242                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1243                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1244                               digitsInVolumeIndices[digIndex],
1245                               digitInteresting->GetTracks());
1246       }
1247       if (digitIndexLocal==-1) interestingCounter--; // AdC
1248     }
1249
1250     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
1251
1252     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
1253       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
1254         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
1255
1256         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
1257           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
1258             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
1259
1260             for (Int_t adesso3=adesso2+1; adesso3<interestingCounter+1; adesso3++) {
1261               for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
1262                 if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
1263
1264
1265                 if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
1266                     ||
1267                     TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
1268                     ||
1269                     TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime) continue;
1270
1271                 interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
1272                 interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
1273                 interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
1274                 interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
1275                 Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
1276                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
1277                 Int_t volDum = vol1[3];
1278                 vol1[3] = vol1[4];
1279                 vol1[4] = volDum;
1280                 fTOFGeometry->GetPosPar(vol1,pos);
1281                 interestingX[0] = pos[0];
1282                 interestingY[0] = pos[1];
1283                 interestingZ[0] = pos[2];
1284
1285                 interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
1286                 interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
1287                 interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
1288                 interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
1289                 Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
1290                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
1291                 volDum = vol2[3];
1292                 vol2[3] = vol2[4];
1293                 vol2[4] = volDum;
1294                 fTOFGeometry->GetPosPar(vol2,pos);
1295                 interestingX[1] = pos[0];
1296                 interestingY[1] = pos[1];
1297                 interestingZ[1] = pos[2];
1298
1299                 interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
1300                 interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
1301                 interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
1302                 interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
1303                 Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
1304                 AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
1305                 volDum = vol3[3];
1306                 vol3[3] = vol3[4];
1307                 vol3[4] = volDum;
1308                 fTOFGeometry->GetPosPar(vol3,pos);
1309                 interestingX[2] = pos[0];
1310                 interestingY[2] = pos[1];
1311                 interestingZ[2] = pos[2];
1312
1313
1314                 AverageCalculations(3, interestingX, interestingY, interestingZ,
1315                                     interestingTOF, interestingTOT, interestingADC,
1316                                     interestingWeight,
1317                                     parTOF, posClus, check);
1318
1319
1320                 for (jj=0; jj<5; jj++) det[jj] = -1;
1321                 for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
1322                 fTOFGeometry->GetDetID(posF, det);
1323
1324                 volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
1325                 //volIdClus = GetClusterVolIndex(det);
1326
1327                 for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1328                 padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
1329                 padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
1330                 padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
1331                 padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
1332                 padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
1333                 padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
1334
1335                 for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1336                 Int_t ** indDet = new Int_t*[3];
1337                 for (jj=0; jj<3; jj++) indDet[jj] = new Int_t [5];
1338                 for (jj=0; jj<3; jj++) indDet[jj][0] = nSector;
1339                 for (jj=0; jj<3; jj++) indDet[jj][1] = nPlate;
1340                 for (jj=0; jj<3; jj++) indDet[jj][2] = nStrip;
1341                 for (jj=0; jj<3; jj++) indDet[jj][3] = padsCluster[2*jj+3];
1342                 for (jj=0; jj<3; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
1343                 GetClusterPars(/*check,*/ 3, indDet, interestingWeight, posClus, covClus);
1344                 for (jj=0; jj<3; jj++) delete [] indDet[jj];
1345                 delete [] indDet;
1346
1347                 // To fill the track index array
1348                 dummyCounter=-1;
1349                 for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1350                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1351                   if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
1352                   else {
1353                     dummyCounter++;
1354                     tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
1355                   }
1356                 }
1357                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1358                   if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
1359                   else {
1360
1361                     alreadyStored = kFALSE;
1362                     for (jj=0; jj<dummyCounter+1; jj++)
1363                       alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
1364
1365                     if (alreadyStored) continue;
1366                     if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1367                       AliWarning("  Siamo al limite!");
1368                       continue;
1369                     }
1370
1371                     dummyCounter++;
1372                     tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
1373
1374                   }
1375
1376                 }
1377                 for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1378                   if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
1379                   else {
1380
1381                     alreadyStored = kFALSE;
1382                     for (jj=0; jj<dummyCounter+1; jj++)
1383                       alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
1384
1385                     if (alreadyStored) continue;
1386                     if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1387                       AliWarning("  Siamo al limite!");
1388                       continue;
1389                     }
1390
1391                     dummyCounter++;
1392                     tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
1393
1394                   }
1395
1396                 }
1397
1398
1399                 AliTOFcluster *tofCluster =
1400                   new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
1401                                     covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1402                                     tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
1403                 InsertCluster(tofCluster);
1404
1405                 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", 
1406                                  volIdClus, posClus[0], posClus[1], posClus[2],
1407                                  covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1408                                  tracks[0], tracks[1], tracks[2],
1409                                  det[0], det[1], det[2], det[3], det[4],
1410                                  parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
1411                                  status, selectedDigit[adesso1][firstIndex]->GetIndex()));
1412
1413
1414                 volDum = vol1[3];
1415                 vol1[3] = vol1[4];
1416                 vol1[4] = volDum;
1417                 fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
1418                 volDum = vol2[3];
1419                 vol2[3] = vol2[4];
1420                 vol2[4] = volDum;
1421                 fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
1422                 volDum = vol3[3];
1423                 vol3[3] = vol3[4];
1424                 vol3[4] = volDum;
1425                 fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
1426
1427
1428               } // close loop on third digit
1429             } // close loop on adesso3
1430
1431           } // close loop on second digit
1432         } // close loop on adesso2
1433
1434       } // close loop on first digit
1435     } // close loop on adesso1
1436
1437     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1438       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1439         selectedDigit[ii][jj] = 0x0;
1440
1441   } // loop on iPad
1442
1443   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
1444     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
1445       delete [] selectedDigit[ii][jj];
1446       selectedDigit[ii][jj] = 0x0;
1447     }
1448     delete [] selectedDigit[ii];
1449     selectedDigit[ii] = 0x0;
1450   }
1451   delete [] selectedDigit;
1452   selectedDigit = 0x0;
1453
1454 }
1455 //_____________________________________________________________________________
1456
1457 void AliTOFClusterFinderV1::FindClusters23(Int_t nSector,
1458                                            Int_t nPlate,
1459                                            Int_t nStrip)
1460 {
1461   //
1462   // This function searches the neighbouring digits (stored in the fDigits object),
1463   // to perform clusters (stored in the fTofClusters array).
1464   //
1465   // This research has been made by checking the fTOFdigitMap object,
1466   // filled at digits/raw-data reading time.
1467   //
1468
1469   const Int_t kMaxNumberOfInterestingPads = 4;
1470   const Int_t kMaxNumberOfTracksPerDigit = 3;
1471   const Int_t kMaxNumberOfDigitsPerVolume = 10;
1472
1473   Int_t ii = 0;
1474
1475   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
1476   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1477     digitsInVolumeIndices[ii] = -1;
1478
1479   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
1480
1481   Float_t pos[3] = {0.,0.,0.};
1482
1483   Int_t jj = 0;
1484   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1485   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1486   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1487   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1488   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1489   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1490   Double_t interestingADC[kMaxNumberOfInterestingPads];
1491   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1492   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1493   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1494   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1495   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1496
1497   Float_t interestingX[kMaxNumberOfInterestingPads];
1498   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1499   Float_t interestingY[kMaxNumberOfInterestingPads];
1500   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1501   Float_t interestingZ[kMaxNumberOfInterestingPads];
1502   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1503
1504   Float_t interDigit[kMaxNumberOfInterestingPads];
1505   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1506
1507   Int_t padsCluster[11];
1508   padsCluster[0] = nSector;
1509   padsCluster[1] = nPlate;
1510   padsCluster[2] = nStrip;
1511   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1512
1513   Int_t interestingCounter=-1;
1514   Int_t digitIndexLocal = -1;
1515   Int_t iPad  = -1;
1516   Int_t iPadX = -1;
1517   Int_t iPadZ = -1;
1518
1519   Bool_t check = kFALSE;
1520   Int_t parTOF[7];
1521   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
1522   Double_t posClus[3];
1523   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1524   Int_t det[5];
1525   for (jj=0; jj<5; jj++) det[jj] = -1;
1526   Float_t posF[3];
1527   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1528   UShort_t volIdClus = 0;
1529   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1530   Double_t covClus[6];
1531   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1532   Int_t tracks[kMaxNumberOfTracksPerDigit];
1533   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1534   Int_t dummyCounter=-1;
1535   Bool_t alreadyStored = kFALSE;
1536
1537   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1538   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1539     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1540
1541   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1542     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1543
1544   AliTOFdigit *digitInteresting;
1545
1546   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1547
1548     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1549     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1550
1551     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1552
1553
1554
1555
1556
1557
1558     interestingCounter=-1;
1559
1560     vol[4] = iPadZ  , vol[3]  = iPadX;
1561     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1562       interestingCounter++;
1563
1564     vol[4] = iPadZ, vol[3] = iPadX+1;
1565     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1566       interestingCounter++;
1567
1568     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1569     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1570       interestingCounter++;
1571
1572     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1573     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1574       interestingCounter++;
1575
1576     if (interestingCounter+1!=3) continue; // the hit pads have to be 3
1577     else interestingCounter=-1;
1578
1579
1580     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1581       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1582         selectedDigit[ii][jj] = 0x0;
1583
1584
1585     vol[4] = iPadZ, vol[3] = iPadX;
1586
1587     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1588       interestingCounter++;
1589       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1590         digitsInVolumeIndices[ii] = -1;
1591       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1592       digitIndexLocal=-1;
1593       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1594         if (digitsInVolumeIndices[digIndex]<0) continue;
1595         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1596         if (digitInteresting->GetToT()<=0) continue; // AdC
1597         digitIndexLocal++; // AdC
1598
1599         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1600                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1601                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1602                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1603                         digitsInVolumeIndices[digIndex],
1604                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1605
1606         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1607           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1608                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1609                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1610                               digitsInVolumeIndices[digIndex],
1611                               digitInteresting->GetTracks());
1612       }
1613       if (digitIndexLocal==-1) interestingCounter--; // AdC
1614     }
1615
1616
1617     vol[4] = iPadZ, vol[3] = iPadX+1;
1618
1619     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1620       interestingCounter++;
1621       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1622         digitsInVolumeIndices[ii] = -1;
1623       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1624       digitIndexLocal=-1; // AdC
1625       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1626         if (digitsInVolumeIndices[digIndex]<0) continue;
1627         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1628         if (digitInteresting->GetToT()<=0) continue; // AdC
1629         digitIndexLocal++; // AdC
1630
1631         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1632                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1633                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1634                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1635                         digitsInVolumeIndices[digIndex],
1636                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1637
1638         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1639           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1640                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1641                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1642                               digitsInVolumeIndices[digIndex],
1643                               digitInteresting->GetTracks());
1644       }
1645       if (digitIndexLocal==-1) interestingCounter--; // AdC
1646     }
1647
1648
1649     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1650
1651     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1652       interestingCounter++;
1653       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1654         digitsInVolumeIndices[ii] = -1;
1655       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1656       digitIndexLocal=-1; // AdC
1657       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1658         if (digitsInVolumeIndices[digIndex]<0) continue;
1659         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1660         if (digitInteresting->GetToT()<=0) continue; // AdC
1661         digitIndexLocal++; // AdC
1662
1663         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1664                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1665                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1666                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1667                         digitsInVolumeIndices[digIndex],
1668                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1669
1670         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1671           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1672                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1673                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1674                               digitsInVolumeIndices[digIndex],
1675                               digitInteresting->GetTracks());
1676       }
1677       if (digitIndexLocal==-1) interestingCounter--; // AdC
1678     }
1679
1680
1681     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1682
1683     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1684       interestingCounter++;
1685       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1686         digitsInVolumeIndices[ii] = -1;
1687       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1688       digitIndexLocal=-1; // AdC
1689       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
1690         if (digitsInVolumeIndices[digIndex]<0) continue;
1691         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
1692         if (digitInteresting->GetToT()<=0) continue; // AdC
1693         digitIndexLocal++; // AdC
1694
1695         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
1696                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
1697                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
1698                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
1699                         digitsInVolumeIndices[digIndex],
1700                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
1701
1702         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
1703           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
1704                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
1705                               digitInteresting->GetToT()*digitInteresting->GetToT(),
1706                               digitsInVolumeIndices[digIndex],
1707                               digitInteresting->GetTracks());
1708       }
1709       if (digitIndexLocal==-1) interestingCounter--; // AdC
1710     }
1711
1712     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
1713
1714     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
1715       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
1716         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
1717
1718         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
1719           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
1720             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
1721
1722             if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) continue;
1723
1724             interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
1725             interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
1726             interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
1727             interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
1728             Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
1729             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
1730             Int_t volDum = vol1[3];
1731             vol1[3] = vol1[4];
1732             vol1[4] = volDum;
1733             fTOFGeometry->GetPosPar(vol1,pos);
1734             interestingX[0] = pos[0];
1735             interestingY[0] = pos[1];
1736             interestingZ[0] = pos[2];
1737
1738             interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
1739             interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
1740             interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
1741             interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
1742             Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
1743             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
1744             volDum = vol2[3];
1745             vol2[3] = vol2[4];
1746             vol2[4] = volDum;
1747             fTOFGeometry->GetPosPar(vol2,pos);
1748             interestingX[1] = pos[0];
1749             interestingY[1] = pos[1];
1750             interestingZ[1] = pos[2];
1751
1752             AverageCalculations(2, interestingX, interestingY, interestingZ,
1753                                 interestingTOF, interestingTOT, interestingADC,
1754                                 interestingWeight,
1755                                 parTOF, posClus, check);
1756
1757             for (jj=0; jj<5; jj++) det[jj] = -1;
1758             for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
1759             fTOFGeometry->GetDetID(posF, det);
1760
1761             volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
1762             //volIdClus = GetClusterVolIndex(det);
1763
1764             for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1765             padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
1766             padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
1767             padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
1768             padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
1769
1770             for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1771             Int_t ** indDet = new Int_t*[2];
1772             for (jj=0; jj<2; jj++) indDet[jj] = new Int_t [5];
1773             for (jj=0; jj<2; jj++) indDet[jj][0] = nSector;
1774             for (jj=0; jj<2; jj++) indDet[jj][1] = nPlate;
1775             for (jj=0; jj<2; jj++) indDet[jj][2] = nStrip;
1776             for (jj=0; jj<2; jj++) indDet[jj][3] = padsCluster[2*jj+3];
1777             for (jj=0; jj<2; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
1778             GetClusterPars(/*check,*/ 2, indDet, interestingWeight, posClus, covClus);
1779             for (jj=0; jj<2; jj++) delete [] indDet[jj];
1780             delete [] indDet;
1781
1782             // To fill the track index array
1783             dummyCounter=-1;
1784             for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1785             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1786               if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
1787               else {
1788                 dummyCounter++;
1789                 tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
1790               }
1791             }
1792             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
1793               if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
1794               else {
1795
1796                 alreadyStored = kFALSE;
1797                 for (jj=0; jj<dummyCounter+1; jj++)
1798                   alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
1799
1800                 if (alreadyStored) continue;
1801                 if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
1802                   AliWarning("  Siamo al limite!");
1803                   continue;
1804                 }
1805
1806                 dummyCounter++;
1807                 tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
1808
1809               }
1810
1811             }
1812
1813
1814             AliTOFcluster *tofCluster =
1815               new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
1816                                 covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1817                                 tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
1818             InsertCluster(tofCluster);
1819
1820             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", 
1821                              volIdClus, posClus[0], posClus[1], posClus[2],
1822                              covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
1823                              tracks[0], tracks[1], tracks[2],
1824                              det[0], det[1], det[2], det[3], det[4],
1825                              parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
1826                              status, selectedDigit[adesso1][firstIndex]->GetIndex()));
1827
1828             volDum = vol1[3];
1829             vol1[3] = vol1[4];
1830             vol1[4] = volDum;
1831             fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
1832             volDum = vol2[3];
1833             vol2[3] = vol2[4];
1834             vol2[4] = volDum;
1835             fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
1836
1837
1838           } // close loop on second digit
1839         } // close loop on adesso2
1840
1841       } // close loop on first digit
1842     } // close loop on adesso1
1843
1844     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1845       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1846         selectedDigit[ii][jj] = 0x0;
1847
1848   } // loop on iPad
1849
1850   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
1851     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
1852       delete [] selectedDigit[ii][jj];
1853       selectedDigit[ii][jj] = 0x0;
1854     }
1855     delete [] selectedDigit[ii];
1856     selectedDigit[ii] = 0x0;
1857   }
1858   delete [] selectedDigit;
1859   selectedDigit = 0x0;
1860
1861 }
1862 //_____________________________________________________________________________
1863
1864 void AliTOFClusterFinderV1::FindClusters24(Int_t nSector,
1865                                            Int_t nPlate,
1866                                            Int_t nStrip)
1867 {
1868   //
1869   // This function searches the neighbouring digits (stored in the fDigits object),
1870   // to perform clusters (stored in the fTofClusters array).
1871   //
1872   // This research has been made by checking the fTOFdigitMap object,
1873   // filled at digits/raw-data reading time.
1874   //
1875
1876   const Int_t kMaxNumberOfInterestingPads = 4;
1877   const Int_t kMaxNumberOfTracksPerDigit = 3;
1878   const Int_t kMaxNumberOfDigitsPerVolume = 10;
1879
1880   Int_t ii = 0;
1881
1882   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
1883   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1884     digitsInVolumeIndices[ii] = -1;
1885
1886   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
1887
1888   Float_t pos[3] = {0.,0.,0.};
1889
1890   Int_t jj = 0;
1891   Int_t interestingPadX[kMaxNumberOfInterestingPads];
1892   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
1893   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
1894   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
1895   Double_t interestingTOT[kMaxNumberOfInterestingPads];
1896   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
1897   Double_t interestingADC[kMaxNumberOfInterestingPads];
1898   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
1899   Double_t interestingTOF[kMaxNumberOfInterestingPads];
1900   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
1901   Double_t interestingWeight[kMaxNumberOfInterestingPads];
1902   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
1903
1904   Float_t interestingX[kMaxNumberOfInterestingPads];
1905   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
1906   Float_t interestingY[kMaxNumberOfInterestingPads];
1907   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
1908   Float_t interestingZ[kMaxNumberOfInterestingPads];
1909   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
1910
1911   Float_t interDigit[kMaxNumberOfInterestingPads];
1912   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
1913
1914   Int_t padsCluster[11];
1915   padsCluster[0] = nSector;
1916   padsCluster[1] = nPlate;
1917   padsCluster[2] = nStrip;
1918   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
1919
1920   Int_t interestingCounter=-1;
1921   Int_t digitIndexLocal = -1;
1922   Int_t iPad  = -1;
1923   Int_t iPadX = -1;
1924   Int_t iPadZ = -1;
1925
1926   Bool_t check = kFALSE;
1927   Int_t parTOF[7];
1928   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
1929   Double_t posClus[3];
1930   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
1931   Int_t det[5];
1932   for (jj=0; jj<5; jj++) det[jj] = -1;
1933   Float_t posF[3];
1934   for (jj=0; jj<3; jj++) posF[jj] = 0.;
1935   UShort_t volIdClus = 0;
1936   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
1937   Double_t covClus[6];
1938   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
1939   Int_t tracks[kMaxNumberOfTracksPerDigit];
1940   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
1941   Int_t dummyCounter=-1;
1942   Bool_t alreadyStored = kFALSE;
1943
1944   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
1945   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1946     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
1947
1948   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1949     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
1950
1951   AliTOFdigit *digitInteresting;
1952
1953   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
1954
1955     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
1956     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
1957
1958     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
1959
1960
1961
1962
1963
1964
1965     interestingCounter=-1;
1966
1967     vol[4] = iPadZ  , vol[3]  = iPadX;
1968     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1969       interestingCounter++;
1970
1971     vol[4] = iPadZ, vol[3] = iPadX+1;
1972     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1973       interestingCounter++;
1974
1975     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
1976     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1977       interestingCounter++;
1978
1979     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
1980     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
1981       interestingCounter++;
1982
1983     if (interestingCounter+1!=4) continue; // the hit pads have to be 4
1984     else interestingCounter=-1;
1985
1986
1987     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
1988       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
1989         selectedDigit[ii][jj] = 0x0;
1990
1991
1992     vol[4] = iPadZ, vol[3] = iPadX;
1993
1994     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
1995       interestingCounter++;
1996       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
1997         digitsInVolumeIndices[ii] = -1;
1998       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
1999       digitIndexLocal=-1; // AdC
2000       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2001         if (digitsInVolumeIndices[digIndex]<0) continue;
2002         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2003         if (digitInteresting->GetToT()<=0) continue; // AdC
2004         digitIndexLocal++; // AdC
2005
2006         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2007                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2008                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2009                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2010                         digitsInVolumeIndices[digIndex],
2011                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2012
2013         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2014           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2015                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2016                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2017                               digitsInVolumeIndices[digIndex],
2018                               digitInteresting->GetTracks());
2019       }
2020       if (digitIndexLocal==-1) interestingCounter--; // AdC
2021     }
2022
2023
2024     vol[4] = iPadZ, vol[3] = iPadX+1;
2025
2026     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2027       interestingCounter++;
2028       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2029         digitsInVolumeIndices[ii] = -1;
2030       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2031       digitIndexLocal=-1; // AdC
2032       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2033         if (digitsInVolumeIndices[digIndex]<0) continue;
2034         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2035         if (digitInteresting->GetToT()<=0) continue; // AdC
2036         digitIndexLocal++; // AdC
2037
2038         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2039                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2040                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2041                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2042                         digitsInVolumeIndices[digIndex],
2043                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2044
2045         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2046           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2047                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2048                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2049                               digitsInVolumeIndices[digIndex],
2050                               digitInteresting->GetTracks());
2051       }
2052       if (digitIndexLocal==-1) interestingCounter--; // AdC
2053     }
2054
2055
2056     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2057
2058     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2059       interestingCounter++;
2060       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2061         digitsInVolumeIndices[ii] = -1;
2062       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2063       digitIndexLocal=-1; // AdC
2064       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2065         if (digitsInVolumeIndices[digIndex]<0) continue;
2066         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2067         if (digitInteresting->GetToT()<=0) continue; // AdC
2068         digitIndexLocal++; // AdC
2069
2070         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2071                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2072                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2073                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2074                         digitsInVolumeIndices[digIndex],
2075                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2076
2077         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2078           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2079                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2080                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2081                               digitsInVolumeIndices[digIndex],
2082                               digitInteresting->GetTracks());
2083       }
2084       if (digitIndexLocal==-1) interestingCounter--; // AdC
2085     }
2086
2087
2088     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2089
2090     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2091       interestingCounter++;
2092       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2093         digitsInVolumeIndices[ii] = -1;
2094       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2095       digitIndexLocal=-1; // AdC
2096       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2097         if (digitsInVolumeIndices[digIndex]<0) continue;
2098         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2099         if (digitInteresting->GetToT()<=0) continue; // AdC
2100         digitIndexLocal++; // AdC
2101
2102         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2103                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2104                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2105                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2106                         digitsInVolumeIndices[digIndex],
2107                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2108
2109         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2110           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2111                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2112                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2113                               digitsInVolumeIndices[digIndex],
2114                               digitInteresting->GetTracks());
2115       }
2116       if (digitIndexLocal==-1) interestingCounter--; // AdC
2117     }
2118
2119     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
2120
2121     for (Int_t adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2122       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2123         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2124
2125         for (Int_t adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2126           for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2127             if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2128
2129             if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) continue;
2130
2131             interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2132             interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2133             interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2134             interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2135             Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2136             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2137             Int_t volDum = vol1[3];
2138             vol1[3] = vol1[4];
2139             vol1[4] = volDum;
2140             fTOFGeometry->GetPosPar(vol1,pos);
2141             interestingX[0] = pos[0];
2142             interestingY[0] = pos[1];
2143             interestingZ[0] = pos[2];
2144
2145             interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2146             interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2147             interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2148             interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2149             Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2150             AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2151             volDum = vol2[3];
2152             vol2[3] = vol2[4];
2153             vol2[4] = volDum;
2154             fTOFGeometry->GetPosPar(vol2,pos);
2155             interestingX[1] = pos[0];
2156             interestingY[1] = pos[1];
2157             interestingZ[1] = pos[2];
2158
2159
2160             AverageCalculations(2, interestingX, interestingY, interestingZ,
2161                                 interestingTOF, interestingTOT, interestingADC,
2162                                 interestingWeight,
2163                                 parTOF, posClus, check);
2164
2165             for (jj=0; jj<5; jj++) det[jj] = -1;
2166             for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2167             fTOFGeometry->GetDetID(posF, det);
2168
2169             volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2170             //volIdClus = GetClusterVolIndex(det);
2171
2172             for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2173             padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2174             padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2175             padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2176             padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2177
2178             for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2179             Int_t ** indDet = new Int_t*[2];
2180             for (jj=0; jj<2; jj++) indDet[jj] = new Int_t [5];
2181             for (jj=0; jj<2; jj++) indDet[jj][0] = nSector;
2182             for (jj=0; jj<2; jj++) indDet[jj][1] = nPlate;
2183             for (jj=0; jj<2; jj++) indDet[jj][2] = nStrip;
2184             for (jj=0; jj<2; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2185             for (jj=0; jj<2; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2186             GetClusterPars(/*check,*/ 2, indDet, interestingWeight, posClus, covClus);
2187             for (jj=0; jj<2; jj++) delete [] indDet[jj];
2188             delete [] indDet;
2189
2190             // To fill the track index array
2191             dummyCounter=-1;
2192             for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2193             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2194               if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2195               else {
2196                 dummyCounter++;
2197                 tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2198               }
2199             }
2200             for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2201               if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2202               else {
2203
2204                 alreadyStored = kFALSE;
2205                 for (jj=0; jj<dummyCounter+1; jj++)
2206                   alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2207
2208                 if (alreadyStored) continue;
2209                 if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2210                   AliWarning("  Siamo al limite!");
2211                   continue;
2212                 }
2213
2214                 dummyCounter++;
2215                 tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2216
2217               }
2218
2219             }
2220
2221
2222             AliTOFcluster *tofCluster =
2223               new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2224                                 covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2225                                 tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
2226             InsertCluster(tofCluster);
2227
2228             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", 
2229                              volIdClus, posClus[0], posClus[1], posClus[2],
2230                              covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2231                              tracks[0], tracks[1], tracks[2],
2232                              det[0], det[1], det[2], det[3], det[4],
2233                              parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2234                              status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2235
2236             volDum = vol1[3];
2237             vol1[3] = vol1[4];
2238             vol1[4] = volDum;
2239             fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2240             volDum = vol2[3];
2241             vol2[3] = vol2[4];
2242             vol2[4] = volDum;
2243             fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2244
2245
2246           } // close loop on second digit
2247         } // close loop on adesso2
2248
2249       } // close loop on first digit
2250     } // close loop on adesso1
2251
2252     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2253       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
2254         selectedDigit[ii][jj] = 0x0;
2255
2256   } // loop on iPad
2257
2258   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
2259     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
2260       delete [] selectedDigit[ii][jj];
2261       selectedDigit[ii][jj] = 0x0;
2262     }
2263     delete [] selectedDigit[ii];
2264     selectedDigit[ii] = 0x0;
2265   }
2266   delete [] selectedDigit;
2267   selectedDigit = 0x0;
2268
2269 }
2270 //_____________________________________________________________________________
2271
2272 void AliTOFClusterFinderV1::FindClustersPerStrip(Int_t nSector,
2273                                                  Int_t nPlate,
2274                                                  Int_t nStrip,
2275                                                  Int_t group)
2276 {
2277   //
2278   // This function searches the neighbouring digits (stored in the fDigits object),
2279   // to perform clusters (stored in the fTofClusters array).
2280   //
2281   // Each strip is read four times:
2282   //  - 1st time: it searches possible clusters formed by four
2283   //              neighbouring digits;
2284   //  - 2nd time: it searches possible clusters formed by three
2285   //              neighbouring digits;
2286   //  - 3rd time: it searches possible clusters formed by two
2287   //              neighbouring digits;
2288   //  - 4th time: the remaining isolated digits have been transformed
2289   //              in clusters.
2290   // This research has been made by checking the fTOFdigitMap object,
2291   // filled at digits/raw-data reading time.
2292   //
2293
2294   const Int_t kMaxNumberOfInterestingPads = 4;
2295   const Int_t kMaxNumberOfTracksPerDigit = 3;
2296   const Int_t kMaxNumberOfDigitsPerVolume = 10;
2297
2298   Int_t ii = 0;
2299
2300   Int_t digitsInVolumeIndices[kMaxNumberOfDigitsPerVolume];
2301   for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2302     digitsInVolumeIndices[ii] = -1;
2303
2304   Int_t vol[5] = {nSector,nPlate,nStrip,-1,-1};
2305
2306   Float_t pos[3] = {0.,0.,0.};
2307
2308   Int_t jj = 0;
2309   Int_t interestingPadX[kMaxNumberOfInterestingPads];
2310   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadX[jj] = -1;
2311   Int_t interestingPadZ[kMaxNumberOfInterestingPads];
2312   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingPadZ[jj] = -1;
2313   Double_t interestingTOT[kMaxNumberOfInterestingPads];
2314   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOT[jj] = 0;
2315   Double_t interestingADC[kMaxNumberOfInterestingPads];
2316   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingADC[jj] = 0;
2317   Double_t interestingTOF[kMaxNumberOfInterestingPads];
2318   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingTOF[jj] = 0;
2319   Double_t interestingWeight[kMaxNumberOfInterestingPads];
2320   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingWeight[jj] = 0;
2321
2322   Float_t interestingX[kMaxNumberOfInterestingPads];
2323   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingX[jj] = 0;
2324   Float_t interestingY[kMaxNumberOfInterestingPads];
2325   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingY[jj] = 0;
2326   Float_t interestingZ[kMaxNumberOfInterestingPads];
2327   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interestingZ[jj] = 0;
2328
2329   Float_t interDigit[kMaxNumberOfInterestingPads];
2330   for (jj=0; jj<kMaxNumberOfInterestingPads; jj++) interDigit[jj] = 0;
2331
2332   Int_t padsCluster[11];
2333   padsCluster[0] = nSector;
2334   padsCluster[1] = nPlate;
2335   padsCluster[2] = nStrip;
2336   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2337
2338   Int_t interestingCounter=-1;
2339   Int_t digitIndexLocal = -1;
2340   Int_t iPad  = -1;
2341   Int_t iPadX = -1;
2342   Int_t iPadZ = -1;
2343
2344   Bool_t check = kFALSE;
2345   Int_t parTOF[7];
2346   for (jj=0; jj<7; jj++) parTOF[jj] = 0;
2347   Double_t posClus[3];
2348   for (jj=0; jj<3; jj++) posClus[jj] = 0.;
2349   Int_t det[5];
2350   for (jj=0; jj<5; jj++) det[jj] = -1;
2351   Float_t posF[3];
2352   for (jj=0; jj<3; jj++) posF[jj] = 0.;
2353   UShort_t volIdClus = 0;
2354   Bool_t status = kTRUE; //assume all sim channels ok in the beginning...
2355   Double_t covClus[6];
2356   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2357   Int_t tracks[kMaxNumberOfTracksPerDigit];
2358   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2359   Int_t dummyCounter=-1;
2360   Bool_t alreadyStored = kFALSE;
2361
2362   AliTOFselectedDigit ***selectedDigit = new AliTOFselectedDigit**[kMaxNumberOfInterestingPads];
2363   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2364     selectedDigit[ii] = new AliTOFselectedDigit*[kMaxNumberOfDigitsPerVolume];
2365
2366   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2367     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) selectedDigit[ii][jj] = 0x0;
2368
2369   AliTOFdigit *digitInteresting;
2370
2371   group = group-1;
2372
2373   for (iPad=0; iPad<AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX()-3; iPad+=2) {
2374
2375     iPadZ = iPad%AliTOFGeometry::NpadZ(); //iPad%2;
2376     iPadX = iPad/AliTOFGeometry::NpadZ(); //iPad/2;
2377
2378     AliDebug(3, Form("%2d %1d %2d\n", iPad, iPadZ, iPadX));
2379
2380
2381
2382
2383
2384
2385     interestingCounter=-1;
2386
2387     vol[4] = iPadZ  , vol[3]  = iPadX;
2388     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2389       interestingCounter++;
2390
2391     vol[4] = iPadZ, vol[3] = iPadX+1;
2392     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2393       interestingCounter++;
2394
2395     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2396     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2397       interestingCounter++;
2398
2399     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2400     if (fTOFdigitMap->GetNumberOfDigits(vol)>0)
2401       interestingCounter++;
2402
2403     if (interestingCounter!=group) continue;
2404     else interestingCounter=-1;
2405
2406
2407     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
2408       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
2409         selectedDigit[ii][jj] = 0x0;
2410
2411
2412     vol[4] = iPadZ, vol[3] = iPadX;
2413
2414     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2415       interestingCounter++;
2416       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2417         digitsInVolumeIndices[ii] = -1;
2418       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2419       digitIndexLocal=-1; // AdC
2420       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2421         if (digitsInVolumeIndices[digIndex]<0) continue;
2422         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2423         if (digitInteresting->GetToT()<=0) continue; // AdC
2424         digitIndexLocal++; // AdC
2425
2426         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2427                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2428                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2429                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2430                         digitsInVolumeIndices[digIndex],
2431                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2432         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2433
2434         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2435           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2436                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2437                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2438                               digitsInVolumeIndices[digIndex],
2439                               digitInteresting->GetTracks());
2440       }
2441       if (digitIndexLocal==-1) interestingCounter--; // AdC
2442     }
2443
2444
2445     vol[4] = iPadZ, vol[3] = iPadX+1;
2446
2447     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2448       interestingCounter++;
2449       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2450         digitsInVolumeIndices[ii] = -1;
2451       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2452       digitIndexLocal=-1; // AdC
2453       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2454         if (digitsInVolumeIndices[digIndex]<0) continue;
2455         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2456         if (digitInteresting->GetToT()<=0) continue; // AdC
2457         digitIndexLocal++; // AdC
2458
2459         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2460                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2461                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2462                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2463                         digitsInVolumeIndices[digIndex],
2464                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2465         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2466
2467         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2468           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2469                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2470                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2471                               digitsInVolumeIndices[digIndex],
2472                               digitInteresting->GetTracks());
2473       }
2474       if (digitIndexLocal==-1) interestingCounter--; // AdC
2475     }
2476
2477
2478     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX;
2479
2480     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2481       interestingCounter++;
2482       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2483         digitsInVolumeIndices[ii] = -1;
2484       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2485       digitIndexLocal=-1; // AdC
2486       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2487         if (digitsInVolumeIndices[digIndex]<0) continue;
2488         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2489         if (digitInteresting->GetToT()<=0) continue; // AdC
2490         digitIndexLocal++; // AdC
2491
2492         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2493                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2494                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2495                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2496                         digitsInVolumeIndices[digIndex],
2497                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2498         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2499
2500         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2501           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2502                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2503                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2504                               digitsInVolumeIndices[digIndex],
2505                               digitInteresting->GetTracks());
2506       }
2507       if (digitIndexLocal==-1) interestingCounter--; // AdC
2508     }
2509
2510
2511     vol[4] = AliTOFGeometry::NpadZ()-(iPadZ+1), vol[3] = iPadX+1;
2512
2513     if (fTOFdigitMap->GetNumberOfDigits(vol)>0) {
2514       interestingCounter++;
2515       for (ii=0; ii<kMaxNumberOfDigitsPerVolume; ii++)
2516         digitsInVolumeIndices[ii] = -1;
2517       fTOFdigitMap->GetDigitIndex(vol, digitsInVolumeIndices);
2518       digitIndexLocal=-1; // AdC
2519       for(Int_t digIndex=0; digIndex<kMaxNumberOfDigitsPerVolume; digIndex++) {
2520         if (digitsInVolumeIndices[digIndex]<0) continue;
2521         digitInteresting = (AliTOFdigit*)fDigits->UncheckedAt(digitsInVolumeIndices[digIndex]);
2522         if (digitInteresting->GetToT()<=0) continue; // AdC
2523         digitIndexLocal++; // AdC
2524
2525         AliDebug(1,Form(" %2d %1d %2d %1d %2d  %d %d %d %d  %5d  %5d %5d %5d",
2526                         vol[0], vol[1], vol[2] ,vol[4], vol[3],
2527                         digitInteresting->GetTdc(), digitInteresting->GetAdc(),
2528                         digitInteresting->GetToT(), digitInteresting->GetToT()*digitInteresting->GetToT(),
2529                         digitsInVolumeIndices[digIndex],
2530                         digitInteresting->GetTrack(0), digitInteresting->GetTrack(1), digitInteresting->GetTrack(2)));
2531         AliDebug(1,Form("   to try   %d %d ",digIndex, interestingCounter));
2532
2533         selectedDigit[interestingCounter][digitIndexLocal] = new // AdC
2534           AliTOFselectedDigit(vol, digitInteresting->GetTdc(),
2535                               digitInteresting->GetAdc(), digitInteresting->GetToT(),
2536                               digitInteresting->GetToT()*digitInteresting->GetToT(),
2537                               digitsInVolumeIndices[digIndex],
2538                               digitInteresting->GetTracks());
2539       }
2540       if (digitIndexLocal==-1) interestingCounter--; // AdC
2541     }
2542
2543     AliDebug(1,Form(" e adesso %1d", interestingCounter+1));
2544
2545     Int_t adesso1 = -1;
2546     Int_t adesso2 = -1;
2547     Int_t adesso3 = -1;
2548     Int_t adesso4 = -1;
2549
2550     switch(interestingCounter+1) {
2551
2552     case 2:
2553
2554       //for (adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2555       adesso1 = 0;
2556         for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2557           if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2558
2559           //for (adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2560           adesso2 = 1;
2561             for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2562               if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2563
2564               if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime) {
2565                 AliDebug(1,Form(" selD1[%d][%d]->GetTDC()=%d selD2[%d][%d]->GetTDC()=%d -- %d ",
2566                                 adesso1,firstIndex,(Int_t)selectedDigit[adesso1][firstIndex]->GetTDC(),
2567                                 adesso2,secondIndex,(Int_t)selectedDigit[adesso2][secondIndex]->GetTDC(),
2568                                 fMaxDeltaTime));
2569                 continue;
2570               }
2571
2572               AliDebug(1, Form(" %1d %1d (0x%p) %1d %1d (0x%p)", adesso1, firstIndex,selectedDigit[adesso1][firstIndex],
2573                                adesso2, secondIndex,selectedDigit[adesso2][secondIndex]));
2574
2575               interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2576               interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2577               interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2578               interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2579               Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2580               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2581               Int_t volDum = vol1[3];
2582               vol1[3] = vol1[4];
2583               vol1[4] = volDum;
2584               fTOFGeometry->GetPosPar(vol1,pos);
2585               AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
2586               interestingX[0] = pos[0];
2587               interestingY[0] = pos[1];
2588               interestingZ[0] = pos[2];
2589
2590               interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2591               interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2592               interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2593               interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2594               Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2595               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2596               volDum = vol2[3];
2597               vol2[3] = vol2[4];
2598               vol2[4] = volDum;
2599               fTOFGeometry->GetPosPar(vol2,pos);
2600               AliDebug(1,Form(" %f %f %f", pos[0], pos[1], pos[2]));
2601               interestingX[1] = pos[0];
2602               interestingY[1] = pos[1];
2603               interestingZ[1] = pos[2];
2604
2605
2606               AverageCalculations(interestingCounter+1,
2607                                   interestingX, interestingY, interestingZ,
2608                                   interestingTOF, interestingTOT, interestingADC,
2609                                   interestingWeight,
2610                                   parTOF, posClus, check);
2611
2612               for (jj=0; jj<5; jj++) det[jj] = -1;
2613               for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2614
2615               AliDebug(1,Form(" %f %f %f", posF[0], posF[1], posF[2]));
2616               fTOFGeometry->GetDetID(posF, det);
2617               AliDebug(1,Form(" %2d %1d %2d %1d %2d", det[0], det[1], det[2], det[3], det[4]));
2618
2619               volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2620               //volIdClus = GetClusterVolIndex(det);
2621
2622               for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2623               padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2624               padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2625               padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2626               padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2627
2628               for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2629               Int_t ** indDet = new Int_t*[interestingCounter+1];
2630               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
2631               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
2632               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
2633               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
2634               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2635               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2636               GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
2637               for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
2638               delete [] indDet;
2639
2640               // To fill the track index array
2641               dummyCounter=-1;
2642               for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2643               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2644                 if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2645                 else {
2646                   dummyCounter++;
2647                   tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2648                 }
2649               }
2650               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2651                 if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2652                 else {
2653
2654                   alreadyStored = kFALSE;
2655                   for (jj=0; jj<dummyCounter+1; jj++)
2656                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2657
2658                   if (alreadyStored) continue;
2659                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2660                     AliWarning("  Siamo al limite!");
2661                     continue;
2662                   }
2663
2664                   dummyCounter++;
2665                   tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2666
2667                 }
2668
2669               }
2670
2671
2672               AliTOFcluster *tofCluster =
2673                 new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2674                                   covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2675                                   tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); //to updated
2676               InsertCluster(tofCluster);
2677
2678               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", 
2679                                volIdClus, posClus[0], posClus[1], posClus[2],
2680                                covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2681                                tracks[0], tracks[1], tracks[2],
2682                                det[0], det[1], det[2], det[3], det[4],
2683                                parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2684                                status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2685
2686               volDum = vol1[3];
2687               vol1[3] = vol1[4];
2688               vol1[4] = volDum;
2689               fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2690               volDum = vol2[3];
2691               vol2[3] = vol2[4];
2692               vol2[4] = volDum;
2693               fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2694
2695
2696             } // close loop on second digit
2697             //} // close loop on adesso2
2698
2699         } // close loop on first digit
2700           //} // close loop on adesso1
2701
2702
2703       break;
2704
2705     case 3:
2706
2707       //for (adesso1=0; adesso1<interestingCounter+1; adesso1++) {
2708       adesso1 = 0;
2709         for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2710           if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2711
2712           //for (adesso2=adesso1+1; adesso2<interestingCounter+1; adesso2++) {
2713           adesso2 = 1;
2714             for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2715               if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2716
2717               //for (adesso3=adesso2+1; adesso3<interestingCounter+1; adesso3++) {
2718               adesso3 = 2;
2719                 for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
2720                   if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
2721
2722
2723                   if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
2724                       ||
2725                       TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2726                       ||
2727                       TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime) continue;
2728
2729                   interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2730                   interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2731                   interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2732                   interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2733                   Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2734                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2735                   Int_t volDum = vol1[3];
2736                   vol1[3] = vol1[4];
2737                   vol1[4] = volDum;
2738                   fTOFGeometry->GetPosPar(vol1,pos);
2739                   interestingX[0] = pos[0];
2740                   interestingY[0] = pos[1];
2741                   interestingZ[0] = pos[2];
2742
2743                   interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2744                   interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2745                   interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2746                   interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2747                   Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2748                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2749                   volDum = vol2[3];
2750                   vol2[3] = vol2[4];
2751                   vol2[4] = volDum;
2752                   fTOFGeometry->GetPosPar(vol2,pos);
2753                   interestingX[1] = pos[0];
2754                   interestingY[1] = pos[1];
2755                   interestingZ[1] = pos[2];
2756
2757                   interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
2758                   interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
2759                   interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
2760                   interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
2761                   Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
2762                   AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
2763                   volDum = vol3[3];
2764                   vol3[3] = vol3[4];
2765                   vol3[4] = volDum;
2766                   fTOFGeometry->GetPosPar(vol3,pos);
2767                   interestingX[2] = pos[0];
2768                   interestingY[2] = pos[1];
2769                   interestingZ[2] = pos[2];
2770
2771
2772                   AverageCalculations(interestingCounter+1,
2773                                       interestingX, interestingY, interestingZ,
2774                                       interestingTOF, interestingTOT, interestingADC,
2775                                       interestingWeight,
2776                                       parTOF, posClus, check);
2777
2778                   for (jj=0; jj<5; jj++) det[jj] = -1;
2779                   for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2780                   fTOFGeometry->GetDetID(posF, det);
2781
2782                   volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2783                   //volIdClus = GetClusterVolIndex(det);
2784
2785                   for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
2786                   padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
2787                   padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
2788                   padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
2789                   padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
2790                   padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
2791                   padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
2792
2793                   for (jj=0; jj<6; jj++) covClus[jj] = 0.;
2794                   Int_t ** indDet = new Int_t*[interestingCounter+1];
2795                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
2796                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
2797                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
2798                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
2799                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
2800                   for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
2801                   GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
2802                   for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
2803                   delete [] indDet;
2804
2805
2806                   // To fill the track index array
2807                   dummyCounter=-1;
2808                   for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
2809                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2810                     if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
2811                     else {
2812                       dummyCounter++;
2813                       tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
2814                     }
2815                   }
2816                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2817                     if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
2818                     else {
2819
2820                       alreadyStored = kFALSE;
2821                       for (jj=0; jj<dummyCounter+1; jj++)
2822                         alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
2823
2824                       if (alreadyStored) continue;
2825                       if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2826                         AliWarning("  Siamo al limite!");
2827                         continue;
2828                       }
2829
2830                       dummyCounter++;
2831                       tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
2832
2833                     }
2834
2835                   }
2836                   for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
2837                     if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
2838                     else {
2839
2840                       alreadyStored = kFALSE;
2841                       for (jj=0; jj<dummyCounter+1; jj++)
2842                         alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
2843
2844                       if (alreadyStored) continue;
2845                       if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
2846                         AliWarning("  Siamo al limite!");
2847                         continue;
2848                       }
2849
2850                       dummyCounter++;
2851                       tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
2852
2853                     }
2854
2855                   }
2856
2857
2858                   AliTOFcluster *tofCluster =
2859                     new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
2860                                       covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2861                                       tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
2862                   InsertCluster(tofCluster);
2863
2864                   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", 
2865                                    volIdClus, posClus[0], posClus[1], posClus[2],
2866                                    covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
2867                                    tracks[0], tracks[1], tracks[2],
2868                                    det[0], det[1], det[2], det[3], det[4],
2869                                    parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
2870                                    status, selectedDigit[adesso1][firstIndex]->GetIndex()));
2871
2872                   volDum = vol1[3];
2873                   vol1[3] = vol1[4];
2874                   vol1[4] = volDum;
2875                   fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
2876                   volDum = vol2[3];
2877                   vol2[3] = vol2[4];
2878                   vol2[4] = volDum;
2879                   fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
2880                   volDum = vol3[3];
2881                   vol3[3] = vol3[4];
2882                   vol3[4] = volDum;
2883                   fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
2884
2885
2886                 } // close loop on third digit
2887                 //} // close loop on adesso3
2888
2889             } // close loop on second digit
2890             //} // close loop on adesso2
2891
2892         } // close loop on first digit
2893         //} // close loop on adesso1
2894
2895
2896       break;
2897
2898     case 4:
2899
2900       adesso1 = 0;
2901       for (Int_t firstIndex=0; firstIndex<kMaxNumberOfDigitsPerVolume; firstIndex++) {
2902         if (selectedDigit[adesso1][firstIndex]==0x0) continue;
2903
2904         adesso2 = 1;
2905         for (Int_t secondIndex=0; secondIndex<kMaxNumberOfDigitsPerVolume; secondIndex++) {
2906           if (selectedDigit[adesso2][secondIndex]==0x0) continue;
2907
2908           adesso3 = 2;
2909           for (Int_t thirdIndex=0; thirdIndex<kMaxNumberOfDigitsPerVolume; thirdIndex++) {
2910             if (selectedDigit[adesso3][thirdIndex]==0x0) continue;
2911
2912             adesso4 = 3;
2913             for (Int_t fourthIndex=0; fourthIndex<kMaxNumberOfDigitsPerVolume; fourthIndex++) {
2914               if (selectedDigit[adesso4][fourthIndex]==0x0) continue;
2915
2916
2917               if (TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso2][secondIndex]->GetTDC())>fMaxDeltaTime
2918                   ||
2919                   TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2920                   ||
2921                   TMath::Abs(selectedDigit[adesso1][firstIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime
2922                   ||
2923                   TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso3][thirdIndex]->GetTDC())>fMaxDeltaTime
2924                   ||
2925                   TMath::Abs(selectedDigit[adesso2][secondIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime
2926                   ||
2927                   TMath::Abs(selectedDigit[adesso3][thirdIndex]->GetTDC()-selectedDigit[adesso4][fourthIndex]->GetTDC())>fMaxDeltaTime) continue;
2928
2929               interestingTOF[0] = selectedDigit[adesso1][firstIndex]->GetTDC();
2930               interestingTOT[0] = selectedDigit[adesso1][firstIndex]->GetTOT();
2931               interestingADC[0] = selectedDigit[adesso1][firstIndex]->GetADC();
2932               interestingWeight[0] = selectedDigit[adesso1][firstIndex]->GetWeight();
2933               Int_t vol1[5]; for(jj=0; jj<5; jj++) vol1[jj] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(jj);
2934               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol1[0], vol1[1], vol1[2], vol1[4], vol1[3]));
2935               Int_t volDum = vol1[3];
2936               vol1[3] = vol1[4];
2937               vol1[4] = volDum;
2938               fTOFGeometry->GetPosPar(vol1,pos);
2939               interestingX[0] = pos[0];
2940               interestingY[0] = pos[1];
2941               interestingZ[0] = pos[2];
2942
2943               interestingTOF[1] = selectedDigit[adesso2][secondIndex]->GetTDC();
2944               interestingTOT[1] = selectedDigit[adesso2][secondIndex]->GetTOT();
2945               interestingADC[1] = selectedDigit[adesso2][secondIndex]->GetADC();
2946               interestingWeight[1] = selectedDigit[adesso2][secondIndex]->GetWeight();
2947               Int_t vol2[5]; for(jj=0; jj<5; jj++) vol2[jj] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(jj);
2948               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol2[0], vol2[1], vol2[2], vol2[4], vol2[3]));
2949               volDum = vol2[3];
2950               vol2[3] = vol2[4];
2951               vol2[4] = volDum;
2952               fTOFGeometry->GetPosPar(vol2,pos);
2953               interestingX[1] = pos[0];
2954               interestingY[1] = pos[1];
2955               interestingZ[1] = pos[2];
2956
2957               interestingTOF[2] = selectedDigit[adesso3][thirdIndex]->GetTDC();
2958               interestingTOT[2] = selectedDigit[adesso3][thirdIndex]->GetTOT();
2959               interestingADC[2] = selectedDigit[adesso3][thirdIndex]->GetADC();
2960               interestingWeight[2] = selectedDigit[adesso3][thirdIndex]->GetWeight();
2961               Int_t vol3[5]; for(jj=0; jj<5; jj++) vol3[jj] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(jj);
2962               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol3[0], vol3[1], vol3[2], vol3[4], vol3[3]));
2963               volDum = vol3[3];
2964               vol3[3] = vol3[4];
2965               vol3[4] = volDum;
2966               fTOFGeometry->GetPosPar(vol3,pos);
2967               interestingX[2] = pos[0];
2968               interestingY[2] = pos[1];
2969               interestingZ[2] = pos[2];
2970
2971               interestingTOF[3] = selectedDigit[adesso4][fourthIndex]->GetTDC();
2972               interestingTOT[3] = selectedDigit[adesso4][fourthIndex]->GetTOT();
2973               interestingADC[3] = selectedDigit[adesso4][fourthIndex]->GetADC();
2974               interestingWeight[3] = selectedDigit[adesso4][fourthIndex]->GetWeight();
2975               Int_t vol4[5]; for(jj=0; jj<5; jj++) vol4[jj] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(jj);
2976               AliDebug(1,Form(" %2d %1d %2d %1d %2d", vol4[0], vol4[1], vol4[2], vol4[4], vol4[3]));
2977               volDum = vol4[3];
2978               vol4[3] = vol4[4];
2979               vol4[4] = volDum;
2980               fTOFGeometry->GetPosPar(vol4,pos);
2981               interestingX[3] = pos[0];
2982               interestingY[3] = pos[1];
2983               interestingZ[3] = pos[2];
2984
2985
2986               AverageCalculations(interestingCounter+1,
2987                                   interestingX, interestingY, interestingZ,
2988                                   interestingTOF, interestingTOT, interestingADC,
2989                                   interestingWeight,
2990                                   parTOF, posClus, check);
2991
2992               for (jj=0; jj<5; jj++) det[jj] = -1;
2993               for (jj=0; jj<3; jj++) posF[jj] = posClus[jj];
2994               fTOFGeometry->GetDetID(posF, det);
2995
2996               volIdClus = fTOFGeometry->GetAliSensVolIndex(det[0],det[1],det[2]);
2997               //volIdClus = GetClusterVolIndex(det);
2998
2999               for (jj=3; jj<11; jj++) padsCluster[jj] = -1;
3000               padsCluster[3] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(4);
3001               padsCluster[4] = selectedDigit[adesso1][firstIndex]->GetDetectorIndex(3);
3002               padsCluster[5] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(4);
3003               padsCluster[6] = selectedDigit[adesso2][secondIndex]->GetDetectorIndex(3);
3004               padsCluster[7] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(4);
3005               padsCluster[8] = selectedDigit[adesso3][thirdIndex]->GetDetectorIndex(3);
3006               padsCluster[9] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(4);
3007               padsCluster[10] = selectedDigit[adesso4][fourthIndex]->GetDetectorIndex(3);
3008
3009               for (jj=0; jj<6; jj++) covClus[jj] = 0.;
3010               Int_t ** indDet = new Int_t*[interestingCounter+1];
3011               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj] = new Int_t [5];
3012               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][0] = nSector;
3013               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][1] = nPlate;
3014               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][2] = nStrip;
3015               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][3] = padsCluster[2*jj+3];
3016               for (jj=0; jj<interestingCounter+1; jj++) indDet[jj][4] = padsCluster[2*jj+1+3];
3017               GetClusterPars(/*check,*/ interestingCounter+1, indDet, interestingWeight, posClus, covClus);
3018               for (jj=0; jj<interestingCounter+1; jj++) delete [] indDet[jj];
3019               delete [] indDet;
3020
3021               // To fill the track index array
3022               dummyCounter=-1;
3023               for (jj=0; jj<kMaxNumberOfTracksPerDigit; jj++) tracks[jj] = -1;
3024               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3025                 if (selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk)==-1) continue;
3026                 else {
3027                   dummyCounter++;
3028                   tracks[dummyCounter] = selectedDigit[adesso1][firstIndex]->GetTrackLabel(kk);
3029                 }
3030               }
3031               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3032                 if (selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk)==-1) continue;
3033                 else {
3034
3035                   alreadyStored = kFALSE;
3036                   for (jj=0; jj<dummyCounter+1; jj++)
3037                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk));
3038
3039                   if (alreadyStored) continue;
3040                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3041                     AliWarning("  Siamo al limite!");
3042                     continue;
3043                   }
3044
3045                   dummyCounter++;
3046                   tracks[dummyCounter] = selectedDigit[adesso2][secondIndex]->GetTrackLabel(kk);
3047
3048                 }
3049
3050               }
3051               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3052                 if (selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk)==-1) continue;
3053                 else {
3054
3055                   alreadyStored = kFALSE;
3056                   for (jj=0; jj<dummyCounter+1; jj++)
3057                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk));
3058
3059                   if (alreadyStored) continue;
3060                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3061                     AliWarning("  Siamo al limite!");
3062                     continue;
3063                   }
3064
3065                   dummyCounter++;
3066                   tracks[dummyCounter] = selectedDigit[adesso3][thirdIndex]->GetTrackLabel(kk);
3067
3068                 }
3069
3070               }
3071               for (Int_t kk=0; kk<kMaxNumberOfTracksPerDigit; kk++) { // three is the max number of tracks associated to one digit
3072                 if (selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk)==-1) continue;
3073                 else {
3074
3075                   alreadyStored = kFALSE;
3076                   for (jj=0; jj<dummyCounter+1; jj++)
3077                     alreadyStored = alreadyStored || (tracks[jj]!=-1 && tracks[jj]==selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk));
3078
3079                   if (alreadyStored) continue;
3080                   if (dummyCounter==2) { // three is the max number of tracks associated to one cluster
3081                     AliWarning("  Siamo al limite!");
3082                     continue;
3083                   }
3084
3085                   dummyCounter++;
3086                   tracks[dummyCounter] = selectedDigit[adesso4][fourthIndex]->GetTrackLabel(kk);
3087
3088                 }
3089
3090               }
3091
3092
3093               AliTOFcluster *tofCluster =
3094                 new AliTOFcluster(volIdClus, posClus[0], posClus[1], posClus[2],
3095                                   covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
3096                                   tracks, det, parTOF, status, selectedDigit[adesso1][firstIndex]->GetIndex()); // to be updated
3097               InsertCluster(tofCluster);
3098
3099               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", 
3100                                volIdClus, posClus[0], posClus[1], posClus[2],
3101                                covClus[0], covClus[1], covClus[2], covClus[3], covClus[4], covClus[5],
3102                                tracks[0], tracks[1], tracks[2],
3103                                det[0], det[1], det[2], det[3], det[4],
3104                                parTOF[0], parTOF[1], parTOF[2], parTOF[3], parTOF[4],
3105                                status, selectedDigit[adesso1][firstIndex]->GetIndex()));
3106
3107               volDum = vol1[3];
3108               vol1[3] = vol1[4];
3109               vol1[4] = volDum;
3110               fTOFdigitMap->ResetDigitNumber(vol1,selectedDigit[adesso1][firstIndex]->GetIndex());
3111               volDum = vol2[3];
3112               vol2[3] = vol2[4];
3113               vol2[4] = volDum;
3114               fTOFdigitMap->ResetDigitNumber(vol2,selectedDigit[adesso2][secondIndex]->GetIndex());
3115               volDum = vol3[3];
3116               vol3[3] = vol3[4];
3117               vol3[4] = volDum;
3118               fTOFdigitMap->ResetDigitNumber(vol3,selectedDigit[adesso3][thirdIndex]->GetIndex());
3119               volDum = vol4[3];
3120               vol4[3] = vol4[4];
3121               vol4[4] = volDum;
3122               fTOFdigitMap->ResetDigitNumber(vol4,selectedDigit[adesso4][fourthIndex]->GetIndex());
3123
3124
3125             } // close loop on fourth digit
3126
3127           } // close loop on third digit
3128
3129         } // close loop on second digit
3130
3131       } // close loop on first digit
3132
3133       break;
3134
3135     }
3136
3137     for (ii=0; ii<kMaxNumberOfInterestingPads; ii++)
3138       for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++)
3139         selectedDigit[ii][jj] = 0x0;
3140
3141   } // loop on iPad
3142
3143   for (ii=0; ii<kMaxNumberOfInterestingPads; ii++) {
3144     for (jj=0; jj<kMaxNumberOfDigitsPerVolume; jj++) {
3145       delete [] selectedDigit[ii][jj];
3146       selectedDigit[ii][jj] = 0x0;
3147     }
3148     delete [] selectedDigit[ii];
3149     selectedDigit[ii] = 0x0;
3150   }
3151   delete [] selectedDigit;
3152   selectedDigit = 0x0;
3153
3154 }
3155 //_____________________________________________________________________________
3156
3157 Int_t AliTOFClusterFinderV1::InsertCluster(AliTOFcluster *tofCluster)
3158 {
3159   //
3160   // This function adds a TOF cluster to the array of TOF clusters
3161   // sorted in Z, i.e. fTofClusters
3162   /