Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonGeometryList.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JsonGeometryList.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
10 
11 #include <fstream>
12 #include <initializer_list>
13 
14 void ActsExamples::from_json(const nlohmann::json& data,
15  Acts::GeometryIdentifier& geoId) {
17  geoId.setVolume(data.value("volume", null));
18  geoId.setBoundary(data.value("boundary", null));
19  geoId.setLayer(data.value("layer", null));
20  geoId.setApproach(data.value("approach", null));
21  geoId.setSensitive(data.value("sensitive", null));
22 }
23 
24 void ActsExamples::to_json(nlohmann::json& data,
25  const Acts::GeometryIdentifier& geoId) {
26  if (geoId.volume() != 0u) {
27  data["volume"] = geoId.volume();
28  }
29  if (geoId.boundary() != 0u) {
30  data["boundary"] = geoId.boundary();
31  }
32  if (geoId.layer() != 0u) {
33  data["layer"] = geoId.layer();
34  }
35  if (geoId.approach() != 0u) {
36  data["approach"] = geoId.approach();
37  }
38  if (geoId.sensitive() != 0u) {
39  data["sensitive"] = geoId.sensitive();
40  }
41 }
42 
43 void ActsExamples::from_json(const nlohmann::json& data,
44  std::vector<Acts::GeometryIdentifier>& geoIdList) {
45  for (auto& entry : data) {
47  from_json(entry, geoId);
48  geoIdList.push_back(geoId);
49  }
50 }
51 
53  nlohmann::json& data,
54  const std::vector<Acts::GeometryIdentifier>& geoIdList) {
55  for (auto& geoId : geoIdList) {
56  nlohmann::json entry;
57  to_json(entry, geoId);
58  data.push_back(entry);
59  }
60 }
61 
62 std::vector<Acts::GeometryIdentifier> ActsExamples::readJsonGeometryList(
63  const std::string& path) {
64  nlohmann::json data;
65  std::vector<Acts::GeometryIdentifier> geoIdList;
66  std::ifstream infile(path, std::ifstream::in | std::ifstream::binary);
67  infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
68  infile >> data;
69  from_json(data, geoIdList);
70  return geoIdList;
71 }
72 
74  const std::vector<Acts::GeometryIdentifier>& geoIdList,
75  const std::string& path) {
76  nlohmann::json data;
77  to_json(data, geoIdList);
78  std::ofstream outfile(path, std::ofstream::out | std::ofstream::binary);
79  outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
80  outfile << data;
81 }