12#include "detray/builders/detector_builder.hpp"
13#include "detray/io/backend/concepts.hpp"
14#include "detray/io/frontend/reader_interface.hpp"
15#include "detray/io/frontend/writer_interface.hpp"
16#include "detray/io/json/json.hpp"
17#include "detray/io/json/json_io.hpp"
18#include "detray/io/utils/file_handle.hpp"
29template <
class detector_t,
class backend_t>
40template <
class detector_t,
class backend_t>
44 using io_backend = backend_t;
55 const std::string& file_name)
override {
57 io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
60 nlohmann::json in_json;
64 io_backend::template from_payload<detector_t>(det_builder, in_json[
"data"]);
76template <
class detector_t,
class backend_t>
77 requires concepts::writer_backend<detector_t, backend_t>
78class json_converter<detector_t, backend_t> final
79 :
public writer_interface<detector_t> {
80 using io_backend = backend_t;
92 std::string
write(
const detector_t& det,
93 const typename detector_t::name_map& names,
94 const std::ios_base::openmode mode = std::ios::out |
96 const std::filesystem::path& file_path = {
"./"})
override {
98 assert(((mode == std::ios_base::out) ||
99 (mode == (std::ios_base::out | std::ios_base::binary)) ||
100 (mode == (std::ios_base::out | std::ios_base::trunc)) ||
101 (mode == (std::ios_base::out | std::ios_base::trunc |
102 std::ios_base::binary))) &&
103 "Illegal file mode for json writer");
105 std::string det_name = det.name(names);
108 std::string file_stem{det_name +
"_" + std::string(io_backend::tag)};
109 io::file_handle file{file_path / file_stem, this->file_extension(), mode};
112 nlohmann::ordered_json out_json;
113 out_json[
"header"] = io_backend::header_to_payload(det, det_name);
117 out_json[
"data"] = io_backend::to_payload(det, names);
120 *file << std::setw(4) << out_json << std::endl;
122 return file_stem + this->file_extension();
Provides functionality to build a detray detector volume by volume.
Definition detector_builder.hpp:40
Wrapper around a file stream.
Definition file_handle.hpp:42
json_converter()
Set json file extension.
Definition json_converter.hpp:48
std::string write(const detector_t &det, const typename detector_t::name_map &names, const std::ios_base::openmode mode=std::ios::out|std::ios::binary, const std::filesystem::path &file_path={"./"}) override
Writes the geometry to file with a given name.
Definition json_converter.hpp:92
void read(detector_builder< typename detector_t::metadata, volume_builder > &det_builder, const std::string &file_name) override
Writes the geometry to file with a given name.
Definition json_converter.hpp:53
Class that adds converts io payloads to json and back.
Definition json_converter.hpp:30
reader_interface()=delete
All readers must define a file extension.
writer_interface()=delete
All writers must define a file extension.
Concept for detray io reader backends.
Definition concepts.hpp:22
The following enums are global for all detectors.
Definition concepts.hpp:18