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_(false),
187  playback_(globalConf->playback_) {
188  if (pset.exists("readDB"))
189  readDB_ = pset.getParameter<bool>("readDB");
190 
191  for (size_t makeIdx = 0; makeIdx < maxNbSources_; makeIdx++) {
192  if (globalConf->inputConfigs_[makeIdx]) {
193  const edm::ParameterSet& psin =
194  pset.getParameter<edm::ParameterSet>(globalConf->inputConfigs_[makeIdx]->sourcename_);
195  inputSources_.push_back(std::make_shared<PileUp>(psin, globalConf->inputConfigs_[makeIdx]));
196  inputSources_.back()->input(makeIdx);
197  } else {
198  inputSources_.push_back(nullptr);
199  }
200  }
201  }
202 
203  // Virtual destructor needed.
205 
206  namespace MixingCache {
207  Config::Config(edm::ParameterSet const& pset, unsigned int maxNbSources)
208  : bunchSpace_(pset.getParameter<int>("bunchspace")),
209  minBunch_((pset.getParameter<int>("minBunch") * 25) / bunchSpace_),
210  maxBunch_((pset.getParameter<int>("maxBunch") * 25) / bunchSpace_),
211  playback_(pset.getUntrackedParameter<bool>("playback", false)) {
212  if (playback_) {
213  //this could be explicitly checked
214  LogInfo("MixingModule") << " ATTENTION:Mixing will be done in playback mode! \n"
215  << " ATTENTION:Mixing Configuration must be the same as for the original mixing!";
216  }
217 
218  // Just for debugging print out.
219  sourceNames_.push_back("input");
220  sourceNames_.push_back("cosmics");
221  sourceNames_.push_back("beamhalo_plus");
222  sourceNames_.push_back("beamhalo_minus");
223 
224  for (size_t makeIdx = 0; makeIdx < maxNbSources; makeIdx++) {
225  inputConfigs_.push_back(maybeConfigPileUp(pset, sourceNames_[makeIdx], minBunch_, maxBunch_, playback_));
226  }
227  }
228  } // namespace MixingCache
229 
230  std::unique_ptr<MixingCache::Config> BMixingModule::initializeGlobalCache(edm::ParameterSet const& pset) {
231  return std::make_unique<MixingCache::Config>(pset, maxNbSources_);
232  }
233 
234  // update method call at begin run/lumi to reload the mixing configuration
236  update(setup);
237  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
238  if (inputSources_[endIdx])
239  inputSources_[endIdx]->beginLuminosityBlock(lumi, setup);
240  }
241  }
242 
244  update(setup);
245  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
246  if (inputSources_[endIdx])
247  inputSources_[endIdx]->beginRun(run, setup);
248  }
249  }
250 
252  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
253  if (inputSources_[endIdx])
254  inputSources_[endIdx]->endLuminosityBlock(lumi, setup);
255  }
256  }
257 
259  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
260  if (inputSources_[endIdx])
261  inputSources_[endIdx]->endRun(run, setup);
262  }
263  }
264 
267  for (size_t makeIdx = 0; makeIdx < maxNbSources_; makeIdx++) {
268  if (inputSources_[makeIdx])
269  inputSources_[makeIdx]->reload(setup);
270  }
271  reload(setup);
272  }
273  }
274 
275  // Functions that get called by framework every event
277  // Check if the signal is present in the root file
278  // for all the objects we want to mix
279  checkSignal(e);
280 
281  // Create EDProduct
283 
285 
286  // Add signals
287  if (!mixProdStep1_) {
288  addSignals(e, setup);
289  }
290 
291  doPileUp(e, setup);
292 
293  // Includes putting digi products into the edm::Event.
295 
296  // Put output into event (here only playback info)
297  put(e, setup);
298  }
299 
301  for (size_t dropIdx = 0; dropIdx < maxNbSources_; ++dropIdx) {
302  if (inputSources_[dropIdx])
303  inputSources_[dropIdx]->setupPileUpEvent(setup);
304  }
305  }
306 
307  void BMixingModule::dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
308  for (size_t dropIdx = 0; dropIdx < maxNbSources_; ++dropIdx) {
309  if (inputSources_[dropIdx])
310  inputSources_[dropIdx]->dropUnwantedBranches(wantedBranches);
311  }
312  }
313 
315  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
316  if (inputSources_[endIdx])
317  inputSources_[endIdx]->beginStream(iID);
318  }
319  }
320 
322  for (size_t endIdx = 0; endIdx < maxNbSources_; ++endIdx) {
323  if (inputSources_[endIdx])
324  inputSources_[endIdx]->endStream();
325  }
326  }
327 
329  edm::LogWarning("MixingModule") << "BMixingModule::createnewEDProduct must be overwritten!";
330  }
331 
333  edm::LogWarning("MixingModule") << "BMixingModule::checkSignal must be overwritten!";
334  }
335 
337  edm::LogWarning("MixingModule") << "BMixingModule::setBcrOffset must be overwritten!";
338  }
339 
340  void BMixingModule::setSourceOffset(const unsigned int s) {
341  edm::LogWarning("MixingModule") << "BMixingModule::setSourceOffset must be overwritten!";
342  }
343 
345  edm::LogWarning("MixingModule") << "BMixingModule::doPileUp must be overwritten!";
346  }
347 
348 } // 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:258
edm::ESWatcher::check
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
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:251
Handle.h
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
edm::BMixingModule::setupPileUpEvent
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: BMixingModule.cc:300
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::Run
Definition: Run.h:45
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::MixingCache::Config::playback_
bool playback_
Definition: BMixingModule.h:34
h
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
edm::BMixingModule::setSourceOffset
virtual void setSourceOffset(const unsigned int s)
Definition: BMixingModule.cc:340
edm::BMixingModule::beginLuminosityBlock
void beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &setup) override
Definition: BMixingModule.cc:235
edm::MixingCache::Config::Config
Config(edm::ParameterSet const &pset, unsigned int maxNbSources)
Definition: BMixingModule.cc:207
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:321
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:265
h
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:332
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
edm::ParameterSet
Definition: ParameterSet.h:47
GetReleaseVersion.h
Event.h
edm::BMixingModule::produce
void produce(edm::Event &e1, const edm::EventSetup &c) override
Definition: BMixingModule.cc:276
edm::BMixingModule::~BMixingModule
~BMixingModule() override
Definition: BMixingModule.cc:204
edm::MixingCache::Config
Definition: BMixingModule.h:29
edm::BMixingModule::beginStream
void beginStream(edm::StreamID) override
Definition: BMixingModule.cc:314
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:307
edm::EventSetup
Definition: EventSetup.h:58
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
edm::BMixingModule::addSignals
virtual void addSignals(const edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:77
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::BMixingModule::mixProdStep1_
const bool mixProdStep1_
Definition: BMixingModule.h:96
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:328
edm::BMixingModule::BMixingModule
BMixingModule(const edm::ParameterSet &ps, MixingCache::Config const *globalConf)
Definition: BMixingModule.cc:179
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:245
HltBtagPostValidation_cff.histoName
histoName
Definition: HltBtagPostValidation_cff.py:17
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::MixingCache::Config::inputConfigs_
std::vector< std::shared_ptr< PileUpConfig > > inputConfigs_
Definition: BMixingModule.h:36
timingPdfMaker.infile
infile
Definition: timingPdfMaker.py:349
edm::BMixingModule::parameterWatcher_
edm::ESWatcher< MixingRcd > parameterWatcher_
Definition: BMixingModule.h:111
timingPdfMaker.outfile
outfile
Definition: timingPdfMaker.py:350
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
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
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:336
edm::BMixingModule::initializeGlobalCache
static std::unique_ptr< MixingCache::Config > initializeGlobalCache(edm::ParameterSet const &)
Definition: BMixingModule.cc:230
edm::BMixingModule::beginRun
void beginRun(const edm::Run &r, const edm::EventSetup &setup) override
Definition: BMixingModule.cc:243
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:344