using reco and trigger parameters for T0
[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
34 #include <TArrayI.h>
35 #include <TGraph.h>
36 #include <TMath.h>
37 #include <Riostream.h>
38
39 ClassImp(AliT0Reconstructor)
40
41   AliT0Reconstructor:: AliT0Reconstructor(): AliReconstructor(),
42                                              fdZonA(0),
43                                              fdZonC(0),
44                                              fZposition(0),
45                                              fParam(NULL),
46                                              fAmpLEDrec(),
47                                              fQTC(0),
48                                              fAmpLED(0),
49                                              fCalib()
50 {
51   //constructor
52
53   fParam = AliT0Parameters::Instance();
54   fParam->Init();
55  
56   for (Int_t i=0; i<24; i++){
57         TGraph* gr = fParam ->GetAmpLEDRec(i);
58         if (gr) fAmpLEDrec.AddAtAndExpand(gr,i) ; 
59           TGraph* gr1 = fParam ->GetAmpLED(i);
60           if (gr1) fAmpLED.AddAtAndExpand(gr1,i) ; 
61           TGraph* gr2 = fParam ->GetQTC(i);
62           if (gr2) fQTC.AddAtAndExpand(gr2,i) ;         
63   }
64
65   
66   fdZonC = TMath::Abs(fParam->GetZPositionShift("T0/C/PMT1"));
67   fdZonA = TMath::Abs(fParam->GetZPositionShift("T0/A/PMT15"));
68
69   fCalib = new AliT0Calibrator();
70
71 }
72 //____________________________________________________________________
73
74 AliT0Reconstructor::AliT0Reconstructor(const AliT0Reconstructor &r):
75   AliReconstructor(r),
76                                              fdZonA(0),
77                                              fdZonC(0),
78                                              fZposition(0),
79                                              fParam(NULL),
80                                              fAmpLEDrec(0),
81                                              fQTC(0),
82                                              fAmpLED(0),
83                                              fCalib()
84
85  {
86   //
87   // AliT0Reconstructor copy constructor
88   //
89
90   ((AliT0Reconstructor &) r).Copy(*this);
91
92 }
93
94 //_____________________________________________________________________________
95 AliT0Reconstructor &AliT0Reconstructor::operator=(const AliT0Reconstructor &r)
96 {
97   //
98   // Assignment operator
99   //
100   if (this != &r) ((AliT0Reconstructor &) r).Copy(*this);
101   return *this;
102
103 }
104
105 //_____________________________________________________________________________
106
107 void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
108   
109 {
110   // T0 digits reconstruction
111   Int_t refAmp = GetRecoParam()->GetRefAmp();
112   Int_t refPoint = GetRecoParam()->GetRefPoint();
113  
114   TArrayI * timeCFD = new TArrayI(24); 
115   TArrayI * timeLED = new TArrayI(24); 
116   TArrayI * chargeQT0 = new TArrayI(24); 
117   TArrayI * chargeQT1 = new TArrayI(24); 
118
119  
120   Float_t channelWidth = fParam->GetChannelWidth() ;  
121   Float_t meanVertex = fParam->GetMeanVertex();
122   
123   AliDebug(1,Form("Start DIGITS reconstruction "));
124   
125
126   TBranch *brDigits=digitsTree->GetBranch("T0");
127   AliT0digit *fDigits = new AliT0digit() ;
128   if (brDigits) {
129     brDigits->SetAddress(&fDigits);
130   }else{
131     AliError(Form("EXEC Branch T0 digits not found"));
132      return;
133   }
134   
135   digitsTree->GetEvent(0);
136   digitsTree->GetEntry(0);
137   brDigits->GetEntry(0);
138   fDigits->GetTimeCFD(*timeCFD);
139   fDigits->GetTimeLED(*timeLED);
140   fDigits->GetQT0(*chargeQT0);
141   fDigits->GetQT1(*chargeQT1);
142   Int_t onlineMean =  fDigits->MeanTime();
143
144   
145   Float_t besttimeA=999999;
146   Float_t besttimeC=999999;
147   Int_t pmtBestA=99999;
148   Int_t pmtBestC=99999;
149   Float_t timeDiff=999999, meanTime=0;
150   
151   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
152   clustersTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
153   
154   Float_t time[24], adc[24];
155   for (Int_t ipmt=0; ipmt<24; ipmt++) {
156     if(timeCFD->At(ipmt)>0 ){
157      if(( chargeQT1->At(ipmt) - chargeQT0->At(ipmt))>0)  
158         adc[ipmt] = chargeQT1->At(ipmt) - chargeQT0->At(ipmt);
159       else
160         adc[ipmt] = 0;
161       
162       time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, adc[ipmt],  timeCFD->At(ipmt)) ;
163              
164       Double_t sl = Double_t(timeLED->At(ipmt) - timeCFD->At(ipmt));
165       //    time[ipmt] = fCalib-> WalkCorrection( ipmt, Int_t(sl), timeCFD[ipmt],"cosmic" ) ;
166       AliDebug(10,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
167                        ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
168       Double_t ampMip =((TGraph*)fAmpLED.At(ipmt))->Eval(sl);
169       Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
170       AliDebug(10,Form("  Amlitude in MIPS LED %f ,  QTC %f in channels %i\n ",ampMip,qtMip, adc[ipmt]));
171       
172       frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
173       frecpoints->SetAmp(ipmt, Float_t( ampMip)); //for cosmic &pp beam 
174       frecpoints->SetAmpLED(ipmt, Float_t(qtMip));
175       
176     }
177     else {
178       time[ipmt] = 0;
179       adc[ipmt] = 0;
180     }
181   }
182   
183   for (Int_t ipmt=0; ipmt<12; ipmt++){
184     if(time[ipmt] > 1 ) {
185       if(time[ipmt]<besttimeC){
186         besttimeC=time[ipmt]; //timeC
187         pmtBestC=ipmt;
188       }
189     }
190   }
191   for ( Int_t ipmt=12; ipmt<24; ipmt++){
192     if(time[ipmt] > 1) {
193       if(time[ipmt]<besttimeA) {
194         besttimeA=time[ipmt]; //timeA
195         pmtBestA=ipmt;}
196     }
197   }
198   if(besttimeA !=999999)  frecpoints->SetTimeBestA(Int_t(besttimeA));
199   if( besttimeC != 999999 ) frecpoints->SetTimeBestC(Int_t(besttimeC));
200   AliDebug(10,Form(" besttimeA %f ch,  besttimeC %f ch",besttimeA, besttimeC));
201   Float_t c = 0.0299792; // cm/ps
202   Float_t vertex = 0;
203   if(besttimeA !=999999 && besttimeC != 999999 ){
204     timeDiff = (besttimeC - besttimeA)*channelWidth;
205     meanTime = Float_t((besttimeA + besttimeC)/2);// * channelWidth); 
206     //    meanTime = (meanT0 - (besttimeA + besttimeC)/2) * channelWidth;
207     vertex = meanVertex - c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2; 
208     frecpoints->SetVertex(vertex);
209     frecpoints->SetMeanTime(Int_t(meanTime));
210     //online mean
211     frecpoints->SetOnlineMean(Int_t(onlineMean));
212     AliDebug(10,Form("  timeDiff %f #channel,  meanTime %f #channel, vertex %f cm online mean %i ps",timeDiff, meanTime,vertex, Int_t(onlineMean)));
213     
214   }
215   
216   clustersTree->Fill();
217
218   delete timeCFD;
219   delete timeLED;
220   delete chargeQT0; 
221   delete chargeQT1; 
222 }
223
224
225 //_______________________________________________________________________
226
227 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
228 {
229   // T0 raw ->
230   //
231   // reference amplitude and time ref. point from reco param
232
233  Int_t refAmp = GetRecoParam()->GetRefAmp();
234  Int_t refPoint = GetRecoParam()->GetRefPoint();
235
236
237   Int_t allData[110][5];
238   
239   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
240    
241   for (Int_t i0=0; i0<105; i0++)
242     {
243       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0;   
244     }
245    
246   Float_t besttimeA=9999999;
247   Float_t besttimeC=9999999;
248   Int_t pmtBestA=99999;
249   Int_t pmtBestC=99999;
250   Float_t timeDiff=9999999, meanTime=0;
251   Float_t meanVertex = fParam->GetMeanVertex();
252    
253   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
254   
255   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
256    
257   AliDebug(10," before read data ");
258   AliT0RawReader myrawreader(rawReader);
259   if (!myrawreader.Next())
260     AliDebug(1,Form(" no raw data found!!"));
261   else
262     {  
263       for (Int_t i=0; i<105; i++) {
264         for (Int_t iHit=0; iHit<5; iHit++) 
265           {
266             allData[i][iHit] = myrawreader.GetData(i,iHit);
267           }
268       }
269     
270       Float_t channelWidth = fParam->GetChannelWidth() ;  
271       
272       //       Int_t meanT0 = fParam->GetMeanT0();
273        
274           
275           for (Int_t in=0; in<12; in++)  
276             {
277               timeCFD[in] = allData[in+1][0] ;
278               timeCFD[in+12] = allData[in+56+1][0] ;
279               timeLED[in] = allData[in+12+1][0] ;
280               timeLED[in+12] = allData[in+68+1][0] ;
281               AliDebug(10, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
282                                 in, timeCFD[in],timeCFD[in+12],timeLED[in], 
283                                 timeLED[in+12]));   
284             }
285           
286           for (Int_t in=0; in<12;  in++)
287             {
288               chargeQT0[in]=allData[2*in+25][0];
289               chargeQT1[in]=allData[2*in+26][0];
290             }
291           
292            for (Int_t in=12; in<24;  in++)
293              {
294                chargeQT0[in]=allData[2*in+57][0];
295                chargeQT1[in]=allData[2*in+58][0];
296              }
297            
298            //    } //cosmic with physics event
299        for (Int_t in=0; in<24; in++)  
300          AliDebug(10, Form(" readed Raw %i %i %i %i %i",
301                            in, timeLED[in],timeCFD[in],chargeQT0[in],chargeQT1[in]));
302        Int_t onlineMean = allData[49][0];       
303        
304        Float_t time[24], adc[24];
305        for (Int_t ipmt=0; ipmt<24; ipmt++) {
306          if(timeCFD[ipmt]>0 && timeLED[ipmt]>0){
307            //for simulated data
308              //for physics  data
309            if(( chargeQT1[ipmt] - chargeQT0[ipmt])>0)  
310              adc[ipmt] = chargeQT1[ipmt] - chargeQT0[ipmt];
311            else
312              adc[ipmt] = 0;
313            
314
315            time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, adc[ipmt], timeCFD[ipmt] ) ;
316            
317            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
318              //    time[ipmt] = fCalib-> WalkCorrection( ipmt, Int_t(sl), timeCFD[ipmt] ) ;
319            AliDebug(10,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
320                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
321            Double_t ampMip =( (TGraph*)fAmpLED.At(ipmt))->Eval(sl);
322            Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
323            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %i\n ",ampMip,qtMip, adc[ipmt]));
324              
325            frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
326            frecpoints->SetAmp(ipmt, Float_t( ampMip)); //for cosmic &pp beam 
327            frecpoints->SetAmpLED(ipmt, Float_t(qtMip));
328              
329          }
330          else {
331            time[ipmt] = 0;
332            adc[ipmt] = 0;
333          }
334        }
335        
336        for (Int_t ipmt=0; ipmt<12; ipmt++){
337          if(time[ipmt] > 1 ) {
338            if(time[ipmt]<besttimeC){
339              besttimeC=time[ipmt]; //timeC
340              pmtBestC=ipmt;
341            }
342          }
343        }
344        for ( Int_t ipmt=12; ipmt<24; ipmt++){
345          if(time[ipmt] > 1) {
346            if(time[ipmt]<besttimeA) {
347              besttimeA=time[ipmt]; //timeA
348              pmtBestA=ipmt;}
349          }
350        }
351        if(besttimeA !=9999999)  frecpoints->SetTimeBestA(Int_t(besttimeA));
352        if( besttimeC != 9999999 ) frecpoints->SetTimeBestC(Int_t(besttimeC));
353        AliDebug(5,Form(" pmtA %i besttimeA %f ps, pmtC %i besttimeC %f #channel",
354                        pmtBestA,besttimeA, pmtBestC,  besttimeC));
355        Float_t c = 0.0299792458; // cm/ps
356        Float_t vertex = 99999;
357        if(besttimeA <9999999 && besttimeC < 9999999 ){
358          timeDiff = ( besttimeC - besttimeA) *channelWidth;
359          meanTime =  Float_t((besttimeA + besttimeC)/2.);  
360          onlineMean = onlineMean ;
361          vertex =  meanVertex - c*(timeDiff)/2.; //+ (fdZonA - fdZonC)/2; 
362          frecpoints->SetVertex(vertex);
363          frecpoints->SetMeanTime(Int_t(meanTime));
364          frecpoints->SetOnlineMean(Int_t(onlineMean));
365          AliDebug(5,Form("  timeDiff %f #channel,  meanTime %f #channel, vertex %f cm meanVertex %f online mean %i ",timeDiff, meanTime,vertex,meanVertex, onlineMean));
366          
367        }
368     } // if (else )raw data
369          recTree->Fill();
370          if(frecpoints) delete frecpoints;
371 }
372        
373        
374 //____________________________________________________________
375
376 void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
377 {
378
379   /***************************************************
380   Resonstruct digits to vertex position
381   ****************************************************/
382   
383   AliDebug(1,Form("Start FillESD T0"));
384
385   TTree *treeR = clustersTree;
386   
387    AliT0RecPoint* frecpoints= new AliT0RecPoint ();
388     if (!frecpoints) {
389     AliError("Reconstruct Fill ESD >> no recpoints found");
390     return;
391   }
392   
393   AliDebug(1,Form("Start FillESD T0"));
394   TBranch *brRec = treeR->GetBranch("T0");
395   if (brRec) {
396     brRec->SetAddress(&frecpoints);
397   }else{
398     AliError(Form("EXEC Branch T0 rec not found"));
399     return;
400   } 
401     
402     brRec->GetEntry(0);
403     Float_t amp[24], time[24];
404     Float_t  zPosition = frecpoints -> GetVertex();
405     Float_t timeStart = frecpoints -> GetMeanTime() ;
406     for ( Int_t i=0; i<24; i++) {
407       time[i] = Float_t (frecpoints -> GetTime(i)); // ps to ns
408       amp[i] = frecpoints -> GetAmp(i);
409     }
410     pESD->SetT0zVertex(zPosition); //vertex Z position 
411     pESD->SetT0(timeStart);        // interaction time 
412     pESD->SetT0time(time);         // best TOF on each PMT 
413     pESD->SetT0amplitude(amp);     // number of particles(MIPs) on each PMT
414  
415     AliDebug(1,Form(" Z position %f cm,  T0  %f ps",zPosition , timeStart));
416
417 } // vertex in 3 sigma
418
419
420
421
422
423