Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Helpers.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Helpers.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2018 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 
12 
13 #include <algorithm>
14 #include <iostream>
15 #include <limits>
16 #include <memory>
17 #include <optional>
18 #include <string>
19 #include <vector>
20 
21 #define ACTS_CHECK_BIT(value, mask) ((value & mask) == mask)
22 
23 namespace Acts {
24 
30 template <typename T>
31 std::vector<T*> unpack_shared_vector(
32  const std::vector<std::shared_ptr<T>>& items) {
33  std::vector<T*> rawPtrs;
34  rawPtrs.reserve(items.size());
35  for (const std::shared_ptr<T>& item : items) {
36  rawPtrs.push_back(item.get());
37  }
38  return rawPtrs;
39 }
40 
46 template <typename T>
47 std::vector<const T*> unpack_shared_vector(
48  const std::vector<std::shared_ptr<const T>>& items) {
49  std::vector<const T*> rawPtrs;
50  rawPtrs.reserve(items.size());
51  for (const std::shared_ptr<const T>& item : items) {
52  rawPtrs.push_back(item.get());
53  }
54  return rawPtrs;
55 }
56 
62 template <typename T>
63 std::vector<const T*> unpack_shared_const_vector(
64  const std::vector<std::shared_ptr<T>>& items) {
65  std::vector<const T*> rawPtrs;
66  rawPtrs.reserve(items.size());
67  for (const std::shared_ptr<T>& item : items) {
68  rawPtrs.push_back(item.get());
69  }
70  return rawPtrs;
71 }
72 
80 template <std::size_t kDIM, typename value_type>
81 std::array<value_type, kDIM> to_array(const std::vector<value_type>& vecvals) {
82  std::array<value_type, kDIM> rarray = {};
83  for (const auto [iv, v] : enumerate(vecvals)) {
84  if (iv < kDIM) {
85  rarray[iv] = v;
86  }
87  }
88  return rarray;
89 }
90 
108 template <template <size_t> class Callable, size_t N, size_t NMAX,
109  typename... Args>
110 auto template_switch(size_t v, Args&&... args) {
111  if (v == N) {
112  return Callable<N>::invoke(std::forward<Args>(args)...);
113  }
114  if (v == 0) {
115  std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
116  << ") is not valid (v == 0 and N != 0)" << std::endl;
117  std::abort();
118  }
119  if constexpr (N < NMAX) {
120  return template_switch<Callable, N + 1, NMAX>(v,
121  std::forward<Args>(args)...);
122  }
123  std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
124  << ") is not valid (v > NMAX)" << std::endl;
125  std::abort();
126 }
127 
135 template <size_t N, size_t NMAX, typename Lambda, typename... Args>
136 auto template_switch_lambda(size_t v, Lambda&& func, Args&&... args) {
137  if (v == N) {
138  return func(std::integral_constant<size_t, N>{},
139  std::forward<Args>(args)...);
140  }
141  if (v == 0) {
142  std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
143  << ") is not valid (v == 0 and N != 0)" << std::endl;
144  std::abort();
145  }
146  if constexpr (N < NMAX) {
147  return template_switch_lambda<N + 1, NMAX>(v, func,
148  std::forward<Args>(args)...);
149  }
150  std::cerr << "template_switch<Fn, " << N << ", " << NMAX << ">(v=" << v
151  << ") is not valid (v > NMAX)" << std::endl;
152  std::abort();
153 }
154 
160 template <typename T, typename U>
162  return std::clamp(value, static_cast<U>(std::numeric_limits<T>::lowest()),
163  static_cast<U>(std::numeric_limits<T>::max()));
164 }
165 
174 template <typename T>
175 std::array<typename T::value_type, 2u> min_max(const T& tseries) {
176  return {*std::min_element(tseries.begin(), tseries.end()),
177  *std::max_element(tseries.begin(), tseries.end())};
178 }
179 
187 template <typename T>
188 std::tuple<typename T::value_type, ActsScalar> range_medium(const T& tseries) {
189  auto [min, max] = min_max(tseries);
190  typename T::value_type range = (max - min);
191  ActsScalar medium = static_cast<ActsScalar>((max + min) * 0.5);
192  return std::tie(range, medium);
193 }
194 
195 } // namespace Acts