Skip to content

Commit

Permalink
Merge pull request #28902 from hqucms/particle-net-11X
Browse files Browse the repository at this point in the history
MXNet-based implementation of the ParticleNet tagger
  • Loading branch information
cmsbuild authored Mar 10, 2020
2 parents 69ad0c0 + 3c9ce7f commit c1ef845
Show file tree
Hide file tree
Showing 12 changed files with 942 additions and 25 deletions.
16 changes: 16 additions & 0 deletions PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
, 'pfDeepDoubleXTagInfos'
# DeepBoostedJet tag infos
, 'pfDeepBoostedJetTagInfos'
# ParticleNet tag infos
, 'pfParticleNetTagInfos'
# Pixel Cluster tag infos
, 'pixelClusterTagInfos'
]
Expand Down Expand Up @@ -231,3 +233,17 @@
for disc in _pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs:
supportedMetaDiscr[disc] = _pfMassDecorrelatedDeepBoostedJetTagsProbs
# -----------------------------------

# -----------------------------------
# setup ParticleNet
from RecoBTag.MXNet.pfParticleNet_cff import _pfParticleNetJetTagsProbs, _pfParticleNetJetTagsMetaDiscrs, \
_pfMassDecorrelatedParticleNetJetTagsProbs, _pfMassDecorrelatedParticleNetJetTagsMetaDiscrs
# update supportedBtagDiscr
for disc in _pfParticleNetJetTagsProbs + _pfMassDecorrelatedParticleNetJetTagsProbs:
supportedBtagDiscr[disc] = [["pfParticleNetTagInfos"]]
# update supportedMetaDiscr
for disc in _pfParticleNetJetTagsMetaDiscrs:
supportedMetaDiscr[disc] = _pfParticleNetJetTagsProbs
for disc in _pfMassDecorrelatedParticleNetJetTagsMetaDiscrs:
supportedMetaDiscr[disc] = _pfMassDecorrelatedParticleNetJetTagsProbs
# -----------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def applyDeepBtagging( process, postfix="" ) :
delattr(process, 'selectedUpdatedPatJetsSlimmedDeepFlavour'+postfix)

from RecoBTag.ONNXRuntime.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsAll as pfDeepBoostedJetTagsAll
from RecoBTag.MXNet.pfParticleNet_cff import _pfParticleNetJetTagsAll as pfParticleNetJetTagsAll

# update slimmed jets to include particle-based deep taggers (keep same name)
# make clone for DeepTags-less slimmed AK8 jets, so output name is preserved
Expand All @@ -60,7 +61,7 @@ def applyDeepBtagging( process, postfix="" ) :
'pfMassIndependentDeepDoubleCvLJetTags:probHcc',
'pfMassIndependentDeepDoubleCvBJetTags:probHbb',
'pfMassIndependentDeepDoubleCvBJetTags:probHcc',
) + pfDeepBoostedJetTagsAll
) + pfDeepBoostedJetTagsAll + pfParticleNetJetTagsAll
)
updateJetCollection(
process,
Expand Down
23 changes: 23 additions & 0 deletions PhysicsTools/PatAlgos/python/tools/jetTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,29 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou
),
process, task)

if btagInfo == 'pfParticleNetTagInfos':
if pfCandidates.value() == 'packedPFCandidates':
# case 1: running over jets whose daughters are PackedCandidates (only via updateJetCollection for now)
puppi_value_map = ""
vertex_associator = ""
elif pfCandidates.value() == 'particleFlow':
raise ValueError("Running pfDeepBoostedJetTagInfos with reco::PFCandidates is currently not supported.")
# case 2: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD)
puppi_value_map = "puppi"
vertex_associator = "primaryVertexAssociation:original"
else:
raise ValueError("Invalid pfCandidates collection: %s." % pfCandidates.value())
addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix,
btag.pfParticleNetTagInfos.clone(
jets = jetSource,
vertices = pvSource,
secondary_vertices = svSource,
pf_candidates = pfCandidates,
puppi_value_map = puppi_value_map,
vertex_associator = vertex_associator,
),
process, task)

acceptedTagInfos.append(btagInfo)
elif hasattr(toptag, btagInfo) :
acceptedTagInfos.append(btagInfo)
Expand Down
1 change: 1 addition & 0 deletions RecoBTag/Configuration/python/RecoBTag_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from RecoBTag.ONNXRuntime.pfDeepFlavour_cff import *
from RecoBTag.ONNXRuntime.pfDeepDoubleX_cff import *
from RecoBTag.ONNXRuntime.pfDeepBoostedJet_cff import *
from RecoBTag.MXNet.pfParticleNet_cff import *
from RecoVertex.AdaptiveVertexFinder.inclusiveVertexing_cff import *
from RecoBTag.PixelCluster.pixelClusterTagInfos_cfi import *

