File indexing completed on 2025-05-08 02:18:42
0001
0002 #include <algorithm>
0003 #include <limits>
0004
0005 #include "DataFormats/Common/interface/View.h"
0006 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0007 #include "DataFormats/VertexReco/interface/Vertex.h"
0008 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/global/EDProducer.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 #include "TrackingTools/IPTools/interface/IPTools.h"
0016 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0017 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
0018 #include "helper.h"
0019
0020 template <typename PATOBJ>
0021 class MatchEmbedder : public edm::global::EDProducer<> {
0022
0023
0024 public:
0025 explicit MatchEmbedder(const edm::ParameterSet &cfg)
0026 : src_{consumes<PATOBJCollection>(cfg.getParameter<edm::InputTag>("src"))},
0027 matching_{
0028 consumes<edm::Association<reco::GenParticleCollection> >(cfg.getParameter<edm::InputTag>("matching"))} {
0029 produces<PATOBJCollection>();
0030 }
0031
0032 ~MatchEmbedder() override {}
0033
0034 void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
0035
0036 private:
0037 typedef std::vector<PATOBJ> PATOBJCollection;
0038 const edm::EDGetTokenT<PATOBJCollection> src_;
0039 const edm::EDGetTokenT<edm::Association<reco::GenParticleCollection> > matching_;
0040 };
0041
0042 template <typename PATOBJ>
0043 void MatchEmbedder<PATOBJ>::produce(edm::StreamID, edm::Event &evt, edm::EventSetup const &iSetup) const {
0044
0045 edm::Handle<PATOBJCollection> src;
0046 evt.getByToken(src_, src);
0047
0048 edm::Handle<edm::Association<reco::GenParticleCollection> > matching;
0049 evt.getByToken(matching_, matching);
0050
0051 size_t nsrc = src->size();
0052
0053 std::unique_ptr<PATOBJCollection> out(new PATOBJCollection());
0054 out->reserve(nsrc);
0055
0056 for (unsigned int i = 0; i < nsrc; ++i) {
0057 edm::Ptr<PATOBJ> ptr(src, i);
0058 reco::GenParticleRef match = (*matching)[ptr];
0059 out->emplace_back(src->at(i));
0060 out->back().addUserInt("mcMatch", match.isNonnull() ? match->pdgId() : 0);
0061 }
0062
0063
0064 evt.put(std::move(out));
0065 }
0066
0067 #include "DataFormats/PatCandidates/interface/Muon.h"
0068 typedef MatchEmbedder<pat::Muon> MuonMatchEmbedder;
0069
0070 #include "DataFormats/PatCandidates/interface/Electron.h"
0071 typedef MatchEmbedder<pat::Electron> ElectronMatchEmbedder;
0072
0073 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0074 typedef MatchEmbedder<pat::CompositeCandidate> CompositeCandidateMatchEmbedder;
0075
0076 #include "FWCore/Framework/interface/MakerMacros.h"
0077 DEFINE_FWK_MODULE(MuonMatchEmbedder);
0078 DEFINE_FWK_MODULE(ElectronMatchEmbedder);
0079 DEFINE_FWK_MODULE(CompositeCandidateMatchEmbedder);