File indexing completed on 2024-09-27 07:03:08
0001
0002
0003
0004 #include <algorithms/logger.h>
0005 #include <catch2/catch_test_macros.hpp>
0006 #include <catch2/matchers/catch_matchers.hpp>
0007 #include <catch2/matchers/catch_matchers_floating_point.hpp>
0008 #include <cmath>
0009 #include <edm4eic/TrackPoint.h>
0010 #include <edm4eic/TrackSegmentCollection.h>
0011 #include <edm4hep/Vector3f.h>
0012 #include <gsl/pointers>
0013 #include <memory>
0014 #include <vector>
0015
0016 #include "algorithms/pid/MergeTracks.h"
0017
0018 TEST_CASE("the PID MergeTracks algorithm runs", "[MergeTracks]") {
0019 eicrecon::MergeTracks algo("test");
0020
0021
0022
0023 algo.level(algorithms::LogLevel::kDebug);
0024 algo.init();
0025
0026
0027
0028 const float EPSILON = 1e-5;
0029
0030 struct Point {
0031 decltype(edm4eic::TrackPoint::position) position;
0032 decltype(edm4eic::TrackPoint::momentum) momentum;
0033 decltype(edm4eic::TrackPoint::time) time;
0034 };
0035
0036 auto make_track = [](auto& coll, std::vector<Point> points) {
0037 auto trk = coll->create();
0038 for (auto& point : points) {
0039 edm4eic::TrackPoint trk_point;
0040 trk_point.position = point.position;
0041 trk_point.momentum = point.momentum;
0042 trk_point.time = point.time;
0043 trk.addToPoints(trk_point);
0044 }
0045 };
0046
0047 auto track_length = [](auto trk) {
0048 auto a = trk.getPoints(0);
0049 auto b = trk.getPoints(trk.points_size() - 1);
0050 return std::hypot(a.position.x - b.position.x, a.position.y - b.position.y,
0051 a.position.z - b.position.z);
0052 };
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 auto collection0 = std::make_unique<edm4eic::TrackSegmentCollection>();
0067 auto collection1 = std::make_unique<edm4eic::TrackSegmentCollection>();
0068 auto collection2 = std::make_unique<edm4eic::TrackSegmentCollection>();
0069
0070
0071 make_track(collection0, {
0072 {{0, 0, 0}, {1, 0, 0}, 1},
0073 {{1, 0, 0}, {1, 0, 0}, 2}});
0074 make_track(collection1, {
0075 {{2, 0, 0}, {1, 0, 0}, 3},
0076 {{3, 0, 0}, {1, 0, 0}, 4}});
0077 make_track(collection2, {
0078 {{4, 0, 0}, {1, 0, 0}, 5},
0079 {{5, 0, 0}, {1, 0, 0}, 6}});
0080
0081
0082 make_track(collection0, {
0083 {{0, 0, 0}, {1, 0, 0}, 1},
0084 {{1, 0, 0}, {1, 0, 0}, 2}});
0085 make_track(collection1, {
0086 {}});
0087 make_track(collection2, {
0088 {{2, 0, 0}, {1, 0, 0}, 3}});
0089
0090
0091 make_track(collection0, {
0092 {{0, 0, 0}, {0, 1, 0}, 1},
0093 {{0, 1, 0}, {0, 1, 0}, 2}});
0094 make_track(collection1, {
0095 {{0, 1, 0}, {1, 0, 0}, 3},
0096 {{1, 1, 0}, {1, 0, 0}, 4}});
0097 make_track(collection2, {
0098 {{1, 1, 0}, {0, 1, 0}, 5},
0099 {{1, 2, 0}, {0, 1, 0}, 6}});
0100
0101
0102 make_track(collection0, {
0103 {{1, 0, 0}, {1, 0, 0}, 2},
0104 {{0, 0, 0}, {1, 0, 0}, 1}});
0105 make_track(collection1, {
0106 {{3, 0, 0}, {1, 0, 0}, 4},
0107 {{4, 0, 0}, {1, 0, 0}, 5}});
0108 make_track(collection2, {
0109 {{5, 0, 0}, {1, 0, 0}, 6},
0110 {{2, 0, 0}, {1, 0, 0}, 3}});
0111
0112
0113
0114
0115 SECTION("merge tracks from 1 collection") {
0116
0117 std::vector<gsl::not_null<const edm4eic::TrackSegmentCollection*>> colls = {collection0.get()};
0118
0119 auto trks = std::make_unique<edm4eic::TrackSegmentCollection>();
0120 algo.process({colls}, {trks.get()});
0121
0122 REQUIRE(trks->size() == colls.front()->size());
0123
0124 REQUIRE_THAT(track_length(trks->at(0)), Catch::Matchers::WithinAbs(1, EPSILON));
0125 REQUIRE_THAT(track_length(trks->at(1)), Catch::Matchers::WithinAbs(1, EPSILON));
0126 REQUIRE_THAT(track_length(trks->at(2)), Catch::Matchers::WithinAbs(1, EPSILON));
0127 REQUIRE_THAT(track_length(trks->at(3)), Catch::Matchers::WithinAbs(1, EPSILON));
0128
0129 for (const auto& trk : *trks)
0130 REQUIRE_THAT(trk.getLength(), Catch::Matchers::WithinAbs(0, EPSILON));
0131 }
0132
0133 SECTION("merge tracks from 2 collections") {
0134
0135 std::vector<gsl::not_null<const edm4eic::TrackSegmentCollection*>> colls = {collection0.get(),
0136 collection1.get()};
0137 auto trks = std::make_unique<edm4eic::TrackSegmentCollection>();
0138 algo.process({colls}, {trks.get()});
0139
0140 REQUIRE(trks->size() == colls.front()->size());
0141
0142 REQUIRE_THAT(track_length(trks->at(0)), Catch::Matchers::WithinAbs(3, EPSILON));
0143 REQUIRE_THAT(track_length(trks->at(1)), Catch::Matchers::WithinAbs(1, EPSILON));
0144 REQUIRE_THAT(track_length(trks->at(2)), Catch::Matchers::WithinAbs(std::hypot(1, 1), EPSILON));
0145 REQUIRE_THAT(track_length(trks->at(3)), Catch::Matchers::WithinAbs(4, EPSILON));
0146
0147 for (const auto& trk : *trks)
0148 REQUIRE_THAT(trk.getLength(), Catch::Matchers::WithinAbs(0, EPSILON));
0149 }
0150
0151 SECTION("merge tracks from 3 collections") {
0152
0153 std::vector<gsl::not_null<const edm4eic::TrackSegmentCollection*>> colls = {
0154 collection0.get(), collection1.get(), collection2.get()};
0155 auto trks = std::make_unique<edm4eic::TrackSegmentCollection>();
0156 algo.process({colls}, {trks.get()});
0157
0158 REQUIRE(trks->size() == colls.front()->size());
0159
0160 REQUIRE_THAT(track_length(trks->at(0)), Catch::Matchers::WithinAbs(5, EPSILON));
0161 REQUIRE_THAT(track_length(trks->at(1)), Catch::Matchers::WithinAbs(2, EPSILON));
0162 REQUIRE_THAT(track_length(trks->at(2)), Catch::Matchers::WithinAbs(std::hypot(1, 2), EPSILON));
0163 REQUIRE_THAT(track_length(trks->at(3)), Catch::Matchers::WithinAbs(5, EPSILON));
0164
0165 for (const auto& trk : *trks)
0166 REQUIRE_THAT(trk.getLength(), Catch::Matchers::WithinAbs(0, EPSILON));
0167 }
0168 }