new option for tests in alifakedisplay
[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] != -99999
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] != -99999
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 badLEDminCFD = GetRecoParam() -> GetHigh(350);
333   Int_t equalize = GetRecoParam() -> GetEq();
334   AliDebug(10,Form( "AliT0Reconstructor::Reconstruct::: RecoParam %i LEDminCFD threshold %i \n",equalize, badLEDminCFD) );
335   fCalib->SetEq(equalize);
336   Int_t low[500], high[500];
337   Float_t timefull=-99999;;
338   Float_t tvdc  = -99999; Float_t ora = -99999; Float_t orc = -99999;
339   
340   Int_t allData[110][5];
341   
342   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
343   Float_t time2zero[24]; 
344   Double32_t timeDiff, meanTime, timeclock;
345   timeDiff =  meanTime = timeclock = 9999999;
346   Float_t c = 29.9792458; // cm/ns
347   Double32_t vertex = 9999999;
348   Int_t onlineMean=0;
349   Float_t meanVertex = 0;
350   Int_t pedestal[24];
351   for (Int_t i0=0; i0<24; i0++) {
352     low[i0] = Int_t(fTime0vertex[i0]) - corridor;
353     high[i0] = Int_t(fTime0vertex[i0]) + corridor;
354     time2zero[i0] = 99999;
355     pedestal[i0]=Int_t (GetRecoParam()->GetLow(100+i0) );
356   }
357   
358   for (Int_t i0=0; i0<110; i0++)
359     for (Int_t j0=0; j0<5; j0++)  allData[i0][j0]=0; 
360   
361   Float_t lowAmpThreshold =  GetRecoParam()->GetAmpLowThreshold();  
362   Float_t highAmpThreshold =  GetRecoParam()->GetAmpHighThreshold(); 
363   
364   Double32_t besttimeA=9999999;  Double32_t besttimeA_best=9999999;
365   Double32_t besttimeC=9999999;  Double32_t besttimeC_best=9999999;
366
367    Float_t channelWidth = fParam->GetChannelWidth() ;  
368         
369   AliT0RecPoint frecpoints;
370   AliT0RecPoint * pfrecpoints = &frecpoints;
371   
372   recTree->Branch( "T0", "AliT0RecPoint" ,&pfrecpoints);
373    
374   AliDebug(10," before read data ");
375   AliT0RawReader myrawreader(rawReader);
376
377   UInt_t type =rawReader->GetType();
378
379   if (!myrawreader.Next())
380     AliDebug(1,Form(" no raw data found!!"));
381   else
382     {  
383    for (Int_t i=0; i<24; i++)
384     {
385       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
386     }
387   
388       if(type == 7  ) {  //only physics 
389         for (Int_t i=0; i<107; i++) {
390         for (Int_t iHit=0; iHit<5; iHit++) 
391           {
392             allData[i][iHit] = myrawreader.GetData(i,iHit);
393           }
394         }
395         
396         Int_t fBCID=Int_t (rawReader->GetBCID());
397         Int_t trmbunch= myrawreader.GetTRMBunchID();
398         AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
399         if( (trmbunch-fBCID)!=37  ) {
400           AliDebug(0,Form("wrong :::: CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
401           //      type = -1;
402         }
403         for (Int_t in=0; in<12; in++)  
404           {
405             for (Int_t iHit=0; iHit<5; iHit++) 
406               {
407                 if(allData[in+1][iHit] > low[in] && 
408                    allData[in+1][iHit] < high[in])
409                   {
410                     timeCFD[in] = allData[in+1][iHit] ; 
411                     break;
412                   }
413               }
414             for (Int_t iHit=0; iHit<5; iHit++) 
415               {
416                 if(allData[in+1+56][iHit] > low[in+12] && 
417                    allData[in+1+56][iHit] < high[in+12])
418                   {
419                     timeCFD[in+12] = allData[in+56+1][iHit] ;
420                     break;
421                   }
422               }
423             timeLED[in+12] = allData[in+68+1][0] ;
424             timeLED[in] = allData[in+12+1][0] ;
425             AliDebug(50, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
426                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
427                              timeLED[in+12]));   
428             
429           }
430         
431         for (Int_t in=0; in<12;  in++)
432           {
433             for (Int_t iHit=0; iHit<5; iHit++) 
434               {
435                 if (allData[2*in+26][iHit] > fTime0vertex[0]+2000 &&  
436                     allData[2*in+26][iHit] <fTime0vertex[0]+3000 ) {
437                     chargeQT1[in]=allData[2*in+26][0];
438                     break;
439                 }
440               }
441
442             for (Int_t iHit=0; iHit<5; iHit++) 
443               {
444                 if( (allData[2*in+25][iHit] -  chargeQT1[in])>800 
445                     &&  chargeQT1[in]>0)                
446                   {
447                     chargeQT0[in]=allData[2*in+25][0];
448                     AliDebug(25, Form(" readed Raw %i %i %i",
449                                       in, chargeQT0[in],chargeQT1[in]));
450                     break;
451                   }
452                 }
453           }     
454         for (Int_t in=12; in<24;  in++)
455           {
456             for (Int_t iHit=0; iHit<5; iHit++) 
457               {
458                 if (allData[2*in+58][iHit] > fTime0vertex[0]+2000 &&  
459                     allData[2*in+58][iHit] <fTime0vertex[0]+3000 )
460                   {
461                     chargeQT1[in]=allData[2*in+58][0];
462                     break;
463                   }
464               }
465             for (Int_t iHit=0; iHit<5; iHit++) 
466               {
467                 if( (allData[2*in+57][iHit] -  chargeQT1[in])>800 && 
468                     chargeQT1[in]>0 )
469                   {
470                     chargeQT0[in]=allData[2*in+57][0];
471                     AliDebug(25, Form(" readed Raw %i %i %i",
472                                       in, chargeQT0[in],chargeQT1[in]));
473                     break;
474                   }
475               }
476           }
477         
478         onlineMean = allData[49][0];
479         
480         Double32_t time[24], adc[24], adcmip[24], noncalibtime[24];
481         for (Int_t ipmt=0; ipmt<24; ipmt++) {
482           if(timeCFD[ipmt] >  0 && (chargeQT0[ipmt] - chargeQT1[ipmt])> 0 &&
483           (timeLED[ipmt]-timeCFD[ipmt])<badLEDminCFD){
484            //for simulated data
485              //for physics  data
486            adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
487            Int_t refAmp = Int_t (fTime0vertex[ipmt]);
488            time[ipmt] = fCalib-> WalkCorrection( refAmp, ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
489            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
490            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
491            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
492                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
493            Double_t ampMip = 0;
494            TGraph * ampGraph =  (TGraph*)fAmpLED.At(ipmt);
495            if (ampGraph) ampMip = ampGraph->Eval(sl);
496            Double_t qtMip = 0;
497            TGraph * qtGraph = (TGraph*)fQTC.At(ipmt);
498            if (qtGraph) qtMip = qtGraph->Eval(adc[ipmt]);
499            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %f\n ",ampMip,qtMip, adc[ipmt]));
500            if( equalize  ==0 ) 
501              frecpoints.SetTime(ipmt, Float_t(time[ipmt]) );
502            else 
503              frecpoints.SetTime(ipmt, Float_t(time[ipmt] + fTime0vertex[ipmt]) );
504            // frecpoints.SetTime(ipmt, Float_t(time[ipmt] ) );
505            if(qtMip<0) qtMip=0;
506            frecpoints.SetAmp(ipmt, Double32_t( qtMip)); 
507            adcmip[ipmt]=qtMip;
508            frecpoints.SetAmpLED(ipmt, Double32_t(ampMip));           
509            noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
510           }
511           else {
512            time[ipmt] = -9999;
513            adc[ipmt] = 0;
514            adcmip[ipmt] = 0;
515            noncalibtime[ipmt] = -9999;
516           }
517        }
518        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ; 
519     
520        for (Int_t ipmt=0; ipmt<12; ipmt++){
521          if(time[ipmt] !=0 &&  time[ipmt] > -9000 
522             /*&& badpmt[ipmt]==0 */
523             &&  adcmip[ipmt]>lowAmpThreshold )
524            {
525              if(time[ipmt]<besttimeC) besttimeC=time[ipmt]; //timeC
526              if(TMath::Abs(time[ipmt])<TMath::Abs(besttimeC_best)) 
527                besttimeC_best=time[ipmt]; //timeC            
528            }
529        }
530        for ( Int_t ipmt=12; ipmt<24; ipmt++)
531          {
532            if(time[ipmt] != 0 &&  time[ipmt] > -9000 
533               /* && badpmt[ipmt]==0*/ 
534               && adcmip[ipmt]>lowAmpThreshold )
535              {
536                if(time[ipmt]<besttimeA) besttimeA=time[ipmt]; 
537                if(TMath::Abs(time[ipmt] ) < TMath::Abs(besttimeA_best)) 
538                  besttimeA_best=time[ipmt]; //timeA
539              }
540          }
541        
542        if(besttimeA < 999999 && besttimeA!=0 ) {
543          if( equalize  ==0 ) 
544            frecpoints.SetTime1stA((besttimeA * channelWidth)- 1000.*fLatencyHPTDC + 1000.*fLatencyL1A - 1000.*fGRPdelays - fTimeMeanShift[1] ); 
545          else
546            {
547              frecpoints.SetTimeBestA((besttimeA_best * channelWidth )); 
548              frecpoints.SetTime1stA((besttimeA * channelWidth - fTimeMeanShift[1])); 
549            }
550        }
551        if( besttimeC < 999999 && besttimeC!=0) {
552          if( equalize  ==0 ) 
553            frecpoints.SetTime1stC((besttimeC * channelWidth)- 1000.*fLatencyHPTDC +1000.*fLatencyL1C - 1000.*fGRPdelays - fTimeMeanShift[2]);
554          else
555            {
556              frecpoints.SetTimeBestC((besttimeC_best * channelWidth ));
557              frecpoints.SetTime1stC((besttimeC * channelWidth - fTimeMeanShift[2]));
558            }
559        }
560        AliDebug(5,Form("1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f ",
561                        besttimeA, besttimeA_best,
562                        besttimeC, besttimeC_best) );
563        AliDebug(5,Form("fRecPoints:::  1stimeA %f , besttimeA %f 1sttimeC %f besttimeC %f shiftA %f shiftC %f ",
564                        frecpoints.Get1stTimeA(),  frecpoints.GetBestTimeA(),
565                        frecpoints.Get1stTimeC(),  frecpoints.GetBestTimeC(), 
566                        fTimeMeanShift[1],fTimeMeanShift[2] ) );
567        if( besttimeC < 999999 &&  besttimeA < 999999) { 
568          if(equalize  ==0 )
569            timeclock = (channelWidth*(besttimeC + besttimeA)/2.- 1000.*fLatencyHPTDC +1000.*fLatencyL1 - 1000.*fGRPdelays - fTimeMeanShift[0]);
570          else
571            {
572              timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - fTimeMeanShift[0];
573              meanTime = channelWidth * Float_t(besttimeA_best + besttimeC_best )/2.;
574            }
575          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth ;
576          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
577        }
578        
579       }  //if phys event       
580        AliDebug(1,Form("  timeDiff %f #channel,  meanTime %f #ps, TOFmean%f  vertex %f cm meanVertex %f  \n",timeDiff, meanTime,timeclock, vertex,meanVertex));
581        frecpoints.SetT0clock(timeclock);
582        frecpoints.SetVertex(vertex);
583        frecpoints.SetMeanTime(meanTime);
584        frecpoints.SetOnlineMean(Int_t(onlineMean));
585       
586       // Set triggers
587       Bool_t tr[5];
588       Int_t trchan[5] = {50,51,52,55,56};
589       Float_t lowtr[5] = {meanTVDC-700, meanOrA-700, meanOrC-700, meanOrC-1000, meanOrC-1000 };
590       Float_t hightr[5] = {meanTVDC+700, meanOrA+700, meanOrC+700, meanOrC+1000, meanOrC+1000};
591       
592       for (Int_t i=0; i<5; i++) tr[i] = false; 
593       for (Int_t itr=0; itr<5; itr++) {
594         for (Int_t iHit=0; iHit<1; iHit++) 
595           {
596             Int_t trr=trchan[itr];
597             if( allData[trr][iHit] > lowtr[itr] && allData[trr][iHit] < hightr[itr])  tr[itr]=true;
598             
599             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]));
600           }       
601       }
602       frecpoints.SetT0Trig(tr);
603       
604       // all times with amplitude correction 
605       Float_t timecent;
606       for (Int_t iHit=0; iHit<5; iHit++) 
607         {
608           timefull = timecent = -9999; 
609           tvdc = ora = orc = -9999;
610           if(allData[50][iHit]>0) 
611             tvdc = (Float_t(allData[50][iHit]) - meanTVDC) * channelWidth* 0.001; 
612           if(allData[51][iHit]>0)
613             ora = (Float_t(allData[51][iHit]) - meanOrA) * channelWidth* 0.001;
614           
615           if(allData[52][iHit]>0) 
616             orc = (Float_t(allData[52][iHit]) - meanOrC) * channelWidth* 0.001;
617           
618           frecpoints.SetOrC( iHit, orc);
619           frecpoints.SetOrA( iHit, ora);
620           frecpoints.SetTVDC( iHit, tvdc);
621           for (Int_t i0=0; i0<12; i0++) {
622             if (equalize  ==0 )  
623               timecent = fTime0vertex[i0] + timeDelayCFD[i0];
624             else
625               timecent = fTime0vertex[i0];
626             timefull = -9999; 
627             if(allData[i0+1][iHit]>1) 
628               timefull = (Float_t(allData[i0+1][iHit]) - timecent)* channelWidth* 0.001;
629             frecpoints.SetTimeFull(i0, iHit,timefull) ;
630             //      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);
631             
632           }
633           
634           for (Int_t i0=12; i0<24; i0++) {
635             timefull = -9999; 
636             if (equalize  ==0 )  
637               timecent = fTime0vertex[i0] + timeDelayCFD[i0];
638             else
639               timecent = fTime0vertex[i0];
640             if(allData[i0+45][iHit]>1) {
641               timefull = (Float_t(allData[i0+45][iHit]) - timecent)* channelWidth* 0.001;
642             }
643             //  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);
644             frecpoints.SetTimeFull(i0, iHit, timefull) ;
645           }
646         }
647       
648       
649       //Set MPD
650       Float_t mpda_start=0, mpdc_start=0;
651       for (Int_t iHit=0; iHit<5; iHit++) 
652         {
653           if (allData[54][iHit] > fTime0vertex[0]+2000 &&  
654               allData[54][iHit] <fTime0vertex[0]+3000 ) {
655             mpda_start=Float_t(allData[54][iHit]);
656             AliDebug(15,Form("Reconstruct :::  T0 MPD iHit %i MPDA start %f",iHit, mpda_start ));
657
658             break;
659           }
660         }
661       for (Int_t iHit=0; iHit<5; iHit++) 
662         {
663           if(allData[53][iHit] > fTime0vertex[0]+3000 && 
664              (allData[53][iHit]-mpda_start)>800 && mpda_start>0 && tr[1]) {
665             frecpoints.SetMultA(Float_t(allData[53][iHit] - mpda_start) );
666             AliDebug(15,Form("Reconstruct :::  T0 MPD iHit %i MPDA stop %i MPD %f",iHit, allData[53][iHit],Float_t(allData[53][iHit] - mpda_start) ));
667             break;
668           }
669         }
670       for (Int_t iHit=0; iHit<5; iHit++) 
671         {
672           if (allData[106][iHit] > fTime0vertex[0]+2000 &&  
673               allData[106][iHit] <fTime0vertex[0]+3000 ) {
674             mpdc_start=allData[54][iHit];
675             AliDebug(15,Form("Reconstruct :::  T0 MPD iHit %i MPDC start %f ",iHit, mpdc_start));
676             break;
677           }
678         }
679       for (Int_t iHit=0; iHit<5; iHit++) 
680         {
681           if(allData[105][iHit] > fTime0vertex[0]+3000 && 
682              (allData[105][iHit]-mpdc_start)>800 && mpdc_start>0 && tr[2]) {
683             frecpoints.SetMultC(Float_t(allData[105][iHit] - mpdc_start) );
684             AliDebug(15,Form("Reconstruct :::  T0 MPD iHit %i MPDC stop %i MPD %f",iHit, allData[105][iHit],Float_t(allData[105][iHit] - mpdc_start) ));
685             break;
686           }
687         }
688     } // if (else )raw data
689   recTree->Fill();
690 }
691   
692   
693 //____________________________________________________________
694   
695   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
696   {
697
698   /***************************************************
699   Resonstruct digits to vertex position
700   ****************************************************/
701   
702   AliDebug(1,Form("Start FillESD T0"));
703   if(!pESD) {
704     AliError("No ESD Event");
705     return;
706   }
707   pESD ->SetT0spread(fTimeSigmaShift);
708  
709
710   Float_t channelWidth = fParam->GetChannelWidth() ;  
711   Float_t c = 0.0299792458; // cm/ps
712   Float_t currentVertex=0, shift=0;
713   Int_t ncont=-1;
714   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
715   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
716   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
717   if (!vertex)        vertex = pESD->GetVertex();
718
719   if (vertex) {
720     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
721                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
722     currentVertex = vertex->GetZ();
723     
724     ncont = vertex->GetNContributors();
725     if(ncont>0 ) {
726       shift = currentVertex/c;
727     }
728   }
729   TTree *treeR = clustersTree;
730   
731   AliT0RecPoint frecpoints;
732   AliT0RecPoint * pfrecpoints = &frecpoints;
733   
734   AliDebug(1,Form("Start FillESD T0"));
735   TBranch *brRec = treeR->GetBranch("T0");
736   if (brRec) {
737     brRec->SetAddress(&pfrecpoints);
738   }else{
739     AliError(Form("EXEC Branch T0 rec not found"));
740     return;
741   } 
742   
743   brRec->GetEntry(0);
744   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
745   Double32_t* tcorr;
746   for(Int_t i=0; i<24; i++) 
747     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
748
749   //1st time 
750   Double32_t timeClock[3];
751   Double32_t zPosition = frecpoints.GetVertex();
752
753   timeClock[0] = frecpoints.GetT0clock() ;
754   timeClock[1] = frecpoints.Get1stTimeA() + shift;
755   timeClock[2] = frecpoints.Get1stTimeC() - shift;
756   //best time
757   Double32_t timemean[3];
758   timemean[0] = frecpoints.GetMeanTime();
759   timemean[1] = frecpoints.GetBestTimeA() + shift;
760   timemean[2] = frecpoints.GetBestTimeC() - shift;
761
762   for(Int_t i=0; i<3; i++) {
763     fESDTZERO->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
764     fESDTZERO->SetT0TOFbest(i,timemean[i]);   // interaction time (ns) 
765   }
766   for ( Int_t i=0; i<24; i++) {
767     time[i] =  frecpoints.GetTime(i); // ps to ns
768     if ( time[i] != 0 && time[i]>-9999) {
769       ampQTC[i] = frecpoints.GetAmp(i);
770       amp[i] = frecpoints.AmpLED(i);
771       AliDebug(1,Form("T0: %i  time %f  ampQTC %f ampLED %f \n", i, time[i], ampQTC[i], amp[i]));
772    }
773   }
774   fESDTZERO->SetT0time(time);         // best TOF on each PMT 
775   fESDTZERO->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
776   Int_t trig= frecpoints.GetT0Trig();
777   frecpoints.PrintTriggerSignals( trig);
778   //  printf(" !!!!! FillESD trigger %i \n",trig);
779   fESDTZERO->SetT0Trig(trig);
780   fESDTZERO->SetT0zVertex(zPosition); //vertex Z position 
781
782   Double32_t multA=frecpoints.GetMultA();
783   Double32_t multC=frecpoints.GetMultC();
784   fESDTZERO->SetMultA(multA); // for backward compatubility
785   fESDTZERO->SetMultC(multC); // for backward compatubility
786
787
788   for (Int_t iHit =0; iHit<5; iHit++ ) {
789        AliDebug(10,Form("FillESD ::: iHit %i tvdc %f orA %f orC %f\n", iHit,
790            frecpoints.GetTVDC(iHit),
791            frecpoints.GetOrA(iHit),
792                        frecpoints.GetOrC(iHit) ));
793     fESDTZERO->SetTVDC(iHit,frecpoints.GetTVDC(iHit));
794     fESDTZERO->SetOrA(iHit,frecpoints.GetOrA(iHit));
795     fESDTZERO->SetOrC(iHit,frecpoints.GetOrC(iHit));
796     
797     for (Int_t i0=0; i0<24; i0++) {
798       //  if(frecpoints.GetTimeFull(i0,iHit)>0){
799       //        printf("FillESD ::: iHit %i cfd %i time %f \n", iHit, i0, frecpoints.GetTimeFull(i0,iHit));
800         fESDTZERO->SetTimeFull(i0, iHit,frecpoints.GetTimeFull(i0,iHit));
801         // }
802         
803     }                
804   }
805   
806   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));
807
808   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
809   // background flags
810   Bool_t background = BackgroundFlag();
811   fESDTZERO->SetBackgroundFlag(background);
812   Bool_t pileup =  PileupFlag();
813   fESDTZERO->SetPileupFlag(pileup);
814   for (Int_t i=0; i<5; i++) {
815     fESDTZERO->SetPileupTime(i, frecpoints.GetTVDC(i) ) ;
816     //   printf("!!!!!! FillESD :: pileup %i %f %f \n", i,fESDTZERO->GetPileupTime(i), frecpoints.GetTVDC(i));
817   }
818   Bool_t sat  = SatelliteFlag();
819   fESDTZERO->SetSatelliteFlag(sat);
820   
821   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
822   if (pESD) {
823     
824     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
825     if (fr) {
826       AliDebug(10, Form("Writing TZERO friend data to ESD tree"));
827
828       //     if (ncont>2) {
829         tcorr = fESDTZEROfriend->GetT0timeCorr();
830         for ( Int_t i=0; i<24; i++) {
831           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
832           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
833           if(time[i]>1)   AliDebug(10,Form("T0 friend : %i time %f  ampQTC %f ampLED %f \n", i, timecorr[i], ampQTC[i], amp[i]));
834         }
835         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
836         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
837         fESDTZEROfriend->SetT0ampQTC(ampQTC);
838         fr->SetTZEROfriend(fESDTZEROfriend);
839         //      }//
840     }
841
842     pESD->SetTZEROData(fESDTZERO);
843   }
844
845 } // vertex in 3 sigma
846
847 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
848  //____________________________________________________________
849   
850 Bool_t AliT0Reconstructor::PileupFlag() const
851 {
852   //
853   Bool_t pileup = false;
854   Float_t tvdc[5];
855   for (Int_t ih=0; ih<5; ih++) 
856     {
857       tvdc[ih] =  fESDTZERO->GetTVDC(ih);
858       
859      if(  tvdc[0] !=0 &&  tvdc[0]> -10 && tvdc[0]< 10 )
860        if(ih>0 && tvdc[ih]>20 )         pileup = true; 
861      if( tvdc[0] >20 || (tvdc[0] < -20  &&  tvdc[0] > -9000) ) pileup =true;
862      //    if (pileup) printf(" !!!!! pile up %i  tvdc %f \n",ih,  tvdc[ih]); 
863     }
864
865
866   return pileup;
867
868 }
869
870  //____________________________________________________________
871   
872 Bool_t AliT0Reconstructor::BackgroundFlag() const
873 {
874  
875   Bool_t background = false;
876   /*  
877       Float_t orA = fESDTZERO->GetOrA(0);
878       Float_t orC = fESDTZERO->GetOrC(0);
879       Float_t tvdc =  fESDTZERO->GetTVDC(ih);
880       
881       if ( (orA > -5 && orA <5) && (orC > -5 && orC <5) && (tvdc < -5 || tvdc > 5)) {
882       background = true;
883       //   printf(" orA %f orC %f tvdc %f\n", orA, orC, tvdc);
884       } 
885   */ 
886   return background;
887
888
889 }
890
891
892  //____________________________________________________________
893   
894 Bool_t  AliT0Reconstructor::SatelliteFlag() const
895 {
896
897  Float_t satelliteLow = GetRecoParam() -> GetLowSatelliteThreshold();
898   Float_t satelliteHigh = GetRecoParam() -> GetHighSatelliteThreshold();
899   Bool_t satellite = false;
900   for (Int_t i0=0; i0<24; i0++) {
901     Float_t timefull =  fESDTZERO -> GetTimeFull(i0,0);
902     if( timefull > satelliteLow && timefull < satelliteHigh)  satellite=true;
903   }
904         
905   return satellite;
906
907 }