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
0002
0003
0004
0005
0006
0007
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
0048
0049
0050
0051
0052
0053
0054
0055 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0056 struct adl_serializer<parameters_t> {
0057
0058 using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0059
0060
0061
0062
0063
0064 static void to_json(nlohmann::json& j, const parameters_t& t) {
0065
0066
0067 j["direction"] = t.direction();
0068 j["qOverP"] = t.qOverP();
0069 j["particleHypothesis"] = t.particleHypothesis().absolutePdg();
0070
0071
0072 j["covariance"];
0073 if (t.covariance().has_value()) {
0074
0075
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
0087
0088
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
0101
0102
0103
0104 static parameters_t from_json(const nlohmann::json& j) {
0105
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
0116 std::optional<CovarianceMatrix> cov;
0117 if (j.at("covariance").is_null()) {
0118 cov = std::nullopt;
0119 } else {
0120
0121
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
0134 typename parameters_t::ParticleHypothesis particle(absPdg);
0135
0136
0137
0138
0139
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
0159
0160
0161
0162
0163
0164
0165
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
0183
0184
0185
0186
0187
0188
0189
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 }