Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:05

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021 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 "Acts/Definitions/Algebra.hpp"
0010 #include "Acts/Detector/Detector.hpp"
0011 #include "Acts/Detector/ProtoDetector.hpp"
0012 #include "Acts/Plugins/Json/DetectorJsonConverter.hpp"
0013 #include "Acts/Plugins/Json/JsonMaterialDecorator.hpp"
0014 #include "Acts/Plugins/Json/MaterialMapJsonConverter.hpp"
0015 #include "Acts/Plugins/Json/ProtoDetectorJsonConverter.hpp"
0016 #include "Acts/Plugins/Python/Utilities.hpp"
0017 #include "Acts/Utilities/Logger.hpp"
0018 #include "ActsExamples/Framework/ProcessCode.hpp"
0019 #include "ActsExamples/Io/Json/JsonMaterialWriter.hpp"
0020 #include "ActsExamples/Io/Json/JsonSurfacesReader.hpp"
0021 #include "ActsExamples/Io/Json/JsonSurfacesWriter.hpp"
0022 
0023 #include <fstream>
0024 #include <initializer_list>
0025 #include <memory>
0026 #include <string>
0027 #include <tuple>
0028 #include <vector>
0029 
0030 #include <nlohmann/json.hpp>
0031 #include <pybind11/pybind11.h>
0032 #include <pybind11/stl.h>
0033 
0034 namespace Acts {
0035 class IMaterialDecorator;
0036 }  // namespace Acts
0037 namespace ActsExamples {
0038 class IMaterialWriter;
0039 class IWriter;
0040 }  // namespace ActsExamples
0041 
0042 namespace py = pybind11;
0043 using namespace pybind11::literals;
0044 
0045 using namespace Acts;
0046 using namespace ActsExamples;
0047 
0048 namespace Acts::Python {
0049 void addJson(Context& ctx) {
0050   auto [m, mex] = ctx.get("main", "examples");
0051 
0052   {
0053     py::class_<JsonMaterialDecorator, Acts::IMaterialDecorator,
0054                std::shared_ptr<JsonMaterialDecorator>>(m,
0055                                                        "JsonMaterialDecorator")
0056         .def(py::init<const MaterialMapJsonConverter::Config&,
0057                       const std::string&, Acts::Logging::Level, bool, bool>(),
0058              py::arg("rConfig"), py::arg("jFileName"), py::arg("level"),
0059              py::arg("clearSurfaceMaterial") = true,
0060              py::arg("clearVolumeMaterial") = true);
0061   }
0062 
0063   {
0064     auto cls =
0065         py::class_<MaterialMapJsonConverter>(m, "MaterialMapJsonConverter")
0066             .def(py::init<const MaterialMapJsonConverter::Config&,
0067                           Acts::Logging::Level>(),
0068                  py::arg("config"), py::arg("level"));
0069 
0070     auto c = py::class_<MaterialMapJsonConverter::Config>(cls, "Config")
0071                  .def(py::init<>());
0072     ACTS_PYTHON_STRUCT_BEGIN(c, MaterialMapJsonConverter::Config);
0073     ACTS_PYTHON_MEMBER(context);
0074     ACTS_PYTHON_MEMBER(processSensitives);
0075     ACTS_PYTHON_MEMBER(processApproaches);
0076     ACTS_PYTHON_MEMBER(processRepresenting);
0077     ACTS_PYTHON_MEMBER(processBoundaries);
0078     ACTS_PYTHON_MEMBER(processVolumes);
0079     ACTS_PYTHON_MEMBER(processDenseVolumes);
0080     ACTS_PYTHON_MEMBER(processNonMaterial);
0081     ACTS_PYTHON_STRUCT_END();
0082   }
0083 
0084   {
0085     py::enum_<JsonFormat>(mex, "JsonFormat")
0086         .value("NoOutput", JsonFormat::NoOutput)
0087         .value("Json", JsonFormat::Json)
0088         .value("Cbor", JsonFormat::Cbor)
0089         .value("All", JsonFormat::All);
0090   }
0091 
0092   {
0093     auto cls =
0094         py::class_<JsonMaterialWriter, IMaterialWriter,
0095                    std::shared_ptr<JsonMaterialWriter>>(mex,
0096                                                         "JsonMaterialWriter")
0097             .def(py::init<const JsonMaterialWriter::Config&,
0098                           Acts::Logging::Level>(),
0099                  py::arg("config"), py::arg("level"))
0100             .def("writeMaterial", &JsonMaterialWriter::writeMaterial)
0101             .def("write", &JsonMaterialWriter::write)
0102             .def_property_readonly("config", &JsonMaterialWriter::config);
0103 
0104     auto c =
0105         py::class_<JsonMaterialWriter::Config>(cls, "Config").def(py::init<>());
0106 
0107     ACTS_PYTHON_STRUCT_BEGIN(c, JsonMaterialWriter::Config);
0108     ACTS_PYTHON_MEMBER(converterCfg);
0109     ACTS_PYTHON_MEMBER(fileName);
0110     ACTS_PYTHON_MEMBER(writeFormat);
0111     ACTS_PYTHON_STRUCT_END();
0112   }
0113 
0114   {
0115     auto cls =
0116         py::class_<JsonSurfacesWriter, IWriter,
0117                    std::shared_ptr<JsonSurfacesWriter>>(mex,
0118                                                         "JsonSurfacesWriter")
0119             .def(py::init<const JsonSurfacesWriter::Config&,
0120                           Acts::Logging::Level>(),
0121                  py::arg("config"), py::arg("level"))
0122             .def("write", &JsonSurfacesWriter::write)
0123             .def_property_readonly("config", &JsonSurfacesWriter::config);
0124 
0125     auto c =
0126         py::class_<JsonSurfacesWriter::Config>(cls, "Config").def(py::init<>());
0127 
0128     ACTS_PYTHON_STRUCT_BEGIN(c, JsonSurfacesWriter::Config);
0129     ACTS_PYTHON_MEMBER(trackingGeometry);
0130     ACTS_PYTHON_MEMBER(outputDir);
0131     ACTS_PYTHON_MEMBER(outputPrecision);
0132     ACTS_PYTHON_MEMBER(writeLayer);
0133     ACTS_PYTHON_MEMBER(writeApproach);
0134     ACTS_PYTHON_MEMBER(writeSensitive);
0135     ACTS_PYTHON_MEMBER(writeBoundary);
0136     ACTS_PYTHON_MEMBER(writePerEvent);
0137     ACTS_PYTHON_MEMBER(writeOnlyNames);
0138     ACTS_PYTHON_STRUCT_END();
0139   }
0140 
0141   {
0142     py::class_<Acts::ProtoDetector>(mex, "ProtoDetector")
0143         .def(py::init<>([](std::string pathName) {
0144           nlohmann::json jDetector;
0145           auto in = std::ifstream(pathName, std::ifstream::in);
0146           if (in.good()) {
0147             in >> jDetector;
0148             in.close();
0149           }
0150           Acts::ProtoDetector pDetector = jDetector["detector"];
0151           return pDetector;
0152         }));
0153   }
0154 
0155   {
0156     auto sjOptions = py::class_<ActsExamples::JsonSurfacesReader::Options>(
0157                          mex, "SurfaceJsonOptions")
0158                          .def(py::init<>());
0159 
0160     ACTS_PYTHON_STRUCT_BEGIN(sjOptions,
0161                              ActsExamples::JsonSurfacesReader::Options);
0162     ACTS_PYTHON_MEMBER(inputFile);
0163     ACTS_PYTHON_MEMBER(jsonEntryPath);
0164     ACTS_PYTHON_STRUCT_END();
0165 
0166     mex.def("readSurfaceFromJson", ActsExamples::JsonSurfacesReader::read);
0167   }
0168 
0169   {
0170     mex.def("writeDetectorToJson",
0171             [](const Acts::GeometryContext& gctx,
0172                const Acts::Experimental::Detector& detector,
0173                const std::string& name) -> void {
0174               auto jDetector =
0175                   Acts::DetectorJsonConverter::toJson(gctx, detector);
0176               std::ofstream out;
0177               out.open(name + ".json");
0178               out << jDetector.dump(4);
0179               out.close();
0180             });
0181   }
0182 
0183   {
0184     mex.def("writeDetectorToJsonDetray",
0185             [](const Acts::GeometryContext& gctx,
0186                const Acts::Experimental::Detector& detector,
0187                const std::string& name) -> void {
0188               // Detray format test - manipulate for detray
0189               Acts::DetectorVolumeJsonConverter::Options detrayOptions;
0190               detrayOptions.transformOptions.writeIdentity = true;
0191               detrayOptions.transformOptions.transpose = true;
0192               detrayOptions.surfaceOptions.transformOptions =
0193                   detrayOptions.transformOptions;
0194               detrayOptions.portalOptions.surfaceOptions =
0195                   detrayOptions.surfaceOptions;
0196 
0197               auto jDetector = Acts::DetectorJsonConverter::toJsonDetray(
0198                   gctx, detector,
0199                   Acts::DetectorJsonConverter::Options{detrayOptions});
0200 
0201               // Write out the geometry, surface_grid, material
0202               auto jGeometry = jDetector["geometry"];
0203               auto jSurfaceGrids = jDetector["surface_grids"];
0204               auto jMaterial = jDetector["material"];
0205 
0206               std::ofstream out;
0207               out.open(name + "_geometry_detray.json");
0208               out << jGeometry.dump(4);
0209               out.close();
0210 
0211               out.open(name + "_surface_grids_detray.json");
0212               out << jSurfaceGrids.dump(4);
0213               out.close();
0214 
0215               out.open(name + "_material_detray.json");
0216               out << jMaterial.dump(4);
0217               out.close();
0218             });
0219   }
0220 
0221   {
0222     mex.def(
0223         "readDetectorFromJson",
0224         [](const Acts::GeometryContext& gctx,
0225            const std::string& fileName) -> auto{
0226           auto in = std::ifstream(fileName,
0227                                   std::ifstream::in | std::ifstream::binary);
0228           nlohmann::json jDetectorIn;
0229           in >> jDetectorIn;
0230           in.close();
0231 
0232           return Acts::DetectorJsonConverter::fromJson(gctx, jDetectorIn);
0233         });
0234   }
0235 }
0236 }  // namespace Acts::Python