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"
21#include "detray/test/common/event_generator/random_track_generator_config.hpp"
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;
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;
71 constexpr iterator() =
delete;
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} {}
85 constexpr bool operator==(
const iterator& rhs)
const {
86 return rhs.m_tracks == m_tracks;
92 constexpr auto operator++() -> iterator& {
100 constexpr auto operator++(
int) -> iterator& {
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);
116 const auto& ori = m_cfg.origin();
117 const auto& ori_stddev = m_cfg.origin_stddev();
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]};
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)};
132 vector3_t mom{math::cos(phi) * sin_theta, math::sin(phi) * sin_theta,
135 if constexpr (std::is_same_v<track_t, detail::ray<algebra_t>>) {
136 mom = vector::normalize(mom);
138 sin_theta = (sin_theta == scalar_t{0.f})
139 ? std::numeric_limits<scalar_t>::epsilon()
141 mom = (m_cfg.is_pT() ? 1.f / sin_theta : 1.f) * p_mag *
142 vector::normalize(mom);
147 const auto sign{
static_cast<scalar_t
>(
148 signs[m_cfg.randomize_charge() ? m_rnd_numbers->coin_toss() : 0u])};
150 return track_t{vtx, m_cfg.time(), mom, sign * m_cfg.charge()};
154 std::shared_ptr<generator_t> m_rnd_numbers;
157 std::size_t m_tracks{0u};
163 std::shared_ptr<generator_t> m_gen{
nullptr};
177 : m_gen{std::make_shared<generator_t>(cfg.seed())}, m_cfg(cfg) {}
188 std::size_t n_tracks_,
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);
202 : m_gen(std::move(other.m_gen)), m_cfg(std::move(other.m_cfg)) {}
214 auto begin() noexcept -> iterator {
return {m_gen, m_cfg, 0u}; }
219 auto end() noexcept -> iterator {
return {m_gen, m_cfg, m_cfg.n_tracks()}; }
224 constexpr auto size() const noexcept -> std::
size_t {
225 return m_cfg.n_tracks();
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