ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
track_parameters.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
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"
18
19// System include(s)
20#include <filesystem>
21#include <stdexcept>
22
23namespace detray::io::csv {
24
28 unsigned int track_id = 0;
30 double x = 0.;
32 double y = 0.;
34 double z = 0.;
36 double t = 0.;
38 double px = 0.;
40 double py = 0.;
42 double pz = 0.;
44 double q = 0.;
45
48 q);
49};
50
55 unsigned int track_id = 0;
57 double l0 = 0.;
59 double l1 = 0.;
61 double phi = 0.;
63 double theta = 0.;
65 double qop = 0.;
67 double t = 0.;
68
71 qop, t);
72};
73
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;
79 using scalar_t = dscalar<algebra_t>;
80 using point3_t = dpoint3D<algebra_t>;
81 using vector3_t = dvector3D<algebra_t>;
83
84 dfe::NamedTupleCsvReader<io::csv::free_track_parameters> track_param_reader(
85 file_name);
86
87 io::csv::free_track_parameters track_param_data{};
88 std::vector<std::vector<std::pair<scalar_t, track_t>>> track_params_per_track;
89
90 while (track_param_reader.read(track_param_data)) {
91 // Add new track state to correct track
92 dindex trk_index{track_param_data.track_id};
93 while (track_params_per_track.size() <= trk_index) {
94 track_params_per_track.emplace_back();
95 }
96
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)};
103
104 track_t track_param{pos, static_cast<scalar_t>(track_param_data.t), p,
105 static_cast<scalar_t>(track_param_data.q)};
106
107 // Add to collection
108 track_params_per_track[trk_index].emplace_back(track_param_data.q,
109 track_param);
110 }
111
112 // Check the result
113 if (track_params_per_track.empty()) {
114 throw std::invalid_argument(
115 "ERROR: csv reader: Failed to read free track parameters data");
116 }
117
118 return track_params_per_track;
119}
120
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) {
128 // Don't write over existing data
129 std::string trk_file_name{file_name};
130 if (!replace && io::file_exists(file_name)) {
131 trk_file_name = io::alt_file_name(file_name);
132 } else {
133 // Make sure the output directories exit
134 io::create_path(std::filesystem::path{trk_file_name}.parent_path());
135 }
136
137 dfe::NamedTupleCsvWriter<io::csv::free_track_parameters> track_param_writer(
138 trk_file_name);
139
140 for (const auto &[track_idx, track_params] :
141 detray::views::enumerate(track_params_per_track)) {
142 // Skip empty traces
143 if (track_params.empty()) {
144 continue;
145 }
146
147 for (const auto &[charge, track_param] : track_params) {
148 const auto &glob_pos = track_param.pos();
149 // Momentum may not be retrievable for straight-line tracks
150 const auto &p{charge != 0.f ? track_param.mom(charge)
151 : track_param.dir()};
152
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;
163
164 track_param_writer.append(track_param_data);
165 }
166 }
167}
168
169} // namespace detray::io::csv
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