ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SvtxVertexEval.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SvtxVertexEval.cc
1 #include "SvtxVertexEval.h"
2 
3 #include "SvtxTrackEval.h"
4 #include "SvtxTruthEval.h"
5 
9 
10 #include <g4main/PHG4Particle.h>
12 #include <g4main/PHG4VtxPoint.h>
13 
14 #include <phool/getClass.h>
15 
16 #include <cassert>
17 #include <iostream>
18 #include <set>
19 
20 class SvtxTrack;
21 
22 using namespace std;
23 
25  : _trackeval(topNode)
26  , _vertexmap(nullptr)
27  , _trackmap(nullptr)
28  , _truthinfo(nullptr)
29  , _strict(false)
30  , _verbosity(0)
31  , _errors(0)
32  , _do_cache(true)
33 {
34  set_track_nodename("SvtxTrackMap");
35 }
36 
38 {
39  if (_verbosity > 0)
40  {
41  if ((_errors > 0) || (_verbosity > 1))
42  {
43  cout << "SvtxVertexEval::~SvtxVertexEval() - Error Count: " << _errors << endl;
44  }
45  }
46 }
47 
49 {
56 
57  _trackeval.next_event(topNode);
58 
59  get_node_pointers(topNode);
60 }
61 
62 std::set<PHG4Particle*> SvtxVertexEval::all_truth_particles(SvtxVertex* vertex)
63 {
64  if (!has_node_pointers())
65  {
66  ++_errors;
67  return std::set<PHG4Particle*>();
68  }
69 
70  if (_strict)
71  {
72  assert(vertex);
73  }
74  else if (!vertex)
75  {
76  ++_errors;
77  return std::set<PHG4Particle*>();
78  }
79 
80  if (_do_cache)
81  {
82  std::map<SvtxVertex*, std::set<PHG4Particle*> >::iterator iter =
83  _cache_all_truth_particles.find(vertex);
84  if (iter != _cache_all_truth_particles.end())
85  {
86  return iter->second;
87  }
88  }
89 
90  std::set<PHG4Particle*> all_particles;
91 
92  // loop over all tracks on vertex
93  for (SvtxVertex::TrackIter iter = vertex->begin_tracks();
94  iter != vertex->end_tracks();
95  ++iter)
96  {
97  SvtxTrack* track = _trackmap->get(*iter);
98 
99  if (_strict)
100  {
101  assert(track);
102  }
103  else if (!track)
104  {
105  ++_errors;
106  continue;
107  }
108 
110 
111  if (_strict)
112  {
113  assert(max_particle);
114  }
115  else if (!max_particle)
116  {
117  ++_errors;
118  continue;
119  }
120 
121  all_particles.insert(max_particle);
122  }
123 
124  if (_do_cache) _cache_all_truth_particles.insert(make_pair(vertex, all_particles));
125 
126  return all_particles;
127 }
128 
129 std::set<PHG4VtxPoint*> SvtxVertexEval::all_truth_points(SvtxVertex* vertex)
130 {
131  if (!has_node_pointers())
132  {
133  ++_errors;
134  return std::set<PHG4VtxPoint*>();
135  }
136 
137  if (_strict)
138  {
139  assert(vertex);
140  }
141  else if (!vertex)
142  {
143  ++_errors;
144  return std::set<PHG4VtxPoint*>();
145  }
146 
147  if (_do_cache)
148  {
149  std::map<SvtxVertex*, std::set<PHG4VtxPoint*> >::iterator iter =
150  _cache_all_truth_points.find(vertex);
151  if (iter != _cache_all_truth_points.end())
152  {
153  return iter->second;
154  }
155  }
156 
157  std::set<PHG4VtxPoint*> points;
158 
159  std::set<PHG4Particle*> particles = all_truth_particles(vertex);
160  for (std::set<PHG4Particle*>::iterator iter = particles.begin();
161  iter != particles.end();
162  ++iter)
163  {
164  PHG4Particle* particle = *iter;
166 
167  if (_strict)
168  {
169  assert(point);
170  }
171  else if (!point)
172  {
173  ++_errors;
174  continue;
175  }
176 
177  points.insert(point);
178  }
179 
180  if (_do_cache) _cache_all_truth_points.insert(make_pair(vertex, points));
181 
182  return points;
183 }
184 
186 {
187  if (!has_node_pointers())
188  {
189  ++_errors;
190  return nullptr;
191  }
192 
193  if (_strict)
194  {
195  assert(vertex);
196  }
197  else if (!vertex)
198  {
199  ++_errors;
200  return nullptr;
201  }
202 
203  if (_do_cache)
204  {
205  std::map<SvtxVertex*, PHG4VtxPoint*>::iterator iter =
207  if (iter != _cache_max_truth_point_by_ntracks.end())
208  {
209  return iter->second;
210  }
211  }
212 
213  std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
214 
215  PHG4VtxPoint* max_point = nullptr;
216  unsigned int max_ntracks = 0;
217 
218  for (std::set<PHG4VtxPoint*>::iterator iter = points.begin();
219  iter != points.end();
220  ++iter)
221  {
222  PHG4VtxPoint* candidate = *iter;
223  unsigned int ntracks = get_ntracks_contribution(vertex, candidate);
224  if (ntracks > max_ntracks)
225  {
226  max_ntracks = ntracks;
227  max_point = candidate;
228  }
229  }
230 
231  if (_do_cache) _cache_max_truth_point_by_ntracks.insert(make_pair(vertex, max_point));
232 
233  return max_point;
234 }
235 
236 std::set<SvtxVertex*> SvtxVertexEval::all_vertexes_from(PHG4VtxPoint* truthpoint)
237 {
238  if (!has_node_pointers())
239  {
240  ++_errors;
241  return std::set<SvtxVertex*>();
242  }
243 
244  if (_strict)
245  {
246  assert(truthpoint);
247  }
248  else if (!truthpoint)
249  {
250  ++_errors;
251  return std::set<SvtxVertex*>();
252  }
253 
254  if (_do_cache)
255  {
256  std::map<PHG4VtxPoint*, std::set<SvtxVertex*> >::iterator iter =
257  _cache_all_vertexes_from_point.find(truthpoint);
258  if (iter != _cache_all_vertexes_from_point.end())
259  {
260  return iter->second;
261  }
262  }
263 
264  std::set<SvtxVertex*> all_vertexes;
265 
266  // loop over all vertexes on node
267 
268  for (SvtxVertexMap::Iter iter = _vertexmap->begin();
269  iter != _vertexmap->end();
270  ++iter)
271  {
272  SvtxVertex* vertex = iter->second;
273  std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
274  for (std::set<PHG4VtxPoint*>::iterator jter = points.begin();
275  jter != points.end();
276  ++jter)
277  {
278  PHG4VtxPoint* point = *jter;
279  if (get_truth_eval()->are_same_vertex(point, truthpoint))
280  {
281  all_vertexes.insert(vertex);
282  }
283  }
284  }
285 
286  if (_do_cache) _cache_all_vertexes_from_point.insert(make_pair(truthpoint, all_vertexes));
287 
288  return all_vertexes;
289 }
290 
292 {
293  if (!has_node_pointers())
294  {
295  ++_errors;
296  return nullptr;
297  }
298 
299  if (_strict)
300  {
301  assert(truthpoint);
302  }
303  else if (!truthpoint)
304  {
305  ++_errors;
306  return nullptr;
307  }
308 
309  if (_do_cache)
310  {
311  std::map<PHG4VtxPoint*, SvtxVertex*>::iterator iter =
312  _cache_best_vertex_from_point.find(truthpoint);
313  if (iter != _cache_best_vertex_from_point.end())
314  {
315  return iter->second;
316  }
317  }
318 
319  SvtxVertex* best_vertex = nullptr;
320  unsigned int best_count = 0;
321  std::set<SvtxVertex*> tracks = all_vertexes_from(truthpoint);
322  for (std::set<SvtxVertex*>::iterator iter = tracks.begin();
323  iter != tracks.end();
324  ++iter)
325  {
326  SvtxVertex* vertex = *iter;
327  unsigned int count = get_ntracks_contribution(vertex, truthpoint);
328  if (count > best_count)
329  {
330  best_vertex = vertex;
331  best_count = count;
332  }
333  }
334 
335  if (_do_cache) _cache_best_vertex_from_point.insert(make_pair(truthpoint, best_vertex));
336 
337  return best_vertex;
338 }
339 
340 // overlap calculations
342 {
343  if (!has_node_pointers())
344  {
345  ++_errors;
346  return 0;
347  }
348 
349  if (_strict)
350  {
351  assert(vertex);
352  assert(truthpoint);
353  }
354  else if (!vertex || !truthpoint)
355  {
356  ++_errors;
357  return 0;
358  }
359 
360  if (_do_cache)
361  {
362  std::map<std::pair<SvtxVertex*, PHG4VtxPoint*>, unsigned int>::iterator iter =
363  _cache_get_ntracks_contribution.find(make_pair(vertex, truthpoint));
364  if (iter != _cache_get_ntracks_contribution.end())
365  {
366  return iter->second;
367  }
368  }
369 
370  unsigned int ntracks = 0;
371 
372  for (SvtxVertex::TrackIter iter = vertex->begin_tracks();
373  iter != vertex->end_tracks();
374  ++iter)
375  {
376  SvtxTrack* track = _trackmap->get(*iter);
378 
379  PHG4VtxPoint* candidate = get_truth_eval()->get_vertex(particle);
380 
381  if (_strict)
382  {
383  assert(candidate);
384  }
385  else if (!candidate)
386  {
387  ++_errors;
388  continue;
389  }
390 
391  if (get_truth_eval()->are_same_vertex(candidate, truthpoint))
392  {
393  ++ntracks;
394  }
395  }
396 
397  if (_do_cache) _cache_get_ntracks_contribution.insert(make_pair(make_pair(vertex, truthpoint), ntracks));
398 
399  return ntracks;
400 }
401 
403 {
404  // need things off the DST...
405 
407  {
408  _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap"); // always there, initial vertices
409  }
410  else if (_use_genfit_vertex)
411  {
412  _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapRefit"); // Rave vertices
413  }
414  else
415  {
416  _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapActs"); // Acts vertices
417  }
418  if (!_vertexmap)
419  std::cout << PHWHERE << "Did not find_vertexmap on node tree" << endl;
420 
421  _trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_TrackNodeName);
422 
423  _truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
424 
425  return;
426 }
427 
429 {
430  if (_strict)
431  assert(_vertexmap);
432  else if (!_vertexmap)
433  {
434  std::cout << PHWHERE << " did not find _vertexmap " << std::endl;
435  return false;
436  }
437 
438  if (_strict)
439  assert(_trackmap);
440  else if (!_trackmap)
441  {
442  std::cout << PHWHERE << " did not find _trackmap " << std::endl;
443  return false;
444  }
445 
446  if (_strict)
447  assert(_truthinfo);
448  else if (!_truthinfo)
449  {
450  std::cout << PHWHERE << " did not find _truthinfo " << std::endl;
451  return false;
452  }
453 
454  return true;
455 }
456 
457 void SvtxVertexEval::set_track_nodename(const std::string& name)
458 {
461 }