Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-25 23:59:52

0001 /** \class TriggerResultsFilterFromDB
0002  *
0003  * See header file for documentation
0004  *
0005  *
0006  *  Authors: Martin Grunewald, Andrea Bocci
0007  *
0008  */
0009 
0010 #include <vector>
0011 #include <string>
0012 #include <sstream>
0013 #include <iostream>
0014 #include <iomanip>
0015 
0016 #include "DataFormats/Common/interface/Handle.h"
0017 #include "DataFormats/Common/interface/TriggerResults.h"
0018 #include "CondFormats/DataRecord/interface/AlCaRecoTriggerBitsRcd.h"
0019 #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
0020 #include "FWCore/Framework/interface/ESHandle.h"
0021 #include "FWCore/Utilities/interface/Exception.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 
0025 #include "HLTrigger/HLTcore/interface/TriggerExpressionEvaluator.h"
0026 #include "HLTrigger/HLTcore/interface/TriggerExpressionParser.h"
0027 #include "TriggerResultsFilterFromDB.h"
0028 
0029 //
0030 // constructors and destructor
0031 //
0032 TriggerResultsFilterFromDB::TriggerResultsFilterFromDB(const edm::ParameterSet& config)
0033     : m_eventSetupPathsKey(config.getParameter<std::string>("eventSetupPathsKey")),
0034       m_eventSetupWatcher(),
0035       m_alcaRecoTriggerBitsRcdToken(esConsumes()),
0036       m_expression(nullptr),
0037       m_eventCache(config, consumesCollector()) {}
0038 
0039 TriggerResultsFilterFromDB::~TriggerResultsFilterFromDB() { delete m_expression; }
0040 
0041 void TriggerResultsFilterFromDB::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0042   edm::ParameterSetDescription desc;
0043   // # HLT results   - set to empty to ignore HLT
0044   desc.add<edm::InputTag>("hltResults", edm::InputTag("TriggerResults"));
0045   // # L1 uGT results - set to empty to ignore L1T
0046   desc.add<edm::InputTag>("l1tResults", edm::InputTag("hltGtStage2Digis"));
0047   // # use initial L1 decision, before masks and prescales
0048   desc.add<bool>("l1tIgnoreMaskAndPrescale", false);
0049   // # OBSOLETE - these parameters are ignored, they are left only not to break old configurations
0050   // they will not be printed in the generated cfi.py file
0051   desc.addOptionalNode(edm::ParameterDescription<bool>("l1tIgnoreMask", false, true), false)
0052       ->setComment("This parameter is obsolete and will be ignored.");
0053   desc.addOptionalNode(edm::ParameterDescription<bool>("l1techIgnorePrescales", false, true), false)
0054       ->setComment("This parameter is obsolete and will be ignored.");
0055   desc.addOptionalNode(edm::ParameterDescription<unsigned int>("daqPartitions", 0x01, true), false)
0056       ->setComment("This parameter is obsolete and will be ignored.");
0057   // # throw exception on unknown trigger names
0058   desc.add<bool>("throw", true);
0059   // # read paths from AlCaRecoTriggerBitsRcd via this key
0060   desc.add<std::string>("eventSetupPathsKey", "");
0061   descriptions.add("triggerResultsFilterFromDB", desc);
0062 }
0063 
0064 void TriggerResultsFilterFromDB::parse(const std::vector<std::string>& expressions) {
0065   // parse the logical expressions into functionals
0066   if (expressions.empty()) {
0067     edm::LogWarning("Configuration") << "Empty trigger results expression";
0068   } else if (expressions.size() == 1) {
0069     parse(expressions[0]);
0070   } else {
0071     std::stringstream expression;
0072     expression << "(" << expressions[0] << ")";
0073     for (unsigned int i = 1; i < expressions.size(); ++i)
0074       expression << " OR (" << expressions[i] << ")";
0075     parse(expression.str());
0076   }
0077 }
0078 
0079 void TriggerResultsFilterFromDB::parse(const std::string& expression) {
0080   // parse the logical expressions into functionals
0081   m_expression = triggerExpression::parse(expression);
0082 
0083   // check if the expressions were parsed correctly
0084   if (not m_expression)
0085     edm::LogWarning("Configuration") << "Couldn't parse trigger results expression \"" << expression << "\"";
0086 }
0087 
0088 // read the triggerConditions from the database
0089 void TriggerResultsFilterFromDB::pathsFromSetup(const edm::Event& event, const edm::EventSetup& setup) {
0090   // Get map of strings to concatenated list of names of HLT paths from EventSetup:
0091   auto const& triggerBits = setup.getHandle(m_alcaRecoTriggerBitsRcdToken);
0092 
0093   typedef std::map<std::string, std::string> TriggerMap;
0094   const TriggerMap& triggerMap = triggerBits->m_alcarecoToTrig;
0095 
0096   auto listIter = triggerMap.find(m_eventSetupPathsKey);
0097   if (listIter == triggerMap.end()) {
0098     throw cms::Exception("Configuration")
0099         << "TriggerResultsFilterFromDB [instance: " << moduleDescription().moduleLabel()
0100         << "]: No triggerList with key " << m_eventSetupPathsKey << " in AlCaRecoTriggerBitsRcd";
0101   }
0102 
0103   // avoid a map<string, vector<string>> in DB for performance reason,
0104   // the paths are mapped into one string that we have to decompose:
0105   parse(triggerBits->decompose(listIter->second));
0106 }
0107 
0108 bool TriggerResultsFilterFromDB::filter(edm::Event& event, const edm::EventSetup& setup) {
0109   // if the IOV has changed, re-read the triggerConditions from the database
0110   if (m_eventSetupWatcher.check(setup))
0111     pathsFromSetup(event, setup);
0112 
0113   if (not m_expression)
0114     // no valid expression has been parsed
0115     return false;
0116 
0117   if (not m_eventCache.setEvent(event, setup))
0118     // couldn't properly access all information from the Event
0119     return false;
0120 
0121   // if the L1 or HLT configurations have changed, (re)initialize the filters (including during the first event)
0122   if (m_eventCache.configurationUpdated()) {
0123     m_expression->init(m_eventCache);
0124 
0125     // log the expanded configuration
0126     edm::LogInfo("Configuration") << "TriggerResultsFilterFromDB configuration updated: " << *m_expression;
0127   }
0128 
0129   // run the trigger results filter
0130   return (*m_expression)(m_eventCache);
0131 }
0132 
0133 // register as framework plugin
0134 #include "FWCore/Framework/interface/MakerMacros.h"
0135 DEFINE_FWK_MODULE(TriggerResultsFilterFromDB);