Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:17

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2024 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 http://mozilla.org/MPL/2.0/.
0008 
0009 template <std::size_t DIM>
0010 template <typename... args>
0011 Acts::GridBinFinder<DIM>::GridBinFinder(args&&... vals) {
0012   static_assert(sizeof...(args) == DIM);
0013   static_assert(
0014       std::conjunction<std::disjunction<
0015           std::is_same<int, typename std::decay<args>::type>,
0016           std::is_same<std::pair<int, int>, typename std::decay<args>::type>,
0017           std::is_same<std::vector<std::pair<int, int>>,
0018                        typename std::decay<args>::type>>...>::value);
0019   storeValue(std::forward<args>(vals)...);
0020 }
0021 
0022 template <std::size_t DIM>
0023 template <typename first_value_t, typename... vals>
0024 void Acts::GridBinFinder<DIM>::storeValue(first_value_t&& fv,
0025                                           vals&&... others) {
0026   constexpr std::size_t N = sizeof...(vals);
0027   static_assert(N < DIM);
0028   /// Check the fist value is reasonable
0029   using decayed_value_t = typename std::decay<first_value_t>::type;
0030   if constexpr (std::is_same<int, decayed_value_t>::value) {
0031     /// if int -> value is positive
0032     assert(fv >= 0);
0033     m_values[DIM - N - 1ul] = fv;
0034   } else if constexpr (std::is_same<std::pair<int, int>,
0035                                     decayed_value_t>::value) {
0036     m_values[DIM - N - 1ul] = fv;
0037   } else {
0038     /// If vector of pairs -> also allow for empty vectors
0039     if (!fv.empty()) {
0040       m_values[DIM - N - 1ul] = std::forward<first_value_t>(fv);
0041     } else {
0042       m_values[DIM - N - 1ul] = 1;
0043     }
0044   }
0045   if constexpr (N != 0ul) {
0046     storeValue(std::forward<vals>(others)...);
0047   }
0048 }
0049 
0050 template <std::size_t DIM>
0051 std::array<std::pair<int, int>, DIM> Acts::GridBinFinder<DIM>::getSizePerAxis(
0052     const std::array<std::size_t, DIM>& locPosition) const {
0053   std::array<std::pair<int, int>, DIM> output;
0054   for (std::size_t i(0ul); i < DIM; ++i) {
0055     output[i] = std::visit(
0056         [&locPosition, i](const auto& val) -> std::pair<int, int> {
0057           using value_t = typename std::decay<decltype(val)>::type;
0058           if constexpr (std::is_same<int, value_t>::value) {
0059             assert(val >= 0);
0060             return std::make_pair(-val, val);
0061           } else if constexpr (std::is_same<std::pair<int, int>,
0062                                             value_t>::value) {
0063             return std::make_pair(-val.first, val.second);
0064           } else {
0065             assert(locPosition.size() > i);
0066             assert(locPosition[i] > 0ul);
0067             assert(val.size() >= locPosition[i]);
0068             return val[locPosition[i] - 1ul];
0069           }
0070         },
0071         m_values[i]);
0072   }
0073   return output;
0074 }
0075 
0076 template <std::size_t DIM>
0077 template <typename stored_t, class... Axes>
0078 boost::container::small_vector<std::size_t, Acts::detail::ipow(3, DIM)>
0079 Acts::GridBinFinder<DIM>::findBins(
0080     const std::array<std::size_t, DIM>& locPosition,
0081     const Acts::Grid<stored_t, Axes...>& grid) const {
0082   static_assert(sizeof...(Axes) == DIM);
0083   assert(isGridCompatible(grid));
0084   std::array<std::pair<int, int>, DIM> sizePerAxis =
0085       getSizePerAxis(locPosition);
0086   return grid.neighborHoodIndices(locPosition, sizePerAxis).collect();
0087 }
0088 
0089 template <std::size_t DIM>
0090 template <typename stored_t, class... Axes>
0091 bool Acts::GridBinFinder<DIM>::isGridCompatible(
0092     const Acts::Grid<stored_t, Axes...>& grid) const {
0093   const std::array<std::size_t, DIM> nLocBins = grid.numLocalBins();
0094   for (std::size_t i(0ul); i < DIM; ++i) {
0095     std::size_t nBins = nLocBins[i];
0096     bool isCompabile = std::visit(
0097         [nBins](const auto& val) -> bool {
0098           using value_t = typename std::decay<decltype(val)>::type;
0099           if constexpr (std::is_same<int, value_t>::value ||
0100                         std::is_same<std::pair<int, int>, value_t>::value) {
0101             return true;
0102           } else {
0103             return val.size() == nBins;
0104           }
0105         },
0106         m_values[i]);
0107     if (!isCompabile) {
0108       return false;
0109     }
0110   }
0111   return true;
0112 }