Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 07:59:47

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/Definitions/Algebra.hpp"
0012 #include "Acts/Detector/Detector.hpp"
0013 #include "Acts/Detector/DetectorBuilder.hpp"
0014 #include "Acts/Detector/DetectorComponents.hpp"
0015 #include "Acts/Detector/DetectorVolume.hpp"
0016 #include "Acts/Detector/PortalGenerators.hpp"
0017 #include "Acts/Detector/interface/IDetectorComponentBuilder.hpp"
0018 #include "Acts/Detector/interface/IGeometryIdGenerator.hpp"
0019 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0020 #include "Acts/Geometry/GeometryContext.hpp"
0021 #include "Acts/Geometry/GeometryIdentifier.hpp"
0022 #include "Acts/Navigation/DetectorVolumeFinders.hpp"
0023 #include "Acts/Navigation/InternalNavigation.hpp"
0024 #include "Acts/Surfaces/PlaneSurface.hpp"
0025 #include "Acts/Surfaces/RectangleBounds.hpp"
0026 #include "Acts/Surfaces/Surface.hpp"
0027 #include "Acts/Utilities/Enumerate.hpp"
0028 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0029 
0030 #include <memory>
0031 #include <stdexcept>
0032 #include <vector>
0033 
0034 using namespace Acts;
0035 
0036 namespace ActsTests {
0037 
0038 class CompBuilder final : public Experimental::IDetectorComponentBuilder {
0039  public:
0040   explicit CompBuilder(
0041       const std::vector<std::shared_ptr<Surface>>& sensitives = {})
0042       : m_sensitives(sensitives) {}
0043 
0044   Experimental::DetectorComponent construct(
0045       const GeometryContext& gctx) const final {
0046     auto bounds = std::make_unique<CuboidVolumeBounds>(10., 10., 10.);
0047     // Construct the DetectorVolume
0048     auto dVolume = m_sensitives.empty()
0049                        ? Experimental::DetectorVolumeFactory::construct(
0050                              Experimental::defaultPortalGenerator(), gctx,
0051                              "TestVolume", Transform3::Identity(),
0052                              std::move(bounds), Experimental::tryAllPortals())
0053                        : Experimental::DetectorVolumeFactory::construct(
0054                              Experimental::defaultPortalGenerator(), gctx,
0055                              "TestVolumeWithSurfaces", Transform3::Identity(),
0056                              std::move(bounds), m_sensitives, {},
0057                              Experimental::tryNoVolumes(),
0058                              Experimental::tryAllPortalsAndSurfaces());
0059 
0060     // Fill the portal container
0061     Experimental::DetectorComponent::PortalContainer portalContainer;
0062     for (auto [ip, p] : enumerate(dVolume->portalPtrs())) {
0063       portalContainer[ip] = p;
0064     }
0065 
0066     auto geoID = GeometryIdentifier().withVolume(1);
0067     dVolume->assignGeometryId(geoID);
0068 
0069     return Experimental::DetectorComponent{
0070         {dVolume},
0071         portalContainer,
0072         {{dVolume}, Experimental::tryRootVolumes()}};
0073   }
0074 
0075  private:
0076   std::vector<std::shared_ptr<Surface>> m_sensitives;
0077 };
0078 
0079 class SurfaceGeoIdGenerator : public Experimental::IGeometryIdGenerator {
0080  public:
0081   Experimental::IGeometryIdGenerator::GeoIdCache generateCache() const final {
0082     return std::any();
0083   }
0084 
0085   void assignGeometryId(
0086       Experimental::IGeometryIdGenerator::GeoIdCache& /*cache*/,
0087       Experimental::DetectorVolume& dVolume) const final {
0088     for (auto [is, s] : enumerate(dVolume.surfacePtrs())) {
0089       auto geoID = GeometryIdentifier().withSensitive(is + 1);
0090       s->assignGeometryId(geoID);
0091     }
0092   }
0093 
0094   void assignGeometryId(
0095       Experimental::IGeometryIdGenerator::GeoIdCache& /*cache*/,
0096       Experimental::Portal& /*portal*/) const final {}
0097 
0098   void assignGeometryId(
0099       Experimental::IGeometryIdGenerator::GeoIdCache& /*cache*/,
0100       Surface& /*surface*/) const final {}
0101 };
0102 
0103 GeometryContext tContext;
0104 
0105 BOOST_AUTO_TEST_SUITE(DetectorSuite)
0106 
0107 BOOST_AUTO_TEST_CASE(DetectorBuilder_Misconfigured) {
0108   // Detector builder
0109   Experimental::DetectorBuilder::Config dCfg;
0110   dCfg.auxiliary = "*** Test X * Misconfigued ***";
0111   dCfg.name = "EmptyCylinder";
0112   dCfg.builder = nullptr;
0113 
0114   BOOST_CHECK_THROW(auto a = Experimental::DetectorBuilder(dCfg),
0115                     std::invalid_argument);
0116 
0117   // Detector builder with sensitives but no assigned geometry ids to them
0118   ActsTests::DetectorElementStub detElement0(
0119       Transform3::Identity(), std::make_shared<RectangleBounds>(5., 5.), 0.1);
0120   ActsTests::DetectorElementStub detElement1(
0121       Transform3::Identity(), std::make_shared<RectangleBounds>(5., 5.), 0.1);
0122 
0123   std::vector<std::shared_ptr<Surface>> sensitives;
0124   sensitives.push_back(detElement0.surface().getSharedPtr());
0125   sensitives.push_back(detElement1.surface().getSharedPtr());
0126   dCfg.builder = std::make_shared<CompBuilder>(sensitives);
0127 
0128   BOOST_CHECK_THROW(Experimental::DetectorBuilder(dCfg).construct(tContext),
0129                     std::invalid_argument);
0130 }
0131 
0132 BOOST_AUTO_TEST_CASE(DetectorBuilder_test) {
0133   // Detector builder
0134   Experimental::DetectorBuilder::Config dCfg;
0135   dCfg.auxiliary = "*** Test : Detector ***";
0136   dCfg.name = "TestDetector";
0137   dCfg.builder = std::make_shared<CompBuilder>();
0138 
0139   BOOST_CHECK_NO_THROW(auto a = Experimental::DetectorBuilder(dCfg));
0140 
0141   auto detector = Experimental::DetectorBuilder(dCfg).construct(tContext);
0142 
0143   BOOST_CHECK_EQUAL(detector->name(), "TestDetector");
0144   BOOST_CHECK_EQUAL(detector->rootVolumes().size(), 1);
0145 }
0146 
0147 BOOST_AUTO_TEST_CASE(DetectorBuilder_testWithSurfaces) {
0148   // Detector builder
0149   Experimental::DetectorBuilder::Config dCfg;
0150   dCfg.auxiliary = "*** Test : Detector ***";
0151   dCfg.name = "TestDetectorWithSurfaces";
0152   dCfg.builder = std::make_shared<CompBuilder>();
0153 
0154   // Test detector with surfaces
0155   ActsTests::DetectorElementStub detElement0(
0156       Transform3::Identity(), std::make_shared<RectangleBounds>(5., 5.), 0.1);
0157   ActsTests::DetectorElementStub detElement1(
0158       Transform3::Identity(), std::make_shared<RectangleBounds>(5., 5.), 0.1);
0159 
0160   std::vector<std::shared_ptr<Surface>> sensitives;
0161   sensitives.push_back(detElement0.surface().getSharedPtr());
0162   sensitives.push_back(detElement1.surface().getSharedPtr());
0163   dCfg.builder = std::make_shared<CompBuilder>(sensitives);
0164   dCfg.geoIdGenerator = std::make_shared<SurfaceGeoIdGenerator>();
0165 
0166   auto detector = Experimental::DetectorBuilder(dCfg).construct(tContext);
0167   BOOST_CHECK_EQUAL(detector->name(), "TestDetectorWithSurfaces");
0168   BOOST_CHECK_EQUAL(
0169       detector->volumes()[0]->surfaces()[0]->geometryId().sensitive(), 1);
0170   BOOST_CHECK_EQUAL(
0171       detector->volumes()[0]->surfaces()[1]->geometryId().sensitive(), 2);
0172 }
0173 
0174 BOOST_AUTO_TEST_SUITE_END()
0175 
0176 }  // namespace ActsTests