Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:06:59

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