Skip to content

Commit

Permalink
refactor: Use track weight for vertex truth matching in Examples (#3024)
Browse files Browse the repository at this point in the history
Since tracks at vertices have weights we should use that instead of occurrence to decide on the truth matching.

Apart from that I renamed the matching thresholds and branches to be more comprehensible.
  • Loading branch information
andiwand authored Mar 15, 2024
1 parent 4cb1e5b commit 49e815c
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 38 deletions.
Binary file modified CI/physmon/reference/performance_amvf_gridseeder_seeded_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_gridseeder_ttbar_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_orthogonal_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_seeded_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_truth_estimated_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_truth_smeared_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_amvf_ttbar_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_ivf_orthogonal_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_ivf_seeded_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_ivf_truth_estimated_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/performance_ivf_truth_smeared_hist.root
Binary file not shown.
6 changes: 3 additions & 3 deletions CI/physmon/vertexing_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ histograms:
nbins: 21
min: 0
max: 20
"trkVtxMatch":

"truthVertexMatchRatio":
nbins: 100
min: 0.3
min: 0.0
max: 1.01

"resX|resY|resZ|resSeedZ":
Expand Down
4 changes: 2 additions & 2 deletions CI/physmon/vertexing_ttbar_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ histograms:
nbins: 301
min: 0
max: 300
"trkVtxMatch":

"truthVertexMatchRatio":
nbins: 100
min: 0.0
max: 1.01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ VertexPerformanceWriter::VertexPerformanceWriter(
m_outputTree->Branch("nTracksTruthVtx", &m_nTracksOnTruthVertex);
m_outputTree->Branch("nTracksRecoVtx", &m_nTracksOnRecoVertex);

m_outputTree->Branch("trkVtxMatch", &m_trackVtxMatchFraction);
m_outputTree->Branch("recoVertexTrackWeights", &m_recoVertexTrackWeights);
m_outputTree->Branch("truthVertexTrackWeights", &m_truthVertexTrackWeights);
m_outputTree->Branch("truthVertexMatchRatio", &m_truthVertexMatchRatio);

m_outputTree->Branch("nRecoVtx", &m_nRecoVtx);
m_outputTree->Branch("nTrueVtx", &m_nTrueVtx);
Expand Down Expand Up @@ -323,7 +325,7 @@ ProcessCode VertexPerformanceWriter::writeT(
}
// If we don't know which truth particle corresponds to which track a
// priori, we check how many hits particles and tracks share. We match the
// particle to the track if a fraction of more than truthMatchProbMin of
// particle to the track if a fraction of more than trackMatchThreshold of
// hits that contribute to the track come from the particle. Note that not
// all tracksatVertex have matching parameters in trackParameters in this
// case. Equivalently, one could say that not all tracksAtVertex will be
Expand All @@ -345,7 +347,7 @@ ProcessCode VertexPerformanceWriter::writeT(
std::size_t nMajorityHits = particleHitCounts.front().hitCount;

if (nMajorityHits * 1. / track.nMeasurements() <
m_cfg.truthMatchProbMin) {
m_cfg.trackMatchThreshold) {
continue;
}

Expand Down Expand Up @@ -410,7 +412,7 @@ ProcessCode VertexPerformanceWriter::writeT(
// Containers for storing truth particles and truth vertices that contribute
// to the reconstructed vertex
SimParticleContainer particleAtVtx;
std::vector<SimBarcode> contributingTruthVertices;
std::vector<std::pair<SimBarcode, double>> contributingTruthVertices;

if (m_cfg.useTracks) {
for (const auto& trk : tracksAtVtx) {
Expand All @@ -434,7 +436,7 @@ ProcessCode VertexPerformanceWriter::writeT(
particleAtVtx.insert(particle);
SimBarcode vtxId =
particle.particleId().setParticle(0).setSubParticle(0);
contributingTruthVertices.push_back(vtxId);
contributingTruthVertices.emplace_back(vtxId, trk.trackWeight);
foundMatchingParams = true;
break;
}
Expand All @@ -447,29 +449,36 @@ ProcessCode VertexPerformanceWriter::writeT(
for (const auto& particle : allTruthParticles) {
SimBarcode vtxId =
particle.particleId().setParticle(0).setSubParticle(0);
contributingTruthVertices.push_back(vtxId);
contributingTruthVertices.emplace_back(vtxId, 1);
}
}

double recoVertexTrackWeights = 0;
for (const auto& trk : tracksAtVtx) {
recoVertexTrackWeights += trk.trackWeight;
}

// Find true vertex that contributes most to the reconstructed vertex
std::map<SimBarcode, int> fmap;
for (const SimBarcode& vtxId : contributingTruthVertices) {
fmap[vtxId]++;
std::map<SimBarcode, double> truthVertexWeights;
for (const auto& [vtxId, weight] : contributingTruthVertices) {
truthVertexWeights[vtxId] += weight;
}
int maxOccurrence = -1;
SimBarcode maxOccurrenceId = -1;
for (const auto& [vtxId, occurrence] : fmap) {
if (occurrence > maxOccurrence) {
maxOccurrenceId = vtxId;
maxOccurrence = occurrence;
}
auto truthVertexMatch = std::max_element(
truthVertexWeights.begin(), truthVertexWeights.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });

if (truthVertexMatch == truthVertexWeights.end()) {
ACTS_DEBUG("No truth vertex found for reconstructed vertex.");
continue;
}
SimBarcode truthVertexId = truthVertexMatch->first;
double truthVertexTrackWeights = truthVertexMatch->second;

// Count number of reconstructible tracks on truth vertex
int nTracksOnTruthVertex = 0;
for (const auto& particle : associatedTruthParticles) {
SimBarcode vtxId = particle.particleId().setParticle(0).setSubParticle(0);
if (vtxId == maxOccurrenceId) {
if (vtxId == truthVertexId) {
++nTracksOnTruthVertex;
}
}
Expand All @@ -482,12 +491,12 @@ ProcessCode VertexPerformanceWriter::writeT(
unsigned int nTracksOnRecoVertex =
std::count_if(tracksAtVtx.begin(), tracksAtVtx.end(), weightHighEnough);
// Match reconstructed and truth vertex if the tracks of the truth vertex
// make up at least minTrackVtxMatchFraction of the tracks at the
// make up at least vertexMatchThreshold of the track weight at the
// reconstructed vertex.
double trackVtxMatchFraction =
(m_cfg.useTracks ? (double)fmap[maxOccurrenceId] / nTracksOnRecoVertex
double vertexMatchFraction =
(m_cfg.useTracks ? truthVertexTrackWeights / recoVertexTrackWeights
: 1.0);
if (trackVtxMatchFraction > m_cfg.minTrackVtxMatchFraction) {
if (vertexMatchFraction > m_cfg.vertexMatchThreshold) {
int count = 0;
// Get references to inner vectors where all track variables corresponding
// to the current vertex will be saved
Expand Down Expand Up @@ -532,7 +541,7 @@ ProcessCode VertexPerformanceWriter::writeT(
SimBarcode vtxId =
particle.particleId().setParticle(0).setSubParticle(0);

if (vtxId == maxOccurrenceId) {
if (vtxId == truthVertexId) {
// Vertex found, fill variables

// Helper function for computing the pull
Expand Down Expand Up @@ -640,7 +649,9 @@ ProcessCode VertexPerformanceWriter::writeT(
m_nTracksOnTruthVertex.push_back(nTracksOnTruthVertex);
m_nTracksOnRecoVertex.push_back(nTracksOnRecoVertex);

m_trackVtxMatchFraction.push_back(trackVtxMatchFraction);
m_recoVertexTrackWeights.push_back(recoVertexTrackWeights);
m_truthVertexTrackWeights.push_back(truthVertexTrackWeights);
m_truthVertexMatchRatio.push_back(vertexMatchFraction);
}

// Saving the reconstructed/truth momenta. The reconstructed momenta
Expand Down Expand Up @@ -807,7 +818,9 @@ ProcessCode VertexPerformanceWriter::writeT(
m_sumPt2.clear();
m_nTracksOnTruthVertex.clear();
m_nTracksOnRecoVertex.clear();
m_trackVtxMatchFraction.clear();
m_recoVertexTrackWeights.clear();
m_truthVertexTrackWeights.clear();
m_truthVertexMatchRatio.clear();
m_trkParticleId.clear();
m_trkWeight.clear();
m_recoPhi.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ class VertexPerformanceWriter final
std::string treeName = "vertextree";
/// File access mode.
std::string fileMode = "RECREATE";
/// Minimum fraction of tracks matched between truth
/// and reco vertices to be matched for resolution plots.
double minTrackVtxMatchFraction = 0.5;
/// Minimum fraction of hits associated to particle to consider
/// Minimum fraction of track weight matched between truth
/// and reco vertices to consider as truth matched.
double vertexMatchThreshold = 0.7;
/// Minimum fraction of hits associated to particle to consider track
/// as truth matched.
double truthMatchProbMin = 0.5;
double trackMatchThreshold = 0.5;
/// Whether information about tracks is available
bool useTracks = true;
/// minimum track weight for track to be considered as part of the fit
Expand Down Expand Up @@ -166,7 +166,9 @@ class VertexPerformanceWriter final
std::vector<int> m_nTracksOnTruthVertex;
std::vector<int> m_nTracksOnRecoVertex;

std::vector<double> m_trackVtxMatchFraction;
std::vector<double> m_recoVertexTrackWeights;
std::vector<double> m_truthVertexTrackWeights;
std::vector<double> m_truthVertexMatchRatio;

/// Number of reconstructed vertices
int m_nRecoVtx = -1;
Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/python/acts/examples/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ def addVertexFitting(
inputAssociatedTruthParticles=associatedParticles,
inputVertices=outputVertices,
bField=field,
minTrackVtxMatchFraction=0.5 if associatedParticles else 0.0,
vertexMatchThreshold=0.7 if associatedParticles else 0.0,
treeName="vertexing",
filePath=str(outputDirRoot / "performance_vertexing.root"),
)
Expand Down
4 changes: 2 additions & 2 deletions Examples/Python/src/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ void addOutput(Context& ctx) {
ActsExamples::VertexPerformanceWriter, mex, "VertexPerformanceWriter",
inputAllTruthParticles, inputSelectedTruthParticles,
inputAssociatedTruthParticles, inputTracks, inputMeasurementParticlesMap,
inputVertices, bField, filePath, treeName, fileMode,
minTrackVtxMatchFraction, truthMatchProbMin, useTracks);
inputVertices, bField, filePath, treeName, fileMode, vertexMatchThreshold,
trackMatchThreshold, useTracks);

// CSV WRITERS
ACTS_PYTHON_DECLARE_WRITER(ActsExamples::CsvParticleWriter, mex,
Expand Down

0 comments on commit 49e815c

Please sign in to comment.