Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GridSvgConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GridSvgConverterTests.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 
9 #include <boost/test/unit_test.hpp>
10 
16 #include "actsvg/display/grids.hpp"
17 
18 #include <fstream>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 using namespace Acts;
24 using namespace Acts::detail;
25 
26 namespace {
33 template <typename local_bin_t, size_t DIM>
34 std::string localToString(const local_bin_t& lBin) {
35  std::string lbString = "[";
36  for (size_t ib = 0; ib < DIM; ++ib) {
37  if (ib > 0u) {
38  lbString += std::string(", ");
39  }
40  lbString += std::to_string(lBin[ib]);
41  }
42  lbString += std::string("]");
43  return lbString;
44 }
45 } // namespace
46 
47 BOOST_AUTO_TEST_SUITE(ActSvg)
48 
49 BOOST_AUTO_TEST_CASE(BoundGridXY) {
50  using GlobalBin = size_t;
51  using LocalBin = std::array<size_t, 2u>;
52 
53  // x-y axis and grid
56  Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisX), decltype(axisY)>
57  gridXY({axisX, axisY});
58 
60  auto pGrid = Svg::GridConverter::convert(gridXY, {binX, binY}, cOptions);
61  BOOST_CHECK(pGrid._type == actsvg::proto::grid::type::e_x_y);
62 
63  // Labelling the grid tiles
64  auto edgesX = axisX.getBinEdges();
65  auto edgesY = axisY.getBinEdges();
66 
67  std::vector<actsvg::svg::object> targets = {};
68  for (auto [ix, x] : Acts::enumerate(edgesX)) {
69  if (ix > 0u) {
70  ActsScalar xp = 0.2 * edgesX[ix] + 0.8 * edgesX[ix - 1u];
71  for (auto [iy, y] : Acts::enumerate(edgesY)) {
72  if (iy > 0u) {
73  ActsScalar yp = 0.8 * edgesY[iy] + 0.2 * edgesY[iy - 1u];
74  decltype(gridXY)::point_t p = {xp, yp};
75  // Get local and global index
76  auto g = gridXY.globalBinFromPosition(p);
77  auto l = gridXY.localBinsFromPosition(p);
78  std::string gBin = std::string("g = ") + std::to_string(g);
79  std::string lBin =
80  std::string("l = ") +
81  localToString<decltype(gridXY)::index_t, decltype(gridXY)::DIM>(
82  l);
83  std::vector<std::string> glBin = {gBin, lBin};
84  std::string gBinID = "g_" + std::to_string(g);
85  targets.push_back(
86  actsvg::draw::text(gBinID,
87  {static_cast<actsvg::scalar>(xp),
88  static_cast<actsvg::scalar>(yp)},
89  glBin));
90  }
91  }
92  }
93  }
94  pGrid._connections = targets;
95 
96  auto oGrid = actsvg::display::grid("BoundGridXY", pGrid);
97 
98  // Add some labelling
99  actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
100  actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
101  actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
102 
103  auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
104  actsvg::style::marker({""}), axis_marker);
105  auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"x"}, axis_font);
106  auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 220}, axis_stroke,
107  actsvg::style::marker({""}), axis_marker);
108  auto yLabel = actsvg::draw::text("y_label", {-4, 232}, {"y"}, axis_font);
109 
110  std::vector<std::string> captionText = {
111  "Binning schema for global and local bins: ",
112  "- axis 0 : AxisBoundaryType::Bound, (-200., 200, 4), binX",
113  "- axis 1 : AxisBoundaryType::Bound, (-200, 200, 6), binY"};
114 
115  auto caption = actsvg::draw::text("caption", {-180, -220}, captionText);
116 
117  oGrid.add_object(xAxis);
118  oGrid.add_object(xLabel);
119  oGrid.add_object(yAxis);
120  oGrid.add_object(yLabel);
121  oGrid.add_object(caption);
122 
123  Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
124 }
125 
126 BOOST_AUTO_TEST_CASE(OpenGridXY) {
127  using GlobalBin = size_t;
128  using LocalBin = std::array<size_t, 2u>;
129 
130  // x-y axis and grid
133  Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisX), decltype(axisY)>
134  gridXY({axisX, axisY});
135 
137  auto pGrid = Svg::GridConverter::convert(gridXY, {binX, binY}, cOptions);
138  BOOST_CHECK(pGrid._type == actsvg::proto::grid::type::e_x_y);
139 
140  // Labelling the grid tiles
141  auto edgesX = axisX.getBinEdges();
142  auto edgesY = axisY.getBinEdges();
143 
144  std::vector<actsvg::svg::object> targets = {};
145  size_t ig = 0;
146  for (auto [ix, x] : Acts::enumerate(edgesX)) {
147  if (ix > 0u) {
148  ActsScalar xp = 0.2 * edgesX[ix] + 0.8 * edgesX[ix - 1u];
149  for (auto [iy, y] : Acts::enumerate(edgesY)) {
150  if (iy > 0u) {
151  ActsScalar yp = 0.8 * edgesY[iy] + 0.2 * edgesY[iy - 1u];
152  decltype(gridXY)::point_t p = {xp, yp};
153  // Get local and global index
154  auto g = gridXY.globalBinFromPosition(p);
155  auto l = gridXY.localBinsFromPosition(p);
156  std::string gBin = std::string("g = ") + std::to_string(g);
157  std::string lBin =
158  std::string("l = ") +
159  localToString<decltype(gridXY)::index_t, decltype(gridXY)::DIM>(
160  l);
161  std::vector<std::string> glBin = {gBin, lBin};
162  std::string gBinID = "g_" + std::to_string(ig++);
163  targets.push_back(
164  actsvg::draw::text(gBinID,
165  {static_cast<actsvg::scalar>(xp),
166  static_cast<actsvg::scalar>(yp)},
167  glBin));
168  }
169  }
170  }
171  }
172  pGrid._connections = targets;
173 
174  // Add some labelling
175  actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
176  actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
177  actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
178 
179  auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
180  actsvg::style::marker({""}), axis_marker);
181  auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"x"}, axis_font);
182  auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 220}, axis_stroke,
183  actsvg::style::marker({""}), axis_marker);
184  auto yLabel = actsvg::draw::text("y_label", {-4, 232}, {"y"}, axis_font);
185 
186  std::vector<std::string> captionText = {
187  "Binning schema for global and local bins: ",
188  "- axis 0 : AxisBoundaryType::Open, (-200., 200, 4), binX",
189  "- axis 1 : AxisBoundaryType::Open, (-200, 200, 6), binY"};
190 
191  auto caption = actsvg::draw::text("caption", {-180, -220}, captionText);
192  auto oGrid = actsvg::display::grid("OpenGridXY", pGrid);
193 
194  oGrid.add_object(xAxis);
195  oGrid.add_object(xLabel);
196  oGrid.add_object(yAxis);
197  oGrid.add_object(yLabel);
198  oGrid.add_object(caption);
199 
200  Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
201 }
202 
203 BOOST_AUTO_TEST_CASE(ClosedCylinderGridZPhi) {
204  using GlobalBin = size_t;
205  using LocalBin = std::array<size_t, 2u>;
206 
207  // z-phi Axes & Grid
210  Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisZ), decltype(axisPhi)>
211  gridZPhi({axisZ, axisPhi});
212 
214  auto pGrid = Svg::GridConverter::convert(gridZPhi, {binZ, binPhi}, cOptions);
215  BOOST_CHECK(pGrid._type == actsvg::proto::grid::type::e_z_phi);
216 
217  pGrid._reference_r = 80.;
218 
219  // Labelling the grid tiles
220  auto edgesZ = axisZ.getBinEdges();
221  auto edgesPhi = axisPhi.getBinEdges();
222 
223  std::vector<actsvg::svg::object> targets = {};
224  size_t ig = 0;
225  for (auto [iz, z] : Acts::enumerate(edgesZ)) {
226  if (iz > 0u) {
227  ActsScalar zp = 0.2 * edgesZ[iz] + 0.8 * edgesZ[iz - 1u];
228  for (auto [iphi, phi] : Acts::enumerate(edgesPhi)) {
229  if (iphi > 0u) {
230  ActsScalar phip = 0.8 * edgesPhi[iphi] + 0.2 * edgesPhi[iphi - 1u];
231  decltype(gridZPhi)::point_t p = {zp, phip};
232  // Get local and global index
233  auto g = gridZPhi.globalBinFromPosition(p);
234  auto l = gridZPhi.localBinsFromPosition(p);
235  std::string gBin = std::string("g = ") + std::to_string(g);
236  std::string lBin =
237  std::string("l = ") + localToString<decltype(gridZPhi)::index_t,
238  decltype(gridZPhi)::DIM>(l);
239  std::vector<std::string> glBin = {gBin, lBin};
240  std::string gBinID = "g_" + std::to_string(ig++);
241  targets.push_back(actsvg::draw::text(
242  gBinID,
243  {static_cast<actsvg::scalar>(zp),
244  static_cast<actsvg::scalar>(pGrid._reference_r * phip)},
245  glBin));
246  }
247  }
248  }
249  }
250  pGrid._connections = targets;
251 
252  // Add some labelling
253  actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
254  actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
255  actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
256 
257  auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
258  actsvg::style::marker({""}), axis_marker);
259  auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"z"}, axis_font);
260  auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 260}, axis_stroke,
261  actsvg::style::marker({""}), axis_marker);
262  auto yLabel = actsvg::draw::text("y_label", {-4, 270}, {"phi"}, axis_font);
263 
264  std::vector<std::string> captionText = {
265  "Binning schema for global and local bins: ",
266  "- axis 0 : AxisBoundaryType::Bound, (-200., 200, 3), binZ",
267  "- axis 1 : AxisBoundaryType::Closed, (-PI, PI, 6), binPhi",
268  "- draw reference radius set to 80"};
269 
270  auto caption = actsvg::draw::text("caption", {-180, -270}, captionText);
271  auto oGrid = actsvg::display::grid("ClosedCylinderGridZPhi", pGrid);
272 
273  oGrid.add_object(xAxis);
274  oGrid.add_object(xLabel);
275  oGrid.add_object(yAxis);
276  oGrid.add_object(yLabel);
277  oGrid.add_object(caption);
278 
279  Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
280 }
281 
282 BOOST_AUTO_TEST_CASE(ClosedDiscGridRPhi) {
283  using GlobalBin = size_t;
284  using LocalBin = std::array<size_t, 2u>;
285 
286  // r-phi Axes & Grid
289  Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisR), decltype(axisPhi)>
290  gridRPhi({axisR, axisPhi});
291 
293  auto pGrid = Svg::GridConverter::convert(gridRPhi, {binR, binPhi}, cOptions);
294  BOOST_CHECK(pGrid._type == actsvg::proto::grid::type::e_r_phi);
295 
296  // Labelling the grid tiles
297  auto edgesR = axisR.getBinEdges();
298  auto edgesPhi = axisPhi.getBinEdges();
299 
300  std::vector<actsvg::svg::object> targets = {};
301  size_t ig = 0;
302  for (auto [ir, r] : Acts::enumerate(edgesR)) {
303  if (ir > 0u) {
304  ActsScalar rp = 0.5 * (edgesR[ir] + edgesR[ir - 1u]);
305  for (auto [iphi, phi] : Acts::enumerate(edgesPhi)) {
306  if (iphi > 0u) {
307  ActsScalar phip = 0.5 * (edgesPhi[iphi] + edgesPhi[iphi - 1u]);
308  decltype(gridRPhi)::point_t p = {rp, phip};
309  // Get local and global index
310  auto g = gridRPhi.globalBinFromPosition(p);
311  auto l = gridRPhi.localBinsFromPosition(p);
312  std::string gBin = std::string("g = ") + std::to_string(g);
313  std::string lBin =
314  std::string("l = ") + localToString<decltype(gridRPhi)::index_t,
315  decltype(gridRPhi)::DIM>(l);
316  std::vector<std::string> glBin = {gBin, lBin};
317  std::string gBinID = "g_" + std::to_string(ig++);
318  targets.push_back(
319  actsvg::draw::text(gBinID,
320  {static_cast<actsvg::scalar>(rp * cos(phip)),
321  static_cast<actsvg::scalar>(rp * sin(phip))},
322  glBin));
323  }
324  }
325  }
326  }
327  pGrid._connections = targets;
328 
329  // Add some labelling
330  actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
331  actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
332  actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
333 
334  auto rAxis = actsvg::draw::arrow("r_axis", {0, 0}, {420, 0}, axis_stroke,
335  actsvg::style::marker({""}), axis_marker);
336  auto rLabel = actsvg::draw::text("r_label", {440, -4}, {"r"}, axis_font);
337 
338  auto phiAxis = actsvg::draw::arc_measure(
339  "phi_axis", 410., {410, 0.},
340  {static_cast<actsvg::scalar>(410. * cos(0.25)),
341  static_cast<actsvg::scalar>(410. * sin(0.25))},
342  axis_stroke, actsvg::style::marker(), axis_marker);
343 
344  auto phiLabel =
345  actsvg::draw::text("phi_label", {410, 60}, {"phi"}, axis_font);
346 
347  std::vector<std::string> captionText = {
348  "Binning schema for global and local bins: ",
349  "- axis 0 : AxisBoundaryType::Bound, (100., 400, 3), binR",
350  "- axis 1 : AxisBoundaryType::Closed, (-PI, PI, 4), binPhi"};
351 
352  auto caption = actsvg::draw::text("caption", {-180, -420}, captionText);
353  auto oGrid = actsvg::display::grid("ClosedDiscGridRPhi", pGrid);
354 
355  oGrid.add_object(rAxis);
356  oGrid.add_object(rLabel);
357  oGrid.add_object(phiAxis);
358  oGrid.add_object(phiLabel);
359  oGrid.add_object(caption);
360 
361  Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
362 }
363 
364 BOOST_AUTO_TEST_SUITE_END()