Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:29

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";
0071   } else if (expressions.size() == 1) {
0072     parse(expressions[0]);
0073   } else {
0074     std::stringstream expression;
0075     expression << "(" << expressions[0] << ")";
0076     for (unsigned int i = 1; i < expressions.size(); ++i)
0077       expression << " OR (" << expressions[i] << ")";
0078     parse(expression.str());
0079   }
0080 }
0081 
0082 void TriggerResultsFilterFromDB::parse(const std::string& expression) {
0083   // parse the logical expressions into functionals
0084   m_expression = triggerExpression::parse(expression);
0085 
0086   // check if the expressions were parsed correctly
0087   if (not m_expression)
0088     edm::LogWarning("Configuration") << "Couldn't parse trigger results expression \"" << expression << "\"";
0089 }
0090 
0091 // read the triggerConditions from the database
0092 void TriggerResultsFilterFromDB::pathsFromSetup(const edm::Event& event, const edm::EventSetup& setup) {
0093   // Get map of strings to concatenated list of names of HLT paths from EventSetup:
0094   auto const& triggerBits = setup.getHandle(m_alcaRecoTriggerBitsRcdToken);
0095 
0096   typedef std::map<std::string, std::string> TriggerMap;
0097   const TriggerMap& triggerMap = triggerBits->m_alcarecoToTrig;
0098 
0099   auto listIter = triggerMap.find(m_eventSetupPathsKey);
0100   if (listIter == triggerMap.end()) {
0101     throw cms::Exception("Configuration")
0102         << "TriggerResultsFilterFromDB [instance: " << moduleDescription().moduleLabel()
0103         << "]: No triggerList with key " << m_eventSetupPathsKey << " in AlCaRecoTriggerBitsRcd";
0104   }
0105 
0106   // avoid a map<string, vector<string>> in DB for performance reason,
0107   // the paths are mapped into one string that we have to decompose:
0108   parse(triggerBits->decompose(listIter->second));
0109 }
0110 
0111 bool TriggerResultsFilterFromDB::filter(edm::Event& event, const edm::EventSetup& setup) {
0112   // if the IOV has changed, re-read the triggerConditions from the database
0113   if (m_eventSetupWatcher.check(setup))
0114     pathsFromSetup(event, setup);
0115 
0116   if (not m_expression)
0117     // no valid expression has been parsed
0118     return false;
0119 
0120   if (not m_eventCache.setEvent(event, setup))
0121     // couldn't properly access all information from the Event
0122     return false;
0123 
0124   // if the L1 or HLT configurations have changed, (re)initialize the filters (including during the first event)
0125   if (m_eventCache.configurationUpdated()) {
0126     m_expression->init(m_eventCache);
0127 
0128     // log the expanded configuration
0129     edm::LogInfo("Configuration") << "TriggerResultsFilterFromDB configuration updated: " << *m_expression;
0130   }
0131 
0132   // run the trigger results filter
0133   return (*m_expression)(m_eventCache);
0134 }
0135 
0136 // register as framework plugin
0137 #include "FWCore/Framework/interface/MakerMacros.h"
0138 DEFINE_FWK_MODULE(TriggerResultsFilterFromDB);