Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding performance writer: comments + fake tracks #575

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions performance/src/efficiency/fake_tracks_plot_tool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Local include(s).
#include "../utils/helpers.hpp"

// Project include(s).
#include "traccc/edm/particle.hpp"

// System include(s).
#include <string_view>

// This is a duplicate of file "duplication_plot_tool.hpp", adapted to "fake
// tracks".

namespace traccc {

// Tools for creating "fake tracks" plots to show the proportion of fake tracks
// in the reconstruction data.
//
class fake_tracks_plot_tool {
public:
/// @brief The nested configuration struct
struct config {
std::map<std::string, plot_helpers::binning> var_binning = {
{"Eta", plot_helpers::binning("#eta", 40, -4, 4)},
{"Phi", plot_helpers::binning("#phi", 100, -3.15, 3.15)},
{"Pt", plot_helpers::binning("pT [GeV/c]", 40, 0, 100)},
{"Num", plot_helpers::binning("N", 30, -0.5, 29.5)}};
};

/// @brief Nested Cache struct
struct fake_tracks_plot_cache {
#ifdef TRACCC_HAVE_ROOT
std::unique_ptr<TProfile>
n_fake_vs_pT; ///< Number of fake tracks vs pT
std::unique_ptr<TProfile>
n_fake_vs_eta; ///< Number of fake tracks vs eta
std::unique_ptr<TProfile>
n_fake_vs_phi; ///< Number of fake tracks vs phi
std::unique_ptr<TEfficiency>
fake_rate_vs_pT; ///< Tracking fake tracks rate vs pT
std::unique_ptr<TEfficiency>
fake_rate_vs_eta; ///< Tracking fake tracks rate vs eta
std::unique_ptr<TEfficiency>
fake_rate_vs_phi; ///< Tracking fake tracks rate vs phi
#endif // TRACCC_HAVE_ROOT
};

/// Constructor
///
/// @param cfg Configuration struct
fake_tracks_plot_tool(const config& cfg) : m_cfg(cfg) {}

/// @brief book the fake tracks plots
///
/// @param fakePlotCache the cache for fake tracks plots
void book(std::string_view name, fake_tracks_plot_cache& cache) const {

plot_helpers::binning b_pt = m_cfg.var_binning.at("Pt");
plot_helpers::binning b_eta = m_cfg.var_binning.at("Eta");
plot_helpers::binning b_phi = m_cfg.var_binning.at("Phi");
plot_helpers::binning b_num = m_cfg.var_binning.at("Num");

// Avoid unused variable warnings when building the code without ROOT.
(void)name;
(void)cache;

#ifdef TRACCC_HAVE_ROOT
// fake tracks rate vs pT
cache.fake_rate_vs_pT = plot_helpers::book_eff(
TString(name) + "_fakeTracksRate_vs_pT",
"Fake tracks rate;pT [GeV/c];Fake tracks rate", b_pt);
// fake tracks rate vs eta
cache.fake_rate_vs_eta = plot_helpers::book_eff(
TString(name) + "_fakeTracksRate_vs_eta",
"Fake tracks rate;#eta;Fake tracks rate", b_eta);
// fake tracks rate vs phi
cache.fake_rate_vs_phi = plot_helpers::book_eff(
TString(name) + "_fakeTracksRate_vs_phi",
"Fake tracks rate;#phi;Fake tracks rate", b_phi);

// fake tracks number vs pT
cache.n_fake_vs_pT = plot_helpers::book_prof(
TString(name) + "_nFakeTracks_vs_pT",
"Averaged number of fake tracks per particle", b_pt, b_num);
// fake tracks number vs eta
cache.n_fake_vs_eta = plot_helpers::book_prof(
TString(name) + "_nFakeTracks_vs_eta",
"Averaged number of fake tracks per particle", b_eta, b_num);
// fake tracks number vs phi
cache.n_fake_vs_phi = plot_helpers::book_prof(
TString(name) + "_nFakeTracks_vs_phi",
"Averaged number of fake tracks per particle", b_phi, b_num);
#endif // TRACCC_HAVE_ROOT
}

/// @brief fill number of fake tracks for a truth particle seed
///
/// @param fakePlotCache cache object for fake tracks plots
/// @param truthParticle the truth Particle
/// @param nDuplicatedTracks the number of fake tracks
void fill(fake_tracks_plot_cache& cache, const particle& truth_particle,
size_t n_fake_tracks) const {
const auto t_phi = getter::phi(truth_particle.mom);
const auto t_eta = getter::eta(truth_particle.mom);
const auto t_pT =
getter::perp(vector2{truth_particle.mom[0], truth_particle.mom[1]});

// Avoid unused variable warnings when building the code without ROOT.
(void)t_phi;
(void)t_eta;
(void)t_pT;
(void)cache;
(void)n_fake_tracks;

#ifdef TRACCC_HAVE_ROOT
cache.n_fake_vs_pT->Fill(t_pT, n_fake_tracks);
cache.n_fake_vs_eta->Fill(t_eta, n_fake_tracks);
cache.n_fake_vs_phi->Fill(t_phi, n_fake_tracks);
#endif // TRACCC_HAVE_ROOT
}

/// @brief write the fake tracks plots to file
///
/// @param fakePlotCache cache object for fake tracks plots
void write(const fake_tracks_plot_cache& cache) const {

// Avoid unused variable warnings when building the code without ROOT.
(void)cache;

#ifdef TRACCC_HAVE_ROOT
cache.fake_rate_vs_pT->Write();
cache.fake_rate_vs_eta->Write();
cache.fake_rate_vs_phi->Write();
cache.n_fake_vs_pT->Write();
cache.n_fake_vs_eta->Write();
cache.n_fake_vs_phi->Write();
#endif // TRACCC_HAVE_ROOT
}

private:
config m_cfg; ///< The Config class
};

} // namespace traccc
70 changes: 66 additions & 4 deletions performance/src/efficiency/finding_performance_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "duplication_plot_tool.hpp"
#include "eff_plot_tool.hpp"
#include "fake_tracks_plot_tool.hpp"
#include "track_classification.hpp"

