CMS 3D CMS Logo

LowPtGSFToPackedCandidateLinker.cc
Go to the documentation of this file.
1 #include <string>
2 
22 
24 typedef std::vector<PackedCandidatePtr> PackedCandidatePtrCollection;
25 
27 public:
30 
31  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
33 
34 private:
44 };
45 
47  : pfcands_{consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("PFCandidates"))},
48  packed_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedCandidates"))},
49  lost_tracks_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("lostTracks"))},
50  tracks_{consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))},
51  pf2packed_{consumes<edm::Association<pat::PackedCandidateCollection> >(
52  iConfig.getParameter<edm::InputTag>("packedCandidates"))},
53  lost2trk_{consumes<edm::Association<pat::PackedCandidateCollection> >(
54  iConfig.getParameter<edm::InputTag>("lostTracks"))},
55  gsf2trk_{consumes<edm::Association<reco::TrackCollection> >(iConfig.getParameter<edm::InputTag>("gsfToTrack"))},
56  gsftracks_{consumes<std::vector<reco::GsfTrack> >(iConfig.getParameter<edm::InputTag>("gsfTracks"))},
57  electrons_{consumes<std::vector<pat::Electron> >(iConfig.getParameter<edm::InputTag>("electrons"))} {
58  produces<edm::Association<pat::PackedCandidateCollection> >("gsf2packed");
59  produces<edm::Association<pat::PackedCandidateCollection> >("gsf2lost");
60  produces<edm::ValueMap<PackedCandidatePtr> >("ele2packed");
61  produces<edm::ValueMap<PackedCandidatePtr> >("ele2lost");
62 }
63 
65 
67  auto pfcands = iEvent.getHandle(pfcands_);
68  auto packed = iEvent.getHandle(packed_);
69  auto lost_tracks = iEvent.getHandle(lost_tracks_);
70  auto pf2packed = iEvent.getHandle(pf2packed_);
71  auto lost2trk_assoc = iEvent.getHandle(lost2trk_);
72  auto gsftracks = iEvent.getHandle(gsftracks_);
73  auto tracks = iEvent.getHandle(tracks_);
74  auto gsf2trk = iEvent.getHandle(gsf2trk_);
75  auto electrons = iEvent.getHandle(electrons_);
76 
77  // collection sizes, for reference
78  const size_t npf = pfcands->size();
79  const size_t npacked = packed->size();
80  const size_t nlost = lost_tracks->size();
81  const size_t ntracks = tracks->size();
82  const size_t ngsf = gsftracks->size();
83  const size_t nele = electrons->size();
84 
85  //store index mapping in vectors for easy and fast access
86  std::vector<size_t> trk2packed(ntracks, npacked);
87  std::vector<size_t> trk2lost(ntracks, nlost);
88 
89  //store auxiliary mappings for association
90  std::vector<int> gsf2pack(ngsf, -1);
91  std::vector<int> gsf2lost(ngsf, -1);
92  PackedCandidatePtrCollection ele2packedptr(nele, PackedCandidatePtr(packed, -1));
93  PackedCandidatePtrCollection ele2lostptr(nele, PackedCandidatePtr(lost_tracks, -1));
94 
95  //electrons will never store their track (they store the Gsf track)
96  //map PackedPF <--> Track
97  for (unsigned int icand = 0; icand < npf; ++icand) {
99  const reco::PFCandidate& cand = pfcands->at(icand);
100  auto packed_ref = (*pf2packed)[pf_ref];
101  if (cand.charge() && packed_ref.isNonnull() && cand.trackRef().isNonnull() && cand.trackRef().id() == tracks.id()) {
102  size_t trkid = cand.trackRef().index();
103  trk2packed[trkid] = packed_ref.index();
104  }
105  }
106 
107  //map LostTrack <--> Track
108  for (unsigned int itrk = 0; itrk < ntracks; ++itrk) {
109  reco::TrackRef key(tracks, itrk);
110  pat::PackedCandidateRef lostTrack = (*lost2trk_assoc)[key];
111  if (lostTrack.isNonnull()) {
112  trk2lost[itrk] = lostTrack.index(); // assumes that LostTracks are all made from the same track collection
113  }
114  }
115 
116  //map Track --> GSF and fill GSF --> PackedCandidates and GSF --> Lost associations
117  for (unsigned int igsf = 0; igsf < ngsf; ++igsf) {
118  reco::GsfTrackRef gsf_ref(gsftracks, igsf);
119  reco::TrackRef trk_ref = (*gsf2trk)[gsf_ref];
120  if (trk_ref.id() != tracks.id()) {
121  throw cms::Exception(
122  "WrongCollection",
123  "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
124  }
125  size_t trkid = trk_ref.index();
126  if (trk2packed[trkid] != npacked) {
127  gsf2pack[igsf] = trk2packed[trkid];
128  }
129  if (trk2lost[trkid] != nlost) {
130  gsf2lost[igsf] = trk2lost[trkid];
131  }
132  }
133 
134  //map Electron-->pat::PFCandidatePtr via Electron-->GsfTrack-->Track and Track-->pat::PFCandidatePtr
135  for (unsigned int iele = 0; iele < nele; ++iele) {
136  auto const& ele = (*electrons)[iele];
137  reco::GsfTrackRef gsf_ref = ele.core()->gsfTrack();
138  reco::TrackRef trk_ref = (*gsf2trk)[gsf_ref];
139  if (trk_ref.id() != tracks.id()) {
140  throw cms::Exception(
141  "WrongCollection",
142  "The reco::Track collection used to match against the GSF Tracks was not used to produce such tracks");
143  }
144  size_t trkid = trk_ref.index();
145  auto packedIdx = trk2packed[trkid];
146  if (packedIdx != npacked) {
147  ele2packedptr[iele] = PackedCandidatePtr(packed, packedIdx);
148  }
149  auto lostIdx = trk2lost[trkid];
150  if (lostIdx != nlost) {
151  ele2lostptr[iele] = PackedCandidatePtr(lost_tracks, lostIdx);
152  }
153  }
154 
155  // create output collections from the mappings
156  auto assoc_gsf2pack = std::make_unique<edm::Association<pat::PackedCandidateCollection> >(packed);
157  edm::Association<pat::PackedCandidateCollection>::Filler gsf2pack_filler(*assoc_gsf2pack);
158  gsf2pack_filler.insert(gsftracks, gsf2pack.begin(), gsf2pack.end());
159  gsf2pack_filler.fill();
160  iEvent.put(std::move(assoc_gsf2pack), "gsf2packed");
161 
162  auto assoc_gsf2lost = std::make_unique<edm::Association<pat::PackedCandidateCollection> >(lost_tracks);
163  edm::Association<pat::PackedCandidateCollection>::Filler gsf2lost_filler(*assoc_gsf2lost);
164  gsf2lost_filler.insert(gsftracks, gsf2lost.begin(), gsf2lost.end());
165  gsf2lost_filler.fill();
166  iEvent.put(std::move(assoc_gsf2lost), "gsf2lost");
167 
168  auto map_ele2packedptr = std::make_unique<edm::ValueMap<PackedCandidatePtr> >();
169  edm::ValueMap<PackedCandidatePtr>::Filler ele2packedptr_filler(*map_ele2packedptr);
170  ele2packedptr_filler.insert(electrons, ele2packedptr.begin(), ele2packedptr.end());
171  ele2packedptr_filler.fill();
172  iEvent.put(std::move(map_ele2packedptr), "ele2packed");
173 
174  auto map_ele2lostptr = std::make_unique<edm::ValueMap<PackedCandidatePtr> >();
175  edm::ValueMap<PackedCandidatePtr>::Filler ele2lostptr_filler(*map_ele2lostptr);
176  ele2lostptr_filler.insert(electrons, ele2lostptr.begin(), ele2lostptr.end());
177  ele2lostptr_filler.fill();
178  iEvent.put(std::move(map_ele2lostptr), "ele2lost");
179 }
180 
183  desc.add<edm::InputTag>("PFCandidates", edm::InputTag("particleFlow"));
184  desc.add<edm::InputTag>("packedCandidates", edm::InputTag("packedPFCandidates"));
185  desc.add<edm::InputTag>("lostTracks", edm::InputTag("lostTracks"));
186  desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
187  desc.add<edm::InputTag>("gsfToTrack", edm::InputTag("lowPtGsfToTrackLinks"));
188  desc.add<edm::InputTag>("gsfTracks", edm::InputTag("lowPtGsfEleGsfTracks"));
189  desc.add<edm::InputTag>("electrons", edm::InputTag("selectedPatLowPtElectrons"));
190  descriptions.add("lowPtGsfLinksDefault", desc);
191 }
192 
const edm::EDGetTokenT< pat::PackedCandidateCollection > packed_
const edm::EDGetTokenT< reco::PFCandidateCollection > pfcands_
const edm::EDGetTokenT< std::vector< reco::GsfTrack > > gsftracks_
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > pf2packed_
ProductID id() const
Accessor for product ID.
Definition: Ref.h:244
edm::Ptr< pat::PackedCandidate > PackedCandidatePtr
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::EDGetTokenT< reco::TrackCollection > tracks_
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
std::vector< PackedCandidatePtr > PackedCandidatePtrCollection
int iEvent
Definition: GenABIO.cc:224
const edm::EDGetTokenT< pat::PackedCandidateCollection > lost_tracks_
const edm::EDGetTokenT< edm::Association< reco::TrackCollection > > gsf2trk_
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
auto const & tracks
cannot be loose
key_type index() const
Definition: Ref.h:253
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
const edm::EDGetTokenT< edm::Association< pat::PackedCandidateCollection > > lost2trk_
const edm::EDGetTokenT< std::vector< pat::Electron > > electrons_
static void fillDescriptions(edm::ConfigurationDescriptions &)
LowPtGSFToPackedCandidateLinker(const edm::ParameterSet &)
def move(src, dest)
Definition: eostools.py:511