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