EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryHierarchyMapJsonConverter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryHierarchyMapJsonConverter.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 
13 #include <stdexcept>
14 #include <string>
15 
16 #include <nlohmann/json.hpp>
17 
18 namespace Acts {
19 
35 template <typename value_t>
37  public:
38  using Value = value_t;
40 
44  GeometryHierarchyMapJsonConverter(std::string valueIdentifier)
45  : m_valueIdentifier(std::move(valueIdentifier)) {
46  if (m_valueIdentifier.empty()) {
47  throw std::invalid_argument("Value identifier must be non-empty");
48  }
49  }
50 
55  nlohmann::json toJson(const Container& container) const;
56 
62  Container fromJson(const nlohmann::json& encoded) const;
63 
64  private:
65  static constexpr const char* const kHeaderKey = "acts-geometry-hierarchy-map";
66  static constexpr const char* const kEntriesKey = "entries";
69  static constexpr int kFormatVersion = 0;
70 
71  std::string m_valueIdentifier;
72 
74  nlohmann::json encoded;
75  // only store non-zero identifiers
76  if (id.volume()) {
77  encoded["volume"] = id.volume();
78  }
79  if (id.boundary()) {
80  encoded["boundary"] = id.boundary();
81  }
82  if (id.layer()) {
83  encoded["layer"] = id.layer();
84  }
85  if (id.approach()) {
86  encoded["approach"] = id.approach();
87  }
88  if (id.sensitive()) {
89  encoded["sensitive"] = id.sensitive();
90  }
91  return encoded;
92  }
94  return GeometryIdentifier()
95  .setVolume(encoded.value("volume", GeometryIdentifier::Value(0u)))
96  .setBoundary(encoded.value("boundary", GeometryIdentifier::Value(0u)))
97  .setLayer(encoded.value("layer", GeometryIdentifier::Value(0u)))
98  .setApproach(encoded.value("approach", GeometryIdentifier::Value(0u)))
99  .setSensitive(
100  encoded.value("sensitive", GeometryIdentifier::Value(0u)));
101  }
102 };
103 
104 // implementations
105 
106 template <typename value_t>
108  const Container& container) const {
109  // encode header
111  encoded[kHeaderKey] = {
112  {"format-version", kFormatVersion},
113  {"value-identifier", m_valueIdentifier},
114  };
115  // encode entries
117  for (std::size_t i = 0; i < container.size(); ++i) {
118  auto entry = encodeIdentifier(container.idAt(i));
119  entry["value"] = nlohmann::json(container.valueAt(i));
120  entries.push_back(std::move(entry));
121  }
122  encoded[kEntriesKey] = std::move(entries);
123  return encoded;
124 }
125 
126 template <typename value_t>
128  const nlohmann::json& encoded) const -> Container {
129  // verify json format header
130  auto header = encoded.find(kHeaderKey);
131  if (header == encoded.end()) {
132  throw std::invalid_argument(
133  "Missing header entry in json geometry hierarchy map");
134  }
135  if (header->at("format-version").get<int>() != kFormatVersion) {
136  throw std::invalid_argument(
137  "Invalid format version in json geometry hierarchy map");
138  }
139  if (header->at("value-identifier").get<std::string>() != m_valueIdentifier) {
140  throw std::invalid_argument(
141  "Inconsistent value identifier in Json geometry hierarchy map");
142  }
143  // decode json entries
144  if (not encoded.contains(kEntriesKey)) {
145  throw std::invalid_argument(
146  "Missing entries in json geometry hierarchy map");
147  }
148  std::vector<std::pair<GeometryIdentifier, Value>> elements;
149  for (const auto& entry : encoded.at(kEntriesKey)) {
150  auto id = decodeIdentifier(entry);
151  auto value = entry.at("value").get<Value>();
152  elements.emplace_back(std::move(id), std::move(value));
153  }
154  return elements;
155 }
156 
157 } // namespace Acts