Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:51

0001 #include <string>
0002 
0003 #include "DataFormats/Candidate/interface/Candidate.h"
0004 #include "DataFormats/Common/interface/RefToPtr.h"
0005 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0006 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0007 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
0008 #include "DataFormats/PatCandidates/interface/Electron.h"
0009 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0010 #include "DataFormats/Common/interface/Association.h"
0011 #include "FWCore/Framework/interface/global/EDProducer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 #include "DataFormats/ParticleFlowReco/interface/PreId.h"
0018 #include "DataFormats/ParticleFlowReco/interface/PreIdFwd.h"
0019 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
0020 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 
0023 typedef edm::Ptr<pat::PackedCandidate> PackedCandidatePtr;
0024 typedef std::vector<PackedCandidatePtr> PackedCandidatePtrCollection;
0025 
0026 class LowPtGSFToPackedCandidateLinker : public edm::global::EDProducer<> {
0027 public:
0028   explicit LowPtGSFToPackedCandidateLinker(const edm::ParameterSet&);
0029   ~LowPtGSFToPackedCandidateLinker() override;
0030 
0031   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0032   static void fillDescriptions(edm::ConfigurationDescriptions&);
0033 
0034 private:
0035   const edm::EDGetTokenT<reco::PFCandidateCollection> pfcands_;
0036   const edm::EDGetTokenT<pat::PackedCandidateCollection> packed_;
0037   const edm::EDGetTokenT<pat::PackedCandidateCollection> lost_tracks_;
0038   const edm::EDGetTokenT<reco::TrackCollection> tracks_;
0039   const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection> > pf2packed_;
0040   const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection> > lost2trk_;
0041   const edm::EDGetTokenT<edm::Association<reco::TrackCollection> > gsf2trk_;
0042   const edm::EDGetTokenT<std::vector<reco::GsfTrack> > gsftracks_;
0043   const edm::EDGetTokenT<std::vector<pat::Electron> > electrons_;
0044 };
0045 
0046 LowPtGSFToPackedCandidateLinker::LowPtGSFToPackedCandidateLinker(const edm::ParameterSet& iConfig)
0047     : pfcands_{consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("PFCandidates"))},
0048       packed_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedCandidates"))},
0049       lost_tracks_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("lostTracks"))},
0050       tracks_{consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))},
0051       pf2packed_{consumes<edm::Association<pat::PackedCandidateCollection> >(
0052           iConfig.getParameter<edm::InputTag>("packedCandidates"))},
0053       lost2trk_{consumes<edm::Association<pat::PackedCandidateCollection> >(
0054           iConfig.getParameter<edm::InputTag>("lostTracks"))},
0055       gsf2trk_{consumes<edm::Association<reco::TrackCollection> >(iConfig.getParameter<edm::InputTag>("gsfToTrack"))},
0056       gsftracks_{consumes<std::vector<reco::GsfTrack> >(iConfig.getParameter<edm::InputTag>("gsfTracks"))},
0057       electrons_{consumes<std::vector<pat::Electron> >(iConfig.getParameter<edm::InputTag>("electrons"))} {
0058   produces<edm::Association<pat::PackedCandidateCollection> >("gsf2packed");
0059   produces<edm::Association<pat::PackedCandidateCollection> >("gsf2lost");
0060   produces<edm::ValueMap<PackedCandidatePtr> >("ele2packed");
0061   produces<edm::ValueMap<PackedCandidatePtr> >("ele2lost");
0062 }
0063 
0064 LowPtGSFToPackedCandidateLinker::~LowPtGSFToPackedCandidateLinker() {}
0065 
0066 void LowPtGSFToPackedCandidateLinker::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0067   auto pfcands = iEvent.getHandle(pfcands_);
0068   auto packed = iEvent.getHandle(packed_);
0069   auto lost_tracks = iEvent.getHandle(lost_tracks_);
0070   auto pf2packed = iEvent.getHandle(pf2packed_);
0071   auto lost2trk_assoc = iEvent.getHandle(lost2trk_);
0072   auto gsftracks = iEvent.getHandle(gsftracks_);
0073   auto tracks = iEvent.getHandle(tracks_);
0074   auto gsf2trk = iEvent.getHandle(gsf2trk_);
0075   auto electrons = iEvent.getHandle(electrons_);
0076 
0077   // collection sizes, for reference
0078   const size_t npf = pfcands->size();
0079   const size_t npacked = packed->size();
0080   const size_t nlost = lost_tracks->size();
0081   const size_t ntracks = tracks->size();
0082   const size_t ngsf = gsftracks->size();
0083   const size_t nele = electrons->size();
0084 
0085   //store index mapping in vectors for easy and fast access
0086   std::vector<size_t> trk2packed(ntracks, npacked);
0087   std::vector<size_t> trk2lost(ntracks, nlost);
0088 
0089   //store auxiliary mappings for association
0090   std::vector<int> gsf2pack(ngsf, -1);
0091   std::vector<int> gsf2lost(ngsf, -1);
0092   PackedCandidatePtrCollection ele2packedptr(nele, PackedCandidatePtr(packed, -1));
0093   PackedCandidatePtrCollection ele2lostptr(nele, PackedCandidatePtr(lost_tracks, -1));
0094 
0095   //electrons will never store their track (they store the Gsf track)
0096   //map PackedPF <--> Track
0097   for (unsigned int icand = 0; icand < npf; ++icand) {
0098     edm::Ref<reco::PFCandidateCollection> pf_ref(pfcands, icand);
0099     const reco::PFCandidate& cand = pfcands->at(icand);
0100     auto packed_ref = (*pf2packed)[pf_ref];
0101     if (cand.charge() && packed_ref.isNonnull() && cand.trackRef().isNonnull() && cand.trackRef().id() == tracks.id()) {
0102       size_t trkid = cand.trackRef().index();
0103       trk2packed[trkid] = packed_ref.index();
0104     }
0105   }
0106 
0107   //map LostTrack <--> Track
0108   for (unsigned int itrk = 0; itrk < ntracks; ++itrk) {
0109     reco::TrackRef key(tracks, itrk);
0110     pat::PackedCandidateRef lostTrack = (*lost2trk_assoc)[key];
0111     if (lostTrack.isNonnull()) {
0112       trk2lost[itrk] = lostTrack.index();  // assumes that LostTracks are all made from the same track collection
0113     }
0114   }
0115 
0116   //map Track --> GSF and fill GSF --> PackedCandidates and GSF --> Lost associations
0117   for (unsigned int igsf = 0; igsf < ngsf; ++igsf) {
0118     reco::GsfTrackRef gsf_ref(gsftracks, igsf);
0119     reco::TrackRef trk_ref = (*gsf2trk)[gsf_ref];
0120     if (trk_ref.id() != tracks.id()) {
0121       throw cms::Exception(
0122           "WrongCollection",
0123           "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
0124     }
0125     size_t trkid = trk_ref.index();
0126     if (trk2packed[trkid] != npacked) {
0127       gsf2pack[igsf] = trk2packed[trkid];
0128     }
0129     if (trk2lost[trkid] != nlost) {
0130       gsf2lost[igsf] = trk2lost[trkid];
0131     }
0132   }
0133 
0134   //map Electron-->pat::PFCandidatePtr via Electron-->GsfTrack-->Track and Track-->pat::PFCandidatePtr
0135   for (unsigned int iele = 0; iele < nele; ++iele) {
0136     auto const& ele = (*electrons)[iele];
0137     reco::GsfTrackRef gsf_ref = ele.core()->gsfTrack();
0138     reco::TrackRef trk_ref = (*gsf2trk)[gsf_ref];
0139     if (trk_ref.id() != tracks.id()) {
0140       throw cms::Exception(
0141           "WrongCollection",
0142           "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
0143     }
0144     size_t trkid = trk_ref.index();
0145     auto packedIdx = trk2packed[trkid];
0146     if (packedIdx != npacked) {
0147       ele2packedptr[iele] = PackedCandidatePtr(packed, packedIdx);
0148     }
0149     auto lostIdx = trk2lost[trkid];
0150     if (lostIdx != nlost) {
0151       ele2lostptr[iele] = PackedCandidatePtr(lost_tracks, lostIdx);
0152     }
0153   }
0154 
0155   // create output collections from the mappings
0156   auto assoc_gsf2pack = std::make_unique<edm::Association<pat::PackedCandidateCollection> >(packed);
0157   edm::Association<pat::PackedCandidateCollection>::Filler gsf2pack_filler(*assoc_gsf2pack);
0158   gsf2pack_filler.insert(gsftracks, gsf2pack.begin(), gsf2pack.end());
0159   gsf2pack_filler.fill();
0160   iEvent.put(std::move(assoc_gsf2pack), "gsf2packed");
0161 
0162   auto assoc_gsf2lost = std::make_unique<edm::Association<pat::PackedCandidateCollection> >(lost_tracks);
0163   edm::Association<pat::PackedCandidateCollection>::Filler gsf2lost_filler(*assoc_gsf2lost);
0164   gsf2lost_filler.insert(gsftracks, gsf2lost.begin(), gsf2lost.end());
0165   gsf2lost_filler.fill();
0166   iEvent.put(std::move(assoc_gsf2lost), "gsf2lost");
0167 
0168   auto map_ele2packedptr = std::make_unique<edm::ValueMap<PackedCandidatePtr> >();
0169   edm::ValueMap<PackedCandidatePtr>::Filler ele2packedptr_filler(*map_ele2packedptr);
0170   ele2packedptr_filler.insert(electrons, ele2packedptr.begin(), ele2packedptr.end());
0171   ele2packedptr_filler.fill();
0172   iEvent.put(std::move(map_ele2packedptr), "ele2packed");
0173 
0174   auto map_ele2lostptr = std::make_unique<edm::ValueMap<PackedCandidatePtr> >();
0175   edm::ValueMap<PackedCandidatePtr>::Filler ele2lostptr_filler(*map_ele2lostptr);
0176   ele2lostptr_filler.insert(electrons, ele2lostptr.begin(), ele2lostptr.end());
0177   ele2lostptr_filler.fill();
0178   iEvent.put(std::move(map_ele2lostptr), "ele2lost");
0179 }
0180 
0181 void LowPtGSFToPackedCandidateLinker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0182   edm::ParameterSetDescription desc;
0183   desc.add<edm::InputTag>("PFCandidates", edm::InputTag("particleFlow"));
0184   desc.add<edm::InputTag>("packedCandidates", edm::InputTag("packedPFCandidates"));
0185   desc.add<edm::InputTag>("lostTracks", edm::InputTag("lostTracks"));
0186   desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
0187   desc.add<edm::InputTag>("gsfToTrack", edm::InputTag("lowPtGsfToTrackLinks"));
0188   desc.add<edm::InputTag>("gsfTracks", edm::InputTag("lowPtGsfEleGsfTracks"));
0189   desc.add<edm::InputTag>("electrons", edm::InputTag("selectedPatLowPtElectrons"));
0190   descriptions.add("lowPtGsfLinksDefault", desc);
0191 }
0192 
0193 DEFINE_FWK_MODULE(LowPtGSFToPackedCandidateLinker);