File indexing completed on 2025-01-18 09:27:42
0001
0002
0003
0004
0005
0006
0007
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
0019 namespace Acts {
0020
0021 namespace detail {
0022
0023
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
0036
0037 }
0038
0039 namespace AxisJsonConverter {
0040
0041
0042
0043
0044
0045
0046 nlohmann::json toJson(const IAxis& ia);
0047
0048
0049
0050
0051
0052
0053 nlohmann::json toJsonDetray(const IAxis& ia);
0054
0055 }
0056
0057 namespace GridAccessJsonConverter {
0058
0059
0060
0061
0062
0063
0064 nlohmann::json toJson(const GridAccess::IGlobalToGridLocal& globalToGridLocal);
0065
0066
0067
0068
0069
0070
0071 std::unique_ptr<const GridAccess::IGlobalToGridLocal> globalToGridLocalFromJson(
0072 const nlohmann::json& jGlobalToGridLocal);
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 GridAccess::GlobalToGridLocal1DimDelegate globalToGridLocal1DimDelegateFromJson(
0084 const nlohmann::json& jGlobalToGridLocal);
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 GridAccess::GlobalToGridLocal2DimDelegate globalToGridLocal2DimDelegateFromJson(
0096 const nlohmann::json& jGlobalToGridLocal);
0097
0098
0099
0100
0101
0102
0103 nlohmann::json toJson(const GridAccess::IBoundToGridLocal& boundToGridLocal);
0104
0105
0106
0107
0108
0109
0110 std::unique_ptr<GridAccess::IBoundToGridLocal> boundToGridLocalFromJson(
0111 const nlohmann::json& jBoundToGridLocal);
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 GridAccess::BoundToGridLocal1DimDelegate boundToGridLocal1DimDelegateFromJson(
0123 const nlohmann::json& jBoundToGridLocal);
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 GridAccess::BoundToGridLocal2DimDelegate boundToGridLocal2DimDelegateFromJson(
0135 const nlohmann::json& jBoundToGridLocal);
0136
0137 }
0138
0139 namespace GridJsonConverter {
0140
0141
0142
0143
0144
0145
0146
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
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
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
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 template <typename grid_type>
0195 nlohmann::json toJsonDetray(const grid_type& grid, bool swapAxis = false) {
0196 nlohmann::json jGrid;
0197
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
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
0215 if constexpr (grid_type::DIM == 1u) {
0216 for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0217
0218 typename grid_type::index_t lbin;
0219 lbin[0u] = ib0;
0220 nlohmann::json jBin;
0221 jBin["content"] = grid.atLocalBins(lbin);
0222
0223 lbin[0u] = ib0 - 1u;
0224 jBin["loc_index"] = lbin;
0225 jData.push_back(jBin);
0226 }
0227 }
0228
0229
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
0235 lbin[0u] = swapAxis ? ib1 : ib0;
0236 lbin[1u] = swapAxis ? ib0 : ib1;
0237 nlohmann::json jBin;
0238 jBin["content"] = grid.atLocalBins(lbin);
0239
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
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
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
0268 using GridType = typename axis_generator_type::template grid_type<value_type>;
0269 GridType grid(aGenerator());
0270 nlohmann::json jData = jGrid["data"];
0271
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 }
0290 }