Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Python/Plugins/src/Json.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "Acts/Detector/Detector.hpp"
0010 #include "Acts/Detector/ProtoDetector.hpp"
0011 #include "Acts/Geometry/GeometryContext.hpp"
0012 #include "Acts/Utilities/Logger.hpp"
0013 #include "ActsPlugins/Json/DetectorJsonConverter.hpp"
0014 #include "ActsPlugins/Json/JsonMaterialDecorator.hpp"
0015 #include "ActsPlugins/Json/JsonSurfacesReader.hpp"
0016 #include "ActsPlugins/Json/MaterialMapJsonConverter.hpp"
0017 #include "ActsPlugins/Json/ProtoDetectorJsonConverter.hpp"
0018 #include "ActsPython/Utilities/Helpers.hpp"
0019 #include "ActsPython/Utilities/Macros.hpp"
0020 
0021 #include <memory>
0022 #include <string>
0023 
0024 #include <pybind11/pybind11.h>
0025 
0026 namespace py = pybind11;
0027 using namespace pybind11::literals;
0028 
0029 PYBIND11_MODULE(ActsPluginsPythonBindingsJson, json) {
0030   using namespace Acts;
0031   using namespace ActsPython;
0032 
0033   {
0034     py::class_<JsonMaterialDecorator, IMaterialDecorator,
0035                std::shared_ptr<JsonMaterialDecorator>>(json,
0036                                                        "JsonMaterialDecorator")
0037         .def(py::init<const MaterialMapJsonConverter::Config&,
0038                       const std::string&, Logging::Level, bool, bool>(),
0039              py::arg("rConfig"), py::arg("jFileName"), py::arg("level"),
0040              py::arg("clearSurfaceMaterial") = true,
0041              py::arg("clearVolumeMaterial") = true);
0042   }
0043 
0044   {
0045     auto cls =
0046         py::class_<MaterialMapJsonConverter>(json, "MaterialMapJsonConverter")
0047             .def(py::init<const MaterialMapJsonConverter::Config&,
0048                           Logging::Level>(),
0049                  py::arg("config"), py::arg("level"));
0050 
0051     auto c = py::class_<MaterialMapJsonConverter::Config>(cls, "Config")
0052                  .def(py::init<>());
0053     ACTS_PYTHON_STRUCT(c, context, processSensitives, processApproaches,
0054                        processRepresenting, processBoundaries, processVolumes,
0055                        processDenseVolumes, processNonMaterial);
0056   }
0057 
0058   {
0059     auto sjOptions =
0060         py::class_<JsonSurfacesReader::Options>(json, "SurfaceJsonOptions")
0061             .def(py::init<>());
0062     ACTS_PYTHON_STRUCT(sjOptions, inputFile, jsonEntryPath);
0063 
0064     json.def("readSurfaceHierarchyMapFromJson",
0065              JsonSurfacesReader::readHierarchyMap);
0066 
0067     json.def("readSurfaceVectorFromJson", JsonSurfacesReader::readVector);
0068 
0069     py::class_<JsonDetectorElement, DetectorElementBase,
0070                std::shared_ptr<JsonDetectorElement>>(json,
0071                                                      "JsonDetectorElement")
0072         .def("surface", [](JsonDetectorElement& self) {
0073           return self.surface().getSharedPtr();
0074         });
0075 
0076     json.def("readDetectorElementsFromJson",
0077              JsonSurfacesReader::readDetectorElements);
0078   }
0079 
0080   // Gen2 Geometry code - to be removed when Gen3 is default
0081   using namespace Acts::Experimental;
0082 
0083   {
0084     py::class_<ProtoDetector>(json, "ProtoDetector")
0085         .def(py::init<>([](std::string pathName) {
0086           nlohmann::json jDetector;
0087           auto in = std::ifstream(pathName, std::ifstream::in);
0088           if (in.good()) {
0089             in >> jDetector;
0090             in.close();
0091           }
0092           ProtoDetector pDetector = jDetector["detector"];
0093           return pDetector;
0094         }));
0095   }
0096 
0097   {
0098     json.def("writeDetectorToJson",
0099              [](const GeometryContext& gctx, const Detector& detector,
0100                 const std::string& name) -> void {
0101                auto jDetector = DetectorJsonConverter::toJson(gctx, detector);
0102                std::ofstream out;
0103                out.open(name + ".json");
0104                out << jDetector.dump(4);
0105                out.close();
0106              });
0107   }
0108 
0109   {
0110     json.def("writeDetectorToJsonDetray",
0111              [](const GeometryContext& gctx, const Detector& detector,
0112                 const std::string& name) -> void {
0113                // Detray format test - manipulate for detray
0114                DetectorVolumeJsonConverter::Options detrayOptions;
0115                detrayOptions.transformOptions.writeIdentity = true;
0116                detrayOptions.transformOptions.transpose = true;
0117                detrayOptions.surfaceOptions.transformOptions =
0118                    detrayOptions.transformOptions;
0119                detrayOptions.portalOptions.surfaceOptions =
0120                    detrayOptions.surfaceOptions;
0121 
0122                auto jDetector = DetectorJsonConverter::toJsonDetray(
0123                    gctx, detector,
0124                    DetectorJsonConverter::Options{detrayOptions});
0125 
0126                // Write out the geometry, surface_grid, material
0127                auto jGeometry = jDetector["geometry"];
0128                auto jSurfaceGrids = jDetector["surface_grids"];
0129                auto jMaterial = jDetector["material"];
0130 
0131                std::ofstream out;
0132                out.open(name + "_geometry_detray.json");
0133                out << jGeometry.dump(4);
0134                out.close();
0135 
0136                out.open(name + "_surface_grids_detray.json");
0137                out << jSurfaceGrids.dump(4);
0138                out.close();
0139 
0140                out.open(name + "_material_detray.json");
0141                out << jMaterial.dump(4);
0142                out.close();
0143              });
0144   }
0145 
0146   {
0147     json.def(
0148         "readDetectorFromJson",
0149         [](const GeometryContext& gctx, const std::string& fileName) -> auto {
0150           auto in = std::ifstream(fileName,
0151                                   std::ifstream::in | std::ifstream::binary);
0152           nlohmann::json jDetectorIn;
0153           in >> jDetectorIn;
0154           in.close();
0155 
0156           return DetectorJsonConverter::fromJson(gctx, jDetectorIn);
0157         });
0158   }
0159 }