File indexing completed on 2025-08-05 08:18:01
0001 #include "SvtxVertexEval.h"
0002
0003 #include "SvtxTrackEval.h"
0004 #include "SvtxTruthEval.h"
0005
0006 #include <trackbase_historic/SvtxTrackMap.h>
0007 #include <globalvertex/Vertex.h>
0008 #include <globalvertex/SvtxVertexMap.h>
0009
0010 #include <g4main/PHG4Particle.h>
0011 #include <g4main/PHG4TruthInfoContainer.h>
0012 #include <g4main/PHG4VtxPoint.h>
0013
0014 #include <phool/getClass.h>
0015
0016 #include <cassert>
0017 #include <iostream>
0018 #include <set>
0019
0020 class SvtxTrack;
0021
0022 SvtxVertexEval::SvtxVertexEval(PHCompositeNode* topNode)
0023 : _trackeval(topNode)
0024 {
0025 set_track_nodename("SvtxTrackMap");
0026 }
0027
0028 SvtxVertexEval::~SvtxVertexEval()
0029 {
0030 if (_verbosity > 0)
0031 {
0032 if ((_errors > 0) || (_verbosity > 1))
0033 {
0034 std::cout << "SvtxVertexEval::~SvtxVertexEval() - Error Count: " << _errors << std::endl;
0035 }
0036 }
0037 }
0038
0039 void SvtxVertexEval::next_event(PHCompositeNode* topNode)
0040 {
0041 _cache_all_truth_particles.clear();
0042 _cache_all_truth_points.clear();
0043 _cache_max_truth_point_by_ntracks.clear();
0044 _cache_all_vertexes_from_point.clear();
0045 _cache_best_vertex_from_point.clear();
0046 _cache_get_ntracks_contribution.clear();
0047
0048 _trackeval.next_event(topNode);
0049
0050 get_node_pointers(topNode);
0051 }
0052
0053 std::set<PHG4Particle*> SvtxVertexEval::all_truth_particles(const Vertex* vertex)
0054 {
0055 if (!has_node_pointers())
0056 {
0057 ++_errors;
0058 return std::set<PHG4Particle*>();
0059 }
0060
0061 if (_strict)
0062 {
0063 assert(vertex);
0064 }
0065 else if (!vertex)
0066 {
0067 ++_errors;
0068 return std::set<PHG4Particle*>();
0069 }
0070
0071 if (_do_cache)
0072 {
0073 std::map<const Vertex*, std::set<PHG4Particle*> >::iterator iter =
0074 _cache_all_truth_particles.find(vertex);
0075 if (iter != _cache_all_truth_particles.end())
0076 {
0077 return iter->second;
0078 }
0079 }
0080
0081 std::set<PHG4Particle*> all_particles;
0082
0083
0084 for (Vertex::TrackIter iter = vertex->begin_tracks();
0085 iter != vertex->end_tracks();
0086 ++iter)
0087 {
0088 SvtxTrack* track = _trackmap->get(*iter);
0089
0090 if (_strict)
0091 {
0092 assert(track);
0093 }
0094 else if (!track)
0095 {
0096 ++_errors;
0097 continue;
0098 }
0099
0100 PHG4Particle* max_particle = _trackeval.max_truth_particle_by_nclusters(track);
0101
0102 if (_strict)
0103 {
0104 assert(max_particle);
0105 }
0106 else if (!max_particle)
0107 {
0108 ++_errors;
0109 continue;
0110 }
0111
0112 all_particles.insert(max_particle);
0113 }
0114
0115 if (_do_cache)
0116 {
0117 _cache_all_truth_particles.insert(std::make_pair(vertex, all_particles));
0118 }
0119
0120 return all_particles;
0121 }
0122
0123 std::set<PHG4VtxPoint*> SvtxVertexEval::all_truth_points(const Vertex* vertex)
0124 {
0125 if (!has_node_pointers())
0126 {
0127 ++_errors;
0128 return std::set<PHG4VtxPoint*>();
0129 }
0130
0131 if (_strict)
0132 {
0133 assert(vertex);
0134 }
0135 else if (!vertex)
0136 {
0137 ++_errors;
0138 return std::set<PHG4VtxPoint*>();
0139 }
0140
0141 if (_do_cache)
0142 {
0143 std::map<const Vertex*, std::set<PHG4VtxPoint*> >::iterator iter =
0144 _cache_all_truth_points.find(vertex);
0145 if (iter != _cache_all_truth_points.end())
0146 {
0147 return iter->second;
0148 }
0149 }
0150
0151 std::set<PHG4VtxPoint*> points;
0152
0153 std::set<PHG4Particle*> particles = all_truth_particles(vertex);
0154 for (auto particle : particles)
0155 {
0156 PHG4VtxPoint* point = get_truth_eval()->get_vertex(particle);
0157
0158 if (_strict)
0159 {
0160 assert(point);
0161 }
0162 else if (!point)
0163 {
0164 ++_errors;
0165 continue;
0166 }
0167
0168 points.insert(point);
0169 }
0170
0171 if (_do_cache)
0172 {
0173 _cache_all_truth_points.insert(std::make_pair(vertex, points));
0174 }
0175
0176 return points;
0177 }
0178
0179 PHG4VtxPoint* SvtxVertexEval::max_truth_point_by_ntracks(const Vertex* vertex)
0180 {
0181 if (!has_node_pointers())
0182 {
0183 ++_errors;
0184 return nullptr;
0185 }
0186
0187 if (_strict)
0188 {
0189 assert(vertex);
0190 }
0191 else if (!vertex)
0192 {
0193 ++_errors;
0194 return nullptr;
0195 }
0196
0197 if (_do_cache)
0198 {
0199 std::map<const Vertex*, PHG4VtxPoint*>::iterator iter =
0200 _cache_max_truth_point_by_ntracks.find(vertex);
0201 if (iter != _cache_max_truth_point_by_ntracks.end())
0202 {
0203 return iter->second;
0204 }
0205 }
0206
0207 std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
0208
0209 PHG4VtxPoint* max_point = nullptr;
0210 unsigned int max_ntracks = 0;
0211
0212 for (auto candidate : points)
0213 {
0214 unsigned int ntracks = get_ntracks_contribution(vertex, candidate);
0215 if (ntracks > max_ntracks)
0216 {
0217 max_ntracks = ntracks;
0218 max_point = candidate;
0219 }
0220 }
0221
0222 if (_do_cache)
0223 {
0224 _cache_max_truth_point_by_ntracks.insert(std::make_pair(vertex, max_point));
0225 }
0226
0227 return max_point;
0228 }
0229
0230 std::set<const Vertex*> SvtxVertexEval::all_vertexes_from(PHG4VtxPoint* truthpoint)
0231 {
0232 if (!has_node_pointers())
0233 {
0234 ++_errors;
0235 return std::set<const Vertex*>();
0236 }
0237
0238 if (_strict)
0239 {
0240 assert(truthpoint);
0241 }
0242 else if (!truthpoint)
0243 {
0244 ++_errors;
0245 return std::set<const Vertex*>();
0246 }
0247
0248 if (_do_cache)
0249 {
0250 std::map<PHG4VtxPoint*, std::set<const Vertex*> >::iterator iter =
0251 _cache_all_vertexes_from_point.find(truthpoint);
0252 if (iter != _cache_all_vertexes_from_point.end())
0253 {
0254 return iter->second;
0255 }
0256 }
0257
0258 std::set<const Vertex*> all_vertexes;
0259
0260
0261
0262 for (auto& iter : *_vertexmap)
0263 {
0264 const Vertex* vertex = iter.second;
0265 std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
0266 for (auto point : points)
0267 {
0268 if (get_truth_eval()->are_same_vertex(point, truthpoint))
0269 {
0270 all_vertexes.insert(vertex);
0271 }
0272 }
0273 }
0274
0275 if (_do_cache)
0276 {
0277 _cache_all_vertexes_from_point.insert(std::make_pair(truthpoint, all_vertexes));
0278 }
0279
0280 return all_vertexes;
0281 }
0282
0283 const Vertex* SvtxVertexEval::best_vertex_from(PHG4VtxPoint* truthpoint)
0284 {
0285 if (!has_node_pointers())
0286 {
0287 ++_errors;
0288 return nullptr;
0289 }
0290
0291 if (_strict)
0292 {
0293 assert(truthpoint);
0294 }
0295 else if (!truthpoint)
0296 {
0297 ++_errors;
0298 return nullptr;
0299 }
0300
0301 if (_do_cache)
0302 {
0303 std::map<PHG4VtxPoint*, const Vertex*>::iterator iter =
0304 _cache_best_vertex_from_point.find(truthpoint);
0305 if (iter != _cache_best_vertex_from_point.end())
0306 {
0307 return iter->second;
0308 }
0309 }
0310
0311
0312 unsigned int best_count = 0;
0313 std::set<const Vertex*> tracks = all_vertexes_from(truthpoint);
0314
0315 std::set<const Vertex*>::iterator best_vertex_iter = tracks.begin();
0316 for (auto it = tracks.begin(); it != tracks.end(); ++it)
0317 {
0318 const Vertex* vertex = *it;
0319 unsigned int count = get_ntracks_contribution(vertex, truthpoint);
0320 if (count > best_count)
0321 {
0322 best_vertex_iter = it;
0323 best_count = count;
0324 }
0325 }
0326
0327 const Vertex* the_best = *best_vertex_iter;
0328
0329 if (_do_cache)
0330 {
0331 _cache_best_vertex_from_point.insert(std::make_pair(truthpoint, the_best));
0332 }
0333
0334 return the_best;
0335 }
0336
0337
0338 unsigned int SvtxVertexEval::get_ntracks_contribution(const Vertex* vertex, PHG4VtxPoint* truthpoint)
0339 {
0340 if (!has_node_pointers())
0341 {
0342 ++_errors;
0343 return 0;
0344 }
0345
0346 if (_strict)
0347 {
0348 assert(vertex);
0349 assert(truthpoint);
0350 }
0351 else if (!vertex || !truthpoint)
0352 {
0353 ++_errors;
0354 return 0;
0355 }
0356
0357 if (_do_cache)
0358 {
0359 std::map<std::pair<const Vertex*, PHG4VtxPoint*>, unsigned int>::iterator iter =
0360 _cache_get_ntracks_contribution.find(std::make_pair(vertex, truthpoint));
0361 if (iter != _cache_get_ntracks_contribution.end())
0362 {
0363 return iter->second;
0364 }
0365 }
0366
0367 unsigned int ntracks = 0;
0368
0369 for (Vertex::TrackIter iter = vertex->begin_tracks();
0370 iter != vertex->end_tracks();
0371 ++iter)
0372 {
0373 SvtxTrack* track = _trackmap->get(*iter);
0374 PHG4Particle* particle = _trackeval.max_truth_particle_by_nclusters(track);
0375
0376 PHG4VtxPoint* candidate = get_truth_eval()->get_vertex(particle);
0377
0378 if (_strict)
0379 {
0380 assert(candidate);
0381 }
0382 else if (!candidate)
0383 {
0384 ++_errors;
0385 continue;
0386 }
0387
0388 if (get_truth_eval()->are_same_vertex(candidate, truthpoint))
0389 {
0390 ++ntracks;
0391 }
0392 }
0393
0394 if (_do_cache)
0395 {
0396 _cache_get_ntracks_contribution.insert(std::make_pair(std::make_pair(vertex, truthpoint), ntracks));
0397 }
0398
0399 return ntracks;
0400 }
0401
0402 void SvtxVertexEval::get_node_pointers(PHCompositeNode* topNode)
0403 {
0404
0405
0406 if (_use_initial_vertex)
0407 {
0408 _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap");
0409 }
0410 else if (_use_genfit_vertex)
0411 {
0412 _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapRefit");
0413 }
0414 else
0415 {
0416 _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapActs");
0417 }
0418 if (!_vertexmap)
0419 {
0420 std::cout << PHWHERE << "Did not find_vertexmap on node tree" << std::endl;
0421 }
0422
0423 _trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_TrackNodeName);
0424
0425 _truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0426
0427 return;
0428 }
0429
0430 bool SvtxVertexEval::has_node_pointers()
0431 {
0432 if (_strict)
0433 {
0434 assert(_vertexmap);
0435 }
0436 else if (!_vertexmap)
0437 {
0438 std::cout << PHWHERE << " did not find _vertexmap " << std::endl;
0439 return false;
0440 }
0441
0442 if (_strict)
0443 {
0444 assert(_trackmap);
0445 }
0446 else if (!_trackmap)
0447 {
0448 std::cout << PHWHERE << " did not find _trackmap " << std::endl;
0449 return false;
0450 }
0451
0452 if (_strict)
0453 {
0454 assert(_truthinfo);
0455 }
0456 else if (!_truthinfo)
0457 {
0458 std::cout << PHWHERE << " did not find _truthinfo " << std::endl;
0459 return false;
0460 }
0461
0462 return true;
0463 }
0464
0465 void SvtxVertexEval::set_track_nodename(const std::string& name)
0466 {
0467 m_TrackNodeName = name;
0468 _trackeval.set_track_nodename(name);
0469 }