]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0Reconstructor.cxx
ca1a09d5fb33469d94ed562e64cc916d0ff26f73
[u/mrichter/AliRoot.git] / T0 / AliT0Reconstructor.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 /* $Id$ */
17 /*********************************************************************
18  *  T0 reconstruction and filling ESD
19  *  - reconstruct mean time (interation time) 
20  *  - vertex position
21  *  -  multiplicity
22  ********************************************************************/
23
24 #include <AliESDEvent.h>
25 #include "AliLog.h"
26 #include "AliT0RecPoint.h"
27 #include "AliRawReader.h"
28 #include "AliT0RawReader.h"
29 #include "AliT0digit.h"
30 #include "AliT0Reconstructor.h"
31 #include "AliT0Parameters.h"
32 #include "AliT0Calibrator.h"
33 #include "AliESDfriend.h"
34 #include "AliESDTZERO.h"
35 #include "AliESDTZEROfriend.h"
36 #include "AliLog.h"
37 #include "AliCDBEntry.h" 
38 #include "AliCDBManager.h"
39 #include "AliCTPTimeParams.h"
40 #include "AliLHCClockPhase.h"
41 #include "AliT0CalibSeasonTimeShift.h"
42 #include "AliESDRun.h"
43
44 #include <TArrayI.h>
45 #include <TGraph.h>
46 #include <TMath.h>
47 #include <Riostream.h>
48
49 ClassImp(AliT0Reconstructor)
50
51   AliT0Reconstructor:: AliT0Reconstructor(): AliReconstructor(),
52                                              fdZonA(0),
53                                              fdZonC(0),
54                                              fZposition(0),
55                                              fParam(NULL),
56                                              fAmpLEDrec(),
57                                              fQTC(0),
58                                              fAmpLED(0),
59                                              fCalib(),
60                                              fLatencyHPTDC(9000),
61                                              fLatencyL1(0),
62                                              fLatencyL1A(0),
63                                              fLatencyL1C(0),
64                                              fGRPdelays(0),
65                                              fTimeMeanShift(0x0),
66                                              fTimeSigmaShift(0x0),
67                                              fESDTZEROfriend(NULL),
68                                              fESDTZERO(NULL)
69
70 {
71   for (Int_t i=0; i<24; i++)  fTime0vertex[i] =0;
72
73   //constructor
74   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
75   if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
76   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
77   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
78
79   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
80   if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
81   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
82   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
83  
84   AliCDBEntry *entry4 = AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
85   if (!entry4) AliFatal("LHC clock-phase shift is not found in OCDB !");
86   AliLHCClockPhase *phase = (AliLHCClockPhase*)entry4->GetObject();
87
88   fGRPdelays = l1Delay - phase->GetMeanPhase();
89
90   AliCDBEntry *entry5 = AliCDBManager::Instance()->Get("T0/Calib/TimeAdjust");
91   if (entry5) {
92     AliT0CalibSeasonTimeShift *timeshift = (AliT0CalibSeasonTimeShift*)entry5->GetObject();
93     fTimeMeanShift = timeshift->GetT0Means();
94     fTimeSigmaShift  = timeshift->GetT0Sigmas();
95    }
96   else
97     AliWarning("Time Adjust is not found in OCDB !");
98  
99   fParam = AliT0Parameters::Instance();
100   fParam->Init();
101  
102   for (Int_t i=0; i<24; i++){
103         TGraph* gr = fParam ->GetAmpLEDRec(i);
104         if (gr) fAmpLEDrec.AddAtAndExpand(gr,i) ; 
105           TGraph* gr1 = fParam ->GetAmpLED(i);
106           if (gr1) fAmpLED.AddAtAndExpand(gr1,i) ; 
107           TGraph* gr2 = fParam ->GetQTC(i);
108           if (gr2) fQTC.AddAtAndExpand(gr2,i) ;         
109           fTime0vertex[i] = fParam->GetCFD(i);
110           AliDebug(2,Form("OCDB mean CFD time %i %f \n",i, fTime0vertex[i]));
111  }
112   fLatencyL1 = fParam->GetLatencyL1();
113   fLatencyL1A = fParam->GetLatencyL1A(); 
114   fLatencyL1C = fParam->GetLatencyL1C();
115   fLatencyHPTDC = fParam->GetLatencyHPTDC();
116   AliDebug(2,Form(" LatencyL1 %f latencyL1A %f latencyL1C %f latencyHPTDC %f \n",fLatencyL1, fLatencyL1A, fLatencyL1C, fLatencyHPTDC));
117  
118   for (Int_t i=0; i<24; i++) {
119     if( fTime0vertex[i] < 500 || fTime0vertex[i] > 50000) fTime0vertex[i] =( 1000.*fLatencyHPTDC - 1000.*fLatencyL1 + 1000.*fGRPdelays)/24.4;
120     //  printf(" calulated mean %i %f \n",i, fTime0vertex[i]);
121   }
122   //here real Z position
123   fdZonC = TMath::Abs(fParam->GetZPosition("T0/C/PMT1"));
124   fdZonA = TMath::Abs(fParam->GetZPosition("T0/A/PMT15"));
125
126   fCalib = new AliT0Calibrator();
127   fESDTZEROfriend = new AliESDTZEROfriend();
128   fESDTZERO  = new AliESDTZERO();
129
130  
131 }
132
133 //_____________________________________________________________________________
134 void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
135 {
136   // T0 digits reconstruction
137   Int_t refAmp = 0 ; /*Int_t (GetRecoParam()->GetRefAmp());*/
138   
139   TArrayI * timeCFD = new TArrayI(24); 
140   TArrayI * timeLED = new TArrayI(24); 
141   TArrayI * chargeQT0 = new TArrayI(24); 
142   TArrayI * chargeQT1 = new TArrayI(24); 
143
144  
145   Float_t c = 29.9792458; // cm/ns
146   Float_t channelWidth = fParam->GetChannelWidth() ;  
147   Double32_t vertex = 9999999, meanVertex = 0 ;
148   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
149   
150   
151   AliDebug(1,Form("Start DIGITS reconstruction "));
152   
153   Float_t lowAmpThreshold =  GetRecoParam()->GetAmpLowThreshold();  
154   Float_t highAmpThreshold =  GetRecoParam()->GetAmpHighThreshold(); 
155   printf( "AliT0Reconstructor::Reconstruct::: RecoParam amplitude %f %f \n",lowAmpThreshold, highAmpThreshold);
156  
157   Double32_t besttimeA=9999999;  Double32_t besttimeA_best=9999999;
158   Double32_t besttimeC=9999999;  Double32_t besttimeC_best=9999999;
159   Int_t timeDelayCFD[24]; 
160   Int_t badpmt[24];
161   //Bad channel
162   for (Int_t i=0; i<24; i++) {
163     badpmt[i] = GetRecoParam() -> GetBadChannels(i);
164     timeDelayCFD[i] =  Int_t (fParam->GetTimeDelayCFD(i));
165   }
166   fCalib->SetEq(0);
167   TBranch *brDigits=digitsTree->GetBranch("T0");
168   AliT0digit *fDigits = new AliT0digit() ;
169   if (brDigits) {
170     brDigits->SetAddress(&fDigits);
171   }else{
172     AliError(Form("EXEC Branch T0 digits not found"));
173     return;
174   }
175   
176   digitsTree->GetEvent(0);
177   digitsTree->GetEntry(0);
178   brDigits->GetEntry(0);
179   fDigits->GetTimeCFD(*timeCFD);
180   fDigits->GetTimeLED(*timeLED);
181   fDigits->GetQT0(*chargeQT0);
182   fDigits->GetQT1(*chargeQT1);
183   Int_t onlineMean =  fDigits->MeanTime();
184   
185   Bool_t tr[5];
186   for (Int_t i=0; i<5; i++) tr[i]=false; 
187   
188   
189   AliT0RecPoint frecpoints;
190   AliT0RecPoint * pfrecpoints = &frecpoints;
191   clustersTree->Branch( "T0", "AliT0RecPoint" ,&pfrecpoints);
192   
193   Float_t time[24], adc[24], adcmip[24];
194   for (Int_t ipmt=0; ipmt<24; ipmt++) {
195     if(timeCFD->At(ipmt)>0 ) {
196       Float_t timefull = 0.001*( timeCFD->At(ipmt) - 511 - timeDelayCFD[ipmt])  * channelWidth;
197       frecpoints.SetTimeFull(ipmt, 0 ,timefull) ;
198       if(( chargeQT1->At(ipmt) - chargeQT0->At(ipmt))>0)  
199         adc[ipmt] = chargeQT1->At(ipmt) - chargeQT0->At(ipmt);
200       else
201         adc[ipmt] = 0;
202       
203       time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, Int_t(adc[ipmt]),  timeCFD->At(ipmt)) ;
204       time[ipmt] =   time[ipmt] - 511;   
205       Double_t sl = Double_t(timeLED->At(ipmt) - timeCFD->At(ipmt));
206       //    time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl),  timeCFD->At(ipmt) ) ;
207       AliDebug(5,Form(" ipmt %i QTC  %i , time in chann %i (led-cfd) %i ",
208                       ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
209       
210       Double_t ampMip = 0;
211       TGraph* ampGraph = (TGraph*)fAmpLED.At(ipmt);
212       if (ampGraph) ampMip = ampGraph->Eval(sl);
213       Double_t qtMip = 0;
214       TGraph* qtGraph = (TGraph*)fQTC.At(ipmt);
215       if (qtGraph) qtMip = qtGraph->Eval(adc[ipmt]);
216       AliDebug(5,Form("  Amlitude in MIPS LED %f ,  QTC %f in channels %f\n ",ampMip,qtMip, adc[ipmt]));
217       frecpoints.SetTime(ipmt, Float_t(time[ipmt]) );
218       frecpoints.SetAmpLED(ipmt, Float_t( ampMip)); 
219       frecpoints.SetAmp(ipmt, Float_t(qtMip));
220       adcmip[ipmt]=qtMip;
221       
222     }
223     else {
224       time[ipmt] = -99999;
225       adc[ipmt] = 0;
226       adcmip[ipmt] = 0;
227       
228     }
229   }
230   
231   for (Int_t ipmt=0; ipmt<12; ipmt++){
232     if(time[ipmt] !=0 &&  time[ipmt] > -9000 
233        &&  adcmip[ipmt]>lowAmpThreshold && adcmip[ipmt]<highAmpThreshold )
234       {
235         if(time[ipmt]<besttimeC) besttimeC=time[ipmt]; //timeC
236         if(TMath::Abs(time[ipmt])<TMath::Abs(besttimeC_best)) 
237           besttimeC_best=time[ipmt]; //timeC         
238       }
239   }
240   for ( Int_t ipmt=12; ipmt<24; ipmt++)
241     {
242       if(time[ipmt] != 0 &&  time[ipmt] > -9000 
243          && adcmip[ipmt]>lowAmpThreshold && adcmip[ipmt]<highAmpThreshold)
244         {
245           if(time[ipmt]<besttimeA) besttimeA=time[ipmt]; 
246           if(TMath::Abs(time[ipmt] ) < TMath::Abs(besttimeA_best)) 
247             besttimeA_best=time[ipmt]; //timeA
248         }
249     }
250   
251   if( besttimeA < 999999 && besttimeA!=0) {
252     frecpoints.SetTimeBestA((besttimeA_best * channelWidth  - fdZonA/c)  );
253     frecpoints.SetTime1stA((besttimeA * channelWidth  - fdZonA/c) );
254     tr[1]=true;
255   }
256   
257   if( besttimeC < 999999 && besttimeC!=0) {
258     frecpoints.SetTimeBestC((besttimeC_best * channelWidth  - fdZonC/c) );
259     frecpoints.SetTime1stC((besttimeC * channelWidth  - fdZonC/c) );
260     tr[2]=true;
261   }
262   
263   AliDebug(5,Form("1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f ",
264                   besttimeA, besttimeA_best,
265                   besttimeC, besttimeC_best) );
266
267   if(besttimeA <999999 && besttimeC < 999999 ){
268     //    timeDiff = (besttimeC - besttimeA)*channelWidth;
269     timeDiff = (besttimeA - besttimeC)*channelWidth;
270     meanTime = channelWidth * (besttimeA_best + besttimeC_best)/2. ; 
271     timeclock = channelWidth * (besttimeA + besttimeC)/2. ;
272     vertex = meanVertex - 0.001* c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2;
273     tr[0]=true; 
274   }
275   frecpoints.SetVertex(vertex);
276   frecpoints.SetMeanTime(meanTime );
277   frecpoints.SetT0clock(timeclock );
278   frecpoints.SetT0Trig(tr);
279   
280  AliDebug(5,Form("fRecPoints:::  1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f vertex %f",
281                   frecpoints.Get1stTimeA(),  frecpoints.GetBestTimeA(),
282                   frecpoints.Get1stTimeC(),  frecpoints.GetBestTimeC(), 
283                   vertex ) );
284   
285   AliDebug(5,Form("T0 triggers %d %d %d %d %d",tr[0],tr[1],tr[2],tr[3],tr[4]));
286   
287   //online mean
288   frecpoints.SetOnlineMean(Int_t(onlineMean));
289   AliDebug(10,Form("  timeDiff %f #channel,  meanTime %f #channel, vertex %f cm online mean %i timeclock %f ps",timeDiff, meanTime,vertex, Int_t(onlineMean), timeclock));
290   
291   clustersTree->Fill();
292   
293   delete timeCFD;
294   delete timeLED;
295   delete chargeQT0; 
296   delete chargeQT1; 
297 }
298
299
300 //_______________________________________________________________________
301
302 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
303 {
304   // T0 raw ->
305   //
306
307   Float_t meanOrA = fTime0vertex[0] + 587;
308   Float_t meanOrC = fTime0vertex[0] + 678;
309   Float_t meanTVDC = fTime0vertex[0] + 2564;
310   Int_t timeDelayCFD[24]; 
311   Int_t corridor = GetRecoParam() -> GetCorridor();  
312   printf("!!!! corrior %i \n",corridor);
313   Int_t badpmt[24];
314   //Bad channel
315   for (Int_t i=0; i<24; i++) {
316     badpmt[i] = GetRecoParam() -> GetBadChannels(i);
317     timeDelayCFD[i] =  Int_t (fParam->GetTimeDelayCFD(i));
318   }
319   Int_t equalize = GetRecoParam() -> GetEq();
320   //  printf( "AliT0Reconstructor::Reconstruct::: RecoParam %i \n",equalize);
321   fCalib->SetEq(equalize);
322   Int_t low[500], high[500];
323   Float_t timefull=-9999;;
324   Float_t tvdc  = -9999; Float_t ora = -9999; Float_t orc = -9999;
325
326   Int_t allData[110][5];
327   
328   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
329   Float_t time2zero[24]; 
330   Double32_t timeDiff, meanTime, timeclock;
331   timeDiff =  meanTime = timeclock = 9999999;
332   Float_t c = 29.9792458; // cm/ns
333   Double32_t vertex = 9999999;
334   Int_t onlineMean=0;
335   Float_t meanVertex = 0;
336   Int_t pedestal[24];
337   for (Int_t i0=0; i0<24; i0++) {
338     low[i0] = Int_t(fTime0vertex[i0]) - corridor;
339     high[i0] = Int_t(fTime0vertex[i0]) + corridor;
340     time2zero[i0] = 99999;
341     pedestal[i0]=Int_t (GetRecoParam()->GetLow(100+i0) );
342     //    printf("pmt %i pedestal %f\n", i0,pedestal[i0]);
343
344   }
345   
346   for (Int_t i0=0; i0<110; i0++)
347     for (Int_t j0=0; j0<5; j0++)  allData[i0][j0]=0; 
348   
349   Float_t lowAmpThreshold =  GetRecoParam()->GetAmpLowThreshold();  
350   Float_t highAmpThreshold =  GetRecoParam()->GetAmpHighThreshold(); 
351   printf( "AliT0Reconstructor::Reconstruct::: RecoParam amplitude %f %f \n",lowAmpThreshold, highAmpThreshold);
352  
353   Double32_t besttimeA=9999999;  Double32_t besttimeA_best=9999999;
354   Double32_t besttimeC=9999999;  Double32_t besttimeC_best=9999999;
355
356    Float_t channelWidth = fParam->GetChannelWidth() ;  
357         
358   AliT0RecPoint frecpoints;
359   AliT0RecPoint * pfrecpoints = &frecpoints;
360   
361   recTree->Branch( "T0", "AliT0RecPoint" ,&pfrecpoints);
362    
363   AliDebug(10," before read data ");
364   AliT0RawReader myrawreader(rawReader);
365
366   UInt_t type =rawReader->GetType();
367
368   if (!myrawreader.Next())
369     AliDebug(1,Form(" no raw data found!!"));
370   else
371     {  
372    for (Int_t i=0; i<24; i++)
373     {
374       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
375     }
376      Int_t fBCID=Int_t (rawReader->GetBCID());
377       Int_t trmbunch= myrawreader.GetTRMBunchID();
378       AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
379       if( (trmbunch-fBCID)!=37) {
380         AliDebug(0,Form("wrong :::: CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
381         type = -1;
382       }
383  
384       if(type == 7  ) {  //only physics 
385         for (Int_t i=0; i<107; i++) {
386         for (Int_t iHit=0; iHit<5; iHit++) 
387           {
388             allData[i][iHit] = myrawreader.GetData(i,iHit);
389           }
390         }
391         
392         for (Int_t in=0; in<12; in++)  
393           {
394             for (Int_t iHit=0; iHit<5; iHit++) 
395               {
396                 if(allData[in+1][iHit] > low[in] && 
397                    allData[in+1][iHit] < high[in])
398                   {
399                     timeCFD[in] = allData[in+1][iHit] ; 
400                     break;
401                   }
402               }
403             for (Int_t iHit=0; iHit<5; iHit++) 
404               {
405                 if(allData[in+1+56][iHit] > low[in+12] && 
406                    allData[in+1+56][iHit] < high[in+12])
407                   {
408                     timeCFD[in+12] = allData[in+56+1][iHit] ;
409                     break;
410                   }
411               }
412             timeLED[in+12] = allData[in+68+1][0] ;
413             timeLED[in] = allData[in+12+1][0] ;
414             AliDebug(50, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
415                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
416                              timeLED[in+12]));   
417             
418           }
419         
420         
421         for (Int_t in=0; in<12;  in++)
422           {
423             if(allData[2*in+26][0] > 4700 && 
424                allData[2*in+26][0] < 5700)
425                   {
426
427                     chargeQT0[in]=allData[2*in+25][0];
428                     chargeQT1[in]=allData[2*in+26][0];
429                     AliDebug(25, Form(" readed Raw %i %i %i",
430                                       in, chargeQT0[in],chargeQT1[in]));
431                   }
432           }     
433         for (Int_t in=12; in<24;  in++)
434           {
435             if(allData[2*in+58][0] > 4700 && 
436                allData[2*in+58][0] < 5700)
437             {
438               chargeQT0[in]=allData[2*in+57][0];
439               chargeQT1[in]=allData[2*in+58][0];
440               AliDebug(25, Form(" readed Raw %i %i %i",
441                                 in, chargeQT0[in],chargeQT1[in]));
442             }
443           }
444         
445         onlineMean = allData[49][0];
446         
447         Double32_t time[24], adc[24], adcmip[24], noncalibtime[24];
448         for (Int_t ipmt=0; ipmt<24; ipmt++) {
449           //      if(timeCFD[ipmt] >  0 && (chargeQT0[ipmt] - chargeQT1[ipmt])>pedestal[ipmt]  ){
450           if(timeCFD[ipmt] >  0 && (chargeQT0[ipmt] - chargeQT1[ipmt])> 0 ){
451            //for simulated data
452              //for physics  data
453             if(( chargeQT0[ipmt] - chargeQT1[ipmt])>pedestal[ipmt])  {
454              adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
455            }
456            else
457              adc[ipmt] = 0;
458            //      time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
459            Int_t refAmp = Int_t (fTime0vertex[ipmt]);
460            time[ipmt] = fCalib-> WalkCorrection( refAmp, ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
461            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
462            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
463            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
464                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
465            Double_t ampMip = 0;
466            TGraph * ampGraph =  (TGraph*)fAmpLED.At(ipmt);
467            if (ampGraph) ampMip = ampGraph->Eval(sl);
468            Double_t qtMip = 0;
469            TGraph * qtGraph = (TGraph*)fQTC.At(ipmt);
470            if (qtGraph) qtMip = qtGraph->Eval(adc[ipmt]);
471            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %f\n ",ampMip,qtMip, adc[ipmt]));
472            // if( qtMip>lowAmpThreshold && qtMip<highAmpThreshold )
473              {
474                if( equalize  ==0 ) 
475                  frecpoints.SetTime(ipmt, Float_t(time[ipmt]) );
476                else 
477                  frecpoints.SetTime(ipmt, Float_t(time[ipmt] + fTime0vertex[ipmt]) );
478                // frecpoints.SetTime(ipmt, Float_t(time[ipmt] ) );
479              }
480            frecpoints.SetAmp(ipmt, Double32_t( qtMip)); 
481            adcmip[ipmt]=qtMip;
482            frecpoints.SetAmpLED(ipmt, Double32_t(ampMip));           
483            noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
484          }
485          else {
486            time[ipmt] = -9999;
487            adc[ipmt] = 0;
488            adcmip[ipmt] = 0;
489            noncalibtime[ipmt] = -9999;
490          }
491        }
492        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ; 
493     
494        for (Int_t ipmt=0; ipmt<12; ipmt++){
495          if(time[ipmt] !=0 &&  time[ipmt] > -9000 
496             /*&& badpmt[ipmt]==0 */
497             &&  adcmip[ipmt]>lowAmpThreshold && adcmip[ipmt]<highAmpThreshold )
498            {
499              if(time[ipmt]<besttimeC) besttimeC=time[ipmt]; //timeC
500              if(TMath::Abs(time[ipmt])<TMath::Abs(besttimeC_best)) 
501                besttimeC_best=time[ipmt]; //timeC            
502            }
503        }
504        for ( Int_t ipmt=12; ipmt<24; ipmt++)
505          {
506            if(time[ipmt] != 0 &&  time[ipmt] > -9000 
507               /* && badpmt[ipmt]==0*/ 
508               && adcmip[ipmt]>lowAmpThreshold && adcmip[ipmt]<highAmpThreshold)
509              {
510                if(time[ipmt]<besttimeA) besttimeA=time[ipmt]; 
511                if(TMath::Abs(time[ipmt] ) < TMath::Abs(besttimeA_best)) 
512                  besttimeA_best=time[ipmt]; //timeA
513              }
514          }
515        
516        if(besttimeA < 999999 && besttimeA!=0 ) {
517          if( equalize  ==0 ) 
518            frecpoints.SetTime1stA((besttimeA * channelWidth)- 1000.*fLatencyHPTDC + 1000.*fLatencyL1A - 1000.*fGRPdelays - fTimeMeanShift[1] ); 
519          else
520            {
521              frecpoints.SetTimeBestA((besttimeA_best * channelWidth )); 
522              frecpoints.SetTime1stA((besttimeA * channelWidth - fTimeMeanShift[1])); 
523            }
524        }
525        if( besttimeC < 999999 && besttimeC!=0) {
526          if( equalize  ==0 ) 
527            frecpoints.SetTime1stC((besttimeC * channelWidth)- 1000.*fLatencyHPTDC +1000.*fLatencyL1C - 1000.*fGRPdelays - fTimeMeanShift[2]);
528          else
529            {
530              frecpoints.SetTimeBestC((besttimeC_best * channelWidth ));
531              frecpoints.SetTime1stC((besttimeC * channelWidth - fTimeMeanShift[2]));
532            }
533        }
534        AliDebug(5,Form("1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f ",
535                        besttimeA, besttimeA_best,
536                        besttimeC, besttimeC_best) );
537        AliDebug(5,Form("fRecPoints:::  1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f shiftA %f shiftC %f ",
538                        frecpoints.Get1stTimeA(),  frecpoints.GetBestTimeA(),
539                        frecpoints.Get1stTimeC(),  frecpoints.GetBestTimeC(), 
540                        fTimeMeanShift[1],fTimeMeanShift[2] ) );
541        if( besttimeC < 999999 &&  besttimeA < 999999) { 
542          if(equalize  ==0 )
543            timeclock = ((besttimeC * channelWidth)- 1000.*fLatencyHPTDC +1000.*fLatencyL1 - 1000.*fGRPdelays - fTimeMeanShift[0]);
544          else
545            {
546              timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - fTimeMeanShift[0];
547              meanTime = channelWidth * Float_t(besttimeA_best + besttimeC_best )/2.;
548            }
549          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth ;
550          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
551        }
552        
553       }  //if phys event       
554        AliDebug(1,Form("  timeDiff %f #channel,  meanTime %f #ps, TOFmean%f  vertex %f cm meanVertex %f  \n",timeDiff, meanTime,timeclock, vertex,meanVertex));
555        frecpoints.SetT0clock(timeclock);
556        frecpoints.SetVertex(vertex);
557        frecpoints.SetMeanTime(meanTime);
558        frecpoints.SetOnlineMean(Int_t(onlineMean));
559       
560       // Set triggers
561       Bool_t tr[5];
562       Int_t trchan[5] = {50,51,52,55,56};
563       Float_t lowtr[5] = {meanTVDC-700, meanOrA-700, meanOrC-700, meanOrC-1000, meanOrC-1000 };
564       Float_t hightr[5] = {meanTVDC+700, meanOrA+700, meanOrC+700, meanOrC+1000, meanOrC+1000};
565       
566       for (Int_t i=0; i<5; i++) tr[i] = false; 
567       for (Int_t itr=0; itr<5; itr++) {
568         for (Int_t iHit=0; iHit<1; iHit++) 
569           {
570             Int_t trr=trchan[itr];
571             if( allData[trr][iHit] > lowtr[itr] && allData[trr][iHit] < hightr[itr])  tr[itr]=true;
572             
573             AliDebug(15,Form("Reconstruct :::  T0 triggers iHit %i tvdc %d orA %d orC %d centr %d semicentral %d",iHit, tr[0],tr[1],tr[2],tr[3],tr[4]));
574           }       
575       }
576       frecpoints.SetT0Trig(tr);
577       
578       // all times with amplitude correction 
579       Float_t timecent;
580       for (Int_t iHit=0; iHit<5; iHit++) 
581         {
582           timefull = timecent = -9999; 
583           tvdc = ora = orc = -9999;
584           if(allData[50][iHit]>0) 
585             tvdc = (Float_t(allData[50][iHit]) - meanTVDC) * channelWidth* 0.001; 
586           if(allData[51][iHit]>0)
587             ora = (Float_t(allData[51][iHit]) - meanOrA) * channelWidth* 0.001;
588           
589           if(allData[52][iHit]>0) 
590             orc = (Float_t(allData[52][iHit]) - meanOrC) * channelWidth* 0.001;
591           
592           frecpoints.SetOrC( iHit, orc);
593           frecpoints.SetOrA( iHit, ora);
594           frecpoints.SetTVDC( iHit, tvdc);
595           for (Int_t i0=0; i0<12; i0++) {
596             if (equalize  ==0 )  
597               timecent = fTime0vertex[i0] + timeDelayCFD[i0];
598             else
599               timecent = fTime0vertex[i0];
600             timefull = -9999; 
601             if(allData[i0+1][iHit]>1) 
602               timefull = (Float_t(allData[i0+1][iHit]) - timecent)* channelWidth* 0.001;
603             frecpoints.SetTimeFull(i0, iHit,timefull) ;
604             //      if(allData[i0+1][iHit]>1)  printf("i0 %d iHit %d data %d fTime0vertex %f timefull %f \n",i0, iHit, allData[i0+1][iHit], fTime0vertex[i0], timefull);
605             
606           }
607           
608           for (Int_t i0=12; i0<24; i0++) {
609             timefull = -9999; 
610             if (equalize  ==0 )  
611               timecent = fTime0vertex[i0] + timeDelayCFD[i0];
612             else
613               timecent = fTime0vertex[i0];
614             if(allData[i0+45][iHit]>1) {
615               timefull = (Float_t(allData[i0+45][iHit]) - timecent)* channelWidth* 0.001;
616             }
617             //  if(allData[i0+45][iHit]>1)  printf("i0 %d iHit %d data %d fTime0vertex %f timefull %f \n",i0, iHit, allData[i0+45][iHit], fTime0vertex[i0], timefull);
618             frecpoints.SetTimeFull(i0, iHit, timefull) ;
619           }
620         }
621       
622       
623       //Set MPD
624       if(allData[53][0]>0 && allData[54][0]) 
625         frecpoints.SetMultA(allData[53][0]-allData[54][0]);
626       if(allData[105][0]>0 && allData[106][0]) 
627         frecpoints.SetMultC(allData[105][0]-allData[106][0]);
628       
629       
630     } // if (else )raw data
631   recTree->Fill();
632 }
633   
634   
635 //____________________________________________________________
636   
637   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
638   {
639
640   /***************************************************
641   Resonstruct digits to vertex position
642   ****************************************************/
643   
644   AliDebug(1,Form("Start FillESD T0"));
645   if(!pESD) {
646     AliError("No ESD Event");
647     return;
648   }
649   pESD ->SetT0spread(fTimeSigmaShift);
650  
651
652   Float_t channelWidth = fParam->GetChannelWidth() ;  
653   Float_t c = 0.0299792458; // cm/ps
654   Float_t currentVertex=0, shift=0;
655   Int_t ncont=-1;
656   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
657   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
658   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
659   if (!vertex)        vertex = pESD->GetVertex();
660
661   if (vertex) {
662     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
663                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
664     currentVertex = vertex->GetZ();
665     
666     ncont = vertex->GetNContributors();
667     if(ncont>0 ) {
668       shift = currentVertex/c;
669     }
670   }
671   TTree *treeR = clustersTree;
672   
673   AliT0RecPoint frecpoints;
674   AliT0RecPoint * pfrecpoints = &frecpoints;
675   
676   AliDebug(1,Form("Start FillESD T0"));
677   TBranch *brRec = treeR->GetBranch("T0");
678   if (brRec) {
679     brRec->SetAddress(&pfrecpoints);
680   }else{
681     AliError(Form("EXEC Branch T0 rec not found"));
682     return;
683   } 
684   
685   brRec->GetEntry(0);
686   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
687   Double32_t* tcorr;
688   for(Int_t i=0; i<24; i++) 
689     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
690
691   //1st time 
692   Double32_t timeClock[3];
693   Double32_t zPosition = frecpoints.GetVertex();
694
695   timeClock[0] = frecpoints.GetT0clock() ;
696   timeClock[1] = frecpoints.Get1stTimeA() + shift;
697   timeClock[2] = frecpoints.Get1stTimeC() - shift;
698   //best time
699   Double32_t timemean[3];
700   timemean[0] = frecpoints.GetMeanTime();
701   timemean[1] = frecpoints.GetBestTimeA() + shift;
702   timemean[2] = frecpoints.GetBestTimeC() - shift;
703
704   for(Int_t i=0; i<3; i++) {
705     fESDTZERO->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
706     fESDTZERO->SetT0TOFbest(i,timemean[i]);   // interaction time (ns) 
707   }
708   for ( Int_t i=0; i<24; i++) {
709     time[i] =  frecpoints.GetTime(i); // ps to ns
710     if ( time[i] != 0 && time[i]>-9999) {
711       ampQTC[i] = frecpoints.GetAmp(i);
712       amp[i] = frecpoints.AmpLED(i);
713       AliDebug(1,Form("T0: %i  time %f  ampQTC %f ampLED %f \n", i, time[i], ampQTC[i], amp[i]));
714    }
715   }
716   fESDTZERO->SetT0time(time);         // best TOF on each PMT 
717   fESDTZERO->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
718   Int_t trig= frecpoints.GetT0Trig();
719   frecpoints.PrintTriggerSignals( trig);
720   //  printf(" !!!!! FillESD trigger %i \n",trig);
721   fESDTZERO->SetT0Trig(trig);
722   fESDTZERO->SetT0zVertex(zPosition); //vertex Z position 
723
724   Double32_t multA=frecpoints.GetMultA();
725   Double32_t multC=frecpoints.GetMultC();
726   fESDTZERO->SetMultA(multA); // for backward compatubility
727   fESDTZERO->SetMultC(multC); // for backward compatubility
728
729
730   for (Int_t iHit =0; iHit<5; iHit++ ) {
731        AliDebug(10,Form("FillESD ::: iHit %i tvdc %f orA %f orC %f\n", iHit,
732            frecpoints.GetTVDC(iHit),
733            frecpoints.GetOrA(iHit),
734                        frecpoints.GetOrC(iHit) ));
735     fESDTZERO->SetTVDC(iHit,frecpoints.GetTVDC(iHit));
736     fESDTZERO->SetOrA(iHit,frecpoints.GetOrA(iHit));
737     fESDTZERO->SetOrC(iHit,frecpoints.GetOrC(iHit));
738     
739     for (Int_t i0=0; i0<24; i0++) {
740       //  if(frecpoints.GetTimeFull(i0,iHit)>0){
741       //        printf("FillESD ::: iHit %i cfd %i time %f \n", iHit, i0, frecpoints.GetTimeFull(i0,iHit));
742         fESDTZERO->SetTimeFull(i0, iHit,frecpoints.GetTimeFull(i0,iHit));
743         // }
744         
745     }                
746   }
747   
748   AliDebug(1,Form("T0: SPDshift %f Vertex %f (T0A+T0C)/2 best %f #ps T0signal %f ps OrA %f ps OrC %f ps T0trig %i\n",shift, zPosition, timemean[0], timeClock[0], timeClock[1], timeClock[2], trig));
749
750   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
751   // background flags
752   Bool_t background = BackgroundFlag();
753   fESDTZERO->SetBackgroundFlag(background);
754   Bool_t pileup =  PileupFlag();
755   fESDTZERO->SetPileupFlag(pileup);
756   for (Int_t i=0; i<5; i++) {
757     fESDTZERO->SetPileupTime(i, frecpoints.GetTVDC(i) ) ;
758     //   printf("!!!!!! FillESD :: pileup %i %f %f \n", i,fESDTZERO->GetPileupTime(i), frecpoints.GetTVDC(i));
759   }
760   Bool_t sat  = SatelliteFlag();
761   fESDTZERO->SetSatelliteFlag(sat);
762   
763   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
764   if (pESD) {
765     
766     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
767     if (fr) {
768       AliDebug(10, Form("Writing TZERO friend data to ESD tree"));
769
770       //     if (ncont>2) {
771         tcorr = fESDTZEROfriend->GetT0timeCorr();
772         for ( Int_t i=0; i<24; i++) {
773           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
774           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
775           if(time[i]>1)   AliDebug(10,Form("T0 friend : %i time %f  ampQTC %f ampLED %f \n", i, timecorr[i], ampQTC[i], amp[i]));
776         }
777         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
778         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
779         fESDTZEROfriend->SetT0ampQTC(ampQTC);
780         fr->SetTZEROfriend(fESDTZEROfriend);
781         //      }//
782     }
783
784     pESD->SetTZEROData(fESDTZERO);
785   }
786
787 } // vertex in 3 sigma
788
789 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
790  //____________________________________________________________
791   
792 Bool_t AliT0Reconstructor::PileupFlag() const
793 {
794   //
795   Bool_t pileup = false;
796   Float_t tvdc[5];
797   for (Int_t ih=0; ih<5; ih++) 
798     {
799       tvdc[ih] =  fESDTZERO->GetTVDC(ih);
800       
801      if(  tvdc[0] !=0 &&  tvdc[0]> -10 && tvdc[0]< 10 )
802        if(ih>0 && tvdc[ih]>20 )         pileup = true; 
803      if( tvdc[0] >20 || (tvdc[0] < -20  &&  tvdc[0] > -9000) ) pileup =true;
804      //    if (pileup) printf(" !!!!! pile up %i  tvdc %f \n",ih,  tvdc[ih]); 
805     }
806
807
808   return pileup;
809
810 }
811
812  //____________________________________________________________
813   
814 Bool_t AliT0Reconstructor::BackgroundFlag() const
815 {
816   Bool_t background = false;
817
818   Float_t orA = fESDTZERO->GetOrA(0);
819   Float_t orC = fESDTZERO->GetOrC(0);
820   Float_t tvdc =  fESDTZERO->GetTVDC(0);
821
822   if ( (orA > -5 && orA <5) && (orC > -5 && orC <5) && (tvdc < -5 || tvdc > 5)) {
823     background = true;
824     //   printf(" orA %f orC %f tvdc %f\n", orA, orC, tvdc);
825   } 
826   return background;
827
828
829 }
830
831
832  //____________________________________________________________
833   
834 Bool_t  AliT0Reconstructor::SatelliteFlag() const
835 {
836
837  Float_t satelliteLow = GetRecoParam() -> GetLowSatelliteThreshold();
838   Float_t satelliteHigh = GetRecoParam() -> GetHighSatelliteThreshold();
839   Bool_t satellite = false;
840   for (Int_t i0=0; i0<24; i0++) {
841     Float_t timefull =  fESDTZERO -> GetTimeFull(i0,0);
842     if( timefull > satelliteLow && timefull < satelliteHigh)  satellite=true;
843   }
844         
845   return satellite;
846
847 }