12#include "detray/core/concepts.hpp"
13#include "detray/definitions/geometry.hpp"
14#include "detray/geometry/identifier.hpp"
15#include "detray/io/csv/dfe.hpp"
16#include "detray/io/utils/create_path.hpp"
17#include "detray/navigation/intersection/intersection.hpp"
18#include "detray/utils/ranges.hpp"
25namespace detray::io::csv {
69template <detray::concepts::detector detector_t>
70inline auto read_intersection2D(
const std::string &file_name) {
71 using algebra_t =
typename detector_t::algebra_type;
73 using surface_t =
typename detector_t::surface_type;
74 using nav_link_t =
typename surface_t::navigation_link;
75 using mask_link_t =
typename surface_t::mask_link;
76 using material_link_t =
typename surface_t::material_link;
77 using mask_id_t =
typename detector_t::masks::id;
78 using material_id_t =
typename detector_t::material::id;
80 using intersection_t =
83 dfe::NamedTupleCsvReader<io::csv::intersection2D> inters_reader(file_name);
86 std::vector<std::vector<intersection_t>> intersections_per_track;
89 while (inters_reader.read(inters_data)) {
92 while (intersections_per_track.size() <= trk_index) {
93 intersections_per_track.push_back({});
97 intersection_t inters{};
99 mask_link_t mask_link{};
100 mask_link.set_id(
static_cast<mask_id_t
>(inters_data.
mask_id));
101 if constexpr (detray::concepts::interval<
102 typename mask_link_t::index_type>) {
108 material_link_t material_link{
109 static_cast<material_id_t
>(inters_data.
material_id),
111 inters.set_surface({inters_data.
transform_index, mask_link, material_link,
113 inters.surface().set_identifier(
114 geometry::identifier{inters_data.
identifier});
115 inters.set_local({
static_cast<scalar_t
>(inters_data.
loc_0),
116 static_cast<scalar_t
>(inters_data.
loc_1), 0.f});
117 inters.set_path(
static_cast<scalar_t
>(inters_data.
path));
118 inters.set_volume_link(
static_cast<nav_link_t
>(inters_data.
volume_link));
119 inters.set_direction(
static_cast<bool>(inters_data.
direction));
120 inters.set_status(
static_cast<intersection::status
>(inters_data.
status));
123 intersections_per_track[trk_index].push_back(inters);
127 if (intersections_per_track.empty()) {
128 throw std::invalid_argument(
129 "ERROR: csv reader: Failed to read intersection data");
132 return intersections_per_track;
136template <
typename intersection_t>
137inline void write_intersection2D(
138 const std::string &file_name,
139 const std::vector<std::vector<intersection_t>> &intersections_per_track,
140 const bool replace =
true) {
141 using sf_desc_t =
decltype(intersections_per_track.at(0).at(0).surface());
142 using mask_link_t =
typename sf_desc_t::mask_link;
145 std::string inters_file_name{file_name};
150 io::create_path(std::filesystem::path{inters_file_name}.parent_path());
153 dfe::NamedTupleCsvWriter<io::csv::intersection2D> inters_writer(
156 for (
const auto &[track_idx, intersections] :
157 detray::views::enumerate(intersections_per_track)) {
159 if (intersections.empty()) {
163 for (
const auto &inters : intersections) {
164 io::csv::intersection2D inters_data{};
167 inters_data.identifier = inters.surface().identifier().value();
169 static_cast<unsigned int>(inters.surface().identifier().
id());
170 inters_data.transform_index = inters.surface().transform();
171 inters_data.mask_id =
172 static_cast<unsigned int>(inters.surface().mask().
id());
173 const auto mask_index = inters.surface().mask().index();
174 if constexpr (detray::concepts::interval<
175 typename mask_link_t::index_type>) {
176 inters_data.mask_index =
static_cast<unsigned int>(mask_index.lower());
177 inters_data.n_masks =
static_cast<unsigned int>(mask_index.size());
179 inters_data.mask_index =
static_cast<unsigned int>(mask_index);
180 inters_data.n_masks = 1u;
182 inters_data.material_id =
183 static_cast<unsigned int>(inters.surface().material().
id());
184 inters_data.material_index = inters.surface().material().index();
185 inters_data.loc_0 = inters.local()[0];
186 inters_data.loc_1 = inters.local()[1];
187 inters_data.path = inters.path();
188 inters_data.volume_link = inters.volume_link();
189 inters_data.direction =
static_cast<int>(inters.is_along());
190 inters_data.status =
static_cast<int>(inters.status());
192 inters_writer.append(inters_data);
This class holds the intersection information.
Definition intersection.hpp:212
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
constexpr dindex dindex_invalid
Global invalid index definition.
Definition indexing.hpp:21
Type to read the data of a line-surface intersection.
Definition intersection2D.hpp:28
unsigned int mask_index
Index of the surface mask.
Definition intersection2D.hpp:40
int direction
Navigation direction encoded as an integer flag.
Definition intersection2D.hpp:56
double path
Accumulated path length at the intersection.
Definition intersection2D.hpp:52
unsigned int type
Surface category identifier.
Definition intersection2D.hpp:34
unsigned int material_index
Index of the material representation.
Definition intersection2D.hpp:46
unsigned int track_id
Index of the track owning this intersection.
Definition intersection2D.hpp:30
int status
Intersection status encoded as a navigation status value.
Definition intersection2D.hpp:58
unsigned int transform_index
Index of the surface transform.
Definition intersection2D.hpp:36
unsigned int mask_id
Identifier of the surface mask type.
Definition intersection2D.hpp:38
DETRAY_DFE_NAMEDTUPLE(intersection2D, track_id, identifier, type, transform_index, mask_id, mask_index, material_id, material_index, loc_0, loc_1, path, volume_link, direction, status)
Define the serialized CSV column order for intersection records.
std::uint64_t identifier
Geometry identifier of the intersected surface.
Definition intersection2D.hpp:32
unsigned int n_masks
Number of masks in the linked mask range.
Definition intersection2D.hpp:42
double loc_0
First local intersection coordinate.
Definition intersection2D.hpp:48
unsigned int volume_link
Index of the associated volume link.
Definition intersection2D.hpp:54
unsigned int material_id
Identifier of the material representation.
Definition intersection2D.hpp:44
double loc_1
Second local intersection coordinate.
Definition intersection2D.hpp:50