Fixes for #73467: Corrupted MUON hits when using AliRoot with Root v5.27/05 and v5...
[u/mrichter/AliRoot.git] / T0 / AliT0Reconstructor.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18 /*********************************************************************
19  *  T0 reconstruction and filling ESD
20  *  - reconstruct mean time (interation time) 
21  *  - vertex position
22  *  -  multiplicity
23  ********************************************************************/
24
25 #include <AliESDEvent.h>
26 #include "AliLog.h"
27 #include "AliT0RecPoint.h"
28 #include "AliRawReader.h"
29 #include "AliT0RawReader.h"
30 #include "AliT0digit.h"
31 #include "AliT0Reconstructor.h"
32 #include "AliT0Parameters.h"
33 #include "AliT0Calibrator.h"
34 #include "AliESDfriend.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
42 #include <TArrayI.h>
43 #include <TGraph.h>
44 #include <TMath.h>
45 #include <Riostream.h>
46
47 ClassImp(AliT0Reconstructor)
48
49   AliT0Reconstructor:: AliT0Reconstructor(): AliReconstructor(),
50                                              fdZonA(0),
51                                              fdZonC(0),
52                                              fZposition(0),
53                                              fParam(NULL),
54                                              fAmpLEDrec(),
55                                              fQTC(0),
56                                              fAmpLED(0),
57                                              fCalib(),
58                                              fLatencyHPTDC(9000),
59                                              fLatencyL1(0),
60                                              fLatencyL1A(0),
61                                              fLatencyL1C(0),
62                                              fGRPdelays(0),
63                                              fESDTZEROfriend(NULL)
64
65 {
66   //constructor
67   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
68   if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
69   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
70   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
71
72   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
73   if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
74   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
75   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
76  
77   AliCDBEntry *entry4 = AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
78   if (!entry4) AliFatal("LHC clock-phase shift is not found in OCDB !");
79   AliLHCClockPhase *phase = (AliLHCClockPhase*)entry4->GetObject();
80
81   fGRPdelays = l1Delay - phase->GetMeanPhase();
82
83   fParam = AliT0Parameters::Instance();
84   fParam->Init();
85  
86   for (Int_t i=0; i<24; i++){
87         TGraph* gr = fParam ->GetAmpLEDRec(i);
88         if (gr) fAmpLEDrec.AddAtAndExpand(gr,i) ; 
89           TGraph* gr1 = fParam ->GetAmpLED(i);
90           if (gr1) fAmpLED.AddAtAndExpand(gr1,i) ; 
91           TGraph* gr2 = fParam ->GetQTC(i);
92           if (gr2) fQTC.AddAtAndExpand(gr2,i) ;         
93   }
94
95   fLatencyL1 = fParam->GetLatencyL1();
96   fLatencyL1A = fParam->GetLatencyL1A();
97   fLatencyL1C = fParam->GetLatencyL1C();
98   fLatencyHPTDC = fParam->GetLatencyHPTDC();
99   AliDebug(2,Form(" LatencyL1 %f latencyL1A %f latencyL1C %f latencyHPTDC %f \n",fLatencyL1, fLatencyL1A, fLatencyL1C, fLatencyHPTDC));
100   
101   // fdZonC = TMath::Abs(fParam->GetZPositionShift("T0/C/PMT1"));
102   //fdZonA = TMath::Abs(fParam->GetZPositionShift("T0/A/PMT15"));
103   //here real Z position
104   fdZonC = TMath::Abs(fParam->GetZPosition("T0/C/PMT1"));
105   fdZonA = TMath::Abs(fParam->GetZPosition("T0/A/PMT15"));
106
107   fCalib = new AliT0Calibrator();
108   fESDTZEROfriend = new AliESDTZEROfriend();
109
110 }
111
112 //_____________________________________________________________________________
113 void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
114   
115 {
116   // T0 digits reconstruction
117   Int_t refAmp = Int_t (GetRecoParam()->GetRefAmp());
118   
119   TArrayI * timeCFD = new TArrayI(24); 
120   TArrayI * timeLED = new TArrayI(24); 
121   TArrayI * chargeQT0 = new TArrayI(24); 
122   TArrayI * chargeQT1 = new TArrayI(24); 
123
124  
125   Float_t channelWidth = fParam->GetChannelWidth() ;  
126   Float_t meanVertex = fParam->GetMeanVertex();
127   Float_t c = 0.0299792; // cm/ps
128   Double32_t vertex = 9999999;
129   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
130
131   
132   AliDebug(1,Form("Start DIGITS reconstruction "));
133   
134
135   TBranch *brDigits=digitsTree->GetBranch("T0");
136   AliT0digit *fDigits = new AliT0digit() ;
137   if (brDigits) {
138     brDigits->SetAddress(&fDigits);
139   }else{
140     AliError(Form("EXEC Branch T0 digits not found"));
141      return;
142   }
143   
144   digitsTree->GetEvent(0);
145   digitsTree->GetEntry(0);
146   brDigits->GetEntry(0);
147   fDigits->GetTimeCFD(*timeCFD);
148   fDigits->GetTimeLED(*timeLED);
149   fDigits->GetQT0(*chargeQT0);
150   fDigits->GetQT1(*chargeQT1);
151   Int_t onlineMean =  fDigits->MeanTime();
152
153   Bool_t tr[5];
154   for (Int_t i=0; i<5; i++) tr[i]=false; 
155   
156   Double32_t besttimeA=999999;
157   Double32_t besttimeC=999999;
158   Int_t pmtBestA=99999;
159   Int_t pmtBestC=99999;
160   
161   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
162   clustersTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
163   
164   Float_t time[24], adc[24];
165   for (Int_t ipmt=0; ipmt<24; ipmt++) {
166     if(timeCFD->At(ipmt)>0 ){
167      if(( chargeQT1->At(ipmt) - chargeQT0->At(ipmt))>0)  
168         adc[ipmt] = chargeQT1->At(ipmt) - chargeQT0->At(ipmt);
169       else
170         adc[ipmt] = 0;
171       
172      time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, Int_t(adc[ipmt]),  timeCFD->At(ipmt)) ;
173              
174       Double_t sl = Double_t(timeLED->At(ipmt) - timeCFD->At(ipmt));
175       //    time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl),  timeCFD->At(ipmt) ) ;
176       AliDebug(10,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
177                        ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
178
179       Double_t ampMip =((TGraph*)fAmpLED.At(ipmt))->Eval(sl);
180       Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
181       AliDebug(10,Form("  Amlitude in MIPS LED %f ,  QTC %f in channels %f\n ",ampMip,qtMip, adc[ipmt]));
182       
183       frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
184       frecpoints->SetAmpLED(ipmt, Float_t( ampMip)); //for cosmic &pp beam 
185       frecpoints->SetAmp(ipmt, Float_t(qtMip));
186       
187     }
188     else {
189       time[ipmt] = 0;
190       adc[ipmt] = 0;
191     }
192   }
193   
194   for (Int_t ipmt=0; ipmt<12; ipmt++){
195     if(time[ipmt] > 1 ) {
196       if(time[ipmt]<besttimeC){
197         besttimeC=time[ipmt]; //timeC
198         pmtBestC=ipmt;
199       }
200     }
201   }
202   for ( Int_t ipmt=12; ipmt<24; ipmt++){
203     if(time[ipmt] > 1) {
204       if(time[ipmt]<besttimeA) {
205         besttimeA=time[ipmt]; //timeA
206         pmtBestA=ipmt;}
207     }
208   }
209   if(besttimeA < 999999) {
210     frecpoints->SetTimeBestA(Int_t(besttimeA *channelWidth - fdZonA/c));
211     tr[1]=true;
212   }
213   if( besttimeC < 999999 ) {
214     frecpoints->SetTimeBestC(Int_t(besttimeC *channelWidth - fdZonA/c));
215     tr[2]=true;
216   }
217   AliDebug(10,Form(" besttimeA %f ch,  besttimeC %f ch",besttimeA, besttimeC));
218   if(besttimeA <999999 && besttimeC < 999999 ){
219     //    timeDiff = (besttimeC - besttimeA)*channelWidth;
220     timeDiff = (besttimeA - besttimeC)*channelWidth;
221     meanTime = (besttimeA + besttimeC)/2;// * channelWidth); 
222     timeclock = meanTime *channelWidth -fdZonA/c ;
223     vertex = meanVertex - c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2;
224     tr[0]=true; 
225   }
226   frecpoints->SetVertex(vertex);
227   frecpoints->SetMeanTime(meanTime);
228   frecpoints->SetT0clock(timeclock);
229   frecpoints->SetT0Trig(tr);
230
231   AliDebug(10,Form("T0 triggers %d %d %d %d %d",tr[0],tr[1],tr[2],tr[3],tr[4]));
232
233   //online mean
234   frecpoints->SetOnlineMean(Int_t(onlineMean));
235   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));
236   
237   
238
239    
240   
241   clustersTree->Fill();
242
243   delete timeCFD;
244   delete timeLED;
245   delete chargeQT0; 
246   delete chargeQT1; 
247 }
248
249
250 //_______________________________________________________________________
251
252 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
253 {
254   // T0 raw ->
255   //
256   // reference amplitude and time ref. point from reco param
257
258   Float_t refAmp = GetRecoParam()->GetRefAmp();
259   Int_t refPoint = 0;
260   //Bad channel
261   Int_t badpmt = GetRecoParam()->GetRefPoint();
262   Int_t low[110], high[110];
263
264   Int_t allData[110][5];
265   
266   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
267   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
268   Float_t c = 29.9792458; // cm/ns
269   Double32_t vertex = 9999999;
270   Int_t onlineMean=0;
271   // Float_t meanVertex = fParam->GetMeanVertex();
272   Float_t meanVertex = 0;
273   for (Int_t i0=0; i0<105; i0++)
274     {
275       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0; 
276       low[i0] = Int_t (GetRecoParam()->GetLow(i0));     
277       high[i0] = Int_t (GetRecoParam()->GetHigh(i0));
278     }
279    
280   Double32_t besttimeA=9999999;
281   Double32_t besttimeC=9999999;
282   Int_t pmtBestA=99999;
283   Int_t pmtBestC=99999;
284    
285   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
286   
287   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
288    
289   AliDebug(10," before read data ");
290   AliT0RawReader myrawreader(rawReader);
291
292   UInt_t type =rawReader->GetType();
293
294   if (!myrawreader.Next())
295     AliDebug(1,Form(" no raw data found!!"));
296   else
297     {  
298    for (Int_t i=0; i<24; i++)
299     {
300       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
301     }
302      Int_t fBCID=Int_t (rawReader->GetBCID());
303       Int_t trmbunch= myrawreader.GetTRMBunchID();
304       AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
305  
306       if(type == 7  ) {  //only physics 
307         for (Int_t i=0; i<105; i++) {
308         for (Int_t iHit=0; iHit<5; iHit++) 
309           {
310             allData[i][iHit] = myrawreader.GetData(i,iHit);
311           }
312         }
313         Int_t ref=0;
314         if (refPoint>0) 
315           ref = allData[refPoint][0]-5000;
316         
317         Float_t channelWidth = fParam->GetChannelWidth() ;  
318         
319         //       Int_t meanT0 = fParam->GetMeanT0();
320         
321         for (Int_t in=0; in<12; in++)  
322           {
323             for (Int_t iHit=0; iHit<5; iHit++) 
324               {
325                 if(allData[in+1][iHit] > low[in+1] && 
326                    allData[in+1][iHit] < high[in+1])
327                   {
328                     timeCFD[in] = allData[in+1][iHit] ; 
329                     break;
330                   }
331               }
332             for (Int_t iHit=0; iHit<5; iHit++) 
333               {
334                 if(allData[in+12+1][iHit] > low[in+12+1] && 
335                    allData[in+1+12][iHit] < high[in+12+1])
336                   {
337                         timeLED[in] = allData[in+12+1][iHit] ;
338                         break;
339                   }
340               }
341             for (Int_t iHit=0; iHit<5; iHit++) 
342               {
343                 if(allData[in+1+56][iHit] > low[in+1+56] && 
344                    allData[in+1+56][iHit] < high[in+1+56])
345                   {
346                     timeCFD[in+12] = allData[in+56+1][iHit] ;
347                     break;
348                   }
349               }
350             
351             for (Int_t iHit=0; iHit<5; iHit++) 
352               {
353                 if(allData[in+1+68][iHit] > low[in+1+68] && 
354                    allData[in+1+68][iHit] < high[in+1+68])
355                   {
356                     timeLED[in+12] = allData[in+68+1][iHit] ;
357                     break;
358                   }
359               }
360             AliDebug(10, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
361                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
362                               timeLED[in+12]));   
363             
364           }
365         
366         
367         for (Int_t in=0; in<12;  in++)
368           {
369             chargeQT0[in]=allData[2*in+25][0];
370             chargeQT1[in]=allData[2*in+26][0];
371             AliDebug(10, Form(" readed Raw %i %i %i",
372                               in, chargeQT0[in],chargeQT1[in]));
373           }     
374         for (Int_t in=12; in<24;  in++)
375           {
376             chargeQT0[in]=allData[2*in+57][0];
377             chargeQT1[in]=allData[2*in+58][0];
378             AliDebug(10, Form(" readed Raw %i %i %i",
379                               in, chargeQT0[in],chargeQT1[in]));
380             
381           }
382         
383         for (Int_t iHit=0; iHit<5; iHit++) 
384           {
385               if(allData[49][iHit] > low[49] && 
386                  allData[49][iHit] < high[49]){
387                 onlineMean = allData[49][iHit];
388                 break;
389               }
390           }
391         Double32_t time[24], adc[24],  noncalibtime[24];
392         for (Int_t ipmt=0; ipmt<24; ipmt++) {
393           if(timeCFD[ipmt]>0 ){
394            //for simulated data
395              //for physics  data
396            if(( chargeQT0[ipmt] - chargeQT1[ipmt])>0)  {
397              adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
398            }
399            else
400              adc[ipmt] = 0;
401            time[ipmt] = fCalib-> WalkCorrection(Int_t (refAmp), ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
402            
403            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
404            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
405            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
406                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
407            Double_t ampMip =( (TGraph*)fAmpLED.At(ipmt))->Eval(sl);
408            Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
409            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %f\n ",ampMip,qtMip, adc[ipmt]));
410            //bad peak removing
411              frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
412              // frecpoints->SetTime(ipmt,Double32_t(timeCFD[ipmt]));
413              frecpoints->SetAmp(ipmt, Double32_t( qtMip)); //for cosmic &pp beam 
414              frecpoints->SetAmpLED(ipmt, Double32_t(ampMip));        
415              noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
416          }
417          else {
418            time[ipmt] = 0;
419            adc[ipmt] = 0;
420            noncalibtime[ipmt] = 0;
421          }
422        }
423        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ;     
424        for (Int_t ipmt=0; ipmt<12; ipmt++){
425          if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>0.1 )
426            {
427              if(time[ipmt]<besttimeC){
428                besttimeC=time[ipmt]; //timeC
429                pmtBestC=ipmt;
430              }
431            }
432        }
433        for ( Int_t ipmt=12; ipmt<24; ipmt++)
434          {
435            if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>0.1)
436              {
437                if(time[ipmt]<besttimeA) {
438                  besttimeA=time[ipmt]; //timeA
439                  pmtBestA=ipmt;
440                }
441              }
442          }
443        if(besttimeA < 999999) 
444          frecpoints->SetTimeBestA(besttimeA * channelWidth - 1000.*fLatencyHPTDC + 1000.*fLatencyL1A - 1000.*fGRPdelays);
445        if( besttimeC < 999999 ) 
446          frecpoints->SetTimeBestC(besttimeC * channelWidth - 1000.*fLatencyHPTDC +1000.*fLatencyL1C - 1000.*fGRPdelays);
447        AliDebug(10,Form(" pmtA %i besttimeA %f ps, pmtC %i besttimeC %f ps",
448                        pmtBestA,besttimeA, pmtBestC,  besttimeC));
449         if(besttimeA <999999 && besttimeC < 999999 ){
450          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth + fLatencyL1A - fLatencyL1C;
451          timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - 1000.*fLatencyHPTDC + 1000.*fLatencyL1 - 1000.*fGRPdelays;  
452          meanTime = (besttimeA+besttimeC-2.*Float_t(ref))/2.;
453          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
454         }
455       }  //if phys event       
456       AliDebug(5,Form("  timeDiff %f #channel,  meanTime %f #channel, TOFmean%f  vertex %f cm meanVertex %f online mean %i \n",timeDiff, meanTime,timeclock, vertex,meanVertex, onlineMean));
457       frecpoints->SetT0clock(timeclock);
458       frecpoints->SetVertex(vertex);
459       frecpoints->SetMeanTime(meanTime);
460       frecpoints->SetOnlineMean(Int_t(onlineMean));
461         // Set triggers
462       
463       Bool_t tr[5];
464       Int_t trchan[5]= {50,51,52,55,56};
465       for (Int_t i=0; i<5; i++) tr[i]=false; 
466       for (Int_t itr=0; itr<5; itr++) {
467         for (Int_t iHit=0; iHit<5; iHit++) 
468           {
469             Int_t trr=trchan[itr];
470             if(allData[trr][iHit] > low[trr] &&
471                allData[trr][iHit] < high[trr]&&
472                allData[trr][iHit] > 0) tr[itr]=true;
473           }
474       }
475       frecpoints->SetT0Trig(tr);
476      } // if (else )raw data
477   recTree->Fill();
478   if(frecpoints) delete frecpoints;
479 }
480   
481   
482   //____________________________________________________________
483   
484   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
485   {
486
487   /***************************************************
488   Resonstruct digits to vertex position
489   ****************************************************/
490   
491   AliDebug(1,Form("Start FillESD T0"));
492   Float_t channelWidth = fParam->GetChannelWidth() ;  
493   Float_t c = 0.0299792458; // cm/ps
494   Float_t currentVertex=0, shift=0;
495   Int_t ncont=0;
496   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
497   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
498   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
499   if (!vertex)        vertex = pESD->GetVertex();
500
501   if (vertex) {
502     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
503                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
504     currentVertex = vertex->GetZ();
505     
506     ncont = vertex->GetNContributors();
507     //  cout<<"@@ spdver "<<spdver<<" ncont "<<ncont<<endl;
508     if(ncont>2 ) {
509       shift = currentVertex/c;
510     }
511   }
512   TTree *treeR = clustersTree;
513   
514   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
515   if (!frecpoints) {
516     AliError("Reconstruct Fill ESD >> no recpoints found");
517     return;
518   }
519   
520   AliDebug(1,Form("Start FillESD T0"));
521   TBranch *brRec = treeR->GetBranch("T0");
522   if (brRec) {
523     brRec->SetAddress(&frecpoints);
524   }else{
525     AliError(Form("EXEC Branch T0 rec not found"));
526     return;
527   } 
528   
529   brRec->GetEntry(0);
530   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
531   Double32_t* tcorr;
532   for(Int_t i=0; i<24; i++) 
533     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
534
535   Double32_t timeClock[3];
536   Double32_t zPosition = frecpoints -> GetVertex();
537   Double32_t timeStart = frecpoints -> GetMeanTime();
538   timeClock[0] = frecpoints -> GetT0clock() ;
539   timeClock[1] = frecpoints -> GetBestTimeA() + shift;
540   timeClock[2] = frecpoints -> GetBestTimeC() - shift;
541   for ( Int_t i=0; i<24; i++) {
542     time[i] =  frecpoints -> GetTime(i); // ps to ns
543     if ( time[i] >1) {
544       ampQTC[i] = frecpoints -> GetAmp(i);
545       amp[i] = frecpoints -> AmpLED(i);
546       AliDebug(1,Form("T0: time %f  ampQTC %f ampLED %f \n", time[i], ampQTC[i], amp[i]));
547    }
548   }
549   Int_t trig= frecpoints ->GetT0Trig();
550   pESD->SetT0Trig(trig);
551   
552   pESD->SetT0zVertex(zPosition); //vertex Z position 
553   pESD->SetT0(timeStart);        // interaction time 
554   for(Int_t i=0; i<3; i++) 
555     pESD->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
556   pESD->SetT0time(time);         // best TOF on each PMT 
557   pESD->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
558   
559   AliDebug(1,Form("T0: Vertex %f (T0A+T0C)/2 %f #channels T0signal %f ns OrA %f ns OrC %f T0trig %i\n",zPosition, timeStart, timeClock[0], timeClock[1], timeClock[2], trig));
560   
561   if (pESD) {
562     
563     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
564     if (fr) {
565       AliDebug(1, Form("Writing TZERO friend data to ESD tree"));
566
567       //     if (ncont>2) {
568         tcorr = fESDTZEROfriend->GetT0timeCorr();
569         for ( Int_t i=0; i<24; i++) {
570           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
571           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
572           if(time[i]>1)   AliDebug(1,Form("T0 friend : time %f  ampQTC %f ampLED %f \n", timecorr[i], ampQTC[i], amp[i]));
573         }
574         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
575         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
576         fESDTZEROfriend->SetT0ampQTC(ampQTC);
577         fr->SetTZEROfriend(fESDTZEROfriend);
578         //      }//
579     }
580   }
581      
582
583
584 } // vertex in 3 sigma
585
586
587
588
589
590