42#include <CLHEP/Random/Randomize.h>
43#include <CLHEP/Units/SystemOfUnits.h>
57using CLHEP::RandGaussQ;
124 ATH_MSG_FATAL(
"Bunch-pattern simulation requires continuous readout with QIE disabled");
125 return StatusCode::FAILURE;
129 return StatusCode::FAILURE;
139 m_ps[0]->setUndershootParameters(-0.001964785, 20379.4);
140 m_ps[1]->setUndershootParameters(-0.002073114, 20394.6);
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;
151 for (
int gain = 0; gain < 2; ++gain) {
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;
164 const double accumulationStartTime = std::max(300.0, endTime) + pulseTimingRange;
165 const int firstTailBC =
static_cast<int>(std::ceil(accumulationStartTime / 25.0));
184 ATH_MSG_FATAL(
"Could not find input file Distributions_small_h2000_177531_JetTauEtmiss.root");
185 return StatusCode::FAILURE;
189 return StatusCode::FAILURE;
199 ATH_MSG_FATAL(
"Could not find input file Distributions_MB_minbias_inelastic_lowjetphoton_e8314_e7400_s3508.root");
200 return StatusCode::FAILURE;
205 return StatusCode::FAILURE;
206 ATH_MSG_DEBUG(
"Made PU amp distributions for each partition and channel");
214 ATH_MSG_FATAL(
"Could not find input file Distributions_small_h2000_177531_ZeroBias.root");
215 return StatusCode::FAILURE;
219 return StatusCode::FAILURE;
242 ATH_MSG_FATAL(
"Could not initialize find Random Number Service.");
243 return StatusCode::FAILURE;
250 for (
int ros = 1; ros < 5; ++ros){
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();
265 for (
int ros = 0; ros < 4; ++ros) {
267 for (
int drawer = 0; drawer < 64; ++drawer) {
269 for (
int channel = 0; channel < 48; ++channel) {
280 for (
auto& drawerState : rosState) {
281 drawerState.resize(48, std::array<double, 2>{0.0, 0.0});
285 for (
int ros = 1; ros < 5; ++ros) {
286 for (
int drawer = 0; drawer < 64; ++drawer){
287 for (
int channel = 0; channel < 48; ++channel) {
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)
303 return StatusCode::SUCCESS;
313 bool isPairedBC =
true;
316 if (!bunchGroupHandle.
isValid() || *bunchGroupHandle ==
nullptr) {
318 return StatusCode::FAILURE;
322 if (bunchGroupSet->
size() <= 1) {
324 return StatusCode::FAILURE;
327 if (!pairedBunches || pairedBunches->size() == 0) {
329 return StatusCode::FAILURE;
338 CLHEP::HepRandomEngine* rndmEngine = rngWrapper->
getEngine(ctx);
341 auto digitsContainer = std::make_unique<TileMutableDigitsContainer>(
true,
349 auto rawChannelContainer = std::make_unique<TileMutableRawChannelContainer>(
true,
m_rChType,
m_rChUnit);
350 ATH_CHECK( rawChannelContainer->status() );
352 DataPool < TileDigits > tileDigitsPool(
m_tileHWID->adc_hash_max());
354 double samplingPhase = 0, ped = 100;
357 std::unique_ptr<TF1> pdf_PhaseI;
358 std::unique_ptr<TF1> pdf_lo;
359 std::unique_ptr<TF1> pdf_hi;
362 Double_t sigma_lo = 1;
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);
366 Double_t sigma_hi = 2.5;
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);
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.);
386 double n_inTimeAmp = 0.0;
389 for (
int ros = 1; ros < 5; ++ros) {
390 for (
int drawer = 0; drawer < 64; ++drawer) {
392 for (
int channel = 0; channel < 48; ++channel) {
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;
410 bool isHGSaturated =
false;
412 for (
int igain = 1; igain >= 0; igain--) {
430 m_ps[gain]->scalePulse(deformatedTime, deformatedTime);
437 addPileUp(n_inTimeAmp, ootAmp, gain, ros, drawer, channel);
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);
446 truePhase = n_inTimeAmp != 0.0
452 m_ps[gain]->scalePulse(deformatedTime, deformatedTime);
454 double scaleFactor = (
m_PhaseII ? 40.0 : 64.0);
455 n_inTimeAmp /= scaleFactor;
456 for (
auto &Value :
m_PUAmp[ros-1][drawer][channel]) {
457 ampValue /= scaleFactor;
462 pdf = (gain==1) ? pdf_hi.get() : pdf_lo.get();
465 pdf = pdf_PhaseI.get();
473 m_buf->getValueVector(samples);
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) {
483 if (Rndm_dG[0] <
Norm)
484 samples[js] += (float) Hfn1 * Rndm[js];
486 samples[js] += (float) Hfn2 * Rndm[js];
490 for (
unsigned int i = 0; i < samples.size(); ++i) {
492 isHGSaturated =
true;
502 ATH_MSG_VERBOSE(
"New ADC " << ros <<
"/" << drawer <<
"/" << channel <<
"/ saving gain " << gain);
507 ATH_CHECK( digitsContainer->push_back(digit) );
509 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
515 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
520 ATH_MSG_VERBOSE(
"New ADC " << ros <<
"/" << drawer <<
"/" << channel <<
"/ saving gain " << gain);
525 ATH_CHECK( digitsContainer->push_back(digit) );
527 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
533 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
549 float my_PUAmp[7] = {0};
551 for (
int i = 0; i < 7; i++)
561 m_tsg->fill7SamplesQIE((
float) n_inTimeAmp, my_PUAmp);
565 m_buf->getValueVector(samples);
567 ATH_MSG_VERBOSE(
"New ADC " << ros <<
"/" << drawer <<
"/" << channel <<
"/ saving gain " << gain);
573 ATH_CHECK( digitsContainer->push_back(digit) );
575 auto rawChannel = std::make_unique<TileRawChannel>(digit->adc_HWID(),
581 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
586 bool isHGSaturated =
false;
587 double truthPulseTimeOffset = 0.0;
592 for (
int igain = 1; igain >= 0; --igain) {
609 pdf = (gain == 1) ? pdf_hi.get() : pdf_lo.get();
611 pdf = pdf_PhaseI.get();
624 m_ps[gain]->scalePulse(deformatedTime, deformatedTime);
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);
631 pulseTiming->pop_back();
632 pulseTiming->insert(pulseTiming->begin(), 0.0);
638 truthPulseTimeOffset = pulseTiming->at(
m_nPul_eff);
642 m_PUAmp[ros-1][drawer][channel].front() += n_inTimeAmp;
643 if (pulseTiming &&
m_PUAmp[ros-1][drawer][channel].front() != 0.0)
648 for (
int gainIndex = 0; gainIndex < 2; ++gainIndex) {
649 const double gainRatio = (gainIndex == 1) ? 1.0 : 40.0;
655 tailAtPulseTime *= std::exp(pulseTimeOffset /
m_ps[gainIndex]->undershootDecayTime());
657 accumulatedUndershoot[gainIndex] =
m_tailDecayPerBC[gainIndex] * accumulatedUndershoot[gainIndex]
659 * tailAtPulseTime / gainRatio;
661 sample =
m_tsg->fillSampleWithUndershoot(samplingPhase, ped,
662 m_PUAmp[ros-1][drawer][channel],
665 gain, accumulatedUndershoot[gain], pulseTiming);
667 sample =
m_tsg->fillSample(samplingPhase, ped,
668 m_PUAmp[ros-1][drawer][channel],
681 sample =
m_tsg->fillSampleWithUndershoot(samplingPhase, ped,
682 m_PUAmp[ros-1][drawer][channel],
687 sample =
m_tsg->fillSample(samplingPhase, ped,
688 m_PUAmp[ros-1][drawer][channel],
706 isHGSaturated =
true;
711 samples.push_back(sample);
713 ATH_MSG_VERBOSE(
"New chain ADC " << ros <<
"/" << drawer <<
"/" << channel <<
" - saving gain " << gain);
718 ATH_CHECK(digitsContainer->push_back(digit));
721 const double truePhase = samplingPhase + truthPulseTimeOffset;
722 auto rawChannel = std::make_unique<TileRawChannel>(
728 ATH_CHECK(rawChannelContainer->push_back(std::move(rawChannel)));
749 return StatusCode::SUCCESS;
765 return StatusCode::SUCCESS;
769 file =
new TFile(fileName.c_str());
770 if (
file->IsZombie()) {
771 ATH_MSG_FATAL(
"Error reading amplitude distribution from " << fileName <<
".");
774 TKey *key =
file->FindKey(histName.c_str());
776 ATH_MSG_FATAL(
"Could not find histogram " << histName <<
" in file " << fileName <<
".");
779 hist = (TH1F*)
file->Get(histName.c_str());
781 hist->SetBinContent(i, 0.);
788 std::string histName;
792 file =
new TFile(fileName.c_str());
793 if (
file->IsZombie()) {
794 ATH_MSG_FATAL(
"Error reading amplitude distributions from " << fileName <<
".");
798 for(
int ros=0; ros<4; ros++){
800 hists.push_back(std::vector<TH1F*>());
801 for(
int channel=0; channel<48; channel++){
803 histName =
"ene_ros_" + std::to_string(ros+1) +
"_channel_" + std::to_string(channel+1);
805 key =
file->FindKey(histName.c_str());
807 ATH_MSG_FATAL(
"Could not find histogram " << histName <<
" in file " << fileName <<
".");
811 hist = (TH1F*)
file->Get(histName.c_str());
814 hist->SetBinContent(i, 0.);
816 hists[ros].push_back(hist);
825 double pulseTimeOffset = 0.0;
829 return pulseTimeOffset;
845 for (
int imu = 0; imu<mu; imu++){
861 ATH_MSG_VERBOSE(
"Random amplitudes for PU: " << amp_1 <<
" " << amp_2);
911 double scaleFactor =
m_PhaseII ? 40 : 64;
912 n_inTimeAmp /= scaleFactor;
928 for (
int imu = 0; imu<mu; imu++){
936 m_PUAmp[ros-1][drawer][channel].front() += amp;
942 m_PUAmp[ros-1][drawer][channel].front() /= 64;
945 m_PUAmp[ros-1][drawer][channel].front() /= 40;
949 if(
m_PUAmp[ros-1][drawer][channel].front() < 0)
m_PUAmp[ros-1][drawer][channel].front() = 0;
#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.
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
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
virtual ~TileDigitsFromPulse()
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
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