30 #include "CLHEP/Random/RandPoissonQ.h" 31 #include "CLHEP/Random/RandPoisson.h" 51 static Double_t
GetRandom(TH1* th1, CLHEP::HepRandomEngine* rng) {
52 Int_t
nbinsx = th1->GetNbinsX();
53 Double_t* fIntegral = th1->GetIntegral();
59 Double_t
r1 = rng->flat();
60 Int_t ibin = TMath::BinarySearch(
nbinsx, fIntegral,
r1);
61 Double_t
x = th1->GetBinLowEdge(ibin + 1);
62 if (
r1 > fIntegral[ibin])
63 x += th1->GetBinWidth(ibin + 1) * (
r1 - fIntegral[ibin]) / (fIntegral[ibin + 1] - fIntegral[ibin]);
70 const std::shared_ptr<PileUpConfig>&
config,
72 const bool mixingConfigFromDB)
74 Source_type_(
config->sourcename_),
75 averageNumber_(
config->averageNumber_),
76 intAverage_(static_cast<
int>(averageNumber_)),
77 histo_(
std::make_shared<TH1F>(*
config->histo_)),
78 histoDistribution_(type_ ==
"histo"),
79 probFunctionDistribution_(type_ ==
"probFunction"),
80 poisson_(type_ ==
"poisson"),
81 fixed_(type_ ==
"fixed"),
82 none_(type_ ==
"none"),
86 ->makeVectorInputSource(
95 PoissonDistribution_(),
98 playback_(
config->playback_),
99 sequential_(
pset.getUntrackedParameter<
bool>(
"sequential",
false)) {
100 if (mixingConfigFromDB) {
108 if (
pset.existsAs<std::vector<ParameterSet> >(
"producers",
true)) {
109 std::vector<ParameterSet>
producers =
pset.getParameter<std::vector<ParameterSet> >(
"producers");
111 std::vector<std::string>
names;
114 return iPSet.getParameter<
std::string>(
"@module_label");
116 auto randomGenerator = std::make_unique<PileupRandomNumberGenerator>(
names);
118 std::unique_ptr<edm::RandomNumberGenerator> baseGen =
std::move(randomGenerator);
129 std::make_shared<BranchIDListHelper>(),
130 std::make_shared<ThinnedAssociationsHelper>(),
136 if (
pset.exists(
"nbPileupEvents")) {
138 edm::LogWarning(
"MixingModule") <<
"Parameter nbPileupEvents.seed is not supported";
145 <<
"PileUp requires the RandomNumberGeneratorService\n" 146 "which is not present in the configuration file. You must add the service\n" 147 "in the configuration file or remove the modules that require it.";
151 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
152 <<
"'type' parameter (a string) has a value of '" <<
type_ <<
"'.\n" 153 <<
"Legal values are 'poisson', 'fixed', or 'none'\n";
182 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
183 <<
" Fixed out-of-time pileup requested, but no fixed value given ";
186 throw cms::Exception(
"Illegal parameter value",
"PileUp::PileUp(ParameterSet const& pset)")
187 <<
"'OOT_type' parameter (a string) has a value of '" <<
OOT_type <<
"'.\n" 188 <<
"Legal values are 'poisson' or 'fixed'\n";
191 <<
" distribution. ";
283 edm::LogError(
"MisConfiguration") <<
"type histo cannot be reloaded from DB, yet";
294 const std::vector<int>& dataProbFunctionVar =
config.probFunctionVariable();
295 std::vector<double> dataProb =
config.probValue();
297 int varSize = (
int)dataProbFunctionVar.size();
298 int probSize = (
int)dataProb.size();
300 if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
302 <<
"Please, check the variables of the probability function! The first variable should be 0 and the " 303 "difference between two variables should be 1." 308 if (probSize < varSize) {
309 edm::LogWarning(
"MixingModule") <<
" The probability function data will be completed with " 310 << (varSize - probSize) <<
" values 0.";
312 for (
int i = 0;
i < (varSize - probSize);
i++)
313 dataProb.push_back(0);
315 probSize = dataProb.size();
316 edm::LogInfo(
"MixingModule") <<
" The number of the P(x) data set after adding the values 0 is " << probSize;
320 int xmin = (
int)dataProbFunctionVar[0];
321 int xmax = (
int)dataProbFunctionVar[varSize - 1] + 1;
322 int numBins = varSize;
324 edm::LogInfo(
"MixingModule") <<
"An histogram will be created with " << numBins <<
" bins in the range (" <<
xmin 325 <<
"," <<
xmax <<
")." << std::endl;
327 histo_.reset(
new TH1F(
"h",
"Histo from the user's probability function", numBins,
xmin,
xmax));
329 LogDebug(
"MixingModule") <<
"Filling histogram with the following data:" << std::endl;
331 for (
int j = 0;
j < numBins;
j++) {
332 LogDebug(
"MixingModule") <<
" x = " << dataProbFunctionVar[
j] <<
" P(x) = " << dataProb[
j];
333 histo_->Fill(dataProbFunctionVar[
j] + 0.5,
339 throw cms::Exception(
"BadProbFunction") <<
"The probability function should be normalized!!! " << std::endl;
344 int oot =
config.outOfTime();
351 }
else if (oot == 2) {
361 CLHEP::HepRandomEngine& engine = *
randomEngine(streamID);
369 CLHEP::HepRandomEngine& engine = *
randomEngine(streamID);
392 std::vector<int>& PileupSelection,
393 std::vector<float>& TrueNumInteractions,
398 int nzero_crossing = -1;
399 double Fnzero_crossing = -1;
423 nzero_crossing =
int(
d);
427 for (
int bx = MinBunch;
bx < MaxBunch + 1; ++
bx) {
430 PileupSelection.push_back(nzero_crossing);
431 TrueNumInteractions.push_back(nzero_crossing);
437 PileupSelection.push_back(
poissonDistr_OOT(streamID)->fire(Fnzero_crossing));
439 TrueNumInteractions.push_back(Fnzero_crossing);
447 PileupSelection.push_back(0);
448 TrueNumInteractions.push_back(0.);
464 PileupSelection.push_back(
int(
d));
465 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_
LuminosityBlockIndex index() const
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_