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