Skip to content

Commit 673d408

Browse files
authored
Inhomogeneous Magnetic Field Examples, main branch (2025.06.25.) (#1042)
* Introduced traccc::opts::magnetic_field. Plus a small helper function for constructing an appropriate traccc::bfield object based on the command line options. * Made all non-throughput examples use opts::magnetic_field.
1 parent c482614 commit 673d408

19 files changed

+360
-115
lines changed

examples/options/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ traccc_add_library( traccc_options options TYPE SHARED
1515
"include/traccc/options/detector.hpp"
1616
"include/traccc/options/generation.hpp"
1717
"include/traccc/options/input_data.hpp"
18+
"include/traccc/options/magnetic_field.hpp"
1819
"include/traccc/options/output_data.hpp"
1920
"include/traccc/options/performance.hpp"
2021
"include/traccc/options/program_options.hpp"
@@ -33,6 +34,7 @@ traccc_add_library( traccc_options options TYPE SHARED
3334
"src/detector.cpp"
3435
"src/generation.cpp"
3536
"src/input_data.cpp"
37+
"src/magnetic_field.cpp"
3638
"src/output_data.cpp"
3739
"src/performance.cpp"
3840
"src/program_options.cpp"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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/io/data_format.hpp"
12+
#include "traccc/options/details/interface.hpp"
13+
14+
// Project include(s).
15+
#include "traccc/definitions/common.hpp"
16+
17+
// System include(s).
18+
#include <string>
19+
20+
namespace traccc::opts {
21+
22+
/// Options for the used magnetic field
23+
struct magnetic_field : public interface {
24+
25+
/// @name Options
26+
/// @{
27+
28+
/// Use an inhomogeneous magnetic field from an input file
29+
bool read_from_file = false;
30+
/// The file containing the magnetic field description
31+
std::string file = "geometries/odd/odd-bfield.cvf";
32+
/// The data format of the magnetic field file
33+
traccc::data_format format = data_format::binary;
34+
/// Magnetic field value when not reading from a file
35+
float value = 2.f * unit<float>::T;
36+
37+
/// @}
38+
39+
/// Constructor
40+
magnetic_field();
41+
42+
/// @name Functions implemented from @c traccc::opts::interface
43+
/// @{
44+
45+
/// Read/process the command line options
46+
///
47+
/// @param vm The command line options to interpret/read
48+
///
49+
void read(const boost::program_options::variables_map& vm) override;
50+
51+
/// Get a printable representation of the configuration
52+
std::unique_ptr<configuration_printable> as_printable() const override;
53+
54+
/// @}
55+
56+
}; // class magnetic_field
57+
58+
} // namespace traccc::opts
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// Local include(s).
9+
#include "traccc/options/magnetic_field.hpp"
10+
11+
// Project include(s).
12+
#include "traccc/examples/utils/printable.hpp"
13+
14+
// System include(s).
15+
#include <format>
16+
#include <sstream>
17+
#include <stdexcept>
18+
19+
namespace traccc::opts {
20+
21+
/// Helper namespace for convenience
22+
namespace po = boost::program_options;
23+
24+
/// Type alias for the data format enumeration
25+
using format_type = std::string;
26+
/// Name of the data format option
27+
static const char* format_option = "bfield-file-format";
28+
29+
magnetic_field::magnetic_field() : interface("Magnetic Field Options") {
30+
31+
m_desc.add_options()("read-bfield-from-file",
32+
po::bool_switch(&read_from_file),
33+
"Read the magnetic field from a file");
34+
m_desc.add_options()("bfield-file", po::value(&file)->default_value(file),
35+
"Magnetic field file");
36+
m_desc.add_options()(format_option,
37+
po::value<format_type>()->default_value("binary"),
38+
"Format of the magnetic field file");
39+
m_desc.add_options()("bfield-value",
40+
po::value(&value)->default_value(value),
41+
"Magnetic field value (when not reading from a file)");
42+
}
43+
44+
void magnetic_field::read(const po::variables_map& vm) {
45+
46+
// Decode the magnetic field file data format.
47+
if (vm.count(format_option)) {
48+
const std::string format_string = vm[format_option].as<format_type>();
49+
if (format_string == "csv") {
50+
format = data_format::csv;
51+
} else if (format_string == "binary") {
52+
format = data_format::binary;
53+
} else {
54+
throw std::invalid_argument("Unknown magnetic field data format");
55+
}
56+
}
57+
}
58+
59+
std::unique_ptr<configuration_printable> magnetic_field::as_printable() const {
60+
61+
auto cat = std::make_unique<configuration_category>(m_description);
62+
63+
cat->add_child(std::make_unique<configuration_kv_pair>(
64+
"Read magnetic field from file", std::format("{}", read_from_file)));
65+
cat->add_child(
66+
std::make_unique<configuration_kv_pair>("Magnetic field file", file));
67+
std::ostringstream format_ss;
68+
format_ss << format;
69+
cat->add_child(std::make_unique<configuration_kv_pair>(
70+
"Magnetic field file format", format_ss.str()));
71+
cat->add_child(std::make_unique<configuration_kv_pair>(
72+
"Magnetic field value", std::format("{} T", value / unit<float>::T)));
73+
74+
return cat;
75+
}
76+
77+
} // namespace traccc::opts

examples/run/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
# Project include(s).
88
include( traccc-compiler-options-cpp )
99

10+
# Create the common library.
11+
add_library(traccc_examples_common STATIC
12+
"common/make_magnetic_field.hpp"
13+
"common/make_magnetic_field.cpp"
14+
"common/throughput_mt.hpp"
15+
"common/throughput_mt.ipp"
16+
"common/throughput_st.hpp"
17+
"common/throughput_st.ipp")
18+
target_link_libraries(traccc_examples_common
19+
PUBLIC traccc::core traccc::options
20+
PRIVATE traccc::io)
21+
1022
# Add all the subdirectories that can be built.
1123
add_subdirectory(cpu)
1224

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// Local include(s).
9+
#include "make_magnetic_field.hpp"
10+
11+
// Project include(s).
12+
#include "traccc/definitions/primitives.hpp"
13+
#include "traccc/io/read_bfield.hpp"
14+
15+
namespace traccc::details {
16+
17+
bfield make_magnetic_field(const opts::magnetic_field& opts) {
18+
19+
if (opts.read_from_file) {
20+
covfie::field<inhom_bfield_backend_t<scalar>> field;
21+
io::read_bfield(field, opts.file, opts.format);
22+
return bfield{std::move(field)};
23+
} else {
24+
return bfield{construct_const_bfield<scalar>({0.f, 0.f, opts.value})};
25+
}
26+
}
27+
28+
} // namespace traccc::details
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// Project include(s).
11+
#include "traccc/options/magnetic_field.hpp"
12+
#include "traccc/utils/bfield.hpp"
13+
14+
namespace traccc::details {
15+
16+
/// Create a magnetic field object based on the provided options
17+
///
18+
/// @param opts The command line options for the magnetic field
19+
/// @return A magnetic field object configured according to the options
20+
///
21+
bfield make_magnetic_field(const opts::magnetic_field& opts);
22+
23+
} // namespace traccc::details

examples/run/cpu/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# TRACCC library, part of the ACTS project (R&D line)
22
#
3-
# (c) 2021-2024 CERN for the benefit of the ACTS project
3+
# (c) 2021-2025 CERN for the benefit of the ACTS project
44
#
55
# Mozilla Public License Version 2.0
66

77
traccc_add_executable( seeding_example "seeding_example.cpp"
88
LINK_LIBRARIES vecmem::core traccc::core traccc::io
9-
traccc::performance traccc::options detray::detectors detray::io)
9+
traccc::performance traccc::options detray::detectors detray::io
10+
traccc_examples_common )
1011

1112
traccc_add_executable( seq_example "seq_example.cpp"
1213
LINK_LIBRARIES vecmem::core traccc::core traccc::io
13-
traccc::performance traccc::options detray::detectors detray::io)
14+
traccc::performance traccc::options detray::detectors detray::io
15+
traccc_examples_common )
1416

1517
traccc_add_executable( truth_finding_example "truth_finding_example.cpp"
1618
LINK_LIBRARIES vecmem::core detray::detectors traccc::core traccc::io
17-
traccc::performance traccc::options)
19+
traccc::performance traccc::options traccc_examples_common )
1820

