Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:26

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 // Project include(s)
0010 #include "detray/core/detector.hpp"
0011 #include "detray/tracks/trajectories.hpp"
0012 
0013 // Detray plugin include(s)
0014 #include "detray/plugins/svgtools/illustrator.hpp"
0015 #include "detray/plugins/svgtools/utils/groups.hpp"
0016 #include "detray/plugins/svgtools/writer.hpp"
0017 
0018 // Detray test include(s)
0019 #include "detray/test/common/build_toy_detector.hpp"
0020 #include "detray/test/framework/types.hpp"
0021 #include "detray/test/validation/detector_scanner.hpp"
0022 #include "detray/test/validation/svg_display.hpp"
0023 
0024 // Vecmem include(s)
0025 #include <vecmem/memory/host_memory_resource.hpp>
0026 
0027 // Actsvg include(s)
0028 #include <actsvg/core.hpp>
0029 #include <actsvg/web/web_builder.hpp>
0030 
0031 // GTest include(s).
0032 #include <gtest/gtest.h>
0033 
0034 // System include(s)
0035 #include <array>
0036 #include <filesystem>
0037 #include <string>
0038 
0039 GTEST_TEST(svgtools, web) {
0040   // In this test we will create a web page to show the detector geometry and
0041   // more. We will start by creating the svgs we want to include in the web
0042   // page.
0043 
0044   // Axes.
0045   const auto axes = actsvg::draw::x_y_axes("axes", {-250, 250}, {-250, 250},
0046                                            actsvg::style::stroke());
0047 
0048   // Creating the views.
0049   const actsvg::views::x_y view;
0050 
0051   // Creating the detector and geomentry context.
0052   vecmem::host_memory_resource host_mr;
0053   const auto [det, names] =
0054       detray::build_toy_detector<detray::test::algebra>(host_mr);
0055   using detector_t = decltype(det);
0056 
0057   using test_algebra = typename detector_t::algebra_type;
0058   using scalar = detray::dscalar<test_algebra>;
0059   using vector3 = detray::dvector3D<test_algebra>;
0060 
0061   detector_t::geometry_context gctx{};
0062 
0063   // Creating the svg generator for the detector.
0064   const detray::svgtools::illustrator il{det, names};
0065 
0066   // The vector of svgs that we want to include on the webpage.
0067   std::vector<actsvg::svg::object> svgs;
0068 
0069   // Indexes of the volumes in the detector to be visualized.
0070   std::array indices{0u,  1u,  2u,  3u,  4u,  5u,  6u,  7u,  8u,  9u,
0071                      10u, 11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u, 19u};
0072 
0073   // Draw the volumes and include them in the svg vector.
0074   for (detray::dindex i : indices) {
0075     const auto [svg, _] = il.draw_volume(i, view);
0076     svgs.push_back(svg);
0077   }
0078 
0079   // Draw some example trajectories and include them in the svg vector (along
0080   // with their intersections).
0081   for (const auto qop : std::vector{-4, -8, -16}) {
0082     std::string name = "Helix_qop_" + std::to_string(qop) + ")";
0083 
0084     const typename detector_t::point3_type ori{0.f, 0.f, 80.f};
0085     const typename detector_t::point3_type dir{0.f, 1.f, 1.f};
0086 
0087     // Create the helix trajectory.
0088     // Constant magnetic field
0089     vector3 B{0.f * detray::unit<scalar>::T, 0.f * detray::unit<scalar>::T,
0090               1.f * detray::unit<scalar>::T};
0091 
0092     const detray::detail::helix<test_algebra> helix(
0093         ori, 0.f, detray::vector::normalize(dir), static_cast<float>(qop), B);
0094     const auto helix_ir =
0095         detray::detector_scanner::run<detray::helix_scan>(gctx, det, helix);
0096 
0097     // Draw the helix trajectory.
0098     const auto svg_helix =
0099         il.draw_trajectory(name + "_trajectory", helix, view);
0100 
0101     // Draw the intersection record.
0102     auto helix_intersections =
0103         detray::detail::transcribe_intersections(helix_ir);
0104     const auto svg_helix_ir = il.draw_intersections(
0105         name + "_record", helix_intersections, helix.dir(), view);
0106 
0107     // We one the trajectory and intersection record to be considered as one
0108     // svg. Thus we group them together before adding the group to the svg
0109     // vector.
0110     const auto svg_group = detray::svgtools::utils::group(
0111         name, std::vector{svg_helix, svg_helix_ir});
0112     svgs.push_back(svg_group);
0113   }
0114 
0115   // The output directory for the web page.
0116   const auto current_directory = std::filesystem::current_path();
0117 
0118   // Create the web page builder.
0119   actsvg::web::web_builder builder{};
0120 
0121   // To visualize the svg objects in a specific order we need to pass a
0122   // comparator before we build the page. For instance we might want to
0123   // display helices on top on the volumes (rather than the other way around).
0124   // In this example we will alphanumeric_compare which renders the svgs such
0125   // that the id of the svg object with the greatest id alphanumerically will
0126   // be displayed on top. Build the web page.
0127   auto alphanum_cmp = actsvg::web::compare::alphanumeric;
0128   builder.build(current_directory / "test_svgtools_website", svgs,
0129                 alphanum_cmp);
0130 
0131   // Once the direcroy has been created, run the server using "python3 -m
0132   // http.server" in the directory. Subsequently connect to localhost using
0133   // the respective port. On the web page, click and drag to move the view
0134   // box. Use the scroll wheel to zoom.
0135 }