Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SurfaceJsonConverter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SurfaceJsonConverter.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021-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 
9 #pragma once
10 
17 
18 #include <memory>
19 #include <string>
20 #include <tuple>
21 #include <utility>
22 #include <vector>
23 
24 #include <nlohmann/json.hpp>
25 
26 // Custom Json encoder/decoders. Naming is mandated by nlohmann::json and thus
27 // can not match our naming guidelines.
28 namespace Acts {
29 class ISurfaceMaterial;
30 
32  std::tuple<std::shared_ptr<const Acts::Surface>,
33  std::shared_ptr<const Acts::ISurfaceMaterial>,
35 
37 void to_json(nlohmann::json& j, const SurfaceAndMaterialWithContext& surface);
38 
42 void to_json(nlohmann::json& j, const Surface& surface);
43 
47 void to_json(nlohmann::json& j, const std::shared_ptr<const Surface>& surface);
48 
54 void toJson(nlohmann::json& j, const std::shared_ptr<const Surface>& surface,
56 
62 std::shared_ptr<Surface> surfaceFromJson(const nlohmann::json& j);
63 
72 template <typename surface_t, typename bounds_t>
73 std::shared_ptr<surface_t> surfaceFromJsonT(const nlohmann::json& j) {
74  nlohmann::json jTransform = j["transform"];
76  if constexpr (std::is_same_v<bounds_t, void>) {
77  return Surface::makeShared<surface_t>(sTransform);
78  } else {
79  nlohmann::json jBounds = j["bounds"];
80  auto sBounds = SurfaceBoundsJsonConverter::fromJson<bounds_t>(jBounds);
81  return Surface::makeShared<surface_t>(sTransform, std::move(sBounds));
82  }
83 }
84 
85 namespace SurfaceJsonConverter {
86 
87 struct Options {
88  // The way the transforms are written out
91  // Write the material
92  bool writeMaterial = true;
93  // Write surface as a portal
94  bool portal = false;
95 };
96 
104 nlohmann::json toJson(const GeometryContext& gctx, const Surface& surface,
105  const Options& options = Options{});
106 
116 nlohmann::json toJsonDetray(const GeometryContext& gctx, const Surface& surface,
117  const Options& options = Options{});
118 
124 std::shared_ptr<Surface> fromJson(const nlohmann::json& jSurface);
125 
126 } // namespace SurfaceJsonConverter
127 
128 // This macro create a conversion for the surface type
129 NLOHMANN_JSON_SERIALIZE_ENUM(
131  {{Surface::SurfaceType::Cone, "ConeSurface"},
132  {Surface::SurfaceType::Cylinder, "CylinderSurface"},
133  {Surface::SurfaceType::Disc, "DiscSurface"},
134  {Surface::SurfaceType::Perigee, "PerigeeSurface"},
135  {Surface::SurfaceType::Plane, "PlaneSurface"},
136  {Surface::SurfaceType::Straw, "StrawSurface"},
137  {Surface::SurfaceType::Curvilinear, "CurvilinearSurface"},
138  {Surface::SurfaceType::Other, "Other"}})
139 
140 } // namespace Acts