Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-01 07:50:05

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/Geometry/GeometryContext.hpp"
0013 #include "Acts/Geometry/GeometryIdentifier.hpp"
0014 #include "Acts/Surfaces/ConeBounds.hpp"
0015 #include "Acts/Surfaces/ConeSurface.hpp"
0016 #include "Acts/Surfaces/CylinderBounds.hpp"
0017 #include "Acts/Surfaces/CylinderSurface.hpp"
0018 #include "Acts/Surfaces/DiamondBounds.hpp"
0019 #include "Acts/Surfaces/DiscSurface.hpp"
0020 #include "Acts/Surfaces/LineBounds.hpp"
0021 #include "Acts/Surfaces/PerigeeSurface.hpp"
0022 #include "Acts/Surfaces/PlaneSurface.hpp"
0023 #include "Acts/Surfaces/RadialBounds.hpp"
0024 #include "Acts/Surfaces/StrawSurface.hpp"
0025 #include "Acts/Surfaces/Surface.hpp"
0026 #include "Acts/Surfaces/SurfaceBounds.hpp"
0027 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0028 #include "ActsPlugins/Json/SurfaceJsonConverter.hpp"
0029 
0030 #include <fstream>
0031 #include <memory>
0032 #include <string>
0033 
0034 #include <nlohmann/json.hpp>
0035 
0036 using namespace Acts;
0037 
0038 namespace {
0039 std::ofstream out;
0040 
0041 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0042 }  // namespace
0043 
0044 namespace ActsTests {
0045 
0046 BOOST_AUTO_TEST_SUITE(JsonSuite)
0047 
0048 BOOST_AUTO_TEST_CASE(ConeSurfaceRoundTripTests) {
0049   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0050   auto cone = std::make_shared<ConeBounds>(0.123, 10., 100.);
0051   auto coneRef = Surface::makeShared<ConeSurface>(trf, cone);
0052   coneRef->assignGeometryId(GeometryIdentifier(13u));
0053 
0054   // Test a cone
0055   nlohmann::json coneOut = SurfaceJsonConverter::toJson(gctx, *coneRef);
0056   out.open("ConeSurface.json");
0057   out << coneOut.dump(2);
0058   out.close();
0059 
0060   auto in = std::ifstream("ConeSurface.json",
0061                           std::ifstream::in | std::ifstream::binary);
0062   BOOST_CHECK(in.good());
0063   nlohmann::json coneIn;
0064   in >> coneIn;
0065   in.close();
0066 
0067   auto coneTest = SurfaceJsonConverter::fromJson(coneIn);
0068 
0069   BOOST_CHECK(coneTest->localToGlobalTransform(gctx).isApprox(
0070       coneRef->localToGlobalTransform(gctx)));
0071   BOOST_CHECK_EQUAL(coneTest->geometryId(), coneRef->geometryId());
0072   BOOST_CHECK_EQUAL(coneTest->bounds(), coneRef->bounds());
0073 }
0074 
0075 BOOST_AUTO_TEST_CASE(DiscSurfaceRoundTripTests) {
0076   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0077   auto ring = std::make_shared<RadialBounds>(0., 4.);
0078   auto ringDiscRef = Surface::makeShared<DiscSurface>(trf, ring);
0079   ringDiscRef->assignGeometryId(GeometryIdentifier(10u));
0080 
0081   // Test a disc
0082   nlohmann::json discOut = SurfaceJsonConverter::toJson(gctx, *ringDiscRef);
0083   out.open("DiscSurface.json");
0084   out << discOut.dump(2);
0085   out.close();
0086 
0087   auto in = std::ifstream("DiscSurface.json",
0088                           std::ifstream::in | std::ifstream::binary);
0089   BOOST_CHECK(in.good());
0090   nlohmann::json discIn;
0091   in >> discIn;
0092   in.close();
0093 
0094   auto ringDiscTest = SurfaceJsonConverter::fromJson(discIn);
0095 
0096   BOOST_CHECK(ringDiscTest->localToGlobalTransform(gctx).isApprox(
0097       ringDiscRef->localToGlobalTransform(gctx)));
0098   BOOST_CHECK_EQUAL(ringDiscTest->geometryId(), ringDiscRef->geometryId());
0099   BOOST_CHECK_EQUAL(ringDiscTest->bounds(), ringDiscRef->bounds());
0100 }
0101 
0102 BOOST_AUTO_TEST_CASE(CylinderSurfaceRoundTripTests) {
0103   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0104   auto tube = std::make_shared<CylinderBounds>(5., 20.);
0105   auto cylinderRef = Surface::makeShared<CylinderSurface>(trf, tube);
0106   cylinderRef->assignGeometryId(GeometryIdentifier(11u));
0107 
0108   // Test a cyoinder
0109   nlohmann::json cylinderOut = SurfaceJsonConverter::toJson(gctx, *cylinderRef);
0110   out.open("CylinderSurface.json");
0111   out << cylinderOut.dump(2);
0112   out.close();
0113 
0114   auto in = std::ifstream("CylinderSurface.json",
0115                           std::ifstream::in | std::ifstream::binary);
0116   BOOST_CHECK(in.good());
0117   nlohmann::json cylinderIn;
0118   in >> cylinderIn;
0119   in.close();
0120 
0121   auto cylinderTest = SurfaceJsonConverter::fromJson(cylinderIn);
0122 
0123   BOOST_CHECK(cylinderTest->localToGlobalTransform(gctx).isApprox(
0124       cylinderRef->localToGlobalTransform(gctx)));
0125   BOOST_CHECK_EQUAL(cylinderTest->geometryId(), cylinderRef->geometryId());
0126   BOOST_CHECK_EQUAL(cylinderTest->bounds(), cylinderRef->bounds());
0127 }
0128 
0129 BOOST_AUTO_TEST_CASE(PlaneSurfaceRoundTripTests) {
0130   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0131   auto trapezoid = std::make_shared<TrapezoidBounds>(2., 3., 4.);
0132   auto trapezoidPlaneRef = Surface::makeShared<PlaneSurface>(trf, trapezoid);
0133   trapezoidPlaneRef->assignGeometryId(GeometryIdentifier(9u));
0134 
0135   // Test a plane
0136   nlohmann::json planeOut =
0137       SurfaceJsonConverter::toJson(gctx, *trapezoidPlaneRef);
0138   to_json(planeOut, *trapezoidPlaneRef);
0139   out.open("PlaneSurface.json");
0140   out << planeOut.dump(2);
0141   out.close();
0142 
0143   auto in = std::ifstream("PlaneSurface.json",
0144                           std::ifstream::in | std::ifstream::binary);
0145   BOOST_CHECK(in.good());
0146   nlohmann::json planeIn;
0147   in >> planeIn;
0148   in.close();
0149 
0150   auto trapezoidPlaneTest = SurfaceJsonConverter::fromJson(planeIn);
0151 
0152   BOOST_CHECK(trapezoidPlaneTest->localToGlobalTransform(gctx).isApprox(
0153       trapezoidPlaneRef->localToGlobalTransform(gctx)));
0154   BOOST_CHECK_EQUAL(trapezoidPlaneTest->geometryId(),
0155                     trapezoidPlaneRef->geometryId());
0156   BOOST_CHECK_EQUAL(trapezoidPlaneTest->bounds(), trapezoidPlaneRef->bounds());
0157 }
0158 
0159 BOOST_AUTO_TEST_CASE(StrawSurfaceRoundTripTests) {
0160   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0161   auto straw = std::make_shared<LineBounds>(1., 100.);
0162   auto strawRef = Surface::makeShared<StrawSurface>(trf, straw);
0163   strawRef->assignGeometryId(GeometryIdentifier(12u));
0164 
0165   // Test a straw
0166   nlohmann::json strawOut = SurfaceJsonConverter::toJson(gctx, *strawRef);
0167   out.open("StrawSurface.json");
0168   out << strawOut.dump(2);
0169   out.close();
0170 
0171   auto in = std::ifstream("StrawSurface.json",
0172                           std::ifstream::in | std::ifstream::binary);
0173   BOOST_CHECK(in.good());
0174   nlohmann::json strawIn;
0175   in >> strawIn;
0176   in.close();
0177 
0178   auto strawTest = SurfaceJsonConverter::fromJson(strawIn);
0179 
0180   BOOST_CHECK(strawTest->localToGlobalTransform(gctx).isApprox(
0181       strawRef->localToGlobalTransform(gctx)));
0182   BOOST_CHECK_EQUAL(strawTest->geometryId(), strawRef->geometryId());
0183   BOOST_CHECK_EQUAL(strawTest->bounds(), strawRef->bounds());
0184 }
0185 
0186 BOOST_AUTO_TEST_CASE(PerigeeRoundTripTests) {
0187   Transform3 trf(Transform3::Identity() * Translation3(-1., -2., -7.));
0188   auto perigeeRef = Surface::makeShared<PerigeeSurface>(trf);
0189   perigeeRef->assignGeometryId(GeometryIdentifier(99u));
0190 
0191   // Test a perigee
0192   nlohmann::json perigeeOut = SurfaceJsonConverter::toJson(gctx, *perigeeRef);
0193   out.open("PerigeeSurface.json");
0194   out << perigeeOut.dump(2);
0195   out.close();
0196 
0197   auto in = std::ifstream("PerigeeSurface.json",
0198                           std::ifstream::in | std::ifstream::binary);
0199   BOOST_CHECK(in.good());
0200   nlohmann::json perigeeIn;
0201   in >> perigeeIn;
0202   in.close();
0203 
0204   auto perigeeTest = SurfaceJsonConverter::fromJson(perigeeIn);
0205 
0206   BOOST_CHECK(perigeeTest->localToGlobalTransform(gctx).isApprox(
0207       perigeeRef->localToGlobalTransform(gctx)));
0208   BOOST_CHECK_EQUAL(perigeeTest->geometryId(), perigeeRef->geometryId());
0209 }
0210 
0211 BOOST_AUTO_TEST_CASE(DiamondPlaneSurfaceRoundTripTests) {
0212   Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
0213   auto diamond = std::make_shared<DiamondBounds>(1., 3., 2., 4., 5.);
0214   auto diamondPlaneRef = Surface::makeShared<PlaneSurface>(trf, diamond);
0215   diamondPlaneRef->assignGeometryId(GeometryIdentifier(14u));
0216 
0217   nlohmann::json planeOut =
0218       SurfaceJsonConverter::toJson(gctx, *diamondPlaneRef);
0219   out.open("DiamondPlaneSurface.json");
0220   out << planeOut.dump(2);
0221   out.close();
0222 
0223   auto in = std::ifstream("DiamondPlaneSurface.json",
0224                           std::ifstream::in | std::ifstream::binary);
0225   BOOST_CHECK(in.good());
0226   nlohmann::json planeIn;
0227   in >> planeIn;
0228   in.close();
0229 
0230   auto diamondPlaneTest = SurfaceJsonConverter::fromJson(planeIn);
0231 
0232   BOOST_CHECK(diamondPlaneTest->localToGlobalTransform(gctx).isApprox(
0233       diamondPlaneRef->localToGlobalTransform(gctx)));
0234   BOOST_CHECK_EQUAL(diamondPlaneTest->geometryId(),
0235                     diamondPlaneRef->geometryId());
0236   BOOST_CHECK_EQUAL(diamondPlaneTest->bounds(), diamondPlaneRef->bounds());
0237 }
0238 
0239 BOOST_AUTO_TEST_SUITE_END()
0240 
0241 }  // namespace ActsTests