Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:06

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