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