31 #include "CLHEP/Random/RandPoissonQ.h" 32 #include "CLHEP/Random/RandPoisson.h" 52 static Double_t
GetRandom(TH1* th1, CLHEP::HepRandomEngine* rng) {
53 Int_t
nbinsx = th1->GetNbinsX();
54 Double_t* fIntegral = th1->GetIntegral();
60 Double_t
r1 = rng->flat();
61 Int_t ibin = TMath::BinarySearch(
nbinsx, fIntegral,
r1);
62 Double_t
x = th1->GetBinLowEdge(ibin + 1);
63 if (
r1 > fIntegral[ibin])
64 x += th1->GetBinWidth(ibin + 1) * (
r1 - fIntegral[ibin]) / (fIntegral[ibin + 1] - fIntegral[ibin]);
71 const std::shared_ptr<PileUpConfig>&
config,
73 const bool mixingConfigFromDB)
75 Source_type_(
config->sourcename_),
76 averageNumber_(
config->averageNumber_),
77 intAverage_(static_cast<
int>(averageNumber_)),
78 histo_(
std::make_shared<TH1F>(*
config->histo_)),
79 histoDistribution_(type_ ==
"histo"),
80 probFunctionDistribution_(type_ ==
"probFunction"),
81 poisson_(type_ ==
"poisson"),
82 fixed_(type_ ==
"fixed"),
83 none_(type_ ==
"none"),
87 ->makeVectorInputSource(
96 PoissonDistribution_(),
99 playback_(
config->playback_),
100 sequential_(
pset.getUntrackedParameter<
bool>(
"sequential",
false)) {
101 if (mixingConfigFromDB) {
109 if (
pset.existsAs<std::vector<ParameterSet> >(
"producers",
true)) {
110 std::vector<ParameterSet>
producers =
pset.getParameter<std::vector<ParameterSet> >(
"producers");
112 std::vector<std::string>
names;
115 return iPSet.getParameter<
std::string>(
"@module_label");
117 auto randomGenerator = std::make_unique<PileupRandomNumberGenerator>(
names);
119 std::unique_ptr<edm::RandomNumberGenerator> baseGen =
std::move(randomGenerator);
130 std::make_shared<BranchIDListHelper>(),
131 std::make_shared<ThinnedAssociationsHelper>(),
137 if (
pset.exists(
"nbPileupEvents")) {
139 edm::LogWarning(
"MixingModule") <<
"Parameter nbPileupEvents.seed is not supported";
146 <<
"PileUp requires the RandomNumberGeneratorService\n" 147 "which is not present in the configuration file. You must add the service\n" 148 "in the configuration file or remove the modules that require it.";
152 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
153 <<
"'type' parameter (a string) has a value of '" <<
type_ <<
"'.\n" 154 <<
"Legal values are 'poisson', 'fixed', or 'none'\n";
183 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
184 <<
" Fixed out-of-time pileup requested, but no fixed value given ";
187 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
188 <<
"'OOT_type' parameter (a string) has a value of '" <<
OOT_type <<
"'.\n" 189 <<
"Legal values are 'poisson' or 'fixed'\n";
192 <<
" distribution. ";
223 "Multiple exceptions were thrown while executing PileUp::endStream. An exception message follows for " 241 provider_->endJob(exceptionCollector, globalContext);
307 edm::LogError(
"MisConfiguration") <<
"type histo cannot be reloaded from DB, yet";
318 const std::vector<int>& dataProbFunctionVar =
config.probFunctionVariable();
319 std::vector<double> dataProb =
config.probValue();
321 int varSize = (
int)dataProbFunctionVar.size();
322 int probSize = (
int)dataProb.size();
324 if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
326 <<
"Please, check the variables of the probability function! The first variable should be 0 and the " 327 "difference between two variables should be 1." 332 if (probSize < varSize) {
333 edm::LogWarning(
"MixingModule") <<
" The probability function data will be completed with " 334 << (varSize - probSize) <<
" values 0.";
336 for (
int i = 0;
i < (varSize - probSize);
i++)
337 dataProb.push_back(0);
339 probSize = dataProb.size();
340 edm::LogInfo(
"MixingModule") <<
" The number of the P(x) data set after adding the values 0 is " << probSize;
344 int xmin = (
int)dataProbFunctionVar[0];
345 int xmax = (
int)dataProbFunctionVar[varSize - 1] + 1;
346 int numBins = varSize;
348 edm::LogInfo(
"MixingModule") <<
"An histogram will be created with " << numBins <<
" bins in the range (" <<
xmin 349 <<
"," <<
xmax <<
")." << std::endl;
351 histo_.reset(
new TH1F(
"h",
"Histo from the user's probability function", numBins,
xmin,
xmax));
353 LogDebug(
"MixingModule") <<
"Filling histogram with the following data:" << std::endl;
355 for (
int j = 0;
j < numBins;
j++) {
356 LogDebug(
"MixingModule") <<
" x = " << dataProbFunctionVar[
j] <<
" P(x) = " << dataProb[
j];
357 histo_->Fill(dataProbFunctionVar[
j] + 0.5,
363 throw cms::Exception(
"BadProbFunction") <<
"The probability function should be normalized!!! " << std::endl;
368 int oot =
config.outOfTime();
375 }
else if (oot == 2) {
385 CLHEP::HepRandomEngine& engine = *
randomEngine(streamID);
393 CLHEP::HepRandomEngine& engine = *
randomEngine(streamID);
416 std::vector<int>& PileupSelection,
417 std::vector<float>& TrueNumInteractions,
422 int nzero_crossing = -1;
423 double Fnzero_crossing = -1;
447 nzero_crossing =
int(
d);
451 for (
int bx = MinBunch;
bx < MaxBunch + 1; ++
bx) {
454 PileupSelection.push_back(nzero_crossing);
455 TrueNumInteractions.push_back(nzero_crossing);
461 PileupSelection.push_back(
poissonDistr_OOT(streamID)->fire(Fnzero_crossing));
463 TrueNumInteractions.push_back(Fnzero_crossing);
471 PileupSelection.push_back(0);
472 TrueNumInteractions.push_back(0.);
488 PileupSelection.push_back(
int(
d));
489 TrueNumInteractions.push_back(
d);
std::unique_ptr< SecondaryEventProvider > provider_
void beginJob(eventsetup::ESRecordsToProductResolverIndices const &)
OOT_type
manage out-of-time pileup setting this to True means that the out-of-time pileup will have a differen...
std::shared_ptr< ProcessConfiguration > processConfiguration_
double averageNumber() const
static Double_t GetRandom(TH1 *th1, CLHEP::HepRandomEngine *rng)
void beginStream(edm::StreamID)
std::optional< ServiceToken > serviceToken_
Log< level::Error, false > LogError
PileUp(ParameterSet const &pset, const std::shared_ptr< PileUpConfig > &config, edm::ConsumesCollector iC, const bool mixingConfigFromDB)
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
const std::string names[nVars_]
std::shared_ptr< TH1F > histo_
bool probFunctionDistribution_
T getUntrackedParameter(std::string const &, T const &) const
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
PileupRandomNumberGenerator * randomGenerator_
std::unique_ptr< VectorInputSource > const input_
static ServiceRegistry & instance()
Abs< T >::type abs(const T &t)
static ServiceToken createContaining(std::unique_ptr< T > iService)
create a service token that holds the service defined by iService
void setEngine(CLHEP::HepRandomEngine const &)
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Integral< F, X >::type integral(const F &f)
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
std::unique_ptr< CLHEP::RandPoisson > PoissonDistr_OOT_
Log< level::Info, false > LogInfo
std::string getReleaseVersion()
CLHEP::HepRandomEngine * randomEngine_
void setupPileUpEvent(const edm::EventSetup &setup)
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
void endRun(const edm::Run &run, const edm::EventSetup &setup)
void setRandomEngine(StreamID)
std::shared_ptr< ProductRegistry > productRegistry_
void reload(const edm::EventSetup &setup)
T const & get(Event const &event, InputTag const &tag) noexcept(false)
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
std::unique_ptr< CLHEP::RandPoissonQ > PoissonDistribution_
std::shared_ptr< ProcessContext > processContext_
Log< level::Warning, false > LogWarning
std::unique_ptr< EventPrincipal > eventPrincipal_
std::shared_ptr< RunPrincipal > runPrincipal_
static ParameterSetID emptyParameterSetID()
void setSeed(uint32_t iSeed)
std::shared_ptr< StreamContext > streamContext_
edm::ESGetToken< MixingModuleConfig, MixingRcd > configToken_