hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
TruthMatchingUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <map>
5#include <iostream>
6#include <cmath>
7
8#include "Track.h"
9#include "MCParticle.h"
10#include "TrackerHit.h"
11#include "TRefArray.h"
12
13namespace utils {
14
27inline int getTruthPDG(Track& trk, const std::vector<MCParticle*>* mcParticles, bool debug = false) {
28 if (!mcParticles || mcParticles->empty()) {
29 if (debug) {
30 std::cout << "utils::getTruthPDG: No MC particles available" << std::endl;
31 }
32 return 0;
33 }
34
35 // Try two methods to find the MC particle ID with the most hits on this track:
36 // Method 1: Use getMcpHits() from Track (populated by VertexProcessor)
37 // Method 2: Use getSvtHits() and getMCPartIDs() from TrackerHits
38
39 std::map<int, int> count_per_particle_id;
40
41 // Method 1: Try getMcpHits() first
42 auto mcp_hits = trk.getMcpHits();
43 if (debug) {
44 std::cout << "utils::getTruthPDG: Method 1 - track has " << mcp_hits.size() << " MCP hits" << std::endl;
45 }
46
47 for (const auto& [layer_id, particle_id] : mcp_hits) {
48 count_per_particle_id[particle_id]++;
49 }
50
51 // Method 2: If no MCP hits, try getting MC info from TrackerHits via getSvtHits()
52 if (count_per_particle_id.empty()) {
53 TRefArray svt_hits = trk.getSvtHits();
54 if (debug) {
55 std::cout << "utils::getTruthPDG: Method 2 - track has " << svt_hits.GetEntries() << " SVT hits" << std::endl;
56 }
57
58 for (int i = 0; i < svt_hits.GetEntries(); i++) {
59 TrackerHit* hit = static_cast<TrackerHit*>(svt_hits.At(i));
60 if (hit) {
61 std::vector<int> mc_part_ids = hit->getMCPartIDs();
62 for (int part_id : mc_part_ids) {
63 count_per_particle_id[part_id]++;
64 }
65 }
66 }
67 }
68
69 if (debug) {
70 std::cout << "utils::getTruthPDG: found " << count_per_particle_id.size() << " unique particle IDs" << std::endl;
71 }
72
73 // Find particle with most hits
74 int truth_id{-1}, max_nhits{0};
75 for (const auto& [particle_id, count] : count_per_particle_id) {
76 if (count > max_nhits) {
77 truth_id = particle_id;
78 max_nhits = count;
79 }
80 }
81
82 if (truth_id < 0) {
83 if (debug) {
84 std::cout << "utils::getTruthPDG: No hits found, returning 0" << std::endl;
85 }
86 return 0;
87 }
88
89 // Find the MC particle with this ID and return its PDG
90 for (MCParticle* ptr : *mcParticles) {
91 if (ptr->getID() == truth_id) {
92 if (debug) {
93 std::cout << "utils::getTruthPDG: Match found, PDG=" << ptr->getPDG() << std::endl;
94 }
95 return ptr->getPDG();
96 }
97 }
98
99 if (debug) {
100 std::cout << "utils::getTruthPDG: No MC particle found with ID " << truth_id << std::endl;
101 }
102 return 0;
103}
104
113inline bool hasTruthMatch(Track& trk, const std::vector<MCParticle*>* mcParticles, bool debug = false) {
114 int pdg = getTruthPDG(trk, mcParticles, debug);
115 return (std::abs(pdg) == 11);
116}
117
118} // namespace utils
Class used to encapsulate information about a mc particle.
Class used to encapsulate track information.
Class used to encapsulate tracker hit information.
Definition Track.h:32
TRefArray getSvtHits() const
Definition Track.h:64
std::vector< std::pair< int, int > > getMcpHits()
Definition Track.h:105
std::vector< int > getMCPartIDs() const
Definition TrackerHit.h:127
bool hasTruthMatch(Track &trk, const std::vector< MCParticle * > *mcParticles, bool debug=false)
Check if a track has a truth-matched electron/positron.
int getTruthPDG(Track &trk, const std::vector< MCParticle * > *mcParticles, bool debug=false)
Get the PDG code of the truth particle best matching this track.