#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include "boost/format.hpp"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Common/interface/TriggerNames.h"
#include "DataFormats/Common/interface/TriggerResults.h"
#include "DataFormats/PatCandidates/interface/TriggerEvent.h"
#include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
#include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
#include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
const edm::ParameterSet& config)
: input_(config.getParameter<
edm::InputTag>(
"input")),
input_trigres_(config.getParameter<
edm::InputTag>(
"inputTriggerResults")),
branch_(config.getParameter<
std::string>(
"branch")),
hlt_path_(config.getParameter<
std::string>(
"hltPath")),
store_only_if_fired_(config.getParameter<bool>("storeOnlyIfFired")),
input_is_standalone_(config.getParameter<bool>("inputIsStandAlone")) {
if(!input_is_standalone_){
consumes<pat::TriggerEvent>(input_);
} else {
consumes<pat::TriggerObjectStandAloneCollection>(input_);
}
consumes<edm::TriggerResults>(input_trigres_);
objects_ = new std::vector<ic::TriggerObject>();
std::cout << boost::format("%-15s : %-60s\n") % "Path" % hlt_path_;
}
void ICTriggerObjectProducer::produce(edm::Event& event,
const edm::EventSetup& setup) {
objects_->clear();
if (!input_is_standalone_) {
edm::Handle<pat::TriggerEvent> trig_handle;
event.getByLabel(input_, trig_handle);
std::vector<pat::TriggerPath> const* paths = trig_handle->paths();
bool fired = true;
bool path_found = false;
std::string full_name;
for (unsigned i = 0; i < paths->size(); ++i) {
std::string const& name = paths->at(i).name();
if (name.find(hlt_path_) != name.npos) {
full_name = name;
path_found = true;
if (store_only_if_fired_ && !(paths->at(i).wasAccept())) fired = false;
break;
}
}
if (!fired || !path_found) return;
pat::TriggerObjectRefVector objects =
trig_handle->pathObjects(full_name, false);
for (unsigned i = 0; i < objects.size(); ++i) {
pat::TriggerObject const& src = *(objects.at(i));
std::vector<std::size_t> filter_labels;
pat::TriggerFilterRefVector filters =
trig_handle->objectFilters((objects)[i], false);
for (unsigned k = 0; k < filters.size(); ++k) {
if (!trig_handle->filterInPath(filters[k], full_name, false)) continue;
filter_labels.push_back(
CityHash64(filters[k]->label()));
observed_filters_[filters[k]->label()] =
CityHash64(filters[k]->label());
}
ui64 packed_type;
packed_type.one = 0;
unsigned n_types = std::min(std::size_t(4), src.triggerObjectTypes().size());
for (unsigned t = 0; t < n_types; ++t) {
packed_type.four[t] = src.triggerObjectTypes()[t];
}
}
} else {
edm::Handle<edm::TriggerResults> trigres_handle;
event.getByLabel(input_trigres_, trigres_handle);
edm::TriggerNames const& names = event.triggerNames(*trigres_handle);
bool fired = true;
bool path_found = false;
std::string full_name;
for (unsigned int i = 0, n = trigres_handle->size(); i < n; ++i) {
std::string const& name = names.triggerName(i);
if (name.find(hlt_path_) != name.npos) {
full_name = name;
path_found = true;
if (store_only_if_fired_ && !(trigres_handle->accept(i))) fired = false;
break;
}
}
if (!fired || !path_found) return;
std::set<std::string> path_filters;
std::vector<std::string> const& filt_vec =
hlt_config_.saveTagsModules(full_name);
for (unsigned i = 0; i < filt_vec.size(); ++i)
path_filters.insert(filt_vec[i]);
edm::Handle<pat::TriggerObjectStandAloneCollection> trigobj_handle;
event.getByLabel(input_, trigobj_handle);
for (unsigned i = 0; i < trigobj_handle->size(); ++i) {
pat::TriggerObjectStandAlone src = trigobj_handle->at(i);
#if CMSSW_MAJOR_VERSION >= 7
src.unpackPathNames(names);
#endif
std::vector<std::string> const& pathnames = src.pathNames(false,false);
bool obj_in_path = false;
for (unsigned j = 0; j < pathnames.size(); ++j) {
if (full_name == pathnames[j]) {
obj_in_path = true;
break;
}
}
if (!obj_in_path) continue;
std::vector<std::string> const& filters = src.filterLabels();
std::vector<std::size_t> filter_labels;
for (unsigned k = 0; k < filters.size(); ++k) {
if (!path_filters.count(filters[k])) continue;
observed_filters_[filters[k]] =
CityHash64(filters[k]);
}
ui64 packed_type;
packed_type.one = 0;
unsigned n_types = std::min(std::size_t(4), src.triggerObjectTypes().size());
for (unsigned t = 0; t < n_types; ++t) {
packed_type.four[t] = src.triggerObjectTypes()[t];
}
}
}
}
void ICTriggerObjectProducer::beginRun(edm::Run const& run,
edm::EventSetup const& es) {
if (input_is_standalone_) {
std::string proc =
input_trigres_.process() != "" ? input_trigres_.process() : "HLT";
bool changed = true;
bool res = hlt_config_.init(run, es, proc, changed);
if (!res)
throw std::runtime_error(
"HLTConfigProvider did not initialise correctly");
}
}
void ICTriggerObjectProducer::beginJob() {
}
void ICTriggerObjectProducer::endJob() {
std::cout << std::string(78, '-') << "\n";
std::cout << boost::format("Path: %-50s %20s\n")
% hlt_path_ % std::string("Hash Summmary");
std::map<std::string, std::size_t>::const_iterator iter;
for (iter = observed_filters_.begin(); iter != observed_filters_.end();
++iter) {
std::cout << boost::format("%-56s| %020i\n") % iter->first % iter->second;
}
}