Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DetectorJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DetectorJsonConverter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 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 
13 #include "Acts/Detector/Portal.hpp"
20 
21 #include <algorithm>
22 #include <ctime>
23 #include <set>
24 
27  const Options& options) {
28  // Get the time stamp
29  std::time_t tt = 0;
30  std::time(&tt);
31  auto ti = std::localtime(&tt);
32 
33  nlohmann::json jDetector;
34 
35  std::size_t nSurfaces = 0;
36  std::vector<const Experimental::Portal*> portals;
37 
38  for (const auto* volume : detector.volumes()) {
39  nSurfaces += volume->surfaces().size();
40  for (const auto& portal : volume->portals()) {
41  if (std::find(portals.begin(), portals.end(), portal) == portals.end()) {
42  portals.push_back(portal);
43  }
44  }
45  }
46 
47  // Write the actual data objects
48  nlohmann::json jData;
49  jData["name"] = detector.name();
50 
51  // The portals are written first
52  auto volumes = detector.volumes();
53  nlohmann::json jPortals;
54 
55  for (const auto& portal : portals) {
56  auto jPortal = PortalJsonConverter::toJson(
57  gctx, *portal, volumes, options.volumeOptions.portalOptions);
58  jPortals.push_back(jPortal);
59  }
60  jData["portals"] = jPortals;
61 
62  // The volumes are written next, with portals already defined, the volumes
63  // will only index those
64  nlohmann::json jVolumes;
65  for (const auto& volume : volumes) {
67  gctx, *volume, volumes, portals, options.volumeOptions);
68  jVolumes.push_back(jVolume);
69  }
70  jData["volumes"] = jVolumes;
71 
72  // Write the header
73  nlohmann::json jHeader;
74  jHeader["detector"] = detector.name();
75  jHeader["type"] = "acts";
76  jHeader["date"] = std::asctime(ti);
77  jHeader["surface_count"] = nSurfaces;
78  jHeader["portal_count"] = portals.size();
79  jHeader["volume_count"] = detector.volumes().size();
80  jDetector["header"] = jHeader;
81  jDetector["data"] = jData;
82  return jDetector;
83 }
84 
87  const Options& options) {
88  // Get the time stamp
89  std::time_t tt = 0;
90  std::time(&tt);
91  auto ti = std::localtime(&tt);
92 
93  nlohmann::json jDetector;
94  nlohmann::json jData;
95 
96  std::size_t nSurfaces = 0;
97 
98  auto volumes = detector.volumes();
99 
100  // Convert the volumes
101  nlohmann::json jVolumes;
102  for (const auto& volume : volumes) {
104  gctx, *volume, volumes, options.volumeOptions);
105  jVolumes.push_back(jVolume);
106  if (jVolume.find("surfaces") != jVolume.end() and
107  jVolume["surfaces"].is_array()) {
108  nSurfaces += jVolume["surfaces"].size();
109  }
110  }
111  jData["volumes"] = jVolumes;
112 
114  nlohmann::json jVolumeGridParent;
115  nlohmann::json jVolumeGrid;
116  nlohmann::json jVolumeGridAxes;
117  jVolumeGrid["axes"] = jVolumeGridAxes;
118  jVolumeGrid["entries"] = std::vector<std::size_t>{};
119  jVolumeGridParent["grid"] = jVolumeGrid;
120  jData["volume_grid"] = jVolumeGridParent;
121 
122  // Get the time stamp
123  // Write the header
124  nlohmann::json jHeader;
125  jHeader["detector"] = detector.name();
126  jHeader["date"] = std::asctime(ti);
127  jHeader["volume_count"] = detector.volumes().size();
128 
129  // For detray, the number of surfaces and portals are collected
130  // from the translated volumes
131  jHeader["type"] = "detray";
132  jHeader["surface_count"] = nSurfaces;
133  jDetector["header"] = jHeader;
134  jDetector["data"] = jData;
135 
136  return jDetector;
137 }
138 
139 std::shared_ptr<Acts::Experimental::Detector>
141  const nlohmann::json& jDetector) {
142  // Read back all the data
143  auto jData = jDetector["data"];
144  auto jVolumes = jData["volumes"];
145  auto jPortals = jData["portals"];
146  const std::string name = jData["name"];
147 
148  std::vector<std::shared_ptr<Experimental::DetectorVolume>> volumes;
149  std::vector<std::shared_ptr<Experimental::Portal>> portals;
150 
151  for (const auto& jVolume : jVolumes) {
152  auto volume = DetectorVolumeJsonConverter::fromJson(gctx, jVolume);
153  volumes.push_back(volume);
154  }
155 
156  for (const auto& jPortal : jPortals) {
157  auto portal = PortalJsonConverter::fromJson(gctx, jPortal, volumes);
158  portals.push_back(portal);
159  }
160 
161  // Patch all the portals of the volumes
162  for (auto [iv, v] : enumerate(volumes)) {
163  // Get the Volumes and update the portals with the loaded ones
164  auto jVolume = jVolumes[iv];
165  std::vector<std::size_t> portalLinks = jVolume["portal_links"];
166  for (auto [ip, ipl] : enumerate(portalLinks)) {
167  auto portal = portals[ipl];
168  v->updatePortal(portal, ip);
169  }
170  }
171  return Experimental::Detector::makeShared(name, volumes,
173 }