Skip to content

Commit

Permalink
merge conflict with master
Browse files Browse the repository at this point in the history
  • Loading branch information
ahinzmann committed Aug 31, 2021
1 parent 60eb2fb commit e10f0bf
Show file tree
Hide file tree
Showing 30 changed files with 501 additions and 58 deletions.
32 changes: 28 additions & 4 deletions CommonTools/ParticleFlow/plugins/PFPileUp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ PFPileUp::PFPileUp(const edm::ParameterSet& iConfig) {

tokenVertices_ = consumes<VertexCollection>(iConfig.getParameter<InputTag>("Vertices"));

fUseVertexAssociation = iConfig.getParameter<bool>("useVertexAssociation");
vertexAssociationQuality_ = iConfig.getParameter<int>("vertexAssociationQuality");
if (fUseVertexAssociation) {
tokenVertexAssociation_ = consumes<CandToVertex>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
tokenVertexAssociationQuality_ =
consumes<edm::ValueMap<int>>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
}

enable_ = iConfig.getParameter<bool>("Enable");

verbose_ = iConfig.getUntrackedParameter<bool>("verbose", false);
Expand Down Expand Up @@ -93,10 +101,26 @@ void PFPileUp::produce(Event& iEvent, const EventSetup& iSetup) {
"error.");
}

pileUpAlgo_.process(*pfCandidatesRef, *vertices);
pOutput->insert(
pOutput->end(), pileUpAlgo_.getPFCandidatesFromPU().begin(), pileUpAlgo_.getPFCandidatesFromPU().end());

if (fUseVertexAssociation) {
edm::Handle<edm::Association<reco::VertexCollection>> assoHandle;
iEvent.getByToken(tokenVertexAssociation_, assoHandle);
const edm::Association<reco::VertexCollection>* associatedPV = assoHandle.product();
edm::Handle<edm::ValueMap<int>> assoQualityHandle;
iEvent.getByToken(tokenVertexAssociationQuality_, assoQualityHandle);
const edm::ValueMap<int>* associationQuality = assoQualityHandle.product();
PFCollection pfCandidatesFromPU;
for (unsigned i = 0; i < (*pfCandidatesRef).size(); i++) {
const reco::VertexRef& PVOrig = (*associatedPV)[(*pfCandidatesRef)[i]];
int quality = (*associationQuality)[(*pfCandidatesRef)[i]];
if (PVOrig.isNonnull() && (PVOrig.key() > 0) && (quality >= vertexAssociationQuality_))
pfCandidatesFromPU.push_back((*pfCandidatesRef)[i]);
}
pOutput->insert(pOutput->end(), pfCandidatesFromPU.begin(), pfCandidatesFromPU.end());
} else {
pileUpAlgo_.process(*pfCandidatesRef, *vertices);
pOutput->insert(
pOutput->end(), pileUpAlgo_.getPFCandidatesFromPU().begin(), pileUpAlgo_.getPFCandidatesFromPU().end());
}
// for ( PFCollection::const_iterator byValueBegin = pileUpAlgo_.getPFCandidatesFromPU().begin(),
// byValueEnd = pileUpAlgo_.getPFCandidatesFromPU().end(), ibyValue = byValueBegin;
// ibyValue != byValueEnd; ++ibyValue ) {
Expand Down
9 changes: 8 additions & 1 deletion CommonTools/ParticleFlow/plugins/PFPileUp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "DataFormats/Common/interface/Association.h"

#include "CommonTools/ParticleFlow/interface/PFPileUpAlgo.h"

Expand All @@ -30,9 +31,10 @@ produces the corresponding collection of PileUpCandidates.

