File indexing completed on 2026-05-26 07:35:05
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Io/Json/JsonGeometryList.hpp"
0010
0011 #include <filesystem>
0012 #include <fstream>
0013 #include <system_error>
0014
0015 void ActsExamples::from_json(const nlohmann::json& data,
0016 Acts::GeometryIdentifier& geoId) {
0017 Acts::GeometryIdentifier::Value null(0u);
0018 geoId = geoId.withVolume(data.value("volume", null))
0019 .withBoundary(data.value("boundary", null))
0020 .withLayer(data.value("layer", null))
0021 .withApproach(data.value("approach", null))
0022 .withSensitive(data.value("sensitive", null))
0023 .withExtra(data.value("extra", null));
0024 }
0025
0026 void ActsExamples::to_json(nlohmann::json& data,
0027 const Acts::GeometryIdentifier& geoId) {
0028 if (geoId.volume() != 0u) {
0029 data["volume"] = geoId.volume();
0030 }
0031 if (geoId.boundary() != 0u) {
0032 data["boundary"] = geoId.boundary();
0033 }
0034 if (geoId.layer() != 0u) {
0035 data["layer"] = geoId.layer();
0036 }
0037 if (geoId.approach() != 0u) {
0038 data["approach"] = geoId.approach();
0039 }
0040 if (geoId.sensitive() != 0u) {
0041 data["sensitive"] = geoId.sensitive();
0042 }
0043 if (geoId.extra() != 0u) {
0044 data["extra"] = geoId.extra();
0045 }
0046 }
0047
0048 void ActsExamples::from_json(const nlohmann::json& data,
0049 std::vector<Acts::GeometryIdentifier>& geoIdList) {
0050 for (auto& entry : data) {
0051 Acts::GeometryIdentifier geoId;
0052 from_json(entry, geoId);
0053 geoIdList.push_back(geoId);
0054 }
0055 }
0056
0057 void ActsExamples::to_json(
0058 nlohmann::json& data,
0059 const std::vector<Acts::GeometryIdentifier>& geoIdList) {
0060 for (auto& geoId : geoIdList) {
0061 nlohmann::json entry;
0062 to_json(entry, geoId);
0063 data.push_back(entry);
0064 }
0065 }
0066
0067 std::vector<Acts::GeometryIdentifier> ActsExamples::readJsonGeometryList(
0068 const std::string& path) {
0069 nlohmann::json data;
0070 std::vector<Acts::GeometryIdentifier> geoIdList;
0071 std::ifstream infile(path, std::ifstream::in | std::ifstream::binary);
0072 if (!infile.good()) {
0073 throw std::filesystem::filesystem_error(
0074 path, std::make_error_code(std::errc::no_such_file_or_directory));
0075 }
0076 infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0077 infile >> data;
0078 from_json(data, geoIdList);
0079 return geoIdList;
0080 }
0081
0082 void ActsExamples::writeJsonGeometryList(
0083 const std::vector<Acts::GeometryIdentifier>& geoIdList,
0084 const std::string& path) {
0085 nlohmann::json data;
0086 to_json(data, geoIdList);
0087 std::ofstream outfile(path, std::ofstream::out | std::ofstream::binary);
0088 outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0089 outfile << data;
0090 }