File indexing completed on 2026-10-29 08:25:01
0001
0002
0003
0004
0005
0006 #include <algorithms/geo.h>
0007
0008 #include <catch2/catch_test_macros.hpp>
0009 #include <catch2/matchers/catch_matchers_string.hpp>
0010
0011 #include <DD4hep/Detector.h>
0012
0013 #include <algorithm>
0014 #include <memory>
0015 #include <string>
0016 #include <string_view>
0017 #include <vector>
0018
0019 using namespace algorithms;
0020
0021 namespace {
0022 const std::string kCompactFile{TEST_DATA_DIR "/minimal.xml"};
0023
0024 std::vector<std::string>& messages() {
0025 static std::vector<std::string> m;
0026 return m;
0027 }
0028
0029 void capture() {
0030 messages().clear();
0031 LogSvc::instance().init([](LogLevel, std::string_view, std::string_view msg) {
0032 messages().emplace_back(msg);
0033 });
0034 }
0035 }
0036
0037 TEST_CASE("GeoSvc loads compact files in standalone mode", "[geo]") {
0038 auto& svc = GeoSvc::instance();
0039 svc.setProperty("detectors", std::vector<std::string>{kCompactFile});
0040 svc.init();
0041
0042 CHECK(svc.world().isValid());
0043 CHECK(svc.world().name() == std::string{"world"});
0044 CHECK(svc.detector()->world().ptr() == svc.world().ptr());
0045 CHECK(svc.cellIDPositionConverter().get() != nullptr);
0046 }
0047
0048 TEST_CASE("GeoSvc uses a pre-initialized detector", "[geo]") {
0049
0050 static const auto detector = [] {
0051 auto det = dd4hep::Detector::make_unique("preinitialized");
0052 det->fromCompact(kCompactFile);
0053 return det;
0054 }();
0055
0056 auto& svc = GeoSvc::instance();
0057 svc.init(detector.get());
0058 const auto* converter = svc.cellIDPositionConverter().get();
0059
0060 CHECK(svc.detector() == detector.get());
0061 CHECK(svc.world().ptr() == detector->world().ptr());
0062 CHECK(converter != nullptr);
0063
0064
0065 svc.init(detector.get());
0066 CHECK(svc.cellIDPositionConverter().get() != converter);
0067 }
0068
0069 TEST_CASE("GeoSvc requires compact files in standalone mode", "[geo]") {
0070 capture();
0071 auto& svc = GeoSvc::instance();
0072 svc.setProperty("detectors", std::vector<std::string>{});
0073
0074 CHECK_THROWS_AS(svc.init(), ServiceError);
0075 CHECK_THROWS_WITH(svc.init(), "No external detector provided and no compact files specified "
0076 "in the 'detectors' property");
0077 }
0078
0079 TEST_CASE("GeoSvc logs the compact files it loads", "[geo]") {
0080 capture();
0081 auto& svc = GeoSvc::instance();
0082 svc.setProperty("detectors", std::vector<std::string>{kCompactFile});
0083 svc.init();
0084
0085 CHECK(std::find(messages().begin(), messages().end(), "Loading compact file: " + kCompactFile) !=
0086 messages().end());
0087 }