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