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