12#include "detray/core/concepts.hpp"
13#include "detray/definitions/algebra.hpp"
14#include "detray/io/csv/dfe.hpp"
15#include "detray/io/utils/create_path.hpp"
16#include "detray/tracks/free_track_parameters.hpp"
17#include "detray/utils/ranges.hpp"
23namespace detray::io::csv {
76template <detray::concepts::detector detector_t>
77inline auto read_free_track_params(
const std::string &file_name) {
78 using algebra_t =
typename detector_t::algebra_type;
84 dfe::NamedTupleCsvReader<io::csv::free_track_parameters> track_param_reader(
88 std::vector<std::vector<std::pair<scalar_t, track_t>>> track_params_per_track;
90 while (track_param_reader.read(track_param_data)) {
93 while (track_params_per_track.size() <= trk_index) {
94 track_params_per_track.emplace_back();
97 point3_t pos{
static_cast<scalar_t
>(track_param_data.
x),
98 static_cast<scalar_t
>(track_param_data.
y),
99 static_cast<scalar_t
>(track_param_data.
z)};
100 vector3_t p{
static_cast<scalar_t
>(track_param_data.
px),
101 static_cast<scalar_t
>(track_param_data.
py),
102 static_cast<scalar_t
>(track_param_data.
pz)};
104 track_t track_param{pos,
static_cast<scalar_t
>(track_param_data.
t), p,
105 static_cast<scalar_t
>(track_param_data.
q)};
108 track_params_per_track[trk_index].emplace_back(track_param_data.
q,
113 if (track_params_per_track.empty()) {
114 throw std::invalid_argument(
115 "ERROR: csv reader: Failed to read free track parameters data");
118 return track_params_per_track;
122template <detray::concepts::scalar scalar_t,
typename track_t>
123inline void write_free_track_params(
124 const std::string &file_name,
125 const std::vector<std::vector<std::pair<scalar_t, track_t>>>
126 &track_params_per_track,
127 const bool replace =
true) {
129 std::string trk_file_name{file_name};
137 dfe::NamedTupleCsvWriter<io::csv::free_track_parameters> track_param_writer(
140 for (
const auto &[track_idx, track_params] :
141 detray::views::enumerate(track_params_per_track)) {
143 if (track_params.empty()) {
147 for (
const auto &[charge, track_param] : track_params) {
148 const auto &glob_pos = track_param.pos();
150 const auto &p{charge != 0.f ? track_param.mom(charge)
151 : track_param.dir()};
153 io::csv::free_track_parameters track_param_data{};
154 track_param_data.
track_id = track_idx;
155 track_param_data.x = glob_pos[0];
156 track_param_data.y = glob_pos[1];
157 track_param_data.z = glob_pos[2];
158 track_param_data.t = track_param.time();
159 track_param_data.px = p[0];
160 track_param_data.py = p[1];
161 track_param_data.pz = p[2];
162 track_param_data.q = charge;
164 track_param_writer.append(track_param_data);
std::string alt_file_name(const std::string &name)
Find an unused numbered alternative to an existing file name.
Definition create_path.hpp:31
auto create_path(const std::string &outdir)
Check if a given file path exists and generate it if not.
Definition create_path.hpp:79
bool file_exists(const std::string &outdir)
Check if a file exists.
Definition create_path.hpp:22
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
unsigned int dindex
Default index type.
Definition indexing.hpp:18
get_vector3D_t< A, T > dvector3D
Three-dimensional vector type for algebra A and value type T.
Definition algebra.hpp:80
get_point3D_t< A, T > dpoint3D
Three-dimensional point type for algebra A and value type T.
Definition algebra.hpp:72
free_parameters_vector< algebra_t > free_track_parameters
The free track parameters consist only of the parameter vector.
Definition free_track_parameters.hpp:249
Type to read the data of bound track parameters.
Definition track_parameters.hpp:53
unsigned int track_id
Index of the track owning this state.
Definition track_parameters.hpp:55
double phi
Azimuth angle of the track direction.
Definition track_parameters.hpp:61
double qop
Signed inverse momentum.
Definition track_parameters.hpp:65
DETRAY_DFE_NAMEDTUPLE(bound_track_parameters, track_id, l0, l1, phi, theta, qop, t)
Define CSV column order for the bound-state fields below.
double l0
First local bound coordinate.
Definition track_parameters.hpp:57
double t
Track time.
Definition track_parameters.hpp:67
double theta
Polar angle of the track direction.
Definition track_parameters.hpp:63
double l1
Second local bound coordinate.
Definition track_parameters.hpp:59
Type to read the data of free track parameters.
Definition track_parameters.hpp:26
double px
x component of the momentum vector.
Definition track_parameters.hpp:38
unsigned int track_id
Index of the track owning this state.
Definition track_parameters.hpp:28
DETRAY_DFE_NAMEDTUPLE(free_track_parameters, track_id, x, y, z, t, px, py, pz, q)
Define CSV column order for the free-state fields below.
double q
Particle charge.
Definition track_parameters.hpp:44
double x
Global x coordinate of the track position.
Definition track_parameters.hpp:30
double z
Global z coordinate of the track position.
Definition track_parameters.hpp:34
double y
Global y coordinate of the track position.
Definition track_parameters.hpp:32
double pz
z component of the momentum vector.
Definition track_parameters.hpp:42
double t
Track time.
Definition track_parameters.hpp:36
double py
y component of the momentum vector.
Definition track_parameters.hpp:40