From 79f3a5b949828e5c761b84f690dad7c36f4a3778 Mon Sep 17 00:00:00 2001 From: Nurfikri Norjoharuddeen Date: Wed, 15 Feb 2023 19:15:57 +0100 Subject: [PATCH] Set fallback puppi weight to 1.0 for packed_cand also. throw exception when jet is weighted but no ValueMap is set. code-checks formatting. --- .../interface/ChargedCandidateConverter.h | 5 +- .../interface/NeutralCandidateConverter.h | 5 +- .../plugins/DeepBoostedJetTagInfoProducer.cc | 17 ++++-- .../plugins/DeepDoubleXTagInfoProducer.cc | 59 +++++++++++++------ .../plugins/DeepFlavourTagInfoProducer.cc | 36 +++++++---- .../src/ChargedCandidateConverter.cc | 4 +- .../src/NeutralCandidateConverter.cc | 4 +- 7 files changed, 85 insertions(+), 45 deletions(-) diff --git a/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h b/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h index c621d5cd22f09..f88f01b3daf7c 100644 --- a/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h +++ b/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h @@ -35,10 +35,11 @@ namespace btagbtvdeep { c_pf_features.deltaR = reco::deltaR(*c_pf, jet); float constituentWeight = 1.; - if (isWeightedJet) constituentWeight = puppiw; + if (isWeightedJet) + constituentWeight = puppiw; c_pf_features.ptrel = catch_infs_and_bound((c_pf->pt() * constituentWeight) / jet.pt(), 0, -1, 0, -1); - c_pf_features.ptrel_noclip = (c_pf->pt() * constituentWeight) / jet.pt(); + c_pf_features.ptrel_noclip = (c_pf->pt() * constituentWeight) / jet.pt(); c_pf_features.erel = (c_pf->energy() * constituentWeight) / jet.energy(); const float etasign = jet.eta() > 0 ? 1 : -1; diff --git a/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h b/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h index b54234180308e..1b6d4a9170ae1 100644 --- a/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h +++ b/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h @@ -40,7 +40,8 @@ namespace btagbtvdeep { n_pf_features.drsubjet2 = drSubjetFeatures.second; float constituentWeight = 1.; - if (isWeightedJet) constituentWeight = puppiw; + if (isWeightedJet) + constituentWeight = puppiw; // Jet relative vars n_pf_features.ptrel = catch_infs_and_bound((n_pf->pt() * constituentWeight) / jet.pt(), 0, -1, 0, -1); @@ -49,7 +50,7 @@ namespace btagbtvdeep { n_pf_features.deltaR = catch_infs_and_bound(reco::deltaR(*n_pf, jet), 0, -0.6, 0, -0.6); n_pf_features.deltaR_noclip = reco::deltaR(*n_pf, jet); - + n_pf_features.isGamma = 0; if (std::abs(n_pf->pdgId()) == 22) n_pf_features.isGamma = 1; diff --git a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc index d4fd8427af5bb..e59b71267ac48 100644 --- a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc @@ -200,6 +200,9 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete if (!puppi_value_map_tag.label().empty()) { puppi_value_map_token_ = consumes>(puppi_value_map_tag); use_puppi_value_map_ = true; + } else if (use_puppiP4_) { + throw edm::Exception(edm::errors::Configuration, + "puppi_value_map is not set but use_puppiP4 is set to True. Must also set puppi_value_map."); } const auto &pvas_tag = iConfig.getParameter("vertex_associator"); @@ -323,14 +326,16 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event &iEvent, const edm::Event float DeepBoostedJetTagInfoProducer::puppiWgt(const reco::CandidatePtr &cand) { const auto *pack_cand = dynamic_cast(&(*cand)); const auto *reco_cand = dynamic_cast(&(*cand)); - float wgt = 1.; - if (pack_cand){ - //fallback value - wgt = pack_cand->puppiWeight(); + + // + // Access puppi weight from ValueMap. + // + float wgt = 1.; // Set to fallback value + + if (pack_cand) { if (use_puppi_value_map_) wgt = (*puppi_value_map_)[cand]; - } - else if (reco_cand) { + } else if (reco_cand) { if (use_puppi_value_map_) wgt = (*puppi_value_map_)[cand]; } else diff --git a/RecoBTag/FeatureTools/plugins/DeepDoubleXTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepDoubleXTagInfoProducer.cc index 8d408f94a534f..2642bd7c47dee 100644 --- a/RecoBTag/FeatureTools/plugins/DeepDoubleXTagInfoProducer.cc +++ b/RecoBTag/FeatureTools/plugins/DeepDoubleXTagInfoProducer.cc @@ -79,17 +79,19 @@ DeepDoubleXTagInfoProducer::DeepDoubleXTagInfoProducer(const edm::ParameterSet& shallow_tag_info_token_( consumes(iConfig.getParameter("shallow_tag_infos"))), track_builder_token_( - esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))), + esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))), use_puppi_value_map_(false), fallback_puppi_weight_(iConfig.getParameter("fallback_puppi_weight")), - is_weighted_jet_(iConfig.getParameter("is_weighted_jet")) - { + is_weighted_jet_(iConfig.getParameter("is_weighted_jet")) { produces(); const auto& puppi_value_map_tag = iConfig.getParameter("puppi_value_map"); if (!puppi_value_map_tag.label().empty()) { puppi_value_map_token_ = consumes>(puppi_value_map_tag); use_puppi_value_map_ = true; + } else if (is_weighted_jet_) { + throw edm::Exception(edm::errors::Configuration, + "puppi_value_map is not set but jet is weighted. Must set puppi_value_map."); } } @@ -289,34 +291,37 @@ void DeepDoubleXTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet reco::PFCandidatePtr reco_ptr; if (pf_jet) { reco_ptr = pf_jet->getPFConstituent(i); - } + } reco::CandidatePtr cand_ptr; - if (pat_jet){ + if (pat_jet) { cand_ptr = pat_jet->sourceCandidatePtr(i); } - float puppiw = 1.0; // fallback value + // + // Access puppi weight from ValueMap. + // + float puppiw = 1.0; // Set to fallback value + if (reco_cand) { - puppiw = 1.0; // fallback value for reco_cand if (use_puppi_value_map_) puppiw = (*puppi_value_map)[reco_ptr]; else if (!fallback_puppi_weight_) { throw edm::Exception(edm::errors::InvalidReference, "PUPPI value map missing") - << "use fallback_puppi_weight option to use " << puppiw << " for reco_cand as default"; + << "use fallback_puppi_weight option to use " << puppiw << " for reco_cand as default"; } - } - else if(packed_cand){ - puppiw = packed_cand->puppiWeight(); // fallback value for packed_cand + } else if (packed_cand) { if (use_puppi_value_map_) puppiw = (*puppi_value_map)[cand_ptr]; else if (!fallback_puppi_weight_) { throw edm::Exception(edm::errors::InvalidReference, "PUPPI value map missing") - << "use fallback_puppi_weight option to use puppiWeight() for packed_cand as default"; + << "use fallback_puppi_weight option to use " << puppiw << " for packed_cand as default"; } + } else { + throw edm::Exception(edm::errors::InvalidReference) + << "Cannot convert to either reco::PFCandidate or pat::PackedCandidate"; } - float drminpfcandsv = btagbtvdeep::mindrsvpfcand(svs_unsorted, cand, jet_radius_); if (cand->charge() != 0) { // is charged candidate @@ -326,8 +331,14 @@ void DeepDoubleXTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet // get_ref to vector element auto& c_pf_features = features.c_pf_features.at(entry); if (packed_cand) { - btagbtvdeep::packedCandidateToFeatures( - packed_cand, *pat_jet, trackinfo, is_weighted_jet_, drminpfcandsv, static_cast(jet_radius_), puppiw, c_pf_features); + btagbtvdeep::packedCandidateToFeatures(packed_cand, + *pat_jet, + trackinfo, + is_weighted_jet_, + drminpfcandsv, + static_cast(jet_radius_), + puppiw, + c_pf_features); } else if (reco_cand) { // get vertex association quality int pv_ass_quality = 0; // fallback value @@ -362,11 +373,21 @@ void DeepDoubleXTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet auto& n_pf_features = features.n_pf_features.at(entry); // // fill feature structure if (packed_cand) { - btagbtvdeep::packedCandidateToFeatures( - packed_cand, *pat_jet, is_weighted_jet_, drminpfcandsv, static_cast(jet_radius_), puppiw, n_pf_features); + btagbtvdeep::packedCandidateToFeatures(packed_cand, + *pat_jet, + is_weighted_jet_, + drminpfcandsv, + static_cast(jet_radius_), + puppiw, + n_pf_features); } else if (reco_cand) { - btagbtvdeep::recoCandidateToFeatures( - reco_cand, jet, is_weighted_jet_, drminpfcandsv, static_cast(jet_radius_), puppiw, n_pf_features); + btagbtvdeep::recoCandidateToFeatures(reco_cand, + jet, + is_weighted_jet_, + drminpfcandsv, + static_cast(jet_radius_), + puppiw, + n_pf_features); } } } diff --git a/RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc index 681c5ca0ee5e6..0bbb98b2442d5 100644 --- a/RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc +++ b/RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc @@ -139,6 +139,9 @@ DeepFlavourTagInfoProducer::DeepFlavourTagInfoProducer(const edm::ParameterSet& if (!puppi_value_map_tag.label().empty()) { puppi_value_map_token_ = consumes>(puppi_value_map_tag); use_puppi_value_map_ = true; + } else if (is_weighted_jet_) { + throw edm::Exception(edm::errors::Configuration, + "puppi_value_map is not set but jet is weighted. Must set puppi_value_map."); } const auto& pvas_tag = iConfig.getParameter("vertex_associator"); @@ -368,31 +371,33 @@ void DeepFlavourTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet reco_ptr = pat_jet->getPFConstituent(i); } - reco::CandidatePtr cand_ptr; - if (pat_jet){ + reco::CandidatePtr cand_ptr; + if (pat_jet) { cand_ptr = pat_jet->sourceCandidatePtr(i); } - // get PUPPI weight from value map - float puppiw = 1.0; // fallback value + // + // Access puppi weight from ValueMap. + // + float puppiw = 1.0; // Set to fallback value if (reco_cand) { - puppiw = 1.0; // fallback value for reco_cand if (use_puppi_value_map_) puppiw = (*puppi_value_map)[reco_ptr]; else if (!fallback_puppi_weight_) { throw edm::Exception(edm::errors::InvalidReference, "PUPPI value map missing") - << "use fallback_puppi_weight option to use " << puppiw << " for reco_cand as default"; + << "use fallback_puppi_weight option to use " << puppiw << " for reco_cand as default"; } - } - else if(packed_cand){ - puppiw = packed_cand->puppiWeight(); // fallback value for packed_cand + } else if (packed_cand) { if (use_puppi_value_map_) puppiw = (*puppi_value_map)[cand_ptr]; else if (!fallback_puppi_weight_) { throw edm::Exception(edm::errors::InvalidReference, "PUPPI value map missing") - << "use fallback_puppi_weight option to use puppiWeight() for packed_cand as default"; + << "use fallback_puppi_weight option to use " << puppiw << " for packed_cand as default"; } + } else { + throw edm::Exception(edm::errors::InvalidReference) + << "Cannot convert to either reco::PFCandidate or pat::PackedCandidate"; } float drminpfcandsv = btagbtvdeep::mindrsvpfcand(svs_unsorted, cand); @@ -409,8 +414,15 @@ void DeepFlavourTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet auto& c_pf_features = features.c_pf_features.at(entry); // fill feature structure if (packed_cand) { - btagbtvdeep::packedCandidateToFeatures( - packed_cand, jet, trackinfo, is_weighted_jet_, drminpfcandsv, static_cast(jet_radius_), puppiw, c_pf_features, flip_); + btagbtvdeep::packedCandidateToFeatures(packed_cand, + jet, + trackinfo, + is_weighted_jet_, + drminpfcandsv, + static_cast(jet_radius_), + puppiw, + c_pf_features, + flip_); } else if (reco_cand) { // get vertex association quality int pv_ass_quality = 0; // fallback value diff --git a/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc b/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc index 4ef8c31b7597c..84d0fdb2d1cca 100644 --- a/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc +++ b/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc @@ -5,7 +5,7 @@ namespace btagbtvdeep { void packedCandidateToFeatures(const pat::PackedCandidate* c_pf, const pat::Jet& jet, const TrackInfoBuilder& track_info, - const bool isWeightedJet, + const bool isWeightedJet, const float drminpfcandsv, const float jetR, const float puppiw, @@ -39,7 +39,7 @@ namespace btagbtvdeep { void recoCandidateToFeatures(const reco::PFCandidate* c_pf, const reco::Jet& jet, const TrackInfoBuilder& track_info, - const bool isWeightedJet, + const bool isWeightedJet, const float drminpfcandsv, const float jetR, const float puppiw, diff --git a/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc b/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc index 62135055589c3..95772b14d0618 100644 --- a/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc +++ b/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc @@ -4,7 +4,7 @@ namespace btagbtvdeep { void packedCandidateToFeatures(const pat::PackedCandidate* n_pf, const pat::Jet& jet, - const bool isWeightedJet, + const bool isWeightedJet, const float drminpfcandsv, const float jetR, const float puppiw, @@ -17,7 +17,7 @@ namespace btagbtvdeep { void recoCandidateToFeatures(const reco::PFCandidate* n_pf, const reco::Jet& jet, - const bool isWeightedJet, + const bool isWeightedJet, const float drminpfcandsv, const float jetR, const float puppiw,