Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:03

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/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/JsonSurfacesReader.hpp"
0015 #include "Acts/Plugins/Json/MaterialMapJsonConverter.hpp"
0016 #include "Acts/Plugins/Json/ProtoDetectorJsonConverter.hpp"
0017 #include "Acts/Plugins/Python/Utilities.hpp"
0018 #include "Acts/Utilities/Logger.hpp"
0019 #include "ActsExamples/Framework/ProcessCode.hpp"
0020 #include "ActsExamples/Io/Json/JsonMaterialWriter.hpp"
0021 #include "ActsExamples/Io/Json/JsonSurfacesWriter.hpp"
0022 #include "ActsExamples/Io/Json/JsonTrackParamsLookupReader.hpp"
0023 #include "ActsExamples/Io/Json/JsonTrackParamsLookupWriter.hpp"
0024 
0025 #include <fstream>
0026 #include <initializer_list>
0027 #include <memory>
0028 #include <string>
0029 #include <tuple>
0030 #include <vector>
0031 
0032 #include <nlohmann/json.hpp>
0033 #include <pybind11/pybind11.h>
0034 #include <pybind11/stl.h>
0035 
0036 namespace Acts {
0037 class IMaterialDecorator;
0038 }  // namespace Acts
0039 namespace ActsExamples {
0040 class IMaterialWriter;
0041 class IWriter;
0042 
0043 namespace Experimental {
0044 class ITrackParamsLookupWriter;
0045 }  // namespace Experimental
0046 
0047 }  // namespace ActsExamples
0048 
0049 namespace py = pybind11;
0050 using namespace pybind11::literals;
0051 
0052 using namespace Acts;
0053 using namespace ActsExamples;
0054 
0055 namespace Acts::Python {
0056 void addJson(Context& ctx) {
0057   auto [m, mex] = ctx.get("main", "examples");
0058 
0059   {
0060     py::class_<JsonMaterialDecorator, Acts::IMaterialDecorator,
0061                std::shared_ptr<JsonMaterialDecorator>>(m,
0062                                                        "JsonMaterialDecorator")
0063         .def(py::init<const MaterialMapJsonConverter::Config&,
0064                       const std::string&, Acts::Logging::Level, bool, bool>(),
0065              py::arg("rConfig"), py::arg("jFileName"), py::arg("level"),
0066              py::arg("clearSurfaceMaterial") = true,
0067              py::arg("clearVolumeMaterial") = true);
0068   }
0069 
0070   {
0071     auto cls =
0072         py::class_<MaterialMapJsonConverter>(m, "MaterialMapJsonConverter")
0073             .def(py::init<const MaterialMapJsonConverter::Config&,
0074                           Acts::Logging::Level>(),
0075                  py::arg("config"), py::arg("level"));
0076 
0077     auto c = py::class_<MaterialMapJsonConverter::Config>(cls, "Config")
0078                  .def(py::init<>());
0079     ACTS_PYTHON_STRUCT_BEGIN(c, MaterialMapJsonConverter::Config);
0080     ACTS_PYTHON_MEMBER(context);
0081     ACTS_PYTHON_MEMBER(processSensitives);
0082     ACTS_PYTHON_MEMBER(processApproaches);
0083     ACTS_PYTHON_MEMBER(processRepresenting);
0084     ACTS_PYTHON_MEMBER(processBoundaries);
0085     ACTS_PYTHON_MEMBER(processVolumes);
0086     ACTS_PYTHON_MEMBER(processDenseVolumes);
0087     ACTS_PYTHON_MEMBER(processNonMaterial);
0088     ACTS_PYTHON_STRUCT_END();
0089   }
0090 
0091   {
0092     py::enum_<JsonFormat>(mex, "JsonFormat")
0093         .value("NoOutput", JsonFormat::NoOutput)
0094         .value("Json", JsonFormat::Json)
0095         .value("Cbor", JsonFormat::Cbor)
0096         .value("All", JsonFormat::All);
0097   }
0098 
0099   {
0100     auto cls =
0101         py::class_<JsonMaterialWriter, IMaterialWriter,
0102                    std::shared_ptr<JsonMaterialWriter>>(mex,
0103                                                         "JsonMaterialWriter")
0104             .def(py::init<const JsonMaterialWriter::Config&,
0105                           Acts::Logging::Level>(),
0106                  py::arg("config"), py::arg("level"))
0107             .def("writeMaterial", &JsonMaterialWriter::writeMaterial)
0108             .def("write", &JsonMaterialWriter::write)
0109             .def_property_readonly("config", &JsonMaterialWriter::config);
0110 
0111     auto c =
0112         py::class_<JsonMaterialWriter::Config>(cls, "Config").def(py::init<>());
0113 
0114     ACTS_PYTHON_STRUCT_BEGIN(c, JsonMaterialWriter::Config);
0115     ACTS_PYTHON_MEMBER(converterCfg);
0116     ACTS_PYTHON_MEMBER(fileName);
0117     ACTS_PYTHON_MEMBER(writeFormat);
0118     ACTS_PYTHON_STRUCT_END();
0119   }
0120 
0121   {
0122     using IWriter = ActsExamples::ITrackParamsLookupWriter;
0123     using Writer = ActsExamples::JsonTrackParamsLookupWriter;
0124     using Config = Writer::Config;
0125 
0126     auto cls = py::class_<Writer, IWriter, std::shared_ptr<Writer>>(
0127                    mex, "JsonTrackParamsLookupWriter")
0128                    .def(py::init<const Config&>(), py::arg("config"))
0129                    .def("writeLookup", &Writer::writeLookup)
0130                    .def_property_readonly("config", &Writer::config);
0131 
0132     auto c = py::class_<Config>(cls, "Config")
0133                  .def(py::init<>())
0134                  .def(py::init<const std::string&>(), py::arg("path"));
0135 
0136     ACTS_PYTHON_STRUCT_BEGIN(c, Config);
0137     ACTS_PYTHON_MEMBER(path);
0138     ACTS_PYTHON_STRUCT_END();
0139   }
0140 
0141   {
0142     using IReader = ActsExamples::ITrackParamsLookupReader;
0143     using Reader = ActsExamples::JsonTrackParamsLookupReader;
0144     using Config = Reader::Config;
0145 
0146     auto cls = py::class_<Reader, IReader, std::shared_ptr<Reader>>(
0147                    mex, "JsonTrackParamsLookupReader")
0148                    .def(py::init<const Config&>(), py::arg("config"))
0149                    .def("readLookup", &Reader::readLookup)
0150                    .def_property_readonly("config", &Reader::config);
0151 
0152     auto c = py::class_<Config>(cls, "Config")
0153                  .def(py::init<>())
0154                  .def(py::init<std::unordered_map<Acts::GeometryIdentifier,
0155                                                   const Acts::Surface*>,
0156                                std::pair<double, double>>(),
0157                       py::arg("refLayers"), py::arg("bins"));
0158 
0159     ACTS_PYTHON_STRUCT_BEGIN(c, Config);
0160     ACTS_PYTHON_MEMBER(refLayers);
0161     ACTS_PYTHON_MEMBER(bins);
0162     ACTS_PYTHON_STRUCT_END();
0163   }
0164 
0165   {
0166     auto cls =
0167         py::class_<JsonSurfacesWriter, IWriter,
0168                    std::shared_ptr<JsonSurfacesWriter>>(mex,
0169                                                         "JsonSurfacesWriter")
0170             .def(py::init<const JsonSurfacesWriter::Config&,
0171                           Acts::Logging::Level>(),
0172                  py::arg("config"), py::arg("level"))
0173             .def("write", &JsonSurfacesWriter::write)
0174             .def_property_readonly("config", &JsonSurfacesWriter::config);
0175 
0176     auto c =
0177         py::class_<JsonSurfacesWriter::Config>(cls, "Config").def(py::init<>());
0178 
0179     ACTS_PYTHON_STRUCT_BEGIN(c, JsonSurfacesWriter::Config);
0180     ACTS_PYTHON_MEMBER(trackingGeometry);
0181     ACTS_PYTHON_MEMBER(outputDir);
0182     ACTS_PYTHON_MEMBER(outputPrecision);
0183     ACTS_PYTHON_MEMBER(writeLayer);
0184     ACTS_PYTHON_MEMBER(writeApproach);
0185     ACTS_PYTHON_MEMBER(writeSensitive);
0186     ACTS_PYTHON_MEMBER(writeBoundary);
0187     ACTS_PYTHON_MEMBER(writePerEvent);
0188     ACTS_PYTHON_MEMBER(writeOnlyNames);
0189     ACTS_PYTHON_STRUCT_END();
0190   }
0191 
0192   {
0193     py::class_<Acts::ProtoDetector>(mex, "ProtoDetector")
0194         .def(py::init<>([](std::string pathName) {
0195           nlohmann::json jDetector;
0196           auto in = std::ifstream(pathName, std::ifstream::in);
0197           if (in.good()) {
0198             in >> jDetector;
0199             in.close();
0200           }
0201           Acts::ProtoDetector pDetector = jDetector["detector"];
0202           return pDetector;
0203         }));
0204   }
0205 
0206   {
0207     auto sjOptions =
0208         py::class_<Acts::JsonSurfacesReader::Options>(m, "SurfaceJsonOptions")
0209             .def(py::init<>());
0210 
0211     ACTS_PYTHON_STRUCT_BEGIN(sjOptions, Acts::JsonSurfacesReader::Options);
0212     ACTS_PYTHON_MEMBER(inputFile);
0213     ACTS_PYTHON_MEMBER(jsonEntryPath);
0214     ACTS_PYTHON_STRUCT_END();
0215 
0216     m.def("readSurfaceHierarchyMapFromJson",
0217           Acts::JsonSurfacesReader::readHierarchyMap);
0218 
0219     m.def("readSurfaceVectorFromJson", Acts::JsonSurfacesReader::readVector);
0220 
0221     py::class_<Acts::JsonDetectorElement, Acts::DetectorElementBase,
0222                std::shared_ptr<Acts::JsonDetectorElement>>(
0223         m, "JsonDetectorElement")
0224         .def("surface", [](Acts::JsonDetectorElement& self) {
0225           return self.surface().getSharedPtr();
0226         });
0227 
0228     m.def("readDetectorElementsFromJson",
0229           Acts::JsonSurfacesReader::readDetectorElements);
0230   }
0231 
0232   {
0233     mex.def("writeDetectorToJson",
0234             [](const Acts::GeometryContext& gctx,
0235                const Acts::Experimental::Detector& detector,
0236                const std::string& name) -> void {
0237               auto jDetector =
0238                   Acts::DetectorJsonConverter::toJson(gctx, detector);
0239               std::ofstream out;
0240               out.open(name + ".json");
0241               out << jDetector.dump(4);
0242               out.close();
0243             });
0244   }
0245 
0246   {
0247     mex.def("writeDetectorToJsonDetray",
0248             [](const Acts::GeometryContext& gctx,
0249                const Acts::Experimental::Detector& detector,
0250                const std::string& name) -> void {
0251               // Detray format test - manipulate for detray
0252               Acts::DetectorVolumeJsonConverter::Options detrayOptions;
0253               detrayOptions.transformOptions.writeIdentity = true;
0254               detrayOptions.transformOptions.transpose = true;
0255               detrayOptions.surfaceOptions.transformOptions =
0256                   detrayOptions.transformOptions;
0257               detrayOptions.portalOptions.surfaceOptions =
0258                   detrayOptions.surfaceOptions;
0259 
0260               auto jDetector = Acts::DetectorJsonConverter::toJsonDetray(
0261                   gctx, detector,
0262                   Acts::DetectorJsonConverter::Options{detrayOptions});
0263 
0264               // Write out the geometry, surface_grid, material
0265               auto jGeometry = jDetector["geometry"];
0266               auto jSurfaceGrids = jDetector["surface_grids"];
0267               auto jMaterial = jDetector["material"];
0268 
0269               std::ofstream out;
0270               out.open(name + "_geometry_detray.json");
0271               out << jGeometry.dump(4);
0272               out.close();
0273 
0274               out.open(name + "_surface_grids_detray.json");
0275               out << jSurfaceGrids.dump(4);
0276               out.close();
0277 
0278               out.open(name + "_material_detray.json");
0279               out << jMaterial.dump(4);
0280               out.close();
0281             });
0282   }
0283 
0284   {
0285     mex.def("readDetectorFromJson",
0286             [](const Acts::GeometryContext& gctx,
0287                const std::string& fileName) -> auto {
0288               auto in = std::ifstream(
0289                   fileName, std::ifstream::in | std::ifstream::binary);
0290               nlohmann::json jDetectorIn;
0291               in >> jDetectorIn;
0292               in.close();
0293 
0294               return Acts::DetectorJsonConverter::fromJson(gctx, jDetectorIn);
0295             });
0296   }
0297 }
0298 }  // namespace Acts::Python