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