CMS 3D CMS Logo

PileUp.cc
Go to the documentation of this file.
21 
24 
30 
31 #include "CLHEP/Random/RandPoissonQ.h"
32 #include "CLHEP/Random/RandPoisson.h"
33 
35 
36 #include <algorithm>
37 #include <memory>
38 #include "TMath.h"
39 
44 /*************************************************************************
45  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
46  * All rights reserved. *
47  * *
48  * For the licensing terms see $ROOTSYS/LICENSE. *
49  * For the list of contributors see $ROOTSYS/README/CREDITS. *
50  *************************************************************************/
51 
52 static Double_t GetRandom(TH1* th1, CLHEP::HepRandomEngine* rng) {
53  Int_t nbinsx = th1->GetNbinsX();
54  Double_t* fIntegral = th1->GetIntegral();
55  Double_t integral = fIntegral[nbinsx];
56 
57  if (integral == 0)
58  return 0;
59 
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]);
65  return x;
66 }
68 
69 namespace edm {
71  const std::shared_ptr<PileUpConfig>& config,
73  const bool mixingConfigFromDB)
74  : type_(pset.getParameter<std::string>("type")),
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"),
84  fileNameHash_(0U),
85  productRegistry_(new SignallingProductRegistry),
87  ->makeVectorInputSource(
89  .release()),
90  processConfiguration_(new ProcessConfiguration(std::string("@MIXING"), getReleaseVersion(), getPassID())),
91  processContext_(new ProcessContext()),
92  eventPrincipal_(),
93  lumiPrincipal_(),
94  runPrincipal_(),
95  provider_(),
96  PoissonDistribution_(),
97  PoissonDistr_OOT_(),
98  randomEngine_(),
99  playback_(config->playback_),
100  sequential_(pset.getUntrackedParameter<bool>("sequential", false)) {
101  if (mixingConfigFromDB) {
103  }
104 
105  // Use the empty parameter set for the parameter set ID of our "@MIXING" process.
107  processContext_->setProcessConfiguration(processConfiguration_.get());
108 
109  if (pset.existsAs<std::vector<ParameterSet> >("producers", true)) {
110  std::vector<ParameterSet> producers = pset.getParameter<std::vector<ParameterSet> >("producers");
111 
112  std::vector<std::string> names;
113  names.reserve(producers.size());
114  std::transform(producers.begin(), producers.end(), std::back_inserter(names), [](edm::ParameterSet const& iPSet) {
115  return iPSet.getParameter<std::string>("@module_label");
116  });
117  auto randomGenerator = std::make_unique<PileupRandomNumberGenerator>(names);
118  randomGenerator_ = randomGenerator.get();
119  std::unique_ptr<edm::RandomNumberGenerator> baseGen = std::move(randomGenerator);
121  std::move(baseGen), edm::ServiceRegistry::instance().presentToken(), true);
122 
123  provider_ = std::make_unique<SecondaryEventProvider>(producers, *productRegistry_, processConfiguration_);
124  }
125 
126  productRegistry_->setFrozen();
127 
128  // A modified HistoryAppender must be used for unscheduled processing.
129  eventPrincipal_ = std::make_unique<EventPrincipal>(input_->productRegistry(),
130  std::make_shared<BranchIDListHelper>(),
131  std::make_shared<ThinnedAssociationsHelper>(),
133  nullptr);
134 
135  bool DB = type_ == "readDB";
136 
137  if (pset.exists("nbPileupEvents")) {
138  if (0 != pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed", 0)) {
139  edm::LogWarning("MixingModule") << "Parameter nbPileupEvents.seed is not supported";
140  }
141  }
142 
144  if (!rng.isAvailable()) {
145  throw cms::Exception("Configuration")
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.";
149  }
150 
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";
155  }
156 
157  if (!DB) {
158  manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
159 
160  // Check for string describing special processing. Add these here for individual cases
161  PU_Study_ = false;
162  Study_type_ = pset.getUntrackedParameter<std::string>("Special_Pileup_Studies", "");
163 
164  if (Study_type_ == "Fixed_ITPU_Vary_OOTPU") {
165  PU_Study_ = true;
166  intFixed_ITPU_ = pset.getUntrackedParameter<int>("intFixed_ITPU", 0);
167  }
168 
169  if (manage_OOT_) { // figure out what the parameters are
170 
171  // if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
172  // << " manage_OOT option not allowed with playback ";
173 
174  std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");
175 
176  if (OOT_type == "Poisson" || OOT_type == "poisson") {
177  poisson_OOT_ = true;
178  } else if (OOT_type == "Fixed" || OOT_type == "fixed") {
179  fixed_OOT_ = true;
180  // read back the fixed number requested out-of-time
181  intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
182  if (intFixed_OOT_ < 0) {
183  throw cms::Exception("Illegal parameter value", "PileUp::PileUp(ParameterSet const& pset)")
184  << " Fixed out-of-time pileup requested, but no fixed value given ";
185  }
186  } else {
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";
190  }
191  edm::LogInfo("MixingModule") << " Out-of-time pileup will be generated with a " << OOT_type
192  << " distribution. ";
193  }
194  }
195 
196  if (Source_type_ == "cosmics") { // allow for some extra flexibility for mixing
197  minBunch_cosmics_ = pset.getUntrackedParameter<int>("minBunch_cosmics", -1000);
198  maxBunch_cosmics_ = pset.getUntrackedParameter<int>("maxBunch_cosmics", 1000);
199  }
200  } // end of constructor
201 
203  input_->doBeginJob();
204  if (provider_.get() != nullptr) {
207  provider_->beginJob(*productRegistry_, iES, globalContext);
208  }
209  }
210 
212  auto iID = eventPrincipal_->streamID(); // each producer has its own workermanager, so use default streamid
213  streamContext_.reset(new StreamContext(iID, processContext_.get()));
215  if (provider_.get() != nullptr) {
217  provider_->beginStream(iID, *streamContext_);
218  }
219  }
220 
222  ExceptionCollector exceptionCollector(
223  "Multiple exceptions were thrown while executing PileUp::endStream. An exception message follows for "
224  "each.\n");
225  endStream(exceptionCollector);
226 
227  if (exceptionCollector.hasThrown()) {
228  exceptionCollector.rethrow();
229  }
230  }
231 
232  void PileUp::endStream(ExceptionCollector& exceptionCollector) {
233  if (provider_.get() != nullptr) {
236  provider_->endStream(streamContext_->streamID(), *streamContext_, exceptionCollector);
237  // This is kind of strange, end of job running as part of endStream multiple times...
238  // For the moment, I'm leaving this as is but maybe we should think about this...
239  // I think nothing uses this code anymore anyway...
241  provider_->endJob(exceptionCollector, globalContext);
242  }
243  input_->doEndJob();
244  }
245 
247  if (provider_.get() != nullptr) {
249  runPrincipal_->setAux(run.runAuxiliary());
252  provider_->beginRun(*runPrincipal_, setup.impl(), run.moduleCallingContext(), *streamContext_);
253  }
254  }
256  if (provider_.get() != nullptr) {
258  lumiPrincipal_->setAux(lumi.luminosityBlockAuxiliary());
259  lumiPrincipal_->setRunPrincipal(runPrincipal_);
263  provider_->beginLuminosityBlock(*lumiPrincipal_, setup.impl(), lumi.moduleCallingContext(), *streamContext_);
264  }
265  }
266 
268  if (provider_.get() != nullptr) {
271  provider_->endRun(*runPrincipal_, setup.impl(), run.moduleCallingContext(), *streamContext_);
272  }
273  }
275  if (provider_.get() != nullptr) {
278  provider_->endLuminosityBlock(*lumiPrincipal_, setup.impl(), lumi.moduleCallingContext(), *streamContext_);
279  }
280  }
281 
283  if (provider_.get() != nullptr) {
284  // note: run and lumi numbers must be modified to match lumiPrincipal_
285  eventPrincipal_->setLuminosityBlockPrincipal(lumiPrincipal_.get());
286  eventPrincipal_->setRunAndLumiNumber(lumiPrincipal_->run(), lumiPrincipal_->luminosityBlock());
289  provider_->setupPileUpEvent(*eventPrincipal_, setup.impl(), *streamContext_);
290  }
291  }
292 
294  //get the required parameters from DB.
295  const MixingInputConfig& config = setup.getData(configToken_).config(inputType_);
296 
297  //get the type
298  type_ = config.type();
299  //set booleans
300  histoDistribution_ = type_ == "histo";
301  probFunctionDistribution_ = type_ == "probFunction";
302  poisson_ = type_ == "poisson";
303  fixed_ = type_ == "fixed";
304  none_ = type_ == "none";
305 
306  if (histoDistribution_)
307  edm::LogError("MisConfiguration") << "type histo cannot be reloaded from DB, yet";
308 
309  if (fixed_) {
311  } else if (poisson_) {
312  averageNumber_ = config.averageNumber();
313  if (PoissonDistribution_) {
314  PoissonDistribution_ = std::make_unique<CLHEP::RandPoissonQ>(PoissonDistribution_->engine(), averageNumber_);
315  }
316  } else if (probFunctionDistribution_) {
317  //need to reload the histogram from DB
318  const std::vector<int>& dataProbFunctionVar = config.probFunctionVariable();
319  std::vector<double> dataProb = config.probValue();
320 
321  int varSize = (int)dataProbFunctionVar.size();
322  int probSize = (int)dataProb.size();
323 
324  if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
325  throw cms::Exception("BadProbFunction")
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."
328  << std::endl;
329 
330  // Complete the vector containing the probability function data
331  // with the values "0"
332  if (probSize < varSize) {
333  edm::LogWarning("MixingModule") << " The probability function data will be completed with "
334  << (varSize - probSize) << " values 0.";
335 
336  for (int i = 0; i < (varSize - probSize); i++)
337  dataProb.push_back(0);
338 
339  probSize = dataProb.size();
340  edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
341  }
342 
343  // Create an histogram with the data from the probability function provided by the user
344  int xmin = (int)dataProbFunctionVar[0];
345  int xmax = (int)dataProbFunctionVar[varSize - 1] + 1; // need upper edge to be one beyond last value
346  int numBins = varSize;
347 
348  edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range (" << xmin
349  << "," << xmax << ")." << std::endl;
350 
351  histo_.reset(new TH1F("h", "Histo from the user's probability function", numBins, xmin, xmax));
352 
353  LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
354 
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,
358  dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
359  }
360 
361  // Check if the histogram is normalized
362  if (std::abs(histo_->Integral() - 1) > 1.0e-02) {
363  throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << std::endl;
364  }
365  averageNumber_ = histo_->GetMean();
366  }
367 
368  int oot = config.outOfTime();
369  manage_OOT_ = false;
370  if (oot == 1) {
371  manage_OOT_ = true;
372  poisson_OOT_ = false;
373  fixed_OOT_ = true;
374  intFixed_OOT_ = config.fixedOutOfTime();
375  } else if (oot == 2) {
376  manage_OOT_ = true;
377  poisson_OOT_ = true;
378  fixed_OOT_ = false;
379  }
380  }
382 
383  std::unique_ptr<CLHEP::RandPoissonQ> const& PileUp::poissonDistribution(StreamID const& streamID) {
384  if (!PoissonDistribution_) {
385  CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
386  PoissonDistribution_ = std::make_unique<CLHEP::RandPoissonQ>(engine, averageNumber_);
387  }
388  return PoissonDistribution_;
389  }
390 
391  std::unique_ptr<CLHEP::RandPoisson> const& PileUp::poissonDistr_OOT(StreamID const& streamID) {
392  if (!PoissonDistr_OOT_) {
393  CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
394  PoissonDistr_OOT_ = std::make_unique<CLHEP::RandPoisson>(engine);
395  }
396  return PoissonDistr_OOT_;
397  }
398 
399  CLHEP::HepRandomEngine* PileUp::randomEngine(StreamID const& streamID) {
400  if (!randomEngine_) {
402  randomEngine_ = &rng->getEngine(streamID);
403  }
404  return randomEngine_;
405  }
406 
410  randomGenerator_->setSeed(rng->mySeed());
411  randomGenerator_->setEngine(rng->getEngine(iLumi.index()));
412  }
413 
414  void PileUp::CalculatePileup(int MinBunch,
415  int MaxBunch,
416  std::vector<int>& PileupSelection,
417  std::vector<float>& TrueNumInteractions,
418  StreamID const& streamID) {
419  // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
420  // crossing zero first, save it for later.
421 
422  int nzero_crossing = -1;
423  double Fnzero_crossing = -1;
424 
425  if (provider_) {
426  setRandomEngine(streamID);
427  }
428 
429  if (manage_OOT_) {
430  if (none_) {
431  nzero_crossing = 0;
432  } else if (poisson_) {
433  nzero_crossing = poissonDistribution(streamID)->fire();
434  } else if (fixed_) {
435  nzero_crossing = intAverage_;
437  // RANDOM_NUMBER_ERROR
438  // Random number should be generated by the engines from the
439  // RandomNumberGeneratorService. This appears to use the global
440  // engine in ROOT. This is not thread safe unless the module using
441  // it is a one module and declares a shared resource and all
442  // other modules using it also declare the same shared resource.
443  // This also breaks replay.
444  double d = GetRandom(histo_.get(), randomEngine(streamID));
445  //n = (int) floor(d + 0.5); // incorrect for bins with integer edges
446  Fnzero_crossing = d;
447  nzero_crossing = int(d);
448  }
449  }
450 
451  for (int bx = MinBunch; bx < MaxBunch + 1; ++bx) {
452  if (manage_OOT_) {
453  if (bx == 0 && !poisson_OOT_) {
454  PileupSelection.push_back(nzero_crossing);
455  TrueNumInteractions.push_back(nzero_crossing);
456  } else {
457  if (poisson_OOT_) {
458  if (PU_Study_ && (Study_type_ == "Fixed_ITPU_Vary_OOTPU") && bx == 0) {
459  PileupSelection.push_back(intFixed_ITPU_);
460  } else {
461  PileupSelection.push_back(poissonDistr_OOT(streamID)->fire(Fnzero_crossing));
462  }
463  TrueNumInteractions.push_back(Fnzero_crossing);
464  } else {
465  PileupSelection.push_back(intFixed_OOT_);
466  TrueNumInteractions.push_back(intFixed_OOT_);
467  }
468  }
469  } else {
470  if (none_) {
471  PileupSelection.push_back(0);
472  TrueNumInteractions.push_back(0.);
473  } else if (poisson_) {
474  PileupSelection.push_back(poissonDistribution(streamID)->fire());
475  TrueNumInteractions.push_back(averageNumber_);
476  } else if (fixed_) {
477  PileupSelection.push_back(intAverage_);
478  TrueNumInteractions.push_back(intAverage_);
480  // RANDOM_NUMBER_ERROR
481  // Random number should be generated by the engines from the
482  // RandomNumberGeneratorService. This appears to use the global
483  // engine in ROOT. This is not thread safe unless the module using
484  // it is a one module and declares a shared resource and all
485  // other modules using it also declare the same shared resource.
486  // This also breaks replay.
487  double d = GetRandom(histo_.get(), randomEngine(streamID));
488  PileupSelection.push_back(int(d));
489  TrueNumInteractions.push_back(d);
490  }
491  }
492  }
493  }
494 
495 } //namespace edm
bool manage_OOT_
Definition: PileUp.h:129
std::string getPassID()
Definition: GetPassID.h:7
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:154
void beginJob(eventsetup::ESRecordsToProductResolverIndices const &)
Definition: PileUp.cc:202
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_
Definition: PileUp.h:146
double averageNumber() const
Definition: PileUp.h:71
unsigned int inputType_
Definition: PileUp.h:118
static Double_t GetRandom(TH1 *th1, CLHEP::HepRandomEngine *rng)
Definition: PileUp.cc:52
void beginStream(edm::StreamID)
Definition: PileUp.cc:211
std::optional< ServiceToken > serviceToken_
Definition: PileUp.h:153
bool fixed_
Definition: PileUp.h:127
Definition: config.py:1
Log< level::Error, false > LogError
PileUp(ParameterSet const &pset, const std::shared_ptr< PileUpConfig > &config, edm::ConsumesCollector iC, const bool mixingConfigFromDB)
Definition: PileUp.cc:70
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:383
const std::string names[nVars_]
int const intAverage_
Definition: PileUp.h:122
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:123
bool probFunctionDistribution_
Definition: PileUp.h:125
int maxBunch_cosmics_
Definition: PileUp.h:140
std::string Study_type_
Definition: PileUp.h:134
T getUntrackedParameter(std::string const &, T const &) const
bool histoDistribution_
Definition: PileUp.h:124
bool poisson_
Definition: PileUp.h:126
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:274
PileupRandomNumberGenerator * randomGenerator_
Definition: PileUp.h:152
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:145
bool poisson_OOT_
Definition: PileUp.h:130
static ServiceRegistry & instance()
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static ServiceToken createContaining(std::unique_ptr< T > iService)
create a service token that holds the service defined by iService
int intFixed_ITPU_
Definition: PileUp.h:137
void setEngine(CLHEP::HepRandomEngine const &)
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:246
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
Definition: PileUp.cc:414
bool PU_Study_
Definition: PileUp.h:133
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:255
bool fixed_OOT_
Definition: PileUp.h:131
d
Definition: ztail.py:151
int minBunch_cosmics_
Definition: PileUp.h:139
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:70
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
Definition: PileUp.cc:391
std::unique_ptr< CLHEP::RandPoisson > PoissonDistr_OOT_
Definition: PileUp.h:156
Log< level::Info, false > LogInfo
std::string getReleaseVersion()
CLHEP::HepRandomEngine * randomEngine_
Definition: PileUp.h:157
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:282
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:150
std::string type_
Definition: PileUp.h:119
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:267
void setRandomEngine(StreamID)
Definition: PileUp.cc:407
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:144
std::string Source_type_
Definition: PileUp.h:120
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:293
HLT enums.
bool none_
Definition: PileUp.h:128
T const & get(Event const &event, InputTag const &tag) noexcept(false)
Definition: Event.h:666
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:399
float x
std::unique_ptr< CLHEP::RandPoissonQ > PoissonDistribution_
Definition: PileUp.h:155
bool isAvailable() const
Definition: Service.h:40
void endStream()
Definition: PileUp.cc:221
int intFixed_OOT_
Definition: PileUp.h:136
std::shared_ptr< ProcessContext > processContext_
Definition: PileUp.h:147
Log< level::Warning, false > LogWarning
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:149
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:151
static ParameterSetID emptyParameterSetID()
Definition: ParameterSet.cc:94
def move(src, dest)
Definition: eostools.py:511
std::shared_ptr< StreamContext > streamContext_
Definition: PileUp.h:148
double averageNumber_
Definition: PileUp.h:121
Definition: Run.h:45
edm::ESGetToken< MixingModuleConfig, MixingRcd > configToken_
Definition: PileUp.h:142
#define LogDebug(id)
unsigned transform(const HcalDetId &id, unsigned transformCode)