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