Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-04 09: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 #include <boost/test/unit_test.hpp>
0010 #include <boost/test/unit_test_suite.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "Acts/Surfaces/PlaneSurface.hpp"
0016 #include "Acts/Surfaces/RectangleBounds.hpp"
0017 #include "Acts/Utilities/Zip.hpp"
0018 #include "ActsPlugins/Json/JsonSurfacesReader.hpp"
0019 #include "ActsPlugins/Json/SurfaceJsonConverter.hpp"
0020 
0021 #include <filesystem>
0022 #include <fstream>
0023 #include <memory>
0024 
0025 #include <Eigen/Geometry>
0026 #include <nlohmann/json.hpp>
0027 
0028 using namespace Acts;
0029 
0030 const std::vector<std::shared_ptr<Surface>> surfaces = []() {
0031   std::vector<std::shared_ptr<Surface>> v;
0032 
0033   for (int i = 0; i < 3; ++i) {
0034     auto bounds = std::make_shared<RectangleBounds>(1.0, 1.0);
0035     Transform3 transform = Transform3::Identity();
0036     transform.translate(Vector3::Random());
0037     Vector3 randomAngles = Vector3::Random();
0038     SquareMatrix3 rotMatrix;
0039     rotMatrix = Eigen::AngleAxis(randomAngles[0], Vector3::UnitX()) *
0040                 Eigen::AngleAxis(randomAngles[1], Vector3::UnitY()) *
0041                 Eigen::AngleAxis(randomAngles[2], Vector3::UnitZ());
0042     transform.rotate(rotMatrix);
0043     v.push_back(Surface::makeShared<PlaneSurface>(transform, bounds));
0044   }
0045 
0046   return v;
0047 }();
0048 
0049 const std::string filename = "json_surfaces_reader_tests.json";
0050 
0051 struct FileFixture {
0052   FileFixture() {
0053     nlohmann::json js = nlohmann::json::array();
0054 
0055     for (const auto &s : surfaces) {
0056       js.push_back(SurfaceJsonConverter::toJson({}, *s));
0057     }
0058 
0059     nlohmann::json j;
0060     j["foo"] = js;
0061 
0062     std::ofstream f(filename);
0063     f << j.dump(2);
0064   }
0065 
0066   ~FileFixture() { std::filesystem::remove(filename); }
0067 };
0068 
0069 FileFixture fileFixture;
0070 
0071 namespace ActsTests {
0072 
0073 BOOST_AUTO_TEST_SUITE(JsonSuite)
0074 
0075 BOOST_AUTO_TEST_CASE(surface_reading_test) {
0076   auto readBackSurfaces = JsonSurfacesReader::readVector({filename, {"foo"}});
0077 
0078   BOOST_REQUIRE_EQUAL(surfaces.size(), readBackSurfaces.size());
0079   for (auto [refSurface, surface] : zip(surfaces, readBackSurfaces)) {
0080     BOOST_CHECK(
0081         refSurface->transform({}).isApprox(surface->transform({}), 1.e-4));
0082     BOOST_CHECK(refSurface->center({}).isApprox(surface->center({}), 1.e-4));
0083     BOOST_CHECK_EQUAL(refSurface->type(), surface->type());
0084     BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type());
0085   }
0086 }
0087 
0088 BOOST_AUTO_TEST_CASE(json_detelement_reading_test) {
0089   auto readBackDetElements =
0090       JsonSurfacesReader::readDetectorElements({filename, {"foo"}}, 1.0);
0091 
0092   BOOST_REQUIRE_EQUAL(surfaces.size(), readBackDetElements.size());
0093   for (auto [refSurface, detElement] : zip(surfaces, readBackDetElements)) {
0094     auto surface = &detElement->surface();
0095     BOOST_CHECK(
0096         refSurface->transform({}).isApprox(surface->transform({}), 1.e-4));
0097     BOOST_CHECK(refSurface->center({}).isApprox(surface->center({}), 1.e-4));
0098     BOOST_CHECK_EQUAL(refSurface->type(), surface->type());
0099     BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type());
0100   }
0101 }
0102 
0103 BOOST_AUTO_TEST_SUITE_END()
0104 
0105 }  // namespace ActsTests