Back to home page

EIC code displayed by LXR

 
 

    


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

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/Definitions/PdgParticle.hpp"
0012 #include "Acts/EventData/GenericBoundTrackParameters.hpp"
0013 #include "Acts/EventData/detail/TrackParametersUtils.hpp"
0014 #include "ActsPlugins/Json/SurfaceJsonConverter.hpp"
0015 
0016 #include <nlohmann/json.hpp>
0017 
0018 namespace Acts {
0019 NLOHMANN_JSON_SERIALIZE_ENUM(Acts::PdgParticle,
0020 
0021                              {{Acts::PdgParticle::eInvalid, "Invalid"},
0022                               {Acts::PdgParticle::eElectron, "Electron"},
0023                               {Acts::PdgParticle::eAntiElectron,
0024                                "AntiElectron"},
0025                               {Acts::PdgParticle::ePositron, "Positron"},
0026                               {Acts::PdgParticle::eMuon, "Muon"},
0027                               {Acts::PdgParticle::eAntiMuon, "AntiMuon"},
0028                               {Acts::PdgParticle::eTau, "Tau"},
0029                               {Acts::PdgParticle::eAntiTau, "AntiTau"},
0030                               {Acts::PdgParticle::eGamma, "Gamma"},
0031                               {Acts::PdgParticle::ePionZero, "PionZero"},
0032                               {Acts::PdgParticle::ePionPlus, "PionPlus"},
0033                               {Acts::PdgParticle::ePionMinus, "PionMinus"},
0034                               {Acts::PdgParticle::eKaonPlus, "KaonPlus"},
0035                               {Acts::PdgParticle::eKaonMinus, "KaonMinus"},
0036                               {Acts::PdgParticle::eNeutron, "Neutron"},
0037                               {Acts::PdgParticle::eAntiNeutron, "AntiNeutron"},
0038                               {Acts::PdgParticle::eProton, "Proton"},
0039                               {Acts::PdgParticle::eAntiProton, "AntiProton"},
0040                               {Acts::PdgParticle::eLead, "Lead"}}
0041 
0042 )
0043 }
0044 
0045 #ifdef NLOHMANN_JSON_NAMESPACE_BEGIN
0046 NLOHMANN_JSON_NAMESPACE_BEGIN
0047 #else
0048 namespace nlohmann {
0049 #endif
0050 
0051 /// @brief Serialize a track parameters object to json
0052 ///
0053 /// nlohmann::json serializer specialized for track parameters
0054 /// as they are not default constructible. Is able to serialize
0055 /// either bound or free track parameters given that the constructor
0056 /// convention is followed.
0057 ///
0058 /// @tparam parameters_t The track parameters type
0059 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0060 struct adl_serializer<parameters_t> {
0061   /// Covariance matrix type attached to the parameters
0062   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0063 
0064   /// @brief Serialize track parameters object to json
0065   ///
0066   /// @param j Json object to write to
0067   /// @param t Track parameters object to serialize
0068   static void to_json(nlohmann::json& j, const parameters_t& t) {
0069     // Serialize parameters
0070     // common to all track parameters
0071     j["direction"] = t.direction();
0072     j["qOverP"] = t.qOverP();
0073     j["particleHypothesis"] = t.particleHypothesis().absolutePdg();
0074 
0075     // Covariance is optional
0076     j["covariance"];
0077     if (t.covariance().has_value()) {
0078       // Extract covariance matrix
0079       // parameters and serialize
0080       auto cov = t.covariance().value();
0081       constexpr unsigned int size = cov.rows();
0082       std::array<double, size * size> covData{};
0083       for (std::size_t n = 0; n < size; ++n) {
0084         for (std::size_t m = 0; m < size; ++m) {
0085           covData[n * size + m] = cov(n, m);
0086         }
0087       }
0088       j["covariance"] = covData;
0089     }
0090     // Bound track parameters have
0091     // reference surface attached
0092     // and position takes a geometry context
0093     if constexpr (Acts::detail::isGenericBoundTrackParams<parameters_t>) {
0094       Acts::GeometryContext gctx;
0095       j["position"] = t.fourPosition(gctx);
0096 
0097       j["referenceSurface"] =
0098           Acts::SurfaceJsonConverter::toJson(gctx, t.referenceSurface());
0099     } else {
0100       j["position"] = t.fourPosition();
0101     }
0102   }
0103 
0104   /// @brief Deserialize track parameters object from json
0105   ///
0106   /// @param j Json object to read from
0107   /// @return Track parameters object
0108   static parameters_t from_json(const nlohmann::json& j) {
0109     // Extract common parameters
0110     std::array<double, 4> posData = j.at("position");
0111     Acts::Vector4 position(posData[0], posData[1], posData[2], posData[3]);
0112 
0113     std::array<double, 3> dirData = j.at("direction");
0114     Acts::Vector3 direction(dirData[0], dirData[1], dirData[2]);
0115 
0116     double qOverP = j.at("qOverP");
0117     Acts::PdgParticle absPdg = j.at("particleHypothesis");
0118 
0119     // Covariance is optional
0120     std::optional<CovarianceMatrix> cov;
0121     if (j.at("covariance").is_null()) {
0122       cov = std::nullopt;
0123     } else {
0124       // Extract covariance matrix
0125       // parameters and deserialize
0126       CovarianceMatrix mat;
0127       constexpr unsigned int size = mat.rows();
0128       std::array<double, size * size> covData = j.at("covariance");
0129       for (std::size_t n = 0; n < size; ++n) {
0130         for (std::size_t m = 0; m < size; ++m) {
0131           mat(n, m) = covData[n * size + m];
0132         }
0133       }
0134       cov.emplace(std::move(mat));
0135     }
0136 
0137     // Create particle hypothesis
0138     typename parameters_t::ParticleHypothesis particle(absPdg);
0139 
0140     // Bound track parameters have
0141     // reference surface attached
0142     // and constructor is hidden
0143     // behind a factory method
0144     if constexpr (Acts::detail::isGenericBoundTrackParams<parameters_t>) {
0145       Acts::GeometryContext gctx;
0146       auto referenceSurface =
0147           Acts::SurfaceJsonConverter::fromJson(j.at("referenceSurface"));
0148 
0149       auto res = parameters_t::create(gctx, referenceSurface, position,
0150                                       direction, qOverP, cov, particle);
0151 
0152       if (!res.ok()) {
0153         throw std::invalid_argument("Invalid bound track parameters");
0154       }
0155       return res.value();
0156     } else {
0157       return parameters_t(position, direction, qOverP, cov, particle);
0158     }
0159   }
0160 };
0161 
0162 /// @brief Serialize a shared pointer to track parameters object to json
0163 ///
0164 /// nlohmann::json serializer specialized for shared pointers to track
0165 /// parameters as they are not default constructible. Is able to serialize
0166 /// either bound or free track parameters given that the constructor
0167 /// convention is followed.
0168 ///
0169 /// @tparam parameters_t The track parameters type
0170 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0171 struct adl_serializer<std::shared_ptr<parameters_t>> {
0172   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0173   static void to_json(nlohmann::json& j,
0174                       const std::shared_ptr<parameters_t>& t) {
0175     if (t == nullptr) {
0176       return;
0177     }
0178     j = *t;
0179   }
0180 
0181   static std::shared_ptr<parameters_t> from_json(const nlohmann::json& j) {
0182     return std::make_shared<parameters_t>(j.get<parameters_t>());
0183   }
0184 };
0185 
0186 /// @brief Serialize a unique pointer to track parameters object to json
0187 ///
0188 /// nlohmann::json serializer specialized for unique pointers to track
0189 /// parameters as they are not default constructible. Is able to serialize
0190 /// either bound or free track parameters given that the constructor
0191 /// convention is followed.
0192 ///
0193 /// @tparam parameters_t The track parameters type
0194 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0195 struct adl_serializer<std::unique_ptr<parameters_t>> {
0196   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0197   static void to_json(nlohmann::json& j,
0198                       const std::unique_ptr<parameters_t>& t) {
0199     if (t == nullptr) {
0200       return;
0201     }
0202     j = *t;
0203   }
0204 
0205   static std::unique_ptr<parameters_t> from_json(const nlohmann::json& j) {
0206     return std::make_unique<parameters_t>(j.get<parameters_t>());
0207   }
0208 };
0209 
0210 #ifdef NLOHMANN_JSON_NAMESPACE_END
0211 NLOHMANN_JSON_NAMESPACE_END
0212 #else
0213 }  // namespace nlohmann
0214 #endif