Skip to content

Commit

Permalink
Add Ambiguity Resolution Performance Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJoube committed Feb 21, 2024
1 parent fd145d8 commit 5b41d8c
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/run/cpu/seeding_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "traccc/seeding/track_params_estimation.hpp"

// performance
#include "traccc/efficiency/ambiguity_resolution_performance_writer.hpp"
#include "traccc/efficiency/finding_performance_writer.hpp"
#include "traccc/efficiency/seeding_performance_writer.hpp"
#include "traccc/resolution/fitting_performance_writer.hpp"
Expand Down Expand Up @@ -82,6 +83,9 @@ int seq_run(const traccc::seeding_input_config& /*i_cfg*/,
traccc::fitting_performance_writer fit_performance_writer(
traccc::fitting_performance_writer::config{});

traccc::ambiguity_resolution_performance_writer ar_performance_writer(
traccc::ambiguity_resolution_performance_writer::config{});

// Output stats
uint64_t n_spacepoints = 0;
uint64_t n_measurements = 0;
Expand Down Expand Up @@ -198,6 +202,10 @@ int seq_run(const traccc::seeding_input_config& /*i_cfg*/,
track_states = host_fitting(host_det, field, track_candidates);
n_fitted_tracks += track_states.size();

/*-----------------------------------------
Ambiguity Resolution with Greedy Solver
-----------------------------------------*/

if (common_opts.perform_ambiguity_resolution) {
track_states_ar = host_ambiguity_resolution(track_states);
n_ambiguity_free_tracks += track_states_ar.size();
Expand Down Expand Up @@ -226,6 +234,11 @@ int seq_run(const traccc::seeding_input_config& /*i_cfg*/,
find_performance_writer.write(traccc::get_data(track_candidates),
evt_map);

if (common_opts.perform_ambiguity_resolution) {
ar_performance_writer.write(traccc::get_data(track_states_ar),
evt_map);
}

for (unsigned int i = 0; i < track_states.size(); i++) {
const auto& trk_states_per_track = track_states.at(i).items;

Expand All @@ -241,6 +254,9 @@ int seq_run(const traccc::seeding_input_config& /*i_cfg*/,
sd_performance_writer.finalize();
find_performance_writer.finalize();
fit_performance_writer.finalize();
if (common_opts.perform_ambiguity_resolution) {
ar_performance_writer.finalize();
}
}

std::cout << "==> Statistics ... " << std::endl;
Expand Down
1 change: 1 addition & 0 deletions performance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ traccc_add_library( traccc_performance performance TYPE SHARED
"src/efficiency/seeding_performance_writer.cpp"
"include/traccc/efficiency/finding_performance_writer.hpp"
"src/efficiency/finding_performance_writer.cpp"
"src/efficiency/ambiguity_resolution_performance_writer.cpp"
"src/efficiency/track_classification.hpp"
"include/traccc/efficiency/nseed_performance_writer.hpp"
"src/efficiency/nseed_performance_writer.cpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

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

// Project include(s).
#include "traccc/edm/track_state.hpp"
#include "traccc/io/event_map2.hpp"

// System include(s).
#include <map>
#include <memory>
#include <string>
#include <string_view>

// Adapted from finding_performance_writer.hpp

namespace traccc {
namespace details {

/// Data members that should not pollute the API of
/// @c traccc::ambiguity_resolution_performance_writer
struct ambiguity_resolution_performance_writer_data;

} // namespace details

class ambiguity_resolution_performance_writer {

public:
/// Configuration for the tool
struct config {

/// Output filename.
std::string file_path = "performance_track_ambiguity_resolution.root";
/// Output file mode
std::string file_mode = "RECREATE";

/// Plot tool configurations.
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("p_{T} [GeV/c]", 40, 0, 100)},
{"Num", plot_helpers::binning("N", 30, -0.5, 29.5)}};

/// Cut values
scalar pT_cut = 0.1f * traccc::unit<scalar>::GeV;
};

/// Construct from configuration and log level.
/// @param cfg The configuration
///
ambiguity_resolution_performance_writer(const config& cfg);

/// Destructor
~ambiguity_resolution_performance_writer();

void write(const typename track_state_container_types::const_view&
track_states_view,
const event_map2& evt_map);

void finalize();

private:
/// Configuration for the tool
config m_cfg;

/// Opaque data members for the class
std::unique_ptr<details::ambiguity_resolution_performance_writer_data>
m_data;

}; // class ambiguity_resolution_performance_writer

} // namespace traccc
141 changes: 141 additions & 0 deletions performance/src/efficiency/ambiguity_resolution_performance_writer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "traccc/efficiency/ambiguity_resolution_performance_writer.hpp"

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

// ROOT include(s).
#ifdef TRACCC_HAVE_ROOT
#include <TFile.h>
#endif // TRACCC_HAVE_ROOT

// System include(s).
#include <iostream>
#include <memory>
#include <stdexcept>

// Adapted from finding_performance_writer.cpp

namespace traccc {
namespace details {

struct ambiguity_resolution_performance_writer_data {

/// Constructor
ambiguity_resolution_performance_writer_data(
const ambiguity_resolution_performance_writer::config& cfg)
: m_eff_plot_tool({cfg.var_binning}),
m_duplication_plot_tool({cfg.var_binning}) {}

/// Plot tool for efficiency
eff_plot_tool m_eff_plot_tool;
eff_plot_tool::eff_plot_cache m_eff_plot_cache;

/// Plot tool for duplication rate
duplication_plot_tool m_duplication_plot_tool;
duplication_plot_tool::duplication_plot_cache m_duplication_plot_cache;

measurement_particle_map m_measurement_particle_map;
particle_map m_particle_map;

}; // struct ambiguity_resolution_performance_writer_data

} // namespace details

ambiguity_resolution_performance_writer::
ambiguity_resolution_performance_writer(const config& cfg)
: m_cfg(cfg),
m_data(std::make_unique<
details::ambiguity_resolution_performance_writer_data>(cfg)) {

m_data->m_eff_plot_tool.book("ambiguity_resolution",
m_data->m_eff_plot_cache);
m_data->m_duplication_plot_tool.book("ambiguity_resolution",
m_data->m_duplication_plot_cache);
}

ambiguity_resolution_performance_writer::
~ambiguity_resolution_performance_writer() {}

void ambiguity_resolution_performance_writer::write(
const track_state_container_types::const_view& track_states_view,
const event_map2& evt_map) {

std::map<particle_id, std::size_t> match_counter;

// Iterate over the tracks.
track_state_container_types::const_device track_states(track_states_view);

const unsigned int n_tracks = track_states.size();
for (unsigned int track_index = 0; track_index < n_tracks; ++track_index) {

auto const& [fit_res, states] = track_states.at(track_index);

std::vector<measurement> measurements;
measurements.reserve(states.size());
for (const auto& st : states) {
measurements.push_back(st.get_measurement());
}

// Check which particle matches this seed.
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]++;
}
}

for (auto const& [pid, ptc] : evt_map.ptc_map) {

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

bool is_matched = false;
std::size_t n_matched_seeds_for_particle = 0;
auto it = match_counter.find(pid);
if (it != match_counter.end()) {
is_matched = true;
n_matched_seeds_for_particle = it->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);
}
}

void ambiguity_resolution_performance_writer::finalize() {

#ifdef TRACCC_HAVE_ROOT
// Open the output file.
std::unique_ptr<TFile> ofile(
TFile::Open(m_cfg.file_path.c_str(), m_cfg.file_mode.c_str()));
if ((!ofile) || ofile->IsZombie()) {
throw std::runtime_error("Could not open output file \"" +
m_cfg.file_path + "\" in mode \"" +
m_cfg.file_mode + "\"");
}
ofile->cd();
#else
std::cout << "ROOT file \"" << m_cfg.file_path << "\" is NOT created"
<< std::endl;
#endif // TRACCC_HAVE_ROOT

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);
}

} // namespace traccc

0 comments on commit 5b41d8c

Please sign in to comment.