Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:27:29

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