From 0e951d8f25cac6ae3fcd6af8b95a7be970d067b1 Mon Sep 17 00:00:00 2001 From: Emil Bols Date: Thu, 23 Jan 2020 22:54:13 +0100 Subject: [PATCH] BTV fix for Puppi integration --- .../plugins/JetFlavourClustering.cc | 22 ++++++- .../TemplatedSecondaryVertexProducer.cc | 64 +++++++++++++++---- 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/PhysicsTools/JetMCAlgos/plugins/JetFlavourClustering.cc b/PhysicsTools/JetMCAlgos/plugins/JetFlavourClustering.cc index 0aa938b4e25bc..122b38db46951 100644 --- a/PhysicsTools/JetMCAlgos/plugins/JetFlavourClustering.cc +++ b/PhysicsTools/JetMCAlgos/plugins/JetFlavourClustering.cc @@ -95,6 +95,7 @@ #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h" #include "DataFormats/Math/interface/deltaR.h" #include "PhysicsTools/JetMCUtils/interface/CandMCTag.h" +#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" #include "fastjet/JetDefinition.hh" #include "fastjet/ClusterSequence.hh" @@ -196,6 +197,8 @@ class JetFlavourClustering : public edm::stream::EDProducer<> { const double relPtTolerance_; const bool hadronFlavourHasPriority_; const bool useSubjets_; + bool usePuppi_; + const bool useLeptons_; ClusterSequencePtr fjClusterSeq_; @@ -218,6 +221,7 @@ JetFlavourClustering::JetFlavourClustering(const edm::ParameterSet& iConfig) partonsToken_(consumes(iConfig.getParameter("partons"))), jetAlgorithm_(iConfig.getParameter("jetAlgorithm")), rParam_(iConfig.getParameter("rParam")), + jetPtMin_( 0.), // hardcoded to 0. since we simply want to recluster all input jets which already had some PtMin applied ghostRescaling_(iConfig.exists("ghostRescaling") ? iConfig.getParameter("ghostRescaling") : 1e-18), @@ -232,6 +236,13 @@ JetFlavourClustering::JetFlavourClustering(const edm::ParameterSet& iConfig) { // register your products produces(); + std::string label = iConfig.getParameter("jets").label(); + usePuppi_ = false; + if(label.find("Puppi") != std::string::npos){ + usePuppi_ = true; + // This check will not fire on updatedPatJetsSlimmedDeepFlavour, updatedPatJetsSlimmedAK8DeepTags. Is that what we want? + } + if (useSubjets_) produces("SubJets"); @@ -289,6 +300,7 @@ void JetFlavourClustering::produce(edm::Event& iEvent, const edm::EventSetup& iS if (useLeptons_) iEvent.getByToken(leptonsToken_, leptons); + auto jetFlavourInfos = std::make_unique(reco::JetRefBaseProd(jets)); std::unique_ptr subjetFlavourInfos; if (useSubjets_) @@ -315,7 +327,15 @@ void JetFlavourClustering::produce(edm::Event& iEvent, const edm::EventSetup& iS edm::LogWarning("NullTransverseMomentum") << "dropping input candidate with pt=0"; continue; } - fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + if(usePuppi_){ + const auto pf_constit = dynamic_cast(&*constit); + double w = pf_constit->puppiWeight(); + double E_w = std::sqrt(pf_constit->p() * w * pf_constit->p() * w + pf_constit->mass() * pf_constit->mass()); + fjInputs.push_back(fastjet::PseudoJet(pf_constit->px() * w, pf_constit->py() * w, pf_constit->pz()* w, E_w)); + } + else{ + fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + } } } // insert "ghost" b hadrons in the vector of constituents diff --git a/RecoBTag/SecondaryVertex/plugins/TemplatedSecondaryVertexProducer.cc b/RecoBTag/SecondaryVertex/plugins/TemplatedSecondaryVertexProducer.cc index 256aa4f270aa9..dcc0f29ba4a98 100644 --- a/RecoBTag/SecondaryVertex/plugins/TemplatedSecondaryVertexProducer.cc +++ b/RecoBTag/SecondaryVertex/plugins/TemplatedSecondaryVertexProducer.cc @@ -22,6 +22,11 @@ #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/Exception.h" +#include "FWCore/ParameterSet/interface/Registry.h" +#include "FWCore/Common/interface/Provenance.h" +#include "DataFormats/Provenance/interface/ProductProvenance.h" + + #include "DataFormats/PatCandidates/interface/Jet.h" #include "DataFormats/TrackReco/interface/Track.h" #include "DataFormats/TrackReco/interface/TrackFwd.h" @@ -159,6 +164,7 @@ class TemplatedSecondaryVertexProducer : public edm::stream::EDProducer<> { double ghostRescaling; double relPtTolerance; bool useFatJets; + bool usePuppi; bool useGroomedFatJets; edm::EDGetTokenT > token_fatJets; edm::EDGetTokenT > token_groomedFatJets; @@ -261,6 +267,7 @@ TemplatedSecondaryVertexProducer::TemplatedSecondaryVertexProducer(co } useSVClustering = (params.existsAs("useSVClustering") ? params.getParameter("useSVClustering") : false); useSVMomentum = (params.existsAs("useSVMomentum") ? params.getParameter("useSVMomentum") : false); + usePuppi = false; useFatJets = (useExternalSV && params.exists("fatJets")); useGroomedFatJets = (useExternalSV && params.exists("groomedFatJets")); if (useSVClustering) { @@ -286,10 +293,16 @@ TemplatedSecondaryVertexProducer::TemplatedSecondaryVertexProducer(co throw cms::Exception("InvalidJetAlgorithm") << "Jet clustering algorithm is invalid: " << jetAlgorithm << ", use CambridgeAachen | Kt | AntiKt" << std::endl; } - if (useFatJets) + if (useFatJets){ token_fatJets = consumes >(params.getParameter("fatJets")); - if (useGroomedFatJets) + std::string label = params.getParameter("fatJets").label(); + if( label.find("Puppi") != std::string::npos ){ + usePuppi = true; + } + } + if (useGroomedFatJets){ token_groomedFatJets = consumes >(params.getParameter("groomedFatJets")); + } if (useFatJets && !useSVClustering) rParam = params.getParameter("rParam"); // will be used later as a dR cut @@ -310,7 +323,21 @@ void TemplatedSecondaryVertexProducer::produce(edm::Event &event, con edm::Handle > trackIPTagInfos; event.getByToken(token_trackIPTagInfo, trackIPTagInfos); - + if(!useFatJets){ + if constexpr (std::is_same_v ){ + const edm::Provenance *prov = trackIPTagInfos.provenance(); + const edm::ParameterSet& psetFromProvenance = edm::parameterSet(*prov,event.processHistory()); + std::string label; + label = psetFromProvenance.getParameter("jets").label(); + if( label.find("Puppi") != std::string::npos ){ + usePuppi = true; + } + } + else{ + usePuppi = false; + } + } + // External Sec Vertex collection (e.g. for IVF usage) edm::Handle > extSecVertex; if (useExternalSV) @@ -320,7 +347,6 @@ void TemplatedSecondaryVertexProducer::produce(edm::Event &event, con edm::Handle > groomedFatJetsHandle; if (useFatJets) { event.getByToken(token_fatJets, fatJetsHandle); - if (useGroomedFatJets) { event.getByToken(token_groomedFatJets, groomedFatJetsHandle); @@ -363,7 +389,7 @@ void TemplatedSecondaryVertexProducer::produce(edm::Event &event, con default: /* nothing */; } - + // ------------------------------------ SV clustering START -------------------------------------------- std::vector > clusteredSVs(trackIPTagInfos->size(), std::vector()); if (useExternalSV && useSVClustering && !trackIPTagInfos->empty()) { @@ -376,11 +402,19 @@ void TemplatedSecondaryVertexProducer::produce(edm::Event &event, con std::vector >::const_iterator m; for (m = constituents.begin(); m != constituents.end(); ++m) { reco::CandidatePtr constit = *m; - if (constit->pt() == 0) { - edm::LogWarning("NullTransverseMomentum") << "dropping input candidate with pt=0"; - continue; - } - fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + if (constit->pt() == 0) { + edm::LogWarning("NullTransverseMomentum") << "dropping input candidate with pt=0"; + continue; + } + if(usePuppi){ + const reco::PFCandidate* pf_constit = dynamic_cast(&*constit); + auto w = pf_constit->puppiWeight(); + double E_w = std::sqrt(pf_constit->p() * w * pf_constit->p() * w + pf_constit->mass() * pf_constit->mass()); + fjInputs.push_back(fastjet::PseudoJet(pf_constit->px() * w, pf_constit->py() * w, pf_constit->pz()* w, E_w)); + } + else{ + fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + } } } } else { @@ -394,7 +428,15 @@ void TemplatedSecondaryVertexProducer::produce(edm::Event &event, con edm::LogWarning("NullTransverseMomentum") << "dropping input candidate with pt=0"; continue; } - fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + if(usePuppi){ + const auto* pf_constit = dynamic_cast(&*constit); + auto w = pf_constit->puppiWeight(); + double E_w = std::sqrt(pf_constit->p() * w * pf_constit->p() * w + pf_constit->mass() * pf_constit->mass()); + fjInputs.push_back(fastjet::PseudoJet(pf_constit->px() * w, pf_constit->py() * w, pf_constit->pz()* w, E_w)); + } + else{ + fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy())); + } } } }