Skip to content

Commit

Permalink
Merge pull request #22092 from rappoccio/jetselector_ptrs_101x
Browse files Browse the repository at this point in the history
Adding capability to write out Ptrs in JetConstituentSelector
  • Loading branch information
cmsbuild committed Feb 8, 2018
2 parents c1a87cc + 531601c commit bf01e69
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
66 changes: 59 additions & 7 deletions CommonTools/RecoAlgos/plugins/JetConstituentSelector.cc
Expand Up @@ -20,23 +20,29 @@
#include "DataFormats/JetReco/interface/GenJet.h"
#include "DataFormats/PatCandidates/interface/Jet.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"
#include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
#include "DataFormats/JetReco/interface/CaloJet.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"




template <class T, typename C = std::vector<typename T::ConstituentTypeFwdPtr>>
class JetConstituentSelector : public edm::stream::EDProducer<> {
public:

using JetsOutput = std::vector<T>;
using ConstituentsOutput = C;

using ValueType = typename C::value_type;

JetConstituentSelector(edm::ParameterSet const& params) :
srcToken_{consumes<edm::View<T>>(params.getParameter<edm::InputTag>("src"))},
selector_{params.getParameter<std::string>("cut")}
selector_{params.getParameter<std::string>("cut")},
unpackAK8_{params.getParameter<bool>("unpackAK8")}
{
produces<JetsOutput>();
produces<ConstituentsOutput>("constituents");
Expand All @@ -48,6 +54,7 @@ class JetConstituentSelector : public edm::stream::EDProducer<> {
desc.add<edm::InputTag>("src")->setComment("InputTag used for retrieving jets in event.");
desc.add<std::string>("cut")->setComment("Cut used by which to select jets. For example:\n"
" \"pt > 100.0 && abs(rapidity()) < 2.4\".");
desc.add<bool>("unpackAK8", false)->setComment("Assume the jets are AK8 jets from miniAOD and need to be unpacked.");

// addDefault must be used here instead of add unless this function is specialized
// for different sets of template parameter types. Each specialization would need
Expand All @@ -56,6 +63,11 @@ class JetConstituentSelector : public edm::stream::EDProducer<> {
descriptions.addDefault(desc);
}

// Default initialization is for edm::FwdPtr. Specialization (below) is for edm::Ptr.
typename ConstituentsOutput::value_type const initptr( edm::Ptr<reco::Candidate> const & dau) const{
return typename ConstituentsOutput::value_type( dau, dau );
}

void produce(edm::Event& iEvent, edm::EventSetup const& iSetup) override
{
auto jets = std::make_unique<JetsOutput>();
Expand All @@ -64,15 +76,34 @@ class JetConstituentSelector : public edm::stream::EDProducer<> {
edm::Handle<edm::View<T>> h_jets;
iEvent.getByToken(srcToken_, h_jets);


// Now set the Ptrs with the orphan handles.
for (auto const& jet : *h_jets) {
// Check the selection
if (selector_(jet)) {
// Add the jets that pass to the output collection
jets->push_back(jet);
for (unsigned int ida {}; ida < jet.numberOfDaughters(); ++ida) {
candsOut->emplace_back(jet.daughterPtr(ida), jet.daughterPtr(ida));
}
// Add the jets that pass to the output collection
jets->push_back(jet);

if ( !unpackAK8_) {
for (unsigned int ida {}; ida < jet.numberOfDaughters(); ++ida) {
candsOut->emplace_back( initptr(jet.daughterPtr(ida)) );
}
}
else {
// Special case for AK8 pat::Jets from miniAOD, which store the subjets in the first two
// daughter links, and then the rest of the daughters that do not satisfy the jet groomer.
for (unsigned int ida {}; ida < jet.numberOfDaughters(); ++ida) {
auto const & dau = jet.daughterPtr(ida);
if ( dau->numberOfDaughters() == 0 ) {
candsOut->emplace_back( initptr(dau) );
} else {
auto const * dauJet = dynamic_cast<pat::Jet const *>(dau.get());
for ( unsigned int jda {}; jda < dauJet->numberOfDaughters(); ++jda ) {
candsOut->emplace_back( initptr(dauJet->daughterPtr(jda)) );
}
}
}
}
}
}

Expand All @@ -83,14 +114,35 @@ class JetConstituentSelector : public edm::stream::EDProducer<> {
private:
edm::EDGetTokenT<edm::View<T>> const srcToken_;
StringCutObjectSelector<T> const selector_;
bool unpackAK8_;
};

template<>
edm::Ptr<pat::PackedCandidate> const
JetConstituentSelector< pat::Jet,std::vector<edm::Ptr<pat::PackedCandidate>>>::initptr( edm::Ptr<reco::Candidate> const & dau) const{
edm::Ptr<pat::PackedCandidate> retval( dau );
return retval;
}

template<>
edm::Ptr<pat::PackedGenParticle> const
JetConstituentSelector<reco::GenJet,std::vector<edm::Ptr<pat::PackedGenParticle>>>::initptr( edm::Ptr<reco::Candidate> const & dau) const{
edm::Ptr<pat::PackedGenParticle> retval( dau );
return retval;
}

using PFJetConstituentSelector = JetConstituentSelector<reco::PFJet>;
using GenJetConstituentSelector = JetConstituentSelector<reco::GenJet, std::vector<edm::FwdPtr<reco::GenParticle>>>;
using GenJetPackedConstituentSelector = JetConstituentSelector<reco::GenJet, std::vector<edm::FwdPtr<pat::PackedGenParticle>>>;
using GenJetPackedConstituentPtrSelector = JetConstituentSelector<reco::GenJet, std::vector<edm::Ptr<pat::PackedGenParticle>>>;
using PatJetConstituentSelector = JetConstituentSelector<pat::Jet, std::vector<edm::FwdPtr<pat::PackedCandidate>>>;
using PatJetConstituentPtrSelector = JetConstituentSelector<pat::Jet, std::vector<edm::Ptr<pat::PackedCandidate>>>;
using MiniAODJetConstituentSelector = JetConstituentSelector<reco::PFJet, std::vector<edm::FwdPtr<pat::PackedCandidate>>>;
using MiniAODJetConstituentPtrSelector = JetConstituentSelector<reco::PFJet, std::vector<edm::Ptr<pat::PackedCandidate>>>;

DEFINE_FWK_MODULE(PFJetConstituentSelector);
DEFINE_FWK_MODULE(GenJetConstituentSelector);
DEFINE_FWK_MODULE(GenJetPackedConstituentPtrSelector);
DEFINE_FWK_MODULE(PatJetConstituentSelector);
DEFINE_FWK_MODULE(PatJetConstituentPtrSelector);
DEFINE_FWK_MODULE(MiniAODJetConstituentSelector);
10 changes: 10 additions & 0 deletions DataFormats/PatCandidates/src/classes_def_objects.xml
Expand Up @@ -416,6 +416,8 @@
<class name="edm::Ptr<pat::MET>" />
<class name="edm::Ptr<pat::Conversion>" />
<class name="edm::Ptr<pat::PackedCandidate>" />
<class name="edm::Ptr<pat::PackedGenParticle>" />


<class name="edm::FwdPtr<pat::PackedCandidate>" />
<class name="std::vector<edm::FwdPtr<pat::PackedCandidate> >" />
Expand All @@ -427,6 +429,14 @@
<class name="edm::Wrapper<edm::FwdPtr<pat::PackedGenParticle> >" />
<class name="edm::Wrapper<std::vector<edm::FwdPtr<pat::PackedGenParticle> > >" />

<class name="std::vector<edm::Ptr<pat::PackedCandidate> >" />
<class name="edm::Wrapper<edm::Ptr<pat::PackedCandidate> >" />
<class name="edm::Wrapper<std::vector<edm::Ptr<pat::PackedCandidate> > >" />

<class name="std::vector<edm::Ptr<pat::PackedGenParticle> >" />
<class name="edm::Wrapper<edm::Ptr<pat::PackedGenParticle> >" />
<class name="edm::Wrapper<std::vector<edm::Ptr<pat::PackedGenParticle> > >" />

<!-- PAT Object Collections -->
<class name="std::vector<pat::Electron>" />
<class name="std::vector<pat::Muon>" />
Expand Down
9 changes: 9 additions & 0 deletions DataFormats/PatCandidates/src/classes_objects.h
Expand Up @@ -169,7 +169,12 @@ namespace DataFormats_PatCandidates {
edm::Ptr<pat::Muon> ptr_Muon;
edm::Ptr<pat::Tau> ptr_Tau;
edm::Ptr<pat::PackedCandidate> ptr_PackedCandidate;
edm::Ptr<pat::PackedGenParticle> ptr_PackedGenParticle;
edm::Wrapper< edm::Ptr<pat::PackedCandidate> > w_ptr_pc;
std::vector< edm::Ptr<pat::PackedCandidate> > v_ptr_pc;
edm::Wrapper< std::vector< edm::Ptr<pat::PackedCandidate> > > wv_ptr_pc;


edm::FwdPtr<pat::PackedCandidate> fwdptr_pc;
edm::Wrapper< edm::FwdPtr<pat::PackedCandidate> > w_fwdptr_pc;
std::vector< edm::FwdPtr<pat::PackedCandidate> > v_fwdptr_pc;
Expand All @@ -180,6 +185,10 @@ namespace DataFormats_PatCandidates {
std::vector< edm::FwdPtr<pat::PackedGenParticle> > v_fwdptr_pgp;
edm::Wrapper< std::vector< edm::FwdPtr<pat::PackedGenParticle> > > wv_fwdptr_pgp;

edm::Wrapper< edm::Ptr<pat::PackedGenParticle> > w_ptr_pgp;
std::vector< edm::Ptr<pat::PackedGenParticle> > v_ptr_pgp;
edm::Wrapper< std::vector< edm::Ptr<pat::PackedGenParticle> > > wv_ptr_pgp;


edm::Wrapper<edm::Association<pat::PackedCandidateCollection > > w_asso_pc;
edm::Wrapper<edm::Association<reco::PFCandidateCollection > > w_asso_pfc;
Expand Down

0 comments on commit bf01e69

Please sign in to comment.