ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
intersection2D.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/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"
19
20// System include(s).
21#include <cstdint>
22#include <filesystem>
23#include <stdexcept>
24
25namespace detray::io::csv {
26
30 unsigned int track_id = 0;
32 std::uint64_t identifier = 0ul;
34 unsigned int type = 0u;
36 unsigned int transform_index = 0u;
38 unsigned int mask_id = 0u;
40 unsigned int mask_index = 0u;
42 unsigned int n_masks = 1u;
44 unsigned int material_id = 0u;
46 unsigned int material_index = 0u;
48 double loc_0 = 0.;
50 double loc_1 = 0.;
52 double path = 0.;
54 unsigned int volume_link = 0u;
56 int direction = 0;
58 int status = 0;
59
65};
66
69template <detray::concepts::detector detector_t>
70inline auto read_intersection2D(const std::string &file_name) {
71 using algebra_t = typename detector_t::algebra_type;
72 using scalar_t = dscalar<algebra_t>;
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;
79
80 using intersection_t =
82
83 dfe::NamedTupleCsvReader<io::csv::intersection2D> inters_reader(file_name);
84
85 io::csv::intersection2D inters_data{};
86 std::vector<std::vector<intersection_t>> intersections_per_track;
87
88 // Read the data
89 while (inters_reader.read(inters_data)) {
90 // Add new intersection to correct track
91 dindex trk_index{inters_data.track_id};
92 while (intersections_per_track.size() <= trk_index) {
93 intersections_per_track.push_back({});
94 }
95
96 // Read the intersection
97 intersection_t inters{};
98
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>) {
103 mask_link.set_index({inters_data.mask_index, inters_data.n_masks});
104 } else {
105 mask_link.set_index(inters_data.mask_index);
106 }
107
108 material_link_t material_link{
109 static_cast<material_id_t>(inters_data.material_id),
110 inters_data.material_index};
111 inters.set_surface({inters_data.transform_index, mask_link, material_link,
112 dindex_invalid, surface_id::e_unknown});
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));
121
122 // Add to collection
123 intersections_per_track[trk_index].push_back(inters);
124 }
125
126 // Check the result
127 if (intersections_per_track.empty()) {
128 throw std::invalid_argument(
129 "ERROR: csv reader: Failed to read intersection data");
130 }
131
132 return intersections_per_track;
133}
134
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;
143
144 // Don't write over existing data
145 std::string inters_file_name{file_name};
146 if (!replace && io::file_exists(file_name)) {
147 inters_file_name = io::alt_file_name(file_name);
148 } else {
149 // Make sure the output directories exit
150 io::create_path(std::filesystem::path{inters_file_name}.parent_path());
151 }
152
153 dfe::NamedTupleCsvWriter<io::csv::intersection2D> inters_writer(
154 inters_file_name);
155
156 for (const auto &[track_idx, intersections] :
157 detray::views::enumerate(intersections_per_track)) {
158 // Skip empty traces
159 if (intersections.empty()) {
160 continue;
161 }
162
163 for (const auto &inters : intersections) {
164 io::csv::intersection2D inters_data{};
165
166 inters_data.track_id = track_idx;
167 inters_data.identifier = inters.surface().identifier().value();
168 inters_data.type =
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());
178 } else {
179 inters_data.mask_index = static_cast<unsigned int>(mask_index);
180 inters_data.n_masks = 1u;
181 }
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());
191
192 inters_writer.append(inters_data);
193 }
194 }
195}
196
197} // namespace detray::io::csv
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