1921
traccc_add_executable( truth_fitting_example "truth_fitting_example.cpp"
2022
LINK_LIBRARIES vecmem::core detray::io detray::detectors traccc::core
21-
traccc::io traccc::performance traccc::options)
23+
traccc::io traccc::performance traccc::options traccc_examples_common )
2224

2325
traccc_add_executable( misaligned_truth_fitting_example "misaligned_truth_fitting_example.cpp"
2426
LINK_LIBRARIES vecmem::core detray::io detray::detectors traccc::core
25-
traccc::io traccc::performance traccc::options)
27+
traccc::io traccc::performance traccc::options traccc_examples_common )
2628

2729
#
2830
# Set up the "throughput applications".
@@ -31,7 +33,8 @@ add_library( traccc_examples_cpu STATIC
3133
"full_chain_algorithm.hpp"
3234
"full_chain_algorithm.cpp" )
3335
target_link_libraries( traccc_examples_cpu
34-
PUBLIC vecmem::core detray::core detray::detectors traccc::core )
36+
PUBLIC vecmem::core detray::core detray::detectors traccc::core
37+
traccc_examples_common )
3538

3639
traccc_add_executable( throughput_st "throughput_st.cpp"
3740
LINK_LIBRARIES indicators::indicators vecmem::core detray::detectors

examples/run/cpu/misaligned_truth_fitting_example.cpp

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

88
// Project include(s).
9+
#include "../common/make_magnetic_field.hpp"
910
#include "traccc/definitions/common.hpp"
1011
#include "traccc/definitions/primitives.hpp"
1112
#include "traccc/fitting/kalman_fitting_algorithm.hpp"
@@ -14,6 +15,7 @@
1415
#include "traccc/io/utils.hpp"
1516
#include "traccc/options/detector.hpp"
1617
#include "traccc/options/input_data.hpp"
18+
#include "traccc/options/magnetic_field.hpp"
1719
#include "traccc/options/performance.hpp"
1820
#include "traccc/options/program_options.hpp"
1921
#include "traccc/options/track_fitting.hpp"
@@ -49,13 +51,14 @@ int main(int argc, char* argv[]) {
4951

5052
// Program options.
5153
traccc::opts::detector detector_opts;
54+
traccc::opts::magnetic_field bfield_opts;
5255
traccc::opts::input_data input_opts;
5356
traccc::opts::track_propagation propagation_opts;
5457
traccc::opts::track_fitting fitting_opts;
5558
traccc::opts::performance performance_opts;
5659
traccc::opts::program_options program_opts{
5760
"Truth Track Fitting on the Host",
58-
{detector_opts, input_opts, propagation_opts, fitting_opts,
61+
{detector_opts, bfield_opts, input_opts, propagation_opts, fitting_opts,
5962
performance_opts},
6063
argc,
6164
argv,
@@ -78,11 +81,8 @@ int main(int argc, char* argv[]) {
7881
* Build a geometry
7982
*****************************/
8083

81-
// B field value and its type
82-
// @TODO: Set B field as argument
83-
const traccc::vector3 B{0, 0, 2 * traccc::unit<traccc::scalar>::T};
84-
const traccc::bfield field{
85-
traccc::construct_const_bfield<traccc::scalar>(B)};
84+
// B field value
85+
const traccc::bfield field = details::make_magnetic_field(bfield_opts);
8686

8787
// Read the detector
8888
detray::io::detector_reader_config reader_cfg{};

examples/run/cpu/seeding_example.cpp

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

88
// Project include(s).
9+
#include "../common/make_magnetic_field.hpp"
910
#include "traccc/definitions/common.hpp"
1011
#include "traccc/definitions/primitives.hpp"
1112
#include "traccc/geometry/detector.hpp"
@@ -34,6 +35,7 @@
3435
// options
3536
#include "traccc/options/detector.hpp"
3637
#include "traccc/options/input_data.hpp"
38+
#include "traccc/options/magnetic_field.hpp"
3739
#include "traccc/options/performance.hpp"
3840
#include "traccc/options/program_options.hpp"
3941
#include "traccc/options/track_finding.hpp"
@@ -60,6 +62,7 @@ int seq_run(const traccc::opts::track_seeding& seeding_opts,
6062
const traccc::opts::track_fitting& fitting_opts,
6163
const traccc::opts::input_data& input_opts,
6264
const traccc::opts::detector& detector_opts,
65+
const traccc::opts::magnetic_field& bfield_opts,
6366
const traccc::opts::performance& performance_opts,
6467
std::unique_ptr<const traccc::Logger> ilogger) {
6568
TRACCC_LOCAL_LOGGER(std::move(ilogger));
@@ -98,11 +101,9 @@ int seq_run(const traccc::opts::track_seeding& seeding_opts,
98101
* Build a geometry
99102
*****************************/
100103

101-
// B field value and its type
102-
// @TODO: Set B field as argument
103-
const traccc::vector3 B{0, 0, 2 * traccc::unit<traccc::scalar>::T};
104-
const traccc::bfield field{
105-
traccc::construct_const_bfield<traccc::scalar>(B)};
104+
// B field value
105+
const traccc::bfield field =
106+
traccc::details::make_magnetic_field(bfield_opts);
106107

107108
// Construct a Detray detector object, if supported by the configuration.
108109
traccc::default_detector::host detector{host_mr};
@@ -274,6 +275,7 @@ int main(int argc, char* argv[]) {
274275

275276
// Program options.
276277
traccc::opts::detector detector_opts;
278+
traccc::opts::magnetic_field bfield_opts;
277279
traccc::opts::input_data input_opts;
278280
traccc::opts::track_seeding seeding_opts;
279281
traccc::opts::track_finding finding_opts;
@@ -283,7 +285,7 @@ int main(int argc, char* argv[]) {
283285
traccc::opts::performance performance_opts;
284286
traccc::opts::program_options program_opts{
285287
"Full Tracking Chain on the Host (without clusterization)",
286-
{detector_opts, input_opts, seeding_opts, finding_opts,
288+
{detector_opts, bfield_opts, input_opts, seeding_opts, finding_opts,
287289
propagation_opts, resolution_opts, fitting_opts, performance_opts},
288290
argc,
289291
argv,
@@ -292,5 +294,5 @@ int main(int argc, char* argv[]) {
292294
// Run the application.
293295
return seq_run(seeding_opts, finding_opts, propagation_opts,
294296
resolution_opts, fitting_opts, input_opts, detector_opts,
295-
performance_opts, logger->clone());
297+
bfield_opts, performance_opts, logger->clone());
296298
}

0 commit comments

Comments
 (0)