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

Adding capability to write out Ptrs in JetConstituentSelector #22092

Merged
merged 2 commits into from Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.existsAs<bool>("unpackAK8") ? params.getParameter<bool>("unpackAK8") : false}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, add a "false" as default in the fillDescriptions() method, and get rid of the existsAs here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Won't that affect existing workflows though? If they don't have the parameter they will need to add it. (Or is adding "false" as a default sufficient?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding "false" as default in the parameterSetDescription insures that whenever the parameter is missing the default value is assigned.

Which is the same as you are doing now, but a bit cleaner from the point of view of the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

existsAs is strongly discouraged and exceptions to use it are still possible only for plugins.
This is not the case for this producer.
So, I think that fillDescriptions should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK I will fix it. It looks like there are some matrix failures for some reason, I will fix those too.

{
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")->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 @@ -414,6 +414,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 @@ -425,6 +427,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