Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:12

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // Project include(s)
0012 #include "detray/definitions/algebra.hpp"
0013 #include "detray/definitions/detail/qualifiers.hpp"
0014 
0015 // System include(s)
0016 #include <array>
0017 #include <limits>
0018 #include <random>
0019 
0020 namespace detray::detail {
0021 
0022 /// Wrapper for CPU random number generatrion for the @c random_track_generator
0023 template <concepts::scalar scalar_t,
0024           typename distribution_t = std::uniform_real_distribution<scalar_t>,
0025           typename engine_t = std::mt19937_64>
0026 struct random_numbers {
0027   using distribution_type = distribution_t;
0028   using engine_type = engine_t;
0029   using seed_type = typename engine_t::result_type;
0030 
0031   std::seed_seq m_seeds;
0032   engine_t m_engine;
0033 
0034   /// Default seed
0035   DETRAY_HOST
0036   random_numbers()
0037       : m_seeds{random_numbers::default_seed()}, m_engine{m_seeds} {}
0038 
0039   /// Different seed @param s for every instance
0040   DETRAY_HOST
0041   explicit random_numbers(seed_type s) : m_seeds{s}, m_engine{m_seeds} {}
0042 
0043   /// More entropy in seeds from collection @param s
0044   DETRAY_HOST
0045   explicit random_numbers(const std::vector<seed_type>& s)
0046       : m_seeds{s.begin(), s.end()}, m_engine{m_seeds} {}
0047 
0048   /// Copy constructor
0049   DETRAY_HOST
0050   random_numbers(random_numbers&& other) noexcept
0051       : m_engine(std::move(other.m_engine)) {}
0052 
0053   /// Generate random numbers in a given range
0054   DETRAY_HOST auto operator()(const darray<scalar_t, 2> range = {
0055                                   -std::numeric_limits<scalar_t>::max(),
0056                                   std::numeric_limits<scalar_t>::max()}) {
0057     const scalar_t min{range[0]};
0058     const scalar_t max{range[1]};
0059     assert(min <= max);
0060 
0061     // Uniform
0062     if constexpr (std::is_same_v<distribution_t,
0063                                  std::uniform_real_distribution<scalar_t>>) {
0064       return distribution_t(min, max)(m_engine);
0065 
0066       // Normal
0067     } else if constexpr (std::is_same_v<distribution_t,
0068                                         std::normal_distribution<scalar_t>>) {
0069       scalar_t mu{min + 0.5f * (max - min)};
0070       return distribution_t(mu, 0.5f / 3.0f * (max - min))(m_engine);
0071     }
0072   }
0073 
0074   /// Explicit normal distribution around a @param mean and @param stddev
0075   DETRAY_HOST auto normal(const scalar_t mean, const scalar_t stddev) {
0076     return (stddev == static_cast<scalar_t>(0))
0077                ? mean
0078                : std::normal_distribution<scalar_t>(mean, stddev)(m_engine);
0079   }
0080 
0081   /// 50:50 coin toss
0082   DETRAY_HOST std::uint8_t coin_toss() {
0083     return std::uniform_int_distribution<std::uint8_t>(0u, 1u)(m_engine);
0084   }
0085 
0086   /// Get the default seed of the engine
0087   static constexpr seed_type default_seed() { return engine_t::default_seed; }
0088 };
0089 
0090 }  // namespace detray::detail