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