EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PhysicsList.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PhysicsList.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018-2020 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 http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
13 
14 #include <bitset>
15 #include <tuple>
16 
17 namespace ActsFatras {
18 
25 template <typename... processes_t>
26 class PhysicsList {
27  public:
37  template <typename generator_t>
38  bool operator()(generator_t& generator, const Acts::MaterialSlab& slab,
39  Particle& particle, std::vector<Particle>& generated) const {
40  static_assert(
41  (true && ... &&
42  std::is_same_v<bool, decltype(processes_t()(generator, slab, particle,
43  generated))>),
44  "Not all processes conform to the expected interface");
45 
46  return impl(std::index_sequence_for<processes_t...>(), generator, slab,
47  particle, generated);
48  }
49 
51  template <size_t I>
52  std::tuple_element_t<I, std::tuple<processes_t...>>& get() {
53  return std::get<I>(m_processes);
54  }
56  template <typename process_t>
57  process_t& get() {
58  return std::get<process_t>(m_processes);
59  }
60 
62  void disable(std::size_t i) { m_mask.set(i, true); }
66  template <typename process_t>
67  void disable() {
68  return disable(Index<process_t, std::tuple<processes_t...>>::value);
69  }
70 
71  private:
72  // utility struct to retrieve index of the first matching type in the tuple.
73  // from https://stackoverflow.com/a/18063608.
74  template <class T, class Tuple>
75  struct Index;
76  template <class T, class... Types>
77  struct Index<T, std::tuple<T, Types...>> {
78  static constexpr std::size_t value = 0u;
79  };
80  template <class T, class U, class... Types>
81  struct Index<T, std::tuple<U, Types...>> {
82  static constexpr std::size_t value =
83  1u + Index<T, std::tuple<Types...>>::value;
84  };
85 
86  std::bitset<sizeof...(processes_t)> m_mask;
87  std::tuple<processes_t...> m_processes;
88 
89  // compile-time index-based recursive function call for all processes
90  template <typename generator_t>
91  bool impl(std::index_sequence<>, generator_t&, const Acts::MaterialSlab&,
92  Particle&, std::vector<Particle>&) const {
93  return false;
94  }
95  template <std::size_t I0, std::size_t... INs, typename generator_t>
96  bool impl(std::index_sequence<I0, INs...>, generator_t& generator,
97  const Acts::MaterialSlab& slab, Particle& particle,
98  std::vector<Particle>& generated) const {
99  // only call process if it is not masked
100  if (not m_mask[I0] and
101  std::get<I0>(m_processes)(generator, slab, particle, generated)) {
102  // exit early in case the process signals an abort
103  return true;
104  } else {
105  return impl(std::index_sequence<INs...>(), generator, slab, particle,
106  generated);
107  }
108  }
109 };
110 
111 } // namespace ActsFatras