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