class PFPileUp : public edm::stream::EDProducer<> {
public:
typedef std::vector<edm::FwdPtr<reco::PFCandidate> > PFCollection;
typedef std::vector<edm::FwdPtr<reco::PFCandidate>> PFCollection;
typedef edm::View<reco::PFCandidate> PFView;
typedef std::vector<reco::PFCandidate> PFCollectionByValue;
typedef edm::Association<reco::VertexCollection> CandToVertex;

explicit PFPileUp(const edm::ParameterSet&);

Expand All @@ -59,6 +61,11 @@ class PFPileUp : public edm::stream::EDProducer<> {

/// use the closest z vertex if a track is not in a vertex
bool checkClosestZVertex_;

edm::EDGetTokenT<CandToVertex> tokenVertexAssociation_;
edm::EDGetTokenT<edm::ValueMap<int>> tokenVertexAssociationQuality_;
bool fUseVertexAssociation;
int vertexAssociationQuality_;
};

#endif
45 changes: 45 additions & 0 deletions CommonTools/ParticleFlow/plugins/PFnoPileUp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "CommonTools/ParticleFlow/plugins/PFnoPileUp.h"

#include "DataFormats/VertexReco/interface/Vertex.h"

#include "FWCore/Framework/interface/ESHandle.h"

// #include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Framework/interface/EventSetup.h"

using namespace std;
using namespace edm;
using namespace reco;

PFnoPileUp::PFnoPileUp(const edm::ParameterSet& iConfig) {
tokenCandidatesView_ = consumes<CandidateView>(iConfig.getParameter<InputTag>("candidates"));
vertexAssociationQuality_ = iConfig.getParameter<int>("vertexAssociationQuality");
tokenVertexAssociation_ = consumes<CandToVertex>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
tokenVertexAssociationQuality_ =
consumes<edm::ValueMap<int>>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
produces<edm::PtrVector<reco::Candidate>>();
}

PFnoPileUp::~PFnoPileUp() {}

void PFnoPileUp::produce(Event& iEvent, const EventSetup& iSetup) {
unique_ptr<edm::PtrVector<reco::Candidate>> pOutput(new edm::PtrVector<reco::Candidate>);
Handle<CandidateView> candidateView;
iEvent.getByToken(tokenCandidatesView_, candidateView);
edm::Handle<edm::Association<reco::VertexCollection>> assoHandle;
iEvent.getByToken(tokenVertexAssociation_, assoHandle);
const edm::Association<reco::VertexCollection>* associatedPV = assoHandle.product();
edm::Handle<edm::ValueMap<int>> assoQualityHandle;
iEvent.getByToken(tokenVertexAssociationQuality_, assoQualityHandle);
const edm::ValueMap<int>* associationQuality = assoQualityHandle.product();
for (unsigned i = 0; i < candidateView->size(); i++) {
const reco::VertexRef& PVOrig = (*associatedPV)[candidateView->ptrAt(i)];
int quality = (*associationQuality)[candidateView->ptrAt(i)];
if (!(PVOrig.isNonnull() && (PVOrig.key() > 0) && (quality >= vertexAssociationQuality_)))
pOutput->push_back(candidateView->ptrAt(i));
}
iEvent.put(std::move(pOutput));
}

DEFINE_FWK_MODULE(PFnoPileUp);
48 changes: 48 additions & 0 deletions CommonTools/ParticleFlow/plugins/PFnoPileUp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef CommonTools_ParticleFlow_PFnoPileUp_
#define CommonTools_ParticleFlow_PFnoPileUp_

// system include files
#include <memory>
#include <string>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "DataFormats/Candidate/interface/Candidate.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "DataFormats/Common/interface/Association.h"

/**\class PFnoPileUp
\brief Identifies pile-up candidates from a collection of Candidates, and
produces the corresponding collection of NoPileUpCandidates.
\author Andreas Hinzmann
\date May 2021
*/

class PFnoPileUp : public edm::stream::EDProducer<> {
public:
typedef edm::View<reco::Candidate> CandidateView;
typedef edm::Association<reco::VertexCollection> CandToVertex;

explicit PFnoPileUp(const edm::ParameterSet&);

~PFnoPileUp() override;

void produce(edm::Event&, const edm::EventSetup&) override;

private:
edm::EDGetTokenT<CandidateView> tokenCandidatesView_;
edm::EDGetTokenT<reco::VertexCollection> tokenVertices_;
edm::EDGetTokenT<CandToVertex> tokenVertexAssociation_;
edm::EDGetTokenT<edm::ValueMap<int>> tokenVertexAssociationQuality_;
int vertexAssociationQuality_;
};

#endif
16 changes: 16 additions & 0 deletions CommonTools/ParticleFlow/python/pfCHS_cff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import FWCore.ParameterSet.Config as cms
from CommonTools.ParticleFlow.pfNoPileUpJME_cff import adapt, pfPileUpJME
from PhysicsTools.PatAlgos.sortedPackedPrimaryVertices_cfi import sortedPackedPrimaryVertices

packedPrimaryVertexAssociationJME = sortedPackedPrimaryVertices.clone(
produceSortedVertices = False,
producePileUpCollection = False,
produceNoPileUpCollection = False
)
adapt(packedPrimaryVertexAssociationJME)

pfCHS = cms.EDProducer("PFnoPileUp",
candidates = cms.InputTag("packedPFCandidates"),
vertexAssociationQuality = pfPileUpJME.vertexAssociationQuality,
vertexAssociation = cms.InputTag("packedPrimaryVertexAssociationJME","original")
)
28 changes: 25 additions & 3 deletions CommonTools/ParticleFlow/python/pfNoPileUpJME_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,38 @@

from CommonTools.ParticleFlow.pfPileUp_cfi import pfPileUp as _pfPileUp
from CommonTools.ParticleFlow.TopProjectors.pfNoPileUp_cfi import pfNoPileUp as _pfNoPileUp
from CommonTools.ParticleFlow.goodOfflinePrimaryVertices_cfi import *
from CommonTools.ParticleFlow.goodOfflinePrimaryVertices_cfi import goodOfflinePrimaryVertices
from CommonTools.RecoAlgos.primaryVertexAssociation_cfi import primaryVertexAssociation

def adapt(primaryVertexAssociationJME):
# options for quality PrimaryDz = 6 (used in PUPPI)
primaryVertexAssociationJME.assignment.maxDzSigForPrimaryAssignment = 1e10
primaryVertexAssociationJME.assignment.maxDzForPrimaryAssignment = 0.3
primaryVertexAssociationJME.assignment.maxDzErrorForPrimaryAssignment = 1e10
primaryVertexAssociationJME.assignment.NumOfPUVtxsForCharged = 2
primaryVertexAssociationJME.assignment.PtMaxCharged = 20.
primaryVertexAssociationJME.assignment.EtaMinUseDz = 2.4
primaryVertexAssociationJME.assignment.OnlyUseFirstDz = True
from Configuration.Eras.Modifier_phase2_common_cff import phase2_common
phase2_common.toModify(
primaryVertexAssociationJME.assignment,
maxDzForPrimaryAssignment=0.1,
EtaMinUseDz = 4.0
)
primaryVertexAssociationJME = primaryVertexAssociation.clone(vertices = 'goodOfflinePrimaryVertices')
adapt(primaryVertexAssociationJME)

pfPileUpJME = _pfPileUp.clone(PFCandidates='particleFlowPtrs',
Vertices = 'goodOfflinePrimaryVertices',
checkClosestZVertex = False )
useVertexAssociation = True,
vertexAssociationQuality = 7,
vertexAssociation = cms.InputTag('primaryVertexAssociationJME','original'),
)
pfNoPileUpJME = _pfNoPileUp.clone(topCollection = 'pfPileUpJME',
bottomCollection = 'particleFlowPtrs' )

