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