Skip to content

Commit

Permalink
Cleanup python plotting code in preparation for benchmark plots
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Jan 17, 2025
1 parent b4abb48 commit 2fd05f0
Show file tree
Hide file tree
Showing 29 changed files with 861 additions and 696 deletions.
11 changes: 11 additions & 0 deletions tests/include/detray/test/common/navigation_validation_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ struct navigation_validation_config
std::string m_track_param_file{"truth_trk_parameters.csv"};
/// The maximal number of test tracks to run
std::size_t m_n_tracks{detray::detail::invalid_value<std::size_t>()};
/// Configured momentum range of the test sample (only needed to generate
/// correct file names). If none was passed, it will be determined from
/// the track data (imprecise!)
darray<scalar_type, 2> m_p_range{
detray::detail::invalid_value<scalar_type>(),
detray::detail::invalid_value<scalar_type>()};
/// B-field vector for helix
vector3_type m_B{0.f * unit<scalar_type>::T, 0.f * unit<scalar_type>::T,
2.f * unit<scalar_type>::T};
Expand All @@ -55,6 +61,7 @@ struct navigation_validation_config
const std::string &intersection_file() const { return m_intersection_file; }
const std::string &track_param_file() const { return m_track_param_file; }
std::size_t n_tracks() const { return m_n_tracks; }
darray<scalar_type, 2> p_range() const { return m_p_range; }
const vector3_type &B_vector() { return m_B; }
const auto &svg_style() const { return m_style; }
/// @}
Expand Down Expand Up @@ -86,6 +93,10 @@ struct navigation_validation_config
m_n_tracks = n;
return *this;
}
navigation_validation_config &p_range(const darray<scalar_type, 2> pr) {
m_p_range = pr;
return *this;
}
navigation_validation_config &B_vector(const vector3_type &B) {
m_B = B;
return *this;
Expand Down
19 changes: 14 additions & 5 deletions tests/include/detray/test/cpu/detector_scan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,24 @@ class detector_scan : public test::fixture_base<> {
const std::size_t n_helices{trk_state_generator.size()};
intersection_traces.reserve(n_helices);

const auto pT_range = m_cfg.track_generator().mom_range();
std::string mometum_str{std::to_string(pT_range[0]) + "_" +
std::to_string(pT_range[1])};
std::string momentum_str{""};
if constexpr (!k_use_rays) {
const auto pT_range = m_cfg.track_generator().mom_range();
// Remove floating point imprecisions
momentum_str =
std::to_string(
std::floor(10. * static_cast<double>(pT_range[0])) / 10.) +
"_" +
std::to_string(
std::ceil(10. * static_cast<double>(pT_range[1])) / 10.) +
"_GeV";
}

std::string track_param_file_name{m_cfg.track_param_file() + "_" +
mometum_str + "GeV.csv"};
momentum_str + ".csv"};

std::string intersection_file_name{m_cfg.intersection_file() + "_" +
mometum_str + "GeV.csv"};
momentum_str + ".csv"};

const bool data_files_exist{io::file_exists(intersection_file_name) &&
io::file_exists(track_param_file_name)};
Expand Down
2 changes: 1 addition & 1 deletion tests/include/detray/test/cpu/material_scan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class material_scan : public test::fixture_base<> {
}

// Record track parameters
tracks.push_back({ray.pos(), 0.f, ray.dir(), -1.f});
tracks.push_back({ray.pos(), 0.f, ray.dir(), 0.f});

