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 
16 
17 #include <algorithm>
18 
19 namespace edm {
20  PileUp::PileUp(ParameterSet const& pset, double averageNumber, TH1F * const histo, const bool playback) :
21  type_(pset.getParameter<std::string>("type")),
22  averageNumber_(averageNumber),
23  intAverage_(static_cast<int>(averageNumber)),
24  histo_(histo),
25  histoDistribution_(type_ == "histo"),
26  probFunctionDistribution_(type_ == "probFunction"),
27  poisson_(type_ == "poisson"),
28  fixed_(type_ == "fixed"),
29  none_(type_ == "none"),
30  input_(VectorInputSourceFactory::get()->makeVectorInputSource(pset, InputSourceDescription()).release()),
31  poissonDistribution_(0),
32  poissonDistr_OOT_(0),
33  playback_(playback),
34  sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
35  samelumi_(pset.getUntrackedParameter<bool>("sameLumiBlock", false)),
36  seed_(0)
37  {
38  if (pset.exists("nbPileupEvents"))
39  seed_=pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed",0);
40 
41  bool DB=type_=="readDB";
42 
44  if (!rng.isAvailable()) {
45  throw cms::Exception("Configuration")
46  << "PileUp requires the RandomNumberGeneratorService\n"
47  "which is not present in the configuration file. You must add the service\n"
48  "in the configuration file or remove the modules that require it.";
49  }
50 
51  CLHEP::HepRandomEngine& engine = rng->getEngine();
52  poissonDistribution_ = new CLHEP::RandPoissonQ(engine, averageNumber_);
53 
54  // Get seed for the case when using user histogram or probability function
56  if(seed_ !=0) {
57  gRandom->SetSeed(seed_);
58  LogInfo("MixingModule") << " Change seed for " << type_ << " mode. The seed is set to " << seed_;
59  }
60  else {
61  gRandom->SetSeed(engine.getSeed());
62  }
63  }
64 
65 
67  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
68  << "'type' parameter (a string) has a value of '" << type_ << "'.\n"
69  << "Legal values are 'poisson', 'fixed', or 'none'\n";
70  }
71 
72  if (!DB){
73  manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
74 
75  // Check for string describing special processing. Add these here for individual cases
76 
77  PU_Study_ = false;
78  Study_type_ = "";
79 
80  Study_type_ = pset.getUntrackedParameter<std::string>("Special_Pileup_Studies", "");
81 
82  if(Study_type_ == "Fixed_ITPU_Vary_OOTPU") {
83 
84  PU_Study_ = true;
85  intFixed_ITPU_ = pset.getUntrackedParameter<int>("intFixed_ITPU", 0);
86 
87  }
88 
89  if(manage_OOT_) { // figure out what the parameters are
90 
91  // if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
92  // << " manage_OOT option not allowed with playback ";
93 
94  std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");
95 
96  if(OOT_type == "Poisson" || OOT_type == "poisson") {
97  poisson_OOT_ = true;
98  poissonDistr_OOT_ = new CLHEP::RandPoisson(engine);
99  }
100  else if(OOT_type == "Fixed" || OOT_type == "fixed") {
101  fixed_OOT_ = true;
102  // read back the fixed number requested out-of-time
103  intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
104  if(intFixed_OOT_ < 0) {
105  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
106  << " Fixed out-of-time pileup requested, but no fixed value given ";
107  }
108  }
109  else {
110  throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
111  << "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
112  << "Legal values are 'poisson' or 'fixed'\n";
113  }
114  edm::LogInfo("MixingModule") <<" Out-of-time pileup will be generated with a " << OOT_type << " distribution. " ;
115  }
116  }
117 
118  }
119 
121  //get the required parameters from DB.
123  setup.get<MixingRcd>().get(configM);
124 
125  const MixingInputConfig & config=configM->config(inputType_);
126 
127  //get the type
128  type_=config.type();
129  //set booleans
130  histoDistribution_=type_ == "histo";
131  probFunctionDistribution_=type_ == "probFunction";
132  poisson_=type_ == "poisson";
133  fixed_=type_ == "fixed";
134  none_=type_ == "none";
135 
136  if (histoDistribution_) edm::LogError("MisConfiguration")<<"type histo cannot be reloaded from DB, yet";
137 
138  if (fixed_){
140  }
141  else if (poisson_)
142  {
143  averageNumber_=config.averageNumber();
145  CLHEP::HepRandomEngine& engine = rng->getEngine();
146  delete poissonDistribution_;
147  poissonDistribution_ = new CLHEP::RandPoissonQ(engine, averageNumber_);
148  }
149  else if (probFunctionDistribution_)
150  {
151  //need to reload the histogram from DB
152  const std::vector<int> & dataProbFunctionVar = config.probFunctionVariable();
153  std::vector<double> dataProb = config.probValue();
154 
155  int varSize = (int) dataProbFunctionVar.size();
156  int probSize = (int) dataProb.size();
157 
158  if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
159  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;
160 
161  // Complete the vector containing the probability function data
162  // with the values "0"
163  if (probSize < varSize){
164  edm::LogWarning("MixingModule") << " The probability function data will be completed with " <<(varSize - probSize) <<" values 0.";
165 
166  for (int i=0; i<(varSize - probSize); i++) dataProb.push_back(0);
167 
168  probSize = dataProb.size();
169  edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
170  }
171 
172  // Create an histogram with the data from the probability function provided by the user
173  int xmin = (int) dataProbFunctionVar[0];
174  int xmax = (int) dataProbFunctionVar[varSize-1]+1; // need upper edge to be one beyond last value
175  int numBins = varSize;
176 
177  edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range ("<< xmin << "," << xmax << ")." << std::endl;
178 
179  if (histo_) delete histo_;
180  histo_ = new TH1F("h","Histo from the user's probability function",numBins,xmin,xmax);
181 
182  LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
183 
184  for (int j=0; j < numBins ; j++){
185  LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j ]<< " P(x) = " << dataProb[j];
186  histo_->Fill(dataProbFunctionVar[j]+0.5,dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
187  }
188 
189  // Check if the histogram is normalized
190  if ( ((histo_->Integral() - 1) > 1.0e-02) && ((histo_->Integral() - 1) < -1.0e-02)){
191  throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << std::endl;
192  }
193  averageNumber_=histo_->GetMean();
194  }
195 
196  int oot=config.outOfTime();
197  manage_OOT_=false;
198  if (oot==1)
199  {
200  manage_OOT_=true;
201  poisson_OOT_ = false;
203  fixed_OOT_ = true;
204  intFixed_OOT_=config.fixedOutOfTime();
205  }
206  else if (oot==2)
207  {
208  manage_OOT_=true;
209  poisson_OOT_ = true;
210  fixed_OOT_ = false;
211  if (!poissonDistr_OOT_) {
212  //no need to trash the previous one if already there
214  CLHEP::HepRandomEngine& engine = rng->getEngine();
215  poissonDistr_OOT_ = new CLHEP::RandPoisson(engine);
216  }
217  }
218 
219 
220  }
222  delete poissonDistribution_;
223  delete poissonDistr_OOT_ ;
224  }
225 
226  void PileUp::CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions) {
227 
228  // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
229  // crossing zero first, save it for later.
230 
231  int nzero_crossing = -1;
232  double Fnzero_crossing = -1;
233 
234  if(manage_OOT_) {
235  if (none_){
236  nzero_crossing = 0;
237  }else if (poisson_){
238  nzero_crossing = poissonDistribution_->fire() ;
239  }else if (fixed_){
240  nzero_crossing = intAverage_ ;
242  double d = histo_->GetRandom();
243  //n = (int) floor(d + 0.5); // incorrect for bins with integer edges
244  Fnzero_crossing = d;
245  nzero_crossing = int(d);
246  }
247 
248  }
249 
250  for(int bx = MinBunch; bx < MaxBunch+1; ++bx) {
251 
252  if(manage_OOT_) {
253  if(bx==0 && !poisson_OOT_) {
254  PileupSelection.push_back(nzero_crossing) ;
255  TrueNumInteractions.push_back( nzero_crossing );
256  }
257  else{
258  if(poisson_OOT_) {
259  if(PU_Study_ && (Study_type_ == "Fixed_ITPU_Vary_OOTPU" ) && bx==0 ) {
260  PileupSelection.push_back(intFixed_ITPU_) ;
261  }
262  else{
263  PileupSelection.push_back(poissonDistr_OOT_->fire(Fnzero_crossing)) ;
264  }
265  TrueNumInteractions.push_back( Fnzero_crossing );
266  }
267  else {
268  PileupSelection.push_back(intFixed_OOT_) ;
269  TrueNumInteractions.push_back( intFixed_OOT_ );
270  }
271  }
272  }
273  else {
274  if (none_){
275  PileupSelection.push_back(0);
276  TrueNumInteractions.push_back( 0. );
277  }else if (poisson_){
278  PileupSelection.push_back(poissonDistribution_->fire());
279  TrueNumInteractions.push_back( averageNumber_ );
280  }else if (fixed_){
281  PileupSelection.push_back(intAverage_);
282  TrueNumInteractions.push_back( intAverage_ );
284  double d = histo_->GetRandom();
285  PileupSelection.push_back(int(d));
286  TrueNumInteractions.push_back( d );
287  }
288  }
289 
290  }
291  }
292 
293 
294 } //namespace edm
#define LogDebug(id)
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions)
Definition: PileUp.cc:226
bool manage_OOT_
Definition: PileUp.h:74
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
unsigned int inputType_
Definition: PileUp.h:64
const int fixedOutOfTime() const
bool exists(std::string const &parameterName) const
checks if a parameter exists
TH1F * histo_
Definition: PileUp.h:68
bool fixed_
Definition: PileUp.h:72
const std::vector< double > & probValue() const
const double averageNumber() const
PileUp(ParameterSet const &pset, double averageNumber, TH1F *const histo, const bool playback)
Definition: PileUp.cc:20
int const intAverage_
Definition: PileUp.h:67
bool probFunctionDistribution_
Definition: PileUp.h:70
std::string Study_type_
Definition: PileUp.h:79
bool histoDistribution_
Definition: PileUp.h:69
bool poisson_
Definition: PileUp.h:71
double averageNumber() const
Definition: PileUp.h:42
const int outOfTime() const
bool poisson_OOT_
Definition: PileUp.h:75
bool isAvailable() const
Definition: Service.h:47
int j
Definition: DBlmapReader.cc:9
int intFixed_ITPU_
Definition: PileUp.h:82
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool PU_Study_
Definition: PileUp.h:78
bool fixed_OOT_
Definition: PileUp.h:76
CLHEP::RandPoisson * poissonDistr_OOT_
Definition: PileUp.h:86
std::string type_
Definition: PileUp.h:65
const T & get() const
Definition: EventSetup.h:55
const std::vector< int > & probFunctionVariable() const
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:120
int seed_
Definition: PileUp.h:103
bool none_
Definition: PileUp.h:73
tuple playback
Definition: Playback_cff.py:20
int intFixed_OOT_
Definition: PileUp.h:81
std::string type() const
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
CLHEP::RandPoissonQ * poissonDistribution_
Definition: PileUp.h:85
T get(const Candidate &c)
Definition: component.h:56
double averageNumber_
Definition: PileUp.h:66