CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
20 using namespace std;
21 
23 const unsigned int edm::BMixingModule::maxNbSources_ =4;
24 
25 namespace
26 {
27  boost::shared_ptr<edm::PileUp>
28  maybeMakePileUp(edm::ParameterSet const& ps,std::string sourceName, const int minb, const int maxb, const bool playback)
29  {
30  boost::shared_ptr<edm::PileUp> pileup; // value to be returned
31  // Make sure we have a parameter named 'sourceName'
32  vector<string> names = ps.getParameterNames();
33  if (find(names.begin(), names.end(), sourceName)
34  != names.end())
35  {
36  // We have the parameter
37  // and if we have either averageNumber or cfg by luminosity... make the PileUp
38  double averageNumber;
39  std::string histoFileName=" ";
40  std::string histoName =" ";
41  TH1F * h = new TH1F("h","h",10,0,10);
42  vector<int> dataProbFunctionVar;
43  vector<double> dataProb;
44 
46  if (psin.getParameter<std::string>("type")!="none") {
47  vector<string> namesIn = psin.getParameterNames();
48  if (find(namesIn.begin(), namesIn.end(), std::string("nbPileupEvents"))
49  != namesIn.end()) {
50  edm::ParameterSet psin_average=psin.getParameter<edm::ParameterSet>("nbPileupEvents");
51  vector<string> namesAverage = psin_average.getParameterNames();
52  if (find(namesAverage.begin(), namesAverage.end(), std::string("averageNumber"))
53  != namesAverage.end())
54  {
55  averageNumber=psin_average.getParameter<double>("averageNumber");
56  pileup.reset(new edm::PileUp(ps.getParameter<edm::ParameterSet>(sourceName),minb,maxb,averageNumber,h,playback));
57  edm::LogInfo("MixingModule") <<" Created source "<<sourceName<<" with minBunch,maxBunch "<<minb<<" "<<maxb<<" and averageNumber "<<averageNumber;
58  }
59  else if (find(namesAverage.begin(), namesAverage.end(), std::string("fileName"))
60  != namesAverage.end() && find(namesAverage.begin(), namesAverage.end(), std::string("histoName"))
61  != namesAverage.end()){
62 
63 
64  std::string histoFileName = psin_average.getUntrackedParameter<std::string>("fileName");
65  std::string histoName = psin_average.getUntrackedParameter<std::string>("histoName");
66 
67  TFile *infile = new TFile(histoFileName.c_str());
68  TH1F *h = (TH1F*)infile->Get(histoName.c_str());
69 
70  // Check if the histogram exists
71  if (!h) {
72  throw cms::Exception("HistogramNotFound") << " Could not find the histogram " << histoName
73  << "in the file " << histoFileName << "." << std::endl;
74  }else{
75  edm::LogInfo("MixingModule") << "Open a root file " << histoFileName << " containing the probability distribution histogram " << histoName << std::endl;
76  edm::LogInfo("MixingModule") << "The PileUp number to be added will be chosen randomly from this histogram" << std::endl;
77  }
78 
79  // Check if the histogram is normalized
80  if (((h->Integral() - 1) > 1.0e-02) && ((h->Integral() - 1) < -1.0e-02)) throw cms::Exception("BadHistoDistribution") << "The histogram should be normalized!" << std::endl;
81 
82  // Get the averageNumber from the histo
83  averageNumber = h->GetMean();
84 
85  pileup.reset(new edm::PileUp(ps.getParameter<edm::ParameterSet>(sourceName),minb,maxb,averageNumber,h,playback));
86  edm::LogInfo("MixingModule") <<" Created source "<<sourceName<<" with minBunch,maxBunch "<<minb<<" "<<maxb<<" and averageNumber "<<averageNumber;
87 
88  }
89  else if (find(namesAverage.begin(), namesAverage.end(), std::string("probFunctionVariable"))
90  != namesAverage.end() && find(namesAverage.begin(), namesAverage.end(), std::string("probValue"))
91  != namesAverage.end() && find(namesAverage.begin(), namesAverage.end(), std::string("histoFileName"))
92  != namesAverage.end()){
93 
94  dataProbFunctionVar = psin_average.getParameter<vector<int> >("probFunctionVariable");
95  dataProb = psin_average.getParameter<vector<double> >("probValue");
96  histoFileName = psin_average.getUntrackedParameter<std::string>("histoFileName");
97 
98  int varSize = (int) dataProbFunctionVar.size();
99  int probSize = (int) dataProb.size();
100 
101  if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1)))
102  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." << endl;
103 
104  // Complete the vector containing the probability function data
105  // with the values "0"
106  if (probSize < varSize){
107  edm::LogInfo("MixingModule") << " The probability function data will be completed with " <<(varSize - probSize) <<" values 0.";
108 
109  for (int i=0; i<(varSize - probSize); i++) dataProb.push_back(0);
110 
111  probSize = dataProb.size();
112  edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
113  }
114 
115  // Create an histogram with the data from the probability function provided by the user
116  int xmin = (int) dataProbFunctionVar[0];
117  int xmax = (int) dataProbFunctionVar[varSize-1]+1; // need upper edge to be one beyond last value
118  int numBins = varSize;
119 
120  edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range ("<< xmin << "," << xmax << ")." << std::endl;
121 
122  TH1F *hprob = new TH1F("h","Histo from the user's probability function",numBins,xmin,xmax);
123 
124  LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
125 
126  for (int j=0; j < numBins ; j++){
127  LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j ]<< " P(x) = " << dataProb[j];
128  hprob->Fill(dataProbFunctionVar[j]+0.5,dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges
129  }
130 
131  // Check if the histogram is normalized
132  if ( ((hprob->Integral() - 1) > 1.0e-02) && ((hprob->Integral() - 1) < -1.0e-02)){
133  throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << endl;
134  }
135 
136  averageNumber = hprob->GetMean();
137 
138  // Write the created histogram into a root file
139  edm::LogInfo("MixingModule") << " The histogram created from the x, P(x) values will be written into the root file " << histoFileName;
140 
141  TFile * outfile = new TFile(histoFileName.c_str(),"RECREATE");
142  hprob->Write();
143  outfile->Write();
144  outfile->Close();
145  outfile->Delete();
146 
147  pileup.reset(new edm::PileUp(ps.getParameter<edm::ParameterSet>(sourceName),minb,maxb,averageNumber,hprob,playback));
148  edm::LogInfo("MixingModule") <<" Created source "<<sourceName<<" with minBunch,maxBunch "<<minb<<" "<<maxb<<" and averageNumber "<<averageNumber;
149 
150  }
151  //special for pileup input
152  else if (sourceName=="input" && find(namesAverage.begin(), namesAverage.end(), std::string("Lumi"))
153  != namesAverage.end() && find(namesAverage.begin(), namesAverage.end(), std::string("sigmaInel"))
154  != namesAverage.end()) {
155 
156  averageNumber=psin_average.getParameter<double>("Lumi")*psin_average.getParameter<double>("sigmaInel")*ps.getParameter<int>("bunchspace")/1000*3564./2808.; //FIXME
157  pileup.reset(new
158  edm::PileUp(ps.getParameter<edm::ParameterSet>(sourceName),minb,maxb,averageNumber,h,playback));
159  edm::LogInfo("MixingModule") <<" Created source "<<sourceName<<" with minBunch,maxBunch "<<minb<<" "<<maxb;
160  edm::LogInfo("MixingModule")<<" Luminosity configuration, average number used is "<<averageNumber;
161  }
162  }
163  }
164  }
165  return pileup;
166  }
167 }
168 
169 namespace edm {
170 
171  // Constructor
172  BMixingModule::BMixingModule(const edm::ParameterSet& pset) :
173  bunchSpace_(pset.getParameter<int>("bunchspace")),
174  minBunch_((pset.getParameter<int>("minBunch")*25)/pset.getParameter<int>("bunchspace")),
175  maxBunch_((pset.getParameter<int>("maxBunch")*25)/pset.getParameter<int>("bunchspace")),
176  mixProdStep1_(pset.getParameter<bool>("mixProdStep1")),
177  mixProdStep2_(pset.getParameter<bool>("mixProdStep2"))
178  {
179  // FIXME: temporary to keep bwds compatibility for cfg files
180  vector<string> names = pset.getParameterNames();
181  if (find(names.begin(), names.end(),"playback")
182  != names.end()) {
183  playback_=pset.getUntrackedParameter<bool>("playback");
184  } else
185  playback_=false;
186 
187  //We use std::cout in order to make sure the message appears in all possible configurations of the Message Logger
188  if (playback_) {
189  LogInfo("MixingModule") <<" ATTENTION:Mixing will be done in playback mode! \n"
190  <<" ATTENTION:Mixing Configuration must be the same as for the original mixing!";
191  }
192 
193  input_= maybeMakePileUp(pset,"input",minBunch_,maxBunch_,playback_);
194  cosmics_= maybeMakePileUp(pset,"cosmics",minBunch_,maxBunch_,playback_);
195  beamHalo_p_=maybeMakePileUp(pset,"beamhalo_plus",minBunch_,maxBunch_,playback_);
196  beamHalo_m_=maybeMakePileUp(pset,"beamhalo_minus",minBunch_,maxBunch_,playback_);
197 
198  //prepare playback info structures
200  }
201 
202  // Virtual destructor needed.
204 
205  // Functions that get called by framework every event
207 
208  // Check if the signal is present in the root file
209  // for all the objects we want to mix
210  checkSignal(e);
211 
212  // Create EDProduct
214 
215  // Add signals
216  if (!mixProdStep1_){
217  addSignals(e,setup);
218  }
219 
220  // Read the PileUp
221  for (unsigned int is=0;is< maxNbSources_;++is) {
222  doit_[is]=false;
223  pileup_[is].clear();
224  TrueNumInteractions_[is].clear();
225  }
226 
227  if (input_) {
228  if (playback_) {
229  getEventStartInfo(e,0);
230  input_->readPileUp(pileup_[0], vectorEventIDs_, TrueNumInteractions_[0]);
231  } else {
232  input_->readPileUp(pileup_[0], vectorEventIDs_, TrueNumInteractions_[0]);
234  }
235  if (input_->doPileup()) {
236  LogDebug("MixingModule") <<"\n\n==============================>Adding pileup to signal event "<<e.id();
237  doit_[0]=true;
238  }
239  }
240  if (cosmics_) {
241  if (playback_) {
242  getEventStartInfo(e,1);
244  } else {
247  }
248  if (cosmics_->doPileup()) {
249  LogDebug("MixingModule") <<"\n\n==============================>Adding cosmics to signal event "<<e.id();
250  doit_[1]=true;
251  }
252  }
253 
254  if (beamHalo_p_) {
255  if (playback_) {
256  getEventStartInfo(e,2);
258  } else {
261  }
262  if (beamHalo_p_->doPileup()) {
263  LogDebug("MixingModule") <<"\n\n==============================>Adding beam halo+ to signal event "<<e.id();
264  doit_[2]=true;
265  }
266  }
267 
268  if (beamHalo_m_) {
269  if (playback_) {
270  getEventStartInfo(e,3);
272  } else {
275  }
276  if (beamHalo_m_->doPileup()) {
277  LogDebug("MixingModule") <<"\n\n==============================>Adding beam halo- to signal event "<<e.id();
278  doit_[3]=true;
279  }
280  }
281 
282  doPileUp(e,setup);
283 
284  // Put output into event (here only playback info)
285  put(e,setup);
286  }
287 
288  void BMixingModule::merge(const int bcr, const EventPrincipalVector& vec, unsigned int worker, const edm::EventSetup& setup) {
289  //
290  // main loop: loop over events and merge
291  //
292  eventId_=0;
293  LogDebug("MixingModule") <<"For bunchcrossing "<<bcr<<", "<<vec.size()<<" events will be merged";
294  vertexoffset=0;
295  int i=0;
296  for (EventPrincipalVector::const_iterator it = vec.begin(); it != vec.end(); ++it) {
297  LogDebug("MixingModule") <<" merging Event: id " << (*it)->id();
298 
299  addPileups(bcr, &(**it), ++eventId_,worker,setup);
300  i = i + 1;
301  }// end main loop
302  }
303 
304  void BMixingModule::dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
305  if (input_) input_->dropUnwantedBranches(wantedBranches);
306  if (cosmics_) cosmics_->dropUnwantedBranches(wantedBranches);
307  if (beamHalo_p_) beamHalo_p_->dropUnwantedBranches(wantedBranches);
308  if (beamHalo_m_) beamHalo_m_->dropUnwantedBranches(wantedBranches);
309  }
310 
312  if (input_) input_->endJob();
313  if (cosmics_) cosmics_->endJob();
314  if (beamHalo_p_) beamHalo_p_->endJob();
315  if (beamHalo_m_) beamHalo_m_->endJob();
316  }
317 
318 } //edm
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
virtual void addPileups(const int bcr, EventPrincipal *ep, unsigned int eventId, unsigned int worker, const edm::EventSetup &c)
Definition: BMixingModule.h:54
static int vertexoffset
Definition: BMixingModule.h:66
virtual void addSignals(const edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:53
virtual void endJob()
std::vector< EventPrincipalVector > pileup_[4]
Definition: BMixingModule.h:81
tuple histoFileName
Definition: diJetCalib.py:108
std::vector< std::vector< edm::EventID > > vectorEventIDs_
Definition: BMixingModule.h:76
void merge(const int bcr, const EventPrincipalVector &vec, unsigned int worker, const edm::EventSetup &c)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual void put(edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:57
virtual void checkSignal(const edm::Event &e)
Definition: BMixingModule.h:51
list outfile
Definition: EdgesToViz.py:91
PileUp::EventPrincipalVector EventPrincipalVector
Definition: BMixingModule.h:31
int j
Definition: DBlmapReader.cc:9
static const unsigned int maxNbSources_
Definition: BMixingModule.h:79
std::vector< std::string > getParameterNames() const
std::vector< float > TrueNumInteractions_[4]
Definition: BMixingModule.h:82
boost::shared_ptr< PileUp > cosmics_
Definition: BMixingModule.h:87
virtual void setEventStartInfo(const unsigned int s)
Definition: BMixingModule.h:59
boost::shared_ptr< PileUp > beamHalo_m_
Definition: BMixingModule.h:89
edm::EventID id() const
Definition: EventBase.h:56
bool const mixProdStep1_
Definition: BMixingModule.h:70
virtual ~BMixingModule()
list infile
Definition: EdgesToViz.py:90
virtual void getEventStartInfo(edm::Event &e, const unsigned int source)
Definition: BMixingModule.h:60
virtual void produce(edm::Event &e1, const edm::EventSetup &c)
tuple playback
Definition: Playback_cff.py:20
boost::shared_ptr< PileUp > input_
Definition: BMixingModule.h:86
virtual void doPileUp(edm::Event &e, const edm::EventSetup &c)
Definition: BMixingModule.h:58
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
virtual void createnewEDProduct()
Definition: BMixingModule.h:50
static const HistoName names[]
boost::shared_ptr< PileUp > beamHalo_p_
Definition: BMixingModule.h:88
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
unsigned int eventId_
Definition: BMixingModule.h:91