ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
uniform_track_generator.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/tracks/ray.hpp"
17#include "detray/utils/logging.hpp"
18#include "detray/utils/ranges/ranges.hpp"
19
20// Detray test include(s)
21#include "detray/test/common/event_generator/uniform_track_generator_config.hpp"
22
23// System include(s)
24#include <algorithm>
25#include <limits>
26#include <memory>
27
28namespace detray {
29
40template <typename track_t,
41 typename generator_t =
45 uniform_track_generator<track_t, generator_t>> {
46 using algebra_t = typename track_t::algebra_type;
47 using scalar_t = dscalar<algebra_t>;
48 using vector3_t = dvector3D<algebra_t>;
49
50 public:
52 using track_type = track_t;
53
56
57 private:
59 struct iterator {
60 using difference_type = std::ptrdiff_t;
61 using value_type = track_t;
62 using pointer = track_t*;
63 using reference = track_t&;
64 using iterator_category = detray::ranges::input_iterator_tag;
65
66 constexpr iterator() = default;
67
73 DETRAY_HOST_DEVICE
74 constexpr iterator(std::shared_ptr<generator_t> rand_gen, configuration cfg,
75 std::size_t iph = 1u, std::size_t ith = 0u)
76 : m_rnd_numbers{std::move(rand_gen)},
77 m_cfg{cfg},
78 m_phi_step_size{(cfg.phi_range()[1] - cfg.phi_range()[0]) /
79 static_cast<scalar_t>(cfg.phi_steps())},
80 m_theta_step_size{(cfg.theta_range()[1] - cfg.theta_range()[0]) /
81 static_cast<scalar_t>(cfg.theta_steps() - 1u)},
82 m_eta_step_size{(cfg.eta_range()[1] - cfg.eta_range()[0]) /
83 static_cast<scalar_t>(cfg.eta_steps() - 1u)},
84 m_phi{cfg.phi_range()[0]},
85 m_theta{cfg.uniform_eta() ? get_theta(cfg.eta_range()[0])
86 : cfg.theta_range()[0]},
87 i_phi{iph},
88 i_theta{ith} {}
89
92 DETRAY_HOST_DEVICE
93 constexpr bool operator==(const iterator& rhs) const {
94 return rhs.i_phi == i_phi && rhs.i_theta == i_theta;
95 }
96
101 DETRAY_HOST_DEVICE
102 constexpr auto operator++() -> iterator& {
103 // Check theta range according to step size
104 if (i_theta < m_cfg.theta_steps()) {
105 // Check phi sub-range
106 if (i_phi < m_cfg.phi_steps()) {
107 // Calculate new phi in the given range
108 m_phi = m_cfg.phi_range()[0] +
109 static_cast<scalar_t>(i_phi) * m_phi_step_size;
110 ++i_phi;
111 return *this;
112 }
113 // Reset phi range
114 i_phi = 1;
115 m_phi = m_cfg.phi_range()[0];
116
117 // Calculate new theta in the given range
118 ++i_theta;
119
120 if (m_cfg.uniform_eta()) {
121 const scalar_t eta = m_cfg.eta_range()[0] +
122 static_cast<scalar_t>(i_theta) * m_eta_step_size;
123 m_theta = get_theta(eta);
124 } else {
125 m_theta = m_cfg.theta_range()[0] +
126 static_cast<scalar_t>(i_theta) * m_theta_step_size;
127 }
128 }
129 return *this;
130 }
131
134 DETRAY_HOST_DEVICE
135 constexpr auto operator++(int) -> iterator& {
136 auto tmp(*this);
137 ++(*this);
138 return tmp;
139 }
140
143 DETRAY_HOST_DEVICE
144 track_t operator*() const {
145 if (!m_rnd_numbers) {
146 std::string err_str{"Invalid random number generator"};
147 DETRAY_FATAL_HOST(err_str);
148 throw std::invalid_argument(err_str);
149 }
150
151 scalar_t sin_theta{math::sin(m_theta)};
152
153 // Momentum direction from angles
154 vector3_t p{math::cos(m_phi) * sin_theta, math::sin(m_phi) * sin_theta,
155 math::cos(m_theta)};
156
157 // Magnitude of momentum
158 if constexpr (std::is_same_v<track_t, detail::ray<algebra_t>>) {
159 p = vector::normalize(p);
160 } else {
161 sin_theta = (sin_theta == scalar_t{0.f})
162 ? std::numeric_limits<scalar_t>::epsilon()
163 : sin_theta;
164 p = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * m_cfg.m_p_mag *
165 vector::normalize(p);
166 }
167
168 const auto& ori = m_cfg.origin();
169
170 // Randomly flip the charge sign
171 darray<double, 2> signs{1., -1.};
172 const auto sign{static_cast<scalar_t>(
173 signs[m_cfg.randomize_charge() ? m_rnd_numbers->coin_toss() : 0u])};
174
175 return track_t{
176 {ori[0], ori[1], ori[2]}, m_cfg.time(), p, sign * m_cfg.charge()};
177 }
178
180 std::shared_ptr<generator_t> m_rnd_numbers;
181
183 configuration m_cfg{};
184
186 scalar_t m_phi_step_size{0.f};
187 scalar_t m_theta_step_size{0.f};
188 scalar_t m_eta_step_size{0.f};
189
191 scalar_t m_phi{-constant<scalar_t>::pi};
192 scalar_t m_theta{0.f};
193
195 std::size_t i_phi{0u};
196 std::size_t i_theta{0u};
197
198 private:
202 DETRAY_HOST_DEVICE
203 scalar_t get_theta(const scalar_t eta) {
204 return 2.f * math::atan(math::exp(-eta));
205 }
206 };
207
208 std::shared_ptr<generator_t> m_gen{
209 std::make_shared<generator_t>(configuration{}.seed())};
210 configuration m_cfg{};
211
212 public:
214 using iterator_t = iterator;
215
217 constexpr uniform_track_generator() = default;
218
221 DETRAY_HOST_DEVICE
223 : m_gen{std::make_shared<generator_t>(cfg.seed())}, m_cfg{cfg} {}
224
234 DETRAY_HOST_DEVICE
235 uniform_track_generator(std::size_t n_phi, std::size_t n_theta,
236 scalar_t p_mag = 1.f * unit<scalar_t>::GeV,
237 bool uniform_eta = false,
238 scalar_t charge = -1.f * unit<scalar_t>::e)
239 : m_gen{std::make_shared<generator_t>()} {
240 m_cfg.phi_steps(n_phi).theta_steps(n_theta);
241 m_cfg.uniform_eta(uniform_eta);
242 m_cfg.p_tot(p_mag);
243 m_cfg.charge(charge);
244 }
245
248 DETRAY_HOST_DEVICE
250 : m_gen(std::move(other.m_gen)), m_cfg(std::move(other.m_cfg)) {}
251
255 DETRAY_HOST_DEVICE
257 const uniform_track_generator& other) {
258 m_cfg = other.m_cfg;
259 m_gen = other.m_gen;
260 return *this;
261 }
262
265 DETRAY_HOST_DEVICE
266 constexpr configuration& config() { return m_cfg; }
267
271 DETRAY_HOST_DEVICE
272 constexpr auto begin() noexcept -> iterator { return {m_gen, m_cfg, 1u, 0u}; }
273
276 DETRAY_HOST_DEVICE
277 constexpr auto end() noexcept -> iterator {
278 return {m_gen, m_cfg, 1u, m_cfg.theta_steps()};
279 }
280
283 DETRAY_HOST_DEVICE
284 constexpr auto size() const noexcept -> std::size_t {
285 return m_cfg.phi_steps() * m_cfg.theta_steps();
286 }
287};
288
289} // namespace detray
Defines a detray 'view'.
Definition ranges.hpp:190
constexpr auto size() const noexcept -> std::size_t
Definition uniform_track_generator.hpp:284
constexpr configuration & config()
Access the configuration.
Definition uniform_track_generator.hpp:266
constexpr uniform_track_generator()=default
Default constructor.
uniform_track_generator(std::size_t n_phi, std::size_t n_theta, scalar_t p_mag=1.f *unit< scalar_t >::GeV, bool uniform_eta=false, scalar_t charge=-1.f *unit< scalar_t >::e)
Paramtetrized constructor for quick construction of simple tasks.
Definition uniform_track_generator.hpp:235
constexpr auto begin() noexcept -> iterator
Definition uniform_track_generator.hpp:272
uniform_track_generator(uniform_track_generator &&other) noexcept
Move constructor.
Definition uniform_track_generator.hpp:249
uniform_track_generator_config< scalar_t > configuration
Configure how tracks are generated.
Definition uniform_track_generator.hpp:55
track_t track_type
Track type produced by the generator.
Definition uniform_track_generator.hpp:52
constexpr uniform_track_generator(configuration cfg)
Construct from external configuration cfg.
Definition uniform_track_generator.hpp:222
constexpr auto end() noexcept -> iterator
Definition uniform_track_generator.hpp:277
constexpr uniform_track_generator & operator=(const uniform_track_generator &other)
Copy assignment operator.
Definition uniform_track_generator.hpp:256
iterator iterator_t
Iterator type used to enumerate generated tracks.
Definition uniform_track_generator.hpp:214
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
constexpr array_t< scalar1_t, 2 > operator*(const array_t< scalar1_t, 2 > &a, scalar2_t s)
Multiply an array-backed vector by a scalar.
Definition array_operators.hpp:26
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
constexpr bool operator==(const Fastor::Tensor< S, N > &lhs, const Fastor::Tensor< S, N > &rhs)
Compare two Fastor vectors component-wise for exact equality.
Definition fastor.hpp:35
static constexpr scalar_t pi
π
Definition units.hpp:136
Wrapper for CPU random number generatrion for the random_track_generator.
Definition random_numbers.hpp:26
Configuration for the uniform track generator.
Definition uniform_track_generator_config.hpp:28
uniform_track_generator_config & uniform_eta(bool b)
Definition uniform_track_generator_config.hpp:173
uniform_track_generator_config & eta_range(scalar_t low, scalar_t high)
Definition uniform_track_generator_config.hpp:126
uniform_track_generator_config & theta_steps(std::size_t n)
Definition uniform_track_generator_config.hpp:156
uniform_track_generator_config & phi_steps(std::size_t n)
Definition uniform_track_generator_config.hpp:149
uniform_track_generator_config & theta_range(scalar_t low, scalar_t high)
Definition uniform_track_generator_config.hpp:104
uniform_track_generator_config & phi_range(scalar_t low, scalar_t high)
Definition uniform_track_generator_config.hpp:83
uniform_track_generator_config & eta_steps(std::size_t n)
Definition uniform_track_generator_config.hpp:165
static constexpr scalar_t GeV
Native energy unit, the gigaelectronvolt.
Definition units.hpp:81
static constexpr scalar_t e
Charge, native unit e (elementary charge).
Definition units.hpp:105