Skip to content

Commit

Permalink
Merge pull request #29918 from slava77/CMSSW_11_1_X_2020-05-19-1100/s…
Browse files Browse the repository at this point in the history
…ign1103/deltaR-p4Polar

optimize some hotspot use cases of deltaR and polar in reco/miniAOD
  • Loading branch information
cmsbuild committed May 20, 2020
2 parents ae84fe5 + 569cecc commit 2fdd4f0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CommonTools/CandAlgos/plugins/CandPtrProjector.cc
Expand Up @@ -50,7 +50,7 @@ void CandPtrProjector::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetu
bool addcand = true;
if (useDeltaRforFootprint_)
for (const auto& it : vetoedPtrs)
if ((it.isNonnull()) && (it.isAvailable()) && (reco::deltaR2(it->p4(), c->p4()) < 0.00000025)) {
if (it.isNonnull() && it.isAvailable() && reco::deltaR2(*it, *c) < 0.00000025) {
addcand = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion PhysicsTools/PatAlgos/plugins/LeptonUpdater.cc
Expand Up @@ -162,7 +162,7 @@ void pat::LeptonUpdater<T>::produce(edm::StreamID, edm::Event &iEvent, edm::Even
if (computeMiniIso_) {
const auto &params = miniIsoParams(lep);
pat::PFIsolation miniiso = pat::getMiniPFIsolation(pc.product(),
lep.p4(),
lep.polarP4(),
params[0],
params[1],
params[2],
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/PatAlgos/plugins/PATElectronProducer.cc
Expand Up @@ -991,7 +991,7 @@ void PATElectronProducer::setElectronMiniIso(Electron& anElectron, const PackedC
pat::PFIsolation miniiso;
if (anElectron.isEE())
miniiso = pat::getMiniPFIsolation(pc,
anElectron.p4(),
anElectron.polarP4(),
miniIsoParamsE_[0],
miniIsoParamsE_[1],
miniIsoParamsE_[2],
Expand All @@ -1003,7 +1003,7 @@ void PATElectronProducer::setElectronMiniIso(Electron& anElectron, const PackedC
miniIsoParamsE_[8]);
else
miniiso = pat::getMiniPFIsolation(pc,
anElectron.p4(),
anElectron.polarP4(),
miniIsoParamsB_[0],
miniIsoParamsB_[1],
miniIsoParamsB_[2],
Expand Down
82 changes: 44 additions & 38 deletions PhysicsTools/PatAlgos/plugins/PATIsolatedTrackProducer.cc
Expand Up @@ -43,6 +43,7 @@ namespace pat {

class PATIsolatedTrackProducer : public edm::stream::EDProducer<> {
public:
typedef pat::IsolatedTrack::PolarLorentzVector PolarLorentzVector;
typedef pat::IsolatedTrack::LorentzVector LorentzVector;

explicit PATIsolatedTrackProducer(const edm::ParameterSet&);
Expand All @@ -51,17 +52,17 @@ namespace pat {
void produce(edm::Event&, const edm::EventSetup&) override;

// compute iso/miniiso
void getIsolation(const LorentzVector& p4,
void getIsolation(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc,
int pc_idx,
pat::PFIsolation& iso,
pat::PFIsolation& miniiso) const;

bool getPFLeptonOverlap(const LorentzVector& p4, const pat::PackedCandidateCollection* pc) const;
bool getPFLeptonOverlap(const PolarLorentzVector& p4, const pat::PackedCandidateCollection* pc) const;

float getPFNeutralSum(const LorentzVector& p4, const pat::PackedCandidateCollection* pc, int pc_idx) const;
float getPFNeutralSum(const PolarLorentzVector& p4, const pat::PackedCandidateCollection* pc, int pc_idx) const;

void getNearestPCRef(const LorentzVector& p4,
void getNearestPCRef(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc,
int pc_idx,
int& pc_ref_idx) const;
Expand All @@ -70,7 +71,7 @@ namespace pat {

TrackDetMatchInfo getTrackDetMatchInfo(const edm::Event&, const edm::EventSetup&, const reco::Track&);

void getCaloJetEnergy(const LorentzVector&, const reco::CaloJetCollection*, float&, float&) const;
void getCaloJetEnergy(const PolarLorentzVector&, const reco::CaloJetCollection*, float&, float&) const;

private:
const edm::EDGetTokenT<pat::PackedCandidateCollection> pc_;
Expand Down Expand Up @@ -248,6 +249,7 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
bool isInPackedCands = (pcref.isNonnull() && pcref.id() == pc_h.id() && pfCand->charge() != 0);
bool isInLostTracks = (ltref.isNonnull() && ltref.id() == lt_h.id());

PolarLorentzVector polarP4;
LorentzVector p4;
pat::PackedCandidateRef refToCand;
int pdgId, charge, fromPV;
Expand All @@ -258,18 +260,21 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
// get the four-momentum and charge
if (isInPackedCands) {
p4 = pfCand->p4();
polarP4 = pfCand->p4();
charge = pfCand->charge();
pfCandInd = pcref.key();
ltCandInd = -1;
} else if (isInLostTracks) {
p4 = lostTrack->p4();
polarP4 = lostTrack->p4();
charge = lostTrack->charge();
pfCandInd = -1;
ltCandInd = ltref.key();
} else {
double m = 0.13957018; //assume pion mass
double E = sqrt(m * m + gentk.p() * gentk.p());
p4.SetPxPyPzE(gentk.px(), gentk.py(), gentk.pz(), E);
polarP4.SetCoordinates(gentk.pt(), gentk.eta(), gentk.phi(), m);
charge = gentk.charge();
pfCandInd = -1;
ltCandInd = -1;
Expand All @@ -283,20 +288,21 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
}
}

if (p4.pt() < pT_cut_ && prescaled <= 1)
if (polarP4.pt() < pT_cut_ && prescaled <= 1)
continue;
if (charge == 0)
continue;

// get the isolation of the track
pat::PFIsolation isolationDR03;
pat::PFIsolation miniIso;
getIsolation(p4, pc, pfCandInd, isolationDR03, miniIso);
getIsolation(polarP4, pc, pfCandInd, isolationDR03, miniIso);

// isolation cut
if (p4.pt() < pT_cut_noIso_ && prescaled <= 1 &&
!(isolationDR03.chargedHadronIso() < absIso_cut_ || isolationDR03.chargedHadronIso() / p4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso() / p4.pt() < miniRelIso_cut_))
if (polarP4.pt() < pT_cut_noIso_ && prescaled <= 1 &&
!(isolationDR03.chargedHadronIso() < absIso_cut_ ||
isolationDR03.chargedHadronIso() / polarP4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso() / polarP4.pt() < miniRelIso_cut_))
continue;

// get the rest after the pt/iso cuts. Saves some runtime
Expand Down Expand Up @@ -327,20 +333,20 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
}

float caloJetEm, caloJetHad;
getCaloJetEnergy(p4, caloJets.product(), caloJetEm, caloJetHad);
getCaloJetEnergy(polarP4, caloJets.product(), caloJetEm, caloJetHad);

bool pfLepOverlap = getPFLeptonOverlap(p4, pc);
float pfNeutralSum = getPFNeutralSum(p4, pc, pfCandInd);
bool pfLepOverlap = getPFLeptonOverlap(polarP4, pc);
float pfNeutralSum = getPFNeutralSum(polarP4, pc, pfCandInd);

pat::PackedCandidateRef refToNearestPF = pat::PackedCandidateRef();
int refToNearestPF_idx = -1;
getNearestPCRef(p4, pc, pfCandInd, refToNearestPF_idx);
getNearestPCRef(polarP4, pc, pfCandInd, refToNearestPF_idx);
if (refToNearestPF_idx != -1)
refToNearestPF = pat::PackedCandidateRef(pc_h, refToNearestPF_idx);

pat::PackedCandidateRef refToNearestLostTrack = pat::PackedCandidateRef();
int refToNearestLostTrack_idx = -1;
getNearestPCRef(p4, lt, ltCandInd, refToNearestLostTrack_idx);
getNearestPCRef(polarP4, lt, ltCandInd, refToNearestLostTrack_idx);
if (refToNearestLostTrack_idx != -1)
refToNearestLostTrack = pat::PackedCandidateRef(lt_h, refToNearestLostTrack_idx);

Expand Down Expand Up @@ -435,28 +441,28 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
if (pfref.get()->trackRef().isNonnull() && pfref.get()->trackRef().id() == gt_h.id())
continue;

LorentzVector p4;
PolarLorentzVector polarP4;
pat::PackedCandidateRef refToCand;
int pdgId, charge, fromPV;
float dz, dxy, dzError, dxyError;

p4 = pfCand.p4();
polarP4 = pfCand.polarP4();
charge = pfCand.charge();

if (p4.pt() < pT_cut_)
if (polarP4.pt() < pT_cut_)
continue;
if (charge == 0)
continue;

// get the isolation of the track
pat::PFIsolation isolationDR03;
pat::PFIsolation miniIso;
getIsolation(p4, pc, ipc, isolationDR03, miniIso);
getIsolation(polarP4, pc, ipc, isolationDR03, miniIso);

// isolation cut
if (p4.pt() < pT_cut_noIso_ &&
!(isolationDR03.chargedHadronIso() < absIso_cut_ || isolationDR03.chargedHadronIso() / p4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso() / p4.pt() < miniRelIso_cut_))
if (polarP4.pt() < pT_cut_noIso_ && !(isolationDR03.chargedHadronIso() < absIso_cut_ ||
isolationDR03.chargedHadronIso() / polarP4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso() / polarP4.pt() < miniRelIso_cut_))
continue;

pdgId = pfCand.pdgId();
Expand All @@ -473,20 +479,20 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
refToCand = pcref;

float caloJetEm, caloJetHad;
getCaloJetEnergy(p4, caloJets.product(), caloJetEm, caloJetHad);
getCaloJetEnergy(polarP4, caloJets.product(), caloJetEm, caloJetHad);

bool pfLepOverlap = getPFLeptonOverlap(p4, pc);
float pfNeutralSum = getPFNeutralSum(p4, pc, ipc);
bool pfLepOverlap = getPFLeptonOverlap(polarP4, pc);
float pfNeutralSum = getPFNeutralSum(polarP4, pc, ipc);

pat::PackedCandidateRef refToNearestPF = pat::PackedCandidateRef();
int refToNearestPF_idx = -1;
getNearestPCRef(p4, pc, ipc, refToNearestPF_idx);
getNearestPCRef(polarP4, pc, ipc, refToNearestPF_idx);
if (refToNearestPF_idx != -1)
refToNearestPF = pat::PackedCandidateRef(pc_h, refToNearestPF_idx);

pat::PackedCandidateRef refToNearestLostTrack = pat::PackedCandidateRef();
int refToNearestLostTrack_idx = -1;
getNearestPCRef(p4, lt, -1, refToNearestLostTrack_idx);
getNearestPCRef(polarP4, lt, -1, refToNearestLostTrack_idx);
if (refToNearestLostTrack_idx != -1)
refToNearestLostTrack = pat::PackedCandidateRef(lt_h, refToNearestLostTrack_idx);

Expand All @@ -505,7 +511,7 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
caloJetHad,
pfLepOverlap,
pfNeutralSum,
p4,
pfCand.p4(),
charge,
pdgId,
dz,
Expand Down Expand Up @@ -539,7 +545,7 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
}
}

void pat::PATIsolatedTrackProducer::getIsolation(const LorentzVector& p4,
void pat::PATIsolatedTrackProducer::getIsolation(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc,
int pc_idx,
pat::PFIsolation& iso,
Expand All @@ -553,7 +559,7 @@ void pat::PATIsolatedTrackProducer::getIsolation(const LorentzVector& p4,
int id = std::abs(pf_it->pdgId());
bool fromPV = (pf_it->fromPV() > 1 || fabs(pf_it->dz()) < pfIsolation_DZ_);
float pt = pf_it->p4().pt();
float dr = deltaR(p4, pf_it->p4());
float dr = deltaR(p4, *pf_it);

if (dr < pfIsolation_DR_) {
// charged cands from PV get added to trackIso
Expand Down Expand Up @@ -587,7 +593,7 @@ void pat::PATIsolatedTrackProducer::getIsolation(const LorentzVector& p4,
}

//get overlap of isolated track with a PF lepton
bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const LorentzVector& p4,
bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc) const {
bool isOverlap = false;
float dr_min = pflepoverlap_DR_;
Expand All @@ -604,7 +610,7 @@ bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const LorentzVector& p4,
if (pt < pflepoverlap_pTmin_) // exclude pf candidates w/ pT below threshold
continue;

float dr = deltaR(p4, pf.p4());
float dr = deltaR(p4, pf);
if (dr > pflepoverlap_DR_) // exclude pf candidates far from isolated track
continue;

Expand All @@ -621,7 +627,7 @@ bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const LorentzVector& p4,
}

//get ref to nearest pf packed candidate
void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4,
void pat::PATIsolatedTrackProducer::getNearestPCRef(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc,
int pc_idx,
int& pc_ref_idx) const {
Expand All @@ -634,7 +640,7 @@ void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4,
int charge = std::abs(pf_it->charge());
bool fromPV = (pf_it->fromPV() > 1 || fabs(pf_it->dz()) < pfIsolation_DZ_);
float pt = pf_it->p4().pt();
float dr = deltaR(p4, pf_it->p4());
float dr = deltaR(p4, *pf_it);
if (charge == 0) // exclude neutral candidates
continue;
if (pt < pcRefNearest_pTmin_) // exclude candidates w/ pT below threshold
Expand All @@ -661,7 +667,7 @@ void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4,
}

// get PF neutral pT sum around isolated track
float pat::PATIsolatedTrackProducer::getPFNeutralSum(const LorentzVector& p4,
float pat::PATIsolatedTrackProducer::getPFNeutralSum(const PolarLorentzVector& p4,
const pat::PackedCandidateCollection* pc,
int pc_idx) const {
float nsum = 0;
Expand All @@ -671,7 +677,7 @@ float pat::PATIsolatedTrackProducer::getPFNeutralSum(const LorentzVector& p4,
continue;
int id = std::abs(pf_it->pdgId());
float pt = pf_it->p4().pt();
float dr = deltaR(p4, pf_it->p4());
float dr = deltaR(p4, *pf_it);

if (dr < pfneutralsum_DR_) {
// neutral hadron sum
Expand Down Expand Up @@ -747,14 +753,14 @@ TrackDetMatchInfo pat::PATIsolatedTrackProducer::getTrackDetMatchInfo(const edm:
return trackAssociator_.associate(iEvent, iSetup, trackAssocParameters_, &initialState);
}

void pat::PATIsolatedTrackProducer::getCaloJetEnergy(const LorentzVector& p4,
void pat::PATIsolatedTrackProducer::getCaloJetEnergy(const PolarLorentzVector& p4,
const reco::CaloJetCollection* cJets,
float& caloJetEm,
float& caloJetHad) const {
float nearestDR = 999;
int ind = -1;
for (unsigned int i = 0; i < cJets->size(); i++) {
float dR = deltaR(cJets->at(i).p4(), p4);
float dR = deltaR(cJets->at(i), p4);
if (dR < caloJet_DR_ && dR < nearestDR) {
nearestDR = dR;
ind = i;
Expand Down
6 changes: 3 additions & 3 deletions PhysicsTools/PatAlgos/plugins/PATMuonProducer.cc
Expand Up @@ -876,7 +876,7 @@ void PATMuonProducer::fillMuon(Muon& aMuon,

void PATMuonProducer::setMuonMiniIso(Muon& aMuon, const PackedCandidateCollection* pc) {
pat::PFIsolation miniiso = pat::getMiniPFIsolation(pc,
aMuon.p4(),
aMuon.polarP4(),
miniIsoParams_[0],
miniIsoParams_[1],
miniIsoParams_[2],
Expand All @@ -893,8 +893,8 @@ double PATMuonProducer::getRelMiniIsoPUCorrected(const pat::Muon& muon, double r
double mindr(miniIsoParams_[0]);
double maxdr(miniIsoParams_[1]);
double kt_scale(miniIsoParams_[2]);
double drcut = pat::miniIsoDr(muon.p4(), mindr, maxdr, kt_scale);
return pat::muonRelMiniIsoPUCorrected(muon.miniPFIsolation(), muon.p4(), drcut, rho, area);
double drcut = pat::miniIsoDr(muon.polarP4(), mindr, maxdr, kt_scale);
return pat::muonRelMiniIsoPUCorrected(muon.miniPFIsolation(), muon.polarP4(), drcut, rho, area);
}

double PATMuonProducer::puppiCombinedIsolation(const pat::Muon& muon, const pat::PackedCandidateCollection* pc) {
Expand Down
6 changes: 3 additions & 3 deletions PhysicsTools/PatUtils/interface/MiniIsolation.h
Expand Up @@ -15,11 +15,11 @@

namespace pat {

float miniIsoDr(const math::XYZTLorentzVector& p4, float mindr, float maxdr, float kt_scale);
float miniIsoDr(const reco::Candidate::PolarLorentzVector& p4, float mindr, float maxdr, float kt_scale);

// see src file for definitions of parameters
PFIsolation getMiniPFIsolation(const pat::PackedCandidateCollection* pfcands,
const math::XYZTLorentzVector& p4,
const reco::Candidate::PolarLorentzVector& p4,
float mindr = 0.05,
float maxdr = 0.2,
float kt_scale = 10.0,
Expand All @@ -31,7 +31,7 @@ namespace pat {
float dZ_cut = 0.0);

double muonRelMiniIsoPUCorrected(const PFIsolation& iso,
const math::XYZTLorentzVector& p4,
const reco::Candidate::PolarLorentzVector& p4,
double dr,
double rho,
const std::vector<double>& area);
Expand Down

0 comments on commit 2fdd4f0

Please sign in to comment.