Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:07

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 #pragma once
0010 
0011 // Project include(s)
0012 #include "detray/builders/detector_builder.hpp"
0013 #include "detray/io/backend/concepts.hpp"
0014 #include "detray/io/frontend/reader_interface.hpp"
0015 #include "detray/io/frontend/writer_interface.hpp"
0016 #include "detray/io/json/json.hpp"
0017 #include "detray/io/json/json_io.hpp"
0018 #include "detray/io/utils/file_handle.hpp"
0019 
0020 // System include(s)
0021 #include <ios>
0022 #include <iostream>
0023 #include <string>
0024 
0025 namespace detray::io {
0026 
0027 /// @brief Class that adds converts io payloads to json and back
0028 template <class detector_t, class backend_t>
0029 class json_converter {};
0030 
0031 /// @brief Class that adds json functionality to backend reader types.
0032 ///
0033 /// Assemble the json readers from the backend reader types, which handle the
0034 /// volume builders, and this class, which provides the payload data from the
0035 /// json stream. It also includes the respective @c to_json and @c from_json
0036 /// functions for the payloads ("json_serializers").
0037 ///
0038 /// @note The resulting reader types will fulfill @c reader_interface
0039 template <class detector_t, class backend_t>
0040   requires concepts::reader_backend<detector_t, backend_t>
0041 class json_converter<detector_t, backend_t> final
0042     : public reader_interface<detector_t> {
0043   using io_backend = backend_t;
0044 
0045  public:
0046   /// Set json file extension
0047   json_converter() : reader_interface<detector_t>(".json") {}
0048 
0049   /// Writes the geometry to file with a given name
0050   void read(detector_builder<typename detector_t::metadata, volume_builder>&
0051                 det_builder,
0052             const std::string& file_name) override {
0053     // Read json from file
0054     io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
0055 
0056     // Reads the data from file and returns the corresponding io payloads
0057     nlohmann::json in_json;
0058     *file >> in_json;
0059 
0060     // Add the data from the payload to the detray detector builder
0061     io_backend::template from_payload<detector_t>(det_builder, in_json["data"]);
0062   }
0063 };
0064 
0065 /// @brief Class that adds json functionality to backend writer types.
0066 ///
0067 /// Assemble the json writers from the backend writer types, which serialize a
0068 /// detector into the io payloads, and this class, which does the file
0069 /// handling and provides the json stream. It also includes the respective
0070 /// @c to_json and @c from_json functions for the payloads ("json_serializers").
0071 ///
0072 /// @note The resulting writer types will fulfill @c writer_interface
0073 template <class detector_t, class backend_t>
0074   requires concepts::writer_backend<detector_t, backend_t>
0075 class json_converter<detector_t, backend_t> final
0076     : public writer_interface<detector_t> {
0077   using io_backend = backend_t;
0078 
0079  public:
0080   /// File gets created with the json file extension
0081   json_converter() : writer_interface<detector_t>(".json") {}
0082 
0083   /// Writes the geometry to file with a given name
0084   std::string write(const detector_t& det,
0085                     const typename detector_t::name_map& names,
0086                     const std::ios_base::openmode mode = std::ios::out |
0087                                                          std::ios::binary,
0088                     const std::filesystem::path& file_path = {"./"}) override {
0089     // Assert output stream
0090     assert(((mode == std::ios_base::out) ||
0091             (mode == (std::ios_base::out | std::ios_base::binary)) ||
0092             (mode == (std::ios_base::out | std::ios_base::trunc)) ||
0093             (mode == (std::ios_base::out | std::ios_base::trunc |
0094                       std::ios_base::binary))) &&
0095            "Illegal file mode for json writer");
0096 
0097     std::string det_name = det.name(names);
0098 
0099     // Create a new file
0100     std::string file_stem{det_name + "_" + std::string(io_backend::tag)};
0101     io::file_handle file{file_path / file_stem, this->file_extension(), mode};
0102 
0103     // Write some general information
0104     nlohmann::ordered_json out_json;
0105     out_json["header"] = io_backend::header_to_payload(det, det_name);
0106 
0107     // Write the detector data into the json stream by using the
0108     // conversion functions defined in "detray/io/json/json_io.hpp"
0109     out_json["data"] = io_backend::to_payload(det, names);
0110 
0111     // Write to file
0112     *file << std::setw(4) << out_json << std::endl;
0113 
0114     return file_stem + this->file_extension();
0115   }
0116 };
0117 
0118 }  // namespace detray::io