From bd16233b82a52b93e739772f7f0fe2dbb7bee8c8 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Tue, 12 Nov 2024 11:45:37 +0100 Subject: [PATCH 01/10] add code for json detector element --- .../Io/Json/JsonSurfacesReader.hpp | 10 +++++ Examples/Io/Json/src/JsonSurfacesReader.cpp | 23 +++++++++++ Examples/Python/src/Json.cpp | 10 +++++ Plugins/Json/CMakeLists.txt | 1 + .../Acts/Plugins/Json/JsonDetectorElement.hpp | 32 +++++++++++++++ Plugins/Json/src/JsonDetectorElement.cpp | 40 +++++++++++++++++++ 6 files changed, 116 insertions(+) create mode 100644 Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp create mode 100644 Plugins/Json/src/JsonDetectorElement.cpp diff --git a/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp b/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp index 50bbf15d35f..f00828d57ab 100644 --- a/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp +++ b/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp @@ -9,6 +9,7 @@ #pragma once #include "Acts/Geometry/GeometryHierarchyMap.hpp" +#include "Acts/Plugins/Json/JsonDetectorElement.hpp" #include #include @@ -43,4 +44,13 @@ Acts::GeometryHierarchyMap> readHierarchyMap( /// @return a vector of surfaces std::vector> readVector(const Options& options); +/// @brief Read the surfaces from the input file and create +/// detector elements +/// +/// @param inputFile is the input file to read from +/// +/// @return a vector of surfaces +std::vector> readDetectorElements( + const Options& options, double thickness); + } // namespace ActsExamples::JsonSurfacesReader diff --git a/Examples/Io/Json/src/JsonSurfacesReader.cpp b/Examples/Io/Json/src/JsonSurfacesReader.cpp index 469c9828d46..c5ba2d98b4d 100644 --- a/Examples/Io/Json/src/JsonSurfacesReader.cpp +++ b/Examples/Io/Json/src/JsonSurfacesReader.cpp @@ -73,4 +73,27 @@ std::vector> JsonSurfacesReader::readVector( return surfaces; } +std::vector> +JsonSurfacesReader::readDetectorElements(const Options& options, + double thickness = 0.0) { + nlohmann::json j; + { + std::ifstream in(options.inputFile); + in >> j; + } + + // Walk down the path to the surface entries + nlohmann::json jSurfaces = j; + for (const auto& jep : options.jsonEntryPath) { + jSurfaces = jSurfaces[jep]; + } + + std::vector> elements; + for (const auto& jSurface : jSurfaces) { + elements.emplace_back( + std::make_shared(jSurface, thickness)); + } + return elements; +} + } // namespace ActsExamples diff --git a/Examples/Python/src/Json.cpp b/Examples/Python/src/Json.cpp index be367612ac2..049554272dd 100644 --- a/Examples/Python/src/Json.cpp +++ b/Examples/Python/src/Json.cpp @@ -168,6 +168,16 @@ void addJson(Context& ctx) { mex.def("readSurfaceVectorFromJson", ActsExamples::JsonSurfacesReader::readVector); + + py::class_>( + mex, "JsonDetectorElement") + .def("surface", [](Acts::JsonDetectorElement& self) { + return self.surface().getSharedPtr(); + }); + + mex.def("readDetectorElementsFromJson", + ActsExamples::JsonSurfacesReader::readDetectorElements); } { diff --git a/Plugins/Json/CMakeLists.txt b/Plugins/Json/CMakeLists.txt index 0d3ff9875ee..9965c9f8c33 100644 --- a/Plugins/Json/CMakeLists.txt +++ b/Plugins/Json/CMakeLists.txt @@ -22,6 +22,7 @@ add_library( src/VolumeJsonConverter.cpp src/AmbiguityConfigJsonConverter.cpp src/DetrayJsonHelper.cpp + src/JsonDetectorElement.cpp ) target_include_directories( ActsPluginJson diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp new file mode 100644 index 00000000000..7fcdcf73bdb --- /dev/null +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp @@ -0,0 +1,32 @@ +// This file is part of the ACTS project. +// +// Copyright (C) 2016 CERN for the benefit of the ACTS project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. +#pragma once + +#include "Acts/Geometry/DetectorElementBase.hpp" + +#include + +namespace Acts { + +class JsonDetectorElement : public DetectorElementBase { + std::shared_ptr m_surface; + Transform3 m_transform{}; + double m_thickness{}; + + public: + JsonDetectorElement(const nlohmann::json &surfaceJson, double thickness); + + Surface &surface() override; + const Surface &surface() const override; + + double thickness() const override; + + const Transform3 &transform(const GeometryContext &gctx) const override; +}; + +} // namespace Acts diff --git a/Plugins/Json/src/JsonDetectorElement.cpp b/Plugins/Json/src/JsonDetectorElement.cpp new file mode 100644 index 00000000000..c8d8996694e --- /dev/null +++ b/Plugins/Json/src/JsonDetectorElement.cpp @@ -0,0 +1,40 @@ +// This file is part of the ACTS project. +// +// Copyright (C) 2016 CERN for the benefit of the ACTS project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +#include "Acts/Plugins/Json/JsonDetectorElement.hpp" + +#include "Acts/Plugins/Json/SurfaceJsonConverter.hpp" + +namespace Acts { + +JsonDetectorElement::JsonDetectorElement(const nlohmann::json &jSurface, + double thickness) + : m_thickness(thickness) { + m_surface = Acts::SurfaceJsonConverter::fromJson(jSurface); + m_transform = m_surface->transform({}); + m_surface->assignDetectorElement(*this); +} + +const Surface &JsonDetectorElement::surface() const { + return *m_surface; +} + +Surface &JsonDetectorElement::surface() { + return *m_surface; +} + +const Transform3 &JsonDetectorElement::transform( + const GeometryContext &) const { + return m_transform; +} + +double JsonDetectorElement::thickness() const { + return m_thickness; +} + +} // namespace Acts From 106ef4f63f458d189dd5cea6b51d995628accb33 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Tue, 12 Nov 2024 13:41:47 +0100 Subject: [PATCH 02/10] add binding to detector element base --- Examples/Python/src/Detector.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Examples/Python/src/Detector.cpp b/Examples/Python/src/Detector.cpp index 6b9ef68f927..7c1cfde18cb 100644 --- a/Examples/Python/src/Detector.cpp +++ b/Examples/Python/src/Detector.cpp @@ -222,6 +222,12 @@ void addDetector(Context& ctx) { patchKwargsConstructor(c); } + + { + py::class_>( + mex, "DetectorElementBase"); + } } } // namespace Acts::Python From b92508cf05e005d4ef52c0700b8100fec72af382 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Fri, 15 Nov 2024 10:36:27 +0100 Subject: [PATCH 03/10] make clang tidy happy --- Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp | 2 +- Plugins/Json/src/JsonDetectorElement.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp index 7fcdcf73bdb..6752e782fda 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp @@ -19,7 +19,7 @@ class JsonDetectorElement : public DetectorElementBase { double m_thickness{}; public: - JsonDetectorElement(const nlohmann::json &surfaceJson, double thickness); + JsonDetectorElement(const nlohmann::json &jSurface, double thickness); Surface &surface() override; const Surface &surface() const override; diff --git a/Plugins/Json/src/JsonDetectorElement.cpp b/Plugins/Json/src/JsonDetectorElement.cpp index c8d8996694e..7a7bfed7102 100644 --- a/Plugins/Json/src/JsonDetectorElement.cpp +++ b/Plugins/Json/src/JsonDetectorElement.cpp @@ -29,7 +29,7 @@ Surface &JsonDetectorElement::surface() { } const Transform3 &JsonDetectorElement::transform( - const GeometryContext &) const { + const GeometryContext & /*gctx*/) const { return m_transform; } From 7d480349bfa41db54d97ff2c2173772e78de582c Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Fri, 15 Nov 2024 11:14:25 +0100 Subject: [PATCH 04/10] apply review --- Examples/Io/Json/CMakeLists.txt | 1 - Examples/Python/src/Json.cpp | 24 +++++++++---------- Plugins/Json/CMakeLists.txt | 1 + .../Acts/Plugins/Json/JsonDetectorElement.hpp | 9 +++---- .../Acts/Plugins}/Json/JsonSurfacesReader.hpp | 4 ++-- .../Json/src/JsonSurfacesReader.cpp | 6 ++--- 6 files changed, 22 insertions(+), 23 deletions(-) rename {Examples/Io/Json/include/ActsExamples/Io => Plugins/Json/include/Acts/Plugins}/Json/JsonSurfacesReader.hpp (94%) rename {Examples/Io => Plugins}/Json/src/JsonSurfacesReader.cpp (96%) diff --git a/Examples/Io/Json/CMakeLists.txt b/Examples/Io/Json/CMakeLists.txt index 255d862881f..5461f5dd98d 100644 --- a/Examples/Io/Json/CMakeLists.txt +++ b/Examples/Io/Json/CMakeLists.txt @@ -4,7 +4,6 @@ add_library( src/JsonGeometryList.cpp src/JsonMaterialWriter.cpp src/JsonSurfacesWriter.cpp - src/JsonSurfacesReader.cpp src/JsonDigitizationConfig.cpp ) target_include_directories( diff --git a/Examples/Python/src/Json.cpp b/Examples/Python/src/Json.cpp index 049554272dd..5b980fd8fe2 100644 --- a/Examples/Python/src/Json.cpp +++ b/Examples/Python/src/Json.cpp @@ -11,13 +11,13 @@ #include "Acts/Detector/ProtoDetector.hpp" #include "Acts/Plugins/Json/DetectorJsonConverter.hpp" #include "Acts/Plugins/Json/JsonMaterialDecorator.hpp" +#include "Acts/Plugins/Json/JsonSurfacesReader.hpp" #include "Acts/Plugins/Json/MaterialMapJsonConverter.hpp" #include "Acts/Plugins/Json/ProtoDetectorJsonConverter.hpp" #include "Acts/Plugins/Python/Utilities.hpp" #include "Acts/Utilities/Logger.hpp" #include "ActsExamples/Framework/ProcessCode.hpp" #include "ActsExamples/Io/Json/JsonMaterialWriter.hpp" -#include "ActsExamples/Io/Json/JsonSurfacesReader.hpp" #include "ActsExamples/Io/Json/JsonSurfacesWriter.hpp" #include @@ -153,31 +153,29 @@ void addJson(Context& ctx) { } { - auto sjOptions = py::class_( - mex, "SurfaceJsonOptions") - .def(py::init<>()); + auto sjOptions = + py::class_(m, "SurfaceJsonOptions") + .def(py::init<>()); - ACTS_PYTHON_STRUCT_BEGIN(sjOptions, - ActsExamples::JsonSurfacesReader::Options); + ACTS_PYTHON_STRUCT_BEGIN(sjOptions, Acts::JsonSurfacesReader::Options); ACTS_PYTHON_MEMBER(inputFile); ACTS_PYTHON_MEMBER(jsonEntryPath); ACTS_PYTHON_STRUCT_END(); - mex.def("readSurfaceHierarchyMapFromJson", - ActsExamples::JsonSurfacesReader::readHierarchyMap); + m.def("readSurfaceHierarchyMapFromJson", + Acts::JsonSurfacesReader::readHierarchyMap); - mex.def("readSurfaceVectorFromJson", - ActsExamples::JsonSurfacesReader::readVector); + m.def("readSurfaceVectorFromJson", Acts::JsonSurfacesReader::readVector); py::class_>( - mex, "JsonDetectorElement") + m, "JsonDetectorElement") .def("surface", [](Acts::JsonDetectorElement& self) { return self.surface().getSharedPtr(); }); - mex.def("readDetectorElementsFromJson", - ActsExamples::JsonSurfacesReader::readDetectorElements); + m.def("readDetectorElementsFromJson", + Acts::JsonSurfacesReader::readDetectorElements); } { diff --git a/Plugins/Json/CMakeLists.txt b/Plugins/Json/CMakeLists.txt index 9965c9f8c33..20634d2bfe5 100644 --- a/Plugins/Json/CMakeLists.txt +++ b/Plugins/Json/CMakeLists.txt @@ -23,6 +23,7 @@ add_library( src/AmbiguityConfigJsonConverter.cpp src/DetrayJsonHelper.cpp src/JsonDetectorElement.cpp + src/JsonSurfacesReader.cpp ) target_include_directories( ActsPluginJson diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp index 6752e782fda..c0cfea42deb 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp @@ -14,10 +14,6 @@ namespace Acts { class JsonDetectorElement : public DetectorElementBase { - std::shared_ptr m_surface; - Transform3 m_transform{}; - double m_thickness{}; - public: JsonDetectorElement(const nlohmann::json &jSurface, double thickness); @@ -27,6 +23,11 @@ class JsonDetectorElement : public DetectorElementBase { double thickness() const override; const Transform3 &transform(const GeometryContext &gctx) const override; + + private: + std::shared_ptr m_surface; + Transform3 m_transform{}; + double m_thickness{}; }; } // namespace Acts diff --git a/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp similarity index 94% rename from Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp rename to Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp index f00828d57ab..9a085bc6cde 100644 --- a/Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesReader.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp @@ -19,7 +19,7 @@ namespace Acts { class Surface; } -namespace ActsExamples::JsonSurfacesReader { +namespace Acts::JsonSurfacesReader { /// @brief Options specification for surface reading struct Options { @@ -53,4 +53,4 @@ std::vector> readVector(const Options& options); std::vector> readDetectorElements( const Options& options, double thickness); -} // namespace ActsExamples::JsonSurfacesReader +} // namespace Acts::JsonSurfacesReader diff --git a/Examples/Io/Json/src/JsonSurfacesReader.cpp b/Plugins/Json/src/JsonSurfacesReader.cpp similarity index 96% rename from Examples/Io/Json/src/JsonSurfacesReader.cpp rename to Plugins/Json/src/JsonSurfacesReader.cpp index c5ba2d98b4d..fcee0ee7b86 100644 --- a/Examples/Io/Json/src/JsonSurfacesReader.cpp +++ b/Plugins/Json/src/JsonSurfacesReader.cpp @@ -6,7 +6,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -#include "ActsExamples/Io/Json/JsonSurfacesReader.hpp" +#include "Acts/Plugins/Json/JsonSurfacesReader.hpp" #include "Acts/Geometry/GeometryIdentifier.hpp" #include "Acts/Plugins/Json/ActsJson.hpp" @@ -17,7 +17,7 @@ #include #include -namespace ActsExamples { +namespace Acts { Acts::GeometryHierarchyMap> JsonSurfacesReader::readHierarchyMap( @@ -96,4 +96,4 @@ JsonSurfacesReader::readDetectorElements(const Options& options, return elements; } -} // namespace ActsExamples +} // namespace Acts From c61595edc26740076f17cae65b48aca3b9ca9efd Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Fri, 15 Nov 2024 11:27:03 +0100 Subject: [PATCH 05/10] update docs --- .../Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp | 5 +++++ .../Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp index c0cfea42deb..34c5a5b173a 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp @@ -13,6 +13,11 @@ namespace Acts { +/// A implementation of a detector element, that is constructed from a +/// JSON description of a surface. The idea behind this is that it helps +/// importing whole tracking geometries from JSON files. In some parts of +/// the codebase, the existence of a detector element associated to a surface +/// has a specific meaning (e.g., flags surfaces as sensitive). class JsonDetectorElement : public DetectorElementBase { public: JsonDetectorElement(const nlohmann::json &jSurface, double thickness); diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp index 9a085bc6cde..30992639fa5 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp @@ -39,7 +39,7 @@ Acts::GeometryHierarchyMap> readHierarchyMap( /// @brief Read the flat surfaces from the input file /// -/// @param inputFile is the input file to read from +/// @param options options to surface reading /// /// @return a vector of surfaces std::vector> readVector(const Options& options); @@ -47,7 +47,7 @@ std::vector> readVector(const Options& options); /// @brief Read the surfaces from the input file and create /// detector elements /// -/// @param inputFile is the input file to read from +/// @param inputFile options to detector lement reading /// /// @return a vector of surfaces std::vector> readDetectorElements( From 2d9eba72fdfdb32d85148c3c73bace85499d5d06 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Fri, 15 Nov 2024 11:28:41 +0100 Subject: [PATCH 06/10] update docs --- .../Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp index 30992639fa5..bb150db4e94 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp @@ -39,7 +39,7 @@ Acts::GeometryHierarchyMap> readHierarchyMap( /// @brief Read the flat surfaces from the input file /// -/// @param options options to surface reading +/// @param options options for surface reading /// /// @return a vector of surfaces std::vector> readVector(const Options& options); @@ -47,7 +47,8 @@ std::vector> readVector(const Options& options); /// @brief Read the surfaces from the input file and create /// detector elements /// -/// @param inputFile options to detector lement reading +/// @param inputFile options for surface reading +/// @param thickness the thickness used to construct the detector element /// /// @return a vector of surfaces std::vector> readDetectorElements( From 80ecb721e50da2385eef8b67fbfd44db94b6ceb9 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Fri, 15 Nov 2024 11:38:31 +0100 Subject: [PATCH 07/10] finally get it right? --- Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp index bb150db4e94..f676621ac98 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp @@ -47,7 +47,7 @@ std::vector> readVector(const Options& options); /// @brief Read the surfaces from the input file and create /// detector elements /// -/// @param inputFile options for surface reading +/// @param options options for surface reading /// @param thickness the thickness used to construct the detector element /// /// @return a vector of surfaces From 9aa87ed8febb805b832fd8d62fd47a2753b02e58 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Tue, 26 Nov 2024 14:43:22 +0100 Subject: [PATCH 08/10] pr feedback --- .../Acts/Plugins/Json/JsonSurfacesReader.hpp | 5 + Plugins/Json/src/JsonDetectorElement.cpp | 3 +- Tests/UnitTests/Plugins/Json/CMakeLists.txt | 1 + .../Plugins/Json/JsonSurfacesReaderTests.cpp | 98 +++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp diff --git a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp index f676621ac98..17822c24401 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/JsonSurfacesReader.hpp @@ -22,6 +22,8 @@ class Surface; namespace Acts::JsonSurfacesReader { /// @brief Options specification for surface reading +/// The file should contain an array of json surfaces +/// as produced by the SurfaceJsonConverter tools struct Options { /// @brief Which input file to read from std::string inputFile = ""; @@ -30,6 +32,7 @@ struct Options { }; /// @brief Read the surfaces from the input file +/// For details on the file format see the options struct /// /// @param options specifies which file and what to read /// @@ -38,6 +41,7 @@ Acts::GeometryHierarchyMap> readHierarchyMap( const Options& options); /// @brief Read the flat surfaces from the input file +/// For details on the file format see the options struct /// /// @param options options for surface reading /// @@ -46,6 +50,7 @@ std::vector> readVector(const Options& options); /// @brief Read the surfaces from the input file and create /// detector elements +/// For details on the file format see the options struct /// /// @param options options for surface reading /// @param thickness the thickness used to construct the detector element diff --git a/Plugins/Json/src/JsonDetectorElement.cpp b/Plugins/Json/src/JsonDetectorElement.cpp index 7a7bfed7102..5c1b4568ca5 100644 --- a/Plugins/Json/src/JsonDetectorElement.cpp +++ b/Plugins/Json/src/JsonDetectorElement.cpp @@ -8,6 +8,7 @@ #include "Acts/Plugins/Json/JsonDetectorElement.hpp" +#include "Acts/Plugins/Json/AlgebraJsonConverter.hpp" #include "Acts/Plugins/Json/SurfaceJsonConverter.hpp" namespace Acts { @@ -16,7 +17,7 @@ JsonDetectorElement::JsonDetectorElement(const nlohmann::json &jSurface, double thickness) : m_thickness(thickness) { m_surface = Acts::SurfaceJsonConverter::fromJson(jSurface); - m_transform = m_surface->transform({}); + m_transform = Transform3JsonConverter::fromJson(jSurface["transform"]); m_surface->assignDetectorElement(*this); } diff --git a/Tests/UnitTests/Plugins/Json/CMakeLists.txt b/Tests/UnitTests/Plugins/Json/CMakeLists.txt index 141f86fb3fe..82ad95611ed 100644 --- a/Tests/UnitTests/Plugins/Json/CMakeLists.txt +++ b/Tests/UnitTests/Plugins/Json/CMakeLists.txt @@ -16,3 +16,4 @@ add_unittest(SurfaceBoundsJsonConverter SurfaceBoundsJsonConverterTests.cpp) add_unittest(SurfaceJsonConverter SurfaceJsonConverterTests.cpp) add_unittest(VolumeBoundsJsonConverter VolumeBoundsJsonConverterTests.cpp) add_unittest(TrackParametersJsonConverter TrackParametersJsonConverterTests.cpp) +add_unittest(JsonSurfacesReader JsonSurfacesReaderTests.cpp) diff --git a/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp b/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp new file mode 100644 index 00000000000..4ab573e4df7 --- /dev/null +++ b/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp @@ -0,0 +1,98 @@ +// This file is part of the ACTS project. +// +// Copyright (C) 2016 CERN for the benefit of the ACTS project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +#include +#include + +#include "Acts/Definitions/Algebra.hpp" +#include "Acts/Geometry/GeometryContext.hpp" +#include "Acts/Geometry/GeometryIdentifier.hpp" +#include "Acts/Plugins/Json/JsonSurfacesReader.hpp" +#include "Acts/Plugins/Json/SurfaceJsonConverter.hpp" +#include "Acts/Surfaces/PlaneSurface.hpp" +#include "Acts/Surfaces/RectangleBounds.hpp" +#include "Acts/Utilities/Zip.hpp" + +#include +#include +#include + +#include +#include + +const std::vector> surfaces = []() { + std::vector> v; + + for (int i = 0; i < 3; ++i) { + auto bounds = std::make_shared(1.0, 1.0); + Acts::Transform3 transform = Acts::Transform3::Identity(); + transform.translate(Acts::Vector3::Random()); + Acts::Vector3 randomAngles = Acts::Vector3::Random(); + Acts::SquareMatrix3 rotMatrix; + rotMatrix = Eigen::AngleAxis(randomAngles[0], Acts::Vector3::UnitX()) * + Eigen::AngleAxis(randomAngles[1], Acts::Vector3::UnitY()) * + Eigen::AngleAxis(randomAngles[2], Acts::Vector3::UnitZ()); + transform.rotate(rotMatrix); + v.push_back( + Acts::Surface::makeShared(transform, bounds)); + } + + return v; +}(); + +const std::string filename = "json_surfaces_reader_tests.json"; + +struct FileFixture { + FileFixture() { + nlohmann::json js = nlohmann::json::array(); + + for (const auto &s : surfaces) { + js.push_back(Acts::SurfaceJsonConverter::toJson({}, *s)); + } + + nlohmann::json j; + j["foo"] = js; + + std::ofstream f(filename); + f << j.dump(2); + } + + ~FileFixture() { + // std::filesystem::remove(filename); + } +}; + +FileFixture fileFixture; + +BOOST_AUTO_TEST_CASE(surface_reading_test) { + auto readBackSurfaces = + Acts::JsonSurfacesReader::readVector({filename, {"foo"}}); + + for (auto [refSurface, surface] : Acts::zip(surfaces, readBackSurfaces)) { + BOOST_CHECK( + refSurface->transform({}).isApprox(surface->transform({}), 1.e-4)); + BOOST_CHECK(refSurface->center({}).isApprox(surface->center({}), 1.e-4)); + BOOST_CHECK_EQUAL(refSurface->type(), surface->type()); + BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type()); + } +} + +BOOST_AUTO_TEST_CASE(json_detelement_reading_test) { + auto readBackDetElements = + Acts::JsonSurfacesReader::readDetectorElements({filename, {"foo"}}, 1.0); + + for (auto [refSurface, detElement] : + Acts::zip(surfaces, readBackDetElements)) { + auto surface = &detElement->surface(); + BOOST_CHECK( + refSurface->transform({}).isApprox(surface->transform({}), 1.e-4)); + BOOST_CHECK(refSurface->center({}).isApprox(surface->center({}), 1.e-4)); + BOOST_CHECK_EQUAL(refSurface->type(), surface->type()); + BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type()); + } +} From 6f9e476e7179e6a7febd316377eb62a5c08c0407 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Tue, 26 Nov 2024 15:45:23 +0100 Subject: [PATCH 09/10] update test --- Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp b/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp index 4ab573e4df7..e70bad6c7bf 100644 --- a/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp +++ b/Tests/UnitTests/Plugins/Json/JsonSurfacesReaderTests.cpp @@ -62,9 +62,7 @@ struct FileFixture { f << j.dump(2); } - ~FileFixture() { - // std::filesystem::remove(filename); - } + ~FileFixture() { std::filesystem::remove(filename); } }; FileFixture fileFixture; @@ -73,6 +71,7 @@ BOOST_AUTO_TEST_CASE(surface_reading_test) { auto readBackSurfaces = Acts::JsonSurfacesReader::readVector({filename, {"foo"}}); + BOOST_REQUIRE_EQUAL(surfaces.size(), readBackSurfaces.size()); for (auto [refSurface, surface] : Acts::zip(surfaces, readBackSurfaces)) { BOOST_CHECK( refSurface->transform({}).isApprox(surface->transform({}), 1.e-4)); @@ -86,6 +85,7 @@ BOOST_AUTO_TEST_CASE(json_detelement_reading_test) { auto readBackDetElements = Acts::JsonSurfacesReader::readDetectorElements({filename, {"foo"}}, 1.0); + BOOST_REQUIRE_EQUAL(surfaces.size(), readBackDetElements.size()); for (auto [refSurface, detElement] : Acts::zip(surfaces, readBackDetElements)) { auto surface = &detElement->surface(); From 2ce80dd494ac08c1384801ea47d8632b1651306e Mon Sep 17 00:00:00 2001 From: Benjamin Huth <37871400+benjaminhuth@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:24:28 +0100 Subject: [PATCH 10/10] Update lazy_autodoc.py --- docs/_extensions/lazy_autodoc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/_extensions/lazy_autodoc.py b/docs/_extensions/lazy_autodoc.py index 4de41b05b15..184fd59bbfc 100644 --- a/docs/_extensions/lazy_autodoc.py +++ b/docs/_extensions/lazy_autodoc.py @@ -118,6 +118,7 @@ def run() -> None: "Acts::Logging::DefaultFilterPolicy", "Acts::Logging::DefaultPrintPolicy", "Acts::SourceLink", + "Acts::JsonDetectorElement", } role_instances["func"] = {