Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PUPPI MET tails #17072

Merged
merged 4 commits into from Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 45 additions & 36 deletions CommonTools/PileupAlgos/plugins/PuppiPhoton.cc
Expand Up @@ -31,17 +31,22 @@ PuppiPhoton::PuppiPhoton(const edm::ParameterSet& iConfig) {
tokenPFCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
tokenPuppiCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("puppiCandName"));
tokenPhotonCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("photonName"));
tokenPhotonId_ = consumes<edm::ValueMap<bool> >(iConfig.getParameter<edm::InputTag>("photonId"));
usePhotonId_ = (iConfig.getParameter<edm::InputTag>("photonId")).label().size() != 0;
if(usePhotonId_)
tokenPhotonId_ = consumes<edm::ValueMap<bool> >(iConfig.getParameter<edm::InputTag>("photonId"));
runOnMiniAOD_ = iConfig.getParameter<bool>("runOnMiniAOD");
if(!runOnMiniAOD_)
reco2pf_ = consumes<edm::ValueMap<std::vector<reco::PFCandidateRef> > >(iConfig.getParameter<edm::InputTag>("recoToPFMap"));
useValueMap_ = iConfig.getParameter<bool>("useValueMap");
if(useValueMap_)
tokenWeights_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("weightsName"));

pt_ = iConfig.getParameter<double>("pt");
eta_ = iConfig.getParameter<double>("eta");
dRMatch_ = iConfig.getParameter<std::vector<double> > ("dRMatch");
pdgIds_ = iConfig.getParameter<std::vector<int32_t> >("pdgids");
usePFRef_ = iConfig.getParameter<bool>("useRefs");
weight_ = iConfig.getParameter<double>("weight");
useValueMap_ = iConfig.getParameter<bool>("useValueMap");
tokenWeights_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("weightsName"));

usePhotonId_ = (iConfig.getParameter<edm::InputTag>("photonId")).label().size() == 0;
produces<PFOutputCollection>();
produces< edm::ValueMap<reco::CandidatePtr> >();
}
Expand All @@ -60,6 +65,9 @@ void PuppiPhoton::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
std::vector<const reco::Candidate*> phoCands;
std::vector<uint16_t> phoIndx;

edm::Handle<edm::ValueMap<std::vector<reco::PFCandidateRef>>> reco2pf;
if(!runOnMiniAOD_) iEvent.getByToken(reco2pf_, reco2pf);

