CMS 3D CMS Logo

PileUp.cc
Go to the documentation of this file.
19 
22 
28 
29 #include "CLHEP/Random/RandPoissonQ.h"
30 #include "CLHEP/Random/RandPoisson.h"
31 
32 #include <algorithm>
33 #include <memory>
34 #include "TMath.h"
35 
40 /*************************************************************************
41  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
42  * All rights reserved. *
43  * *
44  * For the licensing terms see $ROOTSYS/LICENSE. *
45  * For the list of contributors see $ROOTSYS/README/CREDITS. *
46  *************************************************************************/
47 
48 static Double_t GetRandom(TH1* th1, CLHEP::HepRandomEngine* rng)
49 {
50  Int_t nbinsx = th1->GetNbinsX();
51  Double_t* fIntegral = th1->GetIntegral();
52  Double_t integral = fIntegral[nbinsx];
53 
54  if (integral == 0) return 0;
55 
56  Double_t r1 = rng->flat();
57  Int_t ibin = TMath::BinarySearch(nbinsx,fIntegral,r1);
58  Double_t x = th1->GetBinLowEdge(ibin+1);
59  if (r1 > fIntegral[ibin])
60  x += th1->GetBinWidth(ibin+1)*(r1-fIntegral[ibin])/(fIntegral[ibin+1] - fIntegral[ibin]);
61  return x;
62 }
64 
65 namespace edm {
66  PileUp::PileUp(ParameterSet const& pset, const std::shared_ptr<PileUpConfig>& config) :
67  type_(pset.getParameter<std::string>("type")),
68  Source_type_(config->sourcename_),
69  averageNumber_(config->averageNumber_),
70  intAverage_(static_cast<int>(averageNumber_)),
71  histo_(config->histo_),
72  histoDistribution_(type_ == "histo"),
73  probFunctionDistribution_(type_ == "probFunction"),
74  poisson_(type_ == "poisson"),
75  fixed_(type_ == "fixed"),
76  none_(type_ == "none"),
77  fileNameHash_(0U),
78  productRegistry_(new SignallingProductRegistry),
79  input_(VectorInputSourceFactory::get()->makeVectorInputSource(pset, VectorInputSourceDescription(
80  productRegistry_, edm::PreallocationConfiguration())).release()),
81  processConfiguration_(new ProcessConfiguration(std::string("@MIXING"), getReleaseVersion(), getPassID())),
82  processContext_(new ProcessContext()),
83  eventPrincipal_(),
84  lumiPrincipal_(),
85  runPrincipal_(),
86  provider_(),
87  PoissonDistribution_(),
88  PoissonDistr_OOT_(),
89  randomEngine_(),
90  playback_(config->playback_),
91  sequential_(pset.getUntrackedParameter<bool>("sequential", false)) {
92 
93  // Use the empty parameter set for the parameter set ID of our "@MIXING" process.
95  processContext_->setProcessConfiguration(processConfiguration_.get());
96 
97  if(pset.existsAs<std::vector<ParameterSet> >("producers", true)) {
98  std::vector<ParameterSet> producers = pset.getParameter<std::vector<ParameterSet> >("producers");
100  }
101 
102  productRegistry_->setFrozen();
103 
104  // A modified HistoryAppender must be used for unscheduled processing.
105  eventPrincipal_.reset(new EventPrincipal(input_->productRegistry(),
106  std::make_shared<BranchIDListHelper>(),
107  std::make_shared<ThinnedAssociationsHelper>(),
109  nullptr));
110 
111  bool DB=type_=="readDB";
112 
113  if (pset.exists("nbPileupEvents")) {
114  if (0 != pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed",0)) {
115  edm::LogWarning("MixingModule") << "Parameter nbPileupEvents.seed is not supported";
116  }
117  }
118 
120  if (!rng.isAvailable()) {
121  throw cms::Exception("Configuration")
122  << "PileUp requires the RandomNumberGeneratorService\n"
123  "which is not present in the configuration file. You must add the service\n"
124  "in the configuration file or remove the modules that require it.";
125  }
126 
128  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
129  << "'type' parameter (a string) has a value of '" << type_ << "'.\n"
130  << "Legal values are 'poisson', 'fixed', or 'none'\n";
131  }
132 
133  if (!DB){
134  manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
135 
136  // Check for string describing special processing. Add these here for individual cases
137  PU_Study_ = false;
138  Study_type_ = pset.getUntrackedParameter<std::string>("Special_Pileup_Studies", "");
139 
140  if(Study_type_ == "Fixed_ITPU_Vary_OOTPU") {
141  PU_Study_ = true;
142  intFixed_ITPU_ = pset.getUntrackedParameter<int>("intFixed_ITPU", 0);
143  }
144 
145  if(manage_OOT_) { // figure out what the parameters are
146 
147  // if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
148  // << " manage_OOT option not allowed with playback ";
149 
151 
152  if(OOT_type == "Poisson" || OOT_type == "poisson") {
153  poisson_OOT_ = true;
154  }
155  else if(OOT_type == "Fixed" || OOT_type == "fixed") {
156  fixed_OOT_ = true;
157  // read back the fixed number requested out-of-time
158  intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
159  if(intFixed_OOT_ < 0) {
160  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
161  << " Fixed out-of-time pileup requested, but no fixed value given ";
162  }
163  } else {
164  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
165  << "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
166  << "Legal values are 'poisson' or 'fixed'\n";
167  }
168  edm::LogInfo("MixingModule") <<" Out-of-time pileup will be generated with a " << OOT_type << " distribution. " ;
169  }
170  }
171 
172  if(Source_type_ == "cosmics") { // allow for some extra flexibility for mixing
173  minBunch_cosmics_ = pset.getUntrackedParameter<int>("minBunch_cosmics", -1000);
174  maxBunch_cosmics_ = pset.getUntrackedParameter<int>("maxBunch_cosmics", 1000);
175  }
176  } // end of constructor
177 
179  auto iID = eventPrincipal_->streamID(); // each producer has its own workermanager, so use default streamid
180  streamContext_.reset(new StreamContext(iID, processContext_.get()));
181  input_->doBeginJob();
182  if (provider_.get() != nullptr) {
183  provider_->beginJob(*productRegistry_);
184  provider_->beginStream(iID, *streamContext_);
185  }
186  }
187 
189  if (provider_.get() != nullptr) {
190  provider_->endStream(streamContext_->streamID(), *streamContext_);
191  provider_->endJob();
192  }
193  input_->doEndJob();
194  }
195 
197  if (provider_.get() != nullptr) {
198  auto aux = std::make_shared<RunAuxiliary>(run.runAuxiliary());
200  provider_->beginRun(*runPrincipal_, setup, run.moduleCallingContext(), *streamContext_);
201  }
202  }
204  if (provider_.get() != nullptr) {
205  auto aux = std::make_shared<LuminosityBlockAuxiliary>(lumi.luminosityBlockAuxiliary());
207  lumiPrincipal_->setRunPrincipal(runPrincipal_);
208  provider_->beginLuminosityBlock(*lumiPrincipal_, setup, lumi.moduleCallingContext(), *streamContext_);
209  }
210  }
211 
213  if (provider_.get() != nullptr) {
215  }
216  }
218  if (provider_.get() != nullptr) {
219  provider_->endLuminosityBlock(*lumiPrincipal_, setup, lumi.moduleCallingContext(), *streamContext_);
220  }
221  }
222 
224  if (provider_.get() != nullptr) {
225  // note: run and lumi numbers must be modified to match lumiPrincipal_
226  eventPrincipal_->setLuminosityBlockPrincipal(lumiPrincipal_);
227  eventPrincipal_->setRunAndLumiNumber(lumiPrincipal_->run(), lumiPrincipal_->luminosityBlock());
228  provider_->setupPileUpEvent(*eventPrincipal_, setup, *streamContext_);
229  }
230  }
231 
233  //get the required parameters from DB.
235  setup.get<MixingRcd>().get(configM);
236 
237  const MixingInputConfig & config=configM->config(inputType_);
238 
239  //get the type
240  type_=config.type();
241  //set booleans
242  histoDistribution_=type_ == "histo";
243  probFunctionDistribution_=type_ == "probFunction";
244  poisson_=type_ == "poisson";
245  fixed_=type_ == "fixed";
246  none_=type_ == "none";
247 
248  if (histoDistribution_) edm::LogError("MisConfiguration")<<"type histo cannot be reloaded from DB, yet";
249 
250  if (fixed_){
252  }
253  else if (poisson_)
254  {
255  averageNumber_=config.averageNumber();
257  PoissonDistribution_.reset(new CLHEP::RandPoissonQ(PoissonDistribution_->engine(), averageNumber_));
258  }
259  }
260  else if (probFunctionDistribution_)
261  {
262  //need to reload the histogram from DB
263  const std::vector<int> & dataProbFunctionVar = config.probFunctionVariable();
264  std::vector<double> dataProb = config.probValue();
265 
266  int varSize = (int) dataProbFunctionVar.size();
267  int probSize = (int) dataProb.size();
268 
269  if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
270  throw cms::Exception("BadProbFunction") << "Please, check the variables of the probability function! The first variable should be 0 and the difference between two variables should be 1." << std::endl;
271 
272  // Complete the vector containing the probability function data
273  // with the values "0"
274  if (probSize < varSize){
275  edm::LogWarning("MixingModule") << " The probability function data will be completed with " <<(varSize - probSize) <<" values 0.";
276 
277  for (int i=0; i<(varSize - probSize); i++) dataProb.push_back(0);
278 
279  probSize = dataProb.size();
280  edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
281  }
282 
283  // Create an histogram with the data from the probability function provided by the user
284  int xmin = (int) dataProbFunctionVar[0];
285  int xmax = (int) dataProbFunctionVar[varSize-1]+1; // need upper edge to be one beyond last value
286  int numBins = varSize;
287 
288  edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range ("<< xmin << "," << xmax << ")." << std::endl;
289 
290  histo_.reset(new TH1F("h","Histo from the user's probability function",numBins,xmin,xmax));
291 
292  LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
293 
294  for (int j=0; j < numBins ; j++){
295  LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j ]<< " P(x) = " << dataProb[j];
296  histo_->Fill(dataProbFunctionVar[j]+0.5,dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
297  }
298 
299  // Check if the histogram is normalized
300  if (std::abs(histo_->Integral() - 1) > 1.0e-02){
301  throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << std::endl;
302  }
303  averageNumber_=histo_->GetMean();
304  }
305 
306  int oot=config.outOfTime();
307  manage_OOT_=false;
308  if (oot==1)
309  {
310  manage_OOT_=true;
311  poisson_OOT_ = false;
312  fixed_OOT_ = true;
313  intFixed_OOT_=config.fixedOutOfTime();
314  }
315  else if (oot==2)
316  {
317  manage_OOT_=true;
318  poisson_OOT_ = true;
319  fixed_OOT_ = false;
320  }
321 
322 
323  }
325  }
326 
327  std::unique_ptr<CLHEP::RandPoissonQ> const& PileUp::poissonDistribution(StreamID const& streamID) {
328  if(!PoissonDistribution_) {
329  CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
330  PoissonDistribution_.reset(new CLHEP::RandPoissonQ(engine, averageNumber_));
331  }
332  return PoissonDistribution_;
333  }
334 
335  std::unique_ptr<CLHEP::RandPoisson> const& PileUp::poissonDistr_OOT(StreamID const& streamID) {
336  if(!PoissonDistr_OOT_) {
337  CLHEP::HepRandomEngine& engine = *randomEngine(streamID);
338  PoissonDistr_OOT_.reset(new CLHEP::RandPoisson(engine));
339  }
340  return PoissonDistr_OOT_;
341  }
342 
343  CLHEP::HepRandomEngine* PileUp::randomEngine(StreamID const& streamID) {
344  if(!randomEngine_) {
346  randomEngine_ = &rng->getEngine(streamID);
347  }
348  return randomEngine_;
349  }
350 
351  void PileUp::CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions, StreamID const& streamID) {
352 
353  // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
354  // crossing zero first, save it for later.
355 
356  int nzero_crossing = -1;
357  double Fnzero_crossing = -1;
358 
359  if(manage_OOT_) {
360  if (none_){
361  nzero_crossing = 0;
362  }else if (poisson_){
363  nzero_crossing = poissonDistribution(streamID)->fire() ;
364  }else if (fixed_){
365  nzero_crossing = intAverage_ ;
367  // RANDOM_NUMBER_ERROR
368  // Random number should be generated by the engines from the
369  // RandomNumberGeneratorService. This appears to use the global
370  // engine in ROOT. This is not thread safe unless the module using
371  // it is a one module and declares a shared resource and all
372  // other modules using it also declare the same shared resource.
373  // This also breaks replay.
374  double d = GetRandom(histo_.get(), randomEngine(streamID));
375  //n = (int) floor(d + 0.5); // incorrect for bins with integer edges
376  Fnzero_crossing = d;
377  nzero_crossing = int(d);
378  }
379 
380  }
381 
382  for(int bx = MinBunch; bx < MaxBunch+1; ++bx) {
383 
384  if(manage_OOT_) {
385  if(bx==0 && !poisson_OOT_) {
386  PileupSelection.push_back(nzero_crossing) ;
387  TrueNumInteractions.push_back( nzero_crossing );
388  }
389  else{
390  if(poisson_OOT_) {
391  if(PU_Study_ && (Study_type_ == "Fixed_ITPU_Vary_OOTPU" ) && bx==0 ) {
392  PileupSelection.push_back(intFixed_ITPU_) ;
393  }
394  else{
395  PileupSelection.push_back(poissonDistr_OOT(streamID)->fire(Fnzero_crossing)) ;
396  }
397  TrueNumInteractions.push_back( Fnzero_crossing );
398  }
399  else {
400  PileupSelection.push_back(intFixed_OOT_) ;
401  TrueNumInteractions.push_back( intFixed_OOT_ );
402  }
403  }
404  }
405  else {
406  if (none_){
407  PileupSelection.push_back(0);
408  TrueNumInteractions.push_back( 0. );
409  }else if (poisson_){
410  PileupSelection.push_back(poissonDistribution(streamID)->fire());
411  TrueNumInteractions.push_back( averageNumber_ );
412  }else if (fixed_){
413  PileupSelection.push_back(intAverage_);
414  TrueNumInteractions.push_back( intAverage_ );
416  // RANDOM_NUMBER_ERROR
417  // Random number should be generated by the engines from the
418  // RandomNumberGeneratorService. This appears to use the global
419  // engine in ROOT. This is not thread safe unless the module using
420  // it is a one module and declares a shared resource and all
421  // other modules using it also declare the same shared resource.
422  // This also breaks replay.
423  double d = GetRandom(histo_.get(), randomEngine(streamID));
424  PileupSelection.push_back(int(d));
425  TrueNumInteractions.push_back( d );
426  }
427  }
428 
429  }
430  }
431 
432 
433 } //namespace edm
#define LogDebug(id)
bool manage_OOT_
Definition: PileUp.h:106
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
std::string getPassID()
Definition: GetPassID.h:8
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:129
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
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:123
unsigned int inputType_
Definition: PileUp.h:95
const int fixedOutOfTime() const
bool exists(std::string const &parameterName) const
checks if a parameter exists
static Double_t GetRandom(TH1 *th1, CLHEP::HepRandomEngine *rng)
Definition: PileUp.cc:48
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
void beginStream(edm::StreamID)
Definition: PileUp.cc:178
bool fixed_
Definition: PileUp.h:104
const std::vector< double > & probValue() const
Definition: config.py:1
const double averageNumber() const
ModuleCallingContext const * moduleCallingContext() const
int const intAverage_
Definition: PileUp.h:99
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:100
bool probFunctionDistribution_
Definition: PileUp.h:102
int maxBunch_cosmics_
Definition: PileUp.h:118
std::string Study_type_
Definition: PileUp.h:111
bool histoDistribution_
Definition: PileUp.h:101
bool poisson_
Definition: PileUp.h:103
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:217
T x() const
Cartesian x coordinate.
double averageNumber() const
Definition: PileUp.h:54
const int outOfTime() const
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:122
bool poisson_OOT_
Definition: PileUp.h:107
bool isAvailable() const
Definition: Service.h:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int intFixed_ITPU_
Definition: PileUp.h:115
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:196
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:327
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
Definition: PileUp.cc:351
bool PU_Study_
Definition: PileUp.h:110
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:203
bool fixed_OOT_
Definition: PileUp.h:108
int minBunch_cosmics_
Definition: PileUp.h:117
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:69
std::unique_ptr< CLHEP::RandPoisson > PoissonDistr_OOT_
Definition: PileUp.h:131
std::string getReleaseVersion()
CLHEP::HepRandomEngine * randomEngine_
Definition: PileUp.h:132
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:223
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:127
std::string type_
Definition: PileUp.h:96
LuminosityBlockAuxiliary const & luminosityBlockAuxiliary() const
const T & get() const
Definition: EventSetup.h:55
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:212
const std::vector< int > & probFunctionVariable() const
RunAuxiliary const & runAuxiliary() const
Definition: Run.h:59
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:121
std::string Source_type_
Definition: PileUp.h:97
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
Definition: PileUp.cc:335
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:232
HLT enums.
ModuleCallingContext const * moduleCallingContext() const
Definition: Run.h:139
bool none_
Definition: PileUp.h:105
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:343
std::unique_ptr< CLHEP::RandPoissonQ > PoissonDistribution_
Definition: PileUp.h:130
void endStream()
Definition: PileUp.cc:188
int intFixed_OOT_
Definition: PileUp.h:114
std::shared_ptr< ProcessContext > processContext_
Definition: PileUp.h:124
PileUp(ParameterSet const &pset, const std::shared_ptr< PileUpConfig > &config)
Definition: PileUp.cc:66
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:126
std::string type() const
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:128
static ParameterSetID emptyParameterSetID()
const MixingInputConfig & config(unsigned int i=0) const
std::shared_ptr< StreamContext > streamContext_
Definition: PileUp.h:125
T get(const Candidate &c)
Definition: component.h:55
double averageNumber_
Definition: PileUp.h:98
Definition: Run.h:42