ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
json_converter.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
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"
19
20// System include(s)
21#include <iomanip>
22#include <ios>
23#include <iostream>
24#include <string>
25
26namespace detray::io {
27
29template <class detector_t, class backend_t>
31
40template <class detector_t, class backend_t>
42class json_converter<detector_t, backend_t> final
43 : public reader_interface<detector_t> {
44 using io_backend = backend_t;
45
46 public:
48 json_converter() : reader_interface<detector_t>(".json") {}
49
54 det_builder,
55 const std::string& file_name) override {
56 // Read json from file
57 io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
58
59 // Reads the data from file and returns the corresponding io payloads
60 nlohmann::json in_json;
61 *file >> in_json;
62
63 // Add the data from the payload to the detray detector builder
64 io_backend::template from_payload<detector_t>(det_builder, in_json["data"]);
65 }
66};
67
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;
81
82 public:
84 json_converter() : writer_interface<detector_t>(".json") {}
85
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 |
95 std::ios::binary,
96 const std::filesystem::path& file_path = {"./"}) override {
97 // Assert output stream
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");
104
105 std::string det_name = det.name(names);
106
107 // Create a new file
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};
110
111 // Write some general information
112 nlohmann::ordered_json out_json;
113 out_json["header"] = io_backend::header_to_payload(det, det_name);
114
115 // Write the detector data into the json stream by using the
116 // conversion functions defined in "detray/io/json/json_io.hpp"
117 out_json["data"] = io_backend::to_payload(det, names);
118
119 // Write to file
120 *file << std::setw(4) << out_json << std::endl;
121
122 return file_stem + this->file_extension();
123 }
124};
125
126} // namespace detray::io
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