Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Json.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Json.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
22 
23 #include <fstream>
24 #include <initializer_list>
25 #include <memory>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29 
30 #include <nlohmann/json.hpp>
31 #include <pybind11/pybind11.h>
32 #include <pybind11/stl.h>
33 
34 namespace Acts {
35 class IMaterialDecorator;
36 } // namespace Acts
37 namespace ActsExamples {
38 class IMaterialWriter;
39 class IWriter;
40 } // namespace ActsExamples
41 
42 namespace py = pybind11;
43 using namespace pybind11::literals;
44 
45 using namespace Acts;
46 using namespace ActsExamples;
47 
48 namespace Acts::Python {
49 void addJson(Context& ctx) {
50  auto [m, mex] = ctx.get("main", "examples");
51 
52  {
54  std::shared_ptr<JsonMaterialDecorator>>(m,
55  "JsonMaterialDecorator")
57  const std::string&, Acts::Logging::Level, bool, bool>(),
58  py::arg("rConfig"), py::arg("jFileName"), py::arg("level"),
59  py::arg("clearSurfaceMaterial") = true,
60  py::arg("clearVolumeMaterial") = true);
61  }
62 
63  {
64  auto cls =
65  py::class_<MaterialMapJsonConverter>(m, "MaterialMapJsonConverter")
68  py::arg("config"), py::arg("level"));
69 
70  auto c = py::class_<MaterialMapJsonConverter::Config>(cls, "Config")
71  .def(py::init<>());
73  ACTS_PYTHON_MEMBER(context);
74  ACTS_PYTHON_MEMBER(processSensitives);
75  ACTS_PYTHON_MEMBER(processApproaches);
76  ACTS_PYTHON_MEMBER(processRepresenting);
77  ACTS_PYTHON_MEMBER(processBoundaries);
78  ACTS_PYTHON_MEMBER(processVolumes);
79  ACTS_PYTHON_MEMBER(processDenseVolumes);
80  ACTS_PYTHON_MEMBER(processNonMaterial);
82  }
83 
84  {
85  py::enum_<JsonFormat>(mex, "JsonFormat")
86  .value("NoOutput", JsonFormat::NoOutput)
87  .value("Json", JsonFormat::Json)
88  .value("Cbor", JsonFormat::Cbor)
89  .value("All", JsonFormat::All);
90  }
91 
92  {
93  auto cls =
95  std::shared_ptr<JsonMaterialWriter>>(mex,
96  "JsonMaterialWriter")
99  py::arg("config"), py::arg("level"))
100  .def("writeMaterial", &JsonMaterialWriter::writeMaterial)
101  .def("write", &JsonMaterialWriter::write)
102  .def_property_readonly("config", &JsonMaterialWriter::config);
103 
104  auto c =
105  py::class_<JsonMaterialWriter::Config>(cls, "Config").def(py::init<>());
106 
108  ACTS_PYTHON_MEMBER(converterCfg);
109  ACTS_PYTHON_MEMBER(fileName);
110  ACTS_PYTHON_MEMBER(writeFormat);
112  }
113 
114  {
115  auto cls =
116  py::class_<JsonSurfacesWriter, IWriter,
117  std::shared_ptr<JsonSurfacesWriter>>(mex,
118  "JsonSurfacesWriter")
121  py::arg("config"), py::arg("level"))
122  .def("write", &JsonSurfacesWriter::write)
123  .def_property_readonly("config", &JsonSurfacesWriter::config);
124 
125  auto c =
126  py::class_<JsonSurfacesWriter::Config>(cls, "Config").def(py::init<>());
127 
131  ACTS_PYTHON_MEMBER(outputPrecision);
132  ACTS_PYTHON_MEMBER(writeLayer);
133  ACTS_PYTHON_MEMBER(writeApproach);
134  ACTS_PYTHON_MEMBER(writeSensitive);
135  ACTS_PYTHON_MEMBER(writeBoundary);
136  ACTS_PYTHON_MEMBER(writePerEvent);
137  ACTS_PYTHON_MEMBER(writeOnlyNames);
139  }
140 
141  {
142  py::class_<Acts::ProtoDetector>(mex, "ProtoDetector")
143  .def(py::init<>([](std::string pathName) {
144  nlohmann::json jDetector;
145  auto in = std::ifstream(pathName, std::ifstream::in);
146  if (in.good()) {
147  in >> jDetector;
148  in.close();
149  }
150  Acts::ProtoDetector pDetector = jDetector["detector"];
151  return pDetector;
152  }));
153  }
154 
155  {
156  auto sjOptions = py::class_<ActsExamples::JsonSurfacesReader::Options>(
157  mex, "SurfaceJsonOptions")
158  .def(py::init<>());
159 
160  ACTS_PYTHON_STRUCT_BEGIN(sjOptions,
162  ACTS_PYTHON_MEMBER(inputFile);
163  ACTS_PYTHON_MEMBER(jsonEntryPath);
165 
166  mex.def("readSurfaceFromJson", ActsExamples::JsonSurfacesReader::read);
167  }
168 
169  {
170  mex.def("writeDetectorToJson",
171  [](const Acts::GeometryContext& gctx,
172  const Acts::Experimental::Detector& detector) -> void {
173  auto jDetector =
174  Acts::DetectorJsonConverter::toJson(gctx, detector);
175  std::ofstream out;
176  out.open(detector.name() + ".json");
177  out << jDetector.dump(4);
178  out.close();
179  });
180  }
181 
182  {
183  mex.def("writeDetectorToJsonDetray",
184  [](const Acts::GeometryContext& gctx,
185  const Acts::Experimental::Detector& detector) -> void {
186  // Detray format test - manipulate for detray
188  detrayOptions.transformOptions.writeIdentity = true;
189  detrayOptions.transformOptions.transpose = true;
190  detrayOptions.surfaceOptions.transformOptions =
191  detrayOptions.transformOptions;
192  detrayOptions.portalOptions.surfaceOptions =
193  detrayOptions.surfaceOptions;
194 
196  gctx, detector,
197  Acts::DetectorJsonConverter::Options{detrayOptions});
198  std::ofstream out;
199  out.open(detector.name() + "_detray.json");
200  out << jDetector.dump(4);
201  out.close();
202  });
203  }
204 
205  {
206  mex.def(
207  "readDetectorFromJson",
208  [](const Acts::GeometryContext& gctx,
209  const std::string& fileName) -> auto{
210  auto in = std::ifstream(fileName,
211  std::ifstream::in | std::ifstream::binary);
212  nlohmann::json jDetectorIn;
213  in >> jDetectorIn;
214  in.close();
215 
216  return Acts::DetectorJsonConverter::fromJson(gctx, jDetectorIn);
217  });
218  }
219 }
220 } // namespace Acts::Python