// ROOT include(s).
Expand All @@ -31,7 +32,8 @@ struct finding_performance_writer_data {
finding_performance_writer_data(
const finding_performance_writer::config& cfg)
: m_eff_plot_tool({cfg.var_binning}),
m_duplication_plot_tool({cfg.var_binning}) {}
m_duplication_plot_tool({cfg.var_binning}),
m_fake_tracks_plot_tool({cfg.var_binning}) {}

/// Plot tool for efficiency
eff_plot_tool m_eff_plot_tool;
Expand All @@ -41,6 +43,10 @@ struct finding_performance_writer_data {
duplication_plot_tool m_duplication_plot_tool;
duplication_plot_tool::duplication_plot_cache m_duplication_plot_cache;

// Plot tool for fake tracks monitoring
fake_tracks_plot_tool m_fake_tracks_plot_tool;
fake_tracks_plot_tool::fake_tracks_plot_cache m_fake_tracks_plot_cache;

measurement_particle_map m_measurement_particle_map;
particle_map m_particle_map;

Expand All @@ -56,13 +62,23 @@ finding_performance_writer::finding_performance_writer(const config& cfg)
m_data->m_eff_plot_cache);
m_data->m_duplication_plot_tool.book(m_cfg.algorithm_name,
m_data->m_duplication_plot_cache);
m_data->m_fake_tracks_plot_tool.book(m_cfg.algorithm_name,
m_data->m_fake_tracks_plot_cache);
}

finding_performance_writer::~finding_performance_writer() {}

