Skip to content

Commit

Permalink
Merge pull request #29919 from slava77/CMSSW_10_6_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 (backport of #29918 )
  • Loading branch information
cmsbuild authored May 21, 2020
2 parents 4b13b79 + 89e72b5 commit 4113c6a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 122 deletions.
4 changes: 2 additions & 2 deletions CommonTools/CandAlgos/plugins/CandPtrProjector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ CandPtrProjector::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup con
if (vetoedPtrs.find(c)==vetoedPtrs.cend()) {
bool addcand = true;
if (useDeltaRforFootprint_)
for( const auto& it : vetoedPtrs)
if ((it.isNonnull()) && (it.isAvailable()) && (reco::deltaR2(it->p4(), c->p4()) < 0.00000025)) {
for (const auto& it : vetoedPtrs)
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void pat::LeptonUpdater<T>::produce(edm::StreamID, edm::Event& iEvent, edm::Even
setDZ(lep, pv);
if (computeMiniIso_) {
const auto & params = miniIsoParams(lep);
pat::PFIsolation miniiso = pat::getMiniPFIsolation(pc.product(), lep.p4(),
pat::PFIsolation miniiso = pat::getMiniPFIsolation(pc.product(), lep.polarP4(),
params[0], params[1], params[2],
params[3], params[4], params[5],
params[6], params[7], params[8]);
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/PatAlgos/plugins/PATElectronProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -973,12 +973,12 @@ void PATElectronProducer::setElectronMiniIso(Electron& anElectron, const PackedC
{
pat::PFIsolation miniiso;
if(anElectron.isEE())
miniiso = pat::getMiniPFIsolation(pc, anElectron.p4(),
miniiso = pat::getMiniPFIsolation(pc, anElectron.polarP4(),
miniIsoParamsE_[0], miniIsoParamsE_[1], miniIsoParamsE_[2],
miniIsoParamsE_[3], miniIsoParamsE_[4], miniIsoParamsE_[5],
miniIsoParamsE_[6], miniIsoParamsE_[7], miniIsoParamsE_[8]);
else
miniiso = pat::getMiniPFIsolation(pc, anElectron.p4(),
miniiso = pat::getMiniPFIsolation(pc, anElectron.polarP4(),
miniIsoParamsB_[0], miniIsoParamsB_[1], miniIsoParamsB_[2],
miniIsoParamsB_[3], miniIsoParamsB_[4], miniIsoParamsB_[5],
miniIsoParamsB_[6], miniIsoParamsB_[7], miniIsoParamsB_[8]);
Expand Down
81 changes: 43 additions & 38 deletions PhysicsTools/PatAlgos/plugins/PATIsolatedTrackProducer.cc
Original file line number Diff line number Diff line change
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,20 +52,20 @@ namespace pat {
void produce(edm::Event&, const edm::EventSetup&) override;

// compute iso/miniiso
void getIsolation(const LorentzVector& p4, const pat::PackedCandidateCollection* pc, int pc_idx,
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, const pat::PackedCandidateCollection *pc, int pc_idx, int& pc_ref_idx) const;
void getNearestPCRef(const PolarLorentzVector& p4, const pat::PackedCandidateCollection *pc, int pc_idx, int& pc_ref_idx) const;

float getDeDx(const reco::DeDxHitInfo *hitInfo, bool doPixel, bool doStrip) const ;

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 @@ -239,6 +240,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 @@ -249,18 +251,21 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
// get the four-momentum and charge
if(isInPackedCands){
p4 = pfCand.p4();
polarP4 = pfCand.polarP4();
charge = pfCand.charge();
pfCandInd = pcref.key();
ltCandInd = -1;
}else if(isInLostTracks){
p4 = lostTrack.p4();
polarP4 = lostTrack.polarP4();
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 @@ -274,21 +279,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 &&
if( polarP4.pt() < pT_cut_noIso_ && prescaled <= 1 &&
!(isolationDR03.chargedHadronIso() < absIso_cut_ ||
isolationDR03.chargedHadronIso()/p4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso()/p4.pt() < miniRelIso_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 @@ -319,20 +324,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 @@ -405,29 +410,29 @@ 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_ &&
if( polarP4.pt() < pT_cut_noIso_ &&
!(isolationDR03.chargedHadronIso() < absIso_cut_ ||
isolationDR03.chargedHadronIso()/p4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso()/p4.pt() < miniRelIso_cut_))
isolationDR03.chargedHadronIso()/polarP4.pt() < relIso_cut_ ||
miniIso.chargedHadronIso()/polarP4.pt() < miniRelIso_cut_))
continue;

pdgId = pfCand.pdgId();
Expand All @@ -444,20 +449,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 @@ -470,7 +475,7 @@ void pat::PATIsolatedTrackProducer::produce(edm::Event& iEvent, const edm::Event
int deltaEta=0;
int deltaPhi=0;

outPtrP->push_back(pat::IsolatedTrack(isolationDR03, miniIso, caloJetEm, caloJetHad, pfLepOverlap, pfNeutralSum, p4,
outPtrP->push_back(pat::IsolatedTrack(isolationDR03, miniIso, caloJetEm, caloJetHad, pfLepOverlap, pfNeutralSum, pfCand.p4(),
charge, pdgId, dz, dxy, dzError, dxyError,
hp, dEdxStrip, dEdxPixel, fromPV, trackQuality,
ecalStatus, hcalStatus, deltaEta, deltaPhi, refToCand,
Expand All @@ -492,7 +497,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, pat::PFIsolation &miniiso) const
{
Expand All @@ -505,7 +510,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 @@ -540,7 +545,7 @@ void pat::PATIsolatedTrackProducer::getIsolation(const LorentzVector& p4,
}

//get overlap of isolated track with a PF lepton
bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const LorentzVector& p4, const pat::PackedCandidateCollection *pc) const
bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const PolarLorentzVector& p4, const pat::PackedCandidateCollection *pc) const
{
bool isOverlap = false;
float dr_min = pflepoverlap_DR_;
Expand All @@ -557,7 +562,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 @@ -575,7 +580,7 @@ bool pat::PATIsolatedTrackProducer::getPFLeptonOverlap(const LorentzVector& p4,
}

//get ref to nearest pf packed candidate
void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4, const pat::PackedCandidateCollection *pc, int pc_idx, int& pc_ref_idx) const
void pat::PATIsolatedTrackProducer::getNearestPCRef(const PolarLorentzVector& p4, const pat::PackedCandidateCollection *pc, int pc_idx, int& pc_ref_idx) const
{
float dr_min = pcRefNearest_DR_;
float dr_min_pu = pcRefNearest_DR_;
Expand All @@ -586,7 +591,7 @@ void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4, con
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 @@ -613,7 +618,7 @@ void pat::PATIsolatedTrackProducer::getNearestPCRef(const LorentzVector& p4, con
}

// get PF neutral pT sum around isolated track
float pat::PATIsolatedTrackProducer::getPFNeutralSum(const LorentzVector& p4, const pat::PackedCandidateCollection *pc, int pc_idx) const
float pat::PATIsolatedTrackProducer::getPFNeutralSum(const PolarLorentzVector& p4, const pat::PackedCandidateCollection *pc, int pc_idx) const
{
float nsum=0;
float nhsum=0, phsum=0;
Expand All @@ -622,7 +627,7 @@ float pat::PATIsolatedTrackProducer::getPFNeutralSum(const LorentzVector& p4, co
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 @@ -698,13 +703,13 @@ TrackDetMatchInfo pat::PATIsolatedTrackProducer::getTrackDetMatchInfo(const edm:
return trackAssociator_.associate(iEvent, iSetup, trackAssocParameters_, &initialState);
}

void pat::PATIsolatedTrackProducer::getCaloJetEnergy(const LorentzVector& p4, const reco::CaloJetCollection* cJets,
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
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,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 @@ -885,8 +885,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
14 changes: 7 additions & 7 deletions PhysicsTools/PatUtils/interface/MiniIsolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

namespace pat{

float miniIsoDr(const math::XYZTLorentzVector &p4, float mindr, float maxdr,
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,
float mindr=0.05, float maxdr=0.2, float kt_scale=10.0,
float ptthresh=0.5, float deadcone_ch=0.0001,
float deadcone_pu=0.01, float deadcone_ph=0.01, float deadcone_nh=0.01,
float dZ_cut=0.0);
PFIsolation getMiniPFIsolation(const pat::PackedCandidateCollection *pfcands, const reco::Candidate::PolarLorentzVector& p4,
float mindr=0.05, float maxdr=0.2, float kt_scale=10.0,
float ptthresh=0.5, float deadcone_ch=0.0001,
float deadcone_pu=0.01, float deadcone_ph=0.01, float deadcone_nh=0.01,
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
Loading

0 comments on commit 4113c6a

Please sign in to comment.