Back to home page

EIC code displayed by LXR

 
 

    


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

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