Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-02 23:44:45

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   // # use HLTPathStatus results
0044   desc.add<bool>("usePathStatus", false)
0045       ->setComment("Read the HLT results from the TriggerResults (false) or from the current job's PathStatus (true).");
0046   // # HLT results   - set to empty to ignore HLT
0047   desc.add<edm::InputTag>("hltResults", edm::InputTag("TriggerResults"));
0048   // # L1 uGT results - set to empty to ignore L1T
0049   desc.add<edm::InputTag>("l1tResults", edm::InputTag("hltGtStage2Digis"));
0050   // # use initial L1 decision, before masks and prescales
0051   desc.add<bool>("l1tIgnoreMaskAndPrescale", false);
0052   // # OBSOLETE - these parameters are ignored, they are left only not to break old configurations
0053   // they will not be printed in the generated cfi.py file
0054   desc.addOptionalNode(edm::ParameterDescription<bool>("l1tIgnoreMask", false, true), false)
0055       ->setComment("This parameter is obsolete and will be ignored.");
0056   desc.addOptionalNode(edm::ParameterDescription<bool>("l1techIgnorePrescales", false, true), false)
0057       ->setComment("This parameter is obsolete and will be ignored.");
0058   desc.addOptionalNode(edm::ParameterDescription<unsigned int>("daqPartitions", 0x01, true), false)
0059       ->setComment("This parameter is obsolete and will be ignored.");
0060   // # throw exception on unknown trigger names
0061   desc.add<bool>("throw", true);
0062   // # read paths from AlCaRecoTriggerBitsRcd via this key
0063   desc.add<std::string>("eventSetupPathsKey", "");
0064   descriptions.add("triggerResultsFilterFromDB", desc);
0065 }
0066 
0067 void TriggerResultsFilterFromDB::parse(const std::vector<std::string>& expressions) {
0068   // parse the logical expressions into functionals
0069   if (expressions.empty()) {
0070     edm::LogWarning("Configuration") << "Empty trigger results expression. Will substitute with *";
0071     m_expression = triggerExpression::parse("*");
0072   } else if (expressions.size() == 1) {
0073     parse(expressions[0]);
0074   } else {
0075     std::stringstream expression;
0076     expression << "(" << expressions[0] << ")";
0077     for (unsigned int i = 1; i < expressions.size(); ++i)
0078       expression << " OR (" << expressions[i] << ")";
0079     parse(expression.str());
0080   }
0081 }
0082 
0083 void TriggerResultsFilterFromDB::parse(const std::string& expression) {
0084   // parse the logical expressions into functionals
0085   m_expression = triggerExpression::parse(expression);
0086 
0087   // check if the expressions were parsed correctly
0088   if (not m_expression)
0089     edm::LogWarning("Configuration") << "Couldn't parse trigger results expression \"" << expression << "\"";
0090 }
0091 
0092 // read the triggerConditions from the database
0093 void TriggerResultsFilterFromDB::pathsFromSetup(const edm::Event& event, const edm::EventSetup& setup) {
0094   // Get map of strings to concatenated list of names of HLT paths from EventSetup:
0095   auto const& triggerBits = setup.getHandle(m_alcaRecoTriggerBitsRcdToken);
0096 
0097   typedef std::map<std::string, std::string> TriggerMap;
0098   const TriggerMap& triggerMap = triggerBits->m_alcarecoToTrig;
0099 
0100   auto listIter = triggerMap.find(m_eventSetupPathsKey);
0101   if (listIter == triggerMap.end()) {
0102     throw cms::Exception("Configuration")
0103         << "TriggerResultsFilterFromDB [instance: " << moduleDescription().moduleLabel()
0104         << "]: No triggerList with key " << m_eventSetupPathsKey << " in AlCaRecoTriggerBitsRcd";
0105   }
0106 
0107   // avoid a map<string, vector<string>> in DB for performance reason,
0108   // the paths are mapped into one string that we have to decompose:
0109   parse(triggerBits->decompose(listIter->second));
0110 }
0111 
0112 bool TriggerResultsFilterFromDB::filter(edm::Event& event, const edm::EventSetup& setup) {
0113   // if the IOV has changed, re-read the triggerConditions from the database
0114   if (m_eventSetupWatcher.check(setup))
0115     pathsFromSetup(event, setup);
0116 
0117   if (not m_expression)
0118     // no valid expression has been parsed
0119     return false;
0120 
0121   if (not m_eventCache.setEvent(event, setup))
0122     // couldn't properly access all information from the Event
0123     return false;
0124 
0125   // if the L1 or HLT configurations have changed, (re)initialize the filters (including during the first event)
0126   if (m_eventCache.configurationUpdated()) {
0127     m_expression->init(m_eventCache);
0128 
0129     // log the expanded configuration
0130     edm::LogInfo("Configuration") << "TriggerResultsFilterFromDB configuration updated: " << *m_expression;
0131   }
0132 
0133   // run the trigger results filter
0134   return (*m_expression)(m_eventCache);
0135 }
0136 
0137 // register as framework plugin
0138 #include "FWCore/Framework/interface/MakerMacros.h"
0139 DEFINE_FWK_MODULE(TriggerResultsFilterFromDB);