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