Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHGhostRejection.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHGhostRejection.cc
1 #include "PHGhostRejection.h"
2 
3 #include "PHGhostRejection.h"
4 
6 
7 #include <trackbase/TrkrCluster.h> // for TrkrCluster
8 #include <trackbase/TrkrDefs.h> // for cluskey, getLayer, TrkrId
11 
14 
15 #include <cmath> // for sqrt, fabs, atan2, cos
16 #include <iostream> // for operator<<, basic_ostream
17 #include <map> // for map
18 #include <set> // for _Rb_tree_const_iterator
19 #include <utility> // for pair, make_pair
20 
21 //____________________________________________________________________________..
23  : m_verbosity(verbosity)
24 {
25 }
26 
27 //____________________________________________________________________________..
29 {
30 }
31 
32 //____________________________________________________________________________..
33 void PHGhostRejection::rejectGhostTracks(std::vector<float> &trackChi2)
34 {
35 
36  if(!m_trackMap || m_positions.size() == 0)
37  {
38  std::cout << "Missing containers, will not run TPC seed ghost rejection"
39  << std::endl;
40  return;
41  }
42 
43  if(m_verbosity > 0)
44  std::cout << "PHGhostRejection beginning track map size " << m_trackMap->size() << std::endl;
45 
46  // Try to eliminate repeated tracks
47 
48  std::set<unsigned int> matches_set;
49  std::multimap<unsigned int, unsigned int> matches;
50 
51  for (unsigned int trid1 = 0;
52  trid1 != m_trackMap->size();
53  ++trid1)
54  {
55  TrackSeed* track1 = m_trackMap->get(trid1);
56  if(!track1)
57  { continue; }
58  float track1phi = track1->get_phi(m_positions);
59  float track1x = track1->get_x();
60  float track1y = track1->get_y();
61  float track1z = track1->get_z();
62  float track1eta = track1->get_eta();
63  for (unsigned int trid2 = trid1;
64  trid2 != m_trackMap->size();
65  ++trid2)
66  {
67  if(trid1 == trid2) continue;
68 
69  TrackSeed* track2 = m_trackMap->get(trid2);
70  if(!track2)
71  { continue; }
72  if(fabs( track1phi - track2->get_phi(m_positions)) < _phi_cut &&
73  fabs( track1eta - track2->get_eta() ) < _eta_cut &&
74  fabs( track1x - track2->get_x() ) < _x_cut &&
75  fabs( track1y - track2->get_y() ) < _y_cut &&
76  fabs( track1z - track2->get_z() ) < _z_cut
77  )
78  {
79  matches_set.insert(trid1);
80  matches.insert( std::pair( trid1, trid2) );
81 
82  if(m_verbosity > 1)
83  std::cout << "Found match for tracks " << trid1 << " and " << trid2 << std::endl;
84  }
85  }
86  }
87 
88  std::set<unsigned int> ghost_reject_list;
89 
90  for(auto set_it : matches_set)
91  {
92  if(ghost_reject_list.find(set_it) != ghost_reject_list.end()) continue; // already rejected
93 
94  auto match_list = matches.equal_range(set_it);
95 
96  auto tr1 = m_trackMap->get(set_it);
97  double best_qual = trackChi2.at(set_it);
98  unsigned int best_track = set_it;
99 
100  if(m_verbosity > 1)
101  std::cout << " ****** start checking track " << set_it << " with best quality " << best_qual << " best_track " << best_track << std::endl;
102 
103  for (auto it=match_list.first; it!=match_list.second; ++it)
104  {
105  if(m_verbosity > 1)
106  std::cout << " match of track " << it->first << " to track " << it->second << std::endl;
107 
108  auto tr2 = m_trackMap->get(it->second);
109 
110  // Check that these two tracks actually share the same clusters, if not skip this pair
111 
112  bool is_same_track = checkClusterSharing(tr1, set_it,
113  tr2, it->second);
114  if(!is_same_track) continue;
115 
116  // which one has the best quality?
117  double tr2_qual = trackChi2.at(it->second);
118  if(m_verbosity > 1)
119  {
120  std::cout << " Compare: best quality " << best_qual << " track 2 quality " << tr2_qual << std::endl;
121  std::cout << " tr1: phi " << tr1->get_phi(m_positions) << " eta " << tr1->get_eta()
122  << " x " << tr1->get_x() << " y " << tr1->get_y() << " z " << tr1->get_z() << std::endl;
123  std::cout << " tr2: phi " << tr2->get_phi(m_positions) << " eta " << tr2->get_eta()
124  << " x " << tr2->get_x() << " y " << tr2->get_y() << " z " << tr2->get_z() << std::endl;
125  }
126 
127  if(tr2_qual < best_qual)
128  {
129  if(m_verbosity > 1)
130  std::cout << " --------- Track " << it->second << " has better quality, erase track " << best_track << std::endl;
131  ghost_reject_list.insert(best_track);
132  best_qual = tr2_qual;
133  best_track = it->second;
134  }
135  else
136  {
137  if(m_verbosity > 1)
138  std::cout << " --------- Track " << best_track << " has better quality, erase track " << it->second << std::endl;
139  ghost_reject_list.insert(it->second);
140  }
141 
142  }
143  if(m_verbosity > 1)
144  std::cout << " best track " << best_track << " best_qual " << best_qual << std::endl;
145 
146  }
147 
148  // delete ghost tracks
149  for(auto it : ghost_reject_list)
150  {
151  if(m_verbosity > 1)
152  std::cout << " erasing track ID " << it << std::endl;
153 
154  m_trackMap->erase(it);
155  }
156 
157  if(m_verbosity > 0)
158  std::cout << "Track map size after deleting ghost tracks: " << m_trackMap->size() << std::endl;
159 
160 
161  return;
162 }
163 
164 bool PHGhostRejection::checkClusterSharing(TrackSeed* tr1, unsigned int trid1,
165  TrackSeed* tr2, unsigned int trid2)
166 {
167  // Check that tr1 and tr2 share many clusters
168 
169  bool is_same_track = false;
170 
171  std::multimap<TrkrDefs::cluskey, unsigned int> cluskey_map;
172  std::vector<TrkrDefs::cluskey> clusterkeys;
173 
174  for (TrackSeed::ConstClusterKeyIter key_iter = tr1->begin_cluster_keys();
175  key_iter != tr1->end_cluster_keys();
176  ++key_iter)
177  {
178  TrkrDefs::cluskey cluster_key = *key_iter;
179  if(m_verbosity > 2) std::cout << " track id: " << trid1 << " adding clusterkey " << cluster_key << std::endl;
180  cluskey_map.insert( std::make_pair(cluster_key, trid1) );
181  clusterkeys.push_back(cluster_key);
182  }
183 
184  for (TrackSeed::ConstClusterKeyIter key_iter = tr2->begin_cluster_keys();
185  key_iter != tr2->end_cluster_keys();
186  ++key_iter)
187  {
188  TrkrDefs::cluskey cluster_key = *key_iter;
189  if(m_verbosity > 2) std::cout << " track id: " << trid2 << " adding clusterkey " << cluster_key << std::endl;
190  cluskey_map.insert( std::make_pair(cluster_key, trid2) );
191  }
192 
193  unsigned int nclus = clusterkeys.size();
194 
195  unsigned int nclus_used = 0;
196  for (TrkrDefs::cluskey& cluskey : clusterkeys)
197  {
198  if(cluskey_map.count(cluskey)>0) nclus_used++;
199  }
200 
201  if( (float) nclus_used / (float) nclus > 0.5)
202  is_same_track = true;
203 
204  if(m_verbosity > 1)
205  std::cout << " tr1 " << trid1 << " tr2 " << trid2 << " nclus_used " << nclus_used << " nclus " << nclus << std::endl;
206  if(m_verbosity > 0)
207  if(!is_same_track) std::cout << " ***** not the same track! ********" << " tr1 " << trid1 << " tr2 "
208  << trid2 << " nclus_used " << nclus_used << " nclus " << nclus << std::endl;
209 
210  return is_same_track;
211 }