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/AlgebraJsonConverter.hpp"
0012 #include "Acts/Plugins/Json/GridJsonConverter.hpp"
0013 #include "Acts/Plugins/Json/UtilitiesJsonConverter.hpp"
0014 #include "Acts/Utilities/Grid.hpp"
0015 #include "Acts/Utilities/GridAxisGenerators.hpp"
0016 #include "Acts/Utilities/detail/AxisFwd.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 writout
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 &&
0050                (indexGrid.casts[0u] == binZ && indexGrid.casts[1u] == binPhi);
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<ActsScalar, 2u>, std::size_t> {
0084     std::array<ActsScalar, 2u> range = jAxis["range"];
0085     std::size_t bins = jAxis["bins"];
0086     return std::make_tuple(range, bins);
0087   };
0088 
0089   /// Helper extractor for variable axis
0090   /// @param jAxis the axis
0091   auto vExtractor = [](const nlohmann::json& jAxis) -> std::vector<ActsScalar> {
0092     std::vector<ActsScalar> vEx(jAxis["boundaries"]);
0093     return vEx;
0094   };
0095 
0096   // Peek into the json object to understand what to do
0097   if (jUpdater["type"] == jIndicator) {
0098     if (jUpdater.find("grid") != jUpdater.end()) {
0099       Transform3 transform =
0100           Transform3JsonConverter::fromJson(jUpdater["transform"]);
0101       auto jGrid = jUpdater["grid"];
0102       auto jCasts = jUpdater["casts"].get<std::vector<BinningValue>>();
0103       auto jAxes = jGrid["axes"];
0104 
0105       // 1D cases
0106       if (jAxes.size() == 1u) {
0107         BinningValue bValue = jCasts[0u];
0108         auto jAxis = jAxes[0u];
0109 
0110         detail::AxisType axisType = jAxis["type"];
0111         detail::AxisBoundaryType axisBoundaryType = jAxis["boundary_type"];
0112 
0113         // Equidistant axis
0114         if (axisType == detail::AxisType::Equidistant) {
0115           auto [range, bins] = eqExtractor(jAxis);
0116           if (axisBoundaryType == detail::AxisBoundaryType::Closed) {
0117             EqClosed ecAG{range, bins};
0118             auto grid =
0119                 GridJsonConverter::fromJson<EqClosed, ValueType>(jGrid, ecAG);
0120             return generator.createUpdater(std::move(grid), {bValue},
0121                                            transform);
0122           } else {
0123             EqBound ebAG{range, bins};
0124             auto grid =
0125                 GridJsonConverter::fromJson<EqBound, ValueType>(jGrid, ebAG);
0126             return generator.createUpdater(std::move(grid), {bValue},
0127                                            transform);
0128           }
0129         } else {
0130           // Variable type
0131           if (axisBoundaryType == detail::AxisBoundaryType::Closed) {
0132             VarClosed vcAG{vExtractor(jAxis)};
0133             auto grid =
0134                 GridJsonConverter::fromJson<VarClosed, ValueType>(jGrid, vcAG);
0135             return generator.createUpdater(std::move(grid), {bValue},
0136                                            transform);
0137           } else {
0138             VarBound vbAG{vExtractor(jAxis)};
0139             auto grid =
0140                 GridJsonConverter::fromJson<VarBound, ValueType>(jGrid, vbAG);
0141             return generator.createUpdater(std::move(grid), {bValue},
0142                                            transform);
0143           }
0144         }
0145       } else if (jAxes.size() == 2u) {
0146         // This currently writes out only the main options of 2D grids
0147         // nota bene: it assumes if one axis is closed, it is axis B
0148 
0149         BinningValue bValueA = jCasts[0u];
0150         BinningValue bValueB = jCasts[1u];
0151         auto jAxisA = jAxes[0u];
0152         auto jAxisB = jAxes[1u];
0153 
0154         detail::AxisType axisTypeA = jAxisA["type"];
0155         detail::AxisType axisTypeB = jAxisB["type"];
0156         detail::AxisBoundaryType axisBoundaryTypeB = jAxisB["boundary_type"];
0157 
0158         if (axisBoundaryTypeB != detail::AxisBoundaryType::Closed) {
0159           // First axis equidistant
0160           if (axisTypeA == detail::AxisType::Equidistant) {
0161             auto [rangeA, binsA] = eqExtractor(jAxisA);
0162             if (axisTypeB == detail::AxisType::Equidistant) {
0163               auto [rangeB, binsB] = eqExtractor(jAxisB);
0164               EqBoundEqBound ebebAG{rangeA, binsA, rangeB, binsB};
0165               auto grid =
0166                   GridJsonConverter::fromJson<EqBoundEqBound, ValueType>(
0167                       jGrid, ebebAG);
0168               return generator.createUpdater(std::move(grid),
0169                                              {bValueA, bValueB}, transform);
0170             } else {
0171               EqBoundVarBound ebvbAG{rangeA, binsA, vExtractor(jAxisB)};
0172               auto grid =
0173                   GridJsonConverter::fromJson<EqBoundVarBound, ValueType>(
0174                       jGrid, ebvbAG);
0175               return generator.createUpdater(std::move(grid),
0176                                              {bValueA, bValueB}, transform);
0177             }
0178           } else {
0179             if (axisTypeB == detail::AxisType::Equidistant) {
0180               auto [rangeB, binsB] = eqExtractor(jAxisB);
0181               VarBoundEqBound vbebAG{vExtractor(jAxisA), rangeB, binsB};
0182               auto grid =
0183                   GridJsonConverter::fromJson<VarBoundEqBound, ValueType>(
0184                       jGrid, vbebAG);
0185               return generator.createUpdater(std::move(grid),
0186                                              {bValueA, bValueB}, transform);
0187             } else {
0188               VarBoundVarBound vbvbAG{vExtractor(jAxisA), vExtractor(jAxisB)};
0189               auto grid =
0190                   GridJsonConverter::fromJson<VarBoundVarBound, ValueType>(
0191                       jGrid, vbvbAG);
0192               return generator.createUpdater(std::move(grid),
0193                                              {bValueA, bValueB}, transform);
0194             }
0195           }
0196         } else {
0197           // Closed cases
0198           if (axisTypeA == detail::AxisType::Equidistant) {
0199             auto [rangeA, binsA] = eqExtractor(jAxisA);
0200             if (axisTypeB == detail::AxisType::Equidistant) {
0201               auto [rangeB, binsB] = eqExtractor(jAxisB);
0202               EqBoundEqClosed ebecAG{rangeA, binsA, rangeB, binsB};
0203               auto grid =
0204                   GridJsonConverter::fromJson<EqBoundEqClosed, ValueType>(
0205                       jGrid, ebecAG);
0206               return generator.createUpdater(std::move(grid),
0207                                              {bValueA, bValueB}, transform);
0208             } else {
0209               EqBoundVarClosed ebvcAG{rangeA, binsA, vExtractor(jAxisB)};
0210               auto grid =
0211                   GridJsonConverter::fromJson<EqBoundVarClosed, ValueType>(
0212                       jGrid, ebvcAG);
0213               return generator.createUpdater(std::move(grid),
0214                                              {bValueA, bValueB}, transform);
0215             }
0216           } else {
0217             if (axisTypeB == detail::AxisType::Equidistant) {
0218               auto [rangeB, binsB] = eqExtractor(jAxisB);
0219               VarBoundEqClosed vbecAG{vExtractor(jAxisA), rangeB, binsB};
0220               auto grid =
0221                   GridJsonConverter::fromJson<VarBoundEqClosed, ValueType>(
0222                       jGrid, vbecAG);
0223               return generator.createUpdater(std::move(grid),
0224                                              {bValueA, bValueB}, transform);
0225             } else {
0226               VarBoundVarClosed vbvcAG{vExtractor(jAxisA), vExtractor(jAxisB)};
0227               auto grid =
0228                   GridJsonConverter::fromJson<VarBoundVarClosed, ValueType>(
0229                       jGrid, vbvcAG);
0230               return generator.createUpdater(std::move(grid),
0231                                              {bValueA, bValueB}, transform);
0232             }
0233           }
0234         }
0235       }
0236     }
0237   }
0238   // The return object
0239   updator_type updator;
0240   return updator;
0241 }
0242 
0243 }  // namespace IndexedGridJsonHelper
0244 }  // namespace Acts