Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-11 09:40:22

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 #pragma once
0010 
0011 #include "Acts/Geometry/GeometryIdentifier.hpp"
0012 #include "ActsPlugins/Json/ActsJson.hpp"
0013 
0014 #include <nlohmann/json.hpp>
0015 
0016 // Custom Json encoder/decoders. Naming is mandated by nlohmann::json and thus
0017 // can not match our naming guidelines.
0018 namespace Acts {
0019 
0020 namespace GeometryIdentifierJsonConverter {
0021 /// Encode the geometry identifier
0022 /// @param geoId is the geometry identifier that will be encoded
0023 /// @param compact if true, the raw value is stored
0024 inline nlohmann::json encodeIdentifier(const GeometryIdentifier& geoId,
0025                                        bool compact = false) {
0026   nlohmann::json encoded;
0027   if (compact) {
0028     // store the raw value directly
0029     encoded = geoId.value();
0030     return encoded;
0031   }
0032 
0033   // only store non-zero identifiers
0034   if (geoId.volume() != 0u) {
0035     encoded["volume"] = geoId.volume();
0036   }
0037   if (geoId.boundary() != 0u) {
0038     encoded["boundary"] = geoId.boundary();
0039   }
0040   if (geoId.layer() != 0u) {
0041     encoded["layer"] = geoId.layer();
0042   }
0043   if (geoId.approach() != 0u) {
0044     encoded["approach"] = geoId.approach();
0045   }
0046   if (geoId.sensitive() != 0u) {
0047     encoded["sensitive"] = geoId.sensitive();
0048   }
0049   if (geoId.extra() != 0u) {
0050     encoded["extra"] = geoId.extra();
0051   }
0052   return encoded;
0053 }
0054 
0055 /// @brief  Decode a geometry identifier from a json object
0056 /// @param encoded is the json object that carries the encoded identifier
0057 /// @return a valid geometry Identifier
0058 inline GeometryIdentifier decodeIdentifier(const nlohmann::json& encoded) {
0059   return GeometryIdentifier()
0060       .withVolume(encoded.value("volume", GeometryIdentifier::Value{0u}))
0061       .withBoundary(encoded.value("boundary", GeometryIdentifier::Value{0u}))
0062       .withLayer(encoded.value("layer", GeometryIdentifier::Value{0u}))
0063       .withApproach(encoded.value("approach", GeometryIdentifier::Value{0u}))
0064       .withSensitive(encoded.value("sensitive", GeometryIdentifier::Value{0u}))
0065       .withExtra(encoded.value("extra", GeometryIdentifier::Value{0u}));
0066 }
0067 
0068 }  // namespace GeometryIdentifierJsonConverter
0069 
0070 /// Write to JSON
0071 /// @param j JSON object to write to
0072 /// @param geoId GeometryIdentifier to write
0073 void to_json(nlohmann::json& j, const GeometryIdentifier& geoId);
0074 
0075 /// Read from JSON
0076 /// @param j JSON object to read from
0077 /// @param geoId  GeometryIdentifier to fill
0078 void from_json(const nlohmann::json& j, GeometryIdentifier& geoId);
0079 
0080 }  // namespace Acts