CMS 3D CMS Logo

TrackMergeremb.cc
Go to the documentation of this file.
2 
3 #include <memory>
4 
6 
11 
14 
17 
20 
23 
26 
38 
41 
44 
52 
53 template <typename T1>
55  alias = iConfig.getParameter<std::string>("@module_label");
56  std::vector<edm::InputTag> inCollections = iConfig.getParameter<std::vector<edm::InputTag>>("mergCollections");
57  globalGeomToken_ = esConsumes();
58  for (const auto &inCollection : inCollections) {
59  inputs_[inCollection.instance()].push_back(consumes<TrackCollectionemb>(inCollection));
60  }
61  willconsume(iConfig);
62  for (const auto &toproduce : inputs_) {
63  willproduce(toproduce.first, alias);
64  }
65 }
66 
67 template <typename T1>
69  // nothing to be done yet...
70 }
71 
72 template <typename T1>
74  geometry_ = &iSetup.getData(globalGeomToken_);
75  for (auto input_ : inputs_) {
76  merg_and_put(iEvent, input_.first, input_.second);
77 
78  } // end instance
79 }
80 
81 template <typename T1>
83  produces<TrackCollectionemb>(instance);
84 }
85 
86 template <typename T1>
88 
89 template <typename T1>
93  std::unique_ptr<TrackCollectionemb> outTracks = std::unique_ptr<TrackCollectionemb>(new TrackCollectionemb);
94 
95  for (auto akt_collection : to_merge) {
97  iEvent.getByToken(akt_collection, track_col_in);
98 
99  size_t sedref_it = 0;
100  for (typename TrackCollectionemb::const_iterator it = track_col_in->begin(); it != track_col_in->end();
101  ++it, ++sedref_it) {
102  outTracks->push_back(typename T1::value_type(*it));
103  }
104 
105  } // end merge
106 
107  iEvent.put(std::move(outTracks), instance);
108 }
109 
110 template <>
112  produces<reco::TrackCollection>(instance).setBranchAlias(alias + "Tracks");
113  produces<reco::TrackExtraCollection>(instance).setBranchAlias(alias + "TrackExtras");
114  produces<TrackingRecHitCollection>(instance).setBranchAlias(alias + "RecHits");
115  produces<TrackToTrackMapnew>();
116 }
117 
118 template <>
121  std::unique_ptr<reco::TrackCollection> outTracks = std::make_unique<reco::TrackCollection>();
122  std::unique_ptr<reco::TrackExtraCollection> outTracks_ex = std::make_unique<reco::TrackExtraCollection>();
123  std::unique_ptr<TrackingRecHitCollection> outTracks_rh = std::make_unique<TrackingRecHitCollection>();
124  std::unique_ptr<TrackToTrackMapnew> outTracks_refs = std::make_unique<TrackToTrackMapnew>();
125 
126  auto rTrackExtras = iEvent.getRefBeforePut<reco::TrackExtraCollection>();
127 
128  std::vector<reco::TrackRefVector> trackRefColl;
129 
130  for (auto akt_collection : to_merge) {
132  iEvent.getByToken(akt_collection, track_col_in);
133 
134  unsigned sedref_it = 0;
135  for (reco::TrackCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
136  ++it, ++sedref_it) {
137  outTracks->push_back(reco::Track(*it));
138  auto rechits = it->recHits();
139  // Fixing geometry records of detector components for tracking rec. hits
140  for (auto ith = rechits.begin(); ith != rechits.end(); ith++) {
141  auto hit = *(&(*ith));
142  hit->setDet(*geometry_->idToDet(hit->rawId()));
143  }
144  outTracks_ex->push_back(reco::TrackExtra(*it->extra()));
145  outTracks->back().setExtra(reco::TrackExtraRef(rTrackExtras, outTracks_ex->size() - 1));
146  reco::TrackRef trackRefold(track_col_in, sedref_it);
147 
148  reco::TrackRefVector trackRefColl_helpvec;
149  trackRefColl_helpvec.push_back(trackRefold);
150  trackRefColl.push_back(trackRefColl_helpvec);
151  }
152 
153  } // end merge
154 
155  edm::OrphanHandle<reco::TrackCollection> trackHandle = iEvent.put(std::move(outTracks), instance);
156  iEvent.put(std::move(outTracks_ex), instance);
157 
158  TrackToTrackMapnew::Filler filler(*outTracks_refs);
159  filler.insert(trackHandle, trackRefColl.begin(), trackRefColl.end());
160  filler.fill();
161 
162  iEvent.put(std::move(outTracks_refs));
163  iEvent.put(std::move(outTracks_rh), instance); // not implemented so far
164 }
165 
166 template <>
168  // track refs for trackerdriven seeds
169  inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("generalTracks"));
170  inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("generalTracks"));
171  inputs_rElectronMergedSeeds_ = consumes<reco::ElectronSeedCollection>(edm::InputTag("electronMergedSeeds"));
172  inputs_rElectronMergedSeedViews_ = consumes<edm::View<TrajectorySeed>>(edm::InputTag("electronMergedSeeds"));
173 }
174 
175 template <>
177  produces<reco::GsfTrackCollection>(instance).setBranchAlias(alias + "GsfTracks");
178  produces<reco::TrackExtraCollection>(instance).setBranchAlias(alias + "TrackExtras");
179  produces<reco::GsfTrackExtraCollection>(instance).setBranchAlias(alias + "GsfTrackExtras");
180  produces<TrackingRecHitCollection>(instance).setBranchAlias(alias + "RecHits");
181  produces<GsfTrackToTrackMapnew>();
182 }
183 
184 template <>
187  std::unique_ptr<reco::GsfTrackCollection> outTracks = std::make_unique<reco::GsfTrackCollection>();
188  std::unique_ptr<reco::TrackExtraCollection> outTracks_ex = std::make_unique<reco::TrackExtraCollection>();
189  std::unique_ptr<reco::GsfTrackExtraCollection> outTracks_exgsf = std::make_unique<reco::GsfTrackExtraCollection>();
190  std::unique_ptr<TrackingRecHitCollection> outTracks_rh = std::make_unique<TrackingRecHitCollection>();
191  std::unique_ptr<GsfTrackToTrackMapnew> outTracks_refs = std::make_unique<GsfTrackToTrackMapnew>();
192 
193  auto rTrackExtras = iEvent.getRefBeforePut<reco::TrackExtraCollection>();
194  auto rTrackExtras_gsf = iEvent.getRefBeforePut<reco::GsfTrackExtraCollection>();
195 
196  auto rHits = iEvent.getRefBeforePut<TrackingRecHitCollection>();
197  std::vector<reco::GsfTrackRefVector> trackRefColl;
198 
199  // track to track map for trackerdriven seed fix
200  edm::Handle<TrackToTrackMapnew> track_ref_map;
201  iEvent.getByToken(inputs_fixtrackrefs_, track_ref_map);
202 
204  iEvent.getByToken(inputs_fixtrackcol_, track_new_col);
205 
206  std::map<reco::TrackRef, reco::TrackRef> simple_track_to_track_map;
207  for (unsigned abc = 0; abc < track_new_col->size(); ++abc) {
208  reco::TrackRef trackRef(track_new_col, abc);
209  simple_track_to_track_map[trackRef] = trackRef;
210  simple_track_to_track_map[((*track_ref_map)[trackRef])[0]] = trackRef;
211  }
212 
213  // merge begin
214  for (auto akt_collection : to_merge) {
216  iEvent.getByToken(akt_collection, track_col_in);
217 
218  size_t sedref_it = 0;
219  for (reco::GsfTrackCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
220  ++it, ++sedref_it) {
221  reco::ElectronSeedRef seed = it->seedRef().castTo<reco::ElectronSeedRef>();
222  (const_cast<reco::ElectronSeed *>(seed.get()))->setCtfTrack(simple_track_to_track_map[seed->ctfTrack()]);
223  outTracks->push_back(reco::GsfTrack(*it));
224  auto rechits = it->recHits();
225  // Fixing geometry records of detector components for tracking rec. hits
226  for (auto ith = rechits.begin(); ith != rechits.end(); ith++) {
227  auto hit = *(&(*ith));
228  hit->setDet(*geometry_->idToDet(hit->rawId()));
229  }
230  outTracks_ex->push_back(reco::TrackExtra(*it->extra()));
231  outTracks_exgsf->push_back(reco::GsfTrackExtra(*it->gsfExtra()));
232 
233  outTracks->back().setExtra(reco::TrackExtraRef(rTrackExtras, outTracks_ex->size() - 1));
234  outTracks->back().setGsfExtra(reco::GsfTrackExtraRef(rTrackExtras_gsf, outTracks_exgsf->size() - 1));
235 
236  reco::GsfTrackRef trackRefold(track_col_in, sedref_it);
237  reco::GsfTrackRefVector trackRefColl_helpvec;
238  trackRefColl_helpvec.push_back(trackRefold);
239  trackRefColl.push_back(trackRefColl_helpvec);
240  }
241 
242  } // end merge
243 
245  GsfTrackToTrackMapnew::Filler filler(*outTracks_refs);
246  filler.insert(trackHandle, trackRefColl.begin(), trackRefColl.end());
247  filler.fill();
248 
250  iEvent.getByToken(inputs_rElectronMergedSeeds_, elSeeds);
251  auto bElSeeds = elSeeds->cbegin();
252  auto eElSeeds = elSeeds->cend();
253 
254  edm::Handle<edm::View<TrajectorySeed>> seedViewsHandle;
255  iEvent.getByToken(inputs_rElectronMergedSeedViews_, seedViewsHandle);
256 
257  std::unique_ptr<reco::TrackExtraCollection> outTracks_ex_new = std::make_unique<reco::TrackExtraCollection>();
258 
259  // fix track extras to new merged seeds
260  for (typename reco::TrackExtraCollection::const_iterator tex = outTracks_ex->begin(); tex != outTracks_ex->end();
261  ++tex) {
262  reco::TrackExtra newTrackExtra(*tex);
263 
264  if (tex->seedRef().isAvailable()) {
265  const reco::ElectronSeedRef &trSeedRef(tex->seedRef().castTo<reco::ElectronSeedRef>());
266  if (trSeedRef.isAvailable()) {
267  // find new seed with corresponding supercluster or ctfTrack
268  // note that seed can be both Ecal- and Tracker-driven
269  size_t sedref_it = 0;
270  for (auto tseed = bElSeeds; tseed != eElSeeds; ++tseed, ++sedref_it) {
271  const reco::ElectronSeedRef elSeedRef(elSeeds, sedref_it);
272 
273  if (trSeedRef->isEcalDriven() && elSeedRef->isEcalDriven()) {
274  // match seeds by pair of detIds
275  if (trSeedRef->detId(0) == elSeedRef->detId(0) && trSeedRef->detId(1) == elSeedRef->detId(1)) {
276  edm::RefToBase<TrajectorySeed> traSeedRef(seedViewsHandle, sedref_it);
277  newTrackExtra.setSeedRef(traSeedRef);
278  }
279  }
280  if (trSeedRef->isTrackerDriven() && elSeedRef->isTrackerDriven()) {
281  // if tracker driven, check for same ctf track
282  if (simple_track_to_track_map[trSeedRef->ctfTrack()] == elSeedRef->ctfTrack()) {
283  edm::RefToBase<TrajectorySeed> traSeedRef(seedViewsHandle, sedref_it);
284  newTrackExtra.setSeedRef(traSeedRef);
285  }
286  }
287  }
288  }
289  }
290 
291  outTracks_ex_new->push_back(newTrackExtra);
292  }
293 
294  iEvent.put(std::move(outTracks_refs));
295  iEvent.put(std::move(outTracks_ex_new), instance);
296  iEvent.put(std::move(outTracks_exgsf), instance);
297  iEvent.put(std::move(outTracks_rh), instance);
298 }
299 
300 template <>
302  produces<reco::MuonCollection>();
303  produces<reco::CaloMuonCollection>();
304  produces<reco::MuonTimeExtraMap>("combined");
305  produces<reco::MuonTimeExtraMap>("dt");
306  produces<reco::MuonTimeExtraMap>("csc");
307 
308  // todo make this configurable (or not )
309  produces<reco::IsoDepositMap>("tracker");
310  produces<reco::IsoDepositMap>("ecal");
311  produces<reco::IsoDepositMap>("hcal");
312  produces<reco::IsoDepositMap>("ho");
313  produces<reco::IsoDepositMap>("jets");
314 
315  produces<reco::MuonToMuonMap>();
316 }
317 
318 template <>
320  if (alias == "displacedMuons1stStep") {
321  inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("displacedTracks"));
322  inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("displacedTracks"));
323  } else {
324  inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("generalTracks"));
325  inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("generalTracks"));
326  }
327 }
328 
329 template <>
333  std::unique_ptr<reco::MuonCollection> outTracks = std::make_unique<reco::MuonCollection>();
334  std::unique_ptr<reco::CaloMuonCollection> calomu =
335  std::make_unique<reco::CaloMuonCollection>(); // not implemented so far
336 
337  edm::Handle<TrackToTrackMapnew> track_ref_map;
338  iEvent.getByToken(inputs_fixtrackrefs_, track_ref_map);
339 
341  iEvent.getByToken(inputs_fixtrackcol_, track_new_col);
342  std::map<reco::TrackRef, reco::TrackRef> simple_track_to_track_map;
343  for (unsigned abc = 0; abc < track_new_col->size(); ++abc) {
344  reco::TrackRef trackRef(track_new_col, abc);
345  simple_track_to_track_map[trackRef] =
346  trackRef; // catch the case, where a seed ctf track could already be updated, such that the update doesn't happen again
347  simple_track_to_track_map[((*track_ref_map)[trackRef])[0]] = trackRef;
348  }
349 
350  std::vector<reco::MuonRef> muonRefColl;
351  reco::MuonRefProd outputMuonsRefProd = iEvent.getRefBeforePut<reco::MuonCollection>();
352  unsigned new_idx = 0;
353  for (auto akt_collection : to_merge) {
355  iEvent.getByToken(akt_collection, track_col_in);
356  unsigned old_idx = 0;
357  for (reco::MuonCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
358  ++it, ++old_idx, ++new_idx) {
359  outTracks->push_back(reco::Muon(*it));
360  reco::MuonRef muRefold(track_col_in, old_idx);
361  muonRefColl.push_back(muRefold);
362  reco::MuonRef muRefnew(outputMuonsRefProd, new_idx);
363 
364  if (it->track().isNonnull()) {
365  outTracks->back().setTrack(simple_track_to_track_map[it->track()]);
366  }
367  }
368 
369  } // end merge
370 
371  const int nMuons = outTracks->size();
372 
373  std::vector<reco::MuonTimeExtra> dtTimeColl(nMuons);
374  std::vector<reco::MuonTimeExtra> cscTimeColl(nMuons);
375  std::vector<reco::MuonTimeExtra> combinedTimeColl(nMuons);
376  std::vector<reco::IsoDeposit> trackDepColl(nMuons);
377  std::vector<reco::IsoDeposit> ecalDepColl(nMuons);
378  std::vector<reco::IsoDeposit> hcalDepColl(nMuons);
379  std::vector<reco::IsoDeposit> hoDepColl(nMuons);
380  std::vector<reco::IsoDeposit> jetDepColl(nMuons);
381 
382  edm::OrphanHandle<reco::MuonCollection> muonHandle = iEvent.put(std::move(outTracks));
383 
384  auto fillMap = [](auto refH, auto &vec, edm::Event &ev, const std::string &cAl = "") {
386  std::unique_ptr<MapType> oMap(new MapType());
387  {
388  typename MapType::Filler filler(*oMap);
389  filler.insert(refH, vec.begin(), vec.end());
390  vec.clear();
391  filler.fill();
392  }
393  ev.put(std::move(oMap), cAl);
394  };
395 
396  fillMap(muonHandle, combinedTimeColl, iEvent, "combined");
397  fillMap(muonHandle, dtTimeColl, iEvent, "dt");
398  fillMap(muonHandle, cscTimeColl, iEvent, "csc");
399  fillMap(muonHandle, trackDepColl, iEvent, "tracker");
400  fillMap(muonHandle, ecalDepColl, iEvent, "ecal");
401  fillMap(muonHandle, hcalDepColl, iEvent, "hcal");
402  fillMap(muonHandle, hoDepColl, iEvent, "ho");
403  fillMap(muonHandle, jetDepColl, iEvent, "jets");
404  fillMap(muonHandle, muonRefColl, iEvent);
405  iEvent.put(std::move(calomu));
406 }
407 
408 template <>
410  produces<reco::PFCandidateCollection>(instance);
411 }
412 
413 template <>
415  inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("generalTracks"));
416  inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("generalTracks"));
417  inputs_fixgsftrackrefs_ = consumes<GsfTrackToTrackMapnew>(edm::InputTag("electronGsfTracks"));
418  inputs_fixgsftrackcol_ = consumes<reco::GsfTrackCollection>(edm::InputTag("electronGsfTracks"));
419  inputs_fixmurefs_ = consumes<reco::MuonToMuonMap>(edm::InputTag("muons1stStep"));
420  inputs_fixmucol_ = consumes<reco::MuonCollection>(edm::InputTag("muons1stStep"));
421  inputs_SC_ = consumes<reco::SuperClusterCollection>(edm::InputTag("particleFlowEGamma"));
422 }
423 
424 template <>
427  std::unique_ptr<reco::PFCandidateCollection> outTracks = std::make_unique<reco::PFCandidateCollection>();
428 
429  edm::Handle<TrackToTrackMapnew> track_ref_map;
430  iEvent.getByToken(inputs_fixtrackrefs_, track_ref_map);
431 
433  iEvent.getByToken(inputs_fixtrackcol_, track_new_col);
434  std::map<reco::TrackRef, reco::TrackRef> simple_track_to_track_map;
435  for (unsigned abc = 0; abc < track_new_col->size(); ++abc) {
436  reco::TrackRef trackRef(track_new_col, abc);
437  simple_track_to_track_map[trackRef] =
438  trackRef; // catch the case, where a seed ctf track could already be updated, such that the update doesn't happen again
439  simple_track_to_track_map[((*track_ref_map)[trackRef])[0]] = trackRef;
440  }
441 
442  edm::Handle<GsfTrackToTrackMapnew> gsftrack_ref_map;
443  iEvent.getByToken(inputs_fixgsftrackrefs_, gsftrack_ref_map);
444 
445  edm::Handle<reco::GsfTrackCollection> gsftrack_new_col;
446  iEvent.getByToken(inputs_fixgsftrackcol_, gsftrack_new_col);
447  std::map<reco::GsfTrackRef, reco::GsfTrackRef> simple_gsftrack_to_gsftrack_map;
448  for (unsigned abc = 0; abc < gsftrack_new_col->size(); ++abc) {
449  reco::GsfTrackRef gsfTrackRef(gsftrack_new_col, abc);
450  simple_gsftrack_to_gsftrack_map[((*gsftrack_ref_map)[gsfTrackRef])[0]] = gsfTrackRef;
451  }
452 
454  iEvent.getByToken(inputs_fixmurefs_, muon_ref_map);
455 
457  iEvent.getByToken(inputs_fixmucol_, muon_new_col);
458  std::map<reco::MuonRef, reco::MuonRef> simple_mu_to_mu_map;
459  for (unsigned abc = 0; abc < muon_new_col->size(); ++abc) {
460  reco::MuonRef muRef(muon_new_col, abc);
461  simple_mu_to_mu_map[(*muon_ref_map)[muRef]] = muRef;
462  }
463 
464  // used for photon matching
466  iEvent.getByToken(inputs_SC_, sCs);
467  auto bSc = sCs->cbegin();
468  auto eSc = sCs->cend();
469 
470  for (auto akt_collection : to_merge) {
472  iEvent.getByToken(akt_collection, track_col_in);
473  for (reco::PFCandidateCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end(); ++it) {
474  outTracks->push_back(reco::PFCandidate(*it));
475  if (it->trackRef().isNonnull() && outTracks->back().charge()) {
476  outTracks->back().setTrackRef(simple_track_to_track_map[it->trackRef()]);
477  }
478  if (it->gsfTrackRef().isNonnull()) {
479  outTracks->back().setGsfTrackRef(simple_gsftrack_to_gsftrack_map[it->gsfTrackRef()]);
480  }
481  if (it->superClusterRef().isNonnull()) {
482  const reco::SuperClusterRef &pfScRef(it->superClusterRef());
483 
484  float dx, dy, dz, dr;
485  float drMin = 10.0; // also used as treshold for matching
486  reco::SuperClusterRef ccrefMin;
487  for (auto sc = bSc; sc != eSc; ++sc) {
488  const reco::SuperClusterRef &scRef(reco::SuperClusterRef(sCs, std::distance(bSc, sc)));
489  dx = fabs(scRef->x() - pfScRef->x());
490  dy = fabs(scRef->y() - pfScRef->y());
491  dz = fabs(scRef->z() - pfScRef->z());
492  dr = sqrt(dx * dx + dy * dy + dz * dz);
493  if (dr < drMin) {
494  drMin = dr;
495  outTracks->back().setSuperClusterRef(scRef);
496  }
497  }
498  }
499  if (it->muonRef().isNonnull()) {
500  outTracks->back().setMuonRef(simple_mu_to_mu_map[it->muonRef()]);
501  }
502  }
503  } // end merge
504 
505  iEvent.put(std::move(outTracks), instance);
506 }
507 
509 
TrackMergeremb< reco::PFCandidateCollection > PFColMerger
void willconsume(const edm::ParameterSet &iConfig)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void setSeedRef(const edm::RefToBase< TrajectorySeed > &r)
Definition: TrackExtra.h:130
TrackMergeremb< reco::ConversionCollection > ConversionColMerger
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
TrackMergeremb< reco::MuonCollection > MuonColMerger
static PFTauRenderPlugin instance
void merg_and_put(edm::Event &, std::string, std::vector< edm::EDGetTokenT< TrackCollectionemb >> &)
std::vector< GsfTrackExtra > GsfTrackExtraCollection
collection of GsfTrackExtra objects
~TrackMergeremb() override
void willproduce(std::string instance, std::string alias)
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
int iEvent
Definition: GenABIO.cc:224
TrackMergeremb< reco::GsfElectronCollection > GsfElectronColMerger
tex
Definition: cuy.py:773
T sqrt(T t)
Definition: SSEVec.h:23
bool isAvailable() const
Definition: Ref.h:535
TrackMergeremb< reco::GsfTrackCollection > GsfTrackColMerger
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:10
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
TrackMergeremb< reco::PhotonCollection > PhotonColMerger
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
TrackMergeremb(const edm::ParameterSet &)
TrackMergeremb< reco::TrackCollection > TrackColMerger
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &, const edm::EventSetup &) override