Skip to content

Commit

Permalink
allow TriggerResultsFilter to throw for invalid HLTPathStatus patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
missirol committed Aug 23, 2022
1 parent e18b430 commit e355f21
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 243 deletions.
5 changes: 5 additions & 0 deletions HLTrigger/HLTcore/src/TriggerExpressionData.cc
Expand Up @@ -48,11 +48,16 @@ namespace triggerExpression {
if (usePathStatus()) {
m_pathStatus.clear();
std::vector<std::string> triggerNames;
m_pathStatus.reserve(m_pathStatusTokens.size());
triggerNames.reserve(m_pathStatusTokens.size());
for (auto const& p : m_pathStatusTokens) {
auto const& handle = event.getHandle(p.second);
if (handle.isValid()) {
m_pathStatus.push_back(handle->accept());
triggerNames.push_back(p.first);
} else {
edm::LogError("MissingHLTPathStatus")
<< "invalid handle for requested edm::HLTPathStatus with label \"" << p.first << "\"";
}
}
m_hltUpdated = m_triggerNames != triggerNames;
Expand Down
69 changes: 47 additions & 22 deletions HLTrigger/HLTfilters/plugins/TriggerResultsFilter.cc
Expand Up @@ -10,17 +10,15 @@
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <regex>
#include <vector>
#include <algorithm>

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/TriggerResults.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/RegexMatch.h"
#include "FWCore/Utilities/interface/transform.h"
#include "HLTrigger/HLTcore/interface/TriggerExpressionEvaluator.h"
#include "HLTrigger/HLTcore/interface/TriggerExpressionParser.h"

Expand All @@ -34,30 +32,52 @@ TriggerResultsFilter::TriggerResultsFilter(const edm::ParameterSet& config)
std::vector<std::string> const& expressions = config.getParameter<std::vector<std::string>>("triggerConditions");
parse(expressions);
if (m_expression and m_eventCache.usePathStatus()) {
// if the expression was succesfully parsed, join all the patterns corresponding
// to the CMSSW paths in the logical expression into a single regex
std::vector<std::string> patterns = m_expression->patterns();
if (patterns.empty()) {
// if the expression was successfully parsed, store the list of patterns,
// each in both std::string and std::regex format
// and with a flag for having valid matches
hltPathStatusPatterns_ = edm::vector_transform(m_expression->patterns(), [](std::string const& pattern) {
return PatternData(pattern, std::regex(edm::glob2reg(pattern), std::regex::extended));
});

if (hltPathStatusPatterns_.empty()) {
return;
}
std::string str;
for (auto const& pattern : patterns) {
str += edm::glob2reg(pattern);
str += '|';
}
str.pop_back();
std::regex regex(str, std::regex::extended);

// consume all matching paths
callWhenNewProductsRegistered([this, regex](edm::BranchDescription const& branch) {
if (branch.branchType() == edm::InEvent and branch.className() == "edm::HLTPathStatus" and
std::regex_match(branch.moduleLabel(), regex)) {
m_eventCache.setPathStatusToken(branch, consumesCollector());
callWhenNewProductsRegistered([this](edm::BranchDescription const& branch) {
if (branch.branchType() == edm::InEvent and branch.className() == "edm::HLTPathStatus") {
bool consumeBranch = true;
for (auto& pattern : hltPathStatusPatterns_) {
if (std::regex_match(branch.moduleLabel(), pattern.regex)) {
pattern.matched = true;
if (consumeBranch) {
consumeBranch = false;
m_eventCache.setPathStatusToken(branch, consumesCollector());
}
}
}
}
});
}
}

void TriggerResultsFilter::beginStream(edm::StreamID) {
// if throw=True, check if any of the input patterns had zero matches (and if so, throw an exception)
if (not hltPathStatusPatterns_.empty() and m_eventCache.shouldThrow()) {
auto unmatchedPatternsExist = std::any_of(
hltPathStatusPatterns_.cbegin(), hltPathStatusPatterns_.cend(), [](auto foo) { return (not foo.matched); });
if (unmatchedPatternsExist) {
cms::Exception excpt("UnmatchedPatterns");
excpt << "the parameter \"triggerConditions\" contains patterns with zero matches"
<< " for the available edm::HLTPathStatus collections - invalid patterns are:";
for (auto const& pattern : hltPathStatusPatterns_)
if (not pattern.matched)
excpt << "\n\t" << pattern.str;
throw excpt;
}
}
}

void TriggerResultsFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
// # use HLTPathStatus results
Expand Down Expand Up @@ -107,8 +127,13 @@ void TriggerResultsFilter::parse(const std::string& expression) {
m_expression.reset(triggerExpression::parse(expression));

// check if the expressions were parsed correctly
if (not m_expression)
edm::LogWarning("Configuration") << "Couldn't parse trigger results expression \"" << expression << "\"";
if (not m_expression) {
if (m_eventCache.shouldThrow()) {
throw cms::Exception("Configuration") << "Couldn't parse trigger-results expression \"" << expression << "\"";
} else {
edm::LogWarning("Configuration") << "Couldn't parse trigger-results expression \"" << expression << "\"";
}
}
}

bool TriggerResultsFilter::filter(edm::Event& event, const edm::EventSetup& setup) {
Expand Down
14 changes: 13 additions & 1 deletion HLTrigger/HLTfilters/plugins/TriggerResultsFilter.h
Expand Up @@ -17,12 +17,12 @@
#include <memory>
#include <string>
#include <vector>
#include <regex>

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDFilter.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "HLTrigger/HLTcore/interface/TriggerExpressionData.h"

// forward declaration
Expand All @@ -45,6 +45,8 @@ class TriggerResultsFilter : public edm::stream::EDFilter<> {
bool filter(edm::Event &, const edm::EventSetup &) override;

private:
void beginStream(edm::StreamID) override;

/// parse the logical expression into functionals
void parse(const std::string &expression);
void parse(const std::vector<std::string> &expressions);
Expand All @@ -54,6 +56,16 @@ class TriggerResultsFilter : public edm::stream::EDFilter<> {

/// cache some data from the Event for faster access by the m_expression
triggerExpression::Data m_eventCache;

struct PatternData {
PatternData(std::string const &aStr, std::regex const &aRegex, bool const hasMatch = false)
: str(aStr), regex(aRegex), matched(hasMatch) {}
std::string str;
std::regex regex;
bool matched;
};

std::vector<PatternData> hltPathStatusPatterns_;
};

#endif //TriggerResultsFilter_h

0 comments on commit e355f21

Please sign in to comment.