Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Acts/Plugins/Json/TrackParametersJsonConverter.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "Acts/Plugins/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 namespace nlohmann {
0046 
0047 /// @brief Serialize a track parameters object to json
0048 ///
0049 /// nlohmann::json serializer specialized for track parameters
0050 /// as they are not default constructible. Is able to serialize
0051 /// either bound or free track parameters given that the constructor
0052 /// convention is followed.
0053 ///
0054 /// @tparam parameters_t The track parameters type
0055 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0056 struct adl_serializer<parameters_t> {
0057   /// Covariance matrix type attached to the parameters
0058   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0059 
0060   /// @brief Serialize track parameters object to json
0061   ///
0062   /// @param j Json object to write to
0063   /// @param t Track parameters object to serialize
0064   static void to_json(nlohmann::json& j, const parameters_t& t) {
0065     // Serialize parameters
0066     // common to all track parameters
0067     j["direction"] = t.direction();
0068     j["qOverP"] = t.qOverP();
0069     j["particleHypothesis"] = t.particleHypothesis().absolutePdg();
0070 
0071     // Covariance is optional
0072     j["covariance"];
0073     if (t.covariance().has_value()) {
0074       // Extract covariance matrix
0075       // parameters and serialize
0076       auto cov = t.covariance().value();
0077       constexpr unsigned int size = cov.rows();
0078       std::array<double, size * size> covData{};
0079       for (std::size_t n = 0; n < size; ++n) {
0080         for (std::size_t m = 0; m < size; ++m) {
0081           covData[n * size + m] = cov(n, m);
0082         }
0083       }
0084       j["covariance"] = covData;
0085     }
0086     // Bound track parameters have
0087     // reference surface attached
0088     // and position takes a geometry context
0089     if constexpr (Acts::detail::isGenericBoundTrackParams<parameters_t>) {
0090       Acts::GeometryContext gctx;
0091       j["position"] = t.fourPosition(gctx);
0092 
0093       j["referenceSurface"] =
0094           Acts::SurfaceJsonConverter::toJson(gctx, t.referenceSurface());
0095     } else {
0096       j["position"] = t.fourPosition();
0097     }
0098   }
0099 
0100   /// @brief Deserialize track parameters object from json
0101   ///
0102   /// @param j Json object to read from
0103   /// @return Track parameters object
0104   static parameters_t from_json(const nlohmann::json& j) {
0105     // Extract common parameters
0106     std::array<double, 4> posData = j.at("position");
0107     Acts::Vector4 position(posData[0], posData[1], posData[2], posData[3]);
0108 
0109     std::array<double, 3> dirData = j.at("direction");
0110     Acts::Vector3 direction(dirData[0], dirData[1], dirData[2]);
0111 
0112     double qOverP = j.at("qOverP");
0113     Acts::PdgParticle absPdg = j.at("particleHypothesis");
0114 
0115     // Covariance is optional
0116     std::optional<CovarianceMatrix> cov;
0117     if (j.at("covariance").is_null()) {
0118       cov = std::nullopt;
0119     } else {
0120       // Extract covariance matrix
0121       // parameters and deserialize
0122       CovarianceMatrix mat;
0123       constexpr unsigned int size = mat.rows();
0124       std::array<double, size * size> covData = j.at("covariance");
0125       for (std::size_t n = 0; n < size; ++n) {
0126         for (std::size_t m = 0; m < size; ++m) {
0127           mat(n, m) = covData[n * size + m];
0128         }
0129       }
0130       cov.emplace(std::move(mat));
0131     }
0132 
0133     // Create particle hypothesis
0134     typename parameters_t::ParticleHypothesis particle(absPdg);
0135 
0136     // Bound track parameters have
0137     // reference surface attached
0138     // and constructor is hidden
0139     // behind a factory method
0140     if constexpr (Acts::detail::isGenericBoundTrackParams<parameters_t>) {
0141       Acts::GeometryContext gctx;
0142       auto referenceSurface =
0143           Acts::SurfaceJsonConverter::fromJson(j.at("referenceSurface"));
0144 
0145       auto res = parameters_t::create(referenceSurface, gctx, position,
0146                                       direction, qOverP, cov, particle);
0147 
0148       if (!res.ok()) {
0149         throw std::invalid_argument("Invalid bound track parameters");
0150       }
0151       return res.value();
0152     } else {
0153       return parameters_t(position, direction, qOverP, cov, particle);
0154     }
0155   }
0156 };
0157 
0158 /// @brief Serialize a shared pointer to track parameters object to json
0159 ///
0160 /// nlohmann::json serializer specialized for shared pointers to track
0161 /// parameters as they are not default constructible. Is able to serialize
0162 /// either bound or free track parameters given that the constructor
0163 /// convention is followed.
0164 ///
0165 /// @tparam parameters_t The track parameters type
0166 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0167 struct adl_serializer<std::shared_ptr<parameters_t>> {
0168   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0169   static void to_json(nlohmann::json& j,
0170                       const std::shared_ptr<parameters_t>& t) {
0171     if (t == nullptr) {
0172       return;
0173     }
0174     j = *t;
0175   }
0176 
0177   static std::shared_ptr<parameters_t> from_json(const nlohmann::json& j) {
0178     return std::make_shared<parameters_t>(j.get<parameters_t>());
0179   }
0180 };
0181 
0182 /// @brief Serialize a unique pointer to track parameters object to json
0183 ///
0184 /// nlohmann::json serializer specialized for unique pointers to track
0185 /// parameters as they are not default constructible. Is able to serialize
0186 /// either bound or free track parameters given that the constructor
0187 /// convention is followed.
0188 ///
0189 /// @tparam parameters_t The track parameters type
0190 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0191 struct adl_serializer<std::unique_ptr<parameters_t>> {
0192   using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0193   static void to_json(nlohmann::json& j,
0194                       const std::unique_ptr<parameters_t>& t) {
0195     if (t == nullptr) {
0196       return;
0197     }
0198     j = *t;
0199   }
0200 
0201   static std::unique_ptr<parameters_t> from_json(const nlohmann::json& j) {
0202     return std::make_unique<parameters_t>(j.get<parameters_t>());
0203   }
0204 };
0205 
0206 }  // namespace nlohmann