1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /* $Id: AliADDigitizer.cxx $ */
18 ///_________________________________________________________________________
20 /// This class constructs Digits out of Hits
24 // --- Standard library ---
26 // --- ROOT system ---
30 #include <TGeoManager.h>
31 #include <TGeoPhysicalNode.h>
32 #include <AliGeomManager.h>
37 // --- AliRoot header files ---
39 #include "AliDetector.h"
42 #include "AliADConst.h"
43 #include "AliRunLoader.h"
44 #include "AliLoader.h"
45 #include "AliGRPObject.h"
46 #include "AliDigitizationInput.h"
47 #include "AliCDBManager.h"
48 #include "AliCDBStorage.h"
49 #include "AliCDBEntry.h"
50 #include "AliADCalibData.h"
51 #include "AliCTPTimeParams.h"
52 #include "AliLHCClockPhase.h"
53 #include "AliADdigit.h"
54 #include "AliADDigitizer.h"
55 #include "AliADSDigit.h"
56 #include "AliADTriggerSimulator.h"
58 ClassImp(AliADDigitizer)
60 //____________________________________________________________________________
61 AliADDigitizer::AliADDigitizer()
63 fCalibData(GetCalibData()),
68 fSinglePhESpectrum(NULL),
73 // default constructor
74 // Initialize OCDB and containers used in the digitization
79 //____________________________________________________________________________
80 AliADDigitizer::AliADDigitizer(AliAD *AD, DigiTask_t task)
82 fCalibData(GetCalibData()),
87 fSinglePhESpectrum(NULL),
93 // Initialize OCDB and containers used in the digitization
98 //____________________________________________________________________________
99 AliADDigitizer::AliADDigitizer(AliDigitizationInput* digInput)
100 :AliDigitizer(digInput),
101 fCalibData(GetCalibData()),
106 fSinglePhESpectrum(NULL),
112 // Initialize OCDB and containers used in the digitization
117 //____________________________________________________________________________
118 AliADDigitizer::~AliADDigitizer()
136 if (fSinglePhESpectrum) {
137 delete fSinglePhESpectrum;
138 fSinglePhESpectrum = NULL;
141 for(Int_t i = 0 ; i < 16; ++i) {
142 if (fTime[i]) delete [] fTime[i];
146 //____________________________________________________________________________
147 Bool_t AliADDigitizer::Init()
149 // Initialises the digitizer
150 // Initialize OCDB and containers used in the digitization
152 // check if the digitizer was already initialized
153 if (fSignalShape) return kTRUE;
155 fSignalShape = new TF1("ADSignalShape",this,&AliADDigitizer::SignalShape,0,200,6,"AliADDigitizer","SignalShape");
156 // fSignalShape->SetParameters(0,1.57345e1,-4.25603e-1,2.9,6.40982,3.69339e-01);
157 // fSignalShape->SetParameters(1.34330e+00,1.13007e+02,-4.95705e-01,
158 // 3.68911e+00,1.01040e+00, 3.94675e-01);
159 fSignalShape->SetParameters(-1.07335e+00,2.16002e+01,-1.26133e-01,
160 1.41619e+00,5.50334e-01,3.86111e-01);
161 fPMResponse = new TF1("ADPMResponse",this,&AliADDigitizer::PMResponse,-kPMRespTime,2.*kPMRespTime,0,"AliADDigitizer","PMResponse");
162 fSinglePhESpectrum = new TF1("ADSinglePhESpectrum",this,&AliADDigitizer::SinglePhESpectrum,0,20,0,"AliADDigitizer","SinglePhESpectrum");
164 // Now get the CTP L0->L1 delay
165 AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
166 if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
167 AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
168 Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
170 AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
171 if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
172 AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
173 l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
175 AliCDBEntry *entry2 = AliCDBManager::Instance()->Get("AD/Calib/TimeDelays");
176 if (!entry2) AliFatal("AD time delays are not found in OCDB !");
177 TH1F *delays = (TH1F*)entry2->GetObject();
179 AliCDBEntry *entry3 = AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
180 if (!entry3) AliFatal("LHC clock-phase shift is not found in OCDB !");
181 AliLHCClockPhase *phase = (AliLHCClockPhase*)entry3->GetObject();
183 for(Int_t i = 0 ; i < 16; ++i) {
185 for(Int_t j = 0; j < kNClocks; ++j) fAdc[i][j] = 0;
186 fLeadingTime[i] = fTimeWidth[i] = 0;
188 fPmGain[i] = fCalibData->GetGain(i);
190 fAdcPedestal[i][0] = fCalibData->GetPedestal(i);
191 fAdcSigma[i][0] = fCalibData->GetSigma(i);
192 fAdcPedestal[i][1] = fCalibData->GetPedestal(i+16);
193 fAdcSigma[i][1] = fCalibData->GetSigma(i+16);
195 Int_t board = AliADCalibData::GetBoardNumber(i);
196 fNBins[i] = TMath::Nint(((Float_t)(fCalibData->GetMatchWindow(board)+1)*25.0+
197 (Float_t)kMaxTDCWidth*fCalibData->GetWidthResolution(board))/
198 fCalibData->GetTimeResolution(board));
199 fNBinsLT[i] = TMath::Nint(((Float_t)(fCalibData->GetMatchWindow(board)+1)*25.0)/
200 fCalibData->GetTimeResolution(board));
201 fBinSize[i] = fCalibData->GetTimeResolution(board);
203 fHptdcOffset[i] = (((Float_t)fCalibData->GetRollOver(board)-
204 (Float_t)fCalibData->GetTriggerCountOffset(board))*25.0
205 //+fCalibData->GetTimeOffset(i)
206 +delays->GetBinContent(16-i) - 250);
208 //-phase->GetMeanPhase()
211 fClockOffset[i] = (((Float_t)fCalibData->GetRollOver(board)-
212 (Float_t)fCalibData->GetTriggerCountOffset(board))*25.0
213 +fCalibData->GetTimeOffset(i) - 250);
217 fTime[i] = new Float_t[fNBins[i]];
218 memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
221 //std::cout<<"AD: "<<" fNBins = "<<fNBins[0]<<" fNBinsLT = "<<fNBinsLT[0]<<" fHptdcOffset = "<<fHptdcOffset[0]<<" fClockOffset = "<<fClockOffset[0]<<std::endl;
226 //____________________________________________________________________________
227 void AliADDigitizer::Digitize(Option_t* /*option*/)
229 // Creates digits from hits
232 if (fAD && !fDigInput) {
233 AliLoader *loader = fAD->GetLoader();
235 AliError("Can not get AD Loader via AliAD object!");
238 AliRunLoader* runLoader = AliRunLoader::Instance();
239 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); ++iEvent) {
240 runLoader->GetEvent(iEvent);
241 if (fTask == kHits2Digits) {
248 WriteSDigits(loader);
252 else if (fDigInput) {
255 AliRunLoader *currentLoader = AliRunLoader::GetRunLoader(fDigInput->GetOutputFolderName());
256 AliLoader *loader = currentLoader->GetLoader("ADLoader");
258 AliError("Cannot get AD Loader via RunDigitizer!");
264 AliFatal("Invalid digitization task! Exiting!");
268 //____________________________________________________________________________
269 void AliADDigitizer::DigitizeHits()
271 // Digitize the hits to the level of
272 // SDigits (fTime arrays)
274 for(Int_t i = 0 ; i < 16; ++i) {
275 memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
276 fLabels[i][0] = fLabels[i][1] = fLabels[i][2] = -1;
278 Float_t integral = fPMResponse->Integral(-kPMRespTime,2.*kPMRespTime);
279 Float_t meansPhE = fSinglePhESpectrum->Mean(0,20);
281 AliLoader* loader = fAD->GetLoader();
283 AliError("Can not get AD Loader!");
287 TTree* treeH = loader->TreeH();
289 AliError("Cannot get TreeH!");
292 TClonesArray* hits = fAD->Hits();
294 // Now makes Digits from hits
295 Int_t nTracks = (Int_t) treeH->GetEntries();
296 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
298 treeH->GetEvent(iTrack);
299 Int_t nHits = hits->GetEntriesFast();
300 for (Int_t iHit = 0; iHit < nHits; iHit++) {
301 AliADhit* hit = (AliADhit *)hits->UncheckedAt(iHit);
302 Int_t nPhot = hit->GetNphot();
303 Int_t pmt = hit->GetCell();//One PM per cell in AD
304 if (pmt < 0) continue;
305 Int_t trackLabel = hit->GetTrack();
306 for(Int_t l = 0; l < 3; ++l) {
307 if (fLabels[pmt][l] < 0) {
308 fLabels[pmt][l] = trackLabel;
312 Float_t dt_scintillator = gRandom->Gaus(0,kIntTimeRes);
313 Float_t t = dt_scintillator + hit->GetTof();
314 t += fHptdcOffset[pmt];
316 Float_t charge = nPhot*fPmGain[pmt]*fBinSize[pmt]/integral;
318 Float_t tPhE = t + fSignalShape->GetRandom(0,fBinSize[pmt]*Float_t(fNBins[pmt]));
320 Int_t firstBin = TMath::Max(0,(Int_t)((tPhE-kPMRespTime)/fBinSize[pmt]));
321 Int_t lastBin = TMath::Min(fNBins[pmt]-1,(Int_t)((tPhE+2.*kPMRespTime)/fBinSize[pmt]));
322 //std::cout<<"Bins: "<<firstBin*fBinSize[pmt]<<" - "<<lastBin*fBinSize[pmt]<<std::endl;
323 //std::cout<<"Bins: "<<firstBin<<" - "<<lastBin<<std::endl;
325 for(Int_t iBin = firstBin; iBin <= lastBin; ++iBin) {
326 Float_t tempT = fBinSize[pmt]*(0.5+iBin)-tPhE;
327 fTime[pmt][iBin] += charge*fPMResponse->Eval(tempT);
331 loader->UnloadHits();
334 //____________________________________________________________________________
335 void AliADDigitizer::DigitizeSDigits()
337 // Digitize the fTime arrays (SDigits) to the level of
338 // Digits (fAdc arrays)
339 for(Int_t i = 0 ; i < 16; ++i) {
340 for(Int_t j = 0; j < kNClocks; ++j) fAdc[i][j] = 0;
341 fLeadingTime[i] = fTimeWidth[i] = 0;
344 Float_t maximum = 0.9*fSignalShape->GetMaximum(0,200); // Not exact, one needs to do this on the convoluted
345 Float_t integral2 = fSignalShape->Integral(0,200); // function. Anyway the effect is small <10% on the 2.5 ADC thr
346 for (Int_t ipmt = 0; ipmt < 16; ++ipmt) {
347 Float_t thr = fCalibData->GetCalibDiscriThr(ipmt,kFALSE)*kChargePerADC*maximum*fBinSize[ipmt]/integral2;
349 Bool_t ltFound = kFALSE, ttFound = kFALSE;
350 for (Int_t iBin = 0; iBin < fNBins[ipmt]; ++iBin) {
351 Float_t t = fBinSize[ipmt]*Float_t(iBin);
352 if (fTime[ipmt][iBin] > thr) {
353 if (!ltFound && (iBin < fNBinsLT[ipmt])) {
355 fLeadingTime[ipmt] = t;
362 fTimeWidth[ipmt] = t - fLeadingTime[ipmt];
366 Float_t tadc = t - fClockOffset[ipmt];
367 Int_t clock = kNClocks/2 - Int_t(tadc/25.0);
368 if (clock >= 0 && clock < kNClocks)
369 fAdc[ipmt][clock] += fTime[ipmt][iBin]/kChargePerADC;
371 AliDebug(1,Form("Channel %d Offset %f Time %f",ipmt,fClockOffset[ipmt],fLeadingTime[ipmt]));
372 Int_t board = AliADCalibData::GetBoardNumber(ipmt);
373 if (ltFound && ttFound) {
374 fTimeWidth[ipmt] = fCalibData->GetWidthResolution(board)*
375 Float_t(Int_t(fTimeWidth[ipmt]/fCalibData->GetWidthResolution(board)));
376 if (fTimeWidth[ipmt] < Float_t(kMinTDCWidth)*fCalibData->GetWidthResolution(board))
377 fTimeWidth[ipmt] = Float_t(kMinTDCWidth)*fCalibData->GetWidthResolution(board);
378 if (fTimeWidth[ipmt] > Float_t(kMaxTDCWidth)*fCalibData->GetWidthResolution(board))
379 fTimeWidth[ipmt] = Float_t(kMaxTDCWidth)*fCalibData->GetWidthResolution(board);
383 fEvenOrOdd = gRandom->Integer(2);
384 for (Int_t j=0; j<16; ++j){
385 for (Int_t iClock = 0; iClock < kNClocks; ++iClock) {
386 Int_t integrator = (iClock + fEvenOrOdd) % 2;
387 AliDebug(1,Form("ADC %d %d %f",j,iClock,fAdc[j][iClock]));
388 fAdc[j][iClock] += gRandom->Gaus(fAdcPedestal[j][integrator], fAdcSigma[j][integrator]);
391 //Fill BB and BG flags in trigger simulator
392 AliADTriggerSimulator * triggerSimulator = new AliADTriggerSimulator();
393 triggerSimulator->FillFlags(fBBFlag,fBGFlag,fLeadingTime);
397 //____________________________________________________________________________
398 void AliADDigitizer::ReadSDigits()
400 // Read SDigits which are then to precessed
401 // in the following method
402 for(Int_t i = 0 ; i < 16; ++i) {
403 memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
404 fLabels[i][0] = fLabels[i][1] = fLabels[i][2] = -1;
407 // Loop over input files
408 Int_t nFiles= fDigInput->GetNinputs();
409 for (Int_t inputFile = 0; inputFile < nFiles; inputFile++) {
410 // Get the current loader
411 AliRunLoader* currentLoader =
412 AliRunLoader::GetRunLoader(fDigInput->GetInputFolderName(inputFile));
414 AliLoader *loader = currentLoader->GetLoader("ADLoader");
415 loader->LoadSDigits("READ");
417 // Get the tree of summable digits
418 TTree* sdigitsTree = loader->TreeS();
420 AliError("No sdigit tree from digInput");
425 TBranch* sdigitsBranch = sdigitsTree->GetBranch("ADSDigit");
426 if (!sdigitsBranch) {
427 AliError("Failed to get sdigit branch");
431 // Set the branch address
432 TClonesArray *sdigitsArray = NULL;
433 sdigitsBranch->SetAddress(&sdigitsArray);
435 // Sum contributions from the sdigits
436 // Get number of entries in the tree
437 Int_t nentries = Int_t(sdigitsBranch->GetEntries());
438 for (Int_t entry = 0; entry < nentries; ++entry) {
439 sdigitsBranch->GetEntry(entry);
440 // Get the number of sdigits
441 Int_t nsdigits = sdigitsArray->GetEntries();
443 for (Int_t sdigit = 0; sdigit < nsdigits; sdigit++) {
444 AliADSDigit* sDigit = static_cast<AliADSDigit*>(sdigitsArray->UncheckedAt(sdigit));
445 Int_t pmNumber = sDigit->PMNumber();
446 Int_t nbins = sDigit->GetNBins();
447 if (nbins != fNBins[pmNumber]) {
448 AliError(Form("Incompatible number of bins between digitizer (%d) and sdigit (%d) for PM %d! Skipping sdigit!",
449 fNBins[pmNumber],nbins,pmNumber));
453 Float_t *charges = sDigit->GetCharges();
454 for(Int_t iBin = 0; iBin < nbins; ++iBin) fTime[pmNumber][iBin] += charges[iBin];
456 Int_t *labels = sDigit->GetTracks();
458 for(Int_t i = 0; i < 3; ++i) {
459 if (fLabels[pmNumber][i] < 0) {
460 if (labels[j] < 0) break;
461 fLabels[pmNumber][i] = labels[j];
467 loader->UnloadSDigits();
472 //____________________________________________________________________________
473 void AliADDigitizer::WriteDigits(AliLoader *loader)
475 // Take fAdc arrays filled by the previous
476 // method and produce and add digits to the digit Tree
478 loader->LoadDigits("UPDATE");
480 if (!loader->TreeD()) loader->MakeTree("D");
481 loader->MakeDigitsContainer();
482 TTree* treeD = loader->TreeD();
484 treeD->Branch("ADDigit", &fDigits);
486 Short_t *chargeADC = new Short_t[kNClocks];
487 for (Int_t i=0; i<16; i++) {
488 for (Int_t j = 0; j < kNClocks; ++j) {
489 Int_t tempadc = Int_t(fAdc[i][j]);
490 if (tempadc > 1023) tempadc = 1023;
491 chargeADC[j] = tempadc;
493 AddDigit(i, fLeadingTime[i], fTimeWidth[i], Bool_t((10+fEvenOrOdd)%2), chargeADC, fBBFlag[i], fBGFlag[i], fLabels[i]);
498 loader->WriteDigits("OVERWRITE");
499 loader->UnloadDigits();
503 //____________________________________________________________________________
504 void AliADDigitizer::WriteSDigits(AliLoader *loader)
506 // Take fTime arrays filled by the previous
507 // method and produce and add sdigits to the sdigit Tree
509 loader->LoadSDigits("UPDATE");
511 if (!loader->TreeS()) loader->MakeTree("S");
512 loader->MakeSDigitsContainer();
513 TTree* treeS = loader->TreeS();
515 treeS->Branch("ADSDigit", &fDigits);
516 //fAD->MakeBranchInTree(treeS,"AD",&fDigits,8000,"");
518 for (Int_t ipmt = 0; ipmt < 16; ++ipmt) {
519 AddSDigit(ipmt,fNBins[ipmt],fTime[ipmt],fLabels[ipmt]);
523 loader->WriteSDigits("OVERWRITE");
524 loader->UnloadSDigits();
530 //____________________________________________________________________________
531 void AliADDigitizer::AddDigit(Int_t pmnumber, Float_t time, Float_t width, Bool_t integrator, Short_t *chargeADC, Bool_t bbFlag, Bool_t bgFlag, Int_t *labels)
536 TClonesArray &ldigits = *fDigits;
538 new(ldigits[fNdigits++]) AliADdigit(pmnumber,time,width,integrator,chargeADC,bbFlag,bgFlag,labels);
541 //____________________________________________________________________________
542 void AliADDigitizer::AddSDigit(Int_t pmnumber, Int_t nbins, Float_t *charges, Int_t *labels)
547 TClonesArray &ldigits = *fDigits;
549 new(ldigits[fNdigits++]) AliADSDigit(pmnumber,nbins,charges,labels);
552 //____________________________________________________________________________
553 void AliADDigitizer::ResetDigits()
559 if (fDigits) fDigits->Clear();
562 //____________________________________________________________________________
563 AliADCalibData* AliADDigitizer::GetCalibData() const
566 AliCDBManager *man = AliCDBManager::Instance();
568 AliCDBEntry *entry=0;
570 entry = man->Get("AD/Calib/Data");
572 AliWarning("Load of calibration data from default storage failed!");
573 AliWarning("Calibration data will be loaded from local storage ($ALICE_ROOT)");
575 man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
577 entry = man->Get("AD/Calib/Data");
579 // Retrieval of data in directory AD/Calib/Data:
581 AliADCalibData *calibdata = 0;
583 if (entry) calibdata = (AliADCalibData*) entry->GetObject();
584 if (!calibdata) AliFatal("No calibration data from calibration database !");
590 //____________________________________________________________________________
591 double AliADDigitizer::SignalShape(double *x, double *par)
593 // this function simulates the time
594 // of arrival of the photons at the
597 if (xx <= par[0]) return 0;
598 Double_t a = 1./TMath::Power((xx-par[0])/par[1],1./par[2]);
599 if (xx <= par[3]) return a;
600 Double_t b = 1./TMath::Power((xx-par[3])/par[4],1./par[5]);
601 Double_t f = a*b/(a+b);
602 AliDebug(100,Form("x=%f func=%f",xx,f));
606 //____________________________________________________________________________
607 double AliADDigitizer::PMResponse(double *x, double * /* par */)
609 // this function describes the
610 // PM time response to a single
612 Double_t xx = x[0]+kPMRespTime;
613 return xx*xx*TMath::Exp(-xx*xx/(kPMRespTime*kPMRespTime));
616 //____________________________________________________________________________
617 double AliADDigitizer::SinglePhESpectrum(double *x, double * /* par */)
619 // this function describes the
620 // PM amplitude response to a single
623 if (xx < 0) return 0;
624 return (TMath::Poisson(xx,kPMNbOfSecElec)+kPMTransparency*TMath::Poisson(xx,1.0));
626 //____________________________________________________________________
627 TClonesArray* AliADDigitizer::DigitsArray()
629 // Initialize digit array if not already and
630 // return pointer to it.
632 fDigits = new TClonesArray("AliADdigit", 16);
638 //____________________________________________________________________
639 TClonesArray* AliADDigitizer::SDigitsArray()
641 // Initialize sdigit array if not already and
642 // return pointer to it.
644 fDigits = new TClonesArray("AliADSDigit", 16);