// Get PFCandidate Collection
edm::Handle<CandidateView> hPFProduct;
iEvent.getByToken(tokenPFCandidates_,hPFProduct);
Expand All @@ -74,38 +82,46 @@ void PuppiPhoton::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
if(itPho->isPhoton() && usePhotonId_) passObject = (*photonId) [phoCol->ptrAt(iC)];
if(itPho->pt() < pt_) continue;
if(!passObject && usePhotonId_) continue;
//if(!usePFRef_ && fabs(itPho->eta()) < eta_) phoCands.push_back(&(*itPho)); ===> should add a flag useAODRef_ in place of usePFRef_
//if(!usePFRef_) continue;
const pat::Photon *pPho = dynamic_cast<const pat::Photon*>(&(*itPho));
if(pPho != 0) {
for( const edm::Ref<pat::PackedCandidateCollection> & ref : pPho->associatedPackedPFCandidates() ) {
if(fabs(pfCol->ptrAt(ref.key())->eta()) < eta_ ) {
phoIndx.push_back(ref.key());
phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
}
if(runOnMiniAOD_) {
const pat::Photon *pPho = dynamic_cast<const pat::Photon*>(&(*itPho));
if(pPho != 0) {
for( const edm::Ref<pat::PackedCandidateCollection> & ref : pPho->associatedPackedPFCandidates() ) {
if(fabs(ref->eta()) < eta_ ) {
phoIndx.push_back(ref.key());
phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
}
}
continue;
}
const pat::Electron *pElectron = dynamic_cast<const pat::Electron*>(&(*itPho));
if(pElectron != 0) {
for( const edm::Ref<pat::PackedCandidateCollection> & ref : pElectron->associatedPackedPFCandidates() )
if(fabs(ref->eta()) < eta_ ) {
phoIndx.push_back(ref.key());
phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
}
}
} else {
for( const edm::Ref<std::vector<reco::PFCandidate> > & ref : (*reco2pf)[phoCol->ptrAt(iC)] ) {
if(fabs(ref->eta()) < eta_ ) {
phoIndx.push_back(ref.key());
phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
}
}
continue;
}
const pat::Electron *pElectron = dynamic_cast<const pat::Electron*>(&(*itPho));
if(pElectron != 0) {
for( const edm::Ref<pat::PackedCandidateCollection> & ref : pElectron->associatedPackedPFCandidates() )
if(fabs(pfCol->ptrAt(ref.key())->eta()) < eta_ ) {
phoIndx.push_back(ref.key());
phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
}
}
}
//Get Weights
edm::Handle<edm::ValueMap<float> > pupWeights;
iEvent.getByToken(tokenWeights_,pupWeights);
if(useValueMap_)
iEvent.getByToken(tokenWeights_,pupWeights);
std::unique_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
LorentzVectorCollection puppiP4s;
std::vector<reco::CandidatePtr> values(hPFProduct->size());
int iPF = 0;
std::vector<float> lWeights;
static const reco::PFCandidate dummySinceTranslateIsNotStatic;
corrCandidates_.reset( new PFOutputCollection );
std::vector<int> foundPhoIndex;
std::set<int> foundPhoIndex;
for(CandidateView::const_iterator itPF = pupCol->begin(); itPF!=pupCol->end(); itPF++) {
auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(itPF->pdgId());
const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&(*itPF));
Expand All @@ -117,11 +133,11 @@ void PuppiPhoton::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
int iPho = -1;
for(std::vector<const reco::Candidate*>::iterator itPho = phoCands.begin(); itPho!=phoCands.end(); itPho++) {
iPho++;
if(!matchPFCandidate(&(*itPF),*itPho)) continue;
if((!matchPFCandidate(&(*itPF),*itPho))||(foundPhoIndex.count(iPho)!=0)) continue;
pWeight = weight_;
if(!useValueMap_ && itPF->pt() != 0) pWeight = pWeight*(phoCands[iPho]->pt()/itPF->pt());
if(!useValueMap_ && itPF->pt() == 0) pVec.SetPxPyPzE(phoCands[iPho]->px()*pWeight,phoCands[iPho]->py()*pWeight,phoCands[iPho]->pz()*pWeight,phoCands[iPho]->energy()*pWeight);
foundPhoIndex.push_back(iPho);
foundPhoIndex.insert(iPho);
}
} else {
int iPho = -1;
Expand All @@ -131,7 +147,7 @@ void PuppiPhoton::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
pWeight = weight_;
if(!useValueMap_ && itPF->pt() != 0) pWeight = pWeight*(phoCands[iPho]->pt()/itPF->pt());
if(!useValueMap_ && itPF->pt() == 0) pVec.SetPxPyPzE(phoCands[iPho]->px()*pWeight,phoCands[iPho]->py()*pWeight,phoCands[iPho]->pz()*pWeight,phoCands[iPho]->energy()*pWeight);
foundPhoIndex.push_back(iPho);
foundPhoIndex.insert(iPho);
}
}
if(itPF->pt() != 0) pVec.SetPxPyPzE(itPF->px()*pWeight,itPF->py()*pWeight,itPF->pz()*pWeight,itPF->energy()*pWeight);
Expand All @@ -145,14 +161,7 @@ void PuppiPhoton::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
}
//Add the missing pfcandidates
for(unsigned int iPho = 0; iPho < phoCands.size(); iPho++) {
bool pFound = false;
for(unsigned int jPho = 0; jPho < foundPhoIndex.size(); jPho++) {
if(foundPhoIndex[jPho] == int(iPho)) {
pFound = true;
break;
}
}
if(pFound) continue;
if(foundPhoIndex.count(iPho)!=0) continue;
auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(phoCands[iPho]->pdgId());
reco::PFCandidate pCand(reco::PFCandidate(phoCands[iPho]->charge(), phoCands[iPho]->p4(),id) );
pCand.setSourceCandidatePtr( phoCands[iPho]->sourceCandidatePtr(0) );
Expand Down
2 changes: 2 additions & 0 deletions CommonTools/PileupAlgos/plugins/PuppiPhoton.h
Expand Up @@ -34,11 +34,13 @@ class PuppiPhoton : public edm::stream::EDProducer<> {
edm::EDGetTokenT< CandidateView > tokenPFCandidates_;
edm::EDGetTokenT< CandidateView > tokenPuppiCandidates_;
edm::EDGetTokenT< CandidateView > tokenPhotonCandidates_;
edm::EDGetTokenT< edm::ValueMap<std::vector<reco::PFCandidateRef> > > reco2pf_;
edm::EDGetTokenT< edm::ValueMap<float> > tokenWeights_;
edm::EDGetTokenT< edm::ValueMap<bool> > tokenPhotonId_;
double pt_;
double eta_;
bool usePFRef_;
bool runOnMiniAOD_;
bool usePhotonId_;
std::vector<double> dRMatch_;
std::vector<int32_t> pdgIds_;
Expand Down
5 changes: 2 additions & 3 deletions CommonTools/PileupAlgos/plugins/PuppiProducer.cc
Expand Up @@ -200,9 +200,8 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
throw edm::Exception(edm::errors::LogicError,"PuppiProducer: cannot get weights since inputs are not PackedCandidates");
}
else{
// if (fUseWeightsNoLep){ curpupweight = itPF->puppiWeightNoLep(); }
// else{ curpupweight = itPF->puppiWeight(); }
curpupweight = lPack->puppiWeight();
if (fUseWeightsNoLep){ curpupweight = lPack->puppiWeightNoLep(); }
else{ curpupweight = lPack->puppiWeight(); }
}
lWeights.push_back(curpupweight);
fastjet::PseudoJet curjet( curpupweight*lPack->px(), curpupweight*lPack->py(), curpupweight*lPack->pz(), curpupweight*lPack->energy());
Expand Down
6 changes: 4 additions & 2 deletions CommonTools/PileupAlgos/python/PhotonPuppi_cff.py
Expand Up @@ -2,12 +2,14 @@
from PhysicsTools.SelectorUtils.tools.vid_id_tools import *

puppiPhoton = cms.EDProducer("PuppiPhoton",
candName = cms.InputTag('packedPFCandidates'),
candName = cms.InputTag('particleFlow'),
puppiCandName = cms.InputTag('puppi'),
photonName = cms.InputTag('slimmedPhotons'),
photonName = cms.InputTag('reducedEgamma','reducedGedPhotons'),
recoToPFMap = cms.InputTag("reducedEgamma","reducedPhotonPfCandMap"),
photonId = cms.InputTag("egmPhotonIDs:cutBasedPhotonID-Spring15-25ns-V1-standalone-loose"),
pt = cms.double(10),
eta = cms.double(2.5),
runOnMiniAOD = cms.bool(False),
useRefs = cms.bool(True),
dRMatch = cms.vdouble(0.005,0.005,0.005,0.005),
pdgids = cms.vint32 (22,11,211,130),
Expand Down
12 changes: 9 additions & 3 deletions PhysicsTools/PatAlgos/python/slimming/puppiForMET_cff.py
Expand Up @@ -21,7 +21,7 @@ def makePuppies( process ):
process.puppiMerged = cms.EDProducer("CandViewMerger",src = cms.VInputTag( 'puppiNoLep','pfLeptonsPUPPET'))
process.load('CommonTools.PileupAlgos.PhotonPuppi_cff')
process.puppiForMET = process.puppiPhoton.clone()
#Line below replaces reference linking wiht delta R matching this is because the reference key in packed candidates differs to PF candidates (must be done when reading Reco)
#Line below replaces reference linking wiht delta R matching because the puppi references after merging are not consistent with those of the original PF collection
process.puppiForMET.useRefs = False
#Line below points puppi MET to puppi no lepton which increases the response
process.puppiForMET.puppiCandName = 'puppiMerged'
Expand All @@ -32,18 +32,24 @@ def makePuppiesFromMiniAOD( process, createScheduledSequence=False ):
process.puppi.candName = cms.InputTag('packedPFCandidates')
process.puppi.clonePackedCands = cms.bool(True)
process.puppi.vertexName = cms.InputTag('offlineSlimmedPrimaryVertices')
process.puppi.useExistingWeights = cms.bool(True)
process.pfNoLepPUPPI = cms.EDFilter("CandPtrSelector", src = cms.InputTag("packedPFCandidates"), cut = cms.string("abs(pdgId) != 13 && abs(pdgId) != 11 && abs(pdgId) != 15"))
process.pfLeptonsPUPPET = cms.EDFilter("CandPtrSelector", src = cms.InputTag("packedPFCandidates"), cut = cms.string("abs(pdgId) == 13 || abs(pdgId) == 11 || abs(pdgId) == 15"))
process.puppiNoLep = process.puppi.clone()
process.puppiNoLep.candName = cms.InputTag('pfNoLepPUPPI')
process.puppiNoLep.useWeightsNoLep = cms.bool(True)
process.puppiNoLep.useExistingWeights = cms.bool(True)
process.puppiMerged = cms.EDProducer("CandViewMerger",src = cms.VInputTag( 'puppiNoLep','pfLeptonsPUPPET'))
process.load('CommonTools.PileupAlgos.PhotonPuppi_cff')
process.puppiForMET = process.puppiPhoton.clone()
process.puppiForMET.candName = cms.InputTag('packedPFCandidates')
process.puppiForMET.photonName = cms.InputTag('slimmedPhotons')
process.puppiForMET.runOnMiniAOD = cms.bool(True)
setupPuppiPhotonMiniAOD(process)
#Line below doesn't work because of an issue with references in MiniAOD without setting useRefs=>False and using delta R
process.puppiForMET.puppiCandName = 'puppiMerged'
#Line below replaces reference linking wiht delta R matching because the puppi references after merging are not consistent with those of the original packed candidate collection
process.puppiForMET.useRefs = False
#Line below points puppi MET to puppi no lepton which increases the response
process.puppiForMET.puppiCandName = 'puppiMerged'

#making a sequence for people running the MET tool in scheduled mode
if createScheduledSequence:
Expand Down