CMS 3D CMS Logo

TrigObjTnPHistColl.cc
Go to the documentation of this file.
1 #include "TrigObjTnPHistColl.h"
2 
5 
6 namespace {
7  std::vector<float> convertToFloat(const std::vector<double>& vecD) {
8  return std::vector<float>(vecD.begin(), vecD.end());
9  }
10 } // namespace
11 
13  : tagCuts_(config.getParameter<std::vector<edm::ParameterSet>>("tagCuts")),
14  probeCuts_(config.getParameter<std::vector<edm::ParameterSet>>("probeCuts")),
15  tagFilters_(config.getParameter<edm::ParameterSet>("tagFilters")),
16  collName_(config.getParameter<std::string>("collName")),
17  folderName_(config.getParameter<std::string>("folderName")),
18  histDefs_(config.getParameter<edm::ParameterSet>("histDefs")),
19  evtTrigSel_(config.getParameter<edm::ParameterSet>("evtTrigSel"))
20 
21 {
22  auto probeFilters = config.getParameter<std::vector<std::string>>("probeFilters");
23  for (auto& probeFilter : probeFilters) {
24  probeHists_.emplace_back(ProbeData(std::move(probeFilter)));
25  }
26 }
27 
30  desc.addVPSet(
31  "tagCuts", VarRangeCut<trigger::TriggerObject>::makePSetDescription(), std::vector<edm::ParameterSet>());
32  desc.addVPSet(
33  "probeCuts", VarRangeCut<trigger::TriggerObject>::makePSetDescription(), std::vector<edm::ParameterSet>());
35  desc.add<std::string>("collName", "stdTag");
36  desc.add<std::string>("folderName", "HLT/EGM/TrigObjTnP");
38  desc.add<std::vector<std::string>>("probeFilters", std::vector<std::string>());
40  return desc;
41 }
42 
45  for (auto& probe : probeHists_) {
46  probe.bookHists(collName_, iBooker, histDefs_);
47  }
48 }
49 
51  const edm::TriggerResults& trigResults,
52  const edm::TriggerNames& trigNames) const {
53  if (evtTrigSel_(trigResults, trigNames) == false)
54  return;
55 
56  auto tagTrigKeys = tagFilters_.getPassingKeys(trigEvt);
57  for (auto& tagKey : tagTrigKeys) {
58  const trigger::TriggerObject& tagObj = trigEvt.getObjects()[tagKey];
59  if (tagCuts_(tagObj)) {
60  for (auto& probeColl : probeHists_)
61  probeColl.fill(tagKey, trigEvt, probeCuts_);
62  }
63  }
64 }
65 
66 //trigger::Keys is likely a vector containing 0-3 short ints (probably 10 max),
67 // passing by value makes this much easier code wise (otherwise would have to
68 //create a dummy empty vector) and shouldnt be too much of a performance hit
70  edm::InputTag filterTag(filterName, "", trigEvt.usedProcessName());
71  trigger::size_type filterIndex = trigEvt.filterIndex(filterTag);
72  if (filterIndex < trigEvt.sizeFilters())
73  return trigEvt.filterKeys(filterIndex);
74  else
75  return trigger::Keys();
76 }
77 
79  : isAND_(config.getParameter<bool>("isAND")) {
80  auto filterSetConfigs = config.getParameter<std::vector<edm::ParameterSet>>("filterSets");
81  for (auto& filterSetConfig : filterSetConfigs)
82  filterSets_.emplace_back(FilterSet(filterSetConfig));
83 }
84 
87  desc.addVPSet("filterSets", FilterSet::makePSetDescription(), std::vector<edm::ParameterSet>());
88  desc.add<bool>("isAND", false);
89  return desc;
90 }
91 
93  trigger::Keys passingKeys;
94  bool isFirstFilterSet = true;
95  for (const auto& filterSet : filterSets_) {
96  auto keysOfFilterSet = filterSet.getPassingKeys(trigEvt);
97  if (isFirstFilterSet)
98  passingKeys = keysOfFilterSet;
99  else
100  mergeTrigKeys(passingKeys, keysOfFilterSet, isAND_);
101  isFirstFilterSet = false;
102  }
103  cleanTrigKeys(passingKeys);
104  return passingKeys;
105 }
106 
108  const trigger::Keys& keysToMerge,
109  bool isAND) {
110  if (isAND) {
111  for (auto& key : keys) {
112  if (std::count(keysToMerge.begin(), keysToMerge.end(), key) == 0) {
114  }
115  }
116  } else {
117  for (const auto key : keysToMerge) {
118  keys.push_back(key);
119  }
120  }
121 }
122 
124  std::sort(keys.begin(), keys.end());
125  std::unique(keys.begin(), keys.end());
126  while (!keys.empty() && keys.back() == std::numeric_limits<trigger::size_type>::max()) {
127  keys.pop_back();
128  }
129 }
130 
132  : filters_(config.getParameter<std::vector<std::string>>("filters")), isAND_(config.getParameter<bool>("isAND")) {}
133 
135  : selectionStr_(config.getParameter<std::string>("selectionStr")),
136  isANDForExpandedPaths_(config.getParameter<bool>("isANDForExpandedPaths")),
137  verbose_(config.getParameter<int>("verbose")),
138  isInited_(false) {}
139 
142  desc.add<std::string>("selectionStr", std::string(""));
143  desc.add<bool>("isANDForExpandedPaths", false);
144  desc.add<int>("verbose", 1);
145  return desc;
146 }
147 
149  expandedSelStr_ = expandSelectionStr(selectionStr_, hltConfig, isANDForExpandedPaths_, verbose_);
150  isInited_ = true;
151  if (verbose_ > 1) {
152  edm::LogInfo("TrigObjTnPHistColl::PathSelector") << "trigger selection string: \"" << expandedSelStr_ << "\"";
153  }
154 }
155 
157  const edm::TriggerNames& trigNames) const {
158  if (selectionStr_.empty())
159  return true; //didnt specify any selection, default to pass
160  else if (!isInited_) {
161  edm::LogError("TrigObjTnPHistColl")
162  << " error, TrigObjTnPHistColl::PathSelector is not initalised, returning false ";
163  return false;
164  } else if (expandedSelStr_.empty()) {
165  //there was a problem parsing the expression, it was logged at the start, no need to do each run
166  return false;
167  } else {
168  //as of 20/08/18, there is a bug in L1GtLogicParser, it must take a non-const std::string
169  //as input because it overloads the constructor between const and non-const std::string
170  //for like no reason. And the const version is broken, you have to use non-const
171  //hence we make a non-const copy of the selection string
172  std::string selStr = expandedSelStr_;
173  L1GtLogicParser logicParser(selStr);
174  for (auto& token : logicParser.operandTokenVector()) {
175  const std::string& pathName = token.tokenName;
176  auto pathIndex = trigNames.triggerIndex(pathName);
177  bool accept = pathIndex < trigNames.size() ? trigResults.accept(pathIndex) : false;
178  token.tokenResult = accept;
179  }
180  return logicParser.expressionResult();
181  }
182 }
183 
184 //a port of https://github.com/cms-sw/cmssw/blob/51eb73f59e2016d54618e2a8e19abab84fe33b47/CommonTools/TriggerUtils/src/GenericTriggerEventFlag.cc#L225-L238
187  bool isAND,
188  int verbose) {
189  std::string expandedSelStr(selStr);
190  //it is very important to pass in as a non-const std::string, see comments else where
191  L1GtLogicParser logicParser(expandedSelStr);
192  for (const auto& token : logicParser.operandTokenVector()) {
193  const std::string& pathName = token.tokenName;
194  if (pathName.find('*') != std::string::npos) {
195  std::string pathPatternExpanded = expandPath(pathName, hltConfig, isAND, verbose);
196  expandedSelStr.replace(expandedSelStr.find(pathName), pathName.size(), pathPatternExpanded);
197  }
198  }
199  return expandedSelStr;
200 }
201 
202 //a port of GenericTriggerEventFlag::expandLogicalExpression
203 //https://github.com/cms-sw/cmssw/blob/51eb73f59e2016d54618e2a8e19abab84fe33b47/CommonTools/TriggerUtils/src/GenericTriggerEventFlag.cc#L600-L632
206  bool isAND,
207  int verbose) {
208  // Find matching entries in the menu
209  const std::vector<std::string>& trigNames = hltConfig.triggerNames();
210  std::vector<std::string> matched;
211  const std::string versionWildcard("_v*");
212  if (pathPattern.substr(pathPattern.size() - versionWildcard.size()) == versionWildcard) {
213  const std::string pathPatternBase(pathPattern.substr(0, pathPattern.size() - versionWildcard.size()));
214  matched = hltConfig.restoreVersion(trigNames, pathPatternBase);
215  } else {
216  matched = hltConfig.matched(trigNames, pathPattern);
217  }
218 
219  if (matched.empty()) {
220  if (verbose >= 1)
221  edm::LogWarning("TrigObjTnPHistColl::PathSelector")
222  << "pattern: \"" << pathPattern
223  << "\" could not be resolved, please check your triggers are spelt correctly and present in the data you are "
224  "running over";
225  return "";
226  }
227 
228  // Compose logical expression
229  std::string expanded("(");
230  for (unsigned iVers = 0; iVers < matched.size(); ++iVers) {
231  if (iVers > 0)
232  expanded.append(isAND ? " AND " : " OR ");
233  expanded.append(matched.at(iVers));
234  }
235  expanded.append(")");
236  if (verbose > 1) {
237  edm::LogInfo("TrigObjTnPHistColl::PathSelector") << "Logical expression : \"" << pathPattern << "\"\n"
238  << " expanded to: \"" << expanded << "\"";
239  }
240  return expanded;
241 }
242 
245  desc.add<std::vector<std::string>>("filters", std::vector<std::string>());
246  desc.add<bool>("isAND", true);
247  return desc;
248 }
249 
251  const trigger::TriggerEvent& trigEvt) const {
252  trigger::Keys passingKeys;
253  bool firstFilter = true;
254  for (const auto& filterName : filters_) {
255  const trigger::Keys& trigKeys = getKeys(trigEvt, filterName);
256  if (firstFilter) {
257  passingKeys = trigKeys;
258  firstFilter = false;
259  } else
260  mergeTrigKeys(passingKeys, trigKeys, isAND_);
261  }
262  cleanTrigKeys(passingKeys);
263 
264  return passingKeys;
265 }
266 
268  //first look for "Abs" at the end of the string
269  auto absPos = varName.rfind("Abs");
270  if (absPos != std::string::npos && absPos + 3 == varName.size()) {
271  isAbs_ = true;
272  varName = varName.erase(absPos);
273  }
274  if (varName == "pt")
276  else if (varName == "eta")
278  else if (varName == "phi")
280  else {
281  std::ostringstream msg;
282  msg << "var " << varName << " not recognised (use pt or p rather than et or e for speed!) ";
283  if (isAbs_)
284  msg << " varName was \"Abs\" suffex cleaned where it tried to remove \"Abs\" at the end of the variable name ";
285  msg << __FILE__ << "," << __LINE__ << std::endl;
286  throw cms::Exception("ConfigError") << msg.str();
287  }
288 }
289 
291  : localCuts_(config.getParameter<std::vector<edm::ParameterSet>>("localCuts")),
292  var_(config.getParameter<std::string>("var")) {}
293 
297  desc.add<std::string>("var", "pt");
298  return desc;
299 }
300 
302  float mass,
304  if (localCuts_(probe))
305  hist->Fill(var_(probe), mass);
306 }
307 
309  : massBins_(convertToFloat(config.getParameter<std::vector<double>>("massBins"))) {
310  const auto histConfigs = config.getParameter<std::vector<edm::ParameterSet>>("configs");
311  for (const auto& histConfig : histConfigs) {
312  histData_.emplace_back(Data(histConfig));
313  }
314 }
315 
318  desc.addVPSet("configs", Data::makePSetDescription(), std::vector<edm::ParameterSet>());
319  std::vector<double> massBins;
320  for (float mass = 60; mass <= 120; mass += 1)
321  massBins.push_back(mass);
322  desc.add<std::vector<double>>("massBins", massBins);
323  return desc;
324 }
325 
326 std::vector<std::pair<TrigObjTnPHistColl::HistFiller, dqm::reco::MonitorElement*>>
328  const std::string& name,
329  const std::string& title) const {
330  std::vector<std::pair<HistFiller, dqm::reco::MonitorElement*>> hists;
331  hists.reserve(histData_.size());
332  for (const auto& data : histData_) {
333  hists.push_back({data.filler(), data.book(iBooker, name, title, massBins_)});
334  }
335  return hists;
336 }
337 
339  : histFiller_(config.getParameter<edm::ParameterSet>("filler")),
340  bins_(convertToFloat(config.getParameter<std::vector<double>>("bins"))),
341  nameSuffex_(config.getParameter<std::string>("nameSuffex")),
342  titleSuffex_(config.getParameter<std::string>("titleSuffex")) {}
343 
347  desc.add<std::vector<double>>("bins", {-2.5, -1.5, 0, 1.5, 2.5});
348  desc.add<std::string>("nameSuffex", "_eta");
349  desc.add<std::string>("titleSuffex", ";#eta;mass [GeV]");
350  return desc;
351 }
352 
354  const std::string& name,
355  const std::string& title,
356  const std::vector<float>& massBins) const {
357  return iBooker.book2D((name + nameSuffex_).c_str(),
358  (title + titleSuffex_).c_str(),
359  bins_.size() - 1,
360  bins_.data(),
361  massBins.size() - 1,
362  massBins.data());
363 }
364 
366  const std::string& name,
367  const std::string& title,
368  const HistDefs& histDefs) {
369  hists_ = histDefs.bookHists(iBooker, name, title);
370 }
371 
373  for (auto& hist : hists_) {
374  hist.first(probe, mass, hist.second);
375  }
376 }
377 
379  DQMStore::IBooker& iBooker,
380  const HistDefs& histDefs) {
381  hists_.bookHists(iBooker, tagName + "_" + probeFilter_, tagName + "_" + probeFilter_, histDefs);
382 }
383 
385  const trigger::TriggerEvent& trigEvt,
387  auto probeKeys = getKeys(trigEvt, probeFilter_);
388  for (auto probeKey : probeKeys) {
389  const trigger::TriggerObject& probe = trigEvt.getObjects()[probeKey];
390  if (tagKey != probeKey && probeCuts(probe)) {
391  const trigger::TriggerObject& tag = trigEvt.getObjects()[tagKey];
392  auto massFunc = [](float pt1, float eta1, float phi1, float pt2, float eta2, float phi2) {
393  return std::sqrt(2 * pt1 * pt2 * (std::cosh(eta1 - eta2) - std::cos(phi1 - phi2)));
394  };
395  float mass = massFunc(tag.pt(), tag.eta(), tag.phi(), probe.pt(), probe.eta(), probe.phi());
396  hists_.fill(probe, mass);
397  }
398  }
399 }
bool accept() const
Has at least one path accepted the event?
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:119
static void cleanTrigKeys(trigger::Keys &keys)
bool operator()(const edm::TriggerResults &trigResults, const edm::TriggerNames &trigNames) const
The single EDProduct to be saved for each event (AOD case)
Definition: TriggerEvent.h:26
static edm::ParameterSetDescription makePSetDescription()
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
static edm::ParameterSetDescription makePSetDescription()
static const trigger::Keys getKeys(const trigger::TriggerEvent &trigEvt, const std::string &filterName)
std::vector< ProbeData > probeHists_
bool verbose
TrigObjTnPHistColl(const edm::ParameterSet &config)
void bookHists(DQMStore::IBooker &iBooker)
trigger::size_type sizeFilters() const
Definition: TriggerEvent.h:147
static edm::ParameterSetDescription makePSetDescription()
Data(const edm::ParameterSet &config)
FilterSet(const edm::ParameterSet &config)
Definition: config.py:1
Log< level::Error, false > LogError
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
uint16_t size_type
static std::string expandPath(const std::string &pathPattern, const HLTConfigProvider &hltConfig, bool isAND, int verbose)
VarRangeCutColl< trigger::TriggerObject > probeCuts_
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
void bookHists(const std::string &tagName, DQMStore::IBooker &iBooker, const HistDefs &histDefs)
static edm::ParameterSetDescription makePSetDescription()
void init(const HLTConfigProvider &hltConfig)
void fill(const trigger::size_type tagKey, const trigger::TriggerEvent &trigEvt, const VarRangeCutColl< trigger::TriggerObject > &probeCuts) const
std::vector< OperandToken > & operandTokenVector()
return the vector of operand tokens
T sqrt(T t)
Definition: SSEVec.h:23
def unique(seq, keepstr=True)
Definition: tier0.py:24
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
dqm::reco::MonitorElement * book(DQMStore::IBooker &iBooker, const std::string &name, const std::string &title, const std::vector< float > &massBins) const
const trigger::Keys getPassingKeys(const trigger::TriggerEvent &trigEvt) const
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:102
key
prepare the HTCondor submission files and eventually submit them
void bookHists(DQMStore::IBooker &iBooker, const std::string &name, const std::string &title, const HistDefs &histDefs)
float(trigger::TriggerObject::* varFunc_)() const
static void mergeTrigKeys(trigger::Keys &keys, const trigger::Keys &keysToMerge, bool isAND)
Log< level::Info, false > LogInfo
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:57
void fill(const trigger::TriggerObject &probe, float mass) const
HistFiller(const edm::ParameterSet &config)
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:133
PathSelector(const edm::ParameterSet &config)
static std::string expandSelectionStr(const std::string &selStr, const HLTConfigProvider &hltConfig, bool isAND, int verbose)
std::vector< size_type > Keys
void fill(const trigger::TriggerEvent &trigEvt, const edm::TriggerResults &trigResults, const edm::TriggerNames &trigNames) const
const trigger::Keys getPassingKeys(const trigger::TriggerEvent &trigEvt) const
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:221
tuple msg
Definition: mps_check.py:286
HistDefs(const edm::ParameterSet &config)
std::vector< std::pair< HistFiller, dqm::reco::MonitorElement * > > bookHists(DQMStore::IBooker &iBooker, const std::string &name, const std::string &title) const
std::vector< FilterSet > filterSets_
VarRangeCutColl< trigger::TriggerObject > tagCuts_
const std::string & usedProcessName() const
getters
Definition: TriggerEvent.h:99
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
void operator()(const trigger::TriggerObject &probe, float mass, dqm::reco::MonitorElement *hist) const
static edm::ParameterSetDescription makePSetDescription()
FilterSelector(const edm::ParameterSet &config)
static edm::ParameterSetDescription makePSetDescription()
Log< level::Warning, false > LogWarning
FilterSelector tagFilters_
virtual const bool expressionResult() const
def move(src, dest)
Definition: eostools.py:511
static edm::ParameterSetDescription makePSetDescription()