CMS 3D CMS Logo

MuonToTrackingParticleAssociatorByHitsImpl.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SimMuon/MCTruth
4 // Class : MuonToTrackingParticleAssociatorByHitsImpl
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Wed, 07 Jan 2015 21:35:56 GMT
11 //
12 
13 // system include files
14 
15 // user include files
18 
19 //
20 // constants, enums and typedefs
21 //
22 
23 //
24 // static data member definitions
25 //
26 
27 //
28 // constructors and destructor
29 //
31  TrackerMuonHitExtractor const &iHitExtractor,
32  MuonAssociatorByHitsHelper::Resources const &iResources,
33  MuonAssociatorByHitsHelper const *iHelper)
34  : m_hitExtractor(&iHitExtractor), m_resources(iResources), m_helper(iHelper) {}
35 
36 //
37 // member functions
38 //
39 
40 //
41 // const member functions
42 //
44  reco::MuonToSimCollection &recToSim,
45  reco::SimToMuonCollection &simToRec,
51  edm::OwnVector<TrackingRecHit> allTMRecHits; // this I will fill in only for tracker muon hits from
52  // segments
53 
54  std::vector<edm::OwnVector<TrackingRecHit>> TMRecHits; // used for case GlbOrTrk
56 
57  switch (type) {
58  case reco::InnerTk:
59  for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
61  if (mur->track().isNonnull()) {
62  muonHitRefs.push_back(std::make_pair(mur->track()->recHitsBegin(), mur->track()->recHitsEnd()));
63  } else {
64  muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
65  }
66  }
67  break;
68  case reco::OuterTk:
69  for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
71  if (mur->outerTrack().isNonnull()) {
72  muonHitRefs.push_back(std::make_pair(mur->outerTrack()->recHitsBegin(), mur->outerTrack()->recHitsEnd()));
73  } else {
74  muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
75  }
76  }
77  break;
78  case reco::GlobalTk:
79  for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
81  if (mur->globalTrack().isNonnull()) {
82  muonHitRefs.push_back(std::make_pair(mur->globalTrack()->recHitsBegin(), mur->globalTrack()->recHitsEnd()));
83  } else {
84  muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
85  }
86  }
87  break;
88  case reco::Segments: {
89  // puts hits in the vector, and record indices
90  std::vector<std::pair<size_t, size_t>> muonHitIndices;
91  for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
93  std::pair<size_t, size_t> indices(allTMRecHits.size(), allTMRecHits.size());
94  if (mur->isTrackerMuon()) {
95  std::vector<const TrackingRecHit *> hits = m_hitExtractor->getMuonHits(*mur);
96  for (std::vector<const TrackingRecHit *>::const_iterator ith = hits.begin(), edh = hits.end(); ith != edh;
97  ++ith) {
98  allTMRecHits.push_back(**ith);
99  }
100  indices.second += hits.size();
101  }
102  muonHitIndices.push_back(indices);
103  }
104  // convert indices into pairs of iterators to references
105  typedef std::pair<size_t, size_t> index_pair;
106  trackingRecHit_iterator hitRefBegin = allTMRecHits.data().begin();
107  for (std::vector<std::pair<size_t, size_t>>::const_iterator idxs = muonHitIndices.begin(),
108  idxend = muonHitIndices.end();
109  idxs != idxend;
110  ++idxs) {
111  muonHitRefs.push_back(std::make_pair(hitRefBegin + idxs->first, hitRefBegin + idxs->second));
112  }
113 
114  } break;
115  case reco::GlbOrTrk: {
116  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
117  << "\n"
118  << "There are " << muons.size() << " selected reco::Muons.";
119 
120  int isel = 0;
121  for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
122  edm::RefToBase<reco::Muon> mur = *it;
123 
124  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
125  << " #" << isel << ", reco::Muon key = " << mur.key() << ", q*p = " << mur->charge() * mur->p()
126  << ", pT = " << mur->pt() << ", eta = " << mur->eta() << ", phi = " << mur->phi();
127 
128  // Global muon with valid muon hits
129  if (mur->isGlobalMuon() && mur->globalTrack()->hitPattern().numberOfValidMuonHits() > 0) {
130  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
131  << "\t this is a Global Muon with valid muon hits";
132  muonHitRefs.push_back(std::make_pair(mur->globalTrack()->recHitsBegin(), mur->globalTrack()->recHitsEnd()));
133  }
134 
135  // Tracker Muon
136  else if (mur->isTrackerMuon()) {
137  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t this is a Tracker Muon";
139 
140  std::vector<const TrackingRecHit *> hits = m_hitExtractor->getMuonHits(*mur);
141  for (std::vector<const TrackingRecHit *>::const_iterator ith = hits.begin(), edh = hits.end(); ith != edh;
142  ++ith) {
143  TMvec.push_back(**ith);
144  }
145 
146  TMRecHits.push_back(TMvec);
147 
148  muonHitRefs.push_back(std::make_pair(TMRecHits.rbegin()->data().begin(), TMRecHits.rbegin()->data().end()));
149  }
150 
151  // Standalone muon or Global without valid muon hits
152  else if (mur->outerTrack().isNonnull()) {
153  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t this is a Standalone muon";
154  muonHitRefs.push_back(std::make_pair(mur->outerTrack()->recHitsBegin(), mur->outerTrack()->recHitsEnd()));
155  }
156 
157  else {
158  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t what muon is this ?";
159  edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
160  << "isMuon : " << mur->isMuon() << ", isPFMuon : " << mur->isPFMuon()
161  << ", isTrackerMuon : " << mur->isTrackerMuon() << ", isStandAloneMuon : " << mur->isStandAloneMuon()
162  << ", isGlobalMuon : " << mur->isGlobalMuon() << ", isRPCMuon : " << mur->isRPCMuon()
163  << ", isGEMMuon : " << mur->isGEMMuon() << ", isME0Muon : " << mur->isME0Muon();
164  muonHitRefs.push_back(std::make_pair(noTM.data().end(), noTM.data().end()));
165  }
166 
167  isel++;
168  } // loop on muons
169 
170  } break;
171  }
172 
174  auto recSimColl = m_helper->associateRecoToSimIndices(muonHitRefs, tPC, m_resources);
175  for (auto it = recSimColl.begin(), ed = recSimColl.end(); it != ed; ++it) {
176  edm::RefToBase<reco::Muon> rec = muons[it->first];
177  std::vector<std::pair<TrackingParticleRef, double>> &tpAss = recToSim[rec];
178  for (auto const &a : it->second) {
179  tpAss.push_back(std::make_pair(tPC[a.idx], a.quality));
180  }
181  }
182  auto simRecColl = m_helper->associateSimToRecoIndices(muonHitRefs, tPC, m_resources);
183  for (auto it = simRecColl.begin(), ed = simRecColl.end(); it != ed; ++it) {
184  TrackingParticleRef sim = tPC[it->first];
185  std::vector<std::pair<edm::RefToBase<reco::Muon>, double>> &recAss = simToRec[sim];
186  for (auto const &a : it->second) {
187  recAss.push_back(std::make_pair(muons[a.idx], a.quality));
188  }
189  }
190 }
191 
193  reco::MuonToSimCollection &recToSim,
194  reco::SimToMuonCollection &simToRec,
197  const edm::Handle<TrackingParticleCollection> &tPCH) const {
199  for (unsigned int j = 0; j < tPCH->size(); j++)
201 
202  edm::RefToBaseVector<reco::Muon> muonBaseRefVector;
203  for (size_t i = 0; i < tCH->size(); ++i)
204  muonBaseRefVector.push_back(tCH->refAt(i));
205 
206  associateMuons(recToSim, simToRec, muonBaseRefVector, type, tpc);
207 }
208 
209 //
210 // static member functions
211 //
type
Definition: HCALResponse.h:21
bool isMuon() const override
Definition: Muon.h:290
std::map< edm::RefToBase< reco::Muon >, std::vector< std::pair< TrackingParticleRef, double > >, RefToBaseSort > MuonToSimCollection
Definition: MuonTrackType.h:37
TrackRef track() const override
reference to a Track
Definition: Muon.h:49
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double eta() const final
momentum pseudorapidity
bool isStandAloneMuon() const override
Definition: Muon.h:293
ProductID id() const
Definition: HandleBase.cc:15
std::vector< const TrackingRecHit * > getMuonHits(const reco::Muon &mu) const
void associateMuons(reco::MuonToSimCollection &recoToSim, reco::SimToMuonCollection &simToReco, const edm::RefToBaseVector< reco::Muon > &muons, reco::MuonTrackType type, const edm::RefVector< TrackingParticleCollection > &tpColl) const override
size_type size() const
Definition: OwnVector.h:264
const_iterator end() const
double pt() const final
transverse momentum
int charge() const final
electric charge
Definition: LeafCandidate.h:91
MuonToTrackingParticleAssociatorByHitsImpl(TrackerMuonHitExtractor const &iHitExtractor, MuonAssociatorByHitsHelper::Resources const &iResources, MuonAssociatorByHitsHelper const *iHelper)
IndexAssociation associateSimToRecoIndices(const TrackHitsCollection &, const edm::RefVector< TrackingParticleCollection > &, Resources const &) const
bool isME0Muon() const
Definition: Muon.h:298
bool isTrackerMuon() const override
Definition: Muon.h:292
std::map< TrackingParticleRef, std::vector< std::pair< edm::RefToBase< reco::Muon >, double > > > SimToMuonCollection
Definition: MuonTrackType.h:38
bool isRPCMuon() const
Definition: Muon.h:296
void push_back(D *&d)
Definition: OwnVector.h:290
std::vector< std::pair< trackingRecHit_iterator, trackingRecHit_iterator > > TrackHitsCollection
bool isGlobalMuon() const override
Definition: Muon.h:291
size_t key() const
Definition: RefToBase.h:250
size_type size() const
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:51
double p() const final
magnitude of momentum vector
bool isGEMMuon() const
Definition: Muon.h:297
const_iterator begin() const
IndexAssociation associateRecoToSimIndices(const TrackHitsCollection &, const edm::RefVector< TrackingParticleCollection > &, Resources const &) const
base const & data() const
Definition: OwnVector.h:440
double a
Definition: hdecay.h:121
void push_back(const RefToBase< T > &)
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:69
bool isPFMuon() const
Definition: Muon.h:295
MuonTrackType
Definition: MuonTrackType.h:28
double phi() const final
momentum azimuthal angle
virtual TrackRef globalTrack() const
reference to Track reconstructed in both tracked and muon detector
Definition: Muon.h:54