CMS 3D CMS Logo

PrescaleWeightProvider.cc
Go to the documentation of this file.
1 //
2 // See header file for description
3 //
4 
6 
7 #include <sstream>
8 
13 
17 
19  // default values
20  : init_(false),
21  verbosity_(0),
22  triggerResultsTag_("TriggerResults::HLT"),
23  triggerResultsToken_(iC.mayConsume<edm::TriggerResults>(triggerResultsTag_)),
24  l1GtTriggerMenuLiteTag_("l1GtTriggerMenuLite"),
25  l1GtTriggerMenuLiteToken_(iC.mayConsume<L1GtTriggerMenuLite, edm::InRun>(l1GtTriggerMenuLiteTag_)) {
26  hltPaths_.clear();
27  if (config.exists("prescaleWeightVerbosityLevel"))
28  verbosity_ = config.getParameter<unsigned>("prescaleWeightVerbosityLevel");
29  if (config.exists("prescaleWeightTriggerResults"))
30  triggerResultsTag_ = config.getParameter<edm::InputTag>("prescaleWeightTriggerResults");
31  if (config.exists("prescaleWeightL1GtTriggerMenuLite"))
32  l1GtTriggerMenuLiteTag_ = config.getParameter<edm::InputTag>("prescaleWeightL1GtTriggerMenuLite");
33  if (config.exists("prescaleWeightHltPaths"))
34  hltPaths_ = config.getParameter<std::vector<std::string> >("prescaleWeightHltPaths");
35 
36  configured_ = true;
37  if (triggerResultsTag_.process().empty()) {
38  configured_ = false;
39  if (verbosity_ > 0)
40  edm::LogWarning("PrescaleWeightProvider") << "Process name not configured via TriggerResults InputTag";
41  } else if (triggerResultsTag_.label().empty()) {
42  configured_ = false;
43  if (verbosity_ > 0)
44  edm::LogWarning("PrescaleWeightProvider") << "TriggerResults label not configured";
45  } else if (l1GtTriggerMenuLiteTag_.label().empty()) {
46  configured_ = false;
47  if (verbosity_ > 0)
48  edm::LogWarning("PrescaleWeightProvider") << "L1GtTriggerMenuLite label not configured";
49  } else if (hltPaths_.empty()) {
50  configured_ = false;
51  if (verbosity_ > 0)
52  edm::LogError("PrescaleWeightProvider") << "HLT paths of interest not configured";
53  }
54  if (configured_) {
57  }
58 }
59 
61  init_ = true;
62 
63  if (!configured_) {
64  init_ = false;
65  if (verbosity_ > 0)
66  edm::LogWarning("PrescaleWeightProvider") << "Run initialisation failed due to failing configuration";
67  return;
68  }
69 
70  HLTConfigProvider const& hltConfig = hltPrescaleProvider_->hltConfigProvider();
71  bool hltChanged(false);
72  if (!hltPrescaleProvider_->init(run, setup, triggerResultsTag_.process(), hltChanged)) {
73  if (verbosity_ > 0)
74  edm::LogError("PrescaleWeightProvider")
75  << "HLT config initialization error with process name \"" << triggerResultsTag_.process() << "\"";
76  init_ = false;
77  } else if (hltConfig.size() <= 0) {
78  if (verbosity_ > 0)
79  edm::LogError("PrescaleWeightProvider") << "HLT config size error";
80  init_ = false;
81  } else if (hltChanged) {
82  if (verbosity_ > 0)
83  edm::LogInfo("PrescaleWeightProvider") << "HLT configuration changed";
84  }
85  if (!init_)
86  return;
87 
89  if (!triggerMenuLite_.isValid()) {
90  if (verbosity_ > 0)
91  edm::LogError("PrescaleWeightProvider")
92  << "L1GtTriggerMenuLite with label \"" << l1GtTriggerMenuLiteTag_.label() << "\" not found";
93  init_ = false;
94  }
95 }
96 
98  if (!init_)
99  return 1;
100 
101  // L1
102  L1GtUtils const& l1GtUtils = hltPrescaleProvider_->l1GtUtils();
103 
104  // HLT
105  HLTConfigProvider const& hltConfig = hltPrescaleProvider_->hltConfigProvider();
106 
108  event.getByToken(triggerResultsToken_, triggerResults);
109  if (!triggerResults.isValid()) {
110  if (verbosity_ > 0)
111  edm::LogError("PrescaleWeightProvider::prescaleWeight")
112  << "TriggerResults product not found for InputTag \"" << triggerResultsTag_.encode() << "\"";
113  return 1;
114  }
115 
116  const int SENTINEL(-1);
117  int weight(SENTINEL);
118 
119  for (unsigned ui = 0; ui < hltPaths_.size(); ui++) {
120  const std::string hltPath(hltPaths_.at(ui));
121  unsigned hltIndex(hltConfig.triggerIndex(hltPath));
122  if (hltIndex == hltConfig.size()) {
123  if (verbosity_ > 0)
124  edm::LogError("PrescaleWeightProvider::prescaleWeight") << "HLT path \"" << hltPath << "\" does not exist";
125  continue;
126  }
127  if (!triggerResults->accept(hltIndex))
128  continue;
129 
130  const std::vector<std::pair<bool, std::string> >& level1Seeds = hltConfig.hltL1GTSeeds(hltPath);
131  if (level1Seeds.size() != 1) {
132  if (verbosity_ > 0)
133  edm::LogError("PrescaleWeightProvider::prescaleWeight")
134  << "HLT path \"" << hltPath << "\" provides too many L1 seeds";
135  return 1;
136  }
137  parseL1Seeds(level1Seeds.at(0).second);
138  if (l1SeedPaths_.empty()) {
139  if (verbosity_ > 0)
140  edm::LogWarning("PrescaleWeightProvider::prescaleWeight")
141  << "Failed to parse L1 seeds for HLT path \"" << hltPath << "\"";
142  continue;
143  }
144 
145  int l1Prescale(SENTINEL);
146  for (unsigned uj = 0; uj < l1SeedPaths_.size(); uj++) {
147  int l1TempPrescale(SENTINEL);
148  int errorCode(0);
149  if (level1Seeds.at(0).first) { // technical triggers
150  unsigned techBit(atoi(l1SeedPaths_.at(uj).c_str()));
151  const std::string techName(*(triggerMenuLite_->gtTechTrigName(techBit, errorCode)));
152  if (errorCode != 0)
153  continue;
154  if (!l1GtUtils.decision(event, techName, errorCode))
155  continue;
156  if (errorCode != 0)
157  continue;
158  l1TempPrescale = l1GtUtils.prescaleFactor(event, techName, errorCode);
159  if (errorCode != 0)
160  continue;
161  } else { // algorithmic triggers
162  if (!l1GtUtils.decision(event, l1SeedPaths_.at(uj), errorCode))
163  continue;
164  if (errorCode != 0)
165  continue;
166  l1TempPrescale = l1GtUtils.prescaleFactor(event, l1SeedPaths_.at(uj), errorCode);
167  if (errorCode != 0)
168  continue;
169  }
170  if (l1TempPrescale > 0) {
171  if (l1Prescale == SENTINEL || l1Prescale > l1TempPrescale)
172  l1Prescale = l1TempPrescale;
173  }
174  }
175  if (l1Prescale == SENTINEL) {
176  if (verbosity_ > 0)
177  edm::LogError("PrescaleWeightProvider::prescaleWeight")
178  << "Unable to find the L1 prescale for HLT path \"" << hltPath << "\"";
179  continue;
180  }
181 
182  int hltPrescale(hltPrescaleProvider_->prescaleValue(event, setup, hltPath));
183 
184  if (hltPrescale * l1Prescale > 0) {
185  if (weight == SENTINEL || weight > hltPrescale * l1Prescale) {
186  weight = hltPrescale * l1Prescale;
187  }
188  }
189  }
190 
191  if (weight == SENTINEL) {
192  if (verbosity_ > 0)
193  edm::LogWarning("PrescaleWeightProvider::prescaleWeight")
194  << "No valid weight for any requested HLT path, returning default weight of 1";
195  return 1;
196  }
197  return weight;
198 }
199 
201  l1SeedPaths_.clear();
202  std::stringstream ss(l1Seeds);
204 
205  while (ss.good() && !ss.eof()) {
206  ss >> buf;
207  if (buf[0] == '(' || buf[buf.size() - 1] == ')' || buf == "AND" || buf == "NOT") {
208  l1SeedPaths_.clear();
209  if (verbosity_ > 0)
210  edm::LogWarning("PrescaleWeightProvider::parseL1Seeds") << "Only supported logical expression is OR";
211  return;
212  } else if (buf == "OR")
213  continue;
214  else
215  l1SeedPaths_.push_back(buf);
216  }
217 }
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
PrescaleWeightProvider(const edm::ParameterSet &config, edm::ConsumesCollector &&iC, T &module)
int prescaleWeight(const edm::Event &event, const edm::EventSetup &setup)
std::string encode() const
Definition: InputTag.cc:159
const int prescaleFactor(const edm::Event &iEvent, const std::string &nameAlgoTechTrig, int &errorCode) const
return prescale factor for a given algorithm or technical trigger
Definition: L1GtUtils.cc:1078
edm::Handle< L1GtTriggerMenuLite > triggerMenuLite_
Definition: weight.py:1
Definition: config.py:1
std::string const & label() const
Definition: InputTag.h:36
Log< level::Error, false > LogError
std::vector< std::string > l1SeedPaths_
std::vector< std::string > hltPaths_
std::unique_ptr< HLTPrescaleProvider > hltPrescaleProvider_
void initRun(const edm::Run &run, const edm::EventSetup &setup)
static std::string const triggerResults
Definition: EdmProvDump.cc:44
edm::EDGetTokenT< L1GtTriggerMenuLite > l1GtTriggerMenuLiteToken_
Log< level::Info, false > LogInfo
const std::string * gtTechTrigName(const unsigned int bitNumber, int &errorCode) const
bool isValid() const
Definition: HandleBase.h:70
const bool decision(const edm::Event &iEvent, const std::string &nameAlgoTechTrig, int &errorCode) const
Definition: L1GtUtils.cc:1066
HLT enums.
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
Log< level::Warning, false > LogWarning
std::string const & process() const
Definition: InputTag.h:40
void parseL1Seeds(const std::string &l1Seeds)
Definition: event.py:1
Definition: Run.h:45