Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IndexedSurfacesJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file IndexedSurfacesJsonConverter.cpp
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 
10 
15 
16 #include <array>
17 #include <memory>
18 #include <tuple>
19 #include <vector>
20 
21 namespace {
22 
32 template <typename grid_type>
34  grid_type&& grid, const std::array<Acts::BinningValue, grid_type::DIM>& bv,
35  const Acts::Transform3& transform) {
37  std::move(grid), bv, transform);
38 
39  // The portal delegate
41 
42  // The chained delegate: indexed surfaces and all portals
45  auto indexedSurfacesAllPortals = std::make_unique<const DelegateType>(
46  std::tie(allPortals, indexedSurfaces));
47 
48  // Create the delegate and connect it
50  nStateUpdator.connect<&DelegateType::update>(
51  std::move(indexedSurfacesAllPortals));
52 
53  return nStateUpdator;
54 }
55 
56 } // namespace
57 
60  const nlohmann::json& jSurfaceNavigation) {
61  // The return object
63 
64  // Helper extractor
65  auto eqExtractor = [](const nlohmann::json& jAxis)
66  -> std::tuple<std::array<ActsScalar, 2u>, std::size_t> {
67  std::array<ActsScalar, 2u> range = jAxis["range"];
68  std::size_t bins = jAxis["bins"];
69  return std::make_tuple(range, bins);
70  };
71 
72  auto vExtractor = [](const nlohmann::json& jAxis) -> std::vector<ActsScalar> {
73  return std::vector<ActsScalar>(jAxis["boundaries"]);
74  };
75 
76  // Peek into the json object to understand what to do
77  if (jSurfaceNavigation["type"] == "IndexedSurfaces") {
78  if (jSurfaceNavigation.find("grid") != jSurfaceNavigation.end()) {
80  Transform3JsonConverter::fromJson(jSurfaceNavigation["transform"]);
81  auto jGrid = jSurfaceNavigation["grid"];
82  auto jCasts =
83  jSurfaceNavigation["casts"].get<std::vector<BinningValue>>();
84  auto jAxes = jGrid["axes"];
85 
86  // 1D cases
87  if (jAxes.size() == 1u) {
88  BinningValue bValue = jCasts[0u];
89  auto jAxis = jAxes[0u];
90 
91  detail::AxisType axisType = jAxis["type"];
92  detail::AxisBoundaryType axisBoundaryType = jAxis["boundary_type"];
93 
94  // Equidistant axis
95  if (axisType == detail::AxisType::Equidistant) {
96  auto [range, bins] = eqExtractor(jAxis);
97  if (axisBoundaryType == detail::AxisBoundaryType::Closed) {
98  EqClosed ecAG{range, bins};
99  auto grid = GridJsonConverter::fromJson(jGrid, ecAG);
100  return createUpdator(std::move(grid), {bValue}, transform);
101  } else {
102  EqBound ebAG{range, bins};
103  auto grid = GridJsonConverter::fromJson(jGrid, ebAG);
104  return createUpdator(std::move(grid), {bValue}, transform);
105  }
106  } else {
107  // Variable type
108  if (axisBoundaryType == detail::AxisBoundaryType::Closed) {
109  VarClosed vcAG{vExtractor(jAxis)};
110  auto grid = GridJsonConverter::fromJson(jGrid, vcAG);
111  return createUpdator(std::move(grid), {bValue}, transform);
112  } else {
113  VarBound vbAG{vExtractor(jAxis)};
114  auto grid = GridJsonConverter::fromJson(jGrid, vbAG);
115  return createUpdator(std::move(grid), {bValue}, transform);
116  }
117  }
118  } else if (jAxes.size() == 2u) {
119  // This currently writes out only the main options of 2D grids
120  // nota bene: it assumes if one axis is closed, it is axis B
121 
122  BinningValue bValueA = jCasts[0u];
123  BinningValue bValueB = jCasts[1u];
124  auto jAxisA = jAxes[0u];
125  auto jAxisB = jAxes[1u];
126 
127  detail::AxisType axisTypeA = jAxisA["type"];
128  detail::AxisType axisTypeB = jAxisB["type"];
129  detail::AxisBoundaryType axisBoundaryTypeB = jAxisB["boundary_type"];
130 
131  if (axisBoundaryTypeB != detail::AxisBoundaryType::Closed) {
132  // First axis equidistant
133  if (axisTypeA == detail::AxisType::Equidistant) {
134  auto [rangeA, binsA] = eqExtractor(jAxisA);
135  if (axisTypeB == detail::AxisType::Equidistant) {
136  auto [rangeB, binsB] = eqExtractor(jAxisB);
137  EqBoundEqBound ebebAG{rangeA, binsA, rangeB, binsB};
138  auto grid = GridJsonConverter::fromJson(jGrid, ebebAG);
139  return createUpdator(std::move(grid), {bValueA, bValueB},
140  transform);
141  } else {
142  EqBoundVarBound ebvbAG{rangeA, binsA, vExtractor(jAxisB)};
143  auto grid = GridJsonConverter::fromJson(jGrid, ebvbAG);
144  return createUpdator(std::move(grid), {bValueA, bValueB},
145  transform);
146  }
147  } else {
148  if (axisTypeB == detail::AxisType::Equidistant) {
149  auto [rangeB, binsB] = eqExtractor(jAxisB);
150  VarBoundEqBound vbebAG{vExtractor(jAxisA), rangeB, binsB};
151  auto grid = GridJsonConverter::fromJson(jGrid, vbebAG);
152  return createUpdator(std::move(grid), {bValueA, bValueB},
153  transform);
154  } else {
155  VarBoundVarBound vbvbAG{vExtractor(jAxisA), vExtractor(jAxisB)};
156  auto grid = GridJsonConverter::fromJson(jGrid, vbvbAG);
157  return createUpdator(std::move(grid), {bValueA, bValueB},
158  transform);
159  }
160  }
161  } else {
162  // Closed cases
163  if (axisTypeA == detail::AxisType::Equidistant) {
164  auto [rangeA, binsA] = eqExtractor(jAxisA);
165  if (axisTypeB == detail::AxisType::Equidistant) {
166  auto [rangeB, binsB] = eqExtractor(jAxisB);
167  EqBoundEqClosed ebecAG{rangeA, binsA, rangeB, binsB};
168  auto grid = GridJsonConverter::fromJson(jGrid, ebecAG);
169  return createUpdator(std::move(grid), {bValueA, bValueB},
170  transform);
171  } else {
172  EqBoundVarClosed ebvcAG{rangeA, binsA, vExtractor(jAxisB)};
173  auto grid = GridJsonConverter::fromJson(jGrid, ebvcAG);
174  return createUpdator(std::move(grid), {bValueA, bValueB},
175  transform);
176  }
177  } else {
178  if (axisTypeB == detail::AxisType::Equidistant) {
179  auto [rangeB, binsB] = eqExtractor(jAxisB);
180  VarBoundEqClosed vbecAG{vExtractor(jAxisA), rangeB, binsB};
181  auto grid = GridJsonConverter::fromJson(jGrid, vbecAG);
182  return createUpdator(std::move(grid), {bValueA, bValueB},
183  transform);
184  } else {
185  VarBoundVarClosed vbvcAG{vExtractor(jAxisA), vExtractor(jAxisB)};
186  auto grid = GridJsonConverter::fromJson(jGrid, vbvcAG);
187  return createUpdator(std::move(grid), {bValueA, bValueB},
188  transform);
189  }
190  }
191  }
192  }
193  }
194  }
195  // Return the object
197 }