Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:19

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