Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-28 07:03:06

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023, Christopher Dilks
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   // initialize algorithm
0022   //----------------------------------------------------------
0023   algo.level(algorithms::LogLevel::kDebug);
0024   algo.init();
0025 
0026   // helper functions and objects
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   // inputs
0055   //----------------------------------------------------------
0056   /*
0057      |        | collection0 | collection1 | collection2 |
0058      | ------ | ----------- | ----------- | ----------- |
0059      | track0 | horizontal  | horizontal  | horizontal  |
0060      | track1 | horizontal  | empty       | one point   |
0061      | track2 | vertical    | horizontal  | vertical    |
0062      | track3 | horizontal  | horizontal  | horizontal  | // time disordered
0063   */
0064 
0065   // collections
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   // track0
0071   make_track(collection0, {                           // horizontal
0072                            {{0, 0, 0}, {1, 0, 0}, 1}, // { position, momentum, time }
0073                            {{1, 0, 0}, {1, 0, 0}, 2}});
0074   make_track(collection1, {// horizontal
0075                            {{2, 0, 0}, {1, 0, 0}, 3},
0076                            {{3, 0, 0}, {1, 0, 0}, 4}});
0077   make_track(collection2, {// horizontal
0078                            {{4, 0, 0}, {1, 0, 0}, 5},
0079                            {{5, 0, 0}, {1, 0, 0}, 6}});
0080 
0081   // track1
0082   make_track(collection0, {// horizontal
0083                            {{0, 0, 0}, {1, 0, 0}, 1},
0084                            {{1, 0, 0}, {1, 0, 0}, 2}});
0085   make_track(collection1, {// empty
0086                            {}});
0087   make_track(collection2, {// one point
0088                            {{2, 0, 0}, {1, 0, 0}, 3}});
0089 
0090   // track2
0091   make_track(collection0, {// vertical
0092                            {{0, 0, 0}, {0, 1, 0}, 1},
0093                            {{0, 1, 0}, {0, 1, 0}, 2}});
0094   make_track(collection1, {// horizontal
0095                            {{0, 1, 0}, {1, 0, 0}, 3},
0096                            {{1, 1, 0}, {1, 0, 0}, 4}});
0097   make_track(collection2, {// vertical
0098                            {{1, 1, 0}, {0, 1, 0}, 5},
0099                            {{1, 2, 0}, {0, 1, 0}, 6}});
0100 
0101   // track3
0102   make_track(collection0, {// horizontal
0103                            {{1, 0, 0}, {1, 0, 0}, 2},
0104                            {{0, 0, 0}, {1, 0, 0}, 1}});
0105   make_track(collection1, {// horizontal
0106                            {{3, 0, 0}, {1, 0, 0}, 4},
0107                            {{4, 0, 0}, {1, 0, 0}, 5}});
0108   make_track(collection2, {// horizontal
0109                            {{5, 0, 0}, {1, 0, 0}, 6},
0110                            {{2, 0, 0}, {1, 0, 0}, 3}});
0111 
0112   // tests
0113   //----------------------------------------------------------
0114 
0115   SECTION("merge tracks from 1 collection") {
0116     // run algorithm
0117     std::vector<gsl::not_null<const edm4eic::TrackSegmentCollection*>> colls = {collection0.get()};
0118     // create output collection
0119     auto trks = std::make_unique<edm4eic::TrackSegmentCollection>();
0120     algo.process({colls}, {trks.get()});
0121     // input collection(s) size == output collection size
0122     REQUIRE(trks->size() == colls.front()->size());
0123     // track length: from endpoints
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     // track length: from algorithm // FIXME when implemented in `MergeTracks`
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     // run algorithm
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     // input collection(s) size == output collection size
0140     REQUIRE(trks->size() == colls.front()->size());
0141     // track length: from endpoints
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     // track length: from algorithm // FIXME when implemented in `MergeTracks`
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     // run algorithm
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     // input collection(s) size == output collection size
0158     REQUIRE(trks->size() == colls.front()->size());
0159     // track length: from endpoints
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     // track length: from algorithm // FIXME when implemented in `MergeTracks`
0165     for (const auto& trk : *trks)
0166       REQUIRE_THAT(trk.getLength(), Catch::Matchers::WithinAbs(0, EPSILON));
0167   }
0168 }