Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:34:05

0001 #include <string>
0002 
0003 #include "DataFormats/Candidate/interface/Candidate.h"
0004 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0005 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0006 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0007 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
0008 #include "DataFormats/Common/interface/Association.h"
0009 #include "FWCore/Framework/interface/global/EDProducer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 #include "DataFormats/ParticleFlowReco/interface/PreId.h"
0016 #include "DataFormats/ParticleFlowReco/interface/PreIdFwd.h"
0017 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
0018 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
0019 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
0020 #include "DataFormats/TrackReco/interface/Track.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 
0023 class LowPtGSFToTrackLinker : public edm::global::EDProducer<> {
0024 public:
0025   explicit LowPtGSFToTrackLinker(const edm::ParameterSet&);
0026 
0027   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0028   static void fillDescriptions(edm::ConfigurationDescriptions&);
0029 
0030 private:
0031   const edm::EDGetTokenT<reco::TrackCollection> tracks_;
0032   const edm::EDGetTokenT<std::vector<reco::PreId> > preid_;
0033   const edm::EDGetTokenT<std::vector<reco::GsfTrack> > gsftracks_;
0034 };
0035 
0036 LowPtGSFToTrackLinker::LowPtGSFToTrackLinker(const edm::ParameterSet& iConfig)
0037     : tracks_{consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))},
0038       preid_{consumes<std::vector<reco::PreId> >(iConfig.getParameter<edm::InputTag>("gsfPreID"))},
0039       gsftracks_{consumes<std::vector<reco::GsfTrack> >(iConfig.getParameter<edm::InputTag>("gsfTracks"))} {
0040   produces<edm::Association<reco::TrackCollection> >();
0041 }
0042 
0043 void LowPtGSFToTrackLinker::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const {
0044   auto gsftracks = iEvent.getHandle(gsftracks_);
0045   auto tracks = iEvent.getHandle(tracks_);
0046   auto preid = iEvent.getHandle(preid_);
0047 
0048   // collection sizes, for reference
0049   const size_t ngsf = gsftracks->size();
0050 
0051   //store mapping for association
0052   std::vector<int> gsf2track(ngsf, -1);
0053 
0054   //map Track --> GSF and fill GSF --> PackedCandidates and GSF --> Lost associations
0055   for (unsigned int igsf = 0; igsf < ngsf; ++igsf) {
0056     reco::GsfTrackRef gref(gsftracks, igsf);
0057     reco::TrackRef trk = preid->at(gref->seedRef().castTo<reco::ElectronSeedRef>().index()).trackRef();
0058 
0059     if (trk.id() != tracks.id()) {
0060       throw cms::Exception(
0061           "WrongCollection",
0062           "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
0063     }
0064 
0065     size_t trkid = trk.index();
0066     gsf2track[igsf] = trkid;
0067   }
0068 
0069   // create output collections from the mappings
0070   auto assoc = std::make_unique<edm::Association<reco::TrackCollection> >(tracks);
0071   edm::Association<reco::TrackCollection>::Filler filler(*assoc);
0072   filler.insert(gsftracks, gsf2track.begin(), gsf2track.end());
0073   filler.fill();
0074   iEvent.put(std::move(assoc));
0075 }
0076 
0077 void LowPtGSFToTrackLinker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0078   edm::ParameterSetDescription desc;
0079   desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
0080   desc.add<edm::InputTag>("gsfPreID", edm::InputTag("lowPtGsfElectronSeeds"));
0081   desc.add<edm::InputTag>("gsfTracks", edm::InputTag("lowPtGsfEleGsfTracks"));
0082   descriptions.add("lowPtGsfToTrackLinks", desc);
0083 }
0084 
0085 DEFINE_FWK_MODULE(LowPtGSFToTrackLinker);