Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackClusEvaluator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackClusEvaluator.cc
1 #include "TrackClusEvaluator.h"
2 #include "TrkrClusterIsMatcher.h"
3 #include "ClusKeyIter.h"
4 #include "g4evalfn.h"
5 
10 #include <phool/phool.h> // for PHWHERE
11 
13  if (ismatcher == nullptr) return nullptr;
14  else return ismatcher->m_TruthClusters;
15 }
16 
18  if (ismatcher == nullptr) return nullptr;
19  else return ismatcher->m_RecoClusters;
20 }
21 
22  std::array<int,5> TrackClusEvaluator::cntclus(Vector& keys) {
23  std::array<int,5> cnt { 0, 0, 0, 0, 0 };
24  for (auto& it : keys) {
25  cnt[g4evalfn::trklayer_det(it.first)] += 1;
26  }
27  for (int i=0;i<4;++i) cnt[4] += cnt[i];
28  return cnt;
29  }
30 
32  svtx_keys.clear();
33  for (auto ckey : ClusKeyIter(track)) {
34  svtx_keys.push_back( {TrkrDefs::getHitSetKeyFromClusKey(ckey), ckey} );
35  }
36  std::sort(svtx_keys.begin(), svtx_keys.end());
37  return svtx_keys.size();
38  }
39 
40  std::array<int,5> TrackClusEvaluator::cnt_matchedclus
41  (Vector& keys, std::vector<bool>& matches)
42  {
43  std::array<int,5> cnt { 0, 0, 0, 0, 0 };
44  if (keys.size() != matches.size()) {
45  std::cout << PHWHERE << " matching and key vector not the same size. "
46  << std::endl << " run find_matches() first." << std::endl;
47  return cnt;
48  }
49  for (unsigned int i=0; i<keys.size(); ++i) {
50  if (matches[i]) {
51  cnt[g4evalfn::trklayer_det(keys[i].first)] += 1;
52  }
53  }
54  for (int i=0;i<4;++i) cnt[4] += cnt[i];
55  return cnt;
56  }
57 
59  phg4_keys.clear();
60  for (auto ckey : track->getClusters()) {
61  phg4_keys.push_back( {TrkrDefs::getHitSetKeyFromClusKey(ckey), ckey} );
62  }
63  std::sort(phg4_keys.begin(), phg4_keys.end());
64  return phg4_keys.size();
65  }
66 
68  phg4_keys.clear();
69  phg4_matches.clear();
70  svtx_keys.clear();
71  svtx_matches.clear();
72  }
73 
74  std::array<int,3> TrackClusEvaluator::find_matches() {
75  if (ismatcher == nullptr) {
76  std::cout << PHWHERE
77  << " Won't compare tracks because of missing TrkrClusterIsMatcher" << std::endl;
78  return {0,0,0};
79  }
80  // find the matches between the svtx_keys and phg4_keys
81  // also keep track of the sum of the comparison between then
82 
83  // ---------------------------------
84  // set aliases for notation cleaness
85  // use A for PHG4 and B for SVTX
86  auto& vA = phg4_keys;
87  auto& vB = svtx_keys;
88 
89  auto& matchesA = phg4_matches;
90  auto& matchesB = svtx_matches;
91 
92  match_stat = 0.; // DEPRECATED
93 
94  // matches will say, cluster by cluster, which clusters are matched
95  matchesA = std::vector<bool>(vA.size(),false);
96  matchesB = std::vector<bool>(vB.size(),false);
97 
98  // user iterators to access the vectors
99  auto iA0 = vA.begin();
100  auto iA1 = vA.end();
101 
102  auto iB0 = vB.begin();
103  auto iB1 = vB.end();
104 
105  auto iA = iA0;
106  auto iB = iB0;
107 
108  int n_match {0};
109 
110  while (iA != iA1 && iB != iB1) {
111  if (iA->first == iB->first) {
112  auto hitset = iA->first;
113 
114  // must compare ALL sets of iA and iB with this same hitset
115  auto sAend = iA+1; // search A end
116  while (sAend != iA1 && sAend->first == hitset) ++sAend;
117 
118  auto sBend = iB+1; // search B end
119  while (sBend != iB1 && sBend->first == hitset) ++sBend;
120 
121  for (auto _A = iA; _A!=sAend; ++_A) {
122  for (auto _B = iB; _B!=sBend; ++_B) {
123  auto is_match = ismatcher->operator()(_A->second,_B->second);
124  if (is_match) {
125  matchesA[_A-iA0] = true;
126  matchesB[_B-iB0] = true;
128  ++n_match;
129  }
130  }}
131  iA = sAend;
132  iB = sBend;
133  } else if (iA->first < iB->first) {
134  ++iA;
135  } else {
136  ++iB;
137  }
138  }
139  return { n_match, (int)phg4_keys.size(), (int)svtx_keys.size() };
140  }
141 
142  std::array<int,3> TrackClusEvaluator::find_matches(TrkrTruthTrack* g4_track, SvtxTrack* sv_track) {
143  addClusKeys(sv_track);
144  addClusKeys(g4_track);
145  return find_matches();
146  }
147 
149  return std::accumulate(phg4_matches.begin(), phg4_matches.end(), 0); }
150 
152  return std::accumulate(svtx_matches.begin(), svtx_matches.end(), 0); }
153 
154  std::vector<TrkrClusLoc> TrackClusEvaluator::phg4_clusloc_all() {
155  std::vector<TrkrClusLoc> vec{};
156  for (auto& cluspair : phg4_keys) vec.push_back(g4evalfn::clusloc_PHG4(ismatcher,cluspair.second));
157  return vec;
158  }
159 
160  std::vector<TrkrClusLoc> TrackClusEvaluator::phg4_clusloc_unmatched() {
161  std::vector<TrkrClusLoc> vec{};
162  auto cnt = phg4_keys.size();
163  for (unsigned int i = 0; i<cnt; ++i) {
164  if (!phg4_matches[i]) vec.push_back(g4evalfn::clusloc_PHG4(ismatcher,phg4_keys[i].second));
165  }
166  return vec;
167  }
168 
169  std::vector<TrkrClusLoc> TrackClusEvaluator::svtx_clusloc_all() {
170  std::vector<TrkrClusLoc> vec{};
171  for (auto& cluspair : svtx_keys) vec.push_back(g4evalfn::clusloc_SVTX(ismatcher,cluspair.second));
172  return vec;
173  }
174 
175  std::vector<TrkrClusLoc> TrackClusEvaluator::svtx_clusloc_unmatched() {
176  std::vector<TrkrClusLoc> vec{};
177  auto cnt = svtx_keys.size();
178  for (unsigned int i = 0; i<cnt; ++i) {
179  if (!svtx_matches[i]) vec.push_back(g4evalfn::clusloc_SVTX(ismatcher,svtx_keys[i].second));
180  }
181  return vec;
182  }
183 
184  std::vector<TrkrClusLoc> TrackClusEvaluator::clusloc_matched() {
185  std::vector<TrkrClusLoc> vec{};
186  auto cnt = phg4_keys.size();
187  for (unsigned int i = 0; i<cnt; ++i) {
188  if (phg4_matches[i]) vec.push_back(g4evalfn::clusloc_PHG4(ismatcher,phg4_keys[i].second));
189  }
190  return vec;
191  }