ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
helix_cylinder_intersector.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/definitions/algebra.hpp"
13#include "detray/definitions/detail/qualifiers.hpp"
14#include "detray/definitions/math.hpp"
15#include "detray/definitions/units.hpp"
16#include "detray/geometry/coordinates/concentric_cylindrical2D.hpp"
17#include "detray/geometry/coordinates/cylindrical2D.hpp"
18#include "detray/geometry/shapes/cylinder2D.hpp"
19#include "detray/navigation/intersection/intersection.hpp"
20#include "detray/navigation/intersection/ray_cylinder_intersector.hpp"
21#include "detray/tracks/helix.hpp"
22#include "detray/utils/invalid_values.hpp"
23#include "detray/utils/root_finding.hpp"
24
25namespace detray {
26
27template <typename frame_t, concepts::algebra algebra_t>
29
36template <detray::concepts::aos algebra_t>
37struct helix_intersector_impl<cylindrical2D<algebra_t>, algebra_t>
38 : public ray_intersector_impl<cylindrical2D<algebra_t>, algebra_t,
39 intersection::contains_pos> {
40 private:
41 using scalar_t = dscalar<algebra_t>;
42
43 public:
45 using algebra_type = algebra_t;
46
48 template <typename surface_descr_t>
51
53 template <typename other_algebra_t>
55
56 // Maximum number of solutions this intersector can produce
58 static constexpr std::uint8_t n_solutions{2u};
59
62
71 template <typename mask_t>
72 DETRAY_HOST_DEVICE constexpr result_type point_of_intersection(
74 const mask_t &mask, const scalar_t /*unused*/ = 0.f) const {
75 using point3_t = dpoint3D<algebra_t>;
76 using vector3_t = dvector3D<algebra_t>;
77
78 result_type ret{};
79
80 // Cylinder z axis
81 const vector3_t sz = trf.z();
82 // Cylinder centre
83 const point3_t sc = trf.translation();
84
85 // Starting point on the helix for the Newton iteration
86 // The mask is a cylinder -> it provides its radius as the first
87 // value
88 const scalar_t r{mask[cylinder2D::e_r]};
89
90 // Try to guess the best starting positions for the iteration
91
92 // Direction of the track at the helix origin
93 const auto h_dir = h.dir(0.5f * r);
94 // Default starting path length for the Newton iteration (assumes
95 // concentric cylinder)
96 const scalar_t default_s{r * vector::perp(h_dir)};
97
98 // Initial helix path length parameter
99 darray<scalar_t, 2> paths{default_s, default_s};
100
101 // try to guess good starting path by calculating the intersection
102 // path of the helix tangential with the cylinder. This only has a
103 // chance of working for tracks with reasonably high p_T !
104 detail::ray<algebra_t> t{h.pos(), h.time(), h_dir, h.qop()};
105 const auto qe = this->solve_intersection(t, mask, trf);
106
107 // Obtain both possible solutions by looping over the (different)
108 // starting positions
109 auto n_runs{static_cast<unsigned int>(qe.solutions())};
110
111 // Note: the default path length might be smaller than either
112 // solution
113 switch (qe.solutions()) {
114 case 2:
115 paths[1] = qe.larger();
116 // If there are two solutions, reuse the case for a single
117 // solution to setup the intersection with the smaller path
118 // in ret[0]
119 [[fallthrough]];
120 case 1: {
121 paths[0] = qe.smaller();
122 break;
123 }
124 default: {
125 n_runs = 2u;
126 paths[0] = r;
127 paths[1] = -r;
128 }
129 }
130
132 auto cyl_inters_func = [&h, &r, &sz, &sc](const scalar_t x) {
133 const vector3_t crp = vector::cross(h.pos(x) - sc, sz);
134
135 // f(s) = ((h.pos(s) - sc) x sz)^2 - r^2 == 0
136 const scalar_t f_s{(vector::dot(crp, crp) - r * r)};
137 // f'(s) = 2 * ( (h.pos(s) - sc) x sz) * (h.dir(s) x sz) )
138 const scalar_t df_s{2.f * vector::dot(crp, vector::cross(h.dir(x), sz))};
139
140 return std::make_tuple(f_s, df_s);
141 };
142
143 for (unsigned int i = 0u; i < n_runs; ++i) {
144 const scalar_t &s_ini = paths[i];
145
146 // Run the root finding algorithm
147 scalar_t s;
148 scalar_t ds;
149 if (run_rtsafe) {
150 std::tie(s, ds) =
151 newton_raphson_safe(cyl_inters_func, s_ini, convergence_tolerance,
153 } else {
154 std::tie(s, ds) =
155 newton_raphson(cyl_inters_func, s_ini, convergence_tolerance,
157 }
158
159 ret[i] = {s, h.pos(s), ds};
160 }
161
162 return ret;
163 }
164
168 std::size_t max_n_tries{1000u};
172 bool run_rtsafe{true};
173};
174
176template <concepts::algebra algebra_t>
178 : public helix_intersector_impl<cylindrical2D<algebra_t>, algebra_t> {
180 using algebra_type = algebra_t;
181
183 template <typename surface_descr_t>
186
188 template <typename other_algebra_t>
190
192 static constexpr std::uint8_t n_solutions{1u};
193
196
205 template <typename mask_t>
206 DETRAY_HOST_DEVICE constexpr result_type point_of_intersection(
208 const mask_t &mask, const dscalar<algebra_t> /*unused*/ = 0.f) const {
209 using base_t = helix_intersector_impl<cylindrical2D<algebra_t>, algebra_t>;
210
211 // Array of two solutions
212 typename base_t::result_type results =
213 base_t::point_of_intersection(h, trf, mask, 0.f);
214
215 // For portals, only take the solution along the helix direction,
216 // not the one in the opposite direction
217 return math::signbit(results[0].path) ? results[1] : results[0];
218 }
219};
220
221} // namespace detray
describes a helical trajectory in a given B-field.
Definition helix.hpp:33
describes a straight-line trajectory
Definition ray.hpp:22
This class holds the intersection information.
Definition intersection.hpp:212
Mask a region on a surface and link it to a volume.
Definition mask.hpp:46
Detray include(s).
Definition array_operators.hpp:18
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
std::pair< scalar_t, scalar_t > newton_raphson_safe(function_t &evaluate_func, scalar_t s, const scalar_t convergence_tolerance=1.f *unit< scalar_t >::um, const std::size_t max_n_tries=1000u, const scalar_t max_path=5.f *unit< scalar_t >::m)
Find a root using the Newton-Raphson and Bisection algorithms.
Definition root_finding.hpp:193
std::array< value_t, kDIM > darray
Fixed-size array container.
Definition containers.hpp:30
get_vector3D_t< A, T > dvector3D
Three-dimensional vector type for algebra A and value type T.
Definition algebra.hpp:80
get_transform3D_t< A, T > dtransform3D
Three-dimensional transform type for algebra A and value type T.
Definition algebra.hpp:84
std::pair< scalar_t, scalar_t > newton_raphson(function_t &evaluate_func, scalar_t s, const scalar_t convergence_tolerance=1.f *unit< scalar_t >::um, const std::size_t max_n_tries=1000u, const scalar_t max_path=5.f *unit< scalar_t >::m)
Find a root using the Newton-Raphson algorithm.
Definition root_finding.hpp:121
get_point3D_t< A, T > dpoint3D
Three-dimensional point type for algebra A and value type T.
Definition algebra.hpp:72
Projection into a 2D concentric cylindrical frame (No rotation in coordinate transformation).
Definition concentric_cylindrical2D.hpp:21
Projection into a 2D cylindrical coordinate frame.
Definition cylindrical2D.hpp:20
algebra_t algebra_type
Algebra policy used by this intersector.
Definition helix_cylinder_intersector.hpp:180
intersection2D< surface_descr_t, algebra_t, intersection::contains_pos > intersection_type
Intersection result type associated with a surface descriptor.
Definition helix_cylinder_intersector.hpp:184
intersection_point_err< algebra_t > result_type
Result carrying the intersection point and path uncertainty.
Definition helix_cylinder_intersector.hpp:195
static constexpr std::uint8_t n_solutions
Maximum number of solutions this intersector can produce.
Definition helix_cylinder_intersector.hpp:192
detail::helix< other_algebra_t > trajectory_type
Helix trajectory type accepted by this intersector.
Definition helix_cylinder_intersector.hpp:189
constexpr result_type point_of_intersection(const trajectory_type< algebra_t > &h, const dtransform3D< algebra_t > &trf, const mask_t &mask, const dscalar< algebra_t >=0.f) const
Operator function to find intersections between helix and a concentric cylinder surface.
Definition helix_cylinder_intersector.hpp:206
static constexpr std::uint8_t n_solutions
Maximum number of solutions produced by this intersector.
Definition helix_cylinder_intersector.hpp:58
detail::helix< other_algebra_t > trajectory_type
Trajectory representation accepted by this intersector.
Definition helix_cylinder_intersector.hpp:54
scalar_t convergence_tolerance
Tolerance for convergence.
Definition helix_cylinder_intersector.hpp:166
darray< intersection_point_err< algebra_t >, n_solutions > result_type
Result type returned by the geometric intersection calculation.
Definition helix_cylinder_intersector.hpp:61
bool run_rtsafe
Complement the Newton algorithm with Bisection steps.
Definition helix_cylinder_intersector.hpp:172
intersection2D< surface_descr_t, algebra_t, intersection::contains_pos > intersection_type
Intersection result type associated with a surface descriptor.
Definition helix_cylinder_intersector.hpp:49
scalar_t max_path
Early exit, if the intersection is too far away.
Definition helix_cylinder_intersector.hpp:170
constexpr result_type point_of_intersection(const trajectory_type< algebra_t > &h, const dtransform3D< algebra_t > &trf, const mask_t &mask, const scalar_t=0.f) const
Operator function to find intersections between a helix and a cylinder surface.
Definition helix_cylinder_intersector.hpp:72
algebra_t algebra_type
Algebra policy used by this intersector.
Definition helix_cylinder_intersector.hpp:45
std::size_t max_n_tries
Guard against infinite loops.
Definition helix_cylinder_intersector.hpp:168
Intersection implementation for detector surfaces using a helix trajectory.
Definition helix_intersector.hpp:29
Result of intersector: Distance, intersection point (2D or 3D, local/global) and error estimate on di...
Definition intersection.hpp:150
Intersection implementation for detector surfaces using a ray trajectory.
Definition ray_intersector.hpp:31
static constexpr scalar_t um
Length, native unit mm.
Definition units.hpp:25
static constexpr scalar_t m
Metre expressed in millimetres.
Definition units.hpp:31