Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-11 09:40:22

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/AxisDefinitions.hpp"
0012 #include "Acts/Utilities/Grid.hpp"
0013 #include "Acts/Utilities/GridAxisGenerators.hpp"
0014 #include "ActsPlugins/Json/AlgebraJsonConverter.hpp"
0015 #include "ActsPlugins/Json/GridJsonConverter.hpp"
0016 #include "ActsPlugins/Json/UtilitiesJsonConverter.hpp"
0017 
0018 #include <tuple>
0019 
0020 namespace Acts {
0021 using namespace GridAxisGenerators;
0022 
0023 namespace IndexedGridJsonHelper {
0024 
0025 /// @brief The actual conversion method
0026 ///
0027 /// @param indexGrid is the index grid to be written
0028 /// @param detray is a flag indicating detray writeout
0029 /// @param checkSwap is a flag indicating if the axes should be swapped
0030 template <typename index_grid>
0031 nlohmann::json convertImpl(const index_grid& indexGrid, bool detray = false,
0032                            bool checkSwap = false) {
0033   nlohmann::json jIndexedGrid;
0034 
0035   // Axis swapping (detray version)
0036   bool swapAxes = checkSwap;
0037 
0038   // Fill the casts
0039   nlohmann::json jCasts;
0040   // 1D casts
0041   if constexpr (index_grid::grid_type::DIM == 1u) {
0042     jCasts.push_back(indexGrid.casts[0u]);
0043   }
0044   // 1D casts
0045   if constexpr (index_grid::grid_type::DIM == 2u) {
0046     jCasts.push_back(indexGrid.casts[0u]);
0047     jCasts.push_back(indexGrid.casts[1u]);
0048     // Check for axis swap (detray version)
0049     swapAxes = checkSwap && (indexGrid.casts[0u] == AxisDirection::AxisZ &&
0050                              indexGrid.casts[1u] == AxisDirection::AxisPhi);
0051   }
0052   jIndexedGrid["casts"] = jCasts;
0053   jIndexedGrid["transform"] =
0054       Transform3JsonConverter::toJson(indexGrid.transform);
0055   if (detray) {
0056     jIndexedGrid = GridJsonConverter::toJsonDetray(indexGrid.grid, swapAxes);
0057   } else {
0058     jIndexedGrid["grid"] = GridJsonConverter::toJson(indexGrid.grid);
0059   }
0060   return jIndexedGrid;
0061 }
0062 
0063 /// @brief Helper method to be used for the creation of surface updators
0064 /// and volume updates
0065 ///
0066 /// @tparam updator_type
0067 /// @tparam generator_type
0068 ///
0069 /// @param jUpdater The corresponding json object
0070 /// @param jIndicator the string indicator which one it is
0071 ///
0072 /// @return the updator type
0073 template <typename updator_type, typename generator_type>
0074 updator_type generateFromJson(const nlohmann::json& jUpdater,
0075                               const std::string& jIndicator) {
0076   generator_type generator;
0077 
0078   using ValueType = typename generator_type::value_type;
0079 
0080   /// Helper extractor for equidistant axis
0081   /// @param jAxis is the axis
0082   auto eqExtractor = [](const nlohmann::json& jAxis)
0083       -> std::tuple<std::array<double, 2u>, std::size_t> {
0084     std::array<double, 2u> range = jAxis["range"];
0085     std::size_t bins = jAxis["bins"];
0086     return {range, bins};
0087   };
0088 
0089   /// Helper extractor for variable axis
0090   /// @param jAxis the axis
0091   auto vExtractor = [](const nlohmann::json& jAxis) -> std::vector<double> {
0092     std::vector<double> vEx(jAxis["boundaries"]);
0093     return vEx;
0094   };
0095 
0096   if (jUpdater["type"] != jIndicator ||
0097       jUpdater.find("grid") == jUpdater.end()) {
0098     // The return object
0099     updator_type updator;
0100     return updator;
0101   }
0102 
0103   // Peek into the json object to understand what to do
0104   const Transform3 transform =
0105       Transform3JsonConverter::fromJson(jUpdater["transform"]);
0106   const auto jGrid = jUpdater["grid"];
0107   const auto jCasts = jUpdater["casts"].get<std::vector<AxisDirection>>();
0108   const auto jAxes = jGrid["axes"];
0109 
0110   // 1D cases
0111   if (jAxes.size() == 1u) {
0112     AxisDirection bValue = jCasts[0u];
0113     auto jAxis = jAxes[0u];
0114 
0115     AxisType axisType = jAxis["type"];
0116     AxisBoundaryType axisBoundaryType = jAxis["boundary_type"];
0117 
0118     // Equidistant axis
0119     if (axisType == AxisType::Equidistant) {
0120       auto [range, bins] = eqExtractor(jAxis);
0121       if (axisBoundaryType == AxisBoundaryType::Closed) {
0122         EqClosed ecAG{range, bins};
0123         auto grid =
0124             GridJsonConverter::fromJson<EqClosed, ValueType>(jGrid, ecAG);
0125         return generator.createUpdater(std::move(grid), {bValue}, transform);
0126       } else {
0127         EqBound ebAG{range, bins};
0128         auto grid =
0129             GridJsonConverter::fromJson<EqBound, ValueType>(jGrid, ebAG);
0130         return generator.createUpdater(std::move(grid), {bValue}, transform);
0131       }
0132     } else {
0133       // Variable type
0134       if (axisBoundaryType == AxisBoundaryType::Closed) {
0135         VarClosed vcAG{vExtractor(jAxis)};
0136         auto grid =
0137             GridJsonConverter::fromJson<VarClosed, ValueType>(jGrid, vcAG);
0138         return generator.createUpdater(std::move(grid), {bValue}, transform);
0139       } else {
0140         VarBound vbAG{vExtractor(jAxis)};
0141         auto grid =
0142             GridJsonConverter::fromJson<VarBound, ValueType>(jGrid, vbAG);
0143         return generator.createUpdater(std::move(grid), {bValue}, transform);
0144       }
0145     }
0146   } else if (jAxes.size() == 2u) {
0147     // This currently writes out only the main options of 2D grids
0148     // nota bene: it assumes if one axis is closed, it is axis B
0149 
0150     AxisDirection bValueA = jCasts[0u];
0151     AxisDirection bValueB = jCasts[1u];
0152     auto jAxisA = jAxes[0u];
0153     auto jAxisB = jAxes[1u];
0154 
0155     AxisType axisTypeA = jAxisA["type"];
0156     AxisType axisTypeB = jAxisB["type"];
0157     AxisBoundaryType axisBoundaryTypeB = jAxisB["boundary_type"];
0158 
0159     if (axisBoundaryTypeB != AxisBoundaryType::Closed) {
0160       // First axis equidistant
0161       if (axisTypeA == AxisType::Equidistant) {
0162         auto [rangeA, binsA] = eqExtractor(jAxisA);
0163         if (axisTypeB == AxisType::Equidistant) {
0164           auto [rangeB, binsB] = eqExtractor(jAxisB);
0165           EqBoundEqBound ebebAG{rangeA, binsA, rangeB, binsB};
0166           auto grid = GridJsonConverter::fromJson<EqBoundEqBound, ValueType>(
0167               jGrid, ebebAG);
0168           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0169                                          transform);
0170         } else {
0171           EqBoundVarBound ebvbAG{rangeA, binsA, vExtractor(jAxisB)};
0172           auto grid = GridJsonConverter::fromJson<EqBoundVarBound, ValueType>(
0173               jGrid, ebvbAG);
0174           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0175                                          transform);
0176         }
0177       } else {
0178         if (axisTypeB == AxisType::Equidistant) {
0179           auto [rangeB, binsB] = eqExtractor(jAxisB);
0180           VarBoundEqBound vbebAG{vExtractor(jAxisA), rangeB, binsB};
0181           auto grid = GridJsonConverter::fromJson<VarBoundEqBound, ValueType>(
0182               jGrid, vbebAG);
0183           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0184                                          transform);
0185         } else {
0186           VarBoundVarBound vbvbAG{vExtractor(jAxisA), vExtractor(jAxisB)};
0187           auto grid = GridJsonConverter::fromJson<VarBoundVarBound, ValueType>(
0188               jGrid, vbvbAG);
0189           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0190                                          transform);
0191         }
0192       }
0193     } else {
0194       // Closed cases
0195       if (axisTypeA == AxisType::Equidistant) {
0196         auto [rangeA, binsA] = eqExtractor(jAxisA);
0197         if (axisTypeB == AxisType::Equidistant) {
0198           auto [rangeB, binsB] = eqExtractor(jAxisB);
0199           EqBoundEqClosed ebecAG{rangeA, binsA, rangeB, binsB};
0200           auto grid = GridJsonConverter::fromJson<EqBoundEqClosed, ValueType>(
0201               jGrid, ebecAG);
0202           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0203                                          transform);
0204         } else {
0205           EqBoundVarClosed ebvcAG{rangeA, binsA, vExtractor(jAxisB)};
0206           auto grid = GridJsonConverter::fromJson<EqBoundVarClosed, ValueType>(
0207               jGrid, ebvcAG);
0208           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0209                                          transform);
0210         }
0211       } else {
0212         if (axisTypeB == AxisType::Equidistant) {
0213           auto [rangeB, binsB] = eqExtractor(jAxisB);
0214           VarBoundEqClosed vbecAG{vExtractor(jAxisA), rangeB, binsB};
0215           auto grid = GridJsonConverter::fromJson<VarBoundEqClosed, ValueType>(
0216               jGrid, vbecAG);
0217           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0218                                          transform);
0219         } else {
0220           VarBoundVarClosed vbvcAG{vExtractor(jAxisA), vExtractor(jAxisB)};
0221           auto grid = GridJsonConverter::fromJson<VarBoundVarClosed, ValueType>(
0222               jGrid, vbvcAG);
0223           return generator.createUpdater(std::move(grid), {bValueA, bValueB},
0224                                          transform);
0225         }
0226       }
0227     }
0228   }
0229 
0230   // Default return object if jAxes.size() != 1 or 2
0231   updator_type updator;
0232   return updator;
0233 }
0234 
0235 }  // namespace IndexedGridJsonHelper
0236 }  // namespace Acts