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