File indexing completed on 2025-12-11 09:40:23
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 "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
0052
0053
0054
0055
0056
0057
0058
0059 template <Acts::detail::isBoundOrFreeTrackParams parameters_t>
0060 struct adl_serializer<parameters_t> {
0061
0062 using CovarianceMatrix = typename parameters_t::CovarianceMatrix;
0063
0064
0065
0066
0067
0068 static void to_json(nlohmann::json& j, const parameters_t& t) {
0069
0070
0071 j["direction"] = t.direction();
0072 j["qOverP"] = t.qOverP();
0073 j["particleHypothesis"] = t.particleHypothesis().absolutePdg();
0074
0075
0076 j["covariance"];
0077 if (t.covariance().has_value()) {
0078
0079
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
0091
0092
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
0105
0106
0107
0108 static parameters_t from_json(const nlohmann::json& j) {
0109
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
0120 std::optional<CovarianceMatrix> cov;
0121 if (j.at("covariance").is_null()) {
0122 cov = std::nullopt;
0123 } else {
0124
0125
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
0138 typename parameters_t::ParticleHypothesis particle(absPdg);
0139
0140
0141
0142
0143
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
0163
0164
0165
0166
0167
0168
0169
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
0187
0188
0189
0190
0191
0192
0193
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 }
0214 #endif