ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
json_readers.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/core/concepts.hpp"
13#include "detray/io/backend/geometry_reader.hpp"
14#include "detray/io/backend/homogeneous_material_reader.hpp"
15#include "detray/io/backend/material_map_reader.hpp"
16#include "detray/io/backend/surface_grid_reader.hpp"
17#include "detray/io/frontend/detail/detector_components_reader.hpp"
18#include "detray/io/frontend/payloads.hpp"
19#include "detray/io/json/json_converter.hpp"
20#include "detray/io/utils/io_metadata.hpp"
21#include "detray/utils/logging.hpp"
22
23// System include(s)
24#include <filesystem>
25#include <ios>
26#include <stdexcept>
27#include <string>
28#include <vector>
29
30namespace detray::io::detail {
31
33inline common_header_payload deserialize_json_header(
34 const std::string& file_name) {
35 // Read json file
36 io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
37 nlohmann::json in_json;
38 *file >> in_json;
39
40 // Reads the header from file
41 header_payload<> h = in_json["header"];
42
43 // Need only the common part here
44 const common_header_payload& header = h.common;
45
46 if (header.tag < io::detail::minimal_io_version) {
47 DETRAY_WARN_HOST("File was generated with a different detray version");
48 }
49
50 return header;
51}
52
62template <std::size_t CAP, std::size_t DIM, class detector_t>
63inline void add_json_readers(
64 io::detail::detector_components_reader<detector_t>& reader,
65 const std::vector<std::string>& files) noexcept(false) {
66 for (const std::filesystem::path file_name : files) {
67 if (file_name.empty()) {
68 DETRAY_WARN_HOST("Empty file name. Component will not be built");
69 continue;
70 }
71
72 // Only add readers for json files
73 if (file_name.extension() != ".json") {
74 continue;
75 }
76
77 // Peek at the header to determine the kind of reader that is needed
78 auto header = deserialize_json_header(file_name);
79
80 if (header.tag == "geometry") {
81 reader.set_detector_name(header.detector);
82
83 using json_geometry_reader = json_converter<detector_t, geometry_reader>;
84
85 reader.template add_geometry_reader<json_geometry_reader>(
86 static_cast<std::string>(file_name));
87
88 } else if (header.tag == "homogeneous_material") {
89 if constexpr (detray::concepts::has_homogeneous_material<detector_t>) {
90 using json_hom_material_reader =
91 json_converter<detector_t, homogeneous_material_reader>;
92
93 reader.template add<json_hom_material_reader>(file_name);
94 } else {
95 print_type_warning<detector_t>(header.tag);
96 }
97 } else if (header.tag == "material_maps") {
98 if constexpr (detray::concepts::has_material_maps<detector_t>) {
99 using json_material_map_reader = json_converter<
100 detector_t,
101 material_map_reader<std::integral_constant<std::size_t, DIM>>>;
102
103 reader.template add<json_material_map_reader>(file_name);
104 } else {
105 print_type_warning<detector_t>(header.tag);
106 }
107 } else if (header.tag == "surface_grids") {
108 if constexpr (detray::concepts::has_surface_grids<detector_t>) {
109 using json_surface_grid_reader = json_converter<
110 detector_t,
111 surface_grid_reader<typename detector_t::surface_type,
112 std::integral_constant<std::size_t, CAP>,
113 std::integral_constant<std::size_t, DIM>>>;
114
115 reader.template add<json_surface_grid_reader>(file_name);
116 } else {
117 print_type_warning<detector_t>(header.tag);
118 }
119 } else {
120 throw std::invalid_argument("Unsupported file tag '" + header.tag +
121 "' in input file: " + file_name.string());
122 }
123 }
124}
125
126} // namespace detray::io::detail
constexpr double h
Hour - 3600 seconds.
Definition Units.hpp:134