]> git.uio.no Git - u/mrichter/AliRoot.git/blob - T0/AliT0Reconstructor.cxx
added possibility to read TRM BC ID, calibration done by QTC amplitude
[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
38 #include <TArrayI.h>
39 #include <TGraph.h>
40 #include <TMath.h>
41 #include <Riostream.h>
42
43 ClassImp(AliT0Reconstructor)
44
45   AliT0Reconstructor:: AliT0Reconstructor(): AliReconstructor(),
46                                              fdZonA(0),
47                                              fdZonC(0),
48                                              fZposition(0),
49                                              fParam(NULL),
50                                              fAmpLEDrec(),
51                                              fQTC(0),
52                                              fAmpLED(0),
53                                              fCalib(),
54                                              fLatencyHPTDC(9000),
55                                              fLatencyL1(0),
56                                              fLatencyL1A(0),
57                                              fLatencyL1C(0),
58                                              fESDTZEROfriend(NULL)
59
60 {
61   //constructor
62
63   fParam = AliT0Parameters::Instance();
64   fParam->Init();
65  
66   for (Int_t i=0; i<24; i++){
67         TGraph* gr = fParam ->GetAmpLEDRec(i);
68         if (gr) fAmpLEDrec.AddAtAndExpand(gr,i) ; 
69           TGraph* gr1 = fParam ->GetAmpLED(i);
70           if (gr1) fAmpLED.AddAtAndExpand(gr1,i) ; 
71           TGraph* gr2 = fParam ->GetQTC(i);
72           if (gr2) fQTC.AddAtAndExpand(gr2,i) ;         
73   }
74
75   fLatencyL1 = fParam->GetLatencyL1();
76   fLatencyL1A = fParam->GetLatencyL1A();
77   fLatencyL1C = fParam->GetLatencyL1C();
78   fLatencyHPTDC = fParam->GetLatencyHPTDC();
79   AliDebug(10,Form(" LatencyL1 %f latencyL1A %f latencyL1C %f latencyHPTDC %f \n",fLatencyL1, fLatencyL1A, fLatencyL1C, fLatencyHPTDC));
80   
81   // fdZonC = TMath::Abs(fParam->GetZPositionShift("T0/C/PMT1"));
82   //fdZonA = TMath::Abs(fParam->GetZPositionShift("T0/A/PMT15"));
83   //here real Z position
84   fdZonC = TMath::Abs(fParam->GetZPosition("T0/C/PMT1"));
85   fdZonA = TMath::Abs(fParam->GetZPosition("T0/A/PMT15"));
86
87   fCalib = new AliT0Calibrator();
88   fESDTZEROfriend = new AliESDTZEROfriend();
89
90 }
91
92 //_____________________________________________________________________________
93 void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
94   
95 {
96   // T0 digits reconstruction
97   Int_t refAmp = GetRecoParam()->GetRefAmp();
98   
99   TArrayI * timeCFD = new TArrayI(24); 
100   TArrayI * timeLED = new TArrayI(24); 
101   TArrayI * chargeQT0 = new TArrayI(24); 
102   TArrayI * chargeQT1 = new TArrayI(24); 
103
104  
105   Float_t channelWidth = fParam->GetChannelWidth() ;  
106   Float_t meanVertex = fParam->GetMeanVertex();
107   Float_t c = 0.0299792; // cm/ps
108   Double32_t vertex = 9999999;
109   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
110
111   
112   AliDebug(1,Form("Start DIGITS reconstruction "));
113   
114
115   TBranch *brDigits=digitsTree->GetBranch("T0");
116   AliT0digit *fDigits = new AliT0digit() ;
117   if (brDigits) {
118     brDigits->SetAddress(&fDigits);
119   }else{
120     AliError(Form("EXEC Branch T0 digits not found"));
121      return;
122   }
123   
124   digitsTree->GetEvent(0);
125   digitsTree->GetEntry(0);
126   brDigits->GetEntry(0);
127   fDigits->GetTimeCFD(*timeCFD);
128   fDigits->GetTimeLED(*timeLED);
129   fDigits->GetQT0(*chargeQT0);
130   fDigits->GetQT1(*chargeQT1);
131   Int_t onlineMean =  fDigits->MeanTime();
132
133   Bool_t tr[5];
134   for (Int_t i=0; i<5; i++) tr[i]=false; 
135   
136   Double32_t besttimeA=999999;
137   Double32_t besttimeC=999999;
138   Int_t pmtBestA=99999;
139   Int_t pmtBestC=99999;
140   
141   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
142   clustersTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
143   
144   Float_t time[24], adc[24];
145   for (Int_t ipmt=0; ipmt<24; ipmt++) {
146     if(timeCFD->At(ipmt)>0 ){
147      if(( chargeQT1->At(ipmt) - chargeQT0->At(ipmt))>0)  
148         adc[ipmt] = chargeQT1->At(ipmt) - chargeQT0->At(ipmt);
149       else
150         adc[ipmt] = 0;
151       
152       time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, adc[ipmt],  timeCFD->At(ipmt)) ;
153              
154       Double_t sl = Double_t(timeLED->At(ipmt) - timeCFD->At(ipmt));
155       //    time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl),  timeCFD->At(ipmt) ) ;
156       AliDebug(10,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
157                        ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
158
159       Double_t ampMip =((TGraph*)fAmpLED.At(ipmt))->Eval(sl);
160       Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
161       AliDebug(10,Form("  Amlitude in MIPS LED %f ,  QTC %f in channels %f\n ",ampMip,qtMip, adc[ipmt]));
162       
163       frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
164       frecpoints->SetAmp(ipmt, Float_t( ampMip)); //for cosmic &pp beam 
165       frecpoints->SetAmpLED(ipmt, Float_t(qtMip));
166       
167     }
168     else {
169       time[ipmt] = 0;
170       adc[ipmt] = 0;
171     }
172   }
173   
174   for (Int_t ipmt=0; ipmt<12; ipmt++){
175     if(time[ipmt] > 1 ) {
176       if(time[ipmt]<besttimeC){
177         besttimeC=time[ipmt]; //timeC
178         pmtBestC=ipmt;
179       }
180     }
181   }
182   for ( Int_t ipmt=12; ipmt<24; ipmt++){
183     if(time[ipmt] > 1) {
184       if(time[ipmt]<besttimeA) {
185         besttimeA=time[ipmt]; //timeA
186         pmtBestA=ipmt;}
187     }
188   }
189   if(besttimeA < 999999) {
190     frecpoints->SetTimeBestA(Int_t(besttimeA *channelWidth - fdZonA/c));
191     tr[1]=true;
192   }
193   if( besttimeC < 999999 ) {
194     frecpoints->SetTimeBestC(Int_t(besttimeC *channelWidth - fdZonA/c));
195     tr[2]=true;
196   }
197   AliDebug(10,Form(" besttimeA %f ch,  besttimeC %f ch",besttimeA, besttimeC));
198   if(besttimeA <999999 && besttimeC < 999999 ){
199     //    timeDiff = (besttimeC - besttimeA)*channelWidth;
200     timeDiff = (besttimeA - besttimeC)*channelWidth;
201     meanTime = (besttimeA + besttimeC)/2;// * channelWidth); 
202     timeclock = meanTime *channelWidth -fdZonA/c ;
203     vertex = meanVertex - c*(timeDiff)/2.;// + (fdZonA - fdZonC)/2;
204     tr[0]=true; 
205   }
206   frecpoints->SetVertex(vertex);
207   frecpoints->SetMeanTime(meanTime);
208   frecpoints->SetT0clock(timeclock);
209   frecpoints->SetT0Trig(tr);
210
211   AliDebug(10,Form("T0 triggers %d %d %d %d %d",tr[0],tr[1],tr[2],tr[3],tr[4]));
212
213   //online mean
214   frecpoints->SetOnlineMean(Int_t(onlineMean));
215   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));
216   
217   
218
219    
220   
221   clustersTree->Fill();
222
223   delete timeCFD;
224   delete timeLED;
225   delete chargeQT0; 
226   delete chargeQT1; 
227 }
228
229
230 //_______________________________________________________________________
231
232 void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
233 {
234   // T0 raw ->
235   //
236   // reference amplitude and time ref. point from reco param
237
238   Int_t refAmp = GetRecoParam()->GetRefAmp();
239   Int_t refPoint = GetRecoParam()->GetRefPoint();
240
241   Int_t allData[110][5];
242   
243   Int_t timeCFD[24], timeLED[24], chargeQT0[24], chargeQT1[24];
244   Double32_t timeDiff=999999, meanTime=999999, timeclock=999999;
245   Float_t c = 29.9792458; // cm/ns
246   Double32_t vertex = 9999999;
247   Int_t onlineMean=0;
248   // Float_t meanVertex = fParam->GetMeanVertex();
249   Float_t meanVertex = 0;
250   for (Int_t i0=0; i0<105; i0++)
251     {
252       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0;   
253     }
254    
255   Double32_t besttimeA=9999999;
256   Double32_t besttimeC=9999999;
257   Int_t pmtBestA=99999;
258   Int_t pmtBestC=99999;
259    
260   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
261   
262   recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
263    
264   AliDebug(10," before read data ");
265   AliT0RawReader myrawreader(rawReader);
266
267   UInt_t type =rawReader->GetType();
268
269   if (!myrawreader.Next())
270     AliDebug(1,Form(" no raw data found!!"));
271   else
272     {  
273       Int_t fBCID=Int_t (rawReader->GetBCID());
274       Int_t trmbunch= myrawreader.GetTRMBunchID();
275       if(type == 7 &&( trmbunch -fBCID )==37 ) {  //only physics 
276         for (Int_t i=0; i<105; i++) {
277         for (Int_t iHit=0; iHit<5; iHit++) 
278           {
279             allData[i][iHit] = myrawreader.GetData(i,iHit);
280           }
281         }
282         Int_t ref=0;
283         if (refPoint>0) 
284           ref = allData[refPoint][0]-5000;
285         
286         Float_t channelWidth = fParam->GetChannelWidth() ;  
287         
288         //       Int_t meanT0 = fParam->GetMeanT0();
289         
290         
291         for (Int_t in=0; in<12; in++)  
292           {
293             timeCFD[in] = allData[in+1][0] ;
294             timeCFD[in+12] = allData[in+56+1][0] ;
295             timeLED[in] = allData[in+12+1][0] ;
296             timeLED[in+12] = allData[in+68+1][0] ;
297             AliDebug(10, Form(" readed i %i cfdC %i cfdA %i ledC %i ledA%i ",
298                               in, timeCFD[in],timeCFD[in+12],timeLED[in], 
299                               timeLED[in+12]));   
300           }
301         
302         for (Int_t in=0; in<12;  in++)
303           {
304             chargeQT0[in]=allData[2*in+25][0];
305             chargeQT1[in]=allData[2*in+26][0];
306           }
307         
308         for (Int_t in=12; in<24;  in++)
309           {
310             chargeQT0[in]=allData[2*in+57][0];
311             chargeQT1[in]=allData[2*in+58][0];
312           }
313         
314
315         for (Int_t in=0; in<24; in++)  
316           AliDebug(10, Form(" readed Raw %i %i %i %i %i",
317                             in, timeLED[in],timeCFD[in],chargeQT0[in],chargeQT1[in]));
318         onlineMean = allData[49][0];       
319         
320         Double32_t time[24], adc[24],  noncalibtime[24];
321         for (Int_t ipmt=0; ipmt<24; ipmt++) {
322          if(timeCFD[ipmt]>0 && timeLED[ipmt]>0){
323            //for simulated data
324              //for physics  data
325            if(( chargeQT0[ipmt] - chargeQT1[ipmt])>0)  {
326              adc[ipmt] = chargeQT0[ipmt] - chargeQT1[ipmt];
327            }
328            else
329              adc[ipmt] = 0;
330            
331
332            time[ipmt] = fCalib-> WalkCorrection(refAmp, ipmt, adc[ipmt], timeCFD[ipmt] ) ;
333            
334            Double_t sl = timeLED[ipmt] - timeCFD[ipmt];
335            // time[ipmt] = fCalib-> WalkCorrection( refAmp,ipmt, Int_t(sl), timeCFD[ipmt] ) ;
336            AliDebug(5,Form(" ipmt %i QTC %i , time in chann %i (led-cfd) %i ",
337                             ipmt, Int_t(adc[ipmt]) ,Int_t(time[ipmt]),Int_t( sl)));
338            Double_t ampMip =( (TGraph*)fAmpLED.At(ipmt))->Eval(sl);
339            Double_t qtMip = ((TGraph*)fQTC.At(ipmt))->Eval(adc[ipmt]);
340            AliDebug(10,Form("  Amlitude in MIPS LED %f ; QTC %f;  in channels %i\n ",ampMip,qtMip, adc[ipmt]));
341            //bad peak removing
342            if(sl<550) {
343              frecpoints->SetTime(ipmt, Float_t(time[ipmt]) );
344              // frecpoints->SetTime(ipmt,Double32_t(timeCFD[ipmt]));
345              frecpoints->SetAmpLED(ipmt, Double32_t( qtMip)); //for cosmic &pp beam 
346              frecpoints->SetAmp(ipmt, Double32_t(ampMip));           
347              noncalibtime[ipmt]= Double32_t (timeCFD[ipmt]);
348            }
349          }
350          else {
351            time[ipmt] = 0;
352            adc[ipmt] = 0;
353            noncalibtime[ipmt] = 0;
354          }
355        }
356        fESDTZEROfriend->SetT0timeCorr(noncalibtime) ;     
357        for (Int_t ipmt=0; ipmt<12; ipmt++){
358          if(time[ipmt] > 1 
359             && (timeLED[ipmt] - timeCFD[ipmt])<540 )
360            {
361              if(time[ipmt]<besttimeC){
362                besttimeC=time[ipmt]; //timeC
363                pmtBestC=ipmt;
364              }
365            }
366        }
367        for ( Int_t ipmt=12; ipmt<24; ipmt++)
368          {
369            if(time[ipmt] > 1 
370               && (timeLED[ipmt] - timeCFD[ipmt])<540 )
371              {
372                if(time[ipmt]<besttimeA) {
373                  besttimeA=time[ipmt]; //timeA
374                  pmtBestA=ipmt;
375                }
376              }
377          }
378        if(besttimeA < 999999) 
379          frecpoints->SetTimeBestA(besttimeA * channelWidth - 1000.*fLatencyHPTDC + 1000.*fLatencyL1A);
380        if( besttimeC < 999999 ) 
381          frecpoints->SetTimeBestC(besttimeC * channelWidth - 1000.*fLatencyHPTDC +1000.*fLatencyL1C);
382        AliDebug(10,Form(" pmtA %i besttimeA %f ps, pmtC %i besttimeC %f ps",
383                        pmtBestA,besttimeA, pmtBestC,  besttimeC));
384         if(besttimeA <999999 && besttimeC < 999999 ){
385          timeDiff = ( besttimeA - besttimeC)* 0.001* channelWidth + fLatencyL1A - fLatencyL1C;
386          timeclock = channelWidth * Float_t( besttimeA+besttimeC)/2. - 1000.*fLatencyHPTDC + 1000.*fLatencyL1;  
387          meanTime = (besttimeA+besttimeC-2.*Float_t(ref))/2.;
388          vertex =  meanVertex - c*(timeDiff)/2. ; //+ (fdZonA - fdZonC)/2; 
389         }
390       }  //if phys event       
391       AliDebug(5,Form("  timeDiff %f #channel,  meanTime %f #channel, TOFmean%f  vertex %f cm meanVertex %f online mean %i \n",timeDiff, meanTime,timeclock, vertex,meanVertex, onlineMean));
392       frecpoints->SetT0clock(timeclock);
393       frecpoints->SetVertex(vertex);
394       frecpoints->SetMeanTime(meanTime);
395       frecpoints->SetOnlineMean(Int_t(onlineMean));
396         // Set triggers
397       
398       Bool_t tr[5];
399       Int_t trchan[5]= {50,51,52,55,56};
400       for (Int_t i=0; i<5; i++) tr[i]=false; 
401       for (Int_t itr=0; itr<5; itr++) {
402         if(allData[trchan[itr]][0]>0) tr[itr]=true;
403         frecpoints->SetT0Trig(tr);
404       }
405     } // if (else )raw data
406   recTree->Fill();
407   if(frecpoints) delete frecpoints;
408 }
409   
410   
411   //____________________________________________________________
412   
413   void AliT0Reconstructor::FillESD(TTree */*digitsTree*/, TTree *clustersTree, AliESDEvent *pESD) const
414   {
415
416   /***************************************************
417   Resonstruct digits to vertex position
418   ****************************************************/
419   
420   AliDebug(1,Form("Start FillESD T0"));
421   Float_t channelWidth = fParam->GetChannelWidth() ;  
422   Float_t c = 0.0299792458; // cm/ps
423   Float_t currentVertex=0, shift=0;
424   Int_t ncont=0;
425   const AliESDVertex* vertex = pESD->GetPrimaryVertex();
426   if (!vertex)        vertex = pESD->GetPrimaryVertexSPD();
427   if (!vertex)        vertex = pESD->GetPrimaryVertexTPC();
428   if (!vertex)        vertex = pESD->GetVertex();
429
430   if (vertex) {
431     AliDebug(2, Form("Got %s (%s) from ESD: %f", 
432                     vertex->GetName(), vertex->GetTitle(), vertex->GetZ()));
433     currentVertex = vertex->GetZ();
434     
435     ncont = vertex->GetNContributors();
436     // cout<<" spdver "<<spdver<<" ncont "<<ncont<<endl;
437     if(ncont>2 ) {
438       shift = currentVertex/c;
439       //          cout<<" vertex shif "<<shift<<" vertex "<<spdver<<" IsFromVertexer3D  "<<fverSPD->IsFromVertexer3D()<<endl;
440     }
441   }
442   TTree *treeR = clustersTree;
443   
444   AliT0RecPoint* frecpoints= new AliT0RecPoint ();
445   if (!frecpoints) {
446     AliError("Reconstruct Fill ESD >> no recpoints found");
447     return;
448   }
449   
450   AliDebug(1,Form("Start FillESD T0"));
451   TBranch *brRec = treeR->GetBranch("T0");
452   if (brRec) {
453     brRec->SetAddress(&frecpoints);
454   }else{
455     AliError(Form("EXEC Branch T0 rec not found"));
456     return;
457   } 
458   
459   brRec->GetEntry(0);
460   Double32_t amp[24], time[24], ampQTC[24], timecorr[24];  
461   Double32_t* tcorr;
462   for(Int_t i=0; i<24; i++) 
463     amp[i]=time[i]=ampQTC[i]=timecorr[i]=0;
464
465   Double32_t timeClock[3];
466   Double32_t zPosition = frecpoints -> GetVertex();
467   Double32_t timeStart = frecpoints -> GetMeanTime();
468   timeClock[0] = frecpoints -> GetT0clock() ;
469   timeClock[1] = frecpoints -> GetBestTimeA() + shift;
470   timeClock[2] = frecpoints -> GetBestTimeC() - shift;
471   for ( Int_t i=0; i<24; i++) {
472     time[i] =  frecpoints -> GetTime(i); // ps to ns
473     if ( time[i] >1) {
474       amp[i] = frecpoints -> GetAmp(i);
475       ampQTC[i] = frecpoints -> AmpLED(i);
476     }
477   }
478   Int_t trig= frecpoints ->GetT0Trig();
479   pESD->SetT0Trig(trig);
480   
481   pESD->SetT0zVertex(zPosition); //vertex Z position 
482   pESD->SetT0(timeStart);        // interaction time 
483   for(Int_t i=0; i<3; i++) 
484     pESD->SetT0TOF(i,timeClock[i]);   // interaction time (ns) 
485   pESD->SetT0time(time);         // best TOF on each PMT 
486   pESD->SetT0amplitude(amp);     // number of particles(MIPs) on each PMT
487   
488   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));
489   
490   if (pESD) {
491     
492     AliESDfriend *fr = (AliESDfriend*)pESD->FindListObject("AliESDfriend");
493     if (fr) {
494       AliDebug(1, Form("Writing TZERO friend data to ESD tree"));
495
496       if (ncont>2) {
497         tcorr = fESDTZEROfriend->GetT0timeCorr();
498         for ( Int_t i=0; i<24; i++) {
499           timecorr[i]=tcorr[i];
500           if(i<12 && time[i]>1) timecorr[i] -=  shift/channelWidth;
501           if(i>11 && time[i]>1) timecorr[i] +=  shift/channelWidth;
502         }
503         fESDTZEROfriend->SetT0timeCorr( timecorr) ;
504         fESDTZEROfriend->SetT0ampLEDminCFD(amp);
505         fESDTZEROfriend->SetT0ampQTC(ampQTC);
506         fr->SetTZEROfriend(fESDTZEROfriend);
507       }//
508     }
509   }
510      
511
512
513 } // vertex in 3 sigma
514
515
516
517
518
519