From a5fcc70ff5c646af1fa42c395a5897051f90ac20 Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Wed, 7 Aug 2024 15:07:36 +0200 Subject: [PATCH 1/2] adding json writing functionality --- Examples/Python/src/Json.cpp | 16 ++++++++ .../{geomodel.py => geomodel_geometry.py} | 39 ++++++++++++++++++- .../Plugins/Json/SurfaceJsonConverter.hpp | 5 +++ Plugins/Json/src/SurfaceJsonConverter.cpp | 5 +++ 4 files changed, 63 insertions(+), 2 deletions(-) rename Examples/Scripts/Python/{geomodel.py => geomodel_geometry.py} (83%) diff --git a/Examples/Python/src/Json.cpp b/Examples/Python/src/Json.cpp index 864812c6a1a..a73207138de 100644 --- a/Examples/Python/src/Json.cpp +++ b/Examples/Python/src/Json.cpp @@ -152,6 +152,22 @@ void addJson(Context& ctx) { })); } + { + mex.def( + "writeSensitivesMapToJson", + [](const Acts::Experimental::Detector& detector, + const std::string& fileName, std::size_t outputPrecision) -> void { + using SurfaceConverter = + Acts::GeometryHierarchyMapJsonConverter; + std::ofstream out; + out.open(fileName); + auto j = SurfaceConverter("surfaces") + .toJson(detector.sensitiveHierarchyMap(), nullptr); + out << std::setprecision(outputPrecision) << j.dump(2); + out.close(); + }); + } + { auto sjOptions = py::class_( mex, "SurfaceJsonOptions") diff --git a/Examples/Scripts/Python/geomodel.py b/Examples/Scripts/Python/geomodel_geometry.py similarity index 83% rename from Examples/Scripts/Python/geomodel.py rename to Examples/Scripts/Python/geomodel_geometry.py index 82fc851b727..69d82a51e02 100644 --- a/Examples/Scripts/Python/geomodel.py +++ b/Examples/Scripts/Python/geomodel_geometry.py @@ -79,7 +79,21 @@ def main(): p.add_argument( "--output-json", - help="Write the surfaces to OBJ files", + help="Write the geometry to JSON files", + action="store_true", + default=False, + ) + + p.add_argument( + "--output-json-detray", + help="Write the geometry in detray JSON format", + action="store_true", + default=False, + ) + + p.add_argument( + "--output-json-sensitives-map", + help="Write the surfaces to a dedicated JSON file", action="store_true", default=False, ) @@ -177,6 +191,17 @@ def main(): args.output + "_detector", ) + acts.svg.viewDetector( + gContext, + detector, + args.top_node, + [[ivol, volumeOptions] for ivol in range(detector.numberVolumes())], + [ + ["zr", ["materials"], zrRange], + ], + args.output + "_material_surfaces", + ) + # Output the internal navigation to SVG if args.output_internals_svg: for vol in detector.volumes(): @@ -197,7 +222,17 @@ def main(): ) # Output to a JSON file if args.output_json: - acts.examples.writeDetectorToJsonDetray(gContext, detector, args.output) + # Gen2 detector json format + if args.output_json_detray: + acts.examples.writeDetectorToJsonDetray(gContext, detector, args.output) + else: + acts.examples.writeDetectorToJson(gContext, detector, args.output) + + # Create a surface hierarchy map (Gen1 / Gen2) + if args.output_json_sensitives_map: + acts.examples.writeSensitivesMapToJson( + detector, args.output + "_sensitives.json", 4 + ) return diff --git a/Plugins/Json/include/Acts/Plugins/Json/SurfaceJsonConverter.hpp b/Plugins/Json/include/Acts/Plugins/Json/SurfaceJsonConverter.hpp index 64b270626ab..3a55c56f090 100644 --- a/Plugins/Json/include/Acts/Plugins/Json/SurfaceJsonConverter.hpp +++ b/Plugins/Json/include/Acts/Plugins/Json/SurfaceJsonConverter.hpp @@ -46,6 +46,11 @@ void to_json(nlohmann::json& j, const Surface& surface); /// @note it will take the default context void to_json(nlohmann::json& j, const std::shared_ptr& surface); +/// Non-contextual conversion of a surface +/// +/// @note it will take the default context +void to_json(nlohmann::json& j, const Surface* surface); + /// Contextual conversion of a surface /// /// @param j the json to be filled diff --git a/Plugins/Json/src/SurfaceJsonConverter.cpp b/Plugins/Json/src/SurfaceJsonConverter.cpp index b0911366da7..7380916971a 100644 --- a/Plugins/Json/src/SurfaceJsonConverter.cpp +++ b/Plugins/Json/src/SurfaceJsonConverter.cpp @@ -42,6 +42,11 @@ void Acts::to_json(nlohmann::json& j, const Acts::Surface& surface) { j = SurfaceJsonConverter::toJson(gctx, surface); } +void Acts::to_json(nlohmann::json& j, const Acts::Surface* surface) { + Acts::GeometryContext gctx; + j = SurfaceJsonConverter::toJson(gctx, *surface); +} + void Acts::to_json(nlohmann::json& j, const std::shared_ptr& surface) { Acts::GeometryContext gctx; From 6be3bb42e7b21984b0b37302c43e0bfb2ea7079e Mon Sep 17 00:00:00 2001 From: Andreas Salzburger Date: Thu, 8 Aug 2024 08:47:58 +0200 Subject: [PATCH 2/2] add new test --- .../Json/SurfaceJsonConverterTests.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Tests/UnitTests/Plugins/Json/SurfaceJsonConverterTests.cpp b/Tests/UnitTests/Plugins/Json/SurfaceJsonConverterTests.cpp index 44c8b3470f9..0a0933cf39c 100644 --- a/Tests/UnitTests/Plugins/Json/SurfaceJsonConverterTests.cpp +++ b/Tests/UnitTests/Plugins/Json/SurfaceJsonConverterTests.cpp @@ -203,6 +203,35 @@ BOOST_AUTO_TEST_CASE(PerigeeRoundTripTests) { BOOST_CHECK_EQUAL(perigeeTest->geometryId(), perigeeRef->geometryId()); } +BOOST_AUTO_TEST_CASE(CylinderToJsonPlainPointerReference) { + Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.)); + auto tube = std::make_shared(5., 20.); + auto cylinderRef = Surface::makeShared(trf, tube); + cylinderRef->assignGeometryId(GeometryIdentifier(11u)); + + const Surface* surf = cylinderRef.get(); + + // Test a cylinder + nlohmann::json cylinderOut = surf; + out.open("CylinderSurfacePlainPointer.json"); + out << cylinderOut.dump(2); + out.close(); + + auto in = std::ifstream("CylinderSurfacePlainPointer.json", + std::ifstream::in | std::ifstream::binary); + BOOST_CHECK(in.good()); + nlohmann::json cylinderIn; + in >> cylinderIn; + in.close(); + + auto cylinderTest = SurfaceJsonConverter::fromJson(cylinderIn); + + BOOST_CHECK( + cylinderTest->transform(gctx).isApprox(cylinderRef->transform(gctx))); + BOOST_CHECK_EQUAL(cylinderTest->geometryId(), cylinderRef->geometryId()); + BOOST_CHECK_EQUAL(cylinderTest->bounds(), cylinderRef->bounds()); +} + BOOST_AUTO_TEST_CASE(SurfacesDetrayTests) { Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.)); auto trapezoid = std::make_shared(2., 3., 4.);