Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GridJsonConverter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GridJsonConverter.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 #include "Acts/Utilities/IAxis.hpp"
14 
15 #include <iostream>
16 
17 // Custom Json encoder/decoders.
18 namespace Acts {
19 
20 namespace AxisJsonConverter {
21 
23 //
27 nlohmann::json toJson(const IAxis& ia);
28 
29 } // namespace AxisJsonConverter
30 
31 namespace GridJsonConverter {
32 
39 template <typename grid_type>
40 nlohmann::json toJson(const grid_type& grid) {
41  nlohmann::json jGrid;
42 
43  auto axes = grid.axes();
44  nlohmann::json jAxes;
45  nlohmann::json jData;
46  // 1D connections
47  if constexpr (grid_type::DIM == 1u) {
48  jAxes.push_back(AxisJsonConverter::toJson(*axes[0u]));
49  for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
50  typename grid_type::index_t lbin;
51  lbin[0u] = ib0;
52  jData.push_back(std::tie(lbin, grid.atLocalBins(lbin)));
53  }
54  }
55  // 2D connections
56  if constexpr (grid_type::DIM == 2u) {
57  jAxes.push_back(AxisJsonConverter::toJson(*axes[0u]));
58  jAxes.push_back(AxisJsonConverter::toJson(*axes[1u]));
59 
60  for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
61  for (unsigned int ib1 = 1u; ib1 <= axes[1u]->getNBins(); ++ib1) {
62  typename grid_type::index_t lbin;
63  lbin[0u] = ib0;
64  lbin[1u] = ib1;
65  jData.push_back(std::tie(lbin, grid.atLocalBins(lbin)));
66  }
67  }
68  }
69  jGrid["axes"] = jAxes;
70  jGrid["data"] = jData;
71 
72  return jGrid;
73 }
74 
86 template <typename axis_generator_type>
87 auto fromJson(const nlohmann::json& jGrid,
88  const axis_generator_type& aGenerator) {
89  // Generate the grid
90  using GridType = typename axis_generator_type::template grid_type<
91  std::vector<std::size_t>>;
92  GridType grid(aGenerator());
93  nlohmann::json jData = jGrid["data"];
94  // Index filling
95  if constexpr (GridType::DIM == 1u) {
96  for (const auto& jd : jData) {
97  std::array<std::size_t, 1u> lbin = jd[0u];
98  std::vector<std::size_t> values = jd[1u];
99  grid.atLocalBins(lbin) = values;
100  }
101  }
102  if constexpr (GridType::DIM == 2u) {
103  for (const auto& jd : jData) {
104  std::array<std::size_t, 2u> lbin = jd[0u];
105  std::vector<std::size_t> values = jd[1u];
106  grid.atLocalBins(lbin) = values;
107  }
108  }
109 
110  return grid;
111 }
112 
113 } // namespace GridJsonConverter
114 
116 NLOHMANN_JSON_SERIALIZE_ENUM(Acts::detail::AxisBoundaryType,
117  {{Acts::detail::AxisBoundaryType::Bound, "Bound"},
118  {Acts::detail::AxisBoundaryType::Open, "Open"},
119  {Acts::detail::AxisBoundaryType::Closed,
120  "Closed"}})
121 
122 NLOHMANN_JSON_SERIALIZE_ENUM(Acts::detail::AxisType,
123  {{Acts::detail::AxisType::Equidistant,
124  "Equidistant"},
125  {Acts::detail::AxisType::Variable, "Variable"}})
126 
128 
129 } // namespace Acts