CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MatchingSimTrack.cc
Go to the documentation of this file.
3 
5 
6 #include <iostream>
7 
8 namespace spr{
9 
10  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){
11 
12  edm::SimTrackContainer::const_iterator itr = SimTk->end();;
13 
14  edm::SimTrackContainer::const_iterator simTrkItr;
15  edm::SimVertexContainer::const_iterator simVtxItr;
16 
17  //Get the vector of PsimHits associated to TrackerRecHits and select the
18  //matching SimTrack on the basis of maximum occurance of trackIds
19  std::vector<unsigned int> trkId, trkOcc;
20  int i=0;
21  for (trackingRecHit_iterator iTrkHit = pTrack->recHitsBegin(); iTrkHit != pTrack->recHitsEnd(); ++iTrkHit, ++i) {
22 
23  std::vector<PSimHit> matchedSimIds = associate.associateHit((**iTrkHit));
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) { trkId.push_back(tkId); trkOcc.push_back(1); }
35  }
36  }
37 
38  if (debug) {
39  std::cout << "Reconstructed Track with " << i << " recHits.";
40  for (unsigned int isim=0; isim<trkId.size(); isim++){
41  std::cout << "\n trkId " << trkId[isim] << " Occurance " << trkOcc[isim] << ", ";
42  }
43  std::cout << std::endl;
44  }
45 
46  int matchedId=0;
47  unsigned int matchSimTrk=0;
48  if (trkOcc.size() > 0) {
49  unsigned int maxTrkOcc=0, idxMax=0;
50  for(unsigned int j=0; j<trkOcc.size(); j++) {
51  if(trkOcc[j] > maxTrkOcc ) { maxTrkOcc = trkOcc[j]; idxMax = j; }
52  }
53  matchSimTrk = trkId[idxMax];
54  for (simTrkItr = SimTk->begin(); simTrkItr!= SimTk->end(); simTrkItr++) {
55  if ( simTrkItr->trackId() == matchSimTrk ) {
56  matchedId = simTrkItr->type();
57  if (debug) std::cout << "matched trackId (maximum occurance) " << matchSimTrk << " type " << matchedId << std::endl;
58  itr = simTrkItr;
59  break;
60  }
61  }
62  }
63 
64  if (matchedId==0 && debug) {
65  std::cout << "Could not find matched SimTrk and track history now " << std::endl;
66  }
67 
68  return itr;
69  }
70 
71  //Returns a vector of TrackId originating from the matching SimTrack
73 
74  // get the matching SimTrack
75  edm::SimTrackContainer::const_iterator trkInfo = spr::matchedSimTrack(iEvent, SimTk, SimVtx, pTrack,associate, debug);
76  unsigned int matchSimTrk = trkInfo->trackId();
77  if (debug) std::cout << "matchedSimTrackId finds the SimTrk ID of the current track to be " << matchSimTrk << std::endl;
78 
79  std::vector<int> matchTkid;
80  if( trkInfo->type() != 0) {
81  edm::SimTrackContainer::const_iterator simTrkItr;
82  for(simTrkItr = SimTk->begin(); simTrkItr!= SimTk->end(); simTrkItr++){
83  if (validSimTrack(matchSimTrk, simTrkItr, SimTk, SimVtx, false))
84  matchTkid.push_back((int)simTrkItr->trackId());
85  }
86  }
87  return matchTkid;
88  }
89 
92  for (edm::SimTrackContainer::const_iterator simTrkItr=SimTk->begin();
93  simTrkItr!= SimTk->end(); simTrkItr++) {
94  if (simTkId == simTrkItr->trackId()) {
95  if (spr::validSimTrack(simTkId, simTrkItr, SimTk, SimVtx, debug)) {
96  info.found = true;
97  info.pdgId = simTrkItr->type();
98  info.charge = simTrkItr->charge();
99  } else {
100  edm::SimTrackContainer::const_iterator parentItr = spr::parentSimTrack(simTrkItr, SimTk, SimVtx, debug);
101  if (debug) {
102  if (parentItr != SimTk->end() ) std::cout << "original parent of " << simTrkItr->trackId() << " " << parentItr->trackId() << ", " << parentItr->type() << std::endl;
103  else std::cout << "original parent of " << simTrkItr->trackId() << " not found" << std::endl;
104  }
105  if (parentItr != SimTk->end()) {
106  info.found = true;
107  info.pdgId = parentItr->type();
108  info.charge = parentItr->charge();
109  }
110  }
111  break;
112  }
113  }
114  return info;
115  }
116 
117  // Checks if this SimTrack=thisTrkItr originates from the one with trackId=simTkId
118  bool validSimTrack(unsigned int simTkId, edm::SimTrackContainer::const_iterator thisTrkItr, edm::Handle<edm::SimTrackContainer>& SimTk, edm::Handle<edm::SimVertexContainer>& SimVtx, bool debug){
119 
120  if (debug) std::cout << "Inside validSimTrack: trackId " << thisTrkItr->trackId() << " vtxIndex " << thisTrkItr->vertIndex() << " to be matched to " << simTkId << std::endl;
121 
122  //This track originates from simTkId
123  if (thisTrkItr->trackId() == simTkId) return true;
124 
125  //Otherwise trace back the history using SimTracks and SimVertices
126  int vertIndex = thisTrkItr->vertIndex();
127  if (vertIndex == -1 || vertIndex >= (int)SimVtx->size()) return false;
128 
129  edm::SimVertexContainer::const_iterator simVtxItr= SimVtx->begin();
130  for (int iv=0; iv<vertIndex; iv++) simVtxItr++;
131  int parent = simVtxItr->parentIndex();
132  if (debug) std::cout << "validSimTrack:: parent index " << parent <<" ";
133  if (parent < 0 && simVtxItr != SimVtx->begin()) {
134  const math::XYZTLorentzVectorD pos1 = simVtxItr->position();
135  for (simVtxItr=SimVtx->begin(); simVtxItr!=SimVtx->end(); ++simVtxItr) {
136  if (simVtxItr->parentIndex() > 0) {
137  const math::XYZTLorentzVectorD pos2 = pos1 - simVtxItr->position();
138  double dist = pos2.P();
139  if (dist < 0.001) {
140  parent = simVtxItr->parentIndex();
141  break;
142  }
143  }
144  }
145  }
146  if (debug) std::cout << "final index " << parent << std::endl;;
147  for(edm::SimTrackContainer::const_iterator simTrkItr= SimTk->begin(); simTrkItr!= SimTk->end(); simTrkItr++){
148  if ((int)simTrkItr->trackId() == parent && simTrkItr != thisTrkItr) return validSimTrack(simTkId, simTrkItr, SimTk, SimVtx, debug) ;
149  }
150 
151  return false;
152 
153  }
154 
155  //Returns the parent of a SimTrack
156  edm::SimTrackContainer::const_iterator parentSimTrack(edm::SimTrackContainer::const_iterator thisTrkItr, edm::Handle<edm::SimTrackContainer>& SimTk, edm::Handle<edm::SimVertexContainer>& SimVtx, bool debug){
157 
158  edm::SimTrackContainer::const_iterator itr = SimTk->end();
159 
160  int vertIndex = thisTrkItr->vertIndex();
161  int type = thisTrkItr->type(); int charge = (int)thisTrkItr->charge();
162  if (debug) std::cout << "SimTrackParent " << thisTrkItr->trackId() << " Vertex " << vertIndex << " Type " << type << " Charge " << charge << std::endl;
163 
164  if( vertIndex == -1 ) return thisTrkItr;
165  else if (vertIndex >= (int)SimVtx->size()) return itr;
166 
167  edm::SimVertexContainer::const_iterator simVtxItr= SimVtx->begin();
168  for (int iv=0; iv<vertIndex; iv++) simVtxItr++;
169  int parent = simVtxItr->parentIndex();
170 
171  if (parent < 0 && simVtxItr != SimVtx->begin()) {
172  const math::XYZTLorentzVectorD pos1 = simVtxItr->position();
173  for (simVtxItr=SimVtx->begin(); simVtxItr!=SimVtx->end(); ++simVtxItr) {
174  if (simVtxItr->parentIndex() > 0) {
175  const math::XYZTLorentzVectorD pos2 = pos1 - simVtxItr->position();
176  double dist = pos2.P();
177  if (dist < 0.001) {
178  parent = simVtxItr->parentIndex();
179  break;
180  }
181  }
182  }
183  }
184  for (edm::SimTrackContainer::const_iterator simTrkItr= SimTk->begin(); simTrkItr!= SimTk->end(); simTrkItr++){
185  if ((int)simTrkItr->trackId() == parent && simTrkItr != thisTrkItr) return parentSimTrack(simTrkItr, SimTk, SimVtx, debug);
186  }
187 
188  return thisTrkItr;
189  }
190 
191 
192 }
type
Definition: HCALResponse.h:21
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
list parent
Definition: dbtoconf.py:74
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
double charge(const std::vector< uint8_t > &Ampls)
int iEvent
Definition: GenABIO.cc:230
int j
Definition: DBlmapReader.cc:9
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)
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
#define debug
Definition: HDRShower.cc:19
simTkInfo matchedSimTrackInfo(unsigned int simTkId, edm::Handle< edm::SimTrackContainer > &SimTk, edm::Handle< edm::SimVertexContainer > &SimVtx, bool debug=false)
#define begin
Definition: vmac.h:30
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
tuple cout
Definition: gather_cfg.py:121
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)
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65