47320a763617c18675b6142dee1d283aef944362
[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   Float_t lowAmpThreshold =  GetRecoParam()->GetLow(200);  
294   Float_t highAmpThreshold =  GetRecoParam()->GetHigh(200);  
295   Double32_t besttimeA=9999999;
296   Double32_t besttimeC=9999999;
297   Int_t pmtBestA=99999;
298   Int_t pmtBestC=99999;
299    
300   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
301   
302   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints);
303    
304   AliDebug(10," before read data ");
305   AliT0RawReader myrawreader(rawReader);
306
307   UInt_t type =rawReader->GetType();
308
309   if (!myrawreader.Next())
310     AliDebug(1,Form(" no raw data found!!"));
311   else
312     {  
313    for (Int_t i=0; i<24; i++)
314     {
315       timeCFD[i]=0; timeLED[i]=0; chargeQT0[i]=0; chargeQT1[i]=0;
316     }
317      Int_t fBCID=Int_t (rawReader->GetBCID());
318       Int_t trmbunch= myrawreader.GetTRMBunchID();
319       AliDebug(10,Form(" CDH BC ID %i, TRM BC ID %i \n", fBCID, trmbunch ));
320  
321       if(type == 7  ) {  //only physics 
322         for (Int_t i=0; i<107; i++) {
323         for (Int_t iHit=0; iHit<5; iHit++) 
324           {
325             allData[i][iHit] = myrawreader.GetData(i,iHit);
326           }
327         }
328         Int_t ref=0;
329         if (refPoint>0) 
330           ref = allData[refPoint][0]-5000;
331         
332         Float_t channelWidth = fParam->GetChannelWidth() ;  
333         
334         //       Int_t meanT0 = fParam->GetMeanT0();
335         
336         for (Int_t in=0; in<12; in++)  
337           {
338             for (Int_t iHit=0; iHit<5; iHit++) 
339               {
340                 if(allData[in+1][iHit] > low[in+1] && 
341                    allData[in+1][iHit] < high[in+1])
342                   {
343                     timeCFD[in] = allData[in+1][iHit] ; 
344                     break;
345                   }
346               }
347             for (Int_t iHit=0; iHit<5; iHit++) 
348               {
349                 if(allData[in+12+1][iHit] > low[in+12+1] && 
350                    allData[in+1+12][iHit] < high[in+12+1])
351                   {
352                         timeLED[in] = allData[in+12+1][iHit] ;
353                         break;
354                   }
355               }
356             for (Int_t iHit=0; iHit<5; iHit++) 
357               {
358                 if(allData[in+1+56][iHit] > low[in+1+56] && 
359                    allData[in+1+56][iHit] < high[in+1+56])
360                   {
361                     timeCFD[in+12] = allData[in+56+1][iHit] ;
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]>lowAmpThreshold  && adc[ipmt]<highAmpThreshold)
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]>lowAmpThreshold && adc[ipmt]<highAmpThreshold)
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
467                        pmtBestC,  besttimeC,fTimeMeanShift[2]));
468         if(besttimeA <999999 && besttimeC < 999999 ){
469          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth + fLatencyL1A - fLatencyL1C;
470          timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - 1000.*fLatencyHPTDC + 1000.*fLatencyL1 - 1000.*fGRPdelays - fTimeMeanShift[0];  
471          meanTime = (besttimeA+besttimeC-2.*Float_t(ref))/2.;
472          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
473         }
474       }  //if phys event       
475       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));
476       frecpoints->SetT0clock(timeclock);
477       frecpoints->SetVertex(vertex);
478       frecpoints->SetMeanTime(meanTime);
479       frecpoints->SetOnlineMean(Int_t(onlineMean));
480         // Set triggers
481       
482       Bool_t tr[5];
483       Int_t trchan[5]= {50,51,52,55,56};
484       for (Int_t i=0; i<5; i++) tr[i]=false; 
485       for (Int_t itr=0; itr<5; itr++) {
486         for (Int_t iHit=0; iHit<5; iHit++) 
487           {
488             Int_t trr=trchan[itr];
489             if( allData[trr][iHit] > 0) tr[itr]=true;
490           }
491       }
492       frecpoints->SetT0Trig(tr);
493
494       //Set MPD
495       if(allData[53][0]>0 && allData[54][0]) 
496         frecpoints->SetMultA(allData[53][0]-allData[54][0]);
497         if(allData[105][0]>0 && allData[106][0]) 
498           frecpoints->SetMultC(allData[105][0]-allData[106][0]);
499         
500         
501     } // if (else )raw data
502   recTree->Fill();
503   if(frecpoints) delete frecpoints;
504 }
505   
506   
507   //____________________________________________________________
508   
509   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
510   {
511
512   /***************************************************
513   Resonstruct digits to vertex position
514   ****************************************************/
515   
516   AliDebug(1,Form("Start FillESD T0"));
517   Float_t channelWidth = fParam->GetChannelWidth() ;  
518   Float_t c = 0.0299792458; // cm/ps
519   Float_t currentVertex=0, shift=0;
520   Int_t ncont=-1;
521   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
522   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
523   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
524   if (!vertex)        vertex = pESD->GetVertex();
525
526   if (vertex) {
527     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
528                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
529     currentVertex = vertex->GetZ();
530     
531     ncont = vertex->GetNContributors();
532     //  cout<<"@@ spdver "<<spdver<<" ncont "<<ncont<<endl;
533     if(ncont>0 ) {
534       shift = currentVertex/c;
535     }
536   }
537   TTree *treeR = clustersTree;
538   
539   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
540   if (!frecpoints) {
541     AliError("Reconstruct Fill ESD >> no recpoints found");
542     return;
543   }
544   
545   AliDebug(1,Form("Start FillESD T0"));
546   TBranch *brRec = treeR->GetBranch("T0");
547   if (brRec) {
548     brRec->SetAddress(&frecpoints);
549   }else{
550     AliError(Form("EXEC Branch T0 rec not found"));
551     return;
552   } 
553   
554   brRec->GetEntry(0);
555   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
556   Double32_t* tcorr;
557   for(Int_t i=0; i<24; i++) 
558     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
559
560
561   Double32_t timeClock[3];
562   Double32_t zPosition = frecpoints -> GetVertex();
563   Double32_t timeStart = frecpoints -> GetMeanTime();
564   timeClock[0] = frecpoints -> GetT0clock() ;
565   timeClock[1] = frecpoints -> GetBestTimeA() + shift;
566   timeClock[2] = frecpoints -> GetBestTimeC() - shift;
567   for ( Int_t i=0; i<24; i++) {
568     time[i] =  frecpoints -> GetTime(i); // ps to ns
569     if ( time[i] >1) {
570       ampQTC[i] = frecpoints -> GetAmp(i);
571       amp[i] = frecpoints -> AmpLED(i);
572       AliDebug(1,Form("T0: time %f  ampQTC %f ampLED %f \n", time[i], ampQTC[i], amp[i]));
573    }
574   }
575   Int_t trig= frecpoints ->GetT0Trig();
576   pESD->SetT0Trig(trig);
577   
578   pESD->SetT0zVertex(zPosition); //vertex Z position 
579
580   Double32_t multA=frecpoints ->GetMultA();
581   Double32_t multC=frecpoints ->GetMultC();
582   //  pESD->SetT0MultC(multC);        // multiplicity Cside 
583   //  pESD->SetT0MultA(multA);        // multiplicity Aside 
584   pESD->SetT0(multA); // for backward compatubility
585   pESD->SetT0clock(multC); // for backward compatubility
586
587   for(Int_t i=0; i<3; i++) 
588     pESD->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
589   pESD->SetT0time(time);         // best TOF on each PMT 
590   pESD->SetT0amplitude(ampQTC);     // number of particles(MIPs) on each PMT
591   
592   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));
593   
594   if (pESD) {
595     
596     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
597     if (fr) {
598       AliDebug(1, Form("Writing TZERO friend data to ESD tree"));
599
600       //     if (ncont>2) {
601         tcorr = fESDTZEROfriend->GetT0timeCorr();
602         for ( Int_t i=0; i<24; i++) {
603           if(i<12 && time[i]>1) timecorr[i] = tcorr[i] -  shift/channelWidth;
604           if(i>11 && time[i]>1) timecorr[i] = tcorr[i] +  shift/channelWidth;
605           if(time[i]>1)   AliDebug(10,Form("T0 friend : time %f  ampQTC %f ampLED %f \n", timecorr[i], ampQTC[i], amp[i]));
606         }
607         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
608         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
609         fESDTZEROfriend->SetT0ampQTC(ampQTC);
610         fr->SetTZEROfriend(fESDTZEROfriend);
611         //      }//
612     }
613   }
614      
615
616
617 } // vertex in 3 sigma
618
619
620
621
622
623