Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:42

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 #pragma once
0010 
0011 #include "Acts/Plugins/Json/ActsJson.hpp"
0012 #include "Acts/Utilities/GridAccessHelpers.hpp"
0013 #include "Acts/Utilities/IAxis.hpp"
0014 #include "Acts/Utilities/detail/AxisFwd.hpp"
0015 
0016 #include <iostream>
0017 
0018 // Custom Json encoder/decoders.
0019 namespace Acts {
0020 
0021 namespace detail {
0022 
0023 /// @cond
0024 NLOHMANN_JSON_SERIALIZE_ENUM(Acts::detail::AxisBoundaryType,
0025                              {{Acts::detail::AxisBoundaryType::Bound, "Bound"},
0026                               {Acts::detail::AxisBoundaryType::Open, "Open"},
0027                               {Acts::detail::AxisBoundaryType::Closed,
0028                                "Closed"}})
0029 
0030 NLOHMANN_JSON_SERIALIZE_ENUM(Acts::detail::AxisType,
0031                              {{Acts::detail::AxisType::Equidistant,
0032                                "Equidistant"},
0033                               {Acts::detail::AxisType::Variable, "Variable"}})
0034 
0035 /// @endcond
0036 
0037 }  // namespace detail
0038 
0039 namespace AxisJsonConverter {
0040 
0041 /// Convert an axis to json
0042 //
0043 /// @param ia the axis
0044 ///
0045 /// @return a json object to represent the axis
0046 nlohmann::json toJson(const IAxis& ia);
0047 
0048 /// Convert an axis to json - detray style
0049 ///
0050 /// @param ia the axis
0051 ///
0052 /// @return a json object to represent the axis in detray json
0053 nlohmann::json toJsonDetray(const IAxis& ia);
0054 
0055 }  // namespace AxisJsonConverter
0056 
0057 namespace GridAccessJsonConverter {
0058 
0059 /// Convert a global to local access to json
0060 ///
0061 /// @param globalToGridLocal the global to grid local access
0062 ///
0063 /// @return a json object to represent global class
0064 nlohmann::json toJson(const GridAccess::IGlobalToGridLocal& globalToGridLocal);
0065 
0066 /// Create a global grid to local instance
0067 ///
0068 /// @param jGlobalToGridLocal the json snippet
0069 ///
0070 /// @return a newly created object
0071 std::unique_ptr<const GridAccess::IGlobalToGridLocal> globalToGridLocalFromJson(
0072     const nlohmann::json& jGlobalToGridLocal);
0073 
0074 /// Create the delegate directly
0075 ///
0076 /// @param jGlobalToGridLocal the json snippet
0077 ///
0078 /// This is the usual workflow, as the connect method can be called on
0079 /// the concreate type
0080 ///
0081 /// @note the dimension of the delegate has to be known by peeking
0082 /// into the json object
0083 GridAccess::GlobalToGridLocal1DimDelegate globalToGridLocal1DimDelegateFromJson(
0084     const nlohmann::json& jGlobalToGridLocal);
0085 
0086 /// Create the delegate directly
0087 ///
0088 /// @param jGlobalToGridLocal the json snippet
0089 ///
0090 /// This is the usual workflow, as the connect method can be called on
0091 /// the concreate type
0092 ///
0093 /// @note the dimension of the delegate has to be known by peeking
0094 /// into the json object
0095 GridAccess::GlobalToGridLocal2DimDelegate globalToGridLocal2DimDelegateFromJson(
0096     const nlohmann::json& jGlobalToGridLocal);
0097 
0098 /// Convert a local to local access to json
0099 ///
0100 /// @param boundToGridLocal the local to local access
0101 ///
0102 /// @return a json object to represent local class
0103 nlohmann::json toJson(const GridAccess::IBoundToGridLocal& boundToGridLocal);
0104 
0105 /// Create a local grid to local instance
0106 ///
0107 /// @param jBoundToGridLocal the json snippet
0108 ///
0109 /// @return a newly created object
0110 std::unique_ptr<GridAccess::IBoundToGridLocal> boundToGridLocalFromJson(
0111     const nlohmann::json& jBoundToGridLocal);
0112 
0113 /// Create the delegate directly
0114 ///
0115 /// @param jBoundToGridLocal the json snippe
0116 ///
0117 /// This is the usual workflow, as the connect method can be called on
0118 /// the concreate type
0119 ///
0120 /// @note the dimension of the delegate has to be known by peeking
0121 /// into the json object
0122 GridAccess::BoundToGridLocal1DimDelegate boundToGridLocal1DimDelegateFromJson(
0123     const nlohmann::json& jBoundToGridLocal);
0124 
0125 /// Create the delegate directly
0126 ///
0127 /// @param jBoundToGridLocal the json snippe
0128 ///
0129 /// This is the usual workflow, as the connect method can be called on
0130 /// the concreate type
0131 ///
0132 /// @note the dimension of the delegate has to be known by peeking
0133 /// into the json object
0134 GridAccess::BoundToGridLocal2DimDelegate boundToGridLocal2DimDelegateFromJson(
0135     const nlohmann::json& jBoundToGridLocal);
0136 
0137 }  // namespace GridAccessJsonConverter
0138 
0139 namespace GridJsonConverter {
0140 
0141 /// @brief Templated grid conversion to json
0142 ///
0143 /// @tparam grid_type the type of the grid
0144 /// @param grid the grid object
0145 ///
0146 /// @return a json object to represent the grid
0147 template <typename grid_type>
0148 nlohmann::json toJson(const grid_type& grid) {
0149   nlohmann::json jGrid;
0150 
0151   auto axes = grid.axes();
0152   nlohmann::json jAxes;
0153   for (unsigned int ia = 0u; ia < grid_type::DIM; ++ia) {
0154     auto jAxis = AxisJsonConverter::toJson(*axes[ia]);
0155     jAxes.push_back(jAxis);
0156   }
0157   jGrid["axes"] = jAxes;
0158 
0159   nlohmann::json jData;
0160   // 1D connections
0161   if constexpr (grid_type::DIM == 1u) {
0162     for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0163       typename grid_type::index_t lbin;
0164       lbin[0u] = ib0;
0165       jData.push_back(std::tie(lbin, grid.atLocalBins(lbin)));
0166     }
0167   }
0168   // 2D connections
0169   if constexpr (grid_type::DIM == 2u) {
0170     for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0171       for (unsigned int ib1 = 1u; ib1 <= axes[1u]->getNBins(); ++ib1) {
0172         typename grid_type::index_t lbin;
0173         lbin[0u] = ib0;
0174         lbin[1u] = ib1;
0175         jData.push_back(std::tie(lbin, grid.atLocalBins(lbin)));
0176       }
0177     }
0178   }
0179   jGrid["data"] = jData;
0180 
0181   return jGrid;
0182 }
0183 
0184 /// @brief Templated grid conversion to json
0185 ///
0186 /// @tparam grid_type the type of the grid
0187 /// @param grid the grid object
0188 /// @param swapAxis - this is needed for detray
0189 ///
0190 /// @note detray has a different offset for the
0191 /// local indices, it starts at 0
0192 ///
0193 /// @return a json object to represent the grid
0194 template <typename grid_type>
0195 nlohmann::json toJsonDetray(const grid_type& grid, bool swapAxis = false) {
0196   nlohmann::json jGrid;
0197   // Get the grid axes & potentially swap them
0198   std::array<const Acts::IAxis*, grid_type::DIM> axes = grid.axes();
0199   if (swapAxis && grid_type::DIM == 2u) {
0200     std::swap(axes[0u], axes[1u]);
0201   }
0202 
0203   nlohmann::json jAxes;
0204 
0205   // Fill the axes in the order they are
0206   for (unsigned int ia = 0u; ia < grid_type::DIM; ++ia) {
0207     auto jAxis = AxisJsonConverter::toJsonDetray(*axes[ia]);
0208     jAxis["label"] = ia;
0209     jAxes.push_back(jAxis);
0210   }
0211   jGrid["axes"] = jAxes;
0212 
0213   nlohmann::json jData;
0214   // 1D connections
0215   if constexpr (grid_type::DIM == 1u) {
0216     for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0217       // Lookup bin
0218       typename grid_type::index_t lbin;
0219       lbin[0u] = ib0;
0220       nlohmann::json jBin;
0221       jBin["content"] = grid.atLocalBins(lbin);
0222       // Corrected bin for detray
0223       lbin[0u] = ib0 - 1u;
0224       jBin["loc_index"] = lbin;
0225       jData.push_back(jBin);
0226     }
0227   }
0228 
0229   // 2D connections
0230   if constexpr (grid_type::DIM == 2u) {
0231     for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0232       for (unsigned int ib1 = 1u; ib1 <= axes[1u]->getNBins(); ++ib1) {
0233         typename grid_type::index_t lbin;
0234         // Lookup bin - respect swap (if it happened) for the lookup
0235         lbin[0u] = swapAxis ? ib1 : ib0;
0236         lbin[1u] = swapAxis ? ib0 : ib1;
0237         nlohmann::json jBin;
0238         jBin["content"] = grid.atLocalBins(lbin);
0239         // Corrected bin for detray
0240         lbin[0u] = ib0 - 1u;
0241         lbin[1u] = ib1 - 1u;
0242         jBin["loc_index"] = lbin;
0243         jData.push_back(jBin);
0244       }
0245     }
0246   }
0247   jGrid["bins"] = jData;
0248 
0249   return jGrid;
0250 }
0251 
0252 /// @brief Templated grid conversion from json
0253 ///
0254 /// @tparam axis_generator_type the type of the axis generator
0255 ///         which determines the grid type
0256 ///
0257 /// @param jGrid the json object to represent the grid
0258 /// @param aGenerator the axis generator
0259 ///
0260 /// @note the axis generator also defines the grid dimension
0261 ///
0262 /// @return a grid object
0263 template <typename axis_generator_type,
0264           typename value_type = std::vector<std::size_t>>
0265 auto fromJson(const nlohmann::json& jGrid,
0266               const axis_generator_type& aGenerator) {
0267   // Generate the grid
0268   using GridType = typename axis_generator_type::template grid_type<value_type>;
0269   GridType grid(aGenerator());
0270   nlohmann::json jData = jGrid["data"];
0271   // Index filling
0272   if constexpr (GridType::DIM == 1u) {
0273     for (const auto& jd : jData) {
0274       std::array<std::size_t, 1u> lbin = jd[0u];
0275       value_type values = jd[1u];
0276       grid.atLocalBins(lbin) = values;
0277     }
0278   }
0279   if constexpr (GridType::DIM == 2u) {
0280     for (const auto& jd : jData) {
0281       std::array<std::size_t, 2u> lbin = jd[0u];
0282       value_type values = jd[1u];
0283       grid.atLocalBins(lbin) = values;
0284     }
0285   }
0286   return grid;
0287 }
0288 
0289 }  // namespace GridJsonConverter
0290 }  // namespace Acts