Changes for usage on PROOF
[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$ */
17
18#include <Riostream.h>
19
20#include <TDirectory.h>
21
22#include "AliRunLoader.h"
dc7ca31d 23#include <AliESD.h>
24#include "AliLog.h"
c41ceaac 25#include "AliT0Loader.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#include "AliCDBLocal.h"
34#include "AliCDBStorage.h"
35#include "AliCDBManager.h"
36#include "AliCDBEntry.h"
37
38#include <TArrayI.h>
39#include <TGraph.h>
bd375212 40//#include <TGeoManager.h>
41//#include <TClonesArray.h>
42//#include <TString.h>
43//#include <TGeoPNEntry.h>
44//#include <TGeoPhysicalNode.h>
45//#include <TGeoHMatrix.h>
dc7ca31d 46
47ClassImp(AliT0Reconstructor)
48
c41ceaac 49 AliT0Reconstructor:: AliT0Reconstructor(): AliReconstructor(),
bd375212 50 fZposition(0)
c41ceaac 51
e0bba6cc 52{
c41ceaac 53 AliDebug(1,"Start reconstructor ");
74adb36a 54
55 fParam = AliT0Parameters::Instance();
56 fParam->Init();
57
58 for (Int_t i=0; i<24; i++){
59 TGraph* gr = fParam ->GetAmpLEDRec(i);
60 fAmpLEDrec.AddAtAndExpand(gr,i) ;
61 fTime0vertex[i]= fParam->GetTimeDelayDA(i);
62 }
63
c41ceaac 64}
65//____________________________________________________________________
e0bba6cc 66
c41ceaac 67AliT0Reconstructor::AliT0Reconstructor(const AliT0Reconstructor &r):
c41ceaac 68 fZposition(0)
69
70{
71 //
72 // AliT0Reconstructor copy constructor
73 //
e0bba6cc 74
c41ceaac 75 ((AliT0Reconstructor &) r).Copy(*this);
e0bba6cc 76
77}
78
c41ceaac 79//_____________________________________________________________________________
80AliT0Reconstructor &AliT0Reconstructor::operator=(const AliT0Reconstructor &r)
dc7ca31d 81{
c41ceaac 82 //
83 // Assignment operator
84 //
85
86 if (this != &r) ((AliT0Reconstructor &) r).Copy(*this);
87 return *this;
e0bba6cc 88
dc7ca31d 89}
c41ceaac 90
91//_____________________________________________________________________________
92
dc7ca31d 93void AliT0Reconstructor::Reconstruct(TTree*digitsTree, TTree*clustersTree) const
94c27e4f 94
dc7ca31d 95{
94c27e4f 96 // T0 digits reconstruction
97 // T0RecPoint writing
c41ceaac 98
74adb36a 99
c41ceaac 100 TArrayI * timeCFD = new TArrayI(24);
101 TArrayI * timeLED = new TArrayI(24);
102 TArrayI * chargeQT0 = new TArrayI(24);
103 TArrayI * chargeQT1 = new TArrayI(24);
74adb36a 104
c41ceaac 105 AliT0Calibrator *calib=new AliT0Calibrator();
dc7ca31d 106
94c27e4f 107 // Int_t mV2Mip = param->GetmV2Mip();
dc7ca31d 108 //mV2Mip = param->GetmV2Mip();
74adb36a 109 Int_t channelWidth = fParam->GetChannelWidth() ;
110 Int_t meanT0 = fParam->GetMeanT0();
94c27e4f 111
dc7ca31d 112 AliDebug(1,Form("Start DIGITS reconstruction "));
94c27e4f 113
dc7ca31d 114 TBranch *brDigits=digitsTree->GetBranch("T0");
e0bba6cc 115 AliT0digit *fDigits = new AliT0digit() ;
dc7ca31d 116 if (brDigits) {
117 brDigits->SetAddress(&fDigits);
118 }else{
119 cerr<<"EXEC Branch T0 digits not found"<<endl;
120 return;
121 }
e0bba6cc 122
c41ceaac 123 digitsTree->GetEvent(0);
124 digitsTree->GetEntry(0);
125 brDigits->GetEntry(0);
126 fDigits->GetTimeCFD(*timeCFD);
127 fDigits->GetTimeLED(*timeLED);
128 fDigits->GetQT0(*chargeQT0);
129 fDigits->GetQT1(*chargeQT1);
130
131 Float_t besttimeA=999999;
132 Float_t besttimeC=999999;
133 Int_t pmtBestA=99999;
134 Int_t pmtBestC=99999;
dc7ca31d 135 Float_t timeDiff=999999, meanTime=0;
136
e0bba6cc 137
138
94c27e4f 139 AliT0RecPoint* frecpoints= new AliT0RecPoint ();
140 clustersTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
141
dc7ca31d 142 Float_t time[24], adc[24];
143 for (Int_t ipmt=0; ipmt<24; ipmt++) {
c41ceaac 144 if(timeCFD->At(ipmt)>0 ){
145 Int_t qt0= chargeQT0->At(ipmt);
146 Int_t qt1= chargeQT1->At(ipmt);
147 if((qt1-qt0)>0) adc[ipmt] = TMath::Exp( Double_t (channelWidth*(qt1-qt0)/1000));
94c27e4f 148 // time[ipmt] = channelWidth * (calib-> WalkCorrection( ipmt,qt1 , timeCFD->At(ipmt) ) ) ;
149 time[ipmt] = calib-> WalkCorrection( ipmt,qt1 , timeCFD->At(ipmt) ) ;
c41ceaac 150
151 //LED
94c27e4f 152 Double_t sl = (timeLED->At(ipmt) - timeCFD->At(ipmt))*channelWidth;
74adb36a 153 Double_t qt=((TGraph*)fAmpLEDrec.At(ipmt))->Eval(sl/1000.);
94c27e4f 154 // frecpoints->SetTime(ipmt,time[ipmt]);
dc7ca31d 155 frecpoints->SetAmp(ipmt,adc[ipmt]);
c41ceaac 156 frecpoints->SetAmpLED(ipmt,qt);
dc7ca31d 157 }
158 else {
159 time[ipmt] = 0;
160 adc[ipmt] = 0;
161 }
162 }
94c27e4f 163
dc7ca31d 164 for (Int_t ipmt=0; ipmt<12; ipmt++){
165 if(time[ipmt] > 1 ) {
c41ceaac 166 if(time[ipmt]<besttimeC){
167 besttimeC=time[ipmt]; //timeC
168 pmtBestC=ipmt;
dc7ca31d 169 }
170 }
171 }
172 for ( Int_t ipmt=12; ipmt<24; ipmt++){
173 if(time[ipmt] > 1) {
c41ceaac 174 if(time[ipmt]<besttimeA) {
175 besttimeA=time[ipmt]; //timeA
176 pmtBestA=ipmt;}
dc7ca31d 177 }
178 }
c41ceaac 179 if(besttimeA !=999999) frecpoints->SetTimeBestA(Int_t(besttimeA));
180 if( besttimeC != 999999 ) frecpoints->SetTimeBestC(Int_t(besttimeC));
181 AliDebug(1,Form(" besttimeA %f ps, besttimeC %f ps",besttimeA, besttimeC));
dc7ca31d 182 Float_t c = 0.0299792; // cm/ps
183 Float_t vertex = 0;
c41ceaac 184 if(besttimeA !=999999 && besttimeC != 999999 ){
94c27e4f 185 timeDiff =(besttimeC - besttimeA)*channelWidth;
186 meanTime = (meanT0 - (besttimeA + besttimeC)/2) * channelWidth;
dc7ca31d 187 vertex = c*(timeDiff)/2.; //-(lenr-lenl))/2;
188 AliDebug(1,Form(" timeDiff %f ps, meanTime %f ps, vertex %f cm",timeDiff, meanTime,vertex ));
189 frecpoints->SetVertex(vertex);
190 frecpoints->SetMeanTime(Int_t(meanTime));
191
192 }
94c27e4f 193 //time in each channel as time[ipmt]-MeanTimeinThisChannel(with vertex=0)
194 for (Int_t ipmt=0; ipmt<24; ipmt++) {
195 if(time[ipmt]>1) {
74adb36a 196 time[ipmt] = (time[ipmt] - fTime0vertex[ipmt])*channelWidth;
94c27e4f 197 frecpoints->SetTime(ipmt,time[ipmt]);
198 }
199 }
dc7ca31d 200 clustersTree->Fill();
bd375212 201
202 delete timeCFD;
203 delete timeLED;
204 delete chargeQT0;
205 delete chargeQT1;
dc7ca31d 206}
207
208
c41ceaac 209//_______________________________________________________________________
210
211void AliT0Reconstructor::Reconstruct(AliRawReader* rawReader, TTree*recTree) const
212{
94c27e4f 213 // T0 raw ->
214 // T0RecPoint writing
215
216 //Q->T-> coefficients !!!! should be measured!!!
5325480c 217 Int_t allData[110][5];
94c27e4f 218
c41ceaac 219 TArrayI * timeCFD = new TArrayI(24);
220 TArrayI * timeLED = new TArrayI(24);
221 TArrayI * chargeQT0 = new TArrayI(24);
222 TArrayI * chargeQT1 = new TArrayI(24);
223
5325480c 224 for (Int_t i=0; i<110; i++) {
225 allData[i][0]=0;
226 }
227
c41ceaac 228 AliT0RawReader myrawreader(rawReader);
229 if (!myrawreader.Next())
230 AliDebug(1,Form(" no raw data found!! %i", myrawreader.Next()));
c41ceaac 231 for (Int_t i=0; i<110; i++) {
232 allData[i][0]=myrawreader.GetData(i,0);
233 }
74adb36a 234 AliT0Calibrator *calib = new AliT0Calibrator();
c41ceaac 235
74adb36a 236 // Int_t mV2Mip = param->GetmV2Mip();
c41ceaac 237 //mV2Mip = param->GetmV2Mip();
74adb36a 238 Int_t channelWidth = fParam->GetChannelWidth() ;
239 Int_t meanT0 = fParam->GetMeanT0();
240
5325480c 241 for (Int_t in=0; in<24; in++)
c41ceaac 242 {
243 timeLED->AddAt(allData[in+1][0],in);
244 timeCFD->AddAt(allData[in+25][0],in);
245 chargeQT1->AddAt(allData[in+55][0],in);
246 chargeQT0->AddAt(allData[in+79][0],in);
247 AliDebug(10, Form(" readed Raw %i %i %i %i %i", in, timeLED->At(in),timeCFD->At(in),chargeQT0->At(in),chargeQT1->At(in)));
248 }
249
250 Float_t besttimeA=999999;
251 Float_t besttimeC=999999;
252 Int_t pmtBestA=99999;
253 Int_t pmtBestC=99999;
254 Float_t timeDiff=999999, meanTime=0;
255
256
257 AliT0RecPoint* frecpoints= new AliT0RecPoint ();
258
259 recTree->Branch( "T0", "AliT0RecPoint" ,&frecpoints, 405,1);
260
261
262 Float_t time[24], adc[24];
263 for (Int_t ipmt=0; ipmt<24; ipmt++) {
264 if(timeCFD->At(ipmt)>0 ){
265 Int_t qt0= chargeQT0->At(ipmt);
266 Int_t qt1= chargeQT1->At(ipmt);
267 if((qt1-qt0)>0) adc[ipmt] = TMath::Exp( Double_t (channelWidth*(qt1-qt0)/1000));
94c27e4f 268 // time[ipmt] = channelWidth * (calib-> WalkCorrection( ipmt,qt1 , timeCFD->At(ipmt) ) ) ;
269 time[ipmt] = calib-> WalkCorrection( ipmt,qt1 , timeCFD->At(ipmt) ) ;
270 Double_t sl = (timeLED->At(ipmt) - timeCFD->At(ipmt))*channelWidth;
74adb36a 271 Double_t qt=((TGraph*)fAmpLEDrec.At(ipmt))->Eval(sl/1000.);
c41ceaac 272 frecpoints->SetTime(ipmt,time[ipmt]);
273 frecpoints->SetAmp(ipmt,adc[ipmt]);
274 frecpoints->SetAmpLED(ipmt,qt);
275 }
276 else {
277 time[ipmt] = 0;
278 adc[ipmt] = 0;
279 }
280 }
281
282 for (Int_t ipmt=0; ipmt<12; ipmt++){
283 if(time[ipmt] > 1 ) {
284 if(time[ipmt]<besttimeC){
285 besttimeC=time[ipmt]; //timeC
286 pmtBestC=ipmt;
287 }
288 }
289 }
290 for ( Int_t ipmt=12; ipmt<24; ipmt++){
291 if(time[ipmt] > 1) {
292 if(time[ipmt]<besttimeA) {
293 besttimeA=time[ipmt]; //timeA
294 pmtBestA=ipmt;}
295 }
296 }
297 if(besttimeA !=999999) frecpoints->SetTimeBestA(Int_t(besttimeA));
298 if( besttimeC != 999999 ) frecpoints->SetTimeBestC(Int_t(besttimeC));
299 AliDebug(1,Form(" besttimeA %f ps, besttimeC %f ps",besttimeA, besttimeC));
300 Float_t c = 0.0299792; // cm/ps
301 Float_t vertex = 0;
302 if(besttimeA !=999999 && besttimeC != 999999 ){
94c27e4f 303 timeDiff = (besttimeC - besttimeA)*channelWidth;
304 // meanTime = (besttimeA + besttimeC)/2.;
305 meanTime = (meanT0 - (besttimeA + besttimeC)/2) * channelWidth;
c41ceaac 306 vertex = c*(timeDiff)/2.; //-(lenr-lenl))/2;
307 AliDebug(1,Form(" timeDiff %f ps, meanTime %f ps, vertex %f cm",timeDiff, meanTime,vertex ));
308 frecpoints->SetVertex(vertex);
309 frecpoints->SetMeanTime(Int_t(meanTime));
310
311 }
94c27e4f 312 //time in each channel as time[ipmt]-MeanTimeinThisChannel(with vertex=0)
313 for (Int_t ipmt=0; ipmt<24; ipmt++) {
314 if(time[ipmt]>1) {
74adb36a 315 time[ipmt] = (time[ipmt] - fTime0vertex[ipmt])*channelWidth;
94c27e4f 316 frecpoints->SetTime(ipmt,time[ipmt]);
317 }
318 }
c41ceaac 319 recTree->Fill();
94c27e4f 320
bd375212 321
322 delete timeCFD;
323 delete timeLED;
324 delete chargeQT0;
325 delete chargeQT1;
326
c41ceaac 327}
328//____________________________________________________________
329
dc7ca31d 330void AliT0Reconstructor::FillESD(AliRunLoader* runLoader, AliESD *pESD) const
331{
332
333 /***************************************************
334 Resonstruct digits to vertex position
335 ****************************************************/
336
337
338 if (!runLoader) {
339 AliError("Reconstruct >> No run loader");
340 return;
341 }
342
343 AliDebug(1,Form("Start FillESD T0"));
344
345 AliT0Loader* pStartLoader = (AliT0Loader*) runLoader->GetLoader("T0Loader");
346
347 pStartLoader->LoadRecPoints("READ");
348
349 TTree *treeR = pStartLoader->TreeR();
350
351 AliT0RecPoint* frecpoints= new AliT0RecPoint ();
352 if (!frecpoints) {
353 AliError("Reconstruct Fill ESD >> no recpoints found");
354 return;
355 }
356
357 AliDebug(1,Form("Start FillESD T0"));
358 TBranch *brRec = treeR->GetBranch("T0");
359 if (brRec) {
360 brRec->SetAddress(&frecpoints);
361 }else{
362 cerr<<"EXEC Branch T0 rec not found"<<endl;
363 // exit(111);
364 return;
365 }
366
367 brRec->GetEntry(0);
368 Float_t timeStart, Zposition, amp[24], time[24];
369 Int_t i;
370 Zposition = frecpoints -> GetVertex();
94c27e4f 371 timeStart = frecpoints -> GetMeanTime() ;
dc7ca31d 372 for ( i=0; i<24; i++) {
94c27e4f 373 time[i] = Float_t (frecpoints -> GetTime(i)); // ps to ns
dc7ca31d 374 amp[i] = frecpoints -> GetAmp(i);
375 }
376 pESD->SetT0zVertex(Zposition); //vertex Z position
377 pESD->SetT0(timeStart); // interaction time
378 pESD->SetT0time(time); // best TOF on each PMT
379 pESD->SetT0amplitude(amp); // number of particles(MIPs) on each PMT
5325480c 380
381 AliDebug(1,Form(" Z position %f cm, T0 %f ps",Zposition , timeStart));
dc7ca31d 382
383 pStartLoader->UnloadRecPoints();
384
385} // vertex in 3 sigma
386
387
388
389
390
391