Skip to content

Commit cae081e

Browse files
authored
B-Field Rewrite / Cleanup, main branch (2025.07.10.) (#1069)
* Re-wrote magnetic field handling in the project's libraries. * Adapted the examples to traccc::magnetic_field. * Adapted the tests and benchmarks to traccc::magnetic_field.
1 parent 608bcfc commit cae081e

File tree

103 files changed

+828
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+828
-575
lines changed

benchmarks/common/benchmarks/toy_detector_benchmark.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
// Traccc include(s).
9+
#include "traccc/bfield/construct_const_bfield.hpp"
910
#include "traccc/definitions/common.hpp"
1011
#include "traccc/finding/finding_config.hpp"
1112
#include "traccc/fitting/fitting_config.hpp"
@@ -17,7 +18,6 @@
1718
#include "traccc/simulation/measurement_smearer.hpp"
1819
#include "traccc/simulation/simulator.hpp"
1920
#include "traccc/simulation/smearing_writer.hpp"
20-
#include "traccc/utils/bfield.hpp"
2121

2222
// Detray include(s).
2323
#include <detray/io/frontend/detector_writer.hpp>
@@ -92,7 +92,7 @@ class ToyDetectorBenchmark : public benchmark::Fixture {
9292
detray::build_toy_detector<algebra_type>(host_mr, get_toy_config());
9393

9494
// B field
95-
b_field_t field = traccc::construct_const_bfield<scalar_type>(B);
95+
const auto field = traccc::construct_const_bfield(B);
9696

9797
// Origin of particles
9898
using generator_type = detray::random_track_generator<
@@ -123,7 +123,8 @@ class ToyDetectorBenchmark : public benchmark::Fixture {
123123

124124
auto sim = traccc::simulator<detector_type, b_field_t, generator_type,
125125
writer_type>(
126-
traccc::muon<scalar_type>(), n_events, det, field,
126+
traccc::muon<scalar_type>(), n_events, det,
127+
field.as_field<traccc::const_bfield_backend_t<scalar_type>>(),
127128
std::move(generator), std::move(smearer_writer_cfg), full_path);
128129

129130
// Same propagation configuration for sim and reco

benchmarks/cpu/toy_detector_cpu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
// Traccc core include(s).
9+
#include "traccc/bfield/construct_const_bfield.hpp"
910
#include "traccc/geometry/detector.hpp"
1011

1112
// Traccc algorithm include(s).
@@ -49,7 +50,7 @@ BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CPU)(benchmark::State& state) {
4950
sim_dir + "toy_detector_surface_grids.json");
5051

5152
// B field
52-
const traccc::bfield field{traccc::construct_const_bfield<scalar_type>(B)};
53+
const auto field = traccc::construct_const_bfield(B);
5354

5455
// Algorithms
5556
traccc::host::seeding_algorithm sa(seeding_cfg, grid_cfg, filter_cfg,

benchmarks/cuda/toy_detector_cuda.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
// Project include(s).
9+
#include "traccc/bfield/construct_const_bfield.hpp"
910
#include "traccc/cuda/finding/combinatorial_kalman_filter_algorithm.hpp"
1011
#include "traccc/cuda/fitting/kalman_fitting_algorithm.hpp"
1112
#include "traccc/cuda/seeding/seeding_algorithm.hpp"
@@ -56,8 +57,7 @@ BENCHMARK_DEFINE_F(ToyDetectorBenchmark, CUDA)(benchmark::State& state) {
5657
sim_dir + "toy_detector_surface_grids.json");
5758

5859
// B field
59-
const traccc::bfield field{traccc::construct_const_bfield<
60-
traccc::default_detector::host::scalar_type>(B)};
60+
const auto field = traccc::construct_const_bfield(B);
6161

6262
// Algorithms
6363
traccc::cuda::seeding_algorithm sa_cuda(seeding_cfg, grid_cfg, filter_cfg,

core/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ traccc_add_library( traccc_core core TYPE SHARED
3535
"include/traccc/edm/track_candidate_collection.hpp"
3636
"include/traccc/edm/impl/track_candidate_collection.ipp"
3737
"include/traccc/edm/track_candidate_container.hpp"
38+
# Magnetic field description.
39+
"include/traccc/bfield/magnetic_field_types.hpp"
40+
"include/traccc/bfield/magnetic_field.hpp"
41+
"include/traccc/bfield/impl/magnetic_field.ipp"
42+
"include/traccc/bfield/construct_const_bfield.hpp"
43+
"include/traccc/bfield/impl/construct_const_bfield.ipp"
44+
"src/bfield/construct_const_bfield.cpp"
3845
# Geometry description.
3946
"include/traccc/geometry/detector.hpp"
4047
"include/traccc/geometry/module_map.hpp"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Local include(s).
11+
#include "traccc/bfield/magnetic_field.hpp"
12+
#include "traccc/definitions/primitives.hpp"
13+
14+
namespace traccc {
15+
16+
/// Construct a constant magnetic field object
17+
///
18+
/// @tparam scalar_t The scalar type to construct the field with
19+
///
20+
/// @param x The X component of the constant field
21+
/// @param y The Y component of the constant field
22+
/// @param z The Z component of the constant field
23+
///
24+
template <typename scalar_t>
25+
magnetic_field construct_const_bfield(scalar_t x, scalar_t y, scalar_t z);
26+
27+
/// Construct a constant magnetic field object
28+
///
29+
/// @param v The 3-vector describing the constant field
30+
///
31+
magnetic_field construct_const_bfield(const vector3& v);
32+
33+
} // namespace traccc
34+
35+
// Include the implementation.
36+
#include "traccc/bfield/impl/construct_const_bfield.ipp"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Local include(s).
11+
#include "traccc/bfield/magnetic_field_types.hpp"
12+
13+
// Covfie include(s).
14+
#include <covfie/core/field.hpp>
15+
16+
namespace traccc {
17+
18+
template <typename scalar_t>
19+
magnetic_field construct_const_bfield(scalar_t x, scalar_t y, scalar_t z) {
20+
21+
return magnetic_field{::covfie::field<const_bfield_backend_t<scalar_t>>{
22+
::covfie::make_parameter_pack(
23+
typename const_bfield_backend_t<scalar_t>::configuration_t{x, y,
24+
z})}};
25+
}
26+
27+
} // namespace traccc
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
namespace traccc {
11+
12+
template <covfie::concepts::field_backend bfield_backend_t>
13+
magnetic_field::magnetic_field(covfie::field<bfield_backend_t>&& obj)
14+
: m_field(std::move(obj)) {}
15+
16+
template <covfie::concepts::field_backend bfield_backend_t>
17+
void magnetic_field::set(covfie::field<bfield_backend_t>&& obj) {
18+
m_field = std::move(obj);
19+
}
20+
21+
template <covfie::concepts::field_backend bfield_backend_t>
22+
bool magnetic_field::is() const {
23+
return (m_field.type() == typeid(covfie::field<bfield_backend_t>));
24+
}
25+
26+
template <covfie::concepts::field_backend bfield_backend_t>
27+
typename covfie::field<bfield_backend_t>::view_t magnetic_field::as_view()
28+
const {
29+
return typename covfie::field<bfield_backend_t>::view_t{
30+
std::any_cast<const covfie::field<bfield_backend_t>&>(m_field)};
31+
}
32+
33+
template <covfie::concepts::field_backend bfield_backend_t>
34+
const covfie::field<bfield_backend_t>& magnetic_field::as_field() const {
35+
return std::any_cast<const covfie::field<bfield_backend_t>&>(m_field);
36+
}
37+
38+
} // namespace traccc
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Covfie include(s).
11+
#include <covfie/core/concepts.hpp>
12+
#include <covfie/core/field.hpp>
13+
14+
// System include(s).
15+
#include <any>
16+
17+
namespace traccc {
18+
19+
/// Typeless, owning, host-only magnetic field object
20+
class magnetic_field {
21+
22+
public:
23+
/// Default constructor
24+
magnetic_field() = default;
25+
26+
/// Constructor from a specific b-field object
27+
///
28+
/// @tparam bfield_backend_t The backend type of the b-field object
29+
/// @param obj The b-field object to construct from
30+
///
31+
template <covfie::concepts::field_backend bfield_backend_t>
32+
explicit magnetic_field(covfie::field<bfield_backend_t>&& obj);
33+
34+
/// Set a specific b-field object
35+
///
36+
/// @tparam bfield_backend_t The backend type of the b-field object
37+
/// @param obj The b-field object to set
38+
///
39+
template <covfie::concepts::field_backend bfield_backend_t>
40+
void set(covfie::field<bfield_backend_t>&& obj);
41+
42+
/// Check if the b-field is of a certain type
43+
///
44+
/// @tparam bfield_backend_t The covfie backend type to check
45+
/// @return @c true if the b-field is of the specified type,
46+
/// @c false otherwise
47+
///
48+
template <covfie::concepts::field_backend bfield_backend_t>
49+
bool is() const;
50+
51+
/// Get a b-field view object as a specific type
52+
///
53+
/// @tparam bfield_backend_t The covfie backend type to use
54+
/// @return The b-field view object of the specified type
55+
///
56+
template <covfie::concepts::field_backend bfield_backend_t>
57+
typename covfie::field<bfield_backend_t>::view_t as_view() const;
58+
59+
/// Get the b-field object as a specific type
60+
///
61+
/// @tparam bfield_backend_t The covfie backend type to use
62+
/// @return The b-field object cast to the specified type
63+
///
64+
template <covfie::concepts::field_backend bfield_backend_t>
65+
const covfie::field<bfield_backend_t>& as_field() const;
66+
67+
private:
68+
/// The actualy covfie b-field object
69+
std::any m_field;
70+
71+
}; // class magnetic_field
72+
73+
} // namespace traccc
74+
75+
// Include the implementation.
76+
#include "traccc/bfield/impl/magnetic_field.ipp"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Covfie include(s).
11+
#include <covfie/core/backend/primitive/constant.hpp>
12+
#include <covfie/core/backend/transformer/affine.hpp>
13+
#include <covfie/core/backend/transformer/clamp.hpp>
14+
#include <covfie/core/backend/transformer/linear.hpp>
15+
#include <covfie/core/backend/transformer/strided.hpp>
16+
#include <covfie/core/concepts.hpp>
17+
#include <covfie/core/vector.hpp>
18+
19+
namespace traccc {
20+
21+
/// Constant magnetic field backend type
22+
template <typename scalar_t>
23+
using const_bfield_backend_t =
24+
::covfie::backend::constant<::covfie::vector::vector_d<scalar_t, 3>,
25+
::covfie::vector::vector_d<scalar_t, 3>>;
26+
// Test that the type is a valid backend for a field
27+
static_assert(covfie::concepts::field_backend<const_bfield_backend_t<float>>,
28+
"const_bfield_backend_t is not a valid field backend type");
29+
30+
namespace host {
31+
32+
/// Inhomogeneous magnetic field used for IO
33+
template <typename scalar_t>
34+
using inhom_io_bfield_backend_t =
35+
covfie::backend::affine<covfie::backend::linear<covfie::backend::strided<
36+
covfie::vector::vector_d<std::size_t, 3>,
37+
covfie::backend::array<covfie::vector::vector_d<scalar_t, 3>>>>>;
38+
// Test that the type is a valid backend for a field
39+
static_assert(covfie::concepts::field_backend<inhom_io_bfield_backend_t<float>>,
40+
"inhom_io_bfield_backend_t is not a valid field backend type");
41+
42+
/// Inhomogeneous magnetic field backend type
43+
template <typename scalar_t>
44+
using inhom_bfield_backend_t = covfie::backend::affine<
45+
covfie::backend::linear<covfie::backend::clamp<covfie::backend::strided<
46+
covfie::vector::vector_d<std::size_t, 3>,
47+
covfie::backend::array<covfie::vector::vector_d<scalar_t, 3>>>>>>;
48+
// Test that the type is a valid backend for a field
49+
static_assert(covfie::concepts::field_backend<inhom_bfield_backend_t<float>>,
50+
"inhom_bfield_backend_t is not a valid field backend type");
51+
52+
} // namespace host
53+
} // namespace traccc

core/include/traccc/finding/combinatorial_kalman_filter_algorithm.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#pragma once
99

1010
// Project include(s).
11+
#include "traccc/bfield/magnetic_field.hpp"
1112
#include "traccc/edm/measurement.hpp"
1213
#include "traccc/edm/track_candidate_collection.hpp"
1314
#include "traccc/edm/track_parameters.hpp"
1415
#include "traccc/finding/finding_config.hpp"
1516
#include "traccc/geometry/detector.hpp"
1617
#include "traccc/utils/algorithm.hpp"
17-
#include "traccc/utils/bfield.hpp"
1818
#include "traccc/utils/messaging.hpp"
1919

2020
// VecMem include(s).
@@ -32,11 +32,11 @@ namespace traccc::host {
3232
///
3333
class combinatorial_kalman_filter_algorithm
3434
: public algorithm<edm::track_candidate_collection<default_algebra>::host(
35-
const default_detector::host&, const bfield&,
35+
const default_detector::host&, const magnetic_field&,
3636
const measurement_collection_types::const_view&,
3737
const bound_track_parameters_collection_types::const_view&)>,
3838
public algorithm<edm::track_candidate_collection<default_algebra>::host(
39-
const telescope_detector::host&, const bfield&,
39+
const telescope_detector::host&, const magnetic_field&,
4040
const measurement_collection_types::const_view&,
4141
const bound_track_parameters_collection_types::const_view&)>,
4242
public messaging {
@@ -55,31 +55,31 @@ class combinatorial_kalman_filter_algorithm
5555
/// Execute the algorithm
5656
///
5757
/// @param det The (default) detector object
58-
/// @param field The magnetic field object
58+
/// @param bfield The magnetic field object
5959
/// @param measurements All measurements in an event
6060
/// @param seeds All seeds in an event to start the track finding
6161
/// with
6262
///
6363
/// @return A container of the found track candidates
6464
///
6565
output_type operator()(
66-
const default_detector::host& det, const bfield& field,
66+
const default_detector::host& det, const magnetic_field& bfield,
6767
const measurement_collection_types::const_view& measurements,
6868
const bound_track_parameters_collection_types::const_view& seeds)
6969
const override;
7070

7171
/// Execute the algorithm
7272
///
7373
/// @param det The (telescope) detector object
74-
/// @param field The magnetic field object
74+
/// @param bfield The magnetic field object
7575
/// @param measurements All measurements in an event
7676
/// @param seeds All seeds in an event to start the track finding
7777
/// with
7878
///
7979
/// @return A container of the found track candidates
8080
///
8181
output_type operator()(
82-
const telescope_detector::host& det, const bfield& field,
82+
const telescope_detector::host& det, const magnetic_field& bfield,
8383
const measurement_collection_types::const_view& measurements,
8484
const bound_track_parameters_collection_types::const_view& seeds)
8585
const override;

0 commit comments

Comments
 (0)