CMS 3D CMS Logo

LowPtGSFToPackedCandidateLinker.cc
Go to the documentation of this file.
1 #include <string>
2 
21 
23 public:
26 
27  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
29 
30 private:
39 };
40 
42  pfcands_{consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("PFCandidates"))},
43  packed_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedCandidates"))},
44  lost_tracks_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("lostTracks"))},
45  tracks_{consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))},
46  pf2packed_{consumes<edm::Association<pat::PackedCandidateCollection> >(iConfig.getParameter<edm::InputTag>("packedCandidates"))},
47  lost2trk_{consumes<edm::Association<pat::PackedCandidateCollection> >(iConfig.getParameter<edm::InputTag>("lostTracks"))},
48  gsf2trk_{consumes<edm::Association<reco::TrackCollection> >(iConfig.getParameter<edm::InputTag>("gsfToTrack"))},
49  gsftracks_{consumes<std::vector<reco::GsfTrack> >(iConfig.getParameter<edm::InputTag>("gsfTracks"))} {
50  produces< edm::Association<pat::PackedCandidateCollection> > ("packedCandidates");
51  produces< edm::Association<pat::PackedCandidateCollection> > ("lostTracks");
52  }
53 
55 
58  iEvent.getByToken(pfcands_, pfcands);
59 
61  iEvent.getByToken(packed_, packed);
62 
64  iEvent.getByToken(lost_tracks_, lost_tracks);
65 
67  iEvent.getByToken(pf2packed_, pf2packed);
68 
70  iEvent.getByToken(lost2trk_, lost2trk_assoc);
71 
73  iEvent.getByToken(gsftracks_, gsftracks);
74 
76  iEvent.getByToken(tracks_, tracks);
77 
79  iEvent.getByToken(gsf2trk_, gsf2trk);
80 
81  // collection sizes, for reference
82  const size_t npf = pfcands->size();
83  const size_t npacked = packed->size();
84  const size_t nlost = lost_tracks->size();
85  const size_t ntracks = tracks->size();
86  const size_t ngsf = gsftracks->size();
87 
88  //store index mapping in vectors for easy and fast access
89  std::vector<size_t> trk2packed(ntracks, npacked);
90  std::vector<size_t> trk2lost(ntracks, nlost);
91 
92  //store auxiliary mappings for association
93  std::vector<int> gsf2pack(ngsf, -1);
94  std::vector<int> gsf2lost(ngsf, -1);
95 
96  //electrons will never store their track (they store the Gsf track)
97  //map PackedPF <--> Track
98  for(unsigned int icand=0; icand < npf; ++icand) {
99  edm::Ref<reco::PFCandidateCollection> pf_ref(pfcands,icand);
100  const reco::PFCandidate &cand = pfcands->at(icand);
101  auto packed_ref = (*pf2packed)[pf_ref];
102  if(cand.charge() && packed_ref.isNonnull() && cand.trackRef().isNonnull()
103  && cand.trackRef().id() == tracks.id() ) {
104  size_t trkid = cand.trackRef().index();
105  size_t packid = packed_ref.index();
106  trk2packed[trkid] = packid;
107  }
108  }
109 
110  //map LostTrack <--> Track
111  for(unsigned int itrk=0; itrk < ntracks; ++itrk) {
112  reco::TrackRef key(tracks, itrk);
113  pat::PackedCandidateRef lostTrack = (*lost2trk_assoc)[key];
114  if(lostTrack.isNonnull()) {
115  size_t ilost = lostTrack.index(); //assumes that LostTracks are all made from the same track collection
116  trk2lost[itrk] = ilost;
117  }
118  }
119 
120  //map Track --> GSF and fill GSF --> PackedCandidates and GSF --> Lost associations
121  for(unsigned int igsf=0; igsf < ngsf; ++igsf) {
122  reco::GsfTrackRef gref(gsftracks, igsf);
123  reco::TrackRef trk = (*gsf2trk)[gref];
124  if(trk.id() != tracks.id()) {
125  throw cms::Exception("WrongCollection", "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
126  }
127  size_t trkid = trk.index();
128 
129  if(trk2packed[trkid] != npacked) {
130  gsf2pack[igsf] = trk2packed[trkid];
131  }
132  if(trk2lost[trkid] != nlost) {
133  gsf2lost[igsf] = trk2lost[trkid];
134  }
135  }
136 
137  // create output collections from the mappings
138  auto assoc_gsf2pack = std::make_unique< edm::Association<pat::PackedCandidateCollection> >(packed);
139  edm::Association<pat::PackedCandidateCollection>::Filler gsf2pack_filler(*assoc_gsf2pack);
140  gsf2pack_filler.insert(gsftracks, gsf2pack.begin(), gsf2pack.end());
141  gsf2pack_filler.fill();
142  iEvent.put(std::move(assoc_gsf2pack), "packedCandidates");
143 
144  auto assoc_gsf2lost = std::make_unique< edm::Association<pat::PackedCandidateCollection> >(lost_tracks);
145  edm::Association<pat::PackedCandidateCollection>::Filler gsf2lost_filler(*assoc_gsf2lost);
146  gsf2lost_filler.insert(gsftracks, gsf2lost.begin(), gsf2lost.end());
147  gsf2lost_filler.fill();
148  iEvent.put(std::move(assoc_gsf2lost), "lostTracks");
149 }
150 
153  desc.add<edm::InputTag>("PFCandidates", edm::InputTag("particleFlow"));
154  desc.add<edm::InputTag>("packedCandidates", edm::InputTag("packedPFCandidates"));
155  desc.add<edm::InputTag>("lostTracks", edm::InputTag("lostTracks"));
156  desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
157  desc.add<edm::InputTag>("gsfToTrack", edm::InputTag("lowPtGsfToTrackLinks"));
158  desc.add<edm::InputTag>("gsfTracks", edm::InputTag("lowPtGsfEleGsfTracks"));
159  descriptions.add("lowPtGsfLinksDefault", desc);
160 }
161 
T getParameter(std::string const &) const
const edm::EDGetTokenT< pat::PackedCandidateCollection > packed_
const edm::EDGetTokenT< reco::PFCandidateCollection > pfcands_
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > pf2packed_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
const edm::EDGetTokenT< std::vector< reco::GsfTrack > > gsftracks_
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
const edm::EDGetTokenT< reco::TrackCollection > tracks_
key_type index() const
Definition: Ref.h:266
int charge() const final
electric charge
Definition: LeafCandidate.h:91
ProductID id() const
Accessor for product ID.
Definition: Ref.h:257
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::EDGetTokenT< pat::PackedCandidateCollection > lost_tracks_
const edm::EDGetTokenT< edm::Association< reco::TrackCollection > > gsf2trk_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > lost2trk_
static void fillDescriptions(edm::ConfigurationDescriptions &)
LowPtGSFToPackedCandidateLinker(const edm::ParameterSet &)
def move(src, dest)
Definition: eostools.py:511