f2ba2b215c2aaea6c17970b44fddb5ccda78a2c8
[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];
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       
203     }
204     else {
205       time[ipmt] = 0;
206       adc[ipmt] = 0;
207     }
208   }
209   
210   for (Int_t ipmt=0; ipmt<12; ipmt++){
211     if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>lowAmpThreshold && adc[ipmt]<highAmpThreshold) {
212       if(time[ipmt]<besttimeC){
213         besttimeC=time[ipmt]; //timeC
214         pmtBestC=ipmt;
215       }
216     }
217   }
218   for ( Int_t ipmt=12; ipmt<24; ipmt++){
219     if(time[ipmt] > 1 &&  ipmt != badpmt && adc[ipmt]>lowAmpThreshold && adc[ipmt]<highAmpThreshold) {
220       if(time[ipmt]<besttimeA) {
221         besttimeA=time[ipmt]; //timeA
222         pmtBestA=ipmt;}
223     }
224   }
225   if(besttimeA < 999999) {
226     frecpoints->SetTimeBestA(Int_t(besttimeA *channelWidth - fdZonA/c));
227     tr[1]=true;
228   }
229   if( besttimeC < 999999 ) {
230     frecpoints->SetTimeBestC(Int_t(besttimeC *channelWidth - fdZonA/c));
231     tr[2]=true;
232   }
233   AliDebug(5,Form(" besttimeA %f ch,  besttimeC %f ch",besttimeA, besttimeC));
234   if(besttimeA <999999 && besttimeC < 999999 ){
235     //    timeDiff = (besttimeC - besttimeA)*channelWidth;
236     timeDiff = (besttimeA - besttimeC)*channelWidth;
237     meanTime = (besttimeA + besttimeC)/2;// * channelWidth); 
238     timeclock = meanTime *channelWidth -fdZonA/c ;
239     vertex = meanVertex - c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2;
240     tr[0]=true; 
241   }
242   frecpoints->SetVertex(vertex);
243   frecpoints->SetMeanTime(meanTime);
244   frecpoints->SetT0clock(timeclock);
245   frecpoints->SetT0Trig(tr);
246
247   AliDebug(5,Form("T0 triggers %d %d %d %d %d",tr[0],tr[1],tr[2],tr[3],tr[4]));
248
249   //online mean
250   frecpoints->SetOnlineMean(Int_t(onlineMean));
251   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));
252   
253   
254
255    
256   
257   clustersTree->Fill();
258
259   delete timeCFD;
260   delete timeLED;
261   delete chargeQT0; 
262   delete chargeQT1; 
263 }
264
265
266 //_______________________________________________________________________
267
268 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
269 {
270   // T0 raw ->
271   //
272   // reference amplitude and time ref. point from reco param
273
274   Float_t refAmp = GetRecoParam()->GetRefAmp();
275
276   //  Int_t refPoint = 0;
277
278   //Bad channel
279   Int_t badpmt = GetRecoParam()->GetRefPoint();
280  
281   Int_t low[500], high[500];
282
283   Int_t allData[110][5];
284   
285   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
286   Double32_t timeDiff=999999, meanTime=999999, timeclock=9999999;
287   Float_t c = 29.9792458; // cm/ns
288   Double32_t vertex = 9999999;
289   Int_t onlineMean=0;
290   // Float_t meanVertex = fParam->GetMeanVertex();
291   Float_t meanVertex = 0;
292   for (Int_t i0=0; i0<110; i0++)
293     {
294       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0; 
295       low[i0] = Int_t (GetRecoParam()->GetLow(i0));     
296       high[i0] = Int_t (GetRecoParam()->GetHigh(i0));
297       }
298   Float_t lowAmpThreshold =  GetRecoParam()->GetLow(200);  
299   Float_t highAmpThreshold =  GetRecoParam()->GetHigh(200);  
300   
301   Double32_t besttimeA=9999999;
302   Double32_t besttimeC=9999999;
303   Int_t pmtBestA=99999;
304   Int_t pmtBestC=99999;
305    
306   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
307   
308   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
309    
310   AliDebug(10," before read data ");
311   AliT0RawReader myrawreader(rawReader);
312
313   UInt_t type =rawReader->GetType();
314
315   if (!myrawreader.Next())
316     AliDebug(1,Form(" no raw data found!!"));
317   else
318     {  
319    for (Int_t i=0; i<24; i++)
320     {
321       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
322     }
323      Int_t fBCID=Int_t (rawReader->GetBCID());
324       Int_t trmbunch= myrawreader.GetTRMBunchID();
325       AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
326  
327       if(type == 7  ) {  //only physics 
328         for (Int_t i=0; i<107; i++) {
329         for (Int_t iHit=0; iHit<5; iHit++) 
330           {
331             allData[i][iHit] = myrawreader.GetData(i,iHit);
332           }
333         }
334         Int_t ref=0;
335
336         //      if (refPoint>0) 
337         //  ref = allData[refPoint][0]-5000;
338
339         
340         Float_t channelWidth = fParam->GetChannelWidth() ;  
341         
342         //       Int_t meanT0 = fParam->GetMeanT0();
343         
344         for (Int_t in=0; in<12; in++)  
345           {
346             for (Int_t iHit=0; iHit<5; iHit++) 
347               {
348                 if(allData[in+1][iHit] > low[in+1] && 
349                    allData[in+1][iHit] < high[in+1])
350                   {
351                     timeCFD[in] = allData[in+1][iHit] ; 
352                     break;
353                   }
354               }
355             for (Int_t iHit=0; iHit<5; iHit++) 
356               {
357                 if(allData[in+12+1][iHit] > low[in+12+1] && 
358                    allData[in+1+12][iHit] < high[in+12+1])
359                   {
360                         timeLED[in] = allData[in+12+1][iHit] ;
361                         break;
362                   }
363               }
364             for (Int_t iHit=0; iHit<5; iHit++) 
365               {
366                 if(allData[in+1+56][iHit] > low[in+1+56] && 
367                    allData[in+1+56][iHit] < high[in+1+56])
368                   {
369                     timeCFD[in+12] = allData[in+56+1][iHit] ;
370                     break;
371                   }
372               }
373             
374             for (Int_t iHit=0; iHit<5; iHit++) 
375               {
376                 if(allData[in+1+68][iHit] > low[in+1+68] && 
377                    allData[in+1+68][iHit] < high[in+1+68])
378                   {
379
380                     timeLED[in+12] = allData[in+68+1][iHit] ;
381                     break;
382                   }
383               }
384             AliDebug(5, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
385                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
386                               timeLED[in+12]));   
387             
388           }
389         
390         
391         for (Int_t in=0; in<12;  in++)
392           {
393             chargeQT0[in]=allData[2*in+25][0];
394             chargeQT1[in]=allData[2*in+26][0];
395             AliDebug(10, Form(" readed Raw %i %i %i",
396                               in, chargeQT0[in],chargeQT1[in]));
397           }     
398         for (Int_t in=12; in<24;  in++)
399           {
400             chargeQT0[in]=allData[2*in+57][0];
401             chargeQT1[in]=allData[2*in+58][0];
402             AliDebug(10, Form(" readed Raw %i %i %i",
403                               in, chargeQT0[in],chargeQT1[in]));
404             
405           }
406         
407         for (Int_t iHit=0; iHit<5; iHit++) 
408           {
409               if(allData[49][iHit] > low[49] && 
410                  allData[49][iHit] < high[49]){
411                 onlineMean = allData[49][iHit];
412                 break;
413               }
414           }
415         Double32_t time[24], adc[24],  noncalibtime[24];
416         for (Int_t ipmt=0; ipmt<24; ipmt++) {
417           if(timeCFD[ipmt]>0 && ipmt != badpmt ){
418            //for simulated data
419              //for physics  data
420            if(( chargeQT0[ipmt] - chargeQT1[ipmt])>0)  {
421              adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
422            }
423            else
424              adc[ipmt] = 0;
425            time[ipmt] = fCalib-> WalkCorrection(Int_t (refAmp), ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
426            
427            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
428            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
429            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
430                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
431            Double_t ampMip =( (TGraph*)fAmpLED.At(ipmt))->Eval(sl);
432            Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
433            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %f\n ",ampMip,qtMip, adc[ipmt]));
434            //bad peak removing
435              frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
436              // frecpoints->SetTime(ipmt,Double32_t(timeCFD[ipmt]));
437              frecpoints->SetAmp(ipmt, Double32_t( qtMip)); //for cosmic &pp beam 
438              frecpoints->SetAmpLED(ipmt, Double32_t(ampMip));        
439              noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
440          }
441          else {
442            time[ipmt] = 0;
443            adc[ipmt] = 0;
444            noncalibtime[ipmt] = 0;
445          }
446        }
447        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ;     
448        for (Int_t ipmt=0; ipmt<12; ipmt++){
449          if(time[ipmt] > 1 && ipmt != badpmt &&  adc[ipmt]>lowAmpThreshold && adc[ipmt]<highAmpThreshold )
450            {
451              if(time[ipmt]<besttimeC){
452                   besttimeC=time[ipmt]; //timeC
453                   pmtBestC=ipmt;
454              }
455            }
456        }
457        for ( Int_t ipmt=12; ipmt<24; ipmt++)
458          {
459            if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>lowAmpThreshold && adc[ipmt]<highAmpThreshold)
460              {
461                if(time[ipmt]<besttimeA) {
462                  besttimeA=time[ipmt]; //timeA
463                  pmtBestA=ipmt;
464                }
465              }
466          }
467        if(besttimeA < 999999) 
468          frecpoints->SetTimeBestA(besttimeA * channelWidth - 1000.*fLatencyHPTDC + 1000.*fLatencyL1A - 1000.*fGRPdelays - fTimeMeanShift[1]);
469
470        if( besttimeC < 999999 ) 
471          frecpoints->SetTimeBestC(besttimeC * channelWidth - 1000.*fLatencyHPTDC +1000.*fLatencyL1C - 1000.*fGRPdelays - fTimeMeanShift[2]);
472        AliDebug(5,Form(" pmtA %i besttimeA %f shift A %f ps, pmtC %i besttimeC %f shiftC %f ps",
473                        pmtBestA,besttimeA, fTimeMeanShift[1],
474
475                        pmtBestC,  besttimeC,fTimeMeanShift[2]));
476         if(besttimeA <999999 && besttimeC < 999999 ){
477          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth + fLatencyL1A - fLatencyL1C;
478          timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - 1000.*fLatencyHPTDC + 1000.*fLatencyL1 - 1000.*fGRPdelays - fTimeMeanShift[0];  
479          meanTime = (besttimeA+besttimeC-2.*Float_t(ref))/2.;
480          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
481         }
482       }  //if phys event       
483       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));
484       frecpoints->SetT0clock(timeclock);
485       frecpoints->SetVertex(vertex);
486       frecpoints->SetMeanTime(meanTime);
487       frecpoints->SetOnlineMean(Int_t(onlineMean));
488         // Set triggers
489       
490       Bool_t tr[5];
491       Int_t trchan[5]= {50,51,52,55,56};
492       for (Int_t i=0; i<5; i++) tr[i]=false; 
493       for (Int_t itr=0; itr<5; itr++) {
494         for (Int_t iHit=0; iHit<5; iHit++) 
495           {
496             Int_t trr=trchan[itr];
497             if( allData[trr][iHit] > 0) tr[itr]=true;
498           }
499       }
500       frecpoints->SetT0Trig(tr);
501    
502       //Set MPD
503       if(allData[53][0]>0 && allData[54][0]) 
504         frecpoints->SetMultA(allData[53][0]-allData[54][0]);
505         if(allData[105][0]>0 && allData[106][0]) 
506           frecpoints->SetMultC(allData[105][0]-allData[106][0]);
507         
508         
509     } // if (else )raw data
510   recTree->Fill();
511   if(frecpoints) delete frecpoints;
512 }
513   
514   
515   //____________________________________________________________
516   
517   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
518   {
519
520   /***************************************************
521   Resonstruct digits to vertex position
522   ****************************************************/
523   
524   AliDebug(1,Form("Start FillESD T0"));
525   pESD ->SetT0spread(fTimeSigmaShift);
526  
527
528   Float_t channelWidth = fParam->GetChannelWidth() ;  
529   Float_t c = 0.0299792458; // cm/ps
530   Float_t currentVertex=0, shift=0;
531   Int_t ncont=-1;
532   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
533   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
534   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
535   if (!vertex)        vertex = pESD->GetVertex();
536
537   if (vertex) {
538     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
539                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
540     currentVertex = vertex->GetZ();
541     
542     ncont = vertex->GetNContributors();
543     //  cout<<"@@ spdver "<<spdver<<" ncont "<<ncont<<endl;
544     if(ncont>0 ) {
545       shift = currentVertex/c;
546     }
547   }
548   TTree *treeR = clustersTree;
549   
550   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
551   if (!frecpoints) {
552     AliError("Reconstruct Fill ESD >> no recpoints found");
553     return;
554   }
555   
556   AliDebug(1,Form("Start FillESD T0"));
557   TBranch *brRec = treeR->GetBranch("T0");
558   if (brRec) {
559     brRec->SetAddress(&frecpoints);
560   }else{
561     AliError(Form("EXEC Branch T0 rec not found"));
562     return;
563   } 
564   
565   brRec->GetEntry(0);
566   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
567   Double32_t* tcorr;
568   for(Int_t i=0; i<24; i++) 
569     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
570
571
572   Double32_t timeClock[3];
573   Double32_t zPosition = frecpoints -> GetVertex();
574   Double32_t timeStart = frecpoints -> GetMeanTime();
575   timeClock[0] = frecpoints -> GetT0clock() ;
576   timeClock[1] = frecpoints -> GetBestTimeA() + shift;
577   timeClock[2] = frecpoints -> GetBestTimeC() - shift;
578   for ( Int_t i=0; i<24; i++) {
579     time[i] =  frecpoints -> GetTime(i); // ps to ns
580     if ( time[i] >1) {
581       ampQTC[i] = frecpoints -> GetAmp(i);
582       amp[i] = frecpoints -> AmpLED(i);
583       AliDebug(1,Form("T0: time %f  ampQTC %f ampLED %f \n", time[i], ampQTC[i], amp[i]));
584    }
585   }
586   Int_t trig= frecpoints ->GetT0Trig();
587   pESD->SetT0Trig(trig);
588   
589   pESD->SetT0zVertex(zPosition); //vertex Z position 
590
591   Double32_t multA=frecpoints ->GetMultA();
592   Double32_t multC=frecpoints ->GetMultC();
593   //  pESD->SetT0MultC(multC);        // multiplicity Cside 
594   //  pESD->SetT0MultA(multA);        // multiplicity Aside 
595   pESD->SetT0(multA); // for backward compatubility
596   pESD->SetT0clock(multC); // for backward compatubility
597
598   for(Int_t i=0; i<3; i++) 
599     pESD->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
600   pESD->SetT0time(time);         // best TOF on each PMT 
601   pESD->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
602   
603   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));
604   
605   if (pESD) {
606     
607     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
608     if (fr) {
609       AliDebug(1, Form("Writing TZERO friend data to ESD tree"));
610
611       //     if (ncont>2) {
612         tcorr = fESDTZEROfriend->GetT0timeCorr();
613         for ( Int_t i=0; i<24; i++) {
614           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
615           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
616           if(time[i]>1)   AliDebug(10,Form("T0 friend : time %f  ampQTC %f ampLED %f \n", timecorr[i], ampQTC[i], amp[i]));
617         }
618         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
619         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
620         fESDTZEROfriend->SetT0ampQTC(ampQTC);
621         fr->SetTZEROfriend(fESDTZEROfriend);
622         //      }//
623     }
624   }
625      
626
627
628 } // vertex in 3 sigma
629
630
631
632
633
634