Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SurfaceJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SurfaceJsonConverter.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 
31 
32 #include <algorithm>
33 
34 void Acts::to_json(nlohmann::json& j,
36  toJson(j, std::get<0>(surface), std::get<2>(surface));
37  to_json(j, std::get<1>(surface).get());
38 }
39 
40 void Acts::to_json(nlohmann::json& j, const Acts::Surface& surface) {
42  j = SurfaceJsonConverter::toJson(gctx, surface);
43 }
44 
45 void Acts::to_json(nlohmann::json& j,
46  const std::shared_ptr<const Acts::Surface>& surface) {
48  j = SurfaceJsonConverter::toJson(gctx, *(surface.get()));
49 }
50 
51 void Acts::toJson(nlohmann::json& j,
52  const std::shared_ptr<const Acts::Surface>& surface,
53  const Acts::GeometryContext& gctx) {
54  j = SurfaceJsonConverter::toJson(gctx, *(surface.get()));
55 }
56 
57 std::shared_ptr<Acts::Surface> Acts::SurfaceJsonConverter::fromJson(
58  const nlohmann::json& j) {
59  // The types to understand the types
60  auto sType = j["type"].get<Surface::SurfaceType>();
61  auto bType = j["bounds"]["type"].get<SurfaceBounds::BoundsType>();
62 
63  std::shared_ptr<Surface> mutableSf = nullptr;
64 
66  switch (sType) {
67  // Surface is a plane surface
68  case Surface::SurfaceType::Plane:
69  switch (bType) {
70  case SurfaceBounds::BoundsType::eEllipse:
71  mutableSf = surfaceFromJsonT<PlaneSurface, EllipseBounds>(j);
72  break;
73  case SurfaceBounds::BoundsType::eRectangle:
74  mutableSf = surfaceFromJsonT<PlaneSurface, RectangleBounds>(j);
75  break;
76  case SurfaceBounds::BoundsType::eTrapezoid:
77  mutableSf = surfaceFromJsonT<PlaneSurface, TrapezoidBounds>(j);
78  break;
79 
80  case SurfaceBounds::BoundsType::eBoundless:
81  mutableSf = surfaceFromJsonT<PlaneSurface, void>(j);
82  break;
83  default:
84  throw std::invalid_argument("Invalid bounds type " +
85  std::to_string(bType) +
86  " for plane surface");
87  }
88  break;
89  // Surface is a disc surface
90  case Surface::SurfaceType::Disc:
91  switch (bType) {
92  case SurfaceBounds::BoundsType::eAnnulus:
93  mutableSf = surfaceFromJsonT<DiscSurface, AnnulusBounds>(j);
94  break;
95  case SurfaceBounds::BoundsType::eDisc:
96  mutableSf = surfaceFromJsonT<DiscSurface, RadialBounds>(j);
97  break;
98  case SurfaceBounds::BoundsType::eDiscTrapezoid:
99  mutableSf = surfaceFromJsonT<DiscSurface, DiscTrapezoidBounds>(j);
100  break;
101  default:
102  throw std::invalid_argument("Invalid bounds type " +
103  std::to_string(bType) +
104  " for disc surface");
105  }
106  break;
107  // Surface is a cylinder surface
108  case Surface::SurfaceType::Cylinder:
109  mutableSf = surfaceFromJsonT<CylinderSurface, CylinderBounds>(j);
110  break;
111  // Surface is a cone surface
112  case Surface::SurfaceType::Cone:
113  mutableSf = surfaceFromJsonT<ConeSurface, ConeBounds>(j);
114  break;
115  // Surface is a straw surface
116  case Surface::SurfaceType::Straw:
117  mutableSf = surfaceFromJsonT<StrawSurface, LineBounds>(j);
118  break;
119  // Surface is a perigee surface
120  case Surface::SurfaceType::Perigee:
121  mutableSf = Surface::makeShared<PerigeeSurface>(
122  Transform3JsonConverter::fromJson(j["transform"]));
123  break;
124  default:
125  throw std::invalid_argument("Invalid surface type " +
126  std::to_string(sType));
127  }
128 
129  throw_assert(mutableSf, "Could not create surface from json");
130 
131  GeometryIdentifier geoID(j["geo_id"]);
132  mutableSf->assignGeometryId(geoID);
133  // Add material
134  if (j.find("material") != j.end() and not j["material"].empty()) {
135  const ISurfaceMaterial* surfaceMaterial = nullptr;
136  from_json(j, surfaceMaterial);
137  std::shared_ptr<const ISurfaceMaterial> sharedSurfaceMaterial(
138  surfaceMaterial);
139  mutableSf->assignSurfaceMaterial(sharedSurfaceMaterial);
140  }
141 
142  return mutableSf;
143 }
144 
146  const Surface& surface,
147  const Options& options) {
148  nlohmann::json jSurface;
149  const auto& sBounds = surface.bounds();
150  const auto sTransform = surface.transform(gctx);
151 
152  jSurface["transform"] =
154  jSurface["type"] = surface.type();
155  // Transform is always needed
156  jSurface["bounds"] = SurfaceBoundsJsonConverter::toJson(sBounds);
157  jSurface["geo_id"] = surface.geometryId().value();
158  if (surface.surfaceMaterial() != nullptr and options.writeMaterial) {
159  jSurface["material"] = nlohmann::json(surface.surfaceMaterial());
160  }
161  return jSurface;
162 }
163 
165  const GeometryContext& gctx, const Surface& surface,
166  const Options& options) {
167  nlohmann::json jSurface;
168  const auto& sBounds = surface.bounds();
169  const auto sTransform = surface.transform(gctx);
170 
171  jSurface["transform"] =
173 
174  auto jMask =
176  jSurface["mask"] = jMask;
177  jSurface["source"] = surface.geometryId().value();
178  jSurface["barcode"] = 0;
179  jSurface["type"] =
180  options.portal ? 0 : (surface.geometryId().sensitive() > 0 ? 1u : 2u);
181 
182  return jSurface;
183 }