ATLAS Offline Software
Loading...
Searching...
No Matches
TileDigitsFromPulse.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5//*****************************************************************************
6// Filename : TileDigitsFromPulse.cxx
7// Author : Simon Molander
8// Created : February 2013
9//
10// DESCRIPTION
11//
12// Create TileDigits from simulated pulses.
13//
14// HISTORY:
15//
16// BUGS:
17//
18//*****************************************************************************
19
20// Tile includes
28
29//Simulator includes
33
34// Athena includes
37//Random number service
41
42#include <CLHEP/Random/Randomize.h>
43#include <CLHEP/Units/SystemOfUnits.h>
44
45//Root includes
46#include "TRandom3.h"
47#include "TFile.h"
48#include "TH1F.h"
49#include "TKey.h"
50#include "TF1.h"
51
52#include <cstdlib>
53#include <algorithm>
54#include <cmath>
55
56
57using CLHEP::RandGaussQ;
58using CLHEP::RandFlat;
59
60constexpr double MAX_PULSE_TIME_OFFSET = 12.5; // ns
61constexpr unsigned int LHC_BCIDS = 3564;
62
63//
64// Constructor
65//
66TileDigitsFromPulse::TileDigitsFromPulse(const std::string& name, ISvcLocator* pSvcLocator) :
67 AthAlgorithm(name, pSvcLocator)
68{
69 //Initialisations
70 m_ps[0] = new TilePulseShape(msgSvc(), "TilePulseShapeLo"); //Low Gain
71 m_ps[1] = new TilePulseShape(msgSvc(), "TilePulseShapeHi"); //High Gain
72
73
74 m_itFile = new TFile();
75 m_itDist = new TH1F();
76 m_ootFile = new TFile();
77 m_ootDist = new TH1F();
78 m_ootOffsetDist = new TH1F();
79 m_ootOffsetFile = new TFile();
80
81 m_pileup_AmpDistFile = new TFile();
82}
83
85
86 delete m_ootOffsetFile;
87 delete m_ootFile;
88 delete m_itFile;
90
91 delete m_ps[0];
92 delete m_ps[1];
93}
94
95//
96// Alg standard initialize function
97//
99
100 ATH_MSG_DEBUG("in initialize()");
101
102 m_buf = new TileSampleBuffer(m_nSamples, -(m_nSamples-1)*25/2, 25.);
103
104 m_tsg = new TileSampleGenerator(m_ps[0], m_buf, false); //Set third parameter to true for debug of the sum of pulses
105
106 m_nPul_eff = (m_nPul - 1) / 2; //Used for symetrization of PU in computation
107
108 ATH_CHECK(detStore()->retrieve(m_tileHWID, "TileHWID"));
109 ATH_CHECK(detStore()->retrieve(m_tileInfo, m_infoName.value()));
110 m_i_ADCmax = m_tileInfo->ADCmax();
111 ATH_MSG_DEBUG("Max ADC counts TileDigitsFromPulse: " << m_i_ADCmax);
112
114
115 ATH_CHECK(m_digitsContainerKey.initialize());
116 ATH_MSG_INFO("Output digits container: " << m_digitsContainerKey.key());
117
119 ATH_MSG_INFO("Output raw channel container: " << m_rawChannelContainerKey.key());
120
122 if (m_useBunchPattern) {
123 if (!m_simPulseChain || m_simQIE) {
124 ATH_MSG_FATAL("Bunch-pattern simulation requires continuous readout with QIE disabled");
125 return StatusCode::FAILURE;
126 }
127 if (m_initialBCID >= LHC_BCIDS) {
128 ATH_MSG_FATAL("InitialBCID must be in the range 0 to " << LHC_BCIDS - 1);
129 return StatusCode::FAILURE;
130 }
132 }
133
134
135 //Build pulse shapes
136 m_ps[0]->setPulseShape(m_tileInfo->digitsFullShapeLo());
137 m_ps[1]->setPulseShape(m_tileInfo->digitsFullShapeHi());
139 m_ps[0]->setUndershootParameters(-0.001964785, 20379.4);
140 m_ps[1]->setUndershootParameters(-0.002073114, 20394.6);
141 }
142
145 || m_gausC2C != 0.0 || m_imperfectionMean != 1.0 || m_imperfectionRms != 0.0) {
146 ATH_MSG_FATAL("Recursive undershoot requires (for the moment) continuous readout, gain-dependent "
147 "undershoot, fixed timing, and undeformed pulse shapes");
148 return StatusCode::FAILURE;
149 }
150
151 for (int gain = 0; gain < 2; ++gain) {
152 const TilePulseShape& pulse = *m_ps[gain];
153 const double amplitude = pulse.undershootAmplitude();
154 const double tau = pulse.undershootDecayTime();
155 const double startTime = pulse.effectiveStartTime();
156 const double endTime = pulse.effectiveEndTime();
157
158 if (!std::isfinite(tau) || tau <= 0.0 || !std::isfinite(startTime) || !std::isfinite(endTime)) {
159 ATH_MSG_FATAL("Invalid pulse shape or decay time for gain " << gain);
160 return StatusCode::FAILURE;
161 }
162
163 const double pulseTimingRange = m_gaussianPulseTimingVariation > 0.0 ? MAX_PULSE_TIME_OFFSET : 0.0;
164 const double accumulationStartTime = std::max(300.0, endTime) + pulseTimingRange;
165 const int firstTailBC = static_cast<int>(std::ceil(accumulationStartTime / 25.0));
166
167 m_firstRelevantIndex[gain] = std::max(0, m_nPul_eff + static_cast<int>(std::ceil((startTime - pulseTimingRange) / 25.0)));
168 m_firstAccumulatedIndex[gain] = m_nPul_eff + firstTailBC;
169
170 m_tailDecayPerBC[gain] = std::exp(-25.0 / tau);
171 m_tailAtAccumulation[gain] = amplitude * std::exp(-25.0 * firstTailBC / tau);
172 }
173
176 }
177 m_random = std::make_unique<TRandom3>(m_seed);
178
179 //Initialise distribution histograms if in use
180 if (m_useItADist) {
181 if (m_itADistFileName.value().size() == 0) {
182 m_itADistFileName = PathResolver::find_file("Distributions_small_h2000_177531_JetTauEtmiss.root", "DATAPATH");
183 if (m_itADistFileName.value().size() == 0) {
184 ATH_MSG_FATAL("Could not find input file Distributions_small_h2000_177531_JetTauEtmiss.root");
185 return StatusCode::FAILURE;
186 }
187 }
189 return StatusCode::FAILURE;
190 ATH_MSG_DEBUG("Made in-time distribution");
191 } else
192 delete m_itDist;
193
194 if (m_simPUwPoisson){
195
196 if (m_pileupAmpDistFileName.value().size() == 0) {
197 m_pileupAmpDistFileName = PathResolver::find_file("Distributions_MB_minbias_inelastic_lowjetphoton_e8314_e7400_s3508.root", "DATAPATH");
198 if (m_pileupAmpDistFileName.value().size() == 0 ) {
199 ATH_MSG_FATAL("Could not find input file Distributions_MB_minbias_inelastic_lowjetphoton_e8314_e7400_s3508.root");
200 return StatusCode::FAILURE;
201 }
202
203 }
205 return StatusCode::FAILURE;
206 ATH_MSG_DEBUG("Made PU amp distributions for each partition and channel");
207
208 }
209
210 if (m_useOotADist) {
211 if (m_ootADistFileName.value().size() == 0) {
212 m_ootADistFileName = PathResolver::find_file("Distributions_small_h2000_177531_ZeroBias.root", "DATAPATH");
213 if (m_ootADistFileName.value().size() == 0) {
214 ATH_MSG_FATAL("Could not find input file Distributions_small_h2000_177531_ZeroBias.root");
215 return StatusCode::FAILURE;
216 }
217 }
219 return StatusCode::FAILURE;
220 ATH_MSG_DEBUG("Made Oot distribution");
221 } else
222 delete m_ootDist;
223
224 //Initialise timing offset distribution. If filename is empty, use static offset
225 if (m_ootOffsetFileName.value().size() != 0) {
226 m_ootOffsetFile = TFile::Open(m_ootOffsetFileName.value().c_str());
227 if (m_ootOffsetFile->IsZombie()) {
228 ATH_MSG_WARNING("Error reading offset timing distribution from " << m_ootOffsetFileName.value() << ". Using static timing offset.");
229 } else {
230 TKey *key = m_ootOffsetFile->FindKey(m_ootOffsetHistName.value().c_str());
231 if (key == 0) {
232 ATH_MSG_WARNING("Histogram " << m_ootOffsetHistName.value() << " not found in file " << m_ootOffsetFileName.value() << ". Using static timing offset.");
233 } else {
234 m_ootOffsetDist = (TH1F*) m_ootOffsetFile->Get(m_ootOffsetHistName.value().c_str());
235 m_useOffsetHisto = kTRUE;
236 }
237 }
238 }
239
240 //Start the random number service used to create channel specific noise
241 if (!m_rndmSvc.retrieve().isSuccess()) {
242 ATH_MSG_FATAL("Could not initialize find Random Number Service.");
243 return StatusCode::FAILURE;
244 }
245 if (m_chanNoise)
246 m_gaussNoise = kFALSE; //Make sure channel noise overrides gaussian noise.
247
248 m_PUAmp.clear();
249 m_PUAmp.resize(4);
250 for (int ros = 1; ros < 5; ++ros){
251 m_PUAmp[ros-1].clear();
252 m_PUAmp[ros-1].resize(64);
253
254 for (int drawer = 0; drawer < 64; ++drawer){
255 m_PUAmp[ros-1][drawer].clear();
256 m_PUAmp[ros-1][drawer].resize(48);
257 for (int channel = 0; channel < 48; channel++){
258 m_PUAmp[ros-1][drawer][channel].clear();
259 m_PUAmp[ros-1][drawer][channel].resize(m_useRecursiveUndershoot ? m_pulseHistorySize : m_nPul.value());
260 }
261 }
262 }
264 m_pulseTimeOffsets.resize(4);
265 for (int ros = 0; ros < 4; ++ros) {
266 m_pulseTimeOffsets[ros].resize(64);
267 for (int drawer = 0; drawer < 64; ++drawer) {
268 m_pulseTimeOffsets[ros][drawer].resize(48);
269 for (int channel = 0; channel < 48; ++channel) {
270 m_pulseTimeOffsets[ros][drawer][channel].resize(m_PUAmp[ros][drawer][channel].size(), 0.0);
271 }
272 }
273 }
274 }
275
277 m_accumulatedUndershoot.resize(4);
278 for (auto& rosState : m_accumulatedUndershoot) {
279 rosState.resize(64);
280 for (auto& drawerState : rosState) {
281 drawerState.resize(48, std::array<double, 2>{0.0, 0.0});
282 }
283 }
284 } else if (!m_useBunchPattern) {
285 for (int ros = 1; ros < 5; ++ros) { // initialize the vector of PU amplitudes
286 for (int drawer = 0; drawer < 64; ++drawer){
287 for (int channel = 0; channel < 48; ++channel) {
288 double inTimeAmp = m_inTimeAmp.value();
289 addPileUp(inTimeAmp, m_ootAmp.value(), 1, ros, drawer, channel); // Initialized for HG, for LG use the same divided by the HG/LG ratio
291 for (std::size_t pulse = 0; pulse < m_PUAmp[ros-1][drawer][channel].size(); ++pulse) {
292 if (m_PUAmp[ros-1][drawer][channel][pulse] != 0.0F)
293 m_pulseTimeOffsets[ros-1][drawer][channel][pulse] = generatePulseTimeOffset();
294 }
295 }
296 }
297 }
298 }
299 }
300
301 ATH_MSG_DEBUG("initialize() successful");
302
303 return StatusCode::SUCCESS;
304}
305/*==========================================================================*/
306//
307// Begin Execution Phase.
308//
309StatusCode TileDigitsFromPulse::execute(const EventContext& ctx) {
310
311 ATH_MSG_DEBUG("in execute()");
312
313 bool isPairedBC = true;
314 if (m_useBunchPattern) {
316 if (!bunchGroupHandle.isValid() || *bunchGroupHandle == nullptr) {
317 ATH_MSG_ERROR("Could not retrieve " << m_bunchGroupKey.key());
318 return StatusCode::FAILURE;
319 }
320
321 const TrigConf::L1BunchGroupSet* bunchGroupSet = *bunchGroupHandle;
322 if (bunchGroupSet->size() <= 1) {
323 ATH_MSG_ERROR("Missing paired bunch group in " << m_bunchGroupKey.key());
324 return StatusCode::FAILURE;
325 }
326 const auto& pairedBunches = bunchGroupSet->getBunchGroup(1);
327 if (!pairedBunches || pairedBunches->size() == 0) {
328 ATH_MSG_ERROR("Missing or empty paired bunch group in " << m_bunchGroupKey.key());
329 return StatusCode::FAILURE;
330 }
331
332 isPairedBC = pairedBunches->contains(m_currentBCID);
333 }
334
335 // Prepare RNG service
336 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_randomStreamName);
337 rngWrapper->setSeed( m_randomStreamName, ctx );
338 CLHEP::HepRandomEngine* rndmEngine = rngWrapper->getEngine(ctx);
339
340 // Create new container for digits
341 auto digitsContainer = std::make_unique<TileMutableDigitsContainer>(true,
345
346 ATH_CHECK( digitsContainer->status() );
347
348 //Create RawChannel for truth values.
349 auto rawChannelContainer = std::make_unique<TileMutableRawChannelContainer>(true, m_rChType, m_rChUnit);
350 ATH_CHECK( rawChannelContainer->status() );
351
352 DataPool < TileDigits > tileDigitsPool(m_tileHWID->adc_hash_max());
353
354 double samplingPhase = 0, ped = 100; //Settings for simulation
355
356 TF1* pdf = nullptr;
357 std::unique_ptr<TF1> pdf_PhaseI;
358 std::unique_ptr<TF1> pdf_lo;
359 std::unique_ptr<TF1> pdf_hi;
360 if (!m_simQIE && m_gaussNoise) {
361 if(m_PhaseII){
362 Double_t sigma_lo = 1; //Noise value obtained from ATL-COM-TILECAL-2020-031
363 pdf_lo = std::make_unique<TF1>("pdf_lo","(1/(sqrt(2*pi)*[0])) * (exp(-0.5*(x/[0])**2)/(sqrt(2*pi)*[0]))", -100, 100);
364 pdf_lo->SetParameter(0,sigma_lo);
365
366 Double_t sigma_hi = 2.5; //Noise value obtained from ATL-COM-TILECAL-2020-031
367 pdf_hi = std::make_unique<TF1>("pdf_hi","(1/(sqrt(2*pi)*[0])) * (exp(-0.5*(x/[0])**2)/(sqrt(2*pi)*[0]))", -100, 100);
368 pdf_hi->SetParameter(0,sigma_hi);
369 }
370 else{
371 //Noise pdf for general noise. Maybe use as a member and put in init.
372 //pdf = new TF1("pdf", "[0] * (Gaus(x,0,[1]) + [2] * Gaus(x,0,[3]))", -100., 100.); //Root goes not like "Gaus"
373
374 pdf_PhaseI = std::make_unique<TF1>("pdf_PhaseI", "[0] * (exp(-0.5*(x/[1])**2)/(sqrt(2*pi)*[1]) + [2] *exp(-0.5*(x/[3])**2)/(sqrt(2*pi)*[3]))", -100., 100.);
375 pdf_PhaseI->SetParameters(m_GNAmpOne, m_GNSigmaOne, m_GNAmpTwo, m_GNSigmaTwo);
376 }
377 }
378
379 std::vector<float> samples(m_nSamples);
380
381 std::vector<double> Rndm(m_nSamples);
382 double Rndm_dG[1]; // uniform random number for the double gaussian
383
384 ATH_MSG_DEBUG("Starting loop");
385 int gain = 1;
386 double n_inTimeAmp = 0.0;
387 float sample = 0.0;
388
389 for (int ros = 1; ros < 5; ++ros) {
390 for (int drawer = 0; drawer < 64; ++drawer) {
391 unsigned int drawerIdx = TileCalibUtils::getDrawerIdx(ros, drawer);
392 for (int channel = 0; channel < 48; ++channel) {
393
394 if (!m_simQIE && !m_simPulseChain) { //3-in-1 or FENICS is simulated below
395 double ootAmp = m_ootAmp;
396 double truePhase = samplingPhase;
397 std::vector<double> pulseTimeOffsets;
399 pulseTimeOffsets.resize(m_nPul, 0.0);
400 const std::vector<double>* pulseTiming = pulseTimeOffsets.empty() ? nullptr : &pulseTimeOffsets;
401
402 if(m_PhaseII){
403 ATH_MSG_VERBOSE("executing FENICS code");
404 }
405 else{
406 ATH_MSG_VERBOSE("executing 3-in-1 code");
407 }
408
409
410 bool isHGSaturated = false;
411
412 for (int igain = 1; igain >= 0; igain--) {
413
414 gain = igain;
415 if (m_chanPed){
416 ped = m_tileToolNoiseSample->getPed(drawerIdx, channel, gain, TileRawChannelUnit::ADCcounts, ctx);
417 }
418 else{
419 ped = (gain == 1) ? m_ped_HG : m_ped_LG;
420 }
421
422 if (gain == 1) {
423 n_inTimeAmp = m_useItADist ? m_itDist->GetRandom() : m_inTimeAmp.value();
424
425 if (m_random->Rndm() >= m_pileUpFraction){
426 ootAmp = 0; //Set oot amplitude to 0 if no pile-up.
427 }
428 samplingPhase = m_random->Gaus(0., m_gausC2C); //C2C phase variation
429 double deformatedTime = m_random->Gaus(m_imperfectionMean, m_imperfectionRms); //Widening of pulseshape
430 m_ps[gain]->scalePulse(deformatedTime, deformatedTime); // Deformation of pulse shape by changing its width
431 //if(m_useOffsetHisto) m_ootOffset = m_ootOffsetDist->GetRandom(); //OLD Remove for 7 samples -> BunchSpacing
432
433 // Make sure m_PUAmp[ros-1][drawer][channel] has m_nPul elements, all zero
434 m_PUAmp[ros-1][drawer][channel].assign(m_nPul, 0.0);
435
436 // Fill amplitudes (including pile-up) for HG
437 addPileUp(n_inTimeAmp, ootAmp, gain, ros, drawer, channel);
438 if (pulseTiming) {
439 for (int pulse = 0; pulse < m_nPul; ++pulse) {
440 const bool hasPulse = m_PUAmp[ros-1][drawer][channel][pulse] != 0.0 || (pulse == m_nPul_eff && n_inTimeAmp != 0.0);
441 if (hasPulse)
442 pulseTimeOffsets[pulse] = generatePulseTimeOffset();
443 }
444 }
445 // Without an in-time pulse, retain the sampling phase; no physical pulse phase exists.
446 truePhase = n_inTimeAmp != 0.0
447 ? samplingPhase + (pulseTiming ? pulseTimeOffsets[m_nPul_eff] : 0.0) - m_itOffset
448 : samplingPhase;
449
450 } else {
451 double deformatedTime = m_random->Gaus(m_imperfectionMean, m_imperfectionRms); //Widening of pulseshape
452 m_ps[gain]->scalePulse(deformatedTime, deformatedTime); // Deformation of pulse shape by changing its width
453
454 double scaleFactor = (m_PhaseII ? 40.0 : 64.0);
455 n_inTimeAmp /= scaleFactor;
456 for (auto &ampValue : m_PUAmp[ros-1][drawer][channel]) {
457 ampValue /= scaleFactor;
458 }
459 }
460
461 if(m_PhaseII && m_gaussNoise){
462 pdf = (gain==1) ? pdf_hi.get() : pdf_lo.get();
463 }
464 else if(m_gaussNoise){
465 pdf = pdf_PhaseI.get();
466 }
467
468 m_tsg->setPulseShape(m_ps[gain]);
469 m_tsg->fillNSamples(samplingPhase, ped, n_inTimeAmp, m_PUAmp[ros-1][drawer][channel], pdf, m_gaussNoise, m_itOffset, m_nSamples, m_nPul, pulseTiming); // Sum of Intime + PU pulses
470
471 samples.clear();
472 samples.resize(m_nSamples);
473 m_buf->getValueVector(samples);
474
475 if (m_chanNoise) {
476 double Hfn1 = m_tileToolNoiseSample->getHfn1(drawerIdx, channel, gain, ctx);
477 double Hfn2 = m_tileToolNoiseSample->getHfn2(drawerIdx, channel, gain, ctx);
478 double Norm = m_tileToolNoiseSample->getHfnNorm(drawerIdx, channel, gain, ctx);
479 RandGaussQ::shootArray(rndmEngine, samples.size(), Rndm.data(), 0.0, 1.0);
480 RandFlat::shootArray(rndmEngine, 1, Rndm_dG, 0.0, 1.0);
481 for (unsigned int js = 0; js < samples.size(); ++js) {
482 //using the same gaussian(sigma) for all samples in one channel in one event
483 if (Rndm_dG[0] < Norm)
484 samples[js] += (float) Hfn1 * Rndm[js];
485 else
486 samples[js] += (float) Hfn2 * Rndm[js];
487 }
488 }
489
490 for (unsigned int i = 0; i < samples.size(); ++i) {
491 if (samples[i] >= m_i_ADCmax){
492 isHGSaturated = true;
493 if(m_bigain) samples[i]=m_i_ADCmax;
494 }
495
496 }
497
498 if(!m_bigain && !isHGSaturated){
499 break;
500 }
501
502 ATH_MSG_VERBOSE("New ADC " << ros << "/" << drawer << "/" << channel << "/ saving gain " << gain);
503
504 TileDigits * digit = tileDigitsPool.nextElementPtr();
505 *digit = TileDigits (m_tileHWID->adc_id(ros, drawer, channel, gain), samples);
506
507 ATH_CHECK( digitsContainer->push_back(digit) );
508
509 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
510 n_inTimeAmp,
511 truePhase,
512 ootAmp,
514
515 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
516
517 }
518
519 if(!m_bigain){
520 ATH_MSG_VERBOSE("New ADC " << ros << "/" << drawer << "/" << channel << "/ saving gain " << gain);
521
522 TileDigits * digit = tileDigitsPool.nextElementPtr();
523 *digit = TileDigits (m_tileHWID->adc_id(ros, drawer, channel, gain), std::move(samples));
524
525 ATH_CHECK( digitsContainer->push_back(digit) );
526
527 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
528 n_inTimeAmp,
529 truePhase,
530 ootAmp,
532
533 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
534 }
535
536 } else if (m_simQIE) { //QIE is simulated here --------------------------------------------
537
538 //ATH_MSG_DEBUG("executing QIE code");
539
540 gain = 1; //This is just a place holder. The gain is not used in QIE.
541 n_inTimeAmp = m_useItADist ? m_itDist->GetRandom() : m_inTimeAmp.value();
542 //if (random->Rndm() >= m_pileUpFraction) //m_pileUpFraction is 1 by default
543 m_ootAmp = 0; //Set oot amplitude to 0 if no pile-up.
544 samplingPhase = 0; //TODO: Introduce jitter of the PMT pulse; random->Gaus(0., m_gausC2C); //C2C phase variation
545
546 //Pileup samples
547 //m_PUAmp.clear();
548 //m_PUAmp.resize(nPul);
549 float my_PUAmp[7] = {0}; //I use an array to store the energies/charges of the out-of-time pulses
550
551 for (int i = 0; i < 7; i++)
552 if ((((i - 3) * 25) % (int) m_BunchSpacing) == 0) {
553 if (i != 3) { //index 3 corresponds to the in-time pulse, the signal
554 my_PUAmp[i] = m_useOotADist ? m_ootDist->GetRandom() : m_ootAmp.value(); //out-of-time pulses
555 } else {
556 my_PUAmp[i] = 0;
557 }
558 }
559
560 //fill7SamplesQIE(float t0, float amp_it, float *amp_pu, bool addNoise);
561 m_tsg->fill7SamplesQIE((float) n_inTimeAmp, my_PUAmp); // Sum of In time + out-of-time PU pulses
562
563 samples.clear();
564 samples.resize(m_nSamples);
565 m_buf->getValueVector(samples);
566
567 ATH_MSG_VERBOSE("New ADC " << ros << "/" << drawer << "/" << channel << "/ saving gain " << gain);
568
569 TileDigits * digit = tileDigitsPool.nextElementPtr();
570 *digit = TileDigits (m_tileHWID->adc_id(ros, drawer, channel, gain),
571 std::move(samples));
572
573 ATH_CHECK( digitsContainer->push_back(digit) );
574
575 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
576 n_inTimeAmp,
577 samplingPhase,
578 m_ootAmp,
580
581 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
582 }
583 else if (m_simPulseChain) {
584 ATH_MSG_VERBOSE("executing chain-of-pulses code");
585
586 bool isHGSaturated = false;
587 double truthPulseTimeOffset = 0.0;
588 std::vector<double>* pulseTiming = m_gaussianPulseTimingVariation > 0.0
589 ? &m_pulseTimeOffsets[ros-1][drawer][channel]
590 : nullptr;
591
592 for (int igain = 1; igain >= 0; --igain) {
593 gain = igain;
594 n_inTimeAmp = 0.0;
595 m_ootAmp = 0.0;
596 m_ootOffset = 0.0;
597
598 if (isPairedBC && m_random->Rndm() < m_itAPulseProb) {
599 n_inTimeAmp = m_useItADist
600 ? m_itDist->GetRandom()
601 : m_inTimeAmp.value();
602 } else {
603 n_inTimeAmp = 0;
604 }
605
606 // PDF logic for noise
607 if (m_gaussNoise) {
608 if (m_PhaseII) {
609 pdf = (gain == 1) ? pdf_hi.get() : pdf_lo.get();
610 } else {
611 pdf = pdf_PhaseI.get();
612 }
613 }
614
615 m_tsg->setPulseShape(m_ps[gain]);
616 if (gain == 1) {
617 ped = m_chanPed
618 ? m_tileToolNoiseSample->getPed(drawerIdx, channel, gain,
620 : m_ped_HG.value();
621
622 samplingPhase = m_random->Gaus(0., m_gausC2C);
623 double deformatedTime = m_random->Gaus(m_imperfectionMean, m_imperfectionRms);
624 m_ps[gain]->scalePulse(deformatedTime, deformatedTime);
625
626 // Successive execute calls represent consecutive simulated BCs.
627 // Shift the stored pulses to simulate the next BC.
628 m_PUAmp[ros-1][drawer][channel].pop_back();
629 m_PUAmp[ros-1][drawer][channel].insert(m_PUAmp[ros-1][drawer][channel].begin(), 0);
630 if (pulseTiming) {
631 pulseTiming->pop_back();
632 pulseTiming->insert(pulseTiming->begin(), 0.0);
633 }
634
635 // m_sample_tru is the amplitude for the central BC
636 m_sample_tru = m_PUAmp[ros-1][drawer][channel][(m_nPul - 1) / 2];
637 if (pulseTiming)
638 truthPulseTimeOffset = pulseTiming->at(m_nPul_eff);
639
640 // Fill the new BC at the front
641 if (isPairedBC) addPileUpSample(gain, ros, drawer, channel);
642 m_PUAmp[ros-1][drawer][channel].front() += n_inTimeAmp; // Add amplitude from in-time pulse to true-amp vector
643 if (pulseTiming && m_PUAmp[ros-1][drawer][channel].front() != 0.0)
644 pulseTiming->front() = generatePulseTimeOffset();
645
647 auto& accumulatedUndershoot = m_accumulatedUndershoot[ros-1][drawer][channel];
648 for (int gainIndex = 0; gainIndex < 2; ++gainIndex) {
649 const double gainRatio = (gainIndex == 1) ? 1.0 : 40.0;
650
651 // Decay the accumulated undershoot and add the tail of the next older pulse
652 double tailAtPulseTime = m_tailAtAccumulation[gainIndex];
653 if (pulseTiming) {
654 const double pulseTimeOffset = pulseTiming->at(m_firstAccumulatedIndex[gainIndex]);
655 tailAtPulseTime *= std::exp(pulseTimeOffset / m_ps[gainIndex]->undershootDecayTime());
656 }
657 accumulatedUndershoot[gainIndex] = m_tailDecayPerBC[gainIndex] * accumulatedUndershoot[gainIndex]
658 + m_PUAmp[ros-1][drawer][channel][m_firstAccumulatedIndex[gainIndex]]
659 * tailAtPulseTime / gainRatio;
660 }
661 sample = m_tsg->fillSampleWithUndershoot(samplingPhase, ped,
662 m_PUAmp[ros-1][drawer][channel],
665 gain, accumulatedUndershoot[gain], pulseTiming);
666 } else {
667 sample = m_tsg->fillSample(samplingPhase, ped,
668 m_PUAmp[ros-1][drawer][channel],
669 pdf, m_gaussNoise,
670 (int)m_PUAmp[ros-1][drawer][channel].size(),
671 gain, pulseTiming);
672 }
673 } else {
674 // Low gain
675 ped = m_chanPed
676 ? m_tileToolNoiseSample->getPed(drawerIdx, channel, gain,
678 : m_ped_LG.value();
679
681 sample = m_tsg->fillSampleWithUndershoot(samplingPhase, ped,
682 m_PUAmp[ros-1][drawer][channel],
685 gain, m_accumulatedUndershoot[ros-1][drawer][channel][gain], pulseTiming);
686 } else {
687 sample = m_tsg->fillSample(samplingPhase, ped,
688 m_PUAmp[ros-1][drawer][channel],
689 pdf, m_gaussNoise,
690 (int)m_PUAmp[ros-1][drawer][channel].size(),
691 gain, pulseTiming);
692 }
693
694 // Scale truth amplitude from HG to LG
695 if (m_PhaseII) {
696 m_sample_tru /= 40.0;
697 } else {
698 m_sample_tru /= 64.0;
699 }
700 }
701
702 // Clip the sample
703 if (sample < 0.0) {
704 sample = 0.0;
705 } else if (sample >= m_i_ADCmax) {
706 isHGSaturated = true;
707 if (m_bigain) sample = m_i_ADCmax;
708 }
709
710 samples.clear();
711 samples.push_back(sample);
712
713 ATH_MSG_VERBOSE("New chain ADC " << ros << "/" << drawer << "/" << channel << " - saving gain " << gain);
714
715 TileDigits* digit = tileDigitsPool.nextElementPtr();
716 *digit = TileDigits(m_tileHWID->adc_id(ros, drawer, channel, gain),
717 std::move(samples));
718 ATH_CHECK(digitsContainer->push_back(digit));
719
720 // The pulse timing offset is zero when there is no in-time pulse.
721 const double truePhase = samplingPhase + truthPulseTimeOffset;
722 auto rawChannel = std::make_unique<TileRawChannel>(
723 digit->adc_HWID(),
725 truePhase,
726 m_ootAmp,
727 ped);
728 ATH_CHECK(rawChannelContainer->push_back(std::move(rawChannel)));
729
730 // If not bigain, break if HG didn't saturate
731 if (!m_bigain && !isHGSaturated) {
732 break;
733 }
734 }
735 }
736 }
737 }
738 }
739
741 ATH_CHECK( rawChannelCnt.record(std::move(rawChannelContainer)) );
742
744 ATH_CHECK( digitsCnt.record(std::move(digitsContainer)) );
745
746 ATH_MSG_DEBUG("Execution completed");
748
749 return StatusCode::SUCCESS;
750}
751
753 ATH_MSG_DEBUG("in finalize()");
754 delete m_buf;
755 delete m_tsg;
756
757
758 if (m_useItADist)
759 m_itFile->Close();
760 if (m_useOotADist)
761 m_ootFile->Close();
762 if (m_simPUwPoisson)
763 m_pileup_AmpDistFile->Close();
764
765 return StatusCode::SUCCESS;
766}
767
768bool TileDigitsFromPulse::makeDist(TFile*& file, TH1F*& hist, const std::string& fileName, const std::string& histName) {
769 file = new TFile(fileName.c_str());
770 if (file->IsZombie()) {
771 ATH_MSG_FATAL("Error reading amplitude distribution from " << fileName << ".");
772 return kFALSE;
773 }
774 TKey *key = file->FindKey(histName.c_str());
775 if (key == 0) {
776 ATH_MSG_FATAL("Could not find histogram " << histName << " in file " << fileName << ".");
777 return kFALSE;
778 }
779 hist = (TH1F*) file->Get(histName.c_str());
780 for (int i = 0; i < m_AmpDistLowLim; i++)
781 hist->SetBinContent(i, 0.); // Puts a cut on the amplitude distribution.
782 return kTRUE;
783
784}
785
786bool TileDigitsFromPulse::makeDist(TFile*& file, std::vector<std::vector<TH1F*>>& hists, const std::string& fileName) {
787
788 std::string histName;
789 TKey *key;
790 TH1F* hist;
791
792 file = new TFile(fileName.c_str());
793 if (file->IsZombie()) {
794 ATH_MSG_FATAL("Error reading amplitude distributions from " << fileName << ".");
795 return kFALSE;
796 }
797
798 for(int ros=0; ros<4; ros++){
799
800 hists.push_back(std::vector<TH1F*>());
801 for(int channel=0; channel<48; channel++){
802
803 histName = "ene_ros_" + std::to_string(ros+1) + "_channel_" + std::to_string(channel+1);
804
805 key = file->FindKey(histName.c_str());
806 if (key == 0) {
807 ATH_MSG_FATAL("Could not find histogram " << histName << " in file " << fileName << ".");
808 return kFALSE;
809 }
810
811 hist = (TH1F*) file->Get(histName.c_str());
812
813 for (int i = 0; i < m_AmpDistLowLim; i++)
814 hist->SetBinContent(i, 0.); // Puts a cut on the amplitude distribution.
815
816 hists[ros].push_back(hist);
817 hist->Clear();
818 }
819 }
820
821 return kTRUE;
822}
823
825 double pulseTimeOffset = 0.0;
826 do {
827 pulseTimeOffset = m_random->Gaus(0.0, m_gaussianPulseTimingVariation);
828 } while (std::abs(pulseTimeOffset) > MAX_PULSE_TIME_OFFSET);
829 return pulseTimeOffset;
830}
831
832void TileDigitsFromPulse::addPileUp(double &n_inTimeAmp, double ootAmp, int gain, int ros, int drawer, int channel) {
833 //Pileup samples
834 double amp_1;
835 double amp_2;
836 double mu = 0; //Interaction per bunch crossing for PU simulation
837 if(gain == 1){
838 for (int i = 0; i <= m_nPul_eff; i++) {
839 if (((i * 25) % m_BunchSpacing) == 0) {
840 if(m_simPUwPoisson){
841 mu=m_random->Poisson(m_avgMuForPU);
842 ATH_MSG_VERBOSE("Effective pulse number " << i);
843 ATH_MSG_VERBOSE("Number of interactions for simulation: " << mu );
844
845 for (int imu = 0; imu<mu; imu++){
846
847 if (m_random->Rndm() < m_pileUpFraction){
848 amp_1 = m_pileup_AmpDists[ros-1][channel]->GetRandom();
849 }
850 else{
851 amp_1 = 0;
852 }
853
854 if (m_random->Rndm() < m_pileUpFraction){
855 amp_2 = m_pileup_AmpDists[ros-1][channel]->GetRandom();
856 }
857 else{
858 amp_2 = 0;
859 }
860
861 ATH_MSG_VERBOSE("Random amplitudes for PU: " << amp_1 << " " << amp_2);
862 if(i==0){
863 m_PUAmp[ros-1][drawer][channel][m_nPul_eff] += amp_1;
864 }
865 else{
866 m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] += amp_1;
867 m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] += amp_2;
868 }
869 }
870
871 if(m_PUAmp[ros-1][drawer][channel][m_nPul_eff] < 0) m_PUAmp[ros-1][drawer][channel][m_nPul_eff] = 0;
872 if(m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] < 0) m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] = 0;
873 if(m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] < 0) m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] = 0;
874
875 ATH_MSG_VERBOSE("Final amplitudes for pulse " << m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] << " " << m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i]);
876 }
877 else{
878 m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] = m_useOotADist ? m_ootDist->GetRandom() : ootAmp;
879 m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] = m_useOotADist ? m_ootDist->GetRandom() : ootAmp;
880 }
881
882 if(m_simPulseChain){ // Special treatment for pulse-chain simulation, add in-time pulses when initializing true-amp vector
883 if (m_random->Rndm() < m_itAPulseProb){
884 amp_1 = m_useItADist ? m_itDist->GetRandom() : m_inTimeAmp.value();
885 } else{
886 amp_1 = 0;
887 }
888
889 if (m_random->Rndm() < m_itAPulseProb){
890 amp_2 = m_useItADist ? m_itDist->GetRandom() : m_inTimeAmp.value();
891 } else{
892 amp_2 = 0;
893 }
894
895 if(i==0){
896 m_PUAmp[ros-1][drawer][channel][m_nPul_eff] += amp_1;
897 }
898 else{
899 m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] += amp_1;
900 m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] += amp_2;
901 }
902 }
903 } else {
904 m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] = 0;
905 m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] = 0;
906 }
907 }
908 }
909 else if (gain == 0)
910 {
911 double scaleFactor = m_PhaseII ? 40 : 64;
912 n_inTimeAmp /= scaleFactor;
913 for (int i = 0; i <= m_nPul_eff; i++) {
914 m_PUAmp[ros-1][drawer][channel][m_nPul_eff + i] /= scaleFactor;
915 m_PUAmp[ros-1][drawer][channel][m_nPul_eff - i] /= scaleFactor;
916 }
917 }
918}
919
920void TileDigitsFromPulse::addPileUpSample(int gain, int ros, int drawer, int channel) {
921
922 double amp = 0;
923 double mu = 0; //Interaction per bunch crossing for PU simulation
924
925 mu=m_random->Poisson(m_avgMuForPU);
926
927 if(gain == 1){
928 for (int imu = 0; imu<mu; imu++){
929 if (m_random->Rndm() < m_pileUpFraction){
930 amp = m_pileup_AmpDists[ros-1][channel]->GetRandom();
931 }
932 else{
933 amp = 0;
934 }
935
936 m_PUAmp[ros-1][drawer][channel].front() += amp;
937 }
938 }
939 else if (gain == 0)
940 {
941 if(!m_PhaseII){
942 m_PUAmp[ros-1][drawer][channel].front() /= 64;
943 }
944 else{
945 m_PUAmp[ros-1][drawer][channel].front() /= 40;
946 }
947 }
948
949 if(m_PUAmp[ros-1][drawer][channel].front() < 0) m_PUAmp[ros-1][drawer][channel].front() = 0;
950}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
size_t size() const
Number of registered mappings.
constexpr double MAX_PULSE_TIME_OFFSET
constexpr unsigned int LHC_BCIDS
Helper for holding non-const raw data prior to recording in SG.
Helper for holding non-const raw data prior to recording in SG.
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:154
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:108
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
const ServiceHandle< StoreGateSvc > & detStore() const
pointer nextElementPtr()
obtain the next available element in pool by pointer pool is resized if its limit has been reached On...
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
static unsigned int getDrawerIdx(unsigned int ros, unsigned int drawer)
Returns a drawer hash.
Gaudi::Property< float > m_gausC2C
RMS for the in-time pulse offset (channel-to-channel phase variation).
TFile * m_pileup_AmpDistFile
File containing amplitude histograms for PU pulses.
Gaudi::Property< double > m_ped_HG
Pedestal value for HG if specific channel pedestal is not used.
TFile * m_itFile
File that holds the distribution of in-time amplitudes.
Gaudi::Property< bool > m_useRecursiveUndershoot
Recursively accumulate the long undershoot in continuous readout.
Gaudi::Property< bool > m_useGainDependentUndershoot
Use gain-dependent analytical undershoot parameters.
SG::WriteHandleKey< TileRawChannelContainer > m_rawChannelContainerKey
Gaudi::Property< double > m_ootOffset
Out-of-time pulse offset from nominal time.
Gaudi::Property< std::string > m_ootADistFileName
Filename of file to use for amplitude distribution of out-of-time pulses.
void addPileUp(double &n_inTimeAmp, double ootAmp, int gain, int ros, int drawer, int channel)
Fill vector with pile-up amplitudes.
const TileHWID * m_tileHWID
std::array< int, 2 > m_firstAccumulatedIndex
Gaudi::Property< double > m_ped_LG
Pedestal value for LG if specific channel pedestal is not used.
virtual StatusCode initialize() override
initialize method
virtual StatusCode finalize() override
finalize method
Gaudi::Property< float > m_pileUpFraction
Probability that an out-of-time component will be added.
Gaudi::Property< std::string > m_infoName
Gaudi::Property< bool > m_useOotADist
Set to TRUE in order to use a distribution for the out-of-time amplitude instead of a constant value.
Gaudi::Property< double > m_itOffset
In-time pulse offset from nominal time.
TH1F * m_ootDist
Histogram to hold the distribution of out-of-time amplitudes.
Gaudi::Property< bool > m_chanPed
Use channel specific pedestal value if true.
TFile * m_ootFile
File that holds the distribution of out-of-time amplitudes.
TileFragHash::TYPE m_rChType
Type of TileRawChannels (Digitizar, OF1, OF2, Fit, etc.)(see TileFragHash.h).
TileSampleBuffer * m_buf
Buffer class to hold generated pulses.
ToolHandle< TileCondToolNoiseSample > m_tileToolNoiseSample
virtual StatusCode execute(const EventContext &ctx) override
execute method
Gaudi::Property< unsigned int > m_initialBCID
BCID generated by the first execute call.
Gaudi::Property< bool > m_useBunchPattern
Generate collision-induced pulses only in paired bunch crossings.
TFile * m_ootOffsetFile
File that holds the distribution of out-of-time timing offsets.
std::vector< std::vector< std::vector< std::vector< double > > > > m_pulseTimeOffsets
Gaudi::Property< double > m_GNSigmaOne
Standard deviation of first gaussian of double gaussian noise.
Gaudi::Property< std::string > m_itADistHistName
Name of histogram for in-time amplitude distribution.
Gaudi::Property< std::string > m_pileupAmpDistFileName
Filename for PU amplitude distribution histograms.
void addPileUpSample(int gain, int ros, int drawer, int channel)
Fill only a BC with pile-up amplitude.
std::vector< std::vector< std::vector< std::vector< float > > > > m_PUAmp
Gaudi::Property< std::string > m_ootOffsetFileName
File name for offset timing distribution histogram.
std::unique_ptr< TRandom3 > m_random
Gaudi::Property< int > m_nPul
number of pileup pulses
Gaudi::Property< double > m_imperfectionMean
Mean value of pulse shape broadening.
Gaudi::Property< double > m_GNSigmaTwo
Standard deviation of second gaussian of double gaussian noise.
Gaudi::Property< int > m_seed
bool m_useOffsetHisto
Internally used to keep track of wether a histogram has been opened or not.
TileRawChannelUnit::UNIT m_rChUnit
Units used for the TileRawChannels (ADC, pCb, etc.)(see TileInfo.h).
SG::ReadCondHandleKey< TrigConf::L1BunchGroupSet > m_bunchGroupKey
const TileInfo * m_tileInfo
std::vector< std::vector< std::vector< std::array< double, 2 > > > > m_accumulatedUndershoot
Gaudi::Property< int > m_BunchSpacing
Time between pulses in ms 25, 50 or 75.
double generatePulseTimeOffset()
Generate a pulse arrival-time variation in ns.
TH1F * m_ootOffsetDist
Histogram to hold the distribution of out-of-time timing offsets.
SG::WriteHandleKey< TileDigitsContainer > m_digitsContainerKey
Gaudi::Property< bool > m_gaussNoise
Set to TRUE in order to create noise from double gaussian.
Gaudi::Property< std::string > m_itADistFileName
Filename of file to use for amplitude distribution of in-time pulses.
Gaudi::Property< bool > m_bigain
Gaudi::Property< double > m_itAPulseProb
Probability to add an in-time pulse.
Gaudi::Property< double > m_GNAmpOne
Amplitude of first gaussian of double gaussian noise.
TilePulseShape * m_ps[2]
Class for defining pulse.
Gaudi::Property< double > m_imperfectionRms
RMS of pulse shape broadening.
ServiceHandle< IAthRNGSvc > m_rndmSvc
Random number service to use.
Gaudi::Property< double > m_gaussianPulseTimingVariation
RMS of individual pulse arrival-time variations in ns.
Gaudi::Property< int > m_avgMuForPU
TileSampleGenerator * m_tsg
Pulse generating class.
Gaudi::Property< int > m_nSamples
number of read out samples
Gaudi::Property< std::string > m_randomStreamName
Random Stream Name.
Gaudi::Property< double > m_GNAmpTwo
Amplitude of second gaussian of double gaussian noise.
Gaudi::Property< int > m_AmpDistLowLim
Set all bins to the left of this bin to 0 in the amplitude distribution histograms.
bool makeDist(TFile *&file, TH1F *&hist, const std::string &fileName, const std::string &histName="h_Eopt_hi")
Method to read distribution from file.
std::array< double, 2 > m_tailAtAccumulation
TH1F * m_itDist
Histogram to hold the distribution of in-time amplitudes.
Gaudi::Property< std::string > m_ootOffsetHistName
Name of the histogram for timing offset distribution.
Gaudi::Property< bool > m_chanNoise
Add channel specific noise.
Gaudi::Property< bool > m_simPulseChain
Simulate continous output of readout for HL-LHC paradigm.
Gaudi::Property< bool > m_useItADist
Set to TRUE in order to use a distribution for the in-time amplitude instead of a constant value.
Gaudi::Property< bool > m_PhaseII
std::array< int, 2 > m_firstRelevantIndex
unsigned int m_currentBCID
BCID generated by the current execute call.
TileDigitsFromPulse(const std::string &name, ISvcLocator *pSvcLocator)
std::vector< std::vector< TH1F * > > m_pileup_AmpDists
Matrix of PU amplitude distribution histograms (PU per partition and channel).
std::array< double, 2 > m_tailDecayPerBC
Gaudi::Property< bool > m_simQIE
Raw PMT pulses are generated if the option is set to true.
Gaudi::Property< bool > m_simPUwPoisson
Gaudi::Property< double > m_inTimeAmp
Amplitude of in-time pulse.
Gaudi::Property< double > m_ootAmp
Amplitude of out-of-time pulse.
Gaudi::Property< std::string > m_ootADistHistName
Name of histogram for out-of-time amplitude distribution.
double undershootDecayTime() const
double effectiveStartTime() const
double effectiveEndTime() const
double undershootAmplitude() const
L1 board configuration.
std::size_t size() const
Accessor to the number of defined bunchgroups.
const std::shared_ptr< L1BunchGroup > & getBunchGroup(const std::string &name) const
Accessor to the bunchgroup by name.
void Norm(TH1 *h, double scale, double xmin, double xmax)
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
TFile * file