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

Record track quality to the CKF tracks #844

Merged
merged 2 commits into from
Feb 20, 2025
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
11 changes: 6 additions & 5 deletions benchmarks/common/benchmarks/toy_detector_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

// Detray include(s).
#include <detray/detectors/bfield.hpp>
#include <detray/io/frontend/detector_reader.hpp>
#include <detray/io/frontend/detector_writer.hpp>
#include <detray/navigation/navigator.hpp>
#include <detray/propagator/propagator.hpp>
#include <detray/propagator/rk_stepper.hpp>
#include <detray/test/utils/detectors/build_toy_detector.hpp>
#include <detray/test/utils/simulation/event_generator/track_generators.hpp>
#include <detray/tracks/ray.hpp>
Expand All @@ -38,11 +42,8 @@ class ToyDetectorBenchmark : public benchmark::Fixture {
// VecMem memory resource(s)
vecmem::host_memory_resource host_mr;

// TODO: This causes a segmentation fault
// static const int n_events = 100u;
// static const int n_tracks = 5000u;
static const int n_events = 50u;
static const int n_tracks = 10000u;
static const int n_events = 100u;
static const int n_tracks = 5000u;

std::vector<traccc::spacepoint_collection_types::host> spacepoints;
std::vector<traccc::measurement_collection_types::host> measurements;
Expand Down
8 changes: 5 additions & 3 deletions benchmarks/cpu/toy_detector_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
#include "benchmarks/toy_detector_benchmark.hpp"

// Detray include(s).
#include <detray/core/detector.hpp>
#include <detray/detectors/bfield.hpp>
#include <detray/io/frontend/detector_reader.hpp>
#include <detray/navigation/navigator.hpp>
#include <detray/propagator/propagator.hpp>
#include <detray/propagator/rk_stepper.hpp>

// VecMem include(s).
#include <vecmem/memory/host_memory_resource.hpp>

// Google benchmark include(s).
#include <benchmark/benchmark.h>

BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CPU)(benchmark::State& state) {
BENCHMARK_F(ToyDetectorBenchmark, CPU)(benchmark::State& state) {

// Type declarations
using host_detector_type = traccc::default_detector::host;
Expand Down Expand Up @@ -84,5 +88,3 @@ BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CPU)(benchmark::State& state) {
state.counters["event_throughput_Hz"] = benchmark::Counter(
static_cast<double>(n_events), benchmark::Counter::kIsRate);
}

BENCHMARK_REGISTER_F(ToyDetectorBenchmark, CPU)->UseRealTime();
6 changes: 3 additions & 3 deletions benchmarks/cuda/toy_detector_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#include "benchmarks/toy_detector_benchmark.hpp"

// Detray include(s).
#include <detray/core/detector.hpp>
#include <detray/detectors/bfield.hpp>
#include <detray/io/frontend/detector_reader.hpp>
#include <detray/navigation/navigator.hpp>
#include <detray/propagator/propagator.hpp>
#include <detray/propagator/rk_stepper.hpp>

// VecMem include(s).
Expand All @@ -37,7 +39,7 @@
// Google benchmark include(s).
#include <benchmark/benchmark.h>

BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CUDA)(benchmark::State& state) {
BENCHMARK_F(ToyDetectorBenchmark, CUDA)(benchmark::State& state) {

// Type declarations
using rk_stepper_type = detray::rk_stepper<
Expand Down Expand Up @@ -161,5 +163,3 @@ BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CUDA)(benchmark::State& state) {
state.counters["event_throughput_Hz"] = benchmark::Counter(
static_cast<double>(n_events), benchmark::Counter::kIsRate);
}

BENCHMARK_REGISTER_F(ToyDetectorBenchmark, CUDA)->UseRealTime();
24 changes: 23 additions & 1 deletion core/include/traccc/edm/track_candidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,38 @@
// Project include(s).
#include "traccc/edm/measurement.hpp"
#include "traccc/edm/track_parameters.hpp"
#include "traccc/edm/track_quality.hpp"

namespace traccc {

/// Finding result per track
struct finding_result {

/// Seed track parameter
traccc::bound_track_parameters seed_params;

/// Track summary
traccc::track_quality trk_quality;
};

/// Equality operator for finding result
TRACCC_HOST_DEVICE
inline bool operator==(const finding_result& lhs, const finding_result& rhs) {

return (lhs.seed_params == rhs.seed_params) &&
(lhs.trk_quality == rhs.trk_quality);
}

/// Declare a finding results collection types
using finding_result_collection_types = collection_types<finding_result>;

/// Track candidate is the measurement
using track_candidate = measurement;

/// Declare a track candidates collection types
using track_candidate_collection_types = collection_types<track_candidate>;
/// Declare a track candidates container type
using track_candidate_container_types =
container_types<bound_track_parameters, track_candidate>;
container_types<finding_result, track_candidate>;

} // namespace traccc
47 changes: 47 additions & 0 deletions core/include/traccc/edm/track_quality.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2025 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Project include(s).
#include "traccc/definitions/primitives.hpp"
#include "traccc/definitions/qualifiers.hpp"

namespace traccc {

/// Track quality
struct track_quality {

/// Number of degree of freedoms of fitted track
traccc::scalar ndf{0};

/// Chi square of fitted track
traccc::scalar chi2{0};

// The number of holes (The number of sensitive surfaces which do not have a
// measurement for the track pattern)
unsigned int n_holes{0u};

/// Reset the summary
TRACCC_HOST_DEVICE
void reset_quality() {
ndf = 0.f;
chi2 = 0.f;
n_holes = 0u;
}
};

/// Equality operator for track quality
TRACCC_HOST_DEVICE
inline bool operator==(const track_quality& lhs, const track_quality& rhs) {

return ((math::fabs(lhs.ndf - rhs.ndf) < float_epsilon) &&
(math::fabs(lhs.chi2 - rhs.chi2) < float_epsilon) &&
(lhs.n_holes == rhs.n_holes));
}

} // namespace traccc
20 changes: 3 additions & 17 deletions core/include/traccc/edm/track_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "traccc/edm/container.hpp"
#include "traccc/edm/measurement.hpp"
#include "traccc/edm/track_candidate.hpp"
#include "traccc/edm/track_quality.hpp"

// detray include(s).
#include <detray/tracks/bound_track_parameters.hpp>
Expand All @@ -27,23 +28,8 @@ struct fitting_result {
/// Fitted track parameter
detray::bound_track_parameters<algebra_t> fit_params;

/// Number of degree of freedoms of fitted track
scalar_type ndf{0};

/// Chi square of fitted track
scalar_type chi2{0};

// The number of holes (The number of sensitive surfaces which do not have a
// measurement for the track pattern)
unsigned int n_holes{0u};

/// Reset the statistics
TRACCC_HOST_DEVICE
void reset_statistics() {
ndf = 0.f;
chi2 = 0.f;
n_holes = 0u;
}
/// Track quality
traccc::track_quality trk_quality;
};

/// Fitting result per measurement
Expand Down
3 changes: 3 additions & 0 deletions core/include/traccc/finding/candidate_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct candidate_link {

// How many times it skipped a surface
unsigned int n_skipped;

// chi2
traccc::scalar chi2;
};

} // namespace traccc
38 changes: 28 additions & 10 deletions core/include/traccc/finding/details/find_tracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "traccc/finding/candidate_link.hpp"
#include "traccc/finding/finding_config.hpp"
#include "traccc/fitting/kalman_filter/gain_matrix_updater.hpp"
#include "traccc/fitting/status_codes.hpp"
#include "traccc/sanity/contiguous_on.hpp"
#include "traccc/utils/particle.hpp"
#include "traccc/utils/projections.hpp"
Expand Down Expand Up @@ -237,19 +236,21 @@ track_candidate_container_types::host find_tracks(
track_state<algebra_type> trk_state(meas);

// Run the Kalman update on a copy of the track parameters
const kalman_fitter_status res =
const bool res =
sf.template visit_mask<gain_matrix_updater<algebra_type>>(
trk_state, in_param);

const traccc::scalar chi2 = trk_state.filtered_chi2();

// The chi2 from Kalman update should be less than chi2_max
if (res == kalman_fitter_status::SUCCESS &&
trk_state.filtered_chi2() < config.chi2_max) {
if (res && chi2 < config.chi2_max) {
n_branches++;

links[step].push_back({{previous_step, in_param_id},
item_id,
orig_param_id,
skip_counter});
skip_counter,
chi2});
updated_params.push_back(trk_state.filtered());
}
}
Expand All @@ -261,10 +262,12 @@ track_candidate_container_types::host find_tracks(
if (n_branches == 0) {

// Put an invalid link with max item id
links[step].push_back({{previous_step, in_param_id},
std::numeric_limits<unsigned int>::max(),
orig_param_id,
skip_counter + 1});
links[step].push_back(
{{previous_step, in_param_id},
std::numeric_limits<unsigned int>::max(),
orig_param_id,
skip_counter + 1,
std::numeric_limits<traccc::scalar>::max()});

updated_params.push_back(in_param);
n_branches++;
Expand Down Expand Up @@ -387,6 +390,10 @@ track_candidate_container_types::host find_tracks(
vecmem::vector<track_candidate> cands_per_track;
cands_per_track.resize(n_cands);

// Track summary variables
scalar ndf_sum = 0.f;
scalar chi2_sum = 0.f;

// Reversely iterate to fill the track candidates
for (auto it = cands_per_track.rbegin(); it != cands_per_track.rend();
it++) {
Expand All @@ -405,14 +412,25 @@ track_candidate_container_types::host find_tracks(

*it = measurements.at(L.meas_idx);

// Sanity check on chi2
assert(L.chi2 < std::numeric_limits<traccc::scalar>::max());
assert(L.chi2 >= 0.f);

ndf_sum += static_cast<scalar>(it->meas_dim);
chi2_sum += L.chi2;

// Break the loop if the iterator is at the first candidate and
// fill the seed
if (it == cands_per_track.rend() - 1) {

auto cand_seed = seeds.at(L.previous.second);

// Add seed and track candidates to the output container
output_candidates.push_back(cand_seed, cands_per_track);
output_candidates.push_back(
finding_result{
cand_seed,
track_quality{ndf_sum - 5.f, chi2_sum, L.n_skipped}},
cands_per_track);
} else {
const auto l_pos =
param_to_link[L.previous.first][L.previous.second];
Expand Down
14 changes: 4 additions & 10 deletions core/include/traccc/fitting/details/fit_tracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// Project include(s).
#include "traccc/edm/track_candidate.hpp"
#include "traccc/edm/track_state.hpp"
#include "traccc/fitting/status_codes.hpp"

// VecMem include(s).
#include <vecmem/memory/memory_resource.hpp>
Expand Down Expand Up @@ -62,16 +61,11 @@ track_state_container_types::host fit_tracks(
typename fitter_t::state fitter_state(vecmem::get_data(input_states));

// Run the fitter.
kalman_fitter_status fit_status =
fitter.fit(track_candidates.get_headers()[i], fitter_state);
fitter.fit(track_candidates.get_headers()[i].seed_params, fitter_state);

if (fit_status == kalman_fitter_status::SUCCESS) {
// Save the results into the output container.
result.push_back(std::move(fitter_state.m_fit_res),
std::move(input_states));
} else {
// TODO: Print a warning here.
}
// Save the results into the output container.
result.push_back(std::move(fitter_state.m_fit_res),
std::move(input_states));
}

// Return the fitted track states.
Expand Down
Loading
Loading