Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GridSvgConverter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GridSvgConverter.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 
15 #include <actsvg/core.hpp>
16 #include <actsvg/meta.hpp>
17 
18 #include <array>
19 #include <optional>
20 #include <tuple>
21 #include <vector>
22 
23 namespace Acts {
24 
25 class Surface;
26 
27 namespace Svg {
28 
30 
31 namespace GridConverter {
32 
33 // An optional range and binning value
34 using AxisBound = std::tuple<std::array<ActsScalar, 2u>, BinningValue>;
35 
37 struct Options {
39  Style style{{150, 150, 150}, 0.5};
41  std::optional<AxisBound> optionalBound;
42 };
43 
56 template <typename grid_type>
57 ProtoGrid convert(const grid_type& grid,
58  const std::array<BinningValue, grid_type::DIM>& bValues,
59  const GridConverter::Options& cOptions) {
60  // The return object
61  ProtoGrid pGrid;
62 
63  // Grid axes
64  auto axes = grid.axes();
65 
66  // The edge values - these need to follow the ACTSVG convention,
67  // so there could be swapping when necessary
68  std::vector<Acts::ActsScalar> edges0;
69  std::vector<Acts::ActsScalar> edges1;
70 
71  // 1D case (more to be filled in later)
72  if constexpr (grid_type::DIM == 1u) {
73  if (bValues[0u] == binPhi and
74  axes[0]->getBoundaryType() == detail::AxisBoundaryType::Closed) {
75  // swap needed
76  edges1 = axes[0]->getBinEdges();
77  pGrid._type = actsvg::proto::grid::e_r_phi;
78  }
79  if (cOptions.optionalBound.has_value()) {
80  auto [boundRange, boundValue] = cOptions.optionalBound.value();
81  if (boundValue == binR) {
82  // good - no swap needed
83  edges0 = {boundRange[0u], boundRange[1u]};
84  }
85  }
86  }
87  // 2D cases
88  if constexpr (grid_type::DIM == 2u) {
89  // Assign
90  edges0 = axes[0]->getBinEdges();
91  edges1 = axes[1]->getBinEdges();
92  if (bValues[0] == binPhi and bValues[1] == binZ) {
93  // swap needed
94  std::swap(edges0, edges1);
95  pGrid._type = actsvg::proto::grid::e_z_phi;
96  } else if (bValues[0] == binPhi and bValues[1] == binR) {
97  // swap needed
98  std::swap(edges0, edges1);
99  pGrid._type = actsvg::proto::grid::e_r_phi;
100  } else if (bValues[0] == binZ and bValues[1] == binPhi) {
101  // good - no swap needed
102  pGrid._type = actsvg::proto::grid::e_z_phi;
103  } else if (bValues[0] == binR and bValues[1] == binPhi) {
104  // good - no swap needed
105  pGrid._type = actsvg::proto::grid::e_r_phi;
106  } else if (bValues[0] == binX and bValues[1] == binY) {
107  // good - no swap needed
108  pGrid._type = actsvg::proto::grid::e_x_y;
109  }
110  }
111 
112  // Assign grid edges
113  pGrid._edges_0 = std::vector<actsvg::scalar>(edges0.begin(), edges0.end());
114  pGrid._edges_1 = std::vector<actsvg::scalar>(edges1.begin(), edges1.end());
115 
116  auto [fill, stroke] = cOptions.style.fillAndStroke();
117  pGrid._fill = fill;
118  pGrid._stroke = stroke;
119 
120  return pGrid;
121 }
122 
123 } // namespace GridConverter
124 } // namespace Svg
125 } // namespace Acts