ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
inspectors.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// TODO: Remove this when gcc fixes their false positives.
12#if defined(__GNUC__) && !defined(__clang__)
13#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
14#endif
15
16// Project include(s)
17#include "detray/core/concepts.hpp"
18#include "detray/definitions/algebra.hpp"
19#include "detray/definitions/math.hpp"
20#include "detray/navigation/detail/print_state.hpp"
21#include "detray/navigation/intersection/intersection.hpp"
22#include "detray/navigation/navigation_config.hpp"
23#include "detray/propagator/base_actor.hpp"
24#include "detray/propagator/base_stepper.hpp"
25#include "detray/propagator/detail/print_stepper_state.hpp"
26#include "detray/propagator/stepping_config.hpp"
27#include "detray/tracks/ray.hpp"
28#include "detray/utils/logging.hpp"
29#include "detray/utils/tuple_helpers.hpp"
30
31// Test include(s)
32#include "detray/test/utils/data_record.hpp"
33
34// System include(s)
35#include <iomanip>
36#include <limits>
37#include <sstream>
38#include <string>
39#include <tuple>
40#include <type_traits>
41#include <utility>
42
43namespace detray {
44
46template <typename... Inspectors>
49 using view_type = dmulti_view<detail::get_view_t<Inspectors>...>;
51 using const_view_type = dmulti_view<detail::get_view_t<const Inspectors>...>;
52
54 using inspector_tuple_t = std::tuple<Inspectors...>;
57
60
62 template <concepts::device_view view_t>
63 DETRAY_HOST_DEVICE explicit aggregate_inspector(view_t &view)
65 view, std::make_index_sequence<sizeof...(Inspectors)>{})) {}
66
75 template <unsigned int current_id = 0, typename state_type,
76 concepts::point3D point3_t, concepts::vector3D vector3_t,
77 typename... Args>
78 DETRAY_HOST_DEVICE auto operator()(state_type &state,
79 const navigation::config &cfg,
80 const point3_t &pos, const vector3_t &dir,
81 const char *message, Args &&...args) {
82 // Call inspector
83 std::get<current_id>(_inspectors)(state, cfg, pos, dir, message,
84 std::forward<Args>(args)...);
85
86 // Next inspector
87 if constexpr (current_id < std::tuple_size<inspector_tuple_t>::value - 1) {
88 return operator()<current_id + 1>(state, cfg, pos, dir, message,
89 std::forward<Args>(args)...);
90 }
91 }
92
98 template <unsigned int current_id = 0, typename state_type, typename... Args>
99 DETRAY_HOST_DEVICE auto operator()(state_type &state, const char *message,
100 Args &&...args) {
101 // Call inspector
102 std::get<current_id>(_inspectors)(state, message);
103
104 // Next inspector
105 if constexpr (current_id < std::tuple_size<inspector_tuple_t>::value - 1) {
106 return operator()<current_id + 1>(state, message,
107 std::forward<Args>(args)...);
108 }
109 }
110
112 template <typename inspector_t>
113 DETRAY_HOST_DEVICE constexpr decltype(auto) get() {
114 return std::get<inspector_t>(_inspectors);
115 }
116
118 template <typename inspector_t>
119 DETRAY_HOST_DEVICE constexpr decltype(auto) get() const {
120 return std::get<inspector_t>(_inspectors);
121 }
122
124 template <std::size_t I>
125 DETRAY_HOST_DEVICE constexpr decltype(auto) get() {
126 return std::get<I>(_inspectors);
127 }
128
130 template <std::size_t I>
131 DETRAY_HOST_DEVICE constexpr decltype(auto) get() const {
132 return std::get<I>(_inspectors);
133 }
134
138 template <concepts::device_view view_t, std::size_t... I>
139 DETRAY_HOST_DEVICE constexpr auto unroll_views(
140 view_t &view, std::index_sequence<I...> /*seq*/) {
141 return detail::make_tuple<std::tuple>(
142 Inspectors(detail::get<I>(view.m_view))...);
143 }
144};
145
146namespace navigation {
147
150template <concepts::detector detector_t,
151 template <typename...> class vector_t = dvector,
152 status... navigation_status>
162
164 using view_type = dvector_view<candidate_record_t>;
166 using const_view_type = dvector_view<const candidate_record_t>;
167
169 object_tracer() = default;
170
173 DETRAY_HOST_DEVICE explicit object_tracer(
174 dvector_view<candidate_record_t> &view)
175 : object_trace(view) {}
176
178 vector_t<candidate_record_t> object_trace;
182 const scalar_type inv_pos{detray::detail::invalid_value<scalar_type>()};
186 vector3_type last_dir = {0., 0., 0.};
187
193 template <typename state_type, concepts::point3D point3_t,
194 concepts::vector3D vector3_t, typename... Args>
195 DETRAY_HOST_DEVICE auto operator()(const state_type &state,
196 const navigation::config & /*unused*/,
197 const point3_t &pos, const vector3_t &dir,
198 const char * /*message*/,
199 Args &&.../*unused*/) {
200 DETRAY_VERBOSE_HOST_DEVICE("Actor: Check navigation status...");
201
202 // Record the candidate of an encountered object
203 if ((is_status(state.status(), navigation_status) || ...)) {
204 DETRAY_VERBOSE_HOST_DEVICE("Actor: ...Status matched:");
205 // Reached a new position: log it
206 // Also log volume switches that happen without position update
207 const bool first_obj{
209 (last_dir == point3_type{0., 0., 0.})};
210
211 if ((state.is_on_portal() && current_vol != state.volume()) ||
212 object_trace.empty() || first_obj ||
213 state.current().surface().identifier() !=
214 object_trace.back().intersection().surface().identifier()) {
215 DETRAY_VERBOSE_HOST_DEVICE("Actor: -> Record surface %d",
216 state.current_surface().index());
217
218 last_pos = algebra::cast_to<double>(pos);
219 last_dir = algebra::cast_to<double>(dir);
220 current_vol = state.volume();
221 object_trace.push_back({pos, dir, state.current()});
222 }
223 }
224 }
225
228 template <typename state_type>
229 DETRAY_HOST_DEVICE auto operator()(const state_type & /*state*/,
230 const char * /*message*/) { /* Do nothing*/
231 }
232
235 DETRAY_HOST_DEVICE
236 constexpr const candidate_record_t &operator[](std::size_t i) const {
237 return object_trace[i];
238 }
239
241 DETRAY_HOST_DEVICE
242 constexpr const auto &trace() const { return object_trace; }
243
248 DETRAY_HOST_DEVICE
249 constexpr bool is_status(const status &nav_stat, const status &ref_stat) {
250 return (nav_stat == ref_stat);
251 }
252};
253
258 using view_type = dvector_view<char>;
260 using const_view_type = dvector_view<const char>;
261
262 struct void_generator {};
263
265 print_inspector() = default;
266
270 : debug_stream(other.debug_stream.str()) {}
271
275
277 ~print_inspector() = default;
278
284 // Reset
285 debug_stream.str(std::string());
286 debug_stream.clear();
287 // Move new debug string in
288 debug_stream << other.debug_stream.str();
289 return *this;
290 }
291
296
305 template <typename state_type, concepts::point3D point3_t,
306 concepts::vector3D vector3_t,
307 typename message_generator_t = void_generator>
308 auto operator()(const state_type &state, const navigation::config &cfg,
309 const point3_t &track_pos, const vector3_t &track_dir,
310 const char *message,
311 const message_generator_t &msg_gen = {}) {
312 std::string msg(message);
313 debug_stream << msg;
314 if constexpr (!std::same_as<message_generator_t, void_generator>) {
315 debug_stream << msg_gen();
316
317 if (state.status() == navigation::status::e_abort) {
318 fata_error_msg = msg_gen();
319 }
320 }
321 debug_stream << std::endl;
322 debug_stream << "----------------------------------------" << std::endl;
323
324 debug_stream << navigation::print_state(state);
325 debug_stream << navigation::print_candidates(state, cfg, track_pos,
326 track_dir);
327
328 debug_stream << std::endl << std::endl;
329 }
330
336 template <typename state_type, typename message_generator_t = void_generator>
337 auto operator()(const state_type &state, const char *message,
338 const message_generator_t &msg_gen = {}) {
339 std::string msg(message);
340 debug_stream << msg;
341 if constexpr (!std::same_as<message_generator_t, void_generator>) {
342 debug_stream << msg_gen();
343
344 if (state.status() == navigation::status::e_abort) {
345 fata_error_msg = msg_gen();
346 }
347 }
348 debug_stream << std::endl;
349 debug_stream << "----------------------------------------" << std::endl;
350
351 debug_stream << navigation::print_state(state);
352
353 debug_stream << std::endl << std::endl;
354 }
355
357 std::string to_string() const { return debug_stream.str(); }
358
360 std::stringstream debug_stream{};
362 std::string fata_error_msg{""};
363};
364
365} // namespace navigation
366
367namespace stepping {
368
373 print_inspector() = default;
374
378 : debug_stream(other.debug_stream.str()) {}
379
385 // Reset
386 debug_stream.str(std::string());
387 debug_stream.clear();
388 // Move new debug string in
389 debug_stream << other.debug_stream.str();
390 return *this;
391 }
392
397
399 std::stringstream debug_stream{};
400
405 template <typename state_type, concepts::scalar scalar_t>
406 void operator()(const state_type &state, const stepping::config & /*unused*/,
407 const char *message, const scalar_t dist) {
408 std::string msg(message);
409
410 debug_stream << msg << std::endl;
411 debug_stream << detray::stepping::print_state(state, dist);
412 }
413
419 template <typename state_type, concepts::scalar scalar_t>
420 void operator()(const state_type &state, const stepping::config & /*unused*/,
421 const char *message, const scalar_t /*dist*/,
422 const std::size_t n_trials, const scalar_t step_scalor) {
423 std::string msg(message);
424
425 debug_stream << msg << std::endl;
426 debug_stream << detray::stepping::print_state(state, n_trials, step_scalor);
427 }
428
430 std::string to_string() const { return debug_stream.str(); }
431};
432
433} // namespace stepping
434
435} // namespace detray
Definition concepts.hpp:77
Definition concepts.hpp:65
Detray include(s).
Definition array_operators.hpp:18
vecmem::vector< value_t > dvector
Vector container backed by vecmem.
Definition containers.hpp:34
unsigned int dindex
Default index type.
Definition indexing.hpp:18
constexpr dindex dindex_invalid
Global invalid index definition.
Definition indexing.hpp:21
dmulti_view< detail::get_view_t< Inspectors >... > view_type
Device view type for the contained inspectors.
Definition inspectors.hpp:49
dmulti_view< detail::get_view_t< const Inspectors >... > const_view_type
Const device view type for the contained inspectors.
Definition inspectors.hpp:51
constexpr auto unroll_views(view_t &view, std::index_sequence< I... >)
Definition inspectors.hpp:139
constexpr decltype(auto) get()
Definition inspectors.hpp:113
auto operator()(state_type &state, const navigation::config &cfg, const point3_t &pos, const vector3_t &dir, const char *message, Args &&...args)
Inspector interface.
Definition inspectors.hpp:78
std::tuple< Inspectors... > inspector_tuple_t
Tuple type storing the inspectors.
Definition inspectors.hpp:54
auto operator()(state_type &state, const char *message, Args &&...args)
Inspector interface.
Definition inspectors.hpp:99
aggregate_inspector(view_t &view)
Construct from the inspector.
Definition inspectors.hpp:63
inspector_tuple_t _inspectors
Inspectors aggregated by this object.
Definition inspectors.hpp:56
constexpr decltype(auto) get() const
Definition inspectors.hpp:119
aggregate_inspector()=default
Default constructor.
Record of a surface intersection along a track.
Definition data_record.hpp:27
dscalar< algebra_type > scalar_type
Scalar type used by this record.
Definition data_record.hpp:35
dpoint3D< algebra_type > point3_type
3D point type used to store positions.
Definition data_record.hpp:37
dvector3D< algebra_type > vector3_type
3D vector type used to store directions.
Definition data_record.hpp:39
Navigation configuration.
Definition navigation_config.hpp:24
constexpr const candidate_record_t & operator[](std::size_t i) const
Definition inspectors.hpp:236
typename candidate_record_t::scalar_type scalar_type
Scalar type used by candidate records.
Definition inspectors.hpp:157
object_tracer()=default
Default constructor.
dindex current_vol
Current volume index.
Definition inspectors.hpp:180
intersection_record< detector_t > candidate_record_t
Record type for an encountered candidate.
Definition inspectors.hpp:155
auto operator()(const state_type &, const char *)
Inspector interface.
Definition inspectors.hpp:229
typename candidate_record_t::point3_type point3_type
3D point type used by candidate records.
Definition inspectors.hpp:159
vector3_type last_dir
Last recorded direction.
Definition inspectors.hpp:186
dvector_view< candidate_record_t > view_type
Mutable device view type for candidate records.
Definition inspectors.hpp:164
vector_t< candidate_record_t > object_trace
Record all objects the navigator encounters.
Definition inspectors.hpp:178
typename candidate_record_t::vector3_type vector3_type
3D vector type used by candidate records.
Definition inspectors.hpp:161
object_tracer(dvector_view< candidate_record_t > &view)
Device-side construction from a vecmem based view type.
Definition inspectors.hpp:173
point3_type last_pos
Last recorded position.
Definition inspectors.hpp:184
const scalar_type inv_pos
Invalid position value used to initialize the trace.
Definition inspectors.hpp:182
dvector_view< const candidate_record_t > const_view_type
Const device view type for candidate records.
Definition inspectors.hpp:166
constexpr bool is_status(const status &nav_stat, const status &ref_stat)
Compares a navigation status with the tracers references.
Definition inspectors.hpp:249
constexpr const auto & trace() const
Definition inspectors.hpp:242
auto operator()(const state_type &state, const navigation::config &, const point3_t &pos, const vector3_t &dir, const char *, Args &&...)
Inspector interface.
Definition inspectors.hpp:195
auto operator()(const state_type &state, const char *message, const message_generator_t &msg_gen={})
Inspector interface.
Definition inspectors.hpp:337
std::stringstream debug_stream
Gathers navigation information across navigator update calls.
Definition inspectors.hpp:360
print_inspector & operator=(print_inspector &&other)=default
Move assignemten operator.
~print_inspector()=default
Default destructor.
print_inspector(const print_inspector &other)
Copy constructor ensures that the string stream is set up identically.
Definition inspectors.hpp:269
auto operator()(const state_type &state, const navigation::config &cfg, const point3_t &track_pos, const vector3_t &track_dir, const char *message, const message_generator_t &msg_gen={})
Inspector interface.
Definition inspectors.hpp:308
dvector_view< char > view_type
Mutable device view type for the debug stream.
Definition inspectors.hpp:258
print_inspector & operator=(const print_inspector &other)
Copy assignemten operator ensures that the string stream is set up identically.
Definition inspectors.hpp:283
std::string fata_error_msg
Special message that is collected if the navigator hits a fatal error.
Definition inspectors.hpp:362
print_inspector()=default
Default constructor.
std::string to_string() const
Definition inspectors.hpp:357
dvector_view< const char > const_view_type
Const device view type for the debug stream.
Definition inspectors.hpp:260
print_inspector(print_inspector &&other)=default
Move constructor.
Configuration flags and limits consumed by a stepper.
Definition stepping_config.hpp:29
std::string to_string() const
Definition inspectors.hpp:430
void operator()(const state_type &state, const stepping::config &, const char *message, const scalar_t dist)
Inspector interface.
Definition inspectors.hpp:406
print_inspector(const print_inspector &other)
Copy constructor ensures that the string stream is set up identically.
Definition inspectors.hpp:377
print_inspector & operator=(const print_inspector &other)
Copy assignemten operator ensures that the string stream is set up identically.
Definition inspectors.hpp:384
void operator()(const state_type &state, const stepping::config &, const char *message, const scalar_t, const std::size_t n_trials, const scalar_t step_scalor)
Inspector interface.
Definition inspectors.hpp:420
print_inspector & operator=(print_inspector &&other)=default
Move assignemten operator.
print_inspector()=default
Default constructor.
std::stringstream debug_stream
Gathers stepping information from inside the stepper methods.
Definition inspectors.hpp:399