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