namespace {

// For track finding
/**
* @brief For track finding only. Associates each reconstructed track with its
* measurements.
*
* @param track_candidates_view the track candidates found by the finding
* algorithm.
* @return std::vector<std::vector<measurement>> Associates each track index
* with its corresponding measurements.
*/
std::vector<std::vector<measurement>> prepare_data(
const track_candidate_container_types::const_view& track_candidates_view) {
std::vector<std::vector<measurement>> result;
Expand All @@ -87,7 +103,15 @@ std::vector<std::vector<measurement>> prepare_data(
return result;
}

// For track finding
/**
* @brief For ambiguity resolution only. Associates each reconstructed track
* with its measurements.
*
* @param track_candidates_view the track candidates found by the finding
* algorithm.
* @return std::vector<std::vector<measurement>> Associates each track index
* with its corresponding measurements.
*/
std::vector<std::vector<measurement>> prepare_data(
const track_state_container_types::const_view& track_states_view) {
std::vector<std::vector<measurement>> result;
Expand Down Expand Up @@ -116,8 +140,14 @@ void finding_performance_writer::write_common(
const std::vector<std::vector<measurement>>& tracks,
const event_map2& evt_map) {

// Associates truth particle_ids with the number of tracks made entirely of
// some (or all) of its hits.
std::map<particle_id, std::size_t> match_counter;

// Associates truth particle_ids with the number of tracks sharing hits from
// more than one truth particle.
std::map<particle_id, std::size_t> fake_counter;

// Iterate over the tracks.
const unsigned int n_tracks = tracks.size();

Expand All @@ -126,22 +156,43 @@ void finding_performance_writer::write_common(
const std::vector<measurement>& measurements = tracks[i];

// Check which particle matches this seed.
// Input :
// - the list of measurements for this track
// - the truth particles map
// Output :
// - a list of particles, having for each of them a particle_id and
// a count value.
// If there is only a single truth particle contributing to this track,
// then increment the match_counter for this truth particle id.
// If there are at least two particles contributing to the hit list of
// this track, increment the fake_counter for each truth particle.

std::vector<particle_hit_count> particle_hit_counts =
identify_contributing_particles(measurements, evt_map.meas_ptc_map);

if (particle_hit_counts.size() == 1) {
auto pid = particle_hit_counts.at(0).ptc.particle_id;
match_counter[pid]++;
}

if (particle_hit_counts.size() > 1) {
for (particle_hit_count const& phc : particle_hit_counts) {
auto pid = phc.ptc.particle_id;
fake_counter[pid]++;
}
}
}

// For each truth particle...
for (auto const& [pid, ptc] : evt_map.ptc_map) {

// Count only charged particles which satisfiy pT_cut
// Count only charged particles which satisfy pT_cut
if (ptc.charge == 0 || getter::perp(ptc.mom) < m_cfg.pT_cut) {
continue;
}

// Finds how many tracks were made solely by hits from the current truth
// particle
bool is_matched = false;
std::size_t n_matched_seeds_for_particle = 0;
auto it = match_counter.find(pid);
Expand All @@ -150,10 +201,20 @@ void finding_performance_writer::write_common(
n_matched_seeds_for_particle = it->second;
}

// Finds how many (fake) tracks were made with at least one hit from the
// current truth particle
std::size_t fake_count = 0;
auto itf = fake_counter.find(pid);
if (itf != fake_counter.end()) {
fake_count = itf->second;
}

m_data->m_eff_plot_tool.fill(m_data->m_eff_plot_cache, ptc, is_matched);
m_data->m_duplication_plot_tool.fill(m_data->m_duplication_plot_cache,
ptc,
n_matched_seeds_for_particle - 1);
m_data->m_fake_tracks_plot_tool.fill(m_data->m_fake_tracks_plot_cache,
ptc, fake_count);
}
}

Expand Down Expand Up @@ -194,6 +255,7 @@ void finding_performance_writer::finalize() {

m_data->m_eff_plot_tool.write(m_data->m_eff_plot_cache);
m_data->m_duplication_plot_tool.write(m_data->m_duplication_plot_cache);
m_data->m_fake_tracks_plot_tool.write(m_data->m_fake_tracks_plot_cache);
}

} // namespace traccc
Loading