]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0Reconstructor.cxx
AliT0Reconstructor with season time shift correction
[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 #include "AliT0CalibSeasonTimeShift.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   //constructor
70   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
71   if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
72   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
73   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
74
75   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
76   if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
77   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
78   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
79  
80   AliCDBEntry *entry4 = AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
81   if (!entry4) AliFatal("LHC clock-phase shift is not found in OCDB !");
82   AliLHCClockPhase *phase = (AliLHCClockPhase*)entry4->GetObject();
83
84   fGRPdelays = l1Delay - phase->GetMeanPhase();
85
86   AliCDBEntry *entry5 = AliCDBManager::Instance()->Get("T0/Calib/TimeAdjust");
87   if (entry5) {
88     AliT0CalibSeasonTimeShift *timeshift = (AliT0CalibSeasonTimeShift*)entry5->GetObject();
89     fTimeMeanShift = timeshift->GetT0Means();
90     fTimeSigmaShift  = timeshift->GetT0Sigmas();
91      timeshift->Print();
92   }
93   else
94     AliWarning("Time Adjust is not found in OCDB !");
95
96   fParam = AliT0Parameters::Instance();
97   fParam->Init();
98  
99   for (Int_t i=0; i<24; i++){
100         TGraph* gr = fParam ->GetAmpLEDRec(i);
101         if (gr) fAmpLEDrec.AddAtAndExpand(gr,i) ; 
102           TGraph* gr1 = fParam ->GetAmpLED(i);
103           if (gr1) fAmpLED.AddAtAndExpand(gr1,i) ; 
104           TGraph* gr2 = fParam ->GetQTC(i);
105           if (gr2) fQTC.AddAtAndExpand(gr2,i) ;         
106   }
107
108   fLatencyL1 = fParam->GetLatencyL1();
109   fLatencyL1A = fParam->GetLatencyL1A();
110   fLatencyL1C = fParam->GetLatencyL1C();
111   fLatencyHPTDC = fParam->GetLatencyHPTDC();
112   AliDebug(2,Form(" LatencyL1 %f latencyL1A %f latencyL1C %f latencyHPTDC %f \n",fLatencyL1, fLatencyL1A, fLatencyL1C, fLatencyHPTDC));
113   
114   // fdZonC = TMath::Abs(fParam->GetZPositionShift("T0/C/PMT1"));
115   //fdZonA = TMath::Abs(fParam->GetZPositionShift("T0/A/PMT15"));
116   //here real Z position
117   fdZonC = TMath::Abs(fParam->GetZPosition("T0/C/PMT1"));
118   fdZonA = TMath::Abs(fParam->GetZPosition("T0/A/PMT15"));
119
120   fCalib = new AliT0Calibrator();
121   fESDTZEROfriend = new AliESDTZEROfriend();
122
123 }
124
125 //_____________________________________________________________________________
126 void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
127   
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
148   TBranch *brDigits=digitsTree->GetBranch("T0");
149   AliT0digit *fDigits = new AliT0digit() ;
150   if (brDigits) {
151     brDigits->SetAddress(&fDigits);
152   }else{
153     AliError(Form("EXEC Branch T0 digits not found"));
154      return;
155   }
156   
157   digitsTree->GetEvent(0);
158   digitsTree->GetEntry(0);
159   brDigits->GetEntry(0);
160   fDigits->GetTimeCFD(*timeCFD);
161   fDigits->GetTimeLED(*timeLED);
162   fDigits->GetQT0(*chargeQT0);
163   fDigits->GetQT1(*chargeQT1);
164   Int_t onlineMean =  fDigits->MeanTime();
165
166   Bool_t tr[5];
167   for (Int_t i=0; i<5; i++) tr[i]=false; 
168   
169   Double32_t besttimeA=999999;
170   Double32_t besttimeC=999999;
171   Int_t pmtBestA=99999;
172   Int_t pmtBestC=99999;
173   
174   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
175   clustersTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
176   
177   Float_t time[24], adc[24];
178   for (Int_t ipmt=0; ipmt<24; ipmt++) {
179     if(timeCFD->At(ipmt)>0 ){
180      if(( chargeQT1->At(ipmt) - chargeQT0->At(ipmt))>0)  
181         adc[ipmt] = chargeQT1->At(ipmt) - chargeQT0->At(ipmt);
182       else
183         adc[ipmt] = 0;
184       
185      time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, Int_t(adc[ipmt]),  timeCFD->At(ipmt)) ;
186              
187       Double_t sl = Double_t(timeLED->At(ipmt) - timeCFD->At(ipmt));
188       //    time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl),  timeCFD->At(ipmt) ) ;
189       AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
190                        ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
191
192       Double_t ampMip =((TGraph*)fAmpLED.At(ipmt))->Eval(sl);
193       Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
194       AliDebug(5,Form("  Amlitude in MIPS LED %f ,  QTC %f in channels %f\n ",ampMip,qtMip, adc[ipmt]));
195       
196       frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
197       frecpoints->SetAmpLED(ipmt, Float_t( ampMip)); //for cosmic &pp beam 
198       frecpoints->SetAmp(ipmt, Float_t(qtMip));
199       
200     }
201     else {
202       time[ipmt] = 0;
203       adc[ipmt] = 0;
204     }
205   }
206   
207   for (Int_t ipmt=0; ipmt<12; ipmt++){
208     if(time[ipmt] > 1 ) {
209       if(time[ipmt]<besttimeC){
210         besttimeC=time[ipmt]; //timeC
211         pmtBestC=ipmt;
212       }
213     }
214   }
215   for ( Int_t ipmt=12; ipmt<24; ipmt++){
216     if(time[ipmt] > 1) {
217       if(time[ipmt]<besttimeA) {
218         besttimeA=time[ipmt]; //timeA
219         pmtBestA=ipmt;}
220     }
221   }
222   if(besttimeA < 999999) {
223     frecpoints->SetTimeBestA(Int_t(besttimeA *channelWidth - fdZonA/c));
224     tr[1]=true;
225   }
226   if( besttimeC < 999999 ) {
227     frecpoints->SetTimeBestC(Int_t(besttimeC *channelWidth - fdZonA/c));
228     tr[2]=true;
229   }
230   AliDebug(5,Form(" besttimeA %f ch,  besttimeC %f ch",besttimeA, besttimeC));
231   if(besttimeA <999999 && besttimeC < 999999 ){
232     //    timeDiff = (besttimeC - besttimeA)*channelWidth;
233     timeDiff = (besttimeA - besttimeC)*channelWidth;
234     meanTime = (besttimeA + besttimeC)/2;// * channelWidth); 
235     timeclock = meanTime *channelWidth -fdZonA/c ;
236     vertex = meanVertex - c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2;
237     tr[0]=true; 
238   }
239   frecpoints->SetVertex(vertex);
240   frecpoints->SetMeanTime(meanTime);
241   frecpoints->SetT0clock(timeclock);
242   frecpoints->SetT0Trig(tr);
243
244   AliDebug(5,Form("T0 triggers %d %d %d %d %d",tr[0],tr[1],tr[2],tr[3],tr[4]));
245
246   //online mean
247   frecpoints->SetOnlineMean(Int_t(onlineMean));
248   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));
249   
250   
251
252    
253   
254   clustersTree->Fill();
255
256   delete timeCFD;
257   delete timeLED;
258   delete chargeQT0; 
259   delete chargeQT1; 
260 }
261
262
263 //_______________________________________________________________________
264
265 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
266 {
267   // T0 raw ->
268   //
269   // reference amplitude and time ref. point from reco param
270
271   Float_t refAmp = GetRecoParam()->GetRefAmp();
272   Int_t refPoint = 0;
273   //Bad channel
274   Int_t badpmt = GetRecoParam()->GetRefPoint();
275   Int_t low[110], high[110];
276
277   Int_t allData[110][5];
278   
279   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
280   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
281   Float_t c = 29.9792458; // cm/ns
282   Double32_t vertex = 9999999;
283   Int_t onlineMean=0;
284   // Float_t meanVertex = fParam->GetMeanVertex();
285   Float_t meanVertex = 0;
286   for (Int_t i0=0; i0<110; i0++)
287     {
288       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0; 
289       low[i0] = Int_t (GetRecoParam()->GetLow(i0));     
290       high[i0] = Int_t (GetRecoParam()->GetHigh(i0));
291      }
292    
293   Double32_t besttimeA=9999999;
294   Double32_t besttimeC=9999999;
295   Int_t pmtBestA=99999;
296   Int_t pmtBestC=99999;
297    
298   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
299   
300   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
301    
302   AliDebug(10," before read data ");
303   AliT0RawReader myrawreader(rawReader);
304
305   UInt_t type =rawReader->GetType();
306
307   if (!myrawreader.Next())
308     AliDebug(1,Form(" no raw data found!!"));
309   else
310     {  
311    for (Int_t i=0; i<24; i++)
312     {
313       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
314     }
315      Int_t fBCID=Int_t (rawReader->GetBCID());
316       Int_t trmbunch= myrawreader.GetTRMBunchID();
317       AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
318  
319       if(type == 7  ) {  //only physics 
320         for (Int_t i=0; i<107; i++) {
321         for (Int_t iHit=0; iHit<5; iHit++) 
322           {
323             allData[i][iHit] = myrawreader.GetData(i,iHit);
324           }
325         }
326         Int_t ref=0;
327         if (refPoint>0) 
328           ref = allData[refPoint][0]-5000;
329         
330         Float_t channelWidth = fParam->GetChannelWidth() ;  
331         
332         //       Int_t meanT0 = fParam->GetMeanT0();
333         
334         for (Int_t in=0; in<12; in++)  
335           {
336             for (Int_t iHit=0; iHit<5; iHit++) 
337               {
338                 if(allData[in+1][iHit] > low[in+1] && 
339                    allData[in+1][iHit] < high[in+1])
340                   {
341                     timeCFD[in] = allData[in+1][iHit] ; 
342                     cout<<" low "<<low[in+1]<<" high "<<high[in+1]<<endl;
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                     cout<<" low "<<low[in+1+56]<<" high "<<high[in+1+56]<<endl;
362                     break;
363                   }
364               }
365             
366             for (Int_t iHit=0; iHit<5; iHit++) 
367               {
368                 if(allData[in+1+68][iHit] > low[in+1+68] && 
369                    allData[in+1+68][iHit] < high[in+1+68])
370                   {
371
372                     timeLED[in+12] = allData[in+68+1][iHit] ;
373                     break;
374                   }
375               }
376             AliDebug(5, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
377                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
378                               timeLED[in+12]));   
379             
380           }
381         
382         
383         for (Int_t in=0; in<12;  in++)
384           {
385             chargeQT0[in]=allData[2*in+25][0];
386             chargeQT1[in]=allData[2*in+26][0];
387             AliDebug(10, Form(" readed Raw %i %i %i",
388                               in, chargeQT0[in],chargeQT1[in]));
389           }     
390         for (Int_t in=12; in<24;  in++)
391           {
392             chargeQT0[in]=allData[2*in+57][0];
393             chargeQT1[in]=allData[2*in+58][0];
394             AliDebug(10, Form(" readed Raw %i %i %i",
395                               in, chargeQT0[in],chargeQT1[in]));
396             
397           }
398         
399         for (Int_t iHit=0; iHit<5; iHit++) 
400           {
401               if(allData[49][iHit] > low[49] && 
402                  allData[49][iHit] < high[49]){
403                 onlineMean = allData[49][iHit];
404                 break;
405               }
406           }
407         Double32_t time[24], adc[24],  noncalibtime[24];
408         for (Int_t ipmt=0; ipmt<24; ipmt++) {
409           if(timeCFD[ipmt]>0 ){
410            //for simulated data
411              //for physics  data
412            if(( chargeQT0[ipmt] - chargeQT1[ipmt])>0)  {
413              adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
414            }
415            else
416              adc[ipmt] = 0;
417            time[ipmt] = fCalib-> WalkCorrection(Int_t (refAmp), ipmt, Int_t(adc[ipmt]), timeCFD[ipmt] ) ;
418            
419            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
420            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
421            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
422                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
423            Double_t ampMip =( (TGraph*)fAmpLED.At(ipmt))->Eval(sl);
424            Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
425            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %f\n ",ampMip,qtMip, adc[ipmt]));
426            //bad peak removing
427              frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
428              // frecpoints->SetTime(ipmt,Double32_t(timeCFD[ipmt]));
429              frecpoints->SetAmp(ipmt, Double32_t( qtMip)); //for cosmic &pp beam 
430              frecpoints->SetAmpLED(ipmt, Double32_t(ampMip));        
431              noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
432          }
433          else {
434            time[ipmt] = 0;
435            adc[ipmt] = 0;
436            noncalibtime[ipmt] = 0;
437          }
438        }
439        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ;     
440        for (Int_t ipmt=0; ipmt<12; ipmt++){
441          if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>0.1 )
442            {
443              if(time[ipmt]<besttimeC){
444                   besttimeC=time[ipmt]; //timeC
445                   pmtBestC=ipmt;
446              }
447            }
448        }
449        for ( Int_t ipmt=12; ipmt<24; ipmt++)
450          {
451            if(time[ipmt] > 1 && ipmt != badpmt && adc[ipmt]>0.1)
452              {
453                if(time[ipmt]<besttimeA) {
454                  besttimeA=time[ipmt]; //timeA
455                  pmtBestA=ipmt;
456                }
457              }
458          }
459        if(besttimeA < 999999) 
460          frecpoints->SetTimeBestA(besttimeA * channelWidth - 1000.*fLatencyHPTDC + 1000.*fLatencyL1A - 1000.*fGRPdelays - fTimeMeanShift[1]);
461
462        if( besttimeC < 999999 ) 
463          frecpoints->SetTimeBestC(besttimeC * channelWidth - 1000.*fLatencyHPTDC +1000.*fLatencyL1C - 1000.*fGRPdelays - fTimeMeanShift[2]);
464        AliDebug(5,Form(" pmtA %i besttimeA %f shift A %f ps, pmtC %i besttimeC %f shiftC %f ps",
465                        pmtBestA,besttimeA, fTimeMeanShift[1],
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 %f \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=0;
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>2 ) {
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
584   for(Int_t i=0; i<3; i++) 
585     pESD->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
586   pESD->SetT0time(time);         // best TOF on each PMT 
587   pESD->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
588   
589   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));
590   
591   if (pESD) {
592     
593     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
594     if (fr) {
595       AliDebug(1, Form("Writing TZERO friend data to ESD tree"));
596
597       //     if (ncont>2) {
598         tcorr = fESDTZEROfriend->GetT0timeCorr();
599         for ( Int_t i=0; i<24; i++) {
600           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
601           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
602           if(time[i]>1)   AliDebug(10,Form("T0 friend : time %f  ampQTC %f ampLED %f \n", timecorr[i], ampQTC[i], amp[i]));
603         }
604         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
605         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
606         fESDTZEROfriend->SetT0ampQTC(ampQTC);
607         fr->SetTZEROfriend(fESDTZEROfriend);
608         //      }//
609     }
610   }
611      
612
613
614 } // vertex in 3 sigma
615
616
617
618
619
620