Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:44

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Direction.hpp"
0013 #include "Acts/Detector/Portal.hpp"
0014 #include "Acts/Detector/detail/PortalHelper.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/Plugins/Json/PortalJsonConverter.hpp"
0017 #include "Acts/Surfaces/PlaneSurface.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0020 
0021 #include <fstream>
0022 
0023 #include <nlohmann/json.hpp>
0024 
0025 namespace Acts::Experimental {
0026 class DetectorVolume {};
0027 }  // namespace Acts::Experimental
0028 
0029 Acts::GeometryContext tContext;
0030 
0031 BOOST_AUTO_TEST_SUITE(PortalJsonConverter)
0032 
0033 BOOST_AUTO_TEST_CASE(PortalUnconnected) {
0034   std::ofstream out;
0035 
0036   auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
0037       Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
0038 
0039   auto portal =
0040       std::make_shared<Acts::Experimental::Portal>(std::move(surface));
0041 
0042   BOOST_CHECK_NE(portal, nullptr);
0043 
0044   auto jPortal = Acts::PortalJsonConverter::toJson(tContext, *portal, {});
0045 
0046   out.open("portal.json");
0047   out << jPortal.dump(4);
0048   out.close();
0049 
0050   // Now read it back in
0051   auto in =
0052       std::ifstream("portal.json", std::ifstream::in | std::ifstream::binary);
0053   BOOST_CHECK(in.good());
0054   nlohmann::json jPortalIn;
0055   in >> jPortalIn;
0056   in.close();
0057 
0058   auto portalIn = Acts::PortalJsonConverter::fromJson(tContext, jPortalIn, {});
0059 
0060   BOOST_CHECK_NE(portalIn, nullptr);
0061 }
0062 
0063 BOOST_AUTO_TEST_CASE(PortalSingleConnected) {
0064   std::ofstream out;
0065 
0066   auto forwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
0067   auto backwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
0068 
0069   auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
0070       Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
0071 
0072   auto portal =
0073       std::make_shared<Acts::Experimental::Portal>(std::move(surface));
0074   BOOST_CHECK_NE(portal, nullptr);
0075   // Attaching the portals
0076   Acts::Experimental::detail::PortalHelper::attachDetectorVolumeUpdater(
0077       *portal, forwardVolume, Acts::Direction::Forward);
0078   Acts::Experimental::detail::PortalHelper::attachDetectorVolumeUpdater(
0079       *portal, backwardVolume, Acts::Direction::Backward);
0080 
0081   std::vector<const Acts::Experimental::DetectorVolume*> detectorVolumes = {
0082       forwardVolume.get(), backwardVolume.get()};
0083   // No volumes provided, must bail
0084   BOOST_CHECK_THROW(Acts::PortalJsonConverter::toJson(tContext, *portal, {}),
0085                     std::runtime_error);
0086   auto jPortal =
0087       Acts::PortalJsonConverter::toJson(tContext, *portal, detectorVolumes);
0088 
0089   out.open("portal-single-connected.json");
0090   out << jPortal.dump(4);
0091   out.close();
0092 
0093   // Now read it back in
0094   auto in = std::ifstream("portal-single-connected.json",
0095                           std::ifstream::in | std::ifstream::binary);
0096   BOOST_CHECK(in.good());
0097   nlohmann::json jPortalIn;
0098   in >> jPortalIn;
0099   in.close();
0100 
0101   auto portalIn = Acts::PortalJsonConverter::fromJson(
0102       tContext, jPortalIn, {forwardVolume, backwardVolume});
0103   BOOST_CHECK_NE(portalIn, nullptr);
0104 }
0105 
0106 BOOST_AUTO_TEST_CASE(PortalMultiConnected) {
0107   std::ofstream out;
0108 
0109   auto forwardVolumeA = std::make_shared<Acts::Experimental::DetectorVolume>();
0110   auto forwardVolumeB = std::make_shared<Acts::Experimental::DetectorVolume>();
0111   auto forwardVolumeC = std::make_shared<Acts::Experimental::DetectorVolume>();
0112 
0113   auto backwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
0114 
0115   auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
0116       Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
0117 
0118   auto portal =
0119       std::make_shared<Acts::Experimental::Portal>(std::move(surface));
0120   BOOST_CHECK_NE(portal, nullptr);
0121 
0122   // Attaching the portals
0123   Acts::Experimental::detail::PortalHelper::attachDetectorVolumeUpdater(
0124       *portal, backwardVolume, Acts::Direction::Backward);
0125 
0126   Acts::Experimental::detail::PortalHelper::attachDetectorVolumesUpdater(
0127       tContext, *portal, {forwardVolumeA, forwardVolumeB, forwardVolumeC},
0128       Acts::Direction::Forward, {-100, 10, 20, 200}, Acts::binX);
0129 
0130   std::vector<const Acts::Experimental::DetectorVolume*> detectorVolumes = {
0131       forwardVolumeA.get(), forwardVolumeB.get(), forwardVolumeC.get(),
0132       backwardVolume.get()};
0133 
0134   auto jPortal =
0135       Acts::PortalJsonConverter::toJson(tContext, *portal, detectorVolumes);
0136 
0137   out.open("portal-multi-connected.json");
0138   out << jPortal.dump(4);
0139   out.close();
0140 }
0141 
0142 BOOST_AUTO_TEST_SUITE_END()