Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:11

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