Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICTriggerPathProducer.cc
Go to the documentation of this file.
2 #include <string>
3 #include <vector>
4 #include "boost/format.hpp"
5 #include "boost/lexical_cast.hpp"
6 #include "FWCore/Framework/interface/Event.h"
7 #include "FWCore/Framework/interface/EventSetup.h"
8 #include "FWCore/Framework/interface/MakerMacros.h"
9 #include "FWCore/ParameterSet/interface/ParameterSet.h"
10 #include "FWCore/Common/interface/TriggerNames.h"
11 #include "FWCore/Utilities/interface/InputTag.h"
12 #include "DataFormats/Common/interface/Handle.h"
13 #include "DataFormats/Common/interface/View.h"
14 #include "DataFormats/Common/interface/ValueMap.h"
15 #include "DataFormats/Common/interface/TriggerResults.h"
16 #include "DataFormats/PatCandidates/interface/TriggerEvent.h"
17 #include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
24 
25 #if CMSSW_MAJOR_VERSION >= 7
26 // In MiniAOD the prescales must be extracted from a new
27 // "pat::PackedTriggerPrescales" object, but this class was only introduced in
28 // CMSSW_7_0_5
29 #include "DataFormats/PatCandidates/interface/PackedTriggerPrescales.h"
30 #endif
31 
32 #if CMSSW_MAJOR_VERSION >= 8
33 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
34 #endif
35 
36 
37 ICTriggerPathProducer::ICTriggerPathProducer(const edm::ParameterSet& config)
38  : input_(config.getParameter<edm::InputTag>("input")),
39  branch_(config.getParameter<std::string>("branch")),
40  include_if_fired_(config.getParameter<bool>("includeAcceptedOnly")),
41  save_strings_(config.getParameter<bool>("saveStrings")),
42  split_version_(config.getParameter<bool>("splitVersion")),
43  input_is_standalone_(config.getParameter<bool>("inputIsStandAlone")),
44  input_prescales_(config.getParameter<edm::InputTag>("inputPrescales")),
45  hlt_process_(config.getParameter<std::string>("hltProcess")),
46  prescale_fallback_(config.getParameter<bool>("prescaleFallback"))
47  {
48  if(!input_is_standalone_){
49  consumes<pat::TriggerEvent>(input_);
50  } else {
51  consumes<edm::TriggerResults>(input_);
52  }
53 #if CMSSW_MAJOR_VERSION >= 7
54  consumes<pat::PackedTriggerPrescales>(input_prescales_);
55 #endif
56 #if CMSSW_MAJOR_VERSION >= 8
57  if (prescale_fallback_) {
58  consumes<BXVector<GlobalAlgBlk>>(edm::InputTag("gtStage2Digis"));
59  }
60 #endif
61  paths_ = new std::vector<ic::TriggerPath>();
62  PrintHeaderWithProduces(config, input_, branch_);
63  PrintOptional(1, include_if_fired_, "includeAcceptedOnly");
64  PrintOptional(1, save_strings_, "saveStrings");
65  PrintOptional(1, split_version_, "splitVersion");
66  PrintOptional(1, input_is_standalone_, "inputIsStandAlone");
67 }
68 
70 
71 void ICTriggerPathProducer::produce(edm::Event& event,
72  const edm::EventSetup& setup) {
73  paths_->clear();
74 
75  if (!input_is_standalone_) {
76  edm::Handle<pat::TriggerEvent> trig_handle;
77  event.getByLabel(input_, trig_handle);
78  std::vector<pat::TriggerPath> const* paths = trig_handle->paths();
79  paths_->reserve(paths->size());
80  for (unsigned i = 0; i < paths->size(); ++i) {
81  pat::TriggerPath const& src = paths->at(i);
82  if (!src.wasAccept() && include_if_fired_) continue;
83  paths_->push_back(ic::TriggerPath());
84  ic::TriggerPath & dest = paths_->back();
85  dest.set_accept(src.wasAccept());
86  dest.set_prescale(src.prescale());
87  std::string name = src.name();
88  SetNameInfo(name, &dest);
89  }
90  } else { // i.e. MiniAOD
91  edm::Handle<edm::TriggerResults> trigres_handle;
92  event.getByLabel(input_, trigres_handle);
93 
94 #if CMSSW_MAJOR_VERSION >= 7
95  edm::Handle<pat::PackedTriggerPrescales> prescales_handle;
96  event.getByLabel(input_prescales_, prescales_handle);
97 #endif
98 
99 #if CMSSW_MAJOR_VERSION >= 8
100  edm::Handle<BXVector<GlobalAlgBlk>> l1algo_handle;
101  if (prescale_fallback_) {
102  event.getByLabel(edm::InputTag("gtStage2Digis"), l1algo_handle);
103  }
104 #endif
105 
106  edm::TriggerNames const& names = event.triggerNames(*trigres_handle);
107  paths_->reserve(trigres_handle->size());
108  for (unsigned int i = 0, n = trigres_handle->size(); i < n; ++i) {
109  if (!trigres_handle->accept(i) && include_if_fired_) continue;
110  paths_->push_back(ic::TriggerPath());
111  ic::TriggerPath & dest = paths_->back();
112  dest.set_accept(trigres_handle->accept(i));
113 #if CMSSW_MAJOR_VERSION >= 7
114  dest.set_prescale(prescales_handle->getPrescaleForIndex(i));
115  #if CMSSW_MAJOR_VERSION >= 8
116  if (prescale_fallback_) {
117  unsigned column = l1algo_handle->at(0, 0).getPreScColumn();
118  unsigned other_prescale = hlt_config_.prescaleValue(column, names.triggerName(i));
119  // if (dest.prescale() != other_prescale) {
120  // std::cout << names.triggerName(i) << "\t" << hlt_config_.tableName() << "\t" << dest.prescale() << "\t" << other_prescale << "\n";
121  // }
122  dest.set_prescale(other_prescale);
123  }
124  #endif
125 #else
126  dest.set_prescale(0);
127 #endif
128  std::string name = names.triggerName(i);
129  SetNameInfo(name, &dest);
130  }
131  }
132 }
133 
134 void ICTriggerPathProducer::SetNameInfo(std::string name,
135  ic::TriggerPath* path) {
136  if (split_version_) {
137  std::size_t v_pos = name.find_last_of('v');
138  if (v_pos != std::string::npos) {
139  std::string post_v = name.substr(v_pos+1);
140  std::string pre_v = name.substr(0, v_pos+1);
141  try {
142  unsigned v = boost::lexical_cast<unsigned>(post_v);
143  name = pre_v;
144  path->set_version(v);
145  }
146  catch(boost::bad_lexical_cast const& e) {
147  }
148  }
149  }
150  std::size_t hash = CityHash64(name);
151  if (save_strings_) {
152  path->set_name(name);
153  } else {
154  if (!observed_paths_.count(name)) {
155  observed_paths_[name] = hash;
156  }
157  }
158  path->set_id(hash);
159 }
160 
161 void ICTriggerPathProducer::beginRun(edm::Run const& run,
162  edm::EventSetup const& es) {
163  bool changed = true;
164  bool res = hlt_config_.init(run, es, hlt_process_, changed);
165  if (!res)
166  throw std::runtime_error(
167  "HLTConfigProvider did not initialise correctly");
168 }
169 
170 void ICTriggerPathProducer::beginJob() {
171  ic::StaticTree::tree_->Branch(branch_.c_str(), &paths_);
172 }
173 
174 void ICTriggerPathProducer::endJob() {
175  // If the trigger path strings were not saved print a summary
176  // of the string hashes
177  if (!save_strings_) {
178  std::cout << std::string(78, '-') << "\n";
179  std::cout << boost::format("%-56s %20s\n")
180  % "HLT Paths" % std::string("Hash Summmary");
181  std::map<std::string, std::size_t>::const_iterator iter;
182  for (iter = observed_paths_.begin(); iter != observed_paths_.end();
183  ++iter) {
184  ICHashTreeProducer::Add(iter->second, iter->first);
185  std::cout << boost::format("%-56s| %020i\n") % iter->first % iter->second;
186  }
187  }
188 }
189 
190 // define this as a plug-in
void set_name(std::string const &name)
The trigger path name.
Definition: TriggerPath.hh:40
void set_prescale(unsigned const &prescale)
The trigger prescale.
Definition: TriggerPath.hh:46
uint64 CityHash64(const char *buf, size_t len)
Definition: city.cc:200
Information on a trigger path, including accept, prescale and version information.
Definition: TriggerPath.hh:13
ICTriggerPathProducer(const edm::ParameterSet &)
void set_version(unsigned const &version)
The trigger version number.
Definition: TriggerPath.hh:52
static TTree * tree_
Definition: StaticTree.hh:13
DEFINE_FWK_MODULE(ICTriggerPathProducer)
void set_id(std::size_t const &id)
Unique identifier, usually a hash of the trigger name.
Definition: TriggerPath.hh:49
void PrintOptional(unsigned depth, bool value, std::string text)
void set_accept(bool const &accept)
True if the trigger fired, false otherwise.
Definition: TriggerPath.hh:43
Definition: Consumes.h:19
void PrintHeaderWithProduces(edm::ParameterSet const &config, edm::InputTag const &in, std::string branch)
See documentation here.
static void Add(std::size_t const &id, std::string const &str)