Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:40

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Geometry/GeometryContext.hpp"
0012 #include "Acts/Plugins/ActSVG/GridSvgConverter.hpp"
0013 #include "Acts/Utilities/Enumerate.hpp"
0014 #include "Acts/Utilities/Grid.hpp"
0015 #include "Acts/Utilities/detail/Axis.hpp"
0016 #include "actsvg/display/grids.hpp"
0017 
0018 #include <fstream>
0019 #include <memory>
0020 #include <string>
0021 #include <vector>
0022 
0023 using namespace Acts;
0024 using namespace Acts::detail;
0025 
0026 namespace {
0027 /// Helper method to turn a local bin into a string
0028 ///
0029 /// @tparam local_bin_t the type for the local bin
0030 /// @param lBin the local bin to printed
0031 ///
0032 /// @return a string for screen output
0033 template <typename local_bin_t, std::size_t DIM>
0034 std::string localToString(const local_bin_t& lBin) {
0035   std::string lbString = "[";
0036   for (std::size_t ib = 0; ib < DIM; ++ib) {
0037     if (ib > 0u) {
0038       lbString += std::string(", ");
0039     }
0040     lbString += std::to_string(lBin[ib]);
0041   }
0042   lbString += std::string("]");
0043   return lbString;
0044 }
0045 }  // namespace
0046 
0047 BOOST_AUTO_TEST_SUITE(ActSvg)
0048 
0049 BOOST_AUTO_TEST_CASE(BoundGridXY) {
0050   using GlobalBin = std::size_t;
0051   using LocalBin = std::array<std::size_t, 2u>;
0052 
0053   // x-y axis and grid
0054   Axis<AxisType::Equidistant, AxisBoundaryType::Bound> axisX(-200., 200, 4);
0055   Axis<AxisType::Equidistant, AxisBoundaryType::Bound> axisY(-200, 200, 6);
0056   Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisX), decltype(axisY)>
0057       gridXY({axisX, axisY});
0058 
0059   Svg::GridConverter::Options cOptions;
0060   auto pGrid = Svg::GridConverter::convert(gridXY, {binX, binY}, cOptions);
0061   BOOST_CHECK_EQUAL(pGrid._type, actsvg::proto::grid::type::e_x_y);
0062 
0063   // Labelling the grid tiles
0064   auto edgesX = axisX.getBinEdges();
0065   auto edgesY = axisY.getBinEdges();
0066 
0067   std::vector<actsvg::svg::object> targets = {};
0068   for (auto [ix, x] : Acts::enumerate(edgesX)) {
0069     if (ix > 0u) {
0070       ActsScalar xp = 0.2 * edgesX[ix] + 0.8 * edgesX[ix - 1u];
0071       for (auto [iy, y] : Acts::enumerate(edgesY)) {
0072         if (iy > 0u) {
0073           ActsScalar yp = 0.8 * edgesY[iy] + 0.2 * edgesY[iy - 1u];
0074           decltype(gridXY)::point_t p = {xp, yp};
0075           // Get local and global index
0076           auto g = gridXY.globalBinFromPosition(p);
0077           auto l = gridXY.localBinsFromPosition(p);
0078           std::string gBin = std::string("g = ") + std::to_string(g);
0079           std::string lBin =
0080               std::string("l = ") +
0081               localToString<decltype(gridXY)::index_t, decltype(gridXY)::DIM>(
0082                   l);
0083           std::vector<std::string> glBin = {gBin, lBin};
0084           std::string gBinID = "g_" + std::to_string(g);
0085           targets.push_back(
0086               actsvg::draw::text(gBinID,
0087                                  {static_cast<actsvg::scalar>(xp),
0088                                   static_cast<actsvg::scalar>(yp)},
0089                                  glBin));
0090         }
0091       }
0092     }
0093   }
0094   pGrid._connections = targets;
0095 
0096   auto oGrid = actsvg::display::grid("BoundGridXY", pGrid);
0097 
0098   // Add some labelling
0099   actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
0100   actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
0101   actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
0102 
0103   auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
0104                                    actsvg::style::marker({""}), axis_marker);
0105   auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"x"}, axis_font);
0106   auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 220}, axis_stroke,
0107                                    actsvg::style::marker({""}), axis_marker);
0108   auto yLabel = actsvg::draw::text("y_label", {-4, 232}, {"y"}, axis_font);
0109 
0110   std::vector<std::string> captionText = {
0111       "Binning schema for global and local bins: ",
0112       "- axis 0 : AxisBoundaryType::Bound, (-200., 200, 4), binX",
0113       "- axis 1 : AxisBoundaryType::Bound, (-200, 200, 6), binY"};
0114 
0115   auto caption = actsvg::draw::text("caption", {-180, -220}, captionText);
0116 
0117   oGrid.add_object(xAxis);
0118   oGrid.add_object(xLabel);
0119   oGrid.add_object(yAxis);
0120   oGrid.add_object(yLabel);
0121   oGrid.add_object(caption);
0122 
0123   Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
0124 }
0125 
0126 BOOST_AUTO_TEST_CASE(OpenGridXY) {
0127   using GlobalBin = std::size_t;
0128   using LocalBin = std::array<std::size_t, 2u>;
0129 
0130   // x-y axis and grid
0131   Axis<AxisType::Equidistant, AxisBoundaryType::Open> axisX(-200., 200, 4);
0132   Axis<AxisType::Equidistant, AxisBoundaryType::Open> axisY(-200, 200, 6);
0133   Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisX), decltype(axisY)>
0134       gridXY({axisX, axisY});
0135 
0136   Svg::GridConverter::Options cOptions;
0137   auto pGrid = Svg::GridConverter::convert(gridXY, {binX, binY}, cOptions);
0138   BOOST_CHECK_EQUAL(pGrid._type, actsvg::proto::grid::type::e_x_y);
0139 
0140   // Labelling the grid tiles
0141   auto edgesX = axisX.getBinEdges();
0142   auto edgesY = axisY.getBinEdges();
0143 
0144   std::vector<actsvg::svg::object> targets = {};
0145   std::size_t ig = 0;
0146   for (auto [ix, x] : Acts::enumerate(edgesX)) {
0147     if (ix > 0u) {
0148       ActsScalar xp = 0.2 * edgesX[ix] + 0.8 * edgesX[ix - 1u];
0149       for (auto [iy, y] : Acts::enumerate(edgesY)) {
0150         if (iy > 0u) {
0151           ActsScalar yp = 0.8 * edgesY[iy] + 0.2 * edgesY[iy - 1u];
0152           decltype(gridXY)::point_t p = {xp, yp};
0153           // Get local and global index
0154           auto g = gridXY.globalBinFromPosition(p);
0155           auto l = gridXY.localBinsFromPosition(p);
0156           std::string gBin = std::string("g = ") + std::to_string(g);
0157           std::string lBin =
0158               std::string("l = ") +
0159               localToString<decltype(gridXY)::index_t, decltype(gridXY)::DIM>(
0160                   l);
0161           std::vector<std::string> glBin = {gBin, lBin};
0162           std::string gBinID = "g_" + std::to_string(ig++);
0163           targets.push_back(
0164               actsvg::draw::text(gBinID,
0165                                  {static_cast<actsvg::scalar>(xp),
0166                                   static_cast<actsvg::scalar>(yp)},
0167                                  glBin));
0168         }
0169       }
0170     }
0171   }
0172   pGrid._connections = targets;
0173 
0174   // Add some labelling
0175   actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
0176   actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
0177   actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
0178 
0179   auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
0180                                    actsvg::style::marker({""}), axis_marker);
0181   auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"x"}, axis_font);
0182   auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 220}, axis_stroke,
0183                                    actsvg::style::marker({""}), axis_marker);
0184   auto yLabel = actsvg::draw::text("y_label", {-4, 232}, {"y"}, axis_font);
0185 
0186   std::vector<std::string> captionText = {
0187       "Binning schema for global and local bins: ",
0188       "- axis 0 : AxisBoundaryType::Open, (-200., 200, 4), binX",
0189       "- axis 1 : AxisBoundaryType::Open, (-200, 200, 6), binY"};
0190 
0191   auto caption = actsvg::draw::text("caption", {-180, -220}, captionText);
0192   auto oGrid = actsvg::display::grid("OpenGridXY", pGrid);
0193 
0194   oGrid.add_object(xAxis);
0195   oGrid.add_object(xLabel);
0196   oGrid.add_object(yAxis);
0197   oGrid.add_object(yLabel);
0198   oGrid.add_object(caption);
0199 
0200   Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
0201 }
0202 
0203 BOOST_AUTO_TEST_CASE(ClosedCylinderGridZPhi) {
0204   using GlobalBin = std::size_t;
0205   using LocalBin = std::array<std::size_t, 2u>;
0206 
0207   // z-phi Axes & Grid
0208   Axis<AxisType::Equidistant, AxisBoundaryType::Bound> axisZ(-200., 200., 3);
0209   Axis<AxisType::Equidistant, AxisBoundaryType::Closed> axisPhi(-M_PI, M_PI, 6);
0210   Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisZ), decltype(axisPhi)>
0211       gridZPhi({axisZ, axisPhi});
0212 
0213   Svg::GridConverter::Options cOptions;
0214   auto pGrid = Svg::GridConverter::convert(gridZPhi, {binZ, binPhi}, cOptions);
0215   BOOST_CHECK_EQUAL(pGrid._type, actsvg::proto::grid::type::e_z_phi);
0216 
0217   pGrid._reference_r = 80.;
0218 
0219   // Labelling the grid tiles
0220   auto edgesZ = axisZ.getBinEdges();
0221   auto edgesPhi = axisPhi.getBinEdges();
0222 
0223   std::vector<actsvg::svg::object> targets = {};
0224   std::size_t ig = 0;
0225   for (auto [iz, z] : Acts::enumerate(edgesZ)) {
0226     if (iz > 0u) {
0227       ActsScalar zp = 0.2 * edgesZ[iz] + 0.8 * edgesZ[iz - 1u];
0228       for (auto [iphi, phi] : Acts::enumerate(edgesPhi)) {
0229         if (iphi > 0u) {
0230           ActsScalar phip = 0.8 * edgesPhi[iphi] + 0.2 * edgesPhi[iphi - 1u];
0231           decltype(gridZPhi)::point_t p = {zp, phip};
0232           // Get local and global index
0233           auto g = gridZPhi.globalBinFromPosition(p);
0234           auto l = gridZPhi.localBinsFromPosition(p);
0235           std::string gBin = std::string("g = ") + std::to_string(g);
0236           std::string lBin =
0237               std::string("l = ") + localToString<decltype(gridZPhi)::index_t,
0238                                                   decltype(gridZPhi)::DIM>(l);
0239           std::vector<std::string> glBin = {gBin, lBin};
0240           std::string gBinID = "g_" + std::to_string(ig++);
0241           targets.push_back(actsvg::draw::text(
0242               gBinID,
0243               {static_cast<actsvg::scalar>(zp),
0244                static_cast<actsvg::scalar>(pGrid._reference_r * phip)},
0245               glBin));
0246         }
0247       }
0248     }
0249   }
0250   pGrid._connections = targets;
0251 
0252   // Add some labelling
0253   actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
0254   actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
0255   actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
0256 
0257   auto xAxis = actsvg::draw::arrow("x_axis", {0, 0}, {220, 0}, axis_stroke,
0258                                    actsvg::style::marker({""}), axis_marker);
0259   auto xLabel = actsvg::draw::text("x_label", {230, -4}, {"z"}, axis_font);
0260   auto yAxis = actsvg::draw::arrow("y_axis", {0, 0}, {0, 260}, axis_stroke,
0261                                    actsvg::style::marker({""}), axis_marker);
0262   auto yLabel = actsvg::draw::text("y_label", {-4, 270}, {"phi"}, axis_font);
0263 
0264   std::vector<std::string> captionText = {
0265       "Binning schema for global and local bins: ",
0266       "- axis 0 : AxisBoundaryType::Bound, (-200., 200, 3), binZ",
0267       "- axis 1 : AxisBoundaryType::Closed, (-PI, PI, 6), binPhi",
0268       "- draw reference radius set to 80"};
0269 
0270   auto caption = actsvg::draw::text("caption", {-180, -270}, captionText);
0271   auto oGrid = actsvg::display::grid("ClosedCylinderGridZPhi", pGrid);
0272 
0273   oGrid.add_object(xAxis);
0274   oGrid.add_object(xLabel);
0275   oGrid.add_object(yAxis);
0276   oGrid.add_object(yLabel);
0277   oGrid.add_object(caption);
0278 
0279   Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
0280 }
0281 
0282 BOOST_AUTO_TEST_CASE(ClosedDiscGridRPhi) {
0283   using GlobalBin = std::size_t;
0284   using LocalBin = std::array<std::size_t, 2u>;
0285 
0286   // r-phi Axes & Grid
0287   Axis<AxisType::Equidistant, AxisBoundaryType::Bound> axisR(100., 400., 3);
0288   Axis<AxisType::Equidistant, AxisBoundaryType::Closed> axisPhi(-M_PI, M_PI, 4);
0289   Grid<std::tuple<GlobalBin, LocalBin>, decltype(axisR), decltype(axisPhi)>
0290       gridRPhi({axisR, axisPhi});
0291 
0292   Svg::GridConverter::Options cOptions;
0293   auto pGrid = Svg::GridConverter::convert(gridRPhi, {binR, binPhi}, cOptions);
0294   BOOST_CHECK_EQUAL(pGrid._type, actsvg::proto::grid::type::e_r_phi);
0295 
0296   // Labelling the grid tiles
0297   auto edgesR = axisR.getBinEdges();
0298   auto edgesPhi = axisPhi.getBinEdges();
0299 
0300   std::vector<actsvg::svg::object> targets = {};
0301   std::size_t ig = 0;
0302   for (auto [ir, r] : Acts::enumerate(edgesR)) {
0303     if (ir > 0u) {
0304       ActsScalar rp = 0.5 * (edgesR[ir] + edgesR[ir - 1u]);
0305       for (auto [iphi, phi] : Acts::enumerate(edgesPhi)) {
0306         if (iphi > 0u) {
0307           ActsScalar phip = 0.5 * (edgesPhi[iphi] + edgesPhi[iphi - 1u]);
0308           decltype(gridRPhi)::point_t p = {rp, phip};
0309           // Get local and global index
0310           auto g = gridRPhi.globalBinFromPosition(p);
0311           auto l = gridRPhi.localBinsFromPosition(p);
0312           std::string gBin = std::string("g = ") + std::to_string(g);
0313           std::string lBin =
0314               std::string("l = ") + localToString<decltype(gridRPhi)::index_t,
0315                                                   decltype(gridRPhi)::DIM>(l);
0316           std::vector<std::string> glBin = {gBin, lBin};
0317           std::string gBinID = "g_" + std::to_string(ig++);
0318           targets.push_back(
0319               actsvg::draw::text(gBinID,
0320                                  {static_cast<actsvg::scalar>(rp * cos(phip)),
0321                                   static_cast<actsvg::scalar>(rp * sin(phip))},
0322                                  glBin));
0323         }
0324       }
0325     }
0326   }
0327   pGrid._connections = targets;
0328 
0329   // Add some labelling
0330   actsvg::style::stroke axis_stroke{{{0, 0, 255}}, 3};
0331   actsvg::style::marker axis_marker{{"<"}, 4, {{{0, 0, 255}}}, axis_stroke};
0332   actsvg::style::font axis_font{{{0, 0, 255}}, "Andale Mono", 16};
0333 
0334   auto rAxis = actsvg::draw::arrow("r_axis", {0, 0}, {420, 0}, axis_stroke,
0335                                    actsvg::style::marker({""}), axis_marker);
0336   auto rLabel = actsvg::draw::text("r_label", {440, -4}, {"r"}, axis_font);
0337 
0338   auto phiAxis = actsvg::draw::arc_measure(
0339       "phi_axis", 410., {410, 0.},
0340       {static_cast<actsvg::scalar>(410. * cos(0.25)),
0341        static_cast<actsvg::scalar>(410. * sin(0.25))},
0342       axis_stroke, actsvg::style::marker(), axis_marker);
0343 
0344   auto phiLabel =
0345       actsvg::draw::text("phi_label", {410, 60}, {"phi"}, axis_font);
0346 
0347   std::vector<std::string> captionText = {
0348       "Binning schema for global and local bins: ",
0349       "- axis 0 : AxisBoundaryType::Bound, (100., 400, 3), binR",
0350       "- axis 1 : AxisBoundaryType::Closed, (-PI, PI, 4), binPhi"};
0351 
0352   auto caption = actsvg::draw::text("caption", {-180, -420}, captionText);
0353   auto oGrid = actsvg::display::grid("ClosedDiscGridRPhi", pGrid);
0354 
0355   oGrid.add_object(rAxis);
0356   oGrid.add_object(rLabel);
0357   oGrid.add_object(phiAxis);
0358   oGrid.add_object(phiLabel);
0359   oGrid.add_object(caption);
0360 
0361   Acts::Svg::toFile({oGrid}, oGrid._id + ".svg");
0362 }
0363 
0364 BOOST_AUTO_TEST_SUITE_END()