fixes initialization
[u/mrichter/AliRoot.git] / VZERO / AliVZEROReconstructor.cxx
CommitLineData
b0d2c2d3 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///////////////////////////////////////////////////////////////////////////////
19/// //
20/// class for VZERO reconstruction //
21/// //
22///////////////////////////////////////////////////////////////////////////////
23
b44c933e 24#include <TH1F.h>
25#include <TF1.h>
b6fd9c4a 26#include <TParameter.h>
b44c933e 27
35b120ff 28#include "AliRunLoader.h"
2eb38194 29#include "AliRawReader.h"
fe0adf2a 30#include "AliGRPObject.h"
31#include "AliCDBManager.h"
32#include "AliCDBStorage.h"
33#include "AliCDBEntry.h"
b0d2c2d3 34#include "AliVZEROReconstructor.h"
2eb38194 35#include "AliVZERORawStream.h"
b44c933e 36#include "AliVZEROConst.h"
b14e6eb4 37#include "AliESDEvent.h"
a055ee24 38#include "AliVZEROTriggerMask.h"
b090e6a3 39#include "AliESDfriend.h"
40#include "AliESDVZEROfriend.h"
fad64858 41#include "AliVZEROdigit.h"
e58b4e66 42#include "AliVZEROCalibData.h"
75b6bc77 43#include "AliRunInfo.h"
b44c933e 44#include "AliCTPTimeParams.h"
b0d2c2d3 45
b0d2c2d3 46ClassImp(AliVZEROReconstructor)
47
ce7090f5 48//_____________________________________________________________________________
cb2228e6 49AliVZEROReconstructor:: AliVZEROReconstructor(): AliReconstructor(),
fe0adf2a 50 fESDVZERO(0x0),
51 fESD(0x0),
52 fESDVZEROfriend(0x0),
b44c933e 53 fCalibData(NULL),
54 fTimeSlewing(NULL),
fe0adf2a 55 fCollisionMode(0),
dbf24214 56 fBeamEnergy(0.),
57 fDigitsArray(0)
ce7090f5 58{
59 // Default constructor
ce7090f5 60 // Get calibration data
61
b44c933e 62 fCalibData = GetCalibData();
63
64 AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
65 if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
66 AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
67 Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
68
7905581c 69 AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
70 if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
71 AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
72 l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
73
b44c933e 74 AliCDBEntry *entry2 = AliCDBManager::Instance()->Get("VZERO/Calib/TimeDelays");
75 if (!entry2) AliFatal("VZERO time delays are not found in OCDB !");
76 TH1F *delays = (TH1F*)entry2->GetObject();
77
78 AliCDBEntry *entry3 = AliCDBManager::Instance()->Get("VZERO/Calib/TimeSlewing");
79 if (!entry3) AliFatal("VZERO time slewing function is not found in OCDB !");
80 fTimeSlewing = (TF1*)entry3->GetObject();
81
82 for(Int_t i = 0 ; i < 64; ++i) {
83 Int_t board = AliVZEROCalibData::GetBoardNumber(i);
84 fTimeOffset[i] = (((Float_t)fCalibData->GetTriggerCountOffset(board)-
85 (Float_t)fCalibData->GetRollOver(board))*25.0+
7905581c 86 fCalibData->GetTimeOffset(i)-
b44c933e 87 l1Delay+
88 delays->GetBinContent(i+1)+
89 kV0Offset);
90 }
ce7090f5 91}
92
93
94//_____________________________________________________________________________
95AliVZEROReconstructor& AliVZEROReconstructor::operator =
96 (const AliVZEROReconstructor& /*reconstructor*/)
97{
98// assignment operator
99
100 Fatal("operator =", "assignment operator not implemented");
101 return *this;
102}
103
104//_____________________________________________________________________________
105AliVZEROReconstructor::~AliVZEROReconstructor()
106{
107// destructor
44b6212f 108
b090e6a3 109 delete fESDVZERO;
110 delete fESDVZEROfriend;
dbf24214 111 delete fDigitsArray;
35b120ff 112}
113
114//_____________________________________________________________________________
d76c31f4 115void AliVZEROReconstructor::Init()
35b120ff 116{
ef314913 117// initializer
ce7090f5 118
35b120ff 119 fESDVZERO = new AliESDVZERO;
b090e6a3 120 fESDVZEROfriend = new AliESDVZEROfriend;
fe0adf2a 121
122 GetCollisionMode(); // fCollisionMode =1 for Pb-Pb simulated data
35b120ff 123}
124
125//______________________________________________________________________
2e0ee64a 126void AliVZEROReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
35b120ff 127{
75b6bc77 128// converts RAW to digits
ef314913 129
2e0ee64a 130 if (!digitsTree) {
131 AliError("No digits tree!");
132 return;
133 }
35b120ff 134
dbf24214 135 if (!fDigitsArray)
136 fDigitsArray = new TClonesArray("AliVZEROdigit", 64);
137 digitsTree->Branch("VZERODigit", &fDigitsArray);
2e0ee64a 138
b090e6a3 139 fESDVZEROfriend->Reset();
140
2e0ee64a 141 rawReader->Reset();
142 AliVZERORawStream rawStream(rawReader);
75b6bc77 143 if (rawStream.Next()) {
1d51e5e5 144
145 Int_t aBBflagsV0A = 0;
146 Int_t aBBflagsV0C = 0;
147 Int_t aBGflagsV0A = 0;
148 Int_t aBGflagsV0C = 0;
149
150 for(Int_t iChannel=0; iChannel < 64; ++iChannel) {
151 Int_t offlineCh = rawStream.GetOfflineChannel(iChannel);
152 // ADC charge samples
153 Short_t chargeADC[AliVZEROdigit::kNClocks];
154 for(Int_t iClock=0; iClock < AliVZEROdigit::kNClocks; ++iClock) {
155 chargeADC[iClock] = rawStream.GetPedestal(iChannel,iClock);
156 }
157 // Integrator flag
158 Bool_t integrator = rawStream.GetIntegratorFlag(iChannel,AliVZEROdigit::kNClocks/2);
159 // Beam-beam and beam-gas flags
160 if(offlineCh<32) {
161 if (rawStream.GetBBFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBBflagsV0C |= (1 << offlineCh);
162 if (rawStream.GetBGFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBGflagsV0C |= (1 << offlineCh);
163 } else {
164 if (rawStream.GetBBFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBBflagsV0A |= (1 << (offlineCh-32));
165 if (rawStream.GetBGFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBGflagsV0A |= (1 << (offlineCh-32));
166 }
167 // HPTDC data (leading time and width)
168 Int_t board = AliVZEROCalibData::GetBoardNumber(offlineCh);
169 Float_t time = rawStream.GetTime(iChannel)*fCalibData->GetTimeResolution(board);
170 Float_t width = rawStream.GetWidth(iChannel)*fCalibData->GetWidthResolution(board);
171 // Add a digit
172 if(!fCalibData->IsChannelDead(iChannel)){
173 new ((*fDigitsArray)[fDigitsArray->GetEntriesFast()])
b47d88ce 174 AliVZEROdigit(offlineCh, time,
1d51e5e5 175 width,integrator,
176 chargeADC);
177 }
178
179 // Filling the part of esd friend object that is available only for raw data
180 fESDVZEROfriend->SetBBScalers(offlineCh,rawStream.GetBBScalers(iChannel));
181 fESDVZEROfriend->SetBGScalers(offlineCh,rawStream.GetBGScalers(iChannel));
182 for (Int_t iBunch = 0; iBunch < AliESDVZEROfriend::kNBunches; iBunch++) {
183 fESDVZEROfriend->SetChargeMB(offlineCh,iBunch,rawStream.GetChargeMB(iChannel,iBunch));
184 fESDVZEROfriend->SetIntMBFlag(offlineCh,iBunch,rawStream.GetIntMBFlag(iChannel,iBunch));
185 fESDVZEROfriend->SetBBMBFlag(offlineCh,iBunch,rawStream.GetBBMBFlag(iChannel,iBunch));
186 fESDVZEROfriend->SetBGMBFlag(offlineCh,iBunch,rawStream.GetBGMBFlag(iChannel,iBunch));
187 }
188
189 }
190
191 // Filling the global part of esd friend object that is available only for raw data
192 fESDVZEROfriend->SetTriggerInputs(rawStream.GetTriggerInputs());
193 fESDVZEROfriend->SetTriggerInputsMask(rawStream.GetTriggerInputsMask());
194
195 for(Int_t iScaler = 0; iScaler < AliESDVZEROfriend::kNScalers; iScaler++)
196 fESDVZEROfriend->SetTriggerScalers(iScaler,rawStream.GetTriggerScalers(iScaler));
197
198 for(Int_t iBunch = 0; iBunch < AliESDVZEROfriend::kNBunches; iBunch++)
199 fESDVZEROfriend->SetBunchNumbersMB(iBunch,rawStream.GetBunchNumbersMB(iBunch));
b090e6a3 200
1d51e5e5 201 // Store the BB and BG flags in the digits tree (user info)
202 digitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0A",aBBflagsV0A));
203 digitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0C",aBBflagsV0C));
204 digitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0A",aBGflagsV0A));
205 digitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0C",aBGflagsV0C));
b090e6a3 206
1d51e5e5 207 digitsTree->Fill();
2e0ee64a 208 }
dbf24214 209
210 fDigitsArray->Clear();
1d51e5e5 211}
2e0ee64a 212
213//______________________________________________________________________
214void AliVZEROReconstructor::FillESD(TTree* digitsTree, TTree* /*clustersTree*/,
b14e6eb4 215 AliESDEvent* esd) const
2e0ee64a 216{
75b6bc77 217// fills multiplicities to the ESD - pedestal is now subtracted
fe0adf2a 218
2e0ee64a 219 if (!digitsTree) {
fe0adf2a 220 AliError("No digits tree!");
221 return;
2e0ee64a 222 }
a055ee24 223
2e0ee64a 224 TBranch* digitBranch = digitsTree->GetBranch("VZERODigit");
dbf24214 225 digitBranch->SetAddress(&fDigitsArray);
35b120ff 226
44b6212f 227 Float_t mult[64];
db0db003 228 Float_t adc[64];
229 Float_t time[64];
230 Float_t width[64];
b6fd9c4a 231 Bool_t aBBflag[64];
232 Bool_t aBGflag[64];
d0502ab2 233
35b120ff 234 for (Int_t i=0; i<64; i++){
db0db003 235 adc[i] = 0.0;
d0502ab2 236 mult[i] = 0.0;
b44c933e 237 time[i] = kInvalidTime;
db0db003 238 width[i] = 0.0;
b6fd9c4a 239 aBBflag[i] = kFALSE;
240 aBGflag[i] = kFALSE;
a055ee24 241 }
35b120ff 242
b6fd9c4a 243 Int_t aBBflagsV0A = 0;
244 Int_t aBBflagsV0C = 0;
245 Int_t aBGflagsV0A = 0;
246 Int_t aBGflagsV0C = 0;
247
248 if (digitsTree->GetUserInfo()->FindObject("BBflagsV0A")) {
249 aBBflagsV0A = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BBflagsV0A"))->GetVal();
250 }
251 else
252 AliWarning("V0A beam-beam flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
253
254 if (digitsTree->GetUserInfo()->FindObject("BBflagsV0C")) {
255 aBBflagsV0C = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BBflagsV0C"))->GetVal();
256 }
257 else
258 AliWarning("V0C beam-beam flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
259
260 if (digitsTree->GetUserInfo()->FindObject("BGflagsV0A")) {
261 aBGflagsV0A = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BGflagsV0A"))->GetVal();
262 }
263 else
264 AliWarning("V0A beam-gas flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
265
266 if (digitsTree->GetUserInfo()->FindObject("BGflagsV0C")) {
267 aBGflagsV0C = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BGflagsV0C"))->GetVal();
268 }
269 else
270 AliWarning("V0C beam-gas flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
271
1d51e5e5 272 // Beam-beam and beam-gas flags (hardware)
273 for (Int_t iChannel = 0; iChannel < 64; ++iChannel) {
274 if(iChannel < 32) {
275 aBBflag[iChannel] = (aBBflagsV0C >> iChannel) & 0x1;
276 aBGflag[iChannel] = (aBGflagsV0C >> iChannel) & 0x1;
277 }
278 else {
279 aBBflag[iChannel] = (aBBflagsV0A >> (iChannel-32)) & 0x1;
280 aBGflag[iChannel] = (aBGflagsV0A >> (iChannel-32)) & 0x1;
281 }
282 }
b6fd9c4a 283
2e0ee64a 284 Int_t nEntries = (Int_t)digitsTree->GetEntries();
35b120ff 285 for (Int_t e=0; e<nEntries; e++) {
2e0ee64a 286 digitsTree->GetEvent(e);
35b120ff 287
dbf24214 288 Int_t nDigits = fDigitsArray->GetEntriesFast();
35b120ff 289
290 for (Int_t d=0; d<nDigits; d++) {
dbf24214 291 AliVZEROdigit* digit = (AliVZEROdigit*) fDigitsArray->At(d);
1d51e5e5 292 Int_t pmNumber = digit->PMNumber();
293
294 // Pedestal retrieval and suppression
295 Bool_t integrator = digit->Integrator();
296 Float_t maxadc = 0;
297 Int_t imax = -1;
298 Float_t adcPedSub[AliVZEROdigit::kNClocks];
299 for(Int_t iClock=0; iClock < AliVZEROdigit::kNClocks; ++iClock) {
300 Short_t charge = digit->ChargeADC(iClock);
301 Bool_t iIntegrator = (iClock%2 == 0) ? integrator : !integrator;
302 Int_t k = pmNumber + 64*iIntegrator;
303 adcPedSub[iClock] = (Float_t)charge - fCalibData->GetPedestal(k);
304 if(adcPedSub[iClock] <= GetRecoParam()->GetNSigmaPed()*fCalibData->GetSigma(k)) {
305 adcPedSub[iClock] = 0;
306 continue;
307 }
308 if(iClock < GetRecoParam()->GetStartClock() || iClock > GetRecoParam()->GetEndClock()) continue;
309 if(adcPedSub[iClock] > maxadc) {
310 maxadc = adcPedSub[iClock];
311 imax = iClock;
312 }
b6fd9c4a 313 }
1d51e5e5 314
315 if (imax != -1) {
316 Int_t start = imax - GetRecoParam()->GetNPreClocks();
317 if (start < 0) start = 0;
318 Int_t end = imax + GetRecoParam()->GetNPostClocks();
319 if (end > 20) end = 20;
320 for(Int_t iClock = start; iClock <= end; iClock++) {
321 adc[pmNumber] += adcPedSub[iClock];
322 }
b6fd9c4a 323 }
324
1d51e5e5 325 // HPTDC leading time and width
326 // Correction for slewing and various time delays
327 time[pmNumber] = CorrectLeadingTime(pmNumber,digit->Time(),adc[pmNumber]);
328 width[pmNumber] = digit->Width();
329
330 if (adc[pmNumber] > 0) AliDebug(1,Form("PM = %d ADC = %f TDC %f (%f) Int %d (%d %d %d %d %d) %f %f %f %f %d %d",pmNumber, adc[pmNumber],
331 digit->Time(),time[pmNumber],
b6fd9c4a 332 integrator,
333 digit->ChargeADC(8),digit->ChargeADC(9),digit->ChargeADC(10),
334 digit->ChargeADC(11),digit->ChargeADC(12),
335 fCalibData->GetPedestal(pmNumber),fCalibData->GetSigma(pmNumber),
336 fCalibData->GetPedestal(pmNumber+64),fCalibData->GetSigma(pmNumber+64),
337 aBBflag[pmNumber],aBGflag[pmNumber]));
75b6bc77 338
1d51e5e5 339 mult[pmNumber] = adc[pmNumber]*fCalibData->GetMIPperADC(pmNumber);
340
341 // Fill ESD friend object
342 for (Int_t iEv = 0; iEv < AliESDVZEROfriend::kNEvOfInt; iEv++) {
343 fESDVZEROfriend->SetPedestal(pmNumber,iEv,(Float_t)digit->ChargeADC(iEv));
344 fESDVZEROfriend->SetIntegratorFlag(pmNumber,iEv,(iEv%2 == 0) ? integrator : !integrator);
345 fESDVZEROfriend->SetBBFlag(pmNumber,iEv,aBBflag[pmNumber]);
346 fESDVZEROfriend->SetBGFlag(pmNumber,iEv,aBGflag[pmNumber]);
347 }
348 fESDVZEROfriend->SetTime(pmNumber,digit->Time());
349 fESDVZEROfriend->SetWidth(pmNumber,digit->Width());
350
35b120ff 351 } // end of loop over digits
35b120ff 352 } // end of loop over events in digits tree
6c6d6114 353
b44c933e 354 fESDVZERO->SetBit(AliESDVZERO::kCorrectedLeadingTime,kTRUE);
6c6d6114 355 fESDVZERO->SetMultiplicity(mult);
28fdf12c 356 fESDVZERO->SetADC(adc);
357 fESDVZERO->SetTime(time);
d0502ab2 358 fESDVZERO->SetWidth(width);
89a39339 359 fESDVZERO->SetBit(AliESDVZERO::kOnlineBitsFilled,kTRUE);
b6fd9c4a 360 fESDVZERO->SetBBFlag(aBBflag);
361 fESDVZERO->SetBGFlag(aBGflag);
a055ee24 362
b44c933e 363 // now fill the V0 decision and channel flags
364 {
365 AliVZEROTriggerMask triggerMask;
366 triggerMask.FillMasks(fESDVZERO, fCalibData, fTimeSlewing);
367 }
a055ee24 368
2e0ee64a 369 if (esd) {
44b6212f 370 AliDebug(1, Form("Writing VZERO data to ESD tree"));
371 esd->SetVZEROData(fESDVZERO);
2e0ee64a 372 }
b090e6a3 373
374 if (esd) {
fe0adf2a 375 AliESDfriend *fr = (AliESDfriend*)esd->FindListObject("AliESDfriend");
376 if (fr) {
377 AliDebug(1, Form("Writing VZERO friend data to ESD tree"));
378 fr->SetVZEROfriend(fESDVZEROfriend);
b090e6a3 379 }
380 }
dbf24214 381
382 fDigitsArray->Clear();
35b120ff 383}
384
ce7090f5 385//_____________________________________________________________________________
386AliCDBStorage* AliVZEROReconstructor::SetStorage(const char *uri)
387{
ef314913 388// Sets the storage
389
ce7090f5 390 Bool_t deleteManager = kFALSE;
391
392 AliCDBManager *manager = AliCDBManager::Instance();
393 AliCDBStorage *defstorage = manager->GetDefaultStorage();
394
395 if(!defstorage || !(defstorage->Contains("VZERO"))){
396 AliWarning("No default storage set or default storage doesn't contain VZERO!");
397 manager->SetDefaultStorage(uri);
398 deleteManager = kTRUE;
399 }
400
401 AliCDBStorage *storage = manager->GetDefaultStorage();
402
403 if(deleteManager){
44b6212f 404 AliCDBManager::Instance()->UnsetDefaultStorage();
405 defstorage = 0; // the storage is killed by AliCDBManager::Instance()->Destroy()
ce7090f5 406 }
407
408 return storage;
409}
410
fe0adf2a 411//____________________________________________________________________________
412void AliVZEROReconstructor::GetCollisionMode()
413{
75b6bc77 414 // Retrieval of collision mode
fe0adf2a 415
75b6bc77 416 TString beamType = GetRunInfo()->GetBeamType();
fe0adf2a 417 if(beamType==AliGRPObject::GetInvalidString()){
75b6bc77 418 AliError("VZERO cannot retrieve beam type");
fe0adf2a 419 return;
420 }
421
75b6bc77 422 if( (beamType.CompareTo("P-P") ==0) || (beamType.CompareTo("p-p") ==0) ){
423 fCollisionMode=0;
424 }
425 else if( (beamType.CompareTo("Pb-Pb") ==0) || (beamType.CompareTo("A-A") ==0) ){
426 fCollisionMode=1;
fe0adf2a 427 }
fe0adf2a 428
75b6bc77 429 fBeamEnergy = GetRunInfo()->GetBeamEnergy();
fe0adf2a 430 if(fBeamEnergy==AliGRPObject::GetInvalidFloat()) {
75b6bc77 431 AliError("Missing value for the beam energy ! Using 0");
fe0adf2a 432 fBeamEnergy = 0.;
433 }
434
75b6bc77 435 AliDebug(1,Form("\n ++++++ Beam type and collision mode retrieved as %s %d @ %1.3f GeV ++++++\n\n",beamType.Data(), fCollisionMode, fBeamEnergy));
fe0adf2a 436
437}
438
ce7090f5 439//_____________________________________________________________________________
440AliVZEROCalibData* AliVZEROReconstructor::GetCalibData() const
441{
ef314913 442 // Gets calibration object for VZERO set
ce7090f5 443
c0b82b5a 444 AliCDBManager *man = AliCDBManager::Instance();
ce7090f5 445
c0b82b5a 446 AliCDBEntry *entry=0;
447
448 entry = man->Get("VZERO/Calib/Data");
449
c0b82b5a 450 AliVZEROCalibData *calibdata = 0;
451
452 if (entry) calibdata = (AliVZEROCalibData*) entry->GetObject();
94a600a1 453 if (!calibdata) AliFatal("No calibration data from calibration database !");
ce7090f5 454
455 return calibdata;
456}
44b6212f 457
b44c933e 458Float_t AliVZEROReconstructor::CorrectLeadingTime(Int_t i, Float_t time, Float_t adc) const
459{
460 // Correct the leading time
461 // for slewing effect and
462 // misalignment of the channels
463 if (time < 1e-6) return kInvalidTime;
464
465 // Channel alignment and general offset subtraction
466 if (i < 32) time -= kV0CDelayCables;
467 time -= fTimeOffset[i];
468
469 // In case of pathological signals
470 if (adc < 1e-6) return time;
471
472 // Slewing correction
473 Float_t thr = fCalibData->GetDiscriThr(i);
474 time -= fTimeSlewing->Eval(adc/thr);
475
476 return time;
477}