CMS 3D CMS Logo

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