pfNoPileUpJMETask = cms.Task(
goodOfflinePrimaryVertices,
primaryVertexAssociationJME,
pfPileUpJME,
pfNoPileUpJME
)
Expand Down
5 changes: 4 additions & 1 deletion CommonTools/ParticleFlow/python/pfPileUp_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
# pile-up identification now enabled by default. To be studied for jets
Enable = cms.bool(True),
verbose = cms.untracked.bool(False),
checkClosestZVertex = cms.bool(True)
checkClosestZVertex = cms.bool(True),
useVertexAssociation = cms.bool(False),
vertexAssociationQuality = cms.int32(0),
vertexAssociation = cms.InputTag(''),
)
43 changes: 42 additions & 1 deletion CommonTools/PileupAlgos/plugins/PuppiProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ PuppiProducer::PuppiProducer(const edm::ParameterSet& iConfig) {

tokenPFCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
tokenVertices_ = consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexName"));
fUseVertexAssociation = iConfig.getParameter<bool>("useVertexAssociation");
vertexAssociationQuality_ = iConfig.getParameter<int>("vertexAssociationQuality");
if (fUseVertexAssociation) {
tokenVertexAssociation_ = consumes<CandToVertex>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
tokenVertexAssociationQuality_ =
consumes<edm::ValueMap<int>>(iConfig.getParameter<edm::InputTag>("vertexAssociation"));
}

fUsePUProxyValue = iConfig.getParameter<bool>("usePUProxyValue");

Expand Down Expand Up @@ -80,6 +87,17 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
iEvent.getByToken(tokenVertices_, hVertexProduct);
const reco::VertexCollection* pvCol = hVertexProduct.product();

edm::Handle<edm::Association<reco::VertexCollection>> assoHandle;
const edm::Association<reco::VertexCollection>* associatedPV = nullptr;
edm::Handle<edm::ValueMap<int>> assoQualityHandle;
const edm::ValueMap<int>* associationQuality = nullptr;
if ((fUseVertexAssociation) && (!fUseExistingWeights)) {
iEvent.getByToken(tokenVertexAssociation_, assoHandle);
associatedPV = assoHandle.product();
iEvent.getByToken(tokenVertexAssociationQuality_, assoQualityHandle);
associationQuality = assoQualityHandle.product();
}

double puProxyValue = 0.;
if (fUsePUProxyValue) {
puProxyValue = iEvent.get(puProxyValueToken_);
Expand All @@ -95,6 +113,7 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
//Fill the reco objects
fRecoObjCollection.clear();
fRecoObjCollection.reserve(pfCol->size());
size_t ic = 0;
for (auto const& aPF : *pfCol) {
RecoObj pReco;
pReco.pt = aPF.pt();
Expand All @@ -110,7 +129,25 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
uint pVtxId = 0;
bool isLepton = ((std::abs(pReco.pdgId) == 11) || (std::abs(pReco.pdgId) == 13));
const pat::PackedCandidate* lPack = dynamic_cast<const pat::PackedCandidate*>(&aPF);
if (lPack == nullptr) {

if (fUseVertexAssociation) {
const reco::VertexRef& PVOrig = (*associatedPV)[reco::CandidatePtr(hPFProduct, ic)];
int quality = (*associationQuality)[reco::CandidatePtr(hPFProduct, ic)];
if (PVOrig.isNonnull() && (quality >= vertexAssociationQuality_)) {
closestVtx = PVOrig.get();
pVtxId = PVOrig.key();
}
if (std::abs(pReco.charge) == 0)
pReco.id = 0;
else if (fPuppiNoLep && isLepton)
pReco.id = 3;
else if (closestVtx != nullptr && pVtxId == 0)
pReco.id = 1; // Associated to main vertex
else if (closestVtx != nullptr && pVtxId > 0)
pReco.id = 2; // Associated to PU
else
pReco.id = 0; // Unassociated
} else if (lPack == nullptr) {
const reco::PFCandidate* pPF = dynamic_cast<const reco::PFCandidate*>(&aPF);
double curdz = 9999;
int closestVtxForUnassociateds = -9999;
Expand Down Expand Up @@ -230,6 +267,7 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
}

fRecoObjCollection.push_back(pReco);
ic++;
}

fPuppiContainer->initialize(fRecoObjCollection);
Expand Down Expand Up @@ -401,6 +439,9 @@ void PuppiProducer::fillDescriptions(edm::ConfigurationDescriptions& description
desc.add<double>("vtxZCut", 24);
desc.add<edm::InputTag>("candName", edm::InputTag("particleFlow"));
desc.add<edm::InputTag>("vertexName", edm::InputTag("offlinePrimaryVertices"));
desc.add<bool>("useVertexAssociation", false);
desc.add<int>("vertexAssociationQuality", 0);
desc.add<edm::InputTag>("vertexAssociation", edm::InputTag(""));
desc.add<bool>("applyCHS", true);
desc.add<bool>("invertPuppi", false);
desc.add<bool>("useExp", false);
Expand Down
5 changes: 5 additions & 0 deletions CommonTools/PileupAlgos/plugins/PuppiProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PuppiProducer : public edm::stream::EDProducer<> {
typedef std::vector<reco::PFCandidate> PFOutputCollection;
typedef std::vector<pat::PackedCandidate> PackedOutputCollection;
typedef edm::View<reco::PFCandidate> PFView;
typedef edm::Association<reco::VertexCollection> CandToVertex;

private:
virtual void beginJob();
Expand All @@ -42,6 +43,8 @@ class PuppiProducer : public edm::stream::EDProducer<> {

edm::EDGetTokenT<CandidateView> tokenPFCandidates_;
edm::EDGetTokenT<VertexCollection> tokenVertices_;
edm::EDGetTokenT<CandToVertex> tokenVertexAssociation_;
edm::EDGetTokenT<edm::ValueMap<int>> tokenVertexAssociationQuality_;
edm::EDGetTokenT<PuppiContainer> tokenPuppiContainer_;
edm::EDGetTokenT<PFOutputCollection> tokenPuppiCandidates_;
edm::EDGetTokenT<PackedOutputCollection> tokenPackedPuppiCandidates_;
Expand All @@ -59,6 +62,8 @@ class PuppiProducer : public edm::stream::EDProducer<> {
std::string fPuppiName;
std::string fPFName;
std::string fPVName;
bool fUseVertexAssociation;
int vertexAssociationQuality_;
bool fPuppiDiagnostics;
bool fPuppiNoLep;
bool fUseFromPVLooseTight;
Expand Down
3 changes: 3 additions & 0 deletions CommonTools/PileupAlgos/python/Puppi_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
NumOfPUVtxsForCharged = 2,
PtMaxCharged = 20.,
PtMaxNeutralsStartSlope = 20.,
useVertexAssociation = True,
vertexAssociationQuality = cms.int32(6),
vertexAssociation = cms.InputTag('primaryVertexAssociationJME','original'),
#candName = cms.string('packedPFCandidates'),
#vertexName = cms.string('offlineSlimmedPrimaryVertices'),
clonePackedCands = False, # should only be set to True for MiniAOD
Expand Down
Loading

0 comments on commit e10f0bf

Please sign in to comment.