Back to home page

EIC code displayed by LXR

 
 

    


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

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/test/validation/detector_scanner.hpp"
0011 
0012 #include "detray/definitions/units.hpp"
0013 #include "detray/tracks/tracks.hpp"
0014 #include "detray/tracks/trajectories.hpp"
0015 
0016 // Detray test include(s)
0017 #include "detray/test/common/build_toy_detector.hpp"
0018 #include "detray/test/common/track_generators.hpp"
0019 #include "detray/test/framework/types.hpp"
0020 
0021 // VecMem include(s).
0022 #include <vecmem/memory/host_memory_resource.hpp>
0023 
0024 // GTest include(s).
0025 #include <gtest/gtest.h>
0026 
0027 using namespace detray;
0028 
0029 using test_algebra = test::algebra;
0030 using scalar = test::scalar;
0031 using vector3 = test::vector3;
0032 
0033 constexpr const scalar tol{1e-7f};
0034 
0035 /// Brute force test: Intersect toy geometry and compare between ray and helix
0036 /// without B-field
0037 GTEST_TEST(detray_simulation, detector_scanner) {
0038   // Simulate straight line track
0039   const vector3 B{0.f * unit<scalar>::T, 0.f * unit<scalar>::T,
0040                   tol * unit<scalar>::T};
0041 
0042   // Build the geometry
0043   vecmem::host_memory_resource host_mr;
0044   auto [toy_det, names] = build_toy_detector<test_algebra>(host_mr);
0045 
0046   unsigned int theta_steps{50u};
0047   unsigned int phi_steps{50u};
0048 
0049   // Record ray tracing
0050   using detector_t = decltype(toy_det);
0051   using intersection_trace_t = typename detray::ray_scan<
0052       test_algebra>::template intersection_trace_type<detector_t>;
0053 
0054   detector_t::geometry_context gctx{};
0055 
0056   std::vector<intersection_trace_t> expected;
0057 
0058   //  Iterate through uniformly distributed momentum directions with ray
0059   for (const auto test_ray : uniform_track_generator<detail::ray<test_algebra>>(
0060            phi_steps, theta_steps)) {
0061     // Record all intersections and objects along the ray
0062     const auto intersection_record =
0063         detector_scanner::run<ray_scan>(gctx, toy_det, test_ray);
0064 
0065     expected.push_back(intersection_record);
0066   }
0067 
0068   // Iterate through uniformly distributed momentum directions with helix
0069   std::size_t n_tracks{0u};
0070   for (const auto track :
0071        uniform_track_generator<free_track_parameters<test_algebra>>(
0072            phi_steps, theta_steps)) {
0073     const detail::helix test_helix(track, B);
0074 
0075     // Record all intersections and objects along the ray
0076     const auto intersection_trace =
0077         detector_scanner::run<helix_scan>(gctx, toy_det, test_helix);
0078 
0079     // Should have encountered the same number of tracks (vulnerable to
0080     // floating point errors)
0081     EXPECT_EQ(expected[n_tracks].size(), intersection_trace.size())
0082         << "track: " << n_tracks << "\n"
0083         << test_helix;
0084 
0085     // Feedback if not the same number of intersections was found
0086     const std::size_t n_ray_inters{expected[n_tracks].size()};
0087     const std::size_t n_helix_inters{intersection_trace.size()};
0088     EXPECT_EQ(n_ray_inters, n_helix_inters)
0089         << "track: " << n_tracks << "\n"
0090         << "Ray scan: " << n_ray_inters << ", helix scan: " << n_helix_inters
0091         << std::endl;
0092 
0093     // Check every single recorded intersection
0094     for (std::size_t i = 0u; i < std::min(n_ray_inters, n_helix_inters); ++i) {
0095       if (expected[n_tracks][i].vol_idx != intersection_trace[i].vol_idx) {
0096         // Intersection record at portal bound might be flipped
0097         // (the portals overlap completely)
0098         if (expected[n_tracks][i].vol_idx ==
0099                 intersection_trace[i + 1u].vol_idx &&
0100             expected[n_tracks][i + 1u].vol_idx ==
0101                 intersection_trace[i].vol_idx) {
0102           // Have already checked the next record
0103           ++i;
0104           continue;
0105         }
0106       }
0107       EXPECT_EQ(expected[n_tracks][i].vol_idx, intersection_trace[i].vol_idx)
0108           << "track: " << n_tracks << ", intersection: " << i
0109           << "\n - ray: " << expected[n_tracks][i].intersection
0110           << "\n - helix: " << intersection_trace[i].intersection;
0111     }
0112 
0113     ++n_tracks;
0114   }
0115 }