CMS 3D CMS Logo

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