Expand Down
64 changes: 40 additions & 24 deletions RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> {
const double jet_radius_;
const double min_jet_pt_;
const double min_pt_for_track_properties_;
const bool use_puppiP4_;

edm::EDGetTokenT<edm::View<reco::Jet>> jet_token_;
edm::EDGetTokenT<VertexCollection> vtx_token_;
Expand Down Expand Up @@ -83,10 +84,13 @@ const std::vector<std::string> DeepBoostedJetTagInfoProducer::particle_features_
"pfcand_dphidphi", "pfcand_dxydxy", "pfcand_dzdz", "pfcand_dxydz",
"pfcand_dphidxy", "pfcand_dlambdadz", "pfcand_btagEtaRel", "pfcand_btagPtRatio",
"pfcand_btagPParRatio", "pfcand_btagSip2dVal", "pfcand_btagSip2dSig", "pfcand_btagSip3dVal",
"pfcand_btagSip3dSig", "pfcand_btagJetDistVal",
"pfcand_btagSip3dSig", "pfcand_btagJetDistVal", "pfcand_mask", "pfcand_pt_log_nopuppi",
"pfcand_e_log_nopuppi"

};

const std::vector<std::string> DeepBoostedJetTagInfoProducer::sv_features_{
"sv_mask",
"sv_phirel",
"sv_etarel",
"sv_deltaR",
Expand All @@ -108,6 +112,7 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete
: jet_radius_(iConfig.getParameter<double>("jet_radius")),
min_jet_pt_(iConfig.getParameter<double>("min_jet_pt")),
min_pt_for_track_properties_(iConfig.getParameter<double>("min_pt_for_track_properties")),
use_puppiP4_(iConfig.getParameter<bool>("use_puppiP4")),
jet_token_(consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jets"))),
vtx_token_(consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
sv_token_(consumes<SVCollection>(iConfig.getParameter<edm::InputTag>("secondary_vertices"))),
Expand Down Expand Up @@ -138,6 +143,7 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti
desc.add<double>("jet_radius", 0.8);
desc.add<double>("min_jet_pt", 150);
desc.add<double>("min_pt_for_track_properties", -1);
desc.add<bool>("use_puppiP4", true);
desc.add<edm::InputTag>("vertices", edm::InputTag("offlinePrimaryVertices"));
desc.add<edm::InputTag>("secondary_vertices", edm::InputTag("inclusiveCandidateSecondaryVertices"));
desc.add<edm::InputTag>("pf_candidates", edm::InputTag("particleFlow"));
Expand All @@ -150,8 +156,7 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti
void DeepBoostedJetTagInfoProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
auto output_tag_infos = std::make_unique<DeepBoostedJetTagInfoCollection>();

edm::Handle<edm::View<reco::Jet>> jets;
iEvent.getByToken(jet_token_, jets);
auto jets = iEvent.getHandle(jet_token_);

iEvent.getByToken(vtx_token_, vtxs_);
if (vtxs_->empty()) {
Expand Down Expand Up @@ -259,11 +264,17 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures
// get the original reco/packed candidate not scaled by the puppi weight
daughters.push_back(pfcands_->ptrAt(cand.key()));
}
// sort by Puppi-weighted pt
std::sort(
daughters.begin(), daughters.end(), [&puppi_wgt_cache](const reco::CandidatePtr &a, const reco::CandidatePtr &b) {
return puppi_wgt_cache.at(a.key()) * a->pt() > puppi_wgt_cache.at(b.key()) * b->pt();
});
if (use_puppiP4_) {
// sort by Puppi-weighted pt
std::sort(daughters.begin(),
daughters.end(),
[&puppi_wgt_cache](const reco::CandidatePtr &a, const reco::CandidatePtr &b) {
return puppi_wgt_cache.at(a.key()) * a->pt() > puppi_wgt_cache.at(b.key()) * b->pt();
});
} else {
// sort by original pt (not Puppi-weighted)
std::sort(daughters.begin(), daughters.end(), [](const auto &a, const auto &b) { return a->pt() > b->pt(); });
}

// reserve space
for (const auto &name : particle_features_) {
Expand All @@ -279,7 +290,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures
const auto *packed_cand = dynamic_cast<const pat::PackedCandidate *>(&(*cand));
const auto *reco_cand = dynamic_cast<const reco::PFCandidate *>(&(*cand));

auto puppiP4 = puppi_wgt_cache.at(cand.key()) * cand->p4();
auto candP4 = use_puppiP4_ ? puppi_wgt_cache.at(cand.key()) * cand->p4() : cand->p4();
if (packed_cand) {
float hcal_fraction = 0.;
if (packed_cand->pdgId() == 1 || packed_cand->pdgId() == 130) {
Expand Down Expand Up @@ -343,14 +354,18 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures

// basic kinematics
fts.fill("pfcand_puppiw", puppi_wgt_cache.at(cand.key()));
fts.fill("pfcand_phirel", reco::deltaPhi(puppiP4, jet));
fts.fill("pfcand_etarel", etasign * (puppiP4.eta() - jet.eta()));
fts.fill("pfcand_deltaR", reco::deltaR(puppiP4, jet));
fts.fill("pfcand_abseta", std::abs(puppiP4.eta()));
fts.fill("pfcand_phirel", reco::deltaPhi(candP4, jet));
fts.fill("pfcand_etarel", etasign * (candP4.eta() - jet.eta()));
fts.fill("pfcand_deltaR", reco::deltaR(candP4, jet));
fts.fill("pfcand_abseta", std::abs(candP4.eta()));

fts.fill("pfcand_ptrel_log", catch_infs(std::log(candP4.pt() / jet.pt()), -99));
fts.fill("pfcand_erel_log", catch_infs(std::log(candP4.energy() / jet.energy()), -99));
fts.fill("pfcand_pt_log", catch_infs(std::log(candP4.pt()), -99));

fts.fill("pfcand_ptrel_log", catch_infs(std::log(puppiP4.pt() / jet.pt()), -99));
fts.fill("pfcand_erel_log", catch_infs(std::log(puppiP4.energy() / jet.energy()), -99));
fts.fill("pfcand_pt_log", catch_infs(std::log(puppiP4.pt()), -99));
fts.fill("pfcand_mask", 1);
fts.fill("pfcand_pt_log_nopuppi", catch_infs(std::log(cand->pt()), -99));
fts.fill("pfcand_e_log_nopuppi", catch_infs(std::log(cand->energy()), -99));

double minDR = 999;
for (const auto &sv : *svs_) {
Expand Down Expand Up @@ -390,14 +405,14 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures

TrackInfoBuilder trkinfo(track_builder_);
trkinfo.buildTrackInfo(&(*cand), jet_dir, jet_ref_track_dir, *pv_);
fts.fill("pfcand_btagEtaRel", trkinfo.getTrackEtaRel());
fts.fill("pfcand_btagPtRatio", trkinfo.getTrackPtRatio());
fts.fill("pfcand_btagPParRatio", trkinfo.getTrackPParRatio());
fts.fill("pfcand_btagSip2dVal", trkinfo.getTrackSip2dVal());
fts.fill("pfcand_btagSip2dSig", trkinfo.getTrackSip2dSig());
fts.fill("pfcand_btagSip3dVal", trkinfo.getTrackSip3dVal());
fts.fill("pfcand_btagSip3dSig", trkinfo.getTrackSip3dSig());
fts.fill("pfcand_btagJetDistVal", trkinfo.getTrackJetDistVal());
fts.fill("pfcand_btagEtaRel", catch_infs(trkinfo.getTrackEtaRel()));
fts.fill("pfcand_btagPtRatio", catch_infs(trkinfo.getTrackPtRatio()));
fts.fill("pfcand_btagPParRatio", catch_infs(trkinfo.getTrackPParRatio()));
fts.fill("pfcand_btagSip2dVal", catch_infs(trkinfo.getTrackSip2dVal()));
fts.fill("pfcand_btagSip2dSig", catch_infs(trkinfo.getTrackSip2dSig()));
fts.fill("pfcand_btagSip3dVal", catch_infs(trkinfo.getTrackSip3dVal()));
fts.fill("pfcand_btagSip3dSig", catch_infs(trkinfo.getTrackSip3dSig()));
fts.fill("pfcand_btagJetDistVal", catch_infs(trkinfo.getTrackJetDistVal()));
} else {
fts.fill("pfcand_normchi2", 999);

Expand Down Expand Up @@ -445,6 +460,7 @@ void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts,

for (const auto *sv : jetSVs) {
// basic kinematics
fts.fill("sv_mask", 1);
fts.fill("sv_phirel", reco::deltaPhi(*sv, jet));
fts.fill("sv_etarel", etasign * (sv->eta() - jet.eta()));
fts.fill("sv_deltaR", reco::deltaR(*sv, jet));
Expand Down
Loading

0 comments on commit c1ef845

Please sign in to comment.