Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProtoDetectorJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ProtoDetectorJsonConverter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 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 
12 #include "Acts/Geometry/Extent.hpp"
16 
17 #include <optional>
18 #include <string>
19 #include <vector>
20 
21 void Acts::to_json(nlohmann::json& j, const Acts::ProtoVolume& pv) {
22  j["name"] = pv.name;
23  j["extent"] = pv.extent;
24 
30  auto writeBinning = [&](nlohmann::json& root,
31  const std::vector<BinningData>& binning,
32  const std::string& key) -> void {
33  nlohmann::json jbinning;
34  for (const auto& bd : binning) {
35  jbinning.push_back(bd);
36  }
37  root[key] = jbinning;
38  };
39 
40  // The internal structure
41  if (pv.internal.has_value()) {
42  auto& its = pv.internal.value();
43  nlohmann::json jinternal;
44  if (its.layerType != Surface::SurfaceType::Other) {
45  jinternal["layerType"] = static_cast<int>(its.layerType);
46  }
47  if (not its.surfaceBinning.empty()) {
48  writeBinning(jinternal, its.surfaceBinning, "surfaceBinning");
49  }
50  j["internalStructure"] = jinternal;
51  }
52 
53  // The container structure
54  if (pv.container.has_value()) {
55  auto& cts = pv.container.value();
56  nlohmann::json jcontainer;
57  nlohmann::json jconstituents;
58  for (const auto& pvc : cts.constituentVolumes) {
59  jconstituents.push_back(pvc);
60  }
61  jcontainer["constituents"] = jconstituents;
62  writeBinning(jcontainer, cts.constituentBinning, "constituentBinning");
63  jcontainer["layerContainer"] = cts.layerContainer;
64  j["containerStructure"] = jcontainer;
65  }
66 }
67 
68 void Acts::from_json(const nlohmann::json& j, Acts::ProtoVolume& pv) {
69  pv.name = j["name"];
70  pv.extent = j["extent"];
71 
77  auto readBinning = [&](const nlohmann::json& root,
78  std::vector<BinningData>& binning,
79  const std::string& key) -> void {
80  // return if no surface binning in json
81  if (root.find(key) == root.end() or root[key].is_null()) {
82  return;
83  }
84 
85  for (const auto& jbinning : root[key]) {
86  binning.push_back(jbinning);
87  }
88  };
89 
90  // The internal structure
91  if (j.find("internalStructure") != j.end() and
92  not j["internalStructure"].is_null()) {
93  auto& jinternal = j["internalStructure"];
94  Surface::SurfaceType layerType =
95  static_cast<Surface::SurfaceType>(jinternal["layerType"]);
96  std::vector<BinningData> surfaceBinning;
97  readBinning(jinternal, surfaceBinning, "surfaceBinning");
98  pv.internal = ProtoVolume::InternalStructure{layerType, surfaceBinning};
99  }
100 
101  // The container structure
102  if (j.find("containerStructure") != j.end() and
103  not j["containerStructure"].is_null()) {
104  std::vector<ProtoVolume> constituentVolumes;
105  auto& jcontainer = j["containerStructure"];
106  for (const auto& jc : jcontainer["constituents"]) {
107  constituentVolumes.push_back(jc);
108  }
109  std::vector<BinningData> constituentBinning;
110  readBinning(jcontainer, constituentBinning, "constituentBinning");
111  bool layerContainer = jcontainer["layerContainer"];
113  constituentVolumes, constituentBinning, layerContainer};
114  }
115 }
116 
117 void Acts::to_json(nlohmann::json& j, const Acts::ProtoDetector& pd) {
118  j["name"] = pd.name;
119  j["world"] = pd.worldVolume;
120 }
121 
122 void Acts::from_json(const nlohmann::json& j, Acts::ProtoDetector& pd) {
123  pd.name = j["name"];
124  pd.worldVolume = j["world"];
125 }