CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PileUp.cc
Go to the documentation of this file.
8 
11 
12 #include "CLHEP/Random/RandPoissonQ.h"
13 #include "CLHEP/Random/RandFlat.h"
14 
15 #include "TRandom.h"
16 #include "TFile.h"
17 #include "TH1F.h"
18 
19 #include <algorithm>
20 
21 namespace edm {
22  PileUp::PileUp(ParameterSet const& pset, int const minb, int const maxb, double averageNumber, TH1F * const histo, const bool playback) :
23  type_(pset.getParameter<std::string>("type")),
24  minBunch_(minb),
25  maxBunch_(maxb),
26  averageNumber_(averageNumber),
27  intAverage_(static_cast<int>(averageNumber)),
28  histo_(histo),
29  histoDistribution_(type_ == "histo"),
30  probFunctionDistribution_(type_ == "probFunction"),
31  poisson_(type_ == "poisson"),
32  fixed_(type_ == "fixed"),
33  none_(type_ == "none"),
34  input_(VectorInputSourceFactory::get()->makeVectorInputSource(pset, InputSourceDescription()).release()),
35  poissonDistribution_(0),
36  playback_(playback),
37  sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
38  seed_(pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed",1234))
39  {
40 
41 
43  if (!rng.isAvailable()) {
44  throw cms::Exception("Configuration")
45  << "PileUp requires the RandomNumberGeneratorService\n"
46  "which is not present in the configuration file. You must add the service\n"
47  "in the configuration file or remove the modules that require it.";
48  }
49 
50  CLHEP::HepRandomEngine& engine = rng->getEngine();
51  poissonDistribution_ = new CLHEP::RandPoissonQ(engine, averageNumber_);
52 
53  // Get seed for the case when using user histogram or probability function
55  gRandom->SetSeed(seed_);
56  LogInfo("MixingModule") << " Change seed for " << type_ << " mode. The seed is set to " << seed_;
57  }
58 
59 
61  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
62  << "'type' parameter (a string) has a value of '" << type_ << "'.\n"
63  << "Legal values are 'poisson', 'fixed', or 'none'\n";
64  }
65 
66  manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
67 
68  if(manage_OOT_) { // figure out what the parameters are
69 
70  if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
71  << " manage_OOT option not allowed with playback ";
72 
73  std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");
74 
75  if(OOT_type == "Poisson" || OOT_type == "poisson") {
76  poisson_OOT_ = true;
77  poissonDistr_OOT_ = new CLHEP::RandPoisson(engine);
78  }
79  else if(OOT_type == "Fixed" || OOT_type == "fixed") {
80  fixed_OOT_ = true;
81  // read back the fixed number requested out-of-time
82  intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
83  if(intFixed_OOT_ < 0) {
84  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
85  << " Fixed out-of-time pileup requested, but no fixed value given ";
86  }
87  }
88  else {
89  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
90  << "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
91  << "Legal values are 'poisson' or 'fixed'\n";
92  }
93  edm::LogInfo("MixingModule") <<" Out-of-time pileup will be generated with a " << OOT_type << " distribution. " ;
94  }
95 
96  }
97 
99  delete poissonDistribution_;
100  }
101 
102  void
103  PileUp::readPileUp(std::vector<EventPrincipalVector> & result,std::vector<std::vector<edm::EventID> > &ids) {
104 
105  // set up vector of event counts for each bunch crossing ahead of time, so that we can
106  // allow for an arbitrary distribution for out-of-time vs. in-time pileup
107 
108  std::vector<int> nint;
109 
110  // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
111  // crossing zero first, save it for later.
112 
113  int nzero_crossing = -1;
114 
115  if(manage_OOT_) {
116  if (none_){
117  nzero_crossing = 0;
118  }else if (poisson_){
119  nzero_crossing = poissonDistribution_->fire() ;
120  }else if (fixed_){
121  nzero_crossing = intAverage_ ;
123  double d = histo_->GetRandom();
124  //n = (int) floor(d + 0.5); // incorrect for bins with integer edges
125  nzero_crossing = int(d);
126  }
127  }
128 
129  for (int i = minBunch_; i <= maxBunch_; ++i) {
130 
131  if (playback_){
132  nint.push_back( ids[i-minBunch_].size() );
133  //} else if (sequential_) { // just read many sequentially... why specify?
134  // For now, the use case for sequential read reads only one event at a time.
135  // nint.push_back( 1 );
136  }
137  else if(manage_OOT_) {
138  if(i==0 && !poisson_OOT_) nint.push_back(nzero_crossing);
139  else{
140  if(poisson_OOT_) {
141  nint.push_back( poissonDistr_OOT_->fire(float(nzero_crossing)) );
142  }
143  else {
144  nint.push_back( intFixed_OOT_ );
145  }
146  }
147  }
148  else {
149  if (none_){
150  nint.push_back(0);
151  }else if (poisson_){
152  nint.push_back( poissonDistribution_->fire() );
153  }else if (fixed_){
154  nint.push_back( intAverage_ );
156  double d = histo_->GetRandom();
157  //n = (int) floor(d + 0.5); // incorrect for bins with integer edges
158  nint.push_back( int(d) );
159  }
160 
161  }
162  }
163 
164  int n=0;
165 
166  for (int i = minBunch_; i <= maxBunch_; ++i) {
167  EventPrincipalVector eventVector;
168 
169  n = nint[i-minBunch_];
170 
171  eventVector.reserve(n);
172  while (n > 0) {
173  EventPrincipalVector oneResult;
174  oneResult.reserve(n);
175  std::vector<edm::EventID> oneResultPlayback;
176  oneResultPlayback.reserve(n);
177  if (playback_) {
178  input_->readManySpecified(ids[i-minBunch_],oneResult); // playback
179  } else if (sequential_) {
180  unsigned int file;
181  input_->readManySequential(n, oneResult, file); // sequential
182  for (int j=0;j<(int)oneResult.size();j++){
183  oneResultPlayback.push_back(oneResult[j]->id());
184  }
185  ids[i-minBunch_] = oneResultPlayback;
186  } else {
187  unsigned int file; //FIXME: need unsigned filenr?
188  input_->readManyRandom(n, oneResult,file); //no playback
189  for (int j=0;j<(int)oneResult.size();j++){
190  oneResultPlayback.push_back(oneResult[j]->id());
191  }
192  ids[i-minBunch_] = oneResultPlayback;
193  }
194  LogDebug("readPileup") << "READ: " << oneResult.size();
195  std::copy(oneResult.begin(), oneResult.end(), std::back_inserter(eventVector));
196  n -= oneResult.size();
197  }
198  result.push_back(eventVector);
199  }
200  }
201 
202 } //namespace edm
#define LogDebug(id)
bool manage_OOT_
Definition: PileUp.h:52
int const maxBunch_
Definition: PileUp.h:43
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
bool const histoDistribution_
Definition: PileUp.h:47
list file
Definition: dbtoweb.py:253
bool const none_
Definition: PileUp.h:51
tuple histo
Definition: trackerHits.py:12
VectorInputSource *const input_
Definition: PileUp.h:57
bool const probFunctionDistribution_
Definition: PileUp.h:48
void readManyRandom(int number, EventPrincipalVector &result, unsigned int &fileSeqNumber)
int const intAverage_
Definition: PileUp.h:45
bool playback_
Definition: PileUp.h:67
bool poisson_OOT_
Definition: PileUp.h:53
VectorInputSource::EventPrincipalVector EventPrincipalVector
Definition: PileUp.h:24
tuple result
Definition: query.py:137
bool isAvailable() const
Definition: Service.h:47
int j
Definition: DBlmapReader.cc:9
bool const poisson_
Definition: PileUp.h:49
tuple pset
Definition: CrabTask.py:85
void readManySequential(int number, EventPrincipalVector &result, unsigned int &fileSeqNumber)
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool fixed_OOT_
Definition: PileUp.h:54
CLHEP::RandPoisson * poissonDistr_OOT_
Definition: PileUp.h:59
int nint(float a)
Return the nearest integer - analogous to the FORTRAN intrinsic NINT.
Definition: nint.h:8
bool const fixed_
Definition: PileUp.h:50
void readPileUp(std::vector< EventPrincipalVector > &result, std::vector< std::vector< edm::EventID > > &ids)
Definition: PileUp.cc:103
double const averageNumber_
Definition: PileUp.h:44
TH1F *const histo_
Definition: PileUp.h:46
bool sequential_
Definition: PileUp.h:70
PileUp(ParameterSet const &pset, int const minb, int const maxb, double averageNumber, TH1F *const histo, const bool playback)
Definition: PileUp.cc:22
std::string const type_
Definition: PileUp.h:41
int seed_
Definition: PileUp.h:73
tuple playback
Definition: Playback_cff.py:20
int intFixed_OOT_
Definition: PileUp.h:55
CLHEP::RandPoissonQ * poissonDistribution_
Definition: PileUp.h:58
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:56
void readManySpecified(std::vector< EventID > const &events, EventPrincipalVector &result)
int const minBunch_
Definition: PileUp.h:42