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