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