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