Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:03:00

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