Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions PhysicsTools/NanoAOD/plugins/SimpleJetConstituentTableProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"

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

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/StreamID.h"

#include "DataFormats/PatCandidates/interface/Jet.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"
#include "DataFormats/PatCandidates/interface/PackedGenParticle.h"

#include "DataFormats/Candidate/interface/CandidateFwd.h"

#include "CommonTools/Utils/interface/StringCutObjectSelector.h"
#include "DataFormats/NanoAOD/interface/FlatTable.h"

template <typename T>
class SimpleJetConstituentTableProducer : public edm::stream::EDProducer<> {
public:
explicit SimpleJetConstituentTableProducer(const edm::ParameterSet &);
~SimpleJetConstituentTableProducer() override;

static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);

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

const std::string name_;
const std::string candIdxName_;
const std::string candIdxDoc_;

edm::EDGetTokenT<edm::View<T>> jet_token_;
edm::EDGetTokenT<reco::CandidateView> cand_token_;

const StringCutObjectSelector<T> jetCut_;

edm::Handle<reco::CandidateView> cands_;
};

//
// constructors and destructor
//
template <typename T>
SimpleJetConstituentTableProducer<T>::SimpleJetConstituentTableProducer(const edm::ParameterSet &iConfig)
: name_(iConfig.getParameter<std::string>("name")),
candIdxName_(iConfig.getParameter<std::string>("candIdxName")),
candIdxDoc_(iConfig.getParameter<std::string>("candIdxDoc")),
jet_token_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("jets"))),
cand_token_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("candidates"))),
jetCut_(iConfig.getParameter<std::string>("jetCut")) {
produces<nanoaod::FlatTable>(name_);
produces<std::vector<reco::CandidatePtr>>();
}

template <typename T>
SimpleJetConstituentTableProducer<T>::~SimpleJetConstituentTableProducer() {}

template <typename T>
void SimpleJetConstituentTableProducer<T>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
// elements in all these collections must have the same order!
auto outCands = std::make_unique<std::vector<reco::CandidatePtr>>();

auto jets = iEvent.getHandle(jet_token_);

iEvent.getByToken(cand_token_, cands_);
auto candPtrs = cands_->ptrs();

//
// First, select jets
//
std::vector<T> jetsPassCut;
for (unsigned jetIdx = 0; jetIdx < jets->size(); ++jetIdx) {
const auto &jet = jets->at(jetIdx);
if (!jetCut_(jet))
continue;
jetsPassCut.push_back(jets->at(jetIdx));
}

//
// Then loop over selected jets
//
std::vector<int> parentJetIdx;
std::vector<int> candIdx;
for (unsigned jetIdx = 0; jetIdx < jetsPassCut.size(); ++jetIdx) {
const auto &jet = jetsPassCut.at(jetIdx);

//
// Loop over jet constituents
//
std::vector<reco::CandidatePtr> const &daughters = jet.daughterPtrVector();
for (const auto &cand : daughters) {
auto candInNewList = std::find(candPtrs.begin(), candPtrs.end(), cand);
if (candInNewList == candPtrs.end()) {
continue;
}
outCands->push_back(cand);
parentJetIdx.push_back(jetIdx);
candIdx.push_back(candInNewList - candPtrs.begin());
}
} // end jet loop

auto candTable = std::make_unique<nanoaod::FlatTable>(outCands->size(), name_, false);
// We fill from here only stuff that cannot be created with the SimpleFlatTableProducer
candTable->addColumn<int>(candIdxName_, candIdx, candIdxDoc_);

std::string parentJetIdxName("jetIdx");
std::string parentJetIdxDoc("Index of the parent jet");
if constexpr (std::is_same<T, reco::GenJet>::value) {
parentJetIdxName = "genJetIdx";
parentJetIdxDoc = "Index of the parent gen jet";
}
candTable->addColumn<int>(parentJetIdxName, parentJetIdx, parentJetIdxDoc);

iEvent.put(std::move(candTable), name_);
iEvent.put(std::move(outCands));
}

template <typename T>
void SimpleJetConstituentTableProducer<T>::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
edm::ParameterSetDescription desc;
desc.add<std::string>("name", "FatJetPFCand");
desc.add<std::string>("candIdxName", "PFCandIdx");
desc.add<std::string>("candIdxDoc", "Index in PFCand table");
desc.add<edm::InputTag>("jets", edm::InputTag("finalJetsAK8"));
desc.add<edm::InputTag>("candidates", edm::InputTag("packedPFCandidates"));
desc.add<std::string>("jetCut", "");
descriptions.addWithDefaultLabel(desc);
}

typedef SimpleJetConstituentTableProducer<pat::Jet> SimplePatJetConstituentTableProducer;
typedef SimpleJetConstituentTableProducer<reco::GenJet> SimpleGenJetConstituentTableProducer;

DEFINE_FWK_MODULE(SimplePatJetConstituentTableProducer);
DEFINE_FWK_MODULE(SimpleGenJetConstituentTableProducer);
44 changes: 42 additions & 2 deletions PhysicsTools/NanoAOD/python/jetsAK8_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,50 @@ def nanoAOD_addDeepInfoAK8(process, addDeepBTag, addDeepBoostedJet, addDeepDoubl
fatJetTable.variables.pt.precision=10
subJetTable.variables.pt.precision=10

##############################################################
# AK8 constituents
###############################################################
finalJetsAK8PFConstituents = cms.EDProducer("PatJetConstituentPtrSelector",
src = fatJetTable.src,
cut = cms.string("abs(eta) <= 2.5")
)

finalJetsPFConstituents = cms.EDProducer("PackedCandidatePtrMerger",
src = cms.VInputTag(cms.InputTag("finalJetsAK8PFConstituents", "constituents")),
skipNulls = cms.bool(True),
warnOnSkip = cms.bool(True)
)

pfCandidatesTable = cms.EDProducer("SimplePATCandidateFlatTableProducer",
src = cms.InputTag("finalJetsPFConstituents"),
cut = cms.string(""),
name = cms.string("PFCand"),
doc = cms.string("PF candidate constituents of AK8 puppi jets (FatJet) with |eta| <= 2.5."),
singleton = cms.bool(False),
extension = cms.bool(False),
variables = cms.PSet(
pt = Var("pt * puppiWeight()", float, doc="Puppi-weighted pt", precision=10),
mass = Var("mass * puppiWeight()", float, doc="Puppi-weighted mass", precision=10),
eta = Var("eta", float, precision=12),
phi = Var("phi", float, precision=12),
pdgId = Var("pdgId", int, doc="PF candidate type (+/-211 = ChgHad, 130 = NeuHad, 22 = Photon, +/-11 = Electron, +/-13 = Muon, 1 = HFHad, 2 = HFEM)")
)
)

finalJetsAK8ConstituentsTable = cms.EDProducer("SimplePatJetConstituentTableProducer",
name = cms.string(fatJetTable.name.value()+"PFCand"),
candIdxName = cms.string("PFCandIdx"),
candIdxDoc = cms.string("Index in the PFCand table"),
jets = fatJetTable.src,
candidates = pfCandidatesTable.src,
jetCut = fatJetTable.cut
)

jetAK8UserDataTask = cms.Task(tightJetIdAK8,tightJetIdLepVetoAK8)
jetAK8Task = cms.Task(jetCorrFactorsAK8,updatedJetsAK8,jetAK8UserDataTask,updatedJetsAK8WithUserData,finalJetsAK8)
jetAK8Task = cms.Task(jetCorrFactorsAK8,updatedJetsAK8,jetAK8UserDataTask,updatedJetsAK8WithUserData,finalJetsAK8,finalJetsAK8PFConstituents,finalJetsPFConstituents)

#after lepton collections have been run
jetAK8LepTask = cms.Task(lepInAK8JetVars)

jetAK8TablesTask = cms.Task(fatJetTable,subJetTable)
jetAK8TablesTask = cms.Task(fatJetTable,subJetTable,pfCandidatesTable,finalJetsAK8ConstituentsTable)

11 changes: 11 additions & 0 deletions PhysicsTools/NanoAOD/python/nanoDQM_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@
Plot1D('tau4', 'tau4', 20, 0, 1, 'Nsubjettiness (4 axis)'),
)
),
PFCand = cms.PSet(
sels = cms.PSet(),
plots = cms.VPSet(
Count1D('_size', 10, 0, 100, 'PF candidates'),
Plot1D('pt', 'pt', 20, 0, 50, 'Puppi-weighted pt'),
Plot1D('eta', 'eta', 20, -4, 4., 'eta'),
Plot1D('phi', 'phi', 20, -3.14159, 3.14159, 'phi'),
Plot1D('mass', 'mass', 10, 0, 1., 'Puppi-weighted mass'),
Plot1D('pdgId', 'pdgId', 44, -220, 220, 'PF candidate type (+/-211 = ChgHad, 130 = NeuHad, 22 = Photon, +/-11 = Electron, +/-13 = Muon, 1 = HFHad, 2 = HFEM)'),
)
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nurfikri89 Would it make sense to add FatJetPFCand_PFCandIdx and FatJetPFCand_jetIdx to DQM as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the indices that we store for other objects, we do not make DQM plots for them. I do not see a reason to do anything differently here.

Flag = cms.PSet(
sels = cms.PSet(),
plots = cms.VPSet(
Expand Down
8 changes: 8 additions & 0 deletions PhysicsTools/PatAlgos/plugins/PATObjectSelector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "CommonTools/UtilAlgos/interface/SingleObjectSelector.h"
#include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
#include "DataFormats/Common/interface/RefVector.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"
#include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
#include "DataFormats/PatCandidates/interface/Electron.h"
#include "DataFormats/PatCandidates/interface/GenericParticle.h"
Expand Down Expand Up @@ -248,6 +249,12 @@ namespace pat {
typedef SingleObjectSelector<std::vector<GenericParticle>, StringCutObjectSelector<GenericParticle>>
PATGenericParticleSelector;

typedef SingleObjectSelector<
std::vector<PackedCandidate>,
StringCutObjectSelector<PackedCandidate, true> // true => lazy parsing => get all methods of daughters
>
PATPackedCandidateSelector;

typedef SingleObjectSelector<std::vector<Electron>,
StringCutObjectSelector<Electron>,
edm::RefVector<std::vector<Electron>>>
Expand Down Expand Up @@ -296,6 +303,7 @@ DEFINE_FWK_MODULE(PATPFParticleSelector);
DEFINE_FWK_MODULE(PATCompositeCandidateSelector);
DEFINE_FWK_MODULE(PATTriggerObjectStandAloneSelector);
DEFINE_FWK_MODULE(PATGenericParticleSelector);
DEFINE_FWK_MODULE(PATPackedCandidateSelector);

DEFINE_FWK_MODULE(PATElectronRefSelector);
DEFINE_FWK_MODULE(PATMuonRefSelector);
Expand Down