CMS 3D CMS Logo

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