Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:19

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/Plugins/Json/SurfaceJsonConverter.hpp"
0010 
0011 #include "Acts/Geometry/GeometryIdentifier.hpp"
0012 #include "Acts/Material/ISurfaceMaterial.hpp"
0013 #include "Acts/Plugins/Json/DetrayJsonHelper.hpp"
0014 #include "Acts/Plugins/Json/MaterialJsonConverter.hpp"
0015 #include "Acts/Surfaces/AnnulusBounds.hpp"
0016 #include "Acts/Surfaces/ConeBounds.hpp"
0017 #include "Acts/Surfaces/ConeSurface.hpp"
0018 #include "Acts/Surfaces/CylinderBounds.hpp"
0019 #include "Acts/Surfaces/CylinderSurface.hpp"
0020 #include "Acts/Surfaces/DiscSurface.hpp"
0021 #include "Acts/Surfaces/DiscTrapezoidBounds.hpp"
0022 #include "Acts/Surfaces/EllipseBounds.hpp"
0023 #include "Acts/Surfaces/LineBounds.hpp"
0024 #include "Acts/Surfaces/PerigeeSurface.hpp"
0025 #include "Acts/Surfaces/PlaneSurface.hpp"
0026 #include "Acts/Surfaces/RadialBounds.hpp"
0027 #include "Acts/Surfaces/RectangleBounds.hpp"
0028 #include "Acts/Surfaces/StrawSurface.hpp"
0029 #include "Acts/Surfaces/SurfaceBounds.hpp"
0030 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0031 
0032 #include <algorithm>
0033 
0034 void Acts::to_json(nlohmann::json& j,
0035                    const Acts::SurfaceAndMaterialWithContext& surface) {
0036   toJson(j, std::get<0>(surface), std::get<2>(surface));
0037   to_json(j, std::get<1>(surface).get());
0038 }
0039 
0040 void Acts::to_json(nlohmann::json& j, const Acts::Surface& surface) {
0041   Acts::GeometryContext gctx;
0042   j = SurfaceJsonConverter::toJson(gctx, surface);
0043 }
0044 
0045 void Acts::to_json(nlohmann::json& j,
0046                    const std::shared_ptr<const Acts::Surface>& surface) {
0047   Acts::GeometryContext gctx;
0048   j = SurfaceJsonConverter::toJson(gctx, *(surface.get()));
0049 }
0050 
0051 void Acts::toJson(nlohmann::json& j,
0052                   const std::shared_ptr<const Acts::Surface>& surface,
0053                   const Acts::GeometryContext& gctx) {
0054   j = SurfaceJsonConverter::toJson(gctx, *(surface.get()));
0055 }
0056 
0057 std::shared_ptr<Acts::Surface> Acts::SurfaceJsonConverter::fromJson(
0058     const nlohmann::json& j) {
0059   // The types to understand the types
0060   auto sType = j["type"].get<Surface::SurfaceType>();
0061   auto bType = j["bounds"]["type"].get<SurfaceBounds::BoundsType>();
0062 
0063   std::shared_ptr<Surface> mutableSf = nullptr;
0064 
0065   /// Unroll the types
0066   switch (sType) {
0067     // Surface is a plane surface
0068     case Surface::SurfaceType::Plane:
0069       switch (bType) {
0070         case SurfaceBounds::BoundsType::eEllipse:
0071           mutableSf = surfaceFromJsonT<PlaneSurface, EllipseBounds>(j);
0072           break;
0073         case SurfaceBounds::BoundsType::eRectangle:
0074           mutableSf = surfaceFromJsonT<PlaneSurface, RectangleBounds>(j);
0075           break;
0076         case SurfaceBounds::BoundsType::eTrapezoid:
0077           mutableSf = surfaceFromJsonT<PlaneSurface, TrapezoidBounds>(j);
0078           break;
0079 
0080         case SurfaceBounds::BoundsType::eBoundless:
0081           mutableSf = surfaceFromJsonT<PlaneSurface, void>(j);
0082           break;
0083         default:
0084           throw std::invalid_argument("Invalid bounds type " +
0085                                       std::to_string(bType) +
0086                                       " for plane surface");
0087       }
0088       break;
0089     // Surface is a disc surface
0090     case Surface::SurfaceType::Disc:
0091       switch (bType) {
0092         case SurfaceBounds::BoundsType::eAnnulus:
0093           mutableSf = surfaceFromJsonT<DiscSurface, AnnulusBounds>(j);
0094           break;
0095         case SurfaceBounds::BoundsType::eDisc:
0096           mutableSf = surfaceFromJsonT<DiscSurface, RadialBounds>(j);
0097           break;
0098         case SurfaceBounds::BoundsType::eDiscTrapezoid:
0099           mutableSf = surfaceFromJsonT<DiscSurface, DiscTrapezoidBounds>(j);
0100           break;
0101         default:
0102           throw std::invalid_argument("Invalid bounds type " +
0103                                       std::to_string(bType) +
0104                                       " for disc surface");
0105       }
0106       break;
0107     // Surface is a cylinder surface
0108     case Surface::SurfaceType::Cylinder:
0109       mutableSf = surfaceFromJsonT<CylinderSurface, CylinderBounds>(j);
0110       break;
0111     // Surface is a cone surface
0112     case Surface::SurfaceType::Cone:
0113       mutableSf = surfaceFromJsonT<ConeSurface, ConeBounds>(j);
0114       break;
0115     // Surface is a straw surface
0116     case Surface::SurfaceType::Straw:
0117       mutableSf = surfaceFromJsonT<StrawSurface, LineBounds>(j);
0118       break;
0119     // Surface is a perigee surface
0120     case Surface::SurfaceType::Perigee:
0121       mutableSf = Surface::makeShared<PerigeeSurface>(
0122           Transform3JsonConverter::fromJson(j["transform"]));
0123       break;
0124     default:
0125       throw std::invalid_argument("Invalid surface type " +
0126                                   std::to_string(sType));
0127   }
0128 
0129   throw_assert(mutableSf, "Could not create surface from json");
0130 
0131   GeometryIdentifier geoID(j["geo_id"]);
0132   mutableSf->assignGeometryId(geoID);
0133   // Add material
0134   if (j.find("material") != j.end() && !j["material"].empty()) {
0135     const ISurfaceMaterial* surfaceMaterial = nullptr;
0136     from_json(j, surfaceMaterial);
0137     std::shared_ptr<const ISurfaceMaterial> sharedSurfaceMaterial(
0138         surfaceMaterial);
0139     mutableSf->assignSurfaceMaterial(sharedSurfaceMaterial);
0140   }
0141 
0142   return mutableSf;
0143 }
0144 
0145 nlohmann::json Acts::SurfaceJsonConverter::toJson(const GeometryContext& gctx,
0146                                                   const Surface& surface,
0147                                                   const Options& options) {
0148   nlohmann::json jSurface;
0149   const auto& sBounds = surface.bounds();
0150   const auto sTransform = surface.transform(gctx);
0151 
0152   jSurface["transform"] =
0153       Transform3JsonConverter::toJson(sTransform, options.transformOptions);
0154   jSurface["type"] = surface.type();
0155   // Transform is always needed
0156   jSurface["bounds"] = SurfaceBoundsJsonConverter::toJson(sBounds);
0157   jSurface["geo_id"] = surface.geometryId().value();
0158   if (surface.surfaceMaterial() != nullptr && options.writeMaterial) {
0159     jSurface["material"] = nlohmann::json(surface.surfaceMaterial());
0160   }
0161   return jSurface;
0162 }
0163 
0164 nlohmann::json Acts::SurfaceJsonConverter::toJsonDetray(
0165     const GeometryContext& gctx, const Surface& surface,
0166     const Options& options) {
0167   nlohmann::json jSurface;
0168   const auto& sBounds = surface.bounds();
0169   const auto sTransform = surface.transform(gctx);
0170 
0171   jSurface["transform"] =
0172       Transform3JsonConverter::toJson(sTransform, options.transformOptions);
0173 
0174   auto jMask =
0175       SurfaceBoundsJsonConverter::toJsonDetray(sBounds, options.portal);
0176   jSurface["mask"] = jMask;
0177   jSurface["source"] = surface.geometryId().value();
0178   jSurface["barcode"] = 0;
0179   jSurface["type"] =
0180       options.portal ? 0 : (surface.geometryId().sensitive() > 0 ? 1u : 2u);
0181 
0182   return jSurface;
0183 }