From c7dfe931e668b2e339e80be0f66168c05bffa72b Mon Sep 17 00:00:00 2001 From: Marino Missiroli Date: Sun, 16 Apr 2023 16:09:29 +0200 Subject: [PATCH] improve implementation of HLTJetTag plugin template --- HLTrigger/btau/plugins/HLTJetTag.cc | 128 -------------------- HLTrigger/btau/plugins/HLTJetTag.h | 180 ++++++++++++++++++++++++---- HLTrigger/btau/plugins/modules.cc | 1 - 3 files changed, 157 insertions(+), 152 deletions(-) delete mode 100644 HLTrigger/btau/plugins/HLTJetTag.cc diff --git a/HLTrigger/btau/plugins/HLTJetTag.cc b/HLTrigger/btau/plugins/HLTJetTag.cc deleted file mode 100644 index 61f15b886dc4e..0000000000000 --- a/HLTrigger/btau/plugins/HLTJetTag.cc +++ /dev/null @@ -1,128 +0,0 @@ -/** \class HLTJetTag - * - * This class is an HLTFilter (a spcialized EDFilter) implementing - * tagged multi-jet trigger for b and tau. - * It should be run after the normal multi-jet trigger. - * - * - * \author Arnaud Gay, Ian Tomalin - * \maintainer Andrea Bocci - * - */ - -#include -#include - -#include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "DataFormats/Common/interface/Handle.h" -#include "DataFormats/Common/interface/RefToBase.h" -#include "DataFormats/JetReco/interface/CaloJetCollection.h" -#include "DataFormats/BTauReco/interface/JetTag.h" -#include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h" -#include "HLTrigger/HLTcore/interface/HLTFilter.h" -#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" -#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" -#include "HLTrigger/HLTcore/interface/defaultModuleLabel.h" - -#include "HLTJetTag.h" - -// -// constructors and destructor -// - -template -HLTJetTag::HLTJetTag(const edm::ParameterSet& config) - : HLTFilter(config), - m_Jets(config.getParameter("Jets")), - m_JetTags(config.getParameter("JetTags")), - m_MinTag(config.getParameter("MinTag")), - m_MaxTag(config.getParameter("MaxTag")), - m_MinJets(config.getParameter("MinJets")), - m_TriggerType(config.getParameter("TriggerType")) { - m_JetsToken = consumes>(m_Jets), m_JetTagsToken = consumes(m_JetTags), - - edm::LogInfo("") << " (HLTJetTag) trigger cuts: " << std::endl - << "\ttype of jets used: " << m_Jets.encode() << std::endl - << "\ttype of tagged jets used: " << m_JetTags.encode() << std::endl - << "\tmin/max tag value: [" << m_MinTag << ".." << m_MaxTag << "]" << std::endl - << "\tmin no. tagged jets: " << m_MinJets << "\tTriggerType: " << m_TriggerType << std::endl; -} - -template -HLTJetTag::~HLTJetTag() = default; - -template -void HLTJetTag::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - edm::ParameterSetDescription desc; - makeHLTFilterDescription(desc); - desc.add("Jets", edm::InputTag("hltJetCollection")); - desc.add("JetTags", edm::InputTag("hltJetTagCollection")); - desc.add("MinTag", 2.0); - desc.add("MaxTag", 999999.0); - desc.add("MinJets", 1); - desc.add("TriggerType", 0); - descriptions.add(defaultModuleLabel>(), desc); -} - -// -// member functions -// - -// ------------ method called to produce the data ------------ -template -bool HLTJetTag::hltFilter(edm::Event& event, - const edm::EventSetup& setup, - trigger::TriggerFilterObjectWithRefs& filterproduct) const { - using namespace std; - using namespace edm; - using namespace reco; - - typedef vector TCollection; - typedef Ref TRef; - - edm::Handle h_Jets; - event.getByToken(m_JetsToken, h_Jets); - if (saveTags()) - filterproduct.addCollectionTag(m_Jets); - - edm::Handle h_JetTags; - event.getByToken(m_JetTagsToken, h_JetTags); - - // check if the product this one depends on is available - auto const& handle = h_JetTags; - auto const& dependent = handle->keyProduct(); - if (not dependent.isNull() and not dependent.hasCache()) { - // only an empty AssociationVector can have a invalid dependent collection - edm::StableProvenance const& dependent_provenance = event.getStableProvenance(dependent.id()); - if (dependent_provenance.branchDescription().dropped()) - // FIXME the error message should be made prettier - throw edm::Exception(edm::errors::ProductNotFound) - << "Product " << handle.provenance()->branchName() << " requires product " - << dependent_provenance.branchName() << ", which has been dropped"; - } - - TRef jetRef; - - // Look at all jets in decreasing order of Et. - int nJet = 0; - int nTag = 0; - for (auto const& jet : *h_JetTags) { - jetRef = TRef(h_Jets, jet.first.key()); - LogTrace("") << "Jet " << nJet << " : Et = " << jet.first->et() << " , tag value = " << jet.second; - ++nJet; - // Check if jet is tagged. - if ((m_MinTag <= jet.second) and (jet.second <= m_MaxTag)) { - ++nTag; - - // Store a reference to the jets which passed tagging cuts - filterproduct.addObject(m_TriggerType, jetRef); - } - } - - // filter decision - bool accept = (nTag >= m_MinJets); - - edm::LogInfo("") << " trigger accept ? = " << accept << " nTag/nJet = " << nTag << "/" << nJet << std::endl; - - return accept; -} diff --git a/HLTrigger/btau/plugins/HLTJetTag.h b/HLTrigger/btau/plugins/HLTJetTag.h index 41422479ed5a1..cdaafc5dec382 100644 --- a/HLTrigger/btau/plugins/HLTJetTag.h +++ b/HLTrigger/btau/plugins/HLTJetTag.h @@ -3,7 +3,7 @@ /** \class HLTJetTag * - * This class is an HLTFilter (a spcialized EDFilter) implementing + * This class is an HLTFilter (a specialized EDFilter) implementing * tagged multi-jet trigger for b and tau. * It should be run after the normal multi-jet trigger. * @@ -13,39 +13,173 @@ * */ -#include - -#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "DataFormats/BTauReco/interface/JetTag.h" +#include "DataFormats/Common/interface/Ref.h" +#include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h" +#include "DataFormats/Math/interface/deltaR.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" +#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/Exception.h" #include "FWCore/Utilities/interface/InputTag.h" #include "HLTrigger/HLTcore/interface/HLTFilter.h" -#include "DataFormats/BTauReco/interface/JetTag.h" - -namespace edm { - class ConfigurationDescriptions; -} - -// -// class declaration -// template class HLTJetTag : public HLTFilter { public: - explicit HLTJetTag(const edm::ParameterSet& config); - ~HLTJetTag() override; + explicit HLTJetTag(edm::ParameterSet const& config); + ~HLTJetTag() override = default; + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + bool hltFilter(edm::Event& event, - const edm::EventSetup& setup, + edm::EventSetup const& setup, trigger::TriggerFilterObjectWithRefs& filterproduct) const override; private: - edm::InputTag m_Jets; // module label of input JetCollection - edm::EDGetTokenT > m_JetsToken; - edm::InputTag m_JetTags; // module label of input JetTagCollection - edm::EDGetTokenT m_JetTagsToken; - double m_MinTag, m_MaxTag; // tag descriminator cuts applied to each jet - int m_MinJets; // min. number of jets required to be tagged - int m_TriggerType; + auto logTrace() const { + auto const moduleName = moduleDescription().moduleName(); + return LogTrace(moduleName) << "[" << moduleName << "] "; + } + + bool findTagValueByMinDeltaR2(float& jetTagValue, + T const& jet, + reco::JetTagCollection const& jetTags, + float maxDeltaR2) const; + + edm::InputTag const m_Jets; // module label of input JetCollection + edm::EDGetTokenT> const m_JetsToken; + edm::InputTag const m_JetTags; // module label of input JetTagCollection + edm::EDGetTokenT const m_JetTagsToken; + double const m_MinTag, m_MaxTag; // tag discriminator cuts applied to each jet + int const m_MinJets; // min. number of jets required to be tagged + bool const m_MatchJetsByDeltaR; // flag to enable jet matching by minimum Delta-R distance + double const m_MaxJetDeltaR, m_MaxJetDeltaR2; // max delta-R distance used in jet matching + int const m_TriggerType; // type of trigger objects in output product }; +template +HLTJetTag::HLTJetTag(edm::ParameterSet const& config) + : HLTFilter(config), + m_Jets(config.getParameter("Jets")), + m_JetsToken(consumes(m_Jets)), + m_JetTags(config.getParameter("JetTags")), + m_JetTagsToken(consumes(m_JetTags)), + m_MinTag(config.getParameter("MinTag")), + m_MaxTag(config.getParameter("MaxTag")), + m_MinJets(config.getParameter("MinJets")), + m_MatchJetsByDeltaR(config.getParameter("MatchJetsByDeltaR")), + m_MaxJetDeltaR(config.getParameter("MaxJetDeltaR")), + m_MaxJetDeltaR2(m_MaxJetDeltaR * m_MaxJetDeltaR), + m_TriggerType(config.getParameter("TriggerType")) { + if (m_MatchJetsByDeltaR and m_MaxJetDeltaR < 0) { + throw cms::Exception("InvalidConfigurationParameter") + << "invalid value for parameter \"MaxJetDeltaR\" (must be >= 0): " << m_MaxJetDeltaR; + } + + logTrace() << "HLTFilter parameters:\n" + << "\ttype of jets used: " << m_Jets.encode() << "\n" + << "\ttype of tagged jets used: " << m_JetTags.encode() << "\n" + << "\tmin/max tag value: [" << m_MinTag << ", " << m_MaxTag << "]\n" + << "\tmin num. of tagged jets: " << m_MinJets << "\n" + << "\tmatch jets by Delta-R: " << m_MatchJetsByDeltaR << "\n" + << "\tmax Delta-R for jet matching: " << m_MaxJetDeltaR << "\n" + << "\tTriggerType: " << m_TriggerType << "\n"; +} + +template +void HLTJetTag::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + edm::ParameterSetDescription desc; + makeHLTFilterDescription(desc); + desc.add("Jets", edm::InputTag("hltJetCollection")); + desc.add("JetTags", edm::InputTag("hltJetTagCollection")); + desc.add("MinTag", 2.0); + desc.add("MaxTag", 999999.0); + desc.add("MinJets", 1); + desc.add("MatchJetsByDeltaR", false); + desc.add("MaxJetDeltaR", 0.1); + desc.add("TriggerType", 0); + descriptions.addWithDefaultLabel(desc); +} + +template +bool HLTJetTag::findTagValueByMinDeltaR2(float& jetTagValue, + T const& jet, + reco::JetTagCollection const& jetTags, + float maxDeltaR2) const { + bool ret = false; + for (auto const& jetTag : jetTags) { + auto const tmpDR2 = reco::deltaR2(jet, *(jetTag.first)); + if (tmpDR2 < maxDeltaR2) { + maxDeltaR2 = tmpDR2; + jetTagValue = jetTag.second; + ret = true; + } + } + + return ret; +} + +template +bool HLTJetTag::hltFilter(edm::Event& event, + edm::EventSetup const& setup, + trigger::TriggerFilterObjectWithRefs& filterproduct) const { + auto const h_Jets = event.getHandle(m_JetsToken); + if (saveTags()) + filterproduct.addCollectionTag(m_Jets); + + auto const h_JetTags = event.getHandle(m_JetTagsToken); + + // check if the product this one depends on is available + auto const& handle = h_JetTags; + auto const& dependent = handle->keyProduct(); + if (not dependent.isNull() and not dependent.hasCache()) { + // only an empty AssociationVector can have an invalid dependent collection + edm::StableProvenance const& dependent_provenance = event.getStableProvenance(dependent.id()); + if (dependent_provenance.branchDescription().dropped()) + // FIXME the error message should be made prettier + throw cms::Exception("ProductNotFound") << "Product " << handle.provenance()->branchName() << " requires product " + << dependent_provenance.branchName() << ", which has been dropped"; + } + + int nJetTags = 0; + + for (size_t iJet = 0; iJet < h_Jets->size(); ++iJet) { + auto const& jet = (*h_Jets)[iJet]; + auto const jetRef = edm::Ref>(h_Jets, iJet); + + float jetTag = -1.f; + if (m_MatchJetsByDeltaR) { + // if match-by-DR is used, update jetTag by reference and use it only if the matching is valid + if (not findTagValueByMinDeltaR2(jetTag, jet, *h_JetTags, m_MaxJetDeltaR2)) { + continue; + } + } else { + // operator[] checks consistency between h_Jets and h_JetTags + jetTag = (*h_JetTags)[reco::JetBaseRef(jetRef)]; + } + + // Check if jet is tagged + bool const jetIsTagged = (m_MinTag <= jetTag) and (jetTag <= m_MaxTag); + + if (jetIsTagged) { + // Store a reference to the jets which passed tagging cuts + filterproduct.addObject(m_TriggerType, jetRef); + ++nJetTags; + } + + logTrace() << "Matched Jets -- Jet[" << iJet << "]" + << ": pt=" << jet.pt() << ", eta=" << jet.eta() << ", phi=" << jet.phi() << ", tag=" << jetTag + << ", isTagged=" << jetIsTagged; + } + + // filter decision + bool const accept = (nJetTags >= m_MinJets); + + logTrace() << " trigger accept = " << accept << ", nJetTags = " << nJetTags; + + return accept; +} + #endif // HLTrigger_btau_HLTJetTag_h diff --git a/HLTrigger/btau/plugins/modules.cc b/HLTrigger/btau/plugins/modules.cc index 56c15cc6e6622..cbf5c94393a18 100644 --- a/HLTrigger/btau/plugins/modules.cc +++ b/HLTrigger/btau/plugins/modules.cc @@ -64,7 +64,6 @@ DEFINE_FWK_MODULE(HLTPFJetPairDzMatchFilter); DEFINE_FWK_MODULE(HLTPFTauPairDzMatchFilter); #include "HLTJetTag.h" -#include "HLTJetTag.cc" typedef HLTJetTag HLTCaloJetTag; typedef HLTJetTag HLTPFJetTag; DEFINE_FWK_MODULE(HLTCaloJetTag);