CMS 3D CMS Logo

BMixingModule.cc
Go to the documentation of this file.
1 // File: BMixingModule.cc
2 // Description: see BMixingModule.h
3 // Author: Ursula Berthon, LLR Palaiseau, Bill Tanenbaum
4 //
5 //--------------------------------------------
6 
16 
17 #include "TFile.h"
18 #include "TH1F.h"
19 #include <iostream>
20 #include <memory>
21 
22 const unsigned int edm::BMixingModule::maxNbSources_ = 4;
23 
24 namespace {
25  std::shared_ptr<edm::PileUpConfig> maybeConfigPileUp(
26  edm::ParameterSet const& ps, std::string sourceName, const int minb, const int maxb, const bool playback) {
27  std::shared_ptr<edm::PileUpConfig> pileupconfig; // value to be returned
28  // Make sure we have a parameter named 'sourceName'
29  if (ps.exists(sourceName)) {
30  // We have the parameter
31  // and if we have either averageNumber or cfg by luminosity... make the PileUp
32  double averageNumber;
34  std::string histoName = " ";
35  std::unique_ptr<TH1F> h(new TH1F("h", "h", 10, 0, 10));
36  std::vector<int> dataProbFunctionVar;
37  std::vector<double> dataProb;
38 
39  const edm::ParameterSet& psin = ps.getParameter<edm::ParameterSet>(sourceName);
40  std::string type_ = psin.getParameter<std::string>("type");
41  if (ps.exists("readDB") && ps.getParameter<bool>("readDB")) {
42  //in case of DB access, do not try to load anything from the PSet, but wait for beginRun.
43  edm::LogError("BMixingModule") << "Will read from DB: reset to a dummy PileUp object.";
44  std::unique_ptr<TH1F> h;
45  pileupconfig.reset(new edm::PileUpConfig(sourceName, 0.0, h, playback));
46  return pileupconfig;
47  }
48  if (type_ != "none") {
49  if (psin.exists("nbPileupEvents")) {
50  edm::ParameterSet psin_average = psin.getParameter<edm::ParameterSet>("nbPileupEvents");
51  if (psin_average.exists("averageNumber")) {
52  averageNumber = psin_average.getParameter<double>("averageNumber");
53  pileupconfig.reset(new edm::PileUpConfig(sourceName, averageNumber, h, playback));
54  edm::LogInfo("MixingModule") << " Created source " << sourceName << " with averageNumber " << averageNumber;
55  } else if (psin_average.exists("fileName") && psin_average.exists("histoName")) {
56  std::string histoFileName = psin_average.getUntrackedParameter<std::string>("fileName");
57  std::string histoName = psin_average.getUntrackedParameter<std::string>("histoName");
58 
59  std::unique_ptr<TFile> infile(new TFile(histoFileName.c_str()));
60  std::unique_ptr<TH1F> h((TH1F*)infile->Get(histoName.c_str()));
61 
62  // Check if the histogram exists
63  if (!h) {
64  throw cms::Exception("HistogramNotFound") << " Could not find the histogram " << histoName
65  << "in the file " << histoFileName << "." << std::endl;
66  } else {
67  edm::LogInfo("MixingModule")
68  << "Open a root file " << histoFileName << " containing the probability distribution histogram "
69  << histoName << std::endl;
70  edm::LogInfo("MixingModule")
71  << "The PileUp number to be added will be chosen randomly from this histogram" << std::endl;
72  }
73 
74  // Check if the histogram is normalized
75  if (std::abs(h->Integral() - 1) > 1.0e-02)
76  throw cms::Exception("BadHistoDistribution") << "The histogram should be normalized!" << std::endl;
77 
78  // Get the averageNumber from the histo
79  averageNumber = h->GetMean();
80 
81  pileupconfig.reset(new edm::PileUpConfig(sourceName, averageNumber, h, playback));
82  edm::LogInfo("MixingModule") << " Created source " << sourceName << " with averageNumber " << averageNumber;
83 
84  } else if (psin_average.exists("probFunctionVariable") && psin_average.exists("probValue") &&
85  psin_average.exists("histoFileName")) {
86  if (type_ != "probFunction") {
87  edm::LogError("MisConfiguration")
88  << "type is set to: " << type_ << " while parameters implies probFunction; changing.";
89  type_ = "probFunction";
90  }
91 
92  dataProbFunctionVar = psin_average.getParameter<std::vector<int> >("probFunctionVariable");
93  dataProb = psin_average.getParameter<std::vector<double> >("probValue");
94  histoFileName = psin_average.getUntrackedParameter<std::string>("histoFileName");
95 
96  int varSize = (int)dataProbFunctionVar.size();
97  int probSize = (int)dataProb.size();
98 
99  if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
100  throw cms::Exception("BadProbFunction")
101  << "Please, check the variables of the probability function! The first variable should be 0 and the "
102  "difference between two variables should be 1."
103  << std::endl;
104 
105  // Complete the vector containing the probability function data
106  // with the values "0"
107  if (probSize < varSize) {
108  edm::LogInfo("MixingModule")
109  << " The probability function data will be completed with " << (varSize - probSize) << " values 0.";
110 
111  for (int i = 0; i < (varSize - probSize); i++)
112  dataProb.push_back(0);
113 
114  probSize = dataProb.size();
115  edm::LogInfo("MixingModule")
116  << " The number of the P(x) data set after adding the values 0 is " << probSize;
117  }
118 
119  // Create an histogram with the data from the probability function provided by the user
120  int xmin = (int)dataProbFunctionVar[0];
121  int xmax = (int)dataProbFunctionVar[varSize - 1] + 1; // need upper edge to be one beyond last value
122  int numBins = varSize;
123 
124  edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range ("
125  << xmin << "," << xmax << ")." << std::endl;
126 
127  std::unique_ptr<TH1F> hprob(
128  new TH1F("h", "Histo from the user's probability function", numBins, xmin, xmax));
129 
130  LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
131 
132  for (int j = 0; j < numBins; j++) {
133  LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j] << " P(x) = " << dataProb[j];
134  hprob->Fill(dataProbFunctionVar[j] + 0.5,
135  dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
136  }
137 
138  // Check if the histogram is normalized
139  if (std::abs(hprob->Integral() - 1) > 1.0e-02) {
140  throw cms::Exception("BadProbFunction")
141  << "The probability function should be normalized!!! " << std::endl;
142  }
143 
144  averageNumber = hprob->GetMean();
145 
146  // Write the created histogram into a root file
147  edm::LogInfo("MixingModule")
148  << " The histogram created from the x, P(x) values will be written into the root file "
149  << histoFileName;
150 
151  TFile* outfile = new TFile(histoFileName.c_str(), "RECREATE");
152  hprob->Write();
153  outfile->Write();
154  outfile->Close();
155  outfile->Delete();
156 
157  pileupconfig.reset(new edm::PileUpConfig(sourceName, averageNumber, hprob, playback));
158  edm::LogInfo("MixingModule") << " Created source " << sourceName << " with averageNumber " << averageNumber;
159  }
160  //special for pileup input
161  else if (sourceName == "input" && psin_average.exists("Lumi") && psin_average.exists("sigmaInel")) {
162  averageNumber = psin_average.getParameter<double>("Lumi") * psin_average.getParameter<double>("sigmaInel") *
163  ps.getParameter<int>("bunchspace") / 1000 * 3564. / 2808.; //FIXME
164  pileupconfig.reset(new edm::PileUpConfig(sourceName, averageNumber, h, playback));
165  edm::LogInfo("MixingModule") << " Created source " << sourceName << " with minBunch,maxBunch " << minb
166  << " " << maxb;
167  edm::LogInfo("MixingModule") << " Luminosity configuration, average number used is " << averageNumber;
168  }
169  }
170  }
171  }
172  return pileupconfig;
173  }
174 } // namespace
175 
176 namespace edm {
177 
178  // Constructor
180  : bunchSpace_(globalConf->bunchSpace_),
181  vertexOffset_(0),
182  minBunch_(globalConf->minBunch_),
183  maxBunch_(globalConf->maxBunch_),
184  mixProdStep1_(pset.getParameter<bool>("mixProdStep1")),
185  mixProdStep2_(pset.getParameter<bool>("mixProdStep2")),
186  readDB_(globalConf->configFromDB_),
187  playback_(globalConf->playback_) {
188  for (size_t makeIdx = 0; makeIdx < maxNbSources_; makeIdx++) {
189  if (globalConf->inputConfigs_[makeIdx]) {
190  const edm::ParameterSet& psin =
191  pset.getParameter<edm::ParameterSet>(globalConf->inputConfigs_[makeIdx]->sourcename_);
192  inputSources_.push_back(
193  std::make_shared<PileUp>(psin, globalConf->inputConfigs_[makeIdx], consumesCollector(), readDB_));
194  inputSources_.back()->input(makeIdx);
195  } else {
196  inputSources_.push_back(nullptr);
197  }
198  }
199  }
200 
201  // Virtual destructor needed.
203 
205  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
206  if (inputSources_[endIdx])
207  inputSources_[endIdx]->beginJob(iES);
208  }
209  }
210 
211  namespace MixingCache {
212  Config::Config(edm::ParameterSet const& pset, unsigned int maxNbSources)
213  : bunchSpace_(pset.getParameter<int>("bunchspace")),
214  minBunch_((pset.getParameter<int>("minBunch") * 25) / bunchSpace_),
215  maxBunch_((pset.getParameter<int>("maxBunch") * 25) / bunchSpace_),
216  playback_(pset.getUntrackedParameter<bool>("playback", false)) {
217  if (playback_) {
218  //this could be explicitly checked
219  LogInfo("MixingModule") << " ATTENTION:Mixing will be done in playback mode! \n"
220  << " ATTENTION:Mixing Configuration must be the same as for the original mixing!";
221  }
222 
223  // Just for debugging print out.
224  sourceNames_.push_back("input");
225  sourceNames_.push_back("cosmics");
226  sourceNames_.push_back("beamhalo_plus");
227  sourceNames_.push_back("beamhalo_minus");
228 
229  for (size_t makeIdx = 0; makeIdx < maxNbSources; makeIdx++) {
230  inputConfigs_.push_back(maybeConfigPileUp(pset, sourceNames_[makeIdx], minBunch_, maxBunch_, playback_));
231  }
232 
233  if (pset.exists("readDB"))
234  configFromDB_ = pset.getParameter<bool>("readDB");
235  }
236  } // namespace MixingCache
237 
238  std::unique_ptr<MixingCache::Config> BMixingModule::initializeGlobalCache(edm::ParameterSet const& pset) {
239  return std::make_unique<MixingCache::Config>(pset, maxNbSources_);
240  }
241 
242  // update method call at begin run/lumi to reload the mixing configuration
244  update(setup);
245  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
246  if (inputSources_[endIdx])
247  inputSources_[endIdx]->beginLuminosityBlock(lumi, setup);
248  }
249  }
250 
252  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
253  if (inputSources_[endIdx])
254  inputSources_[endIdx]->beginRun(run, setup);
255  }
256  }
257 
259  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
260  if (inputSources_[endIdx])
261  inputSources_[endIdx]->endLuminosityBlock(lumi, setup);
262  }
263  }
264 
266  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
267  if (inputSources_[endIdx])
268  inputSources_[endIdx]->endRun(run, setup);
269  }
270  }
271 
274  for (size_t makeIdx = 0; makeIdx < maxNbSources_; makeIdx++) {
275  if (inputSources_[makeIdx])
276  inputSources_[makeIdx]->reload(setup);
277  }
278  reload(setup);
279  }
280  }
281 
282  // Functions that get called by framework every event
284  // Check if the signal is present in the root file
285  // for all the objects we want to mix
286  checkSignal(e);
287 
288  // Create EDProduct
290 
292 
293  // Add signals
294  if (!mixProdStep1_) {
295  addSignals(e, setup);
296  }
297 
298  doPileUp(e, setup);
299 
300  // Includes putting digi products into the edm::Event.
302 
303  // Put output into event (here only playback info)
304  put(e, setup);
305  }
306 
308  for (size_t dropIdx = 0; dropIdx < maxNbSources_; ++dropIdx) {
309  if (inputSources_[dropIdx])
310  inputSources_[dropIdx]->setupPileUpEvent(setup);
311  }
312  }
313 
314  void BMixingModule::dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
315  for (size_t dropIdx = 0; dropIdx < maxNbSources_; ++dropIdx) {
316  if (inputSources_[dropIdx])
317  inputSources_[dropIdx]->dropUnwantedBranches(wantedBranches);
318  }
319  }
320 
322  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
323  if (inputSources_[endIdx])
324  inputSources_[endIdx]->beginStream(iID);
325  }
326  }
327 
329  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
330  if (inputSources_[endIdx])
331  inputSources_[endIdx]->endStream();
332  }
333  }
334 
336  edm::LogWarning("MixingModule") << "BMixingModule::createnewEDProduct must be overwritten!";
337  }
338 
340  edm::LogWarning("MixingModule") << "BMixingModule::checkSignal must be overwritten!";
341  }
342 
344  edm::LogWarning("MixingModule") << "BMixingModule::setBcrOffset must be overwritten!";
345  }
346 
347  void BMixingModule::setSourceOffset(const unsigned int s) {
348  edm::LogWarning("MixingModule") << "BMixingModule::setSourceOffset must be overwritten!";
349  }
350 
352  edm::LogWarning("MixingModule") << "BMixingModule::doPileUp must be overwritten!";
353  }
354 
355 } // namespace edm
edm::ESWatcher< MixingRcd > parameterWatcher_
void beginRun(const edm::Run &r, const edm::EventSetup &setup) override
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static std::unique_ptr< MixingCache::Config > initializeGlobalCache(edm::ParameterSet const &)
virtual void addSignals(const edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:80
void produce(edm::Event &e1, const edm::EventSetup &c) override
virtual void finalizeEvent(edm::Event &event, const edm::EventSetup &setup)
Definition: BMixingModule.h:57
std::vector< std::string > sourceNames_
Definition: BMixingModule.h:36
bool exists(std::string const &parameterName) const
checks if a parameter exists
virtual void checkSignal(const edm::Event &e)
void endStream() override
Log< level::Error, false > LogError
virtual void put(edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:85
void endRun(const edm::Run &r, const edm::EventSetup &setup) override
virtual void setBcrOffset()
T getUntrackedParameter(std::string const &, T const &) const
virtual void createnewEDProduct()
virtual void setSourceOffset(const unsigned int s)
virtual void doPileUp(edm::Event &e, const edm::EventSetup &c)
virtual void initializeEvent(const edm::Event &event, const edm::EventSetup &setup)
Definition: BMixingModule.h:54
Config(edm::ParameterSet const &pset, unsigned int maxNbSources)
void beginStream(edm::StreamID) override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static const unsigned int maxNbSources_
virtual void reload(const edm::EventSetup &setup)
Definition: BMixingModule.h:69
Log< level::Info, false > LogInfo
~BMixingModule() override
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
bool const mixProdStep1_
Definition: BMixingModule.h:99
HLT enums.
std::vector< std::shared_ptr< PileUp > > inputSources_
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Log< level::Warning, false > LogWarning
void beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &setup) override
void update(edm::EventSetup const &)
std::vector< std::shared_ptr< PileUpConfig > > inputConfigs_
Definition: BMixingModule.h:37
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void setupPileUpEvent(const edm::EventSetup &setup)
void endLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &setup) override
void registerLateConsumes(eventsetup::ESRecordsToProxyIndices const &) override
Definition: Run.h:45
#define LogDebug(id)
BMixingModule(const edm::ParameterSet &ps, MixingCache::Config const *globalConf)