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