Skip to content
Closed
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
2 changes: 2 additions & 0 deletions DataFormats/PatCandidates/interface/Electron.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace reco {
// Class definition
namespace pat {
class PATElectronSlimmer;
class PATElectronCandidatesRekeyer;

class Electron : public Lepton<reco::GsfElectron> {
public:
Expand Down Expand Up @@ -269,6 +270,7 @@ namespace pat {
}

friend class PATElectronSlimmer;
friend class PATElectronCandidatesRekeyer;

protected:
/// init impact parameter defaults (for use in a constructor)
Expand Down
2 changes: 2 additions & 0 deletions DataFormats/PatCandidates/interface/Muon.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace reco {
namespace pat {

class PATMuonSlimmer;
class PATMuonCandidatesRekeyer;

class Muon : public Lepton<reco::Muon> {
public:
Expand Down Expand Up @@ -272,6 +273,7 @@ namespace pat {
friend std::ostream& reco::operator<<(std::ostream& out, const pat::Muon& obj);

friend class PATMuonSlimmer;
friend class PATMuonCandidatesRekeyer;

float pfEcalEnergy() const { return pfEcalEnergy_; }
void setPfEcalEnergy(float pfEcalEnergy) { pfEcalEnergy_ = pfEcalEnergy; }
Expand Down
2 changes: 2 additions & 0 deletions DataFormats/PatCandidates/interface/Photon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace reco {
// Class definition
namespace pat {
class PATPhotonSlimmer;
class PATPhotonCandidatesRekeyer;

class Photon : public PATObject<reco::Photon> {
public:
Expand Down Expand Up @@ -328,6 +329,7 @@ namespace pat {
reco::CandidatePtr sourceCandidatePtr(size_type i) const override;

friend class PATPhotonSlimmer;
friend class PATPhotonCandidatesRekeyer;

protected:
// ---- for content embedding ----
Expand Down
78 changes: 78 additions & 0 deletions PhysicsTools/PatAlgos/plugins/PATElectronCandidatesRekeyer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/View.h"

#include "DataFormats/PatCandidates/interface/Electron.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"

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

namespace pat {
class PATElectronCandidatesRekeyer : public edm::stream::EDProducer<> {
Copy link
Contributor

Choose a reason for hiding this comment

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

there might be a way to template that PATObjectCandidatesRekeyer to the pat::Electron, leaving a rekey method that is specialised in the loop ; food for thoughts for code improvement if there's time to get this in

public:
explicit PATElectronCandidatesRekeyer(const edm::ParameterSet &iConfig);
~PATElectronCandidatesRekeyer() override;

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

private:
// configurables
edm::EDGetTokenT<std::vector<pat::Electron>> src_;
edm::EDGetTokenT<reco::CandidateView> pcNewCandViewToken_;
edm::EDGetTokenT<pat::PackedCandidateCollection> pcNewToken_;
};

} // namespace pat

using namespace pat;

PATElectronCandidatesRekeyer::PATElectronCandidatesRekeyer(const edm::ParameterSet &iConfig)
:

src_(consumes<std::vector<pat::Electron>>(iConfig.getParameter<edm::InputTag>("src"))),
pcNewCandViewToken_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))),
pcNewToken_(
consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))) {
produces<std::vector<pat::Electron>>();
}

PATElectronCandidatesRekeyer::~PATElectronCandidatesRekeyer() {}

void PATElectronCandidatesRekeyer::produce(edm::Event &iEvent, edm::EventSetup const &) {
edm::Handle<std::vector<pat::Electron>> src;
iEvent.getByToken(src_, src);

edm::Handle<reco::CandidateView> pcNewCandViewHandle;
iEvent.getByToken(pcNewCandViewToken_, pcNewCandViewHandle);

edm::Handle<pat::PackedCandidateCollection> pcNewHandle;
iEvent.getByToken(pcNewToken_, pcNewHandle);

auto outPtrP = std::make_unique<std::vector<pat::Electron>>();
outPtrP->reserve(src->size());

for (size_t i = 0; i < src->size(); ++i) {
// copy original pat object and append to vector
outPtrP->emplace_back((*src)[i]);

std::vector<unsigned int> keys;
for (const edm::Ref<pat::PackedCandidateCollection> &ref : outPtrP->back().associatedPackedPFCandidates()) {
keys.push_back(ref.key());
};
outPtrP->back().setAssociatedPackedPFCandidates(
edm::RefProd<pat::PackedCandidateCollection>(pcNewHandle), keys.begin(), keys.end());
if (keys.size() == 1) {
outPtrP->back().refToOrig_ = outPtrP->back().sourceCandidatePtr(0);
} else {
outPtrP->back().refToOrig_ = reco::CandidatePtr(pcNewHandle.id());
}
}
iEvent.put(std::move(outPtrP));
}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(PATElectronCandidatesRekeyer);
110 changes: 110 additions & 0 deletions PhysicsTools/PatAlgos/plugins/PATJetCandidatesRekeyer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/View.h"

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

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

namespace pat {
class PATJetCandidatesRekeyer : public edm::stream::EDProducer<> {
public:
explicit PATJetCandidatesRekeyer(const edm::ParameterSet &iConfig);
~PATJetCandidatesRekeyer() override;

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

private:
// configurables
edm::EDGetTokenT<std::vector<pat::Jet>> src_;
edm::EDGetTokenT<reco::CandidateView> pcNewCandViewToken_;
edm::EDGetTokenT<pat::PackedCandidateCollection> pcNewToken_;
// std::string subjetLabel_;
};
} // namespace pat

using namespace pat;

PATJetCandidatesRekeyer::PATJetCandidatesRekeyer(const edm::ParameterSet &iConfig)
: src_(consumes<std::vector<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
pcNewCandViewToken_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))),
pcNewToken_(
consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))) {
produces<std::vector<pat::Jet>>();
produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
}

PATJetCandidatesRekeyer::~PATJetCandidatesRekeyer() {}

void PATJetCandidatesRekeyer::produce(edm::Event &iEvent, edm::EventSetup const &) {
edm::Handle<std::vector<pat::Jet>> src;
iEvent.getByToken(src_, src);

edm::Handle<reco::CandidateView> pcNewCandViewHandle;
iEvent.getByToken(pcNewCandViewToken_, pcNewCandViewHandle);

edm::Handle<pat::PackedCandidateCollection> pcNewHandle;
iEvent.getByToken(pcNewToken_, pcNewHandle);

edm::RefProd<edm::OwnVector<reco::BaseTagInfo>> h_tagInfosOut =
iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();

auto outPtrP = std::make_unique<std::vector<pat::Jet>>();
outPtrP->reserve(src->size());

//
//
//
for (std::vector<pat::Jet>::const_iterator ibegin = (*src).begin(), iend = (*src).end(), ijet = ibegin; ijet != iend;
++ijet) {
for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
iinfoEnd = ijet->tagInfosFwdPtr().end(),
iinfo = iinfoBegin;
iinfo != iinfoEnd;
++iinfo) {
tagInfosOut->push_back(**iinfo);
}
}

edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo>> oh_tagInfosOut = iEvent.put(std::move(tagInfosOut), "tagInfos");

//
//
//
unsigned int tagInfoIndex = 0;

for (size_t i = 0; i < src->size(); ++i) {
// copy original pat object and append to vector
outPtrP->emplace_back((*src)[i]);

reco::CompositePtrCandidate::daughters old = outPtrP->back().daughterPtrVector();
outPtrP->back().clearDaughters();
for (const auto &dauItr : old) {
outPtrP->back().addDaughter(edm::Ptr<reco::Candidate>(pcNewHandle, dauItr.key()));
}

// Copy the tag infos
for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = outPtrP->back().tagInfosFwdPtr().begin(),
iinfoEnd = outPtrP->back().tagInfosFwdPtr().end(),
iinfo = iinfoBegin;
iinfo != iinfoEnd;
++iinfo) {
// Update the "forward" bit of the FwdPtr to point at the new collection.
// ptr to "this" info in the global list
edm::Ptr<reco::BaseTagInfo> outPtr(oh_tagInfosOut, tagInfoIndex);
outPtrP->back().updateFwdTagInfoFwdPtr(iinfo - iinfoBegin, outPtr);
++tagInfoIndex;
}
}

iEvent.put(std::move(outPtrP));
}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(PATJetCandidatesRekeyer);
77 changes: 77 additions & 0 deletions PhysicsTools/PatAlgos/plugins/PATMuonCandidatesRekeyer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/View.h"

#include "DataFormats/PatCandidates/interface/Muon.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"

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

namespace pat {
class PATMuonCandidatesRekeyer : public edm::stream::EDProducer<> {
public:
explicit PATMuonCandidatesRekeyer(const edm::ParameterSet &iConfig);
~PATMuonCandidatesRekeyer() override;

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

private:
// configurables
edm::EDGetTokenT<std::vector<pat::Muon>> src_;
edm::EDGetTokenT<reco::CandidateView> pcNewCandViewToken_;
edm::EDGetTokenT<pat::PackedCandidateCollection> pcNewToken_;
};

} // namespace pat

using namespace pat;

PATMuonCandidatesRekeyer::PATMuonCandidatesRekeyer(const edm::ParameterSet &iConfig)
: src_(consumes<std::vector<pat::Muon>>(iConfig.getParameter<edm::InputTag>("src"))),
pcNewCandViewToken_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))),
pcNewToken_(
consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidatesNew"))) {
produces<std::vector<pat::Muon>>();
}

PATMuonCandidatesRekeyer::~PATMuonCandidatesRekeyer() {}

void PATMuonCandidatesRekeyer::produce(edm::Event &iEvent, edm::EventSetup const &) {
edm::Handle<std::vector<pat::Muon>> src;
iEvent.getByToken(src_, src);

edm::Handle<reco::CandidateView> pcNewCandViewHandle;
iEvent.getByToken(pcNewCandViewToken_, pcNewCandViewHandle);

edm::Handle<pat::PackedCandidateCollection> pcNewHandle;
iEvent.getByToken(pcNewToken_, pcNewHandle);

auto outPtrP = std::make_unique<std::vector<pat::Muon>>();
outPtrP->reserve(src->size());

for (size_t i = 0; i < src->size(); ++i) {
// copy original pat object and append to vector
outPtrP->emplace_back((*src)[i]);

//
std::vector<unsigned int> keys;
for (size_t ic = 0; ic < outPtrP->back().numberOfSourceCandidatePtrs(); ++ic) {
const reco::CandidatePtr &candPtr = outPtrP->back().sourceCandidatePtr(ic);
if (candPtr.isNonnull()) {
keys.push_back(candPtr.key());
}
}
if (keys.size() == 1) {
outPtrP->back().refToOrig_ = reco::CandidatePtr(pcNewCandViewHandle, keys[0]);
}
}

iEvent.put(std::move(outPtrP));
}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(PATMuonCandidatesRekeyer);
Loading