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