File indexing completed on 2025-06-30 08:06:59
0001
0002
0003
0004
0005
0006
0007
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
0019 namespace Acts {
0020
0021
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
0032
0033 namespace AxisJsonConverter {
0034
0035
0036
0037
0038
0039
0040 nlohmann::json toJson(const IAxis& ia);
0041
0042
0043
0044
0045
0046
0047 nlohmann::json toJsonDetray(const IAxis& ia);
0048
0049 }
0050
0051 namespace GridAccessJsonConverter {
0052
0053
0054
0055
0056
0057
0058 nlohmann::json toJson(const GridAccess::IGlobalToGridLocal& globalToGridLocal);
0059
0060
0061
0062
0063
0064
0065 std::unique_ptr<const GridAccess::IGlobalToGridLocal> globalToGridLocalFromJson(
0066 const nlohmann::json& jGlobalToGridLocal);
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 GridAccess::GlobalToGridLocal1DimDelegate globalToGridLocal1DimDelegateFromJson(
0078 const nlohmann::json& jGlobalToGridLocal);
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 GridAccess::GlobalToGridLocal2DimDelegate globalToGridLocal2DimDelegateFromJson(
0090 const nlohmann::json& jGlobalToGridLocal);
0091
0092
0093
0094
0095
0096
0097 nlohmann::json toJson(const GridAccess::IBoundToGridLocal& boundToGridLocal);
0098
0099
0100
0101
0102
0103
0104 std::unique_ptr<GridAccess::IBoundToGridLocal> boundToGridLocalFromJson(
0105 const nlohmann::json& jBoundToGridLocal);
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 GridAccess::BoundToGridLocal1DimDelegate boundToGridLocal1DimDelegateFromJson(
0117 const nlohmann::json& jBoundToGridLocal);
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 GridAccess::BoundToGridLocal2DimDelegate boundToGridLocal2DimDelegateFromJson(
0129 const nlohmann::json& jBoundToGridLocal);
0130
0131 }
0132
0133 namespace GridJsonConverter {
0134
0135
0136
0137
0138
0139
0140
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
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
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
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 template <typename grid_type>
0189 nlohmann::json toJsonDetray(const grid_type& grid, bool swapAxis = false) {
0190 nlohmann::json jGrid;
0191
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
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
0209 if constexpr (grid_type::DIM == 1u) {
0210 for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
0211
0212 typename grid_type::index_t lbin;
0213 lbin[0u] = ib0;
0214 nlohmann::json jBin;
0215 jBin["content"] = grid.atLocalBins(lbin);
0216
0217 lbin[0u] = ib0 - 1u;
0218 jBin["loc_index"] = lbin;
0219 jData.push_back(jBin);
0220 }
0221 }
0222
0223
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
0229 lbin[0u] = swapAxis ? ib1 : ib0;
0230 lbin[1u] = swapAxis ? ib0 : ib1;
0231 nlohmann::json jBin;
0232 jBin["content"] = grid.atLocalBins(lbin);
0233
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
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
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
0262 using GridType = typename axis_generator_type::template grid_type<value_type>;
0263 GridType grid(aGenerator());
0264 nlohmann::json jData = jGrid["data"];
0265
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 }
0284 }