Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICEventInfoProducer.cc
Go to the documentation of this file.
2 #include <memory>
3 #include <string>
4 #include <vector>
5 #include "Math/Vector4D.h"
6 #include "Math/Vector4Dfwd.h"
7 #include "boost/format.hpp"
8 #include "FWCore/Framework/interface/Event.h"
9 #include "FWCore/Framework/interface/Run.h"
10 #include "FWCore/Framework/interface/EventSetup.h"
11 #include "FWCore/Framework/interface/MakerMacros.h"
12 #include "FWCore/ParameterSet/interface/ParameterSet.h"
13 #include "FWCore/Utilities/interface/InputTag.h"
14 #include "DataFormats/Common/interface/Handle.h"
15 #include "DataFormats/Common/interface/View.h"
16 #include "DataFormats/VertexReco/interface/Vertex.h"
17 #include "DataFormats/VertexReco/interface/VertexFwd.h"
18 #include "DataFormats/METReco/interface/BeamHaloSummary.h"
19 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
20 #include "DataFormats/Common/interface/TriggerResults.h"
21 #include "FWCore/Common/interface/TriggerNames.h"
22 #include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h"
23 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
24 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
25 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
30 #include "FWCore/Utilities/interface/Exception.h"
32 
33 ICEventInfoProducer::ICEventInfoProducer(const edm::ParameterSet& config)
34  : branch_(config.getParameter<std::string>("branch")),
35  lhe_collection_(config.getParameter<edm::InputTag>("lheProducer")),
36  do_jets_rho_(config.getParameter<bool>("includeJetRho")),
37  input_jets_rho_(config.getParameter<edm::InputTag>("inputJetRho")),
38  do_leptons_rho_(config.getParameter<bool>("includeLeptonRho")),
39  input_leptons_rho_(config.getParameter<edm::InputTag>("inputLeptonRho")),
40  do_vertex_count_(config.getParameter<bool>("includeVertexCount")),
41  input_vertices_(config.getParameter<edm::InputTag>("inputVertices")),
42  do_lhe_weights_(config.getParameter<bool>("includeLHEWeights")),
43  do_embedding_weights_(config.getParameter<bool>("includeEmbeddingWeights")),
44  do_ht_(config.getParameter<bool>("includeHT")),
45  do_csc_filter_(config.getParameter<bool>("includeCSCFilter")),
46  input_csc_filter_(config.getParameter<edm::InputTag>("inputCSCFilter")),
47  do_filtersfromtrig_(config.getParameter<bool>("includeFiltersFromTrig")),
48  filtersfromtrig_input_(config.getParameter<edm::InputTag>("inputfiltersfromtrig")),
49  filtersfromtrig_(config.getParameter<std::vector<std::string> >("filtersfromtrig"))
50 {
51 #if CMSSW_MAJOR_VERSION >= 7
52  consumes<LHERunInfoProduct, edm::InRun>({lhe_collection_});
53 #endif
54  consumes<GenEventInfoProduct>({"generator"});
55  consumes<LHEEventProduct>(lhe_collection_);
56  consumes<double>(input_leptons_rho_);
57  consumes<double>(input_jets_rho_);
58  consumes<edm::View<reco::Vertex>>(input_vertices_);
59  consumes<reco::BeamHaloSummary>(input_csc_filter_);
60  consumes<edm::TriggerResults>(filtersfromtrig_input_);
61  edm::ParameterSet filter_params =
62  config.getParameter<edm::ParameterSet>("filters");
63  std::vector<std::string> filter_names =
64  filter_params.getParameterNamesForType<edm::InputTag>();
65  for (unsigned i = 0; i < filter_names.size(); ++i) {
66  filters_.push_back(std::make_pair(
67  filter_names[i],
68  filter_params.getParameter<edm::InputTag>(filter_names[i])));
69  consumes<bool>(filters_[i].second);
70  if (filters_.back().second.label().at(0) == '!') {
71  std::cout << "Info in <ICEventInfoProducer>: Inverting logic for filter: "
72  << filters_.back().first << std::endl;
73  std::string new_label = filters_.back().second.label();
74  new_label.erase(0, 1);
75  filters_.back().second =
76  edm::InputTag(new_label, filters_.back().second.instance(),
77  filters_.back().second.process());
78  invert_filter_logic_.insert(filters_.back().first);
79  }
80  }
81  for (unsigned iFilter=0;iFilter<filtersfromtrig_.size();iFilter++){
82  if((filtersfromtrig_[iFilter]).at(0)=='!'){
83  filtersfromtrig_[iFilter].erase(0,1);
84  invert_filter_logic_.insert(filtersfromtrig_[iFilter]);
85  }
86  }
87 
88  edm::ParameterSet wt_pset = config.getParameter<edm::ParameterSet>("weights");
89  std::vector<std::string> wt =
90  wt_pset.getParameterNamesForType<edm::InputTag>();
91  for (unsigned i = 0; i < wt.size(); ++i) {
92  weights_.push_back(
93  std::make_pair(wt[i], wt_pset.getParameter<edm::InputTag>(wt[i])));
94  consumes<double>(weights_[i].second);
95  }
96 
97  edm::ParameterSet gwt_pset =
98  config.getParameter<edm::ParameterSet>("genFilterWeights");
99  std::vector<std::string> gwt =
100  gwt_pset.getParameterNamesForType<edm::InputTag>();
101  for (unsigned i = 0; i < gwt.size(); ++i) {
102  gen_weights_.push_back(
103  std::make_pair(gwt[i], gwt_pset.getParameter<edm::InputTag>(gwt[i])));
104  consumes<double>(gen_weights_[i].second);
105  }
106 
107  info_ = new ic::EventInfo();
108 
109  PrintHeaderWithBranch(config, branch_);
110  PrintOptional(1, do_lhe_weights_, "includeLHEWeights");
111  PrintOptional(1, do_embedding_weights_, "includeEmbeddingWeights");
112  PrintOptional(1, do_ht_, "includeHT");
113  PrintOptional(1, do_jets_rho_, "includeJetRho");
114  PrintOptional(1, do_leptons_rho_, "includeLeptonRho");
115  PrintOptional(1, do_vertex_count_, "includeVertexCount");
116  PrintOptional(1, do_csc_filter_, "includeCSCFilter");
117 }
118 
120  delete info_;
121 }
122 
123 void ICEventInfoProducer::endRun(edm::Run const& run, edm::EventSetup const& es) {
124  if (!do_lhe_weights_) return;
125  if (lhe_weight_labels_.size()) return;
126  edm::Handle<LHERunInfoProduct> lhe_info;
127  run.getByLabel(lhe_collection_, lhe_info);
128  bool record = false;
129  for (auto it = lhe_info->headers_begin(); it != lhe_info->headers_end();
130  ++it) {
131  std::vector<std::string>::const_iterator iLt = it->begin();
132  for (; iLt != it->end(); ++iLt) {
133  std::string const& line = *iLt;
134  if (line.find("<weightgroup") != std::string::npos) record = true;
135  if (line.find("</weightgroup") != std::string::npos) record = false;
136  if (record) lhe_weight_labels_.push_back(line);
137  }
138  }
139 }
140 
141 void ICEventInfoProducer::produce(edm::Event& event,
142  const edm::EventSetup& setup) {
143  *info_ = ic::EventInfo();
144  info_->set_is_data(event.isRealData());
145  info_->set_run(event.run());
146 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION ==7 && CMSSW_MINOR_VERSION >= 3)
147  info_->set_event(event.id().event());
148 #else
149  info_->set_event((unsigned long long)event.id().event());
150 #endif
151  info_->set_lumi_block(event.luminosityBlock());
152  info_->set_bunch_crossing(event.bunchCrossing());
153 
154  edm::Handle<double> jets_rho_handle;
155  if (do_jets_rho_) {
156  event.getByLabel(input_jets_rho_, jets_rho_handle);
157  info_->set_jet_rho(*jets_rho_handle);
158  }
159  edm::Handle<double> lepton_rho_handle;
160  if (do_leptons_rho_) {
161  event.getByLabel(input_leptons_rho_, lepton_rho_handle);
162  info_->set_lepton_rho(*lepton_rho_handle);
163  }
164 
165  for (unsigned i = 0; i < weights_.size(); ++i) {
166  edm::Handle<double> weight;
167  event.getByLabel(weights_[i].second, weight);
168  double weights_result = (*weight);
169  info_->set_weight(weights_[i].first, weights_result);
170  }
171 
172 
173  for (unsigned i = 0; i < gen_weights_.size(); ++i) {
174  edm::Handle<GenFilterInfo> weight;
175  event.getByLabel(gen_weights_[i].second, weight);
176  double weights_result = weight->filterEfficiency();
177  info_->set_weight(gen_weights_[i].first, weights_result);
178  }
179 
180  edm::Handle<edm::View<reco::Vertex> > vtxs_handle;
181  if (do_vertex_count_) {
182  event.getByLabel(input_vertices_, vtxs_handle);
183  info_->set_good_vertices(vtxs_handle->size());
184  }
185 
186  edm::Handle<LHEEventProduct> lhe_handle;
187  edm::Handle<GenEventInfoProduct> gen_info_handle;
188  if(do_embedding_weights_){
189  event.getByLabel("generator",gen_info_handle);
190  info_->set_weight("wt_embedding", gen_info_handle->weight());
191  }
192  if(!event.isRealData()){
193  event.getByLabel("generator",gen_info_handle);
194  if(gen_info_handle.isValid()){
195  if(gen_info_handle->weight()>=0){
196  info_->set_weight("wt_mc_sign",1);
197  } else info_->set_weight("wt_mc_sign",-1);
198  }
199  }
200  if(do_lhe_weights_ || do_ht_){
201  event.getByLabel(lhe_collection_, lhe_handle);
202  /* Accessing global evt weights directly from the LHEEventProduct
203  disabled and replaced by taking them from the GenEventInfoProduct
204  which should exist in all samples, independent of which generator was used
205  if(lhe_handle->hepeup().XWGTUP>=0){
206  info_->set_weight("wt_mc_sign",1);
207  } else info_->set_weight("wt_mc_sign",-1);
208  */
209  if (do_ht_){
210  std::vector<lhef::HEPEUP::FiveVector> lheParticles = lhe_handle->hepeup().PUP;
211  double lheHt = 0.;
212  unsigned nOutgoingPartons = 0;
213  std::vector<ROOT::Math::PxPyPzEVector> zll_cands;
214  for(size_t idxPart = 0; idxPart < lheParticles.size();++idxPart){
215  unsigned absPdgId = TMath::Abs(lhe_handle->hepeup().IDUP[idxPart]);
216  unsigned status = lhe_handle->hepeup().ISTUP[idxPart];
217  if(status==1 &&((absPdgId >=1 &&absPdgId<=6) || absPdgId == 21)){
218  lheHt += TMath::Sqrt(TMath::Power(lheParticles[idxPart][0],2) + TMath::Power(lheParticles[idxPart][1],2));
219  nOutgoingPartons++;
220  }
221  if(status == 1 && (absPdgId ==11 || absPdgId == 13 || absPdgId ==15)){
222  zll_cands.push_back(ROOT::Math::PxPyPzEVector(lheParticles[idxPart][0],lheParticles[idxPart][1],lheParticles[idxPart][2],lheParticles[idxPart][3]));
223  }
224  }
225  if(zll_cands.size() == 2){
226  info_->set_gen_mll((zll_cands[0]+zll_cands[1]).M());
227  }
228  info_->set_gen_ht(lheHt);
229  info_->set_n_outgoing_partons(nOutgoingPartons);
230  }
231  if (do_lhe_weights_) {
232  double nominal_wt = lhe_handle->hepeup().XWGTUP;
233  for (unsigned i = 0; i < lhe_handle->weights().size(); ++i) {
234  info_->set_weight(lhe_handle->weights()[i].id,
235  lhe_handle->weights()[i].wgt / nominal_wt, false);
236  }
237  }
238  }
239 
240  for (unsigned i = 0; i < filters_.size(); ++i) {
241  edm::Handle<bool> filter;
242  event.getByLabel(filters_[i].second, filter);
243  bool filter_result = (*filter);
244  if (invert_filter_logic_.find(filters_[i].first) !=
245  invert_filter_logic_.end())
246  filter_result = !filter_result;
247  info_->set_filter_result(filters_[i].first, filter_result);
248  observed_filters_[filters_[i].first] = CityHash64(filters_[i].first);
249  }
250 
251  if (do_filtersfromtrig_) {
252  edm::Handle<edm::TriggerResults> triggerResults;
253  event.getByLabel(filtersfromtrig_input_, triggerResults);
254  if (!triggerResults.isValid()) {
255  throw cms::Exception("TriggerNotValid")
256  << "Trigger Results is not valid\n";
257  }
258 
259  const edm::TriggerNames& triggerNames = event.triggerNames(*triggerResults);
260  for (unsigned iTrigger = 0; iTrigger < triggerResults->size(); iTrigger++) {
261  std::string trigName = triggerNames.triggerName(iTrigger);
262  bool pass = false;
263  if (filtersfromtrig_.size() == 1 && filtersfromtrig_[0] == "*") {
264  // copy all filters
265  pass = true;
266  } else {
267  // copy only filters given in input
268  for (unsigned iFilter = 0; iFilter < filtersfromtrig_.size();
269  iFilter++) {
270  std::string filtername = filtersfromtrig_[iFilter];
271  if (filtername.find(trigName) != std::string::npos) {
272  pass = true;
273  break;
274  }
275  }
276  // if (!pass) {
277  // throw cms::Exception("TriggerNotFound")<<trigName<<" was not found in
278  // trigger results\n";
279  //}
280  }
281  if (!pass) continue;
282  if (!triggerResults->wasrun(iTrigger)) {
283  throw cms::Exception("TriggerNotRun") << trigName << " was not run\n";
284  }
285  bool filter_result = triggerResults->accept(iTrigger);
286  if (invert_filter_logic_.find(trigName) != invert_filter_logic_.end())
287  filter_result = !filter_result;
288 
289  // std::cout << " Filter " << trigName << " " <<
290  // triggerResults->accept(iTrigger) << " " << filter_result << std::endl;
291 
292  info_->set_filter_result(trigName, filter_result);
293  observed_filters_[trigName] = CityHash64(trigName);
294  } // loop on triggers
295  }
296 
297  edm::Handle<reco::BeamHaloSummary> beam_halo_handle;
298  if (do_csc_filter_) {
299  event.getByLabel(input_csc_filter_, beam_halo_handle);
300  info_->set_filter_result("CSCTightHaloFilter",
301  beam_halo_handle->CSCTightHaloId());
302  observed_filters_["CSCTightHaloFilter"] = CityHash64("CSCTightHaloFilter");
303  }
304 }
305 
306 void ICEventInfoProducer::beginJob() {
307  ic::StaticTree::tree_->Branch(branch_.c_str(), &info_);
308 }
309 
310 void ICEventInfoProducer::endJob() {
311  if (!observed_filters_.empty()) {
312  std::cout << std::string(78, '-') << "\n";
313  std::cout << boost::format("%-56s %20s\n") %
314  std::string("EventInfo Filters") %
315  std::string("Hash Summmary");
316  std::map<std::string, std::size_t>::const_iterator iter;
317  for (iter = observed_filters_.begin(); iter != observed_filters_.end();
318  ++iter) {
319  std::cout << boost::format("%-56s| %020i\n") % iter->first % iter->second;
320  }
321  }
322  if (lhe_weight_labels_.size()) {
323  std::cout << std::string(78, '-') << "\n";
324  std::cout << "LHE event weights\n";
325  for (unsigned l = 0; l < lhe_weight_labels_.size(); ++l) {
326  std::cout << lhe_weight_labels_[l];
327  }
328  }
329 }
330 
331 // define this as a plug-in
void set_weight(std::string const &label, double const &weight, bool const &enabled)
Add a new weight, overriding any existing value with the same label.
Definition: EventInfo.hh:146
void set_is_data(bool const &is_data)
If event is real data returns true, otherwise false
Definition: EventInfo.hh:73
void set_filter_result(std::string const &label, bool const &result)
Set the filter result for label to result, overwriting any existing filter result with this label...
Definition: EventInfo.hh:236
uint64 CityHash64(const char *buf, size_t len)
Definition: city.cc:200
void set_n_outgoing_partons(unsigned const &n_outgoing_partons)
Number of outgoing partons at generator level, used for combining n-jet binned samples with inclusive...
Definition: EventInfo.hh:107
void PrintHeaderWithBranch(edm::ParameterSet const &config, std::string branch)
void set_gen_ht(double const &gen_ht)
Generator level HT, used for combining HT binned samples with inclusive samples.
Definition: EventInfo.hh:100
void set_lepton_rho(double const &lepton_rho)
Energy density used for calculating lepton isolation in this event.
Definition: EventInfo.hh:95
void set_run(int const &run)
Run number.
Definition: EventInfo.hh:79
void set_gen_mll(double const &gen_mll)
Generator level M_ll.
Definition: EventInfo.hh:104
static TTree * tree_
Definition: StaticTree.hh:13
ICEventInfoProducer(const edm::ParameterSet &)
Stores core event information such as run, lumi and event number, as well as event weights and filter...
Definition: EventInfo.hh:21
void set_lumi_block(int const &lumi_block)
Lumisection number.
Definition: EventInfo.hh:82
void set_good_vertices(unsigned const &good_vertices)
Number of reconstructed vertices passing some baseline quality requirements.
Definition: EventInfo.hh:110
void set_event(unsigned long long const &event)
Event number.
Definition: EventInfo.hh:76
void PrintOptional(unsigned depth, bool value, std::string text)
Definition: Consumes.h:19
void set_bunch_crossing(int const &bunch_crossing)
Bunch crossing number.
Definition: EventInfo.hh:87
void set_jet_rho(double const &jet_rho)
Energy density used for the jet energy corrections in this event.
Definition: EventInfo.hh:92
Produces an ic::EventInfo object.
DEFINE_FWK_MODULE(ICEventInfoProducer)