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"
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"))
51 #if CMSSW_MAJOR_VERSION >= 7
52 consumes<LHERunInfoProduct, edm::InRun>({lhe_collection_});
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(
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);
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]);
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) {
93 std::make_pair(wt[i], wt_pset.getParameter<edm::InputTag>(wt[i])));
94 consumes<double>(weights_[i].second);
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);
111 PrintOptional(1, do_embedding_weights_,
"includeEmbeddingWeights");
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);
129 for (
auto it = lhe_info->headers_begin(); it != lhe_info->headers_end();
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);
141 void ICEventInfoProducer::produce(edm::Event& event,
142 const edm::EventSetup& setup) {
146 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION ==7 && CMSSW_MINOR_VERSION >= 3)
149 info_->
set_event((
unsigned long long)event.id().event());
154 edm::Handle<double> jets_rho_handle;
156 event.getByLabel(input_jets_rho_, jets_rho_handle);
159 edm::Handle<double> lepton_rho_handle;
160 if (do_leptons_rho_) {
161 event.getByLabel(input_leptons_rho_, lepton_rho_handle);
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);
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);
180 edm::Handle<edm::View<reco::Vertex> > vtxs_handle;
181 if (do_vertex_count_) {
182 event.getByLabel(input_vertices_, vtxs_handle);
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());
192 if(!event.isRealData()){
193 event.getByLabel(
"generator",gen_info_handle);
194 if(gen_info_handle.isValid()){
195 if(gen_info_handle->weight()>=0){
200 if(do_lhe_weights_ || do_ht_){
201 event.getByLabel(lhe_collection_, lhe_handle);
210 std::vector<lhef::HEPEUP::FiveVector> lheParticles = lhe_handle->hepeup().PUP;
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));
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]));
225 if(zll_cands.size() == 2){
226 info_->
set_gen_mll((zll_cands[0]+zll_cands[1]).M());
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);
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;
248 observed_filters_[filters_[i].first] =
CityHash64(filters_[i].first);
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";
259 const edm::TriggerNames& triggerNames =
event.triggerNames(*triggerResults);
260 for (
unsigned iTrigger = 0; iTrigger < triggerResults->size(); iTrigger++) {
261 std::string trigName = triggerNames.triggerName(iTrigger);
263 if (filtersfromtrig_.size() == 1 && filtersfromtrig_[0] ==
"*") {
268 for (
unsigned iFilter = 0; iFilter < filtersfromtrig_.size();
270 std::string filtername = filtersfromtrig_[iFilter];
271 if (filtername.find(trigName) != std::string::npos) {
282 if (!triggerResults->wasrun(iTrigger)) {
283 throw cms::Exception(
"TriggerNotRun") << trigName <<
" was not run\n";
285 bool filter_result = triggerResults->accept(iTrigger);
286 if (invert_filter_logic_.find(trigName) != invert_filter_logic_.end())
287 filter_result = !filter_result;
293 observed_filters_[trigName] =
CityHash64(trigName);
297 edm::Handle<reco::BeamHaloSummary> beam_halo_handle;
298 if (do_csc_filter_) {
299 event.getByLabel(input_csc_filter_, beam_halo_handle);
301 beam_halo_handle->CSCTightHaloId());
302 observed_filters_[
"CSCTightHaloFilter"] =
CityHash64(
"CSCTightHaloFilter");
306 void ICEventInfoProducer::beginJob() {
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();
319 std::cout << boost::format(
"%-56s| %020i\n") % iter->first % iter->second;
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];
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.
void set_is_data(bool const &is_data)
If event is real data returns true, otherwise false
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...
uint64 CityHash64(const char *buf, size_t len)
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...
void set_gen_ht(double const &gen_ht)
Generator level HT, used for combining HT binned samples with inclusive samples.
void set_lepton_rho(double const &lepton_rho)
Energy density used for calculating lepton isolation in this event.
void set_run(int const &run)
Run number.
void set_gen_mll(double const &gen_mll)
Generator level M_ll.
ICEventInfoProducer(const edm::ParameterSet &)
Stores core event information such as run, lumi and event number, as well as event weights and filter...
void set_lumi_block(int const &lumi_block)
Lumisection number.
void set_good_vertices(unsigned const &good_vertices)
Number of reconstructed vertices passing some baseline quality requirements.
void set_event(unsigned long long const &event)
Event number.
void set_bunch_crossing(int const &bunch_crossing)
Bunch crossing number.
void set_jet_rho(double const &jet_rho)
Energy density used for the jet energy corrections in this event.
Produces an ic::EventInfo object.
DEFINE_FWK_MODULE(ICEventInfoProducer)