// New material record
material_record_t mat_record{};
Expand Down
34 changes: 24 additions & 10 deletions tests/include/detray/test/cpu/navigation_validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,18 @@ class navigation_validation : public test::fixture_base<> {
trajectory_type test_traj = get_parametrized_trajectory(track);

const scalar q = start.charge;
// If the momentum is unknown, 1 GeV is the safest option to keep
// the direction vector normalized
const scalar pT{q == 0.f ? 1.f * unit<scalar>::GeV : track.pT(q)};
const scalar p{q == 0.f ? 1.f * unit<scalar>::GeV : track.p(q)};
min_pT = std::min(min_pT, pT);
max_pT = std::max(max_pT, pT);

// If the momentum is unknown, 1 GeV is the safest option to keep
// the direction vector normalized
if (detray::detail::is_invalid_value(m_cfg.p_range()[0])) {
min_pT = std::min(min_pT, pT);
max_pT = std::max(max_pT, pT);
} else {
min_pT = m_cfg.p_range()[0];
max_pT = m_cfg.p_range()[1];
}

// Run the propagation
auto [success, obj_tracer, step_trace, mat_record, mat_trace,
Expand Down Expand Up @@ -223,20 +229,28 @@ class navigation_validation : public test::fixture_base<> {
n_matching_error);

// Print track positions for plotting
std::string mometum_str{std::to_string(min_pT) + "_" +
std::to_string(max_pT)};
std::string momentum_str{""};
if constexpr (!k_use_rays) {
momentum_str =
std::to_string(std::floor(10. * static_cast<double>(min_pT)) /
10.) +
"_" +
std::to_string(std::ceil(10. * static_cast<double>(max_pT)) /
10.) +
"_GeV";
}

const auto data_path{
std::filesystem::path{m_cfg.track_param_file()}.parent_path()};
const auto truth_trk_path{data_path / (prefix + "truth_track_params_" +
mometum_str + "GeV.csv")};
momentum_str + ".csv")};
const auto trk_path{data_path / (prefix + "navigation_track_params_" +
mometum_str + "GeV.csv")};
momentum_str + ".csv")};
const auto mat_path{data_path / (prefix + "accumulated_material_" +
mometum_str + "GeV.csv")};
momentum_str + ".csv")};
const auto missed_path{
data_path /
(prefix + "missed_intersections_dists_" + mometum_str + "GeV.csv")};
(prefix + "missed_intersections_dists_" + momentum_str + ".csv")};

// Write the distance of the missed intersection local position
// to the surface boundaries to file for plotting
Expand Down
34 changes: 24 additions & 10 deletions tests/include/detray/test/device/cuda/navigation_validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,14 @@ class navigation_validation : public test::fixture_base<> {
: trck_param.pT(q)};
const scalar p{q == 0.f ? 1.f * unit<scalar>::GeV
: trck_param.p(q)};
min_pT = std::min(min_pT, pT);
max_pT = std::max(max_pT, pT);

if (detray::detail::is_invalid_value(m_cfg.p_range()[0])) {
min_pT = std::min(min_pT, pT);
max_pT = std::max(max_pT, pT);
} else {
min_pT = m_cfg.p_range()[0];
max_pT = m_cfg.p_range()[1];
}

// Recorded only the start position, which added by default
bool success{true};
Expand Down Expand Up @@ -354,22 +360,30 @@ class navigation_validation : public test::fixture_base<> {
n_matching_error);

// Print track positions for plotting
std::string mometum_str{std::to_string(min_pT) + "_" +
std::to_string(max_pT)};
std::string momentum_str{""};
if constexpr (!k_use_rays) {
momentum_str =
std::to_string(std::floor(10. * static_cast<double>(min_pT)) /
10.) +
"_" +
std::to_string(std::ceil(10. * static_cast<double>(max_pT)) /
10.) +
"_GeV";
}

const auto data_path{
std::filesystem::path{m_cfg.track_param_file()}.parent_path()};
const auto truth_trk_path{
data_path /
(prefix + "truth_track_params_cuda_" + mometum_str + "GeV.csv")};
const auto trk_path{data_path /
(prefix + "navigation_track_params_cuda_" +
mometum_str + "GeV.csv")};
(prefix + "truth_track_params_cuda_" + momentum_str + ".csv")};
const auto trk_path{
data_path /
(prefix + "navigation_track_params_cuda_" + momentum_str + ".csv")};
const auto mat_path{data_path / (prefix + "accumulated_material_cuda_" +
mometum_str + "GeV.csv")};
momentum_str + ".csv")};
const auto missed_path{data_path /
(prefix + "missed_intersections_dists_cuda_" +
mometum_str + "GeV.csv")};
momentum_str + ".csv")};

// Write the distance of the missed intersection local position
// to the surface boundaries to file for plotting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "detray/definitions/detail/math.hpp"
#include "detray/definitions/detail/qualifiers.hpp"
#include "detray/definitions/units.hpp"
#include "detray/navigation/detail/ray.hpp"
#include "detray/utils/ranges/ranges.hpp"

// Detray test include(s)
Expand Down Expand Up @@ -123,12 +124,15 @@ class random_track_generator
vector3_t mom{math::cos(phi) * sin_theta,
math::sin(phi) * sin_theta, math::cos(theta)};

sin_theta = (sin_theta == scalar_t{0.f})
? std::numeric_limits<scalar_t>::epsilon()
: sin_theta;

mom = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * p_mag *
vector::normalize(mom);
if constexpr (std::is_same_v<track_t, detail::ray<algebra_t>>) {
mom = vector::normalize(mom);
} else {
sin_theta = (sin_theta == scalar_t{0.f})
? std::numeric_limits<scalar_t>::epsilon()
: sin_theta;
mom = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * p_mag *
vector::normalize(mom);
}

// Randomly flip the charge sign
darray<double, 2> signs{1., -1.};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ struct random_track_generator_config {
// General
out << "\nRandom track generator\n"
<< "----------------------------\n"
<< " Random seed : " << cfg.seed() << "\n"
<< " No. tracks : " << cfg.n_tracks() << "\n"
<< " Charge : "
<< cfg.charge() / detray::unit<scalar_t>::e << " [e]\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "detray/definitions/detail/math.hpp"
#include "detray/definitions/detail/qualifiers.hpp"
#include "detray/definitions/units.hpp"
#include "detray/navigation/detail/ray.hpp"
#include "detray/utils/ranges/ranges.hpp"

// Detray test include(s)
Expand Down Expand Up @@ -154,11 +155,15 @@ class uniform_track_generator
math::sin(m_phi) * sin_theta, math::cos(m_theta)};

// Magnitude of momentum
sin_theta = (sin_theta == scalar_t{0.f})
? std::numeric_limits<scalar_t>::epsilon()
: sin_theta;
p = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * m_cfg.m_p_mag *
vector::normalize(p);
if constexpr (std::is_same_v<track_t, detail::ray<algebra_t>>) {
p = vector::normalize(p);
} else {
sin_theta = (sin_theta == scalar_t{0.f})
? std::numeric_limits<scalar_t>::epsilon()
: sin_theta;
p = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * m_cfg.m_p_mag *
vector::normalize(p);
}

const auto& ori = m_cfg.origin();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ struct uniform_track_generator_config {
DETRAY_HOST_DEVICE constexpr darray<scalar_t, 2> eta_range() const {
return m_eta_range;
}
DETRAY_HOST_DEVICE constexpr darray<scalar_t, 2> mom_range() const {
return {m_p_mag, m_p_mag};
}
DETRAY_HOST_DEVICE constexpr std::size_t phi_steps() const {
return m_phi_steps;
}
Expand Down Expand Up @@ -230,6 +233,7 @@ struct uniform_track_generator_config {
// General
out << "\nUnform track generator\n"
<< "----------------------------\n"
<< " Random seed : " << cfg.seed() << "\n"
<< " No. tracks : " << cfg.n_tracks() << "\n"
<< " -> phi steps : " << cfg.phi_steps() << "\n"
<< " -> theta/eta steps : " << cfg.theta_steps() << "\n"
Expand Down
3 changes: 2 additions & 1 deletion tests/tools/include/detray/options/propagation_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void add_options<detray::navigation::config>(
"mask_tolerance_scalor",
boost::program_options::value<float>()->default_value(
cfg.mask_tolerance_scalor),
"Mask tolerance scaling")(
"Mask tolerance scale factor")(
"overstep_tolerance",
boost::program_options::value<float>()->default_value(
cfg.overstep_tolerance / unit<float>::um),
Expand Down Expand Up @@ -172,6 +172,7 @@ void configure_options<detray::stepping::config>(

cfg.path_limit = path_limit * unit<float>::m;
}
cfg.do_covariance_transport = false;
if (vm.count("covariance_transport")) {
cfg.do_covariance_transport = true;
}
Expand Down
Loading

0 comments on commit 2fd05f0

Please sign in to comment.