Skip to content

Commit

Permalink
Merge pull request #35341 from bainbrid/LowPtElectrons_ULFastSimFix_106X
Browse files Browse the repository at this point in the history
LowPtElectrons: fix for UL FastSim mini v2 and nano v9 workflows (back port)
  • Loading branch information
cmsbuild authored Sep 24, 2021
2 parents d27d94a + 0f64359 commit 39546b2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
#include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
#include <vector>

namespace reco {
class Track;
}

namespace lowptgsfeleid {

// feature list for new model (2019Sept15)
std::vector<float> features_V1(reco::GsfElectron const& ele, float rho, float unbiased, float field_z);
std::vector<float> features_V1(
reco::GsfElectron const& ele, float rho, float unbiased, float field_z, const reco::Track* trk = nullptr);

// feature list for original models (2019Aug07 and earlier)
std::vector<float> features_V0(reco::GsfElectron const& ele, float rho, float unbiased);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "CommonTools/MVAUtils/interface/GBRForestTools.h"
#include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
#include "DataFormats/PatCandidates/interface/Electron.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"
#include "DataFormats/EgammaReco/interface/SuperCluster.h"
#include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
#include "DataFormats/TrackReco/interface/Track.h"
Expand All @@ -35,12 +36,19 @@ class LowPtGsfElectronIDProducer final : public edm::global::EDProducer<> {
static void fillDescriptions(edm::ConfigurationDescriptions&);

private:
double eval(const std::string& name, const edm::Ptr<reco::GsfElectron>&, double rho, float unbiased, float field_z) const;
double eval(const std::string& name,
const edm::Ptr<reco::GsfElectron>&,
double rho,
float unbiased,
float field_z,
const reco::Track* trk = nullptr) const;

const bool useGsfToTrack_;
const bool usePAT_;
edm::EDGetTokenT<reco::GsfElectronCollection> electrons_;
edm::EDGetTokenT<pat::ElectronCollection> patElectrons_;
const edm::EDGetTokenT<double> rho_;
edm::EDGetTokenT<edm::Association<reco::TrackCollection> > gsf2trk_;
edm::EDGetTokenT<edm::ValueMap<float> > unbiased_;
const std::vector<std::string> names_;
const bool passThrough_;
Expand All @@ -54,17 +62,22 @@ class LowPtGsfElectronIDProducer final : public edm::global::EDProducer<> {
////////////////////////////////////////////////////////////////////////////////
//
LowPtGsfElectronIDProducer::LowPtGsfElectronIDProducer(const edm::ParameterSet& conf)
: usePAT_(conf.getParameter<bool>("usePAT")),
: useGsfToTrack_(conf.getParameter<bool>("useGsfToTrack")),
usePAT_(conf.getParameter<bool>("usePAT")),
electrons_(),
patElectrons_(),
rho_(consumes<double>(conf.getParameter<edm::InputTag>("rho"))),
gsf2trk_(),
unbiased_(),
names_(conf.getParameter<std::vector<std::string> >("ModelNames")),
passThrough_(conf.getParameter<bool>("PassThrough")),
minPtThreshold_(conf.getParameter<double>("MinPtThreshold")),
maxPtThreshold_(conf.getParameter<double>("MaxPtThreshold")),
thresholds_(conf.getParameter<std::vector<double> >("ModelThresholds")),
version_(conf.getParameter<std::string>("Version")) {
if (useGsfToTrack_) {
gsf2trk_ = consumes<edm::Association<reco::TrackCollection> >(conf.getParameter<edm::InputTag>("gsfToTrack"));
}
if (usePAT_) {
patElectrons_ = consumes<pat::ElectronCollection>(conf.getParameter<edm::InputTag>("electrons"));
} else {
Expand Down Expand Up @@ -107,6 +120,12 @@ void LowPtGsfElectronIDProducer::produce(edm::StreamID, edm::Event& event, const
throw cms::Exception("InvalidHandle", os.str());
}

// Retrieve GsfToTrack Association from Event
edm::Handle<edm::Association<reco::TrackCollection> > gsf2trk;
if (useGsfToTrack_) {
event.getByToken(gsf2trk_, gsf2trk);
}

// Retrieve pat::Electrons or reco::GsfElectrons from Event
edm::Handle<pat::ElectronCollection> patElectrons;
edm::Handle<reco::GsfElectronCollection> electrons;
Expand Down Expand Up @@ -135,8 +154,31 @@ void LowPtGsfElectronIDProducer::produce(edm::StreamID, edm::Event& event, const
if (!ele->isElectronIDAvailable("unbiased")) {
continue;
}

// Extract Track
const reco::Track* trk = nullptr;
if (useGsfToTrack_) {
using PackedPtr = edm::Ptr<pat::PackedCandidate>;
const PackedPtr* ptr1 = ele->userData<PackedPtr>("ele2packed");
const PackedPtr* ptr2 = ele->userData<PackedPtr>("ele2lost");
auto hasBestTrack = [](const PackedPtr* ptr) {
return ptr != nullptr && ptr->isNonnull() && ptr->isAvailable() && ptr->get() != nullptr &&
ptr->get()->bestTrack() != nullptr;
};
if (hasBestTrack(ptr1)) {
trk = ptr1->get()->bestTrack();
} else if (hasBestTrack(ptr2)) {
trk = ptr2->get()->bestTrack();
}
} else {
reco::TrackRef ref = ele->closestCtfTrackRef();
if (ref.isNonnull() && ref.isAvailable()) {
trk = ref.get();
}
}

for (unsigned int iname = 0; iname < names_.size(); ++iname) {
output[iname][iele] = eval(names_[iname], ele, *rho, ele->electronID("unbiased"), zfield.z());
output[iname][iele] = eval(names_[iname], ele, *rho, ele->electronID("unbiased"), zfield.z(), trk);
}
}
} else {
Expand All @@ -149,9 +191,26 @@ void LowPtGsfElectronIDProducer::produce(edm::StreamID, edm::Event& event, const
if (gsf.isNull()) {
continue;
}

// Extract Track
const reco::Track* trk = nullptr;
if (useGsfToTrack_) {
if (gsf.isAvailable()) {
auto const& ref = (*gsf2trk)[gsf];
if (ref.isNonnull() && ref.isAvailable()) {
trk = ref.get();
}
}
} else {
reco::TrackRef ref = ele->closestCtfTrackRef();
if (ref.isNonnull() && ref.isAvailable()) {
trk = ref.get();
}
}

float unbiased = (*unbiasedH)[gsf];
for (unsigned int iname = 0; iname < names_.size(); ++iname) {
output[iname][iele] = eval(names_[iname], ele, *rho, unbiased, zfield.z());
output[iname][iele] = eval(names_[iname], ele, *rho, unbiased, zfield.z(), trk);
}
}
}
Expand All @@ -172,8 +231,12 @@ void LowPtGsfElectronIDProducer::produce(edm::StreamID, edm::Event& event, const

//////////////////////////////////////////////////////////////////////////////////////////
//
double LowPtGsfElectronIDProducer::eval(
const std::string& name, const edm::Ptr<reco::GsfElectron>& ele, double rho, float unbiased, float field_z) const {
double LowPtGsfElectronIDProducer::eval(const std::string& name,
const edm::Ptr<reco::GsfElectron>& ele,
double rho,
float unbiased,
float field_z,
const reco::Track* trk) const {
auto iter = std::find(names_.begin(), names_.end(), name);
if (iter != names_.end()) {
int index = std::distance(names_.begin(), iter);
Expand All @@ -185,7 +248,7 @@ double LowPtGsfElectronIDProducer::eval(
} else if (version_ == "V0") {
inputs = lowptgsfeleid::features_V0(*ele, rho, unbiased);
} else if (version_ == "V1") {
inputs = lowptgsfeleid::features_V1(*ele, rho, unbiased, field_z);
inputs = lowptgsfeleid::features_V1(*ele, rho, unbiased, field_z, trk);
}
return models_.at(index)->GetResponse(inputs.data());
} else {
Expand All @@ -198,8 +261,10 @@ double LowPtGsfElectronIDProducer::eval(
//
void LowPtGsfElectronIDProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<bool>("useGsfToTrack", false);
desc.add<bool>("usePAT", false);
desc.add<edm::InputTag>("electrons", edm::InputTag("lowPtGsfElectrons"));
desc.addOptional<edm::InputTag>("gsfToTrack", edm::InputTag("lowPtGsfToTrackLinks"));
desc.addOptional<edm::InputTag>("unbiased", edm::InputTag("lowPtGsfElectronSeedValueMaps:unbiased"));
desc.add<edm::InputTag>("rho", edm::InputTag("fixedGridRhoFastjetAllTmp"));
desc.add<std::vector<std::string> >("ModelNames", std::vector<std::string>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
lowPtGsfElectronID,
ModelWeights = ["RecoEgamma/ElectronIdentification/data/LowPtElectrons/LowPtElectrons_ID_2021May17.root"],
)

from Configuration.Eras.Modifier_fastSim_cff import fastSim
from Configuration.ProcessModifiers.run2_miniAOD_UL_cff import run2_miniAOD_UL
from PhysicsTools.NanoAOD.nano_eras_cff import *
(fastSim & (run2_miniAOD_UL | run2_nanoAOD_106Xv2)).toModify(
lowPtGsfElectronID,
useGsfToTrack = True,
)
25 changes: 12 additions & 13 deletions RecoEgamma/EgammaElectronProducers/src/LowPtGsfElectronFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace lowptgsfeleid {

std::vector<float> features_V1(reco::GsfElectron const& ele, float rho, float unbiased, float field_z) {
std::vector<float> features_V1(
reco::GsfElectron const& ele, float rho, float unbiased, float field_z, const reco::Track* trk) {
float eid_rho = -999.;
float eid_sc_eta = -999.;
float eid_shape_full5x5_r9 = -999.;
Expand Down Expand Up @@ -47,18 +48,16 @@ namespace lowptgsfeleid {
float sc_clus2_E_ov_p = -999.;

// KF tracks
if (ele.core().isNonnull()) {
reco::TrackRef trk = ele.closestCtfTrackRef();
if (trk.isNonnull()) {
eid_trk_p = (float)trk->p();
eid_trk_nhits = (float)trk->found();
eid_trk_chi2red = (float)trk->normalizedChi2();
TVector3 trkTV3(0, 0, 0);
trkTV3.SetPtEtaPhi(trk->pt(), trk->eta(), trk->phi());
TVector3 eleTV3(0, 0, 0);
eleTV3.SetPtEtaPhi(ele.pt(), ele.eta(), ele.phi());
trk_dr = eleTV3.DeltaR(trkTV3);
}
if (trk != nullptr || (ele.core().isNonnull() && ele.closestCtfTrackRef().isNonnull())) {
const reco::Track* tk = trk ? trk : &*ele.closestCtfTrackRef();
eid_trk_p = tk->p();
eid_trk_nhits = tk->found();
eid_trk_chi2red = tk->normalizedChi2();
TVector3 trkTV3(0, 0, 0);
trkTV3.SetPtEtaPhi(tk->pt(), tk->eta(), tk->phi());
TVector3 eleTV3(0, 0, 0);
eleTV3.SetPtEtaPhi(ele.pt(), ele.eta(), ele.phi());
trk_dr = reco::deltaR(*tk, ele);
}

// GSF tracks
Expand Down

0 comments on commit 39546b2

Please sign in to comment.