Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-25 09:22:15

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 #include <boost/test/unit_test.hpp>
0010 #include <boost/test/unit_test_suite.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/TrackParametrization.hpp"
0014 #include "Acts/Definitions/Units.hpp"
0015 #include "Acts/EventData/MultiTrajectory.hpp"
0016 #include "Acts/EventData/ParticleHypothesis.hpp"
0017 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0018 #include "Acts/EventData/VectorTrackContainer.hpp"
0019 #include "Acts/Geometry/GeometryContext.hpp"
0020 #include "Acts/Surfaces/AnnulusBounds.hpp"
0021 #include "Acts/Surfaces/ConeSurface.hpp"
0022 #include "Acts/Surfaces/ConvexPolygonBounds.hpp"
0023 #include "Acts/Surfaces/CylinderBounds.hpp"
0024 #include "Acts/Surfaces/CylinderSurface.hpp"
0025 #include "Acts/Surfaces/DiamondBounds.hpp"
0026 #include "Acts/Surfaces/DiscBounds.hpp"
0027 #include "Acts/Surfaces/DiscSurface.hpp"
0028 #include "Acts/Surfaces/DiscTrapezoidBounds.hpp"
0029 #include "Acts/Surfaces/EllipseBounds.hpp"
0030 #include "Acts/Surfaces/PerigeeSurface.hpp"
0031 #include "Acts/Surfaces/PlanarBounds.hpp"
0032 #include "Acts/Surfaces/PlaneSurface.hpp"
0033 #include "Acts/Surfaces/RadialBounds.hpp"
0034 #include "Acts/Surfaces/RectangleBounds.hpp"
0035 #include "Acts/Surfaces/StrawSurface.hpp"
0036 #include "Acts/Surfaces/SurfaceBounds.hpp"
0037 #include "Acts/Utilities/Helpers.hpp"
0038 #include "Acts/Utilities/Zip.hpp"
0039 #include "ActsPlugins/EDM4hep/PodioTrackContainer.hpp"
0040 #include "ActsPlugins/EDM4hep/PodioTrackStateContainer.hpp"
0041 #include "ActsPlugins/EDM4hep/PodioUtil.hpp"
0042 #include "ActsPodioEdm/Surface.h"
0043 #include <ActsPodioEdm/TrackCollection.h>
0044 
0045 #include <algorithm>
0046 #include <iterator>
0047 #include <memory>
0048 #include <random>
0049 #include <stdexcept>
0050 
0051 using namespace Acts;
0052 using namespace ActsPlugins;
0053 using namespace UnitLiterals;
0054 using namespace HashedStringLiteral;
0055 BOOST_AUTO_TEST_SUITE(PodioTrackContainerTest)
0056 
0057 class NullHelper : public PodioUtil::ConversionHelper {
0058  public:
0059   std::optional<PodioUtil::Identifier> surfaceToIdentifier(
0060       const Surface& /*surface*/) const override {
0061     return {};
0062   }
0063   const Surface* identifierToSurface(
0064       PodioUtil::Identifier /*identifier*/) const override {
0065     return nullptr;
0066   }
0067 
0068   SourceLink identifierToSourceLink(
0069       PodioUtil::Identifier /*identifier*/) const override {
0070     return SourceLink{0};
0071   }
0072 
0073   PodioUtil::Identifier sourceLinkToIdentifier(
0074       const SourceLink& /*sourceLink*/) override {
0075     return 0;
0076   }
0077 };
0078 
0079 struct MapHelper : public NullHelper {
0080   std::optional<PodioUtil::Identifier> surfaceToIdentifier(
0081       const Surface& surface) const override {
0082     for (auto&& [id, srf] : surfaces) {
0083       if (srf == &surface) {
0084         return id;
0085       }
0086     }
0087     return {};
0088   }
0089   const Surface* identifierToSurface(PodioUtil::Identifier id) const override {
0090     auto it = surfaces.find(id);
0091     if (it == surfaces.end()) {
0092       return nullptr;
0093     }
0094 
0095     return it->second;
0096   }
0097 
0098   std::unordered_map<PodioUtil::Identifier, const Surface*> surfaces;
0099 };
0100 
0101 BOOST_AUTO_TEST_CASE(ConvertSurface) {
0102   auto rBounds = std::make_shared<RectangleBounds>(15, 20);
0103 
0104   auto trf = Transform3::Identity();
0105   trf.translation().setRandom();
0106 
0107   auto free = Surface::makeShared<PlaneSurface>(trf, rBounds);
0108 
0109   NullHelper helper;
0110   auto surface = PodioUtil::convertSurfaceToPodio(helper, *free);
0111 
0112   auto free2 = PodioUtil::convertSurfaceFromPodio(helper, surface);
0113 
0114   GeometryContext gctx;
0115 
0116   BOOST_REQUIRE(free2);
0117   BOOST_CHECK_EQUAL(free->type(), free2->type());
0118   BOOST_CHECK_EQUAL(free->bounds().type(), free2->bounds().type());
0119   BOOST_CHECK_EQUAL(free->center(gctx), free2->center(gctx));
0120 
0121   const auto* rBounds2 = dynamic_cast<const RectangleBounds*>(&free2->bounds());
0122   BOOST_REQUIRE_NE(rBounds2, nullptr);
0123 
0124   BOOST_CHECK_EQUAL(rBounds2->halfLengthX(), rBounds->halfLengthX());
0125   BOOST_CHECK_EQUAL(rBounds2->halfLengthY(), rBounds->halfLengthY());
0126 
0127   // this could probably use some more complete checks
0128 }
0129 
0130 BOOST_AUTO_TEST_CASE(ConvertTrack) {
0131   auto rBounds = std::make_shared<RectangleBounds>(15, 20);
0132   auto trf = Transform3::Identity();
0133   trf.translation().setRandom();
0134   auto free = Surface::makeShared<PlaneSurface>(trf, rBounds);
0135 
0136   MapHelper helper;
0137 
0138   auto refCov = BoundMatrix::Random().eval();
0139 
0140   podio::Frame frame;
0141 
0142   ParticleHypothesis pHypo = ParticleHypothesis::pion();
0143 
0144   {
0145     MutablePodioTrackStateContainer tsc{helper};
0146     MutablePodioTrackContainer ptc{helper};
0147     ActsPodioEdm::TrackCollection& tracks = ptc.trackCollection();
0148 
0149     TrackContainer tc{ptc, tsc};
0150 
0151     BOOST_CHECK(!tc.hasColumn("int_column"_hash));
0152     BOOST_CHECK(!tc.hasColumn("float_column"_hash));
0153     tc.addColumn<std::int32_t>("int_column");
0154     tc.addColumn<float>("float_column");
0155     BOOST_CHECK(tc.hasColumn("int_column"_hash));
0156     BOOST_CHECK(tc.hasColumn("float_column"_hash));
0157 
0158     BOOST_CHECK_EQUAL(tc.size(), 0);
0159 
0160     auto t = tc.makeTrack();
0161     BOOST_CHECK_EQUAL(t.tipIndex(), MultiTrajectoryTraits::kInvalid);
0162 
0163     t.setParticleHypothesis(pHypo);
0164     BOOST_CHECK_EQUAL(t.particleHypothesis(), pHypo);
0165 
0166     BOOST_CHECK_EQUAL(tsc.size(), 0);
0167     auto ts1 = t.appendTrackState();
0168     auto ts2 = t.appendTrackState();
0169     auto ts3 = t.appendTrackState();
0170     BOOST_CHECK_EQUAL(tsc.size(), 3);
0171     BOOST_CHECK_EQUAL(ts1.index(), 0);
0172     BOOST_CHECK_EQUAL(ts2.index(), 1);
0173     BOOST_CHECK_EQUAL(ts3.index(), 2);
0174 
0175     BOOST_CHECK_EQUAL(t.nTrackStates(), 3);
0176     BOOST_CHECK_EQUAL(t.tipIndex(), 2);
0177 
0178     BOOST_CHECK_EQUAL(tc.size(), 1);
0179 
0180     auto pTrack = tracks.at(0);
0181     BOOST_CHECK_EQUAL(pTrack.getData().tipIndex, 2);
0182 
0183     t.parameters() << 1, 2, 3, 4, 5, 6;
0184     Eigen::Map<const BoundVector> pars{pTrack.getData().parameters.data()};
0185     BoundVector bv;
0186     bv << 1, 2, 3, 4, 5, 6;
0187     BOOST_CHECK_EQUAL(pars, bv);
0188 
0189     t.covariance() = refCov;
0190 
0191     Eigen::Map<const BoundMatrix> cov{pTrack.getData().covariance.data()};
0192     BOOST_CHECK_EQUAL(refCov, cov);
0193 
0194     t.nMeasurements() = 17;
0195     BOOST_CHECK_EQUAL(pTrack.getData().nMeasurements, 17);
0196 
0197     t.nHoles() = 34;
0198     BOOST_CHECK_EQUAL(pTrack.getData().nHoles, 34);
0199 
0200     t.chi2() = 882.3f;
0201     BOOST_CHECK_EQUAL(pTrack.getData().chi2, 882.3f);
0202 
0203     t.nDoF() = 9;
0204     BOOST_CHECK_EQUAL(pTrack.getData().ndf, 9);
0205 
0206     t.nOutliers() = 77;
0207     BOOST_CHECK_EQUAL(pTrack.getData().nOutliers, 77);
0208 
0209     t.nSharedHits() = 99;
0210     BOOST_CHECK_EQUAL(pTrack.getData().nSharedHits, 99);
0211 
0212     GeometryContext gctx;
0213     t.setReferenceSurface(free);
0214     const auto& free2 = t.referenceSurface();
0215     BOOST_CHECK_EQUAL(free->center(gctx), free2.center(gctx));
0216 
0217     const auto* rBounds2 =
0218         dynamic_cast<const RectangleBounds*>(&free2.bounds());
0219     BOOST_REQUIRE_NE(rBounds2, nullptr);
0220 
0221     BOOST_CHECK_EQUAL(rBounds2->halfLengthX(), rBounds->halfLengthX());
0222     BOOST_CHECK_EQUAL(rBounds2->halfLengthY(), rBounds->halfLengthY());
0223 
0224     BOOST_CHECK_EQUAL(pTrack.getReferenceSurface().identifier,
0225                       PodioUtil::kNoIdentifier);
0226 
0227     auto t2 = tc.makeTrack();
0228     auto t3 = tc.makeTrack();
0229     BOOST_CHECK_EQUAL(tc.size(), 3);
0230 
0231     // Register surface "with the detector"
0232     helper.surfaces[666] = free.get();
0233     t2.setReferenceSurface(free);
0234     auto pTrack2 = tracks.at(1);
0235     BOOST_CHECK_EQUAL(pTrack2.getReferenceSurface().identifier, 666);
0236 
0237     t.component<std::int32_t, "int_column"_hash>() = -11;
0238     t2.component<std::int32_t, "int_column"_hash>() = 42;
0239     t3.component<std::int32_t, "int_column"_hash>() = -98;
0240 
0241     t.component<float, "float_column"_hash>() = -11.2f;
0242     t2.component<float, "float_column"_hash>() = 42.4f;
0243     t3.component<float, "float_column"_hash>() = -98.9f;
0244 
0245     ptc.releaseInto(frame);
0246     tsc.releaseInto(frame);
0247 
0248     BOOST_REQUIRE_NE(frame.get("tracks"), nullptr);
0249     BOOST_CHECK_EQUAL(frame.get("tracks")->size(), 3);
0250     BOOST_REQUIRE_NE(frame.get("tracks_extra__int_column"), nullptr);
0251     BOOST_REQUIRE_NE(frame.get("tracks_extra__float_column"), nullptr);
0252 
0253     BOOST_REQUIRE_NE(frame.get("trackStates"), nullptr);
0254     BOOST_CHECK_EQUAL(frame.get("trackStates")->size(), 3);
0255   }
0256 
0257   {
0258     ConstPodioTrackStateContainer tsc{helper, frame};
0259     ConstPodioTrackContainer ptc{helper, frame};
0260     // const ActsPodioEdm::TrackCollection& tracks = ptc.trackCollection();
0261 
0262     TrackContainer tc{ptc, tsc};
0263 
0264     BOOST_CHECK(tc.hasColumn("int_column"_hash));
0265     BOOST_CHECK(tc.hasColumn("float_column"_hash));
0266 
0267     BOOST_CHECK_EQUAL(tc.size(), 3);
0268 
0269     auto t = tc.getTrack(0);
0270     const auto& freeRecreated = t.referenceSurface();
0271     // Not the exact same surface, it's recreated from values
0272     BOOST_CHECK_NE(free.get(), &freeRecreated);
0273 
0274     BOOST_CHECK_EQUAL(t.particleHypothesis(), pHypo);
0275 
0276     BOOST_CHECK_EQUAL(t.nMeasurements(), 17);
0277 
0278     BOOST_CHECK_EQUAL(t.nHoles(), 34);
0279 
0280     BOOST_CHECK_EQUAL(t.chi2(), 882.3f);
0281 
0282     BOOST_CHECK_EQUAL(t.nDoF(), 9);
0283 
0284     BOOST_CHECK_EQUAL(t.nOutliers(), 77);
0285 
0286     BOOST_CHECK_EQUAL(t.nSharedHits(), 99);
0287 
0288     BOOST_CHECK_EQUAL(t.tipIndex(), 2);
0289     BOOST_CHECK_EQUAL(t.nTrackStates(), 3);
0290 
0291     auto t2 = tc.getTrack(1);
0292     // Is the exact same surface, because it's looked up in the "detector"
0293     BOOST_CHECK_EQUAL(free.get(), &t2.referenceSurface());
0294     BoundVector bv;
0295     bv << 1, 2, 3, 4, 5, 6;
0296     BOOST_CHECK_EQUAL(t.parameters(), bv);
0297 
0298     BOOST_CHECK_EQUAL(t.covariance(), refCov);
0299 
0300     auto t3 = tc.getTrack(2);
0301     BOOST_CHECK(!t3.hasReferenceSurface());
0302 
0303     BOOST_CHECK_EQUAL((t.component<std::int32_t, "int_column"_hash>()), -11);
0304     BOOST_CHECK_EQUAL((t2.component<std::int32_t, "int_column"_hash>()), 42);
0305     BOOST_CHECK_EQUAL((t3.component<std::int32_t, "int_column"_hash>()), -98);
0306 
0307     BOOST_CHECK_EQUAL((t.component<float, "float_column"_hash>()), -11.2f);
0308     BOOST_CHECK_EQUAL((t2.component<float, "float_column"_hash>()), 42.4f);
0309     BOOST_CHECK_EQUAL((t3.component<float, "float_column"_hash>()), -98.9f);
0310   }
0311 }
0312 
0313 BOOST_AUTO_TEST_CASE(CopyTracksIncludingDynamicColumnsDifferentBackends) {
0314   MapHelper helper;
0315 
0316   podio::Frame frame;
0317 
0318   // mutable source
0319   VectorTrackContainer vtc{};
0320   VectorMultiTrajectory mtj{};
0321   TrackContainer tc{vtc, mtj};
0322   tc.addColumn<std::uint64_t>("counter");
0323   tc.addColumn<std::uint8_t>("odd");
0324   mtj.addColumn<std::uint64_t>("ts_counter");
0325   mtj.addColumn<std::uint8_t>("ts_odd");
0326 
0327   MutablePodioTrackStateContainer tsc2{helper};
0328   MutablePodioTrackContainer ptc2{helper};
0329   TrackContainer tc2{ptc2, tsc2};
0330   // doesn't have the dynamic column
0331 
0332   MutablePodioTrackStateContainer tsc3{helper};
0333   MutablePodioTrackContainer ptc3{helper};
0334   TrackContainer tc3{ptc3, tsc3};
0335 
0336   tc3.addColumn<std::uint64_t>("counter");
0337   tc3.addColumn<std::uint8_t>("odd");
0338   tsc3.addColumn<std::uint64_t>("ts_counter");
0339   tsc3.addColumn<std::uint8_t>("ts_odd");
0340 
0341   for (std::size_t i = 0; i < 10; i++) {
0342     auto t = tc.makeTrack();
0343     auto ts = t.appendTrackState();
0344     ts.predicted() = BoundVector::Ones();
0345     ts.component<std::uint64_t, "ts_counter"_hash>() = i;
0346 
0347     ts = t.appendTrackState();
0348     ts.predicted().setOnes();
0349     ts.predicted() *= 2;
0350     ts.component<std::uint64_t, "ts_counter"_hash>() = i + 1;
0351 
0352     ts = t.appendTrackState();
0353     ts.predicted().setOnes();
0354     ts.predicted() *= 3;
0355     ts.component<std::uint64_t, "ts_counter"_hash>() = i + 2;
0356 
0357     t.template component<std::uint64_t>("counter") = i;
0358     t.template component<std::uint8_t>("odd") =
0359         static_cast<std::uint8_t>(i % 2 == 0);
0360 
0361     auto t2 = tc2.makeTrack();
0362     BOOST_CHECK_THROW(t2.copyFrom(t),
0363                       std::invalid_argument);  // this should fail
0364 
0365     auto t3 = tc3.makeTrack();
0366     t3.copyFrom(t);  // this should work
0367 
0368     BOOST_CHECK_NE(t3.tipIndex(), MultiTrajectoryTraits::kInvalid);
0369     BOOST_CHECK_GT(t3.nTrackStates(), 0);
0370     BOOST_REQUIRE_EQUAL(t.nTrackStates(), t3.nTrackStates());
0371 
0372     for (auto [tsa, tsb] :
0373          zip(t.trackStatesReversed(), t3.trackStatesReversed())) {
0374       BOOST_CHECK_EQUAL(tsa.predicted(), tsb.predicted());
0375 
0376       BOOST_CHECK_EQUAL(
0377           (tsa.template component<std::uint64_t, "ts_counter"_hash>()),
0378           (tsb.template component<std::uint64_t, "ts_counter"_hash>()));
0379 
0380       BOOST_CHECK_EQUAL(
0381           (tsa.template component<std::uint8_t, "ts_odd"_hash>()),
0382           (tsb.template component<std::uint8_t, "ts_odd"_hash>()));
0383     }
0384 
0385     BOOST_CHECK_EQUAL(t.template component<std::uint64_t>("counter"),
0386                       t3.template component<std::uint64_t>("counter"));
0387     BOOST_CHECK_EQUAL(t.template component<std::uint8_t>("odd"),
0388                       t3.template component<std::uint8_t>("odd"));
0389   }
0390 }
0391 
0392 BOOST_AUTO_TEST_SUITE_END()