CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MatchingSimTrack.cc
Go to the documentation of this file.
5 
6 #include <sstream>
7 
8 namespace spr {
9 
10  edm::SimTrackContainer::const_iterator matchedSimTrack(const edm::Event& iEvent,
13  const reco::Track* pTrack,
14  TrackerHitAssociator& associate,
15  bool debug) {
16  edm::SimTrackContainer::const_iterator itr = SimTk->end();
17  ;
18 
19  //Get the vector of PsimHits associated to TrackerRecHits and select the
20  //matching SimTrack on the basis of maximum occurance of trackIds
21  std::vector<unsigned int> trkId, trkOcc;
22  for (auto const& trkHit : pTrack->recHits()) {
23  std::vector<PSimHit> matchedSimIds = associate.associateHit(*trkHit);
24  for (unsigned int isim = 0; isim < matchedSimIds.size(); isim++) {
25  unsigned tkId = matchedSimIds[isim].trackId();
26  bool found = false;
27  for (unsigned int j = 0; j < trkId.size(); j++) {
28  if (tkId == trkId[j]) {
29  trkOcc[j]++;
30  found = true;
31  break;
32  }
33  }
34  if (!found) {
35  trkId.push_back(tkId);
36  trkOcc.push_back(1);
37  }
38  }
39  }
40 
41  if (debug) {
42  std::ostringstream st1;
43  for (unsigned int isim = 0; isim < trkId.size(); isim++) {
44  st1 << "\n trkId " << trkId[isim] << " Occurance " << trkOcc[isim] << ", ";
45  }
46  edm::LogVerbatim("IsoTrack") << st1.str();
47  }
48  int matchedId = 0;
49 
50  unsigned int matchSimTrk = 0;
51  if (!trkOcc.empty()) {
52  unsigned int maxTrkOcc = 0, idxMax = 0;
53  for (unsigned int j = 0; j < trkOcc.size(); j++) {
54  if (trkOcc[j] > maxTrkOcc) {
55  maxTrkOcc = trkOcc[j];
56  idxMax = j;
57  }
58  }
59  matchSimTrk = trkId[idxMax];
60  for (auto simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
61  if (simTrkItr->trackId() == matchSimTrk) {
62  matchedId = simTrkItr->type();
63  if (debug)
64  edm::LogVerbatim("IsoTrack") << "matched trackId (maximum occurance) " << matchSimTrk << " type "
65  << matchedId;
66  itr = simTrkItr;
67  break;
68  }
69  }
70  }
71 
72  if (matchedId == 0 && debug) {
73  edm::LogVerbatim("IsoTrack") << "Could not find matched SimTrk and track history now ";
74  }
75  return itr;
76  }
77 
78  //Returns a vector of TrackId originating from the matching SimTrack
79  std::vector<int> matchedSimTrackId(const edm::Event& iEvent,
82  const reco::Track* pTrack,
83  TrackerHitAssociator& associate,
84  bool debug) {
85  // get the matching SimTrack
86  edm::SimTrackContainer::const_iterator trkInfo =
87  spr::matchedSimTrack(iEvent, SimTk, SimVtx, pTrack, associate, debug);
88  unsigned int matchSimTrk = trkInfo->trackId();
89  if (debug)
90  edm::LogVerbatim("IsoTrack") << "matchedSimTrackId finds the SimTrk ID of the current track to be "
91  << matchSimTrk;
92  std::vector<int> matchTkid;
93  if (trkInfo->type() != 0) {
94  for (auto simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
95  if (validSimTrack(matchSimTrk, simTrkItr, SimTk, SimVtx, false))
96  matchTkid.push_back(static_cast<int>(simTrkItr->trackId()));
97  }
98  }
99  return matchTkid;
100  }
101 
102  spr::simTkInfo matchedSimTrackInfo(unsigned int simTkId,
105  bool debug) {
107  for (edm::SimTrackContainer::const_iterator simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
108  if (simTkId == simTrkItr->trackId()) {
109  if (spr::validSimTrack(simTkId, simTrkItr, SimTk, SimVtx, debug)) {
110  info.found = true;
111  info.pdgId = simTrkItr->type();
112  info.charge = simTrkItr->charge();
113  } else {
114  edm::SimTrackContainer::const_iterator parentItr = spr::parentSimTrack(simTrkItr, SimTk, SimVtx, debug);
115  if (debug) {
116  if (parentItr != SimTk->end())
117  edm::LogVerbatim("IsoTrack") << "original parent of " << simTrkItr->trackId() << " "
118  << parentItr->trackId() << ", " << parentItr->type();
119  else
120  edm::LogVerbatim("IsoTrack") << "original parent of " << simTrkItr->trackId() << " not found";
121  }
122  if (parentItr != SimTk->end()) {
123  info.found = true;
124  info.pdgId = parentItr->type();
125  info.charge = parentItr->charge();
126  }
127  }
128  break;
129  }
130  }
131  return info;
132  }
133 
134  // Checks if this SimTrack=thisTrkItr originates from the one with trackId=simTkId
135  bool validSimTrack(unsigned int simTkId,
136  edm::SimTrackContainer::const_iterator thisTrkItr,
139  bool debug) {
140  if (debug)
141  edm::LogVerbatim("IsoTrack") << "Inside validSimTrack: trackId " << thisTrkItr->trackId() << " vtxIndex "
142  << thisTrkItr->vertIndex() << " to be matched to " << simTkId;
143 
144  //This track originates from simTkId
145  if (thisTrkItr->trackId() == simTkId)
146  return true;
147 
148  //Otherwise trace back the history using SimTracks and SimVertices
149  int vertIndex = thisTrkItr->vertIndex();
150  if (vertIndex == -1 || vertIndex >= static_cast<int>(SimVtx->size()))
151  return false;
152 
153  edm::SimVertexContainer::const_iterator simVtxItr = SimVtx->begin();
154  for (int iv = 0; iv < vertIndex; iv++)
155  simVtxItr++;
156  int parent = simVtxItr->parentIndex();
157  if (debug)
158  edm::LogVerbatim("IsoTrack") << "validSimTrack:: parent index " << parent << " ";
159 
160  if (parent < 0 && simVtxItr != SimVtx->begin()) {
161  const math::XYZTLorentzVectorD pos1 = simVtxItr->position();
162  for (simVtxItr = SimVtx->begin(); simVtxItr != SimVtx->end(); ++simVtxItr) {
163  if (simVtxItr->parentIndex() > 0) {
164  const math::XYZTLorentzVectorD pos2 = pos1 - simVtxItr->position();
165  double dist = pos2.P();
166  if (dist < 0.001) {
167  parent = simVtxItr->parentIndex();
168  break;
169  }
170  }
171  }
172  }
173 
174  if (debug)
175  edm::LogVerbatim("IsoTrack") << "final index " << parent;
176  for (edm::SimTrackContainer::const_iterator simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
177  if (static_cast<int>(simTrkItr->trackId()) == parent && simTrkItr != thisTrkItr)
178  return validSimTrack(simTkId, simTrkItr, SimTk, SimVtx, debug);
179  }
180 
181  return false;
182  }
183 
184  //Returns the parent of a SimTrack
185  edm::SimTrackContainer::const_iterator parentSimTrack(edm::SimTrackContainer::const_iterator thisTrkItr,
188  bool debug) {
189  edm::SimTrackContainer::const_iterator itr = SimTk->end();
190 
191  int vertIndex = thisTrkItr->vertIndex();
192  if (debug)
193  edm::LogVerbatim("IsoTrack") << "SimTrackParent " << thisTrkItr->trackId() << " Vertex " << vertIndex << " Type "
194  << thisTrkItr->type() << " Charge " << static_cast<int>(thisTrkItr->charge());
195  if (vertIndex == -1)
196  return thisTrkItr;
197  else if (vertIndex >= static_cast<int>(SimVtx->size()))
198  return itr;
199 
200  edm::SimVertexContainer::const_iterator simVtxItr = SimVtx->begin();
201  for (int iv = 0; iv < vertIndex; iv++)
202  simVtxItr++;
203  int parent = simVtxItr->parentIndex();
204 
205  if (parent < 0 && simVtxItr != SimVtx->begin()) {
206  const math::XYZTLorentzVectorD pos1 = simVtxItr->position();
207  for (simVtxItr = SimVtx->begin(); simVtxItr != SimVtx->end(); ++simVtxItr) {
208  if (simVtxItr->parentIndex() > 0) {
209  const math::XYZTLorentzVectorD pos2 = pos1 - simVtxItr->position();
210  double dist = pos2.P();
211  if (dist < 0.001) {
212  parent = simVtxItr->parentIndex();
213  break;
214  }
215  }
216  }
217  }
218  for (edm::SimTrackContainer::const_iterator simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
219  if (static_cast<int>(simTrkItr->trackId()) == parent && simTrkItr != thisTrkItr)
220  return parentSimTrack(simTrkItr, SimTk, SimVtx, debug);
221  }
222 
223  return thisTrkItr;
224  }
225 
226 } // namespace spr
Log< level::Info, true > LogVerbatim
static const TGPicture * info(bool iBackgroundIsBlack)
int32_t *__restrict__ iv
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
int iEvent
Definition: GenABIO.cc:224
auto recHits() const
Access to reconstructed hits on the track.
Definition: Track.h:85
edm::SimTrackContainer::const_iterator matchedSimTrack(const edm::Event &iEvent, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, const reco::Track *pTrack, TrackerHitAssociator &associate, bool debug=false)
#define debug
Definition: HDRShower.cc:19
simTkInfo matchedSimTrackInfo(unsigned int simTkId, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, bool debug=false)
std::vector< int > matchedSimTrackId(const edm::Event &, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, const reco::Track *pTrack, TrackerHitAssociator &associate, bool debug=false)
std::vector< PSimHit > associateHit(const TrackingRecHit &thit) const
bool validSimTrack(unsigned int simTkId, edm::SimTrackContainer::const_iterator thisTrkItr, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, bool debug=false)
edm::SimTrackContainer::const_iterator parentSimTrack(edm::SimTrackContainer::const_iterator thisTrkItr, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, bool debug=false)