ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
random_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/random_track_generator_config.hpp"
22
23// System include(s)
24#include <algorithm>
25#include <limits>
26#include <memory>
27#include <random>
28
29namespace detray {
30
44template <typename track_t,
45 typename generator_t =
49 random_track_generator<track_t, generator_t>> {
50 using algebra_t = typename track_t::algebra_type;
51 using scalar_t = dscalar<algebra_t>;
52 using point3_t = dpoint3D<algebra_t>;
53 using vector3_t = dvector3D<algebra_t>;
54
55 public:
57 using track_type = track_t;
58
61
62 private:
64 struct iterator {
65 using difference_type = std::ptrdiff_t;
66 using value_type = track_t;
67 using pointer = track_t*;
68 using reference = track_t&;
69 using iterator_category = detray::ranges::input_iterator_tag;
70
71 constexpr iterator() = delete;
72
77 DETRAY_HOST_DEVICE
78 iterator(std::shared_ptr<generator_t> rand_gen, configuration cfg,
79 std::size_t n_tracks_)
80 : m_rnd_numbers{std::move(rand_gen)}, m_tracks{n_tracks_}, m_cfg{cfg} {}
81
84 DETRAY_HOST_DEVICE
85 constexpr bool operator==(const iterator& rhs) const {
86 return rhs.m_tracks == m_tracks;
87 }
88
91 DETRAY_HOST_DEVICE
92 constexpr auto operator++() -> iterator& {
93 ++m_tracks;
94 return *this;
95 }
96
99 DETRAY_HOST_DEVICE
100 constexpr auto operator++(int) -> iterator& {
101 auto tmp(m_tracks);
102 ++m_tracks;
103 return tmp;
104 }
105
108 DETRAY_HOST_DEVICE
109 track_t operator*() const {
110 if (!m_rnd_numbers) {
111 std::string err_str{"Invalid random number generator"};
112 DETRAY_FATAL_HOST(err_str);
113 throw std::invalid_argument(err_str);
114 }
115
116 const auto& ori = m_cfg.origin();
117 const auto& ori_stddev = m_cfg.origin_stddev();
118
119 const point3_t vtx =
120 m_cfg.do_vertex_smearing()
121 ? point3_t{m_rnd_numbers->normal(ori[0], ori_stddev[0]),
122 m_rnd_numbers->normal(ori[1], ori_stddev[1]),
123 m_rnd_numbers->normal(ori[2], ori_stddev[2])}
124 : point3_t{ori[0], ori[1], ori[2]};
125
126 scalar_t p_mag{(*m_rnd_numbers)(m_cfg.mom_range())};
127 scalar_t phi{(*m_rnd_numbers)(m_cfg.phi_range())};
128 scalar_t theta{(*m_rnd_numbers)(m_cfg.theta_range())};
129 scalar_t sin_theta{math::sin(theta)};
130
131 // Momentum direction from angles
132 vector3_t mom{math::cos(phi) * sin_theta, math::sin(phi) * sin_theta,
133 math::cos(theta)};
134
135 if constexpr (std::is_same_v<track_t, detail::ray<algebra_t>>) {
136 mom = vector::normalize(mom);
137 } else {
138 sin_theta = (sin_theta == scalar_t{0.f})
139 ? std::numeric_limits<scalar_t>::epsilon()
140 : sin_theta;
141 mom = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * p_mag *
142 vector::normalize(mom);
143 }
144
145 // Randomly flip the charge sign
146 darray<double, 2> signs{1., -1.};
147 const auto sign{static_cast<scalar_t>(
148 signs[m_cfg.randomize_charge() ? m_rnd_numbers->coin_toss() : 0u])};
149
150 return track_t{vtx, m_cfg.time(), mom, sign * m_cfg.charge()};
151 }
152
154 std::shared_ptr<generator_t> m_rnd_numbers;
155
157 std::size_t m_tracks{0u};
158
160 configuration m_cfg{};
161 };
162
163 std::shared_ptr<generator_t> m_gen{nullptr};
164 configuration m_cfg{};
165
166 public:
168 using iterator_t = iterator;
169
171 constexpr random_track_generator() = default;
172
175 DETRAY_HOST_DEVICE
176 explicit constexpr random_track_generator(const configuration& cfg)
177 : m_gen{std::make_shared<generator_t>(cfg.seed())}, m_cfg(cfg) {}
178
186 DETRAY_HOST_DEVICE
188 std::size_t n_tracks_,
189 darray<scalar_t, 2> mom_range = {1.f * unit<scalar_t>::GeV,
190 1.f * unit<scalar_t>::GeV},
191 scalar_t charge = -1.f * unit<scalar_t>::e)
192 : m_gen{std::make_shared<generator_t>()} {
193 m_cfg.n_tracks(n_tracks_);
194 m_cfg.mom_range(mom_range);
195 m_cfg.charge(charge);
196 }
197
200 DETRAY_HOST_DEVICE
202 : m_gen(std::move(other.m_gen)), m_cfg(std::move(other.m_cfg)) {}
203
206 DETRAY_HOST_DEVICE
207 constexpr configuration& config() { return m_cfg; }
208
213 DETRAY_HOST_DEVICE
214 auto begin() noexcept -> iterator { return {m_gen, m_cfg, 0u}; }
215
218 DETRAY_HOST_DEVICE
219 auto end() noexcept -> iterator { return {m_gen, m_cfg, m_cfg.n_tracks()}; }
220
223 DETRAY_HOST_DEVICE
224 constexpr auto size() const noexcept -> std::size_t {
225 return m_cfg.n_tracks();
226 }
227};
228
229} // namespace detray
random_track_generator(random_track_generator &&other) noexcept
Move constructor.
Definition random_track_generator.hpp:201
constexpr auto size() const noexcept -> std::size_t
Definition random_track_generator.hpp:224
random_track_generator(std::size_t n_tracks_, darray< scalar_t, 2 > mom_range={1.f *unit< scalar_t >::GeV, 1.f *unit< scalar_t >::GeV}, scalar_t charge=-1.f *unit< scalar_t >::e)
Paramtetrized constructor for quick construction of simple tasks.
Definition random_track_generator.hpp:187
constexpr random_track_generator(const configuration &cfg)
Construct from external configuration.
Definition random_track_generator.hpp:176
iterator iterator_t
Iterator type used to enumerate generated tracks.
Definition random_track_generator.hpp:168
auto end() noexcept -> iterator
Definition random_track_generator.hpp:219
random_track_generator_config< scalar_t > configuration
Configure how tracks are generated.
Definition random_track_generator.hpp:60
auto begin() noexcept -> iterator
Definition random_track_generator.hpp:214
track_t track_type
Track type produced by the generator.
Definition random_track_generator.hpp:57
constexpr configuration & config()
Access the configuration.
Definition random_track_generator.hpp:207
constexpr random_track_generator()=default
Default constructor.
Defines a detray 'view'.
Definition ranges.hpp:190
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
get_point3D_t< A, T > dpoint3D
Three-dimensional point type for algebra A and value type T.
Definition algebra.hpp:72
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
Wrapper for CPU random number generatrion for the random_track_generator.
Definition random_numbers.hpp:26
Configuration for the random track generator.
Definition random_track_generator_config.hpp:30
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