Analysis Software
Documentation for sPHENIX simulation 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 
7 #include <globalvertex/Vertex.h>
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 
23  : _trackeval(topNode)
24 {
25  set_track_nodename("SvtxTrackMap");
26 }
27 
29 {
30  if (_verbosity > 0)
31  {
32  if ((_errors > 0) || (_verbosity > 1))
33  {
34  std::cout << "SvtxVertexEval::~SvtxVertexEval() - Error Count: " << _errors << std::endl;
35  }
36  }
37 }
38 
40 {
47 
48  _trackeval.next_event(topNode);
49 
50  get_node_pointers(topNode);
51 }
52 
53 std::set<PHG4Particle*> SvtxVertexEval::all_truth_particles(const Vertex* vertex)
54 {
55  if (!has_node_pointers())
56  {
57  ++_errors;
58  return std::set<PHG4Particle*>();
59  }
60 
61  if (_strict)
62  {
63  assert(vertex);
64  }
65  else if (!vertex)
66  {
67  ++_errors;
68  return std::set<PHG4Particle*>();
69  }
70 
71  if (_do_cache)
72  {
73  std::map<const Vertex*, std::set<PHG4Particle*> >::iterator iter =
74  _cache_all_truth_particles.find(vertex);
75  if (iter != _cache_all_truth_particles.end())
76  {
77  return iter->second;
78  }
79  }
80 
81  std::set<PHG4Particle*> all_particles;
82 
83  // loop over all tracks on vertex
84  for (Vertex::TrackIter iter = vertex->begin_tracks();
85  iter != vertex->end_tracks();
86  ++iter)
87  {
88  SvtxTrack* track = _trackmap->get(*iter);
89 
90  if (_strict)
91  {
92  assert(track);
93  }
94  else if (!track)
95  {
96  ++_errors;
97  continue;
98  }
99 
101 
102  if (_strict)
103  {
104  assert(max_particle);
105  }
106  else if (!max_particle)
107  {
108  ++_errors;
109  continue;
110  }
111 
112  all_particles.insert(max_particle);
113  }
114 
115  if (_do_cache)
116  {
117  _cache_all_truth_particles.insert(std::make_pair(vertex, all_particles));
118  }
119 
120  return all_particles;
121 }
122 
123 std::set<PHG4VtxPoint*> SvtxVertexEval::all_truth_points(const Vertex* vertex)
124 {
125  if (!has_node_pointers())
126  {
127  ++_errors;
128  return std::set<PHG4VtxPoint*>();
129  }
130 
131  if (_strict)
132  {
133  assert(vertex);
134  }
135  else if (!vertex)
136  {
137  ++_errors;
138  return std::set<PHG4VtxPoint*>();
139  }
140 
141  if (_do_cache)
142  {
143  std::map<const Vertex*, std::set<PHG4VtxPoint*> >::iterator iter =
144  _cache_all_truth_points.find(vertex);
145  if (iter != _cache_all_truth_points.end())
146  {
147  return iter->second;
148  }
149  }
150 
151  std::set<PHG4VtxPoint*> points;
152 
153  std::set<PHG4Particle*> particles = all_truth_particles(vertex);
154  for (auto particle : particles)
155  {
157 
158  if (_strict)
159  {
160  assert(point);
161  }
162  else if (!point)
163  {
164  ++_errors;
165  continue;
166  }
167 
168  points.insert(point);
169  }
170 
171  if (_do_cache)
172  {
173  _cache_all_truth_points.insert(std::make_pair(vertex, points));
174  }
175 
176  return points;
177 }
178 
180 {
181  if (!has_node_pointers())
182  {
183  ++_errors;
184  return nullptr;
185  }
186 
187  if (_strict)
188  {
189  assert(vertex);
190  }
191  else if (!vertex)
192  {
193  ++_errors;
194  return nullptr;
195  }
196 
197  if (_do_cache)
198  {
199  std::map<const Vertex*, PHG4VtxPoint*>::iterator iter =
201  if (iter != _cache_max_truth_point_by_ntracks.end())
202  {
203  return iter->second;
204  }
205  }
206 
207  std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
208 
209  PHG4VtxPoint* max_point = nullptr;
210  unsigned int max_ntracks = 0;
211 
212  for (auto candidate : points)
213  {
214  unsigned int ntracks = get_ntracks_contribution(vertex, candidate);
215  if (ntracks > max_ntracks)
216  {
217  max_ntracks = ntracks;
218  max_point = candidate;
219  }
220  }
221 
222  if (_do_cache)
223  {
224  _cache_max_truth_point_by_ntracks.insert(std::make_pair(vertex, max_point));
225  }
226 
227  return max_point;
228 }
229 
230 std::set<const Vertex*> SvtxVertexEval::all_vertexes_from(PHG4VtxPoint* truthpoint)
231 {
232  if (!has_node_pointers())
233  {
234  ++_errors;
235  return std::set<const Vertex*>();
236  }
237 
238  if (_strict)
239  {
240  assert(truthpoint);
241  }
242  else if (!truthpoint)
243  {
244  ++_errors;
245  return std::set<const Vertex*>();
246  }
247 
248  if (_do_cache)
249  {
250  std::map<PHG4VtxPoint*, std::set<const Vertex*> >::iterator iter =
251  _cache_all_vertexes_from_point.find(truthpoint);
252  if (iter != _cache_all_vertexes_from_point.end())
253  {
254  return iter->second;
255  }
256  }
257 
258  std::set<const Vertex*> all_vertexes;
259 
260  // loop over all vertexes on node
261 
262  for (auto& iter : *_vertexmap)
263  {
264  const Vertex* vertex = iter.second;
265  std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
266  for (auto point : points)
267  {
268  if (get_truth_eval()->are_same_vertex(point, truthpoint))
269  {
270  all_vertexes.insert(vertex);
271  }
272  }
273  }
274 
275  if (_do_cache)
276  {
277  _cache_all_vertexes_from_point.insert(std::make_pair(truthpoint, all_vertexes));
278  }
279 
280  return all_vertexes;
281 }
282 
284 {
285  if (!has_node_pointers())
286  {
287  ++_errors;
288  return nullptr;
289  }
290 
291  if (_strict)
292  {
293  assert(truthpoint);
294  }
295  else if (!truthpoint)
296  {
297  ++_errors;
298  return nullptr;
299  }
300 
301  if (_do_cache)
302  {
303  std::map<PHG4VtxPoint*, const Vertex*>::iterator iter =
304  _cache_best_vertex_from_point.find(truthpoint);
305  if (iter != _cache_best_vertex_from_point.end())
306  {
307  return iter->second;
308  }
309  }
310 
311 
312  unsigned int best_count = 0;
313  std::set<const Vertex*> tracks = all_vertexes_from(truthpoint);
314 
315  std::set<const Vertex*>::iterator best_vertex_iter = tracks.begin();
316  for (auto it = tracks.begin(); it != tracks.end(); ++it)
317  {
318  const Vertex* vertex = *it;
319  unsigned int count = get_ntracks_contribution(vertex, truthpoint);
320  if (count > best_count)
321  {
322  best_vertex_iter = it;
323  best_count = count;
324  }
325  }
326 
327  const Vertex* the_best = *best_vertex_iter;
328 
329  if (_do_cache)
330  {
331  _cache_best_vertex_from_point.insert(std::make_pair(truthpoint, the_best));
332  }
333 
334  return the_best;
335 }
336 
337 // overlap calculations
339 {
340  if (!has_node_pointers())
341  {
342  ++_errors;
343  return 0;
344  }
345 
346  if (_strict)
347  {
348  assert(vertex);
349  assert(truthpoint);
350  }
351  else if (!vertex || !truthpoint)
352  {
353  ++_errors;
354  return 0;
355  }
356 
357  if (_do_cache)
358  {
359  std::map<std::pair<const Vertex*, PHG4VtxPoint*>, unsigned int>::iterator iter =
360  _cache_get_ntracks_contribution.find(std::make_pair(vertex, truthpoint));
361  if (iter != _cache_get_ntracks_contribution.end())
362  {
363  return iter->second;
364  }
365  }
366 
367  unsigned int ntracks = 0;
368 
369  for (Vertex::TrackIter iter = vertex->begin_tracks();
370  iter != vertex->end_tracks();
371  ++iter)
372  {
373  SvtxTrack* track = _trackmap->get(*iter);
375 
376  PHG4VtxPoint* candidate = get_truth_eval()->get_vertex(particle);
377 
378  if (_strict)
379  {
380  assert(candidate);
381  }
382  else if (!candidate)
383  {
384  ++_errors;
385  continue;
386  }
387 
388  if (get_truth_eval()->are_same_vertex(candidate, truthpoint))
389  {
390  ++ntracks;
391  }
392  }
393 
394  if (_do_cache)
395  {
396  _cache_get_ntracks_contribution.insert(std::make_pair(std::make_pair(vertex, truthpoint), ntracks));
397  }
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  {
420  std::cout << PHWHERE << "Did not find_vertexmap on node tree" << std::endl;
421  }
422 
423  _trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_TrackNodeName);
424 
425  _truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
426 
427  return;
428 }
429 
431 {
432  if (_strict)
433  {
435  }
436  else if (!_vertexmap)
437  {
438  std::cout << PHWHERE << " did not find _vertexmap " << std::endl;
439  return false;
440  }
441 
442  if (_strict)
443  {
444  assert(_trackmap);
445  }
446  else if (!_trackmap)
447  {
448  std::cout << PHWHERE << " did not find _trackmap " << std::endl;
449  return false;
450  }
451 
452  if (_strict)
453  {
455  }
456  else if (!_truthinfo)
457  {
458  std::cout << PHWHERE << " did not find _truthinfo " << std::endl;
459  return false;
460  }
461 
462  return true;
463 }
464 
466 {
469 }