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