Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 07:51:47

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