Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-18 08:30:37

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026, ePIC Collaboration
0003 
0004 #include <algorithms/logger.h>
0005 #include <catch2/catch_test_macros.hpp>
0006 #include <edm4eic/ClusterCollection.h>
0007 #include <edm4hep/Vector3f.h>
0008 #include <memory>
0009 
0010 #include "algorithms/meta/SortSubsetCollection.h"
0011 
0012 TEST_CASE("the SortSubsetCollection algorithm runs", "[SortSubsetCollection]") {
0013   auto make_input = []() {
0014     auto input = std::make_unique<edm4eic::ClusterCollection>();
0015 
0016     auto c0 = input->create();
0017     c0.setEnergy(3.0f);
0018     c0.setPosition({4.0f, 0.0f, 0.0f});
0019 
0020     auto c1 = input->create();
0021     c1.setEnergy(1.0f);
0022     c1.setPosition({1.0f, 0.0f, 0.0f});
0023 
0024     auto c2 = input->create();
0025     c2.setEnergy(2.0f);
0026     c2.setPosition({3.0f, 0.0f, 0.0f});
0027 
0028     auto c3 = input->create();
0029     c3.setEnergy(2.0f);
0030     c3.setPosition({2.0f, 0.0f, 0.0f});
0031 
0032     return input;
0033   };
0034 
0035   SECTION("sorts by accessor function") {
0036     using AlgoT =
0037         eicrecon::SortSubsetCollection<edm4eic::Cluster, decltype(&edm4eic::Cluster::getEnergy)>;
0038     AlgoT algo("sort_by_function", &edm4eic::Cluster::getEnergy);
0039     algo.level(algorithms::LogLevel::kDebug);
0040     algo.init();
0041 
0042     auto input  = make_input();
0043     auto output = std::make_unique<edm4eic::ClusterCollection>();
0044 
0045     algo.process({input.get()}, {output.get()});
0046 
0047     REQUIRE(output->size() == 4);
0048     REQUIRE(output->at(0).getEnergy() == 1.0f);
0049     REQUIRE(output->at(1).getEnergy() == 2.0f);
0050     REQUIRE(output->at(2).getEnergy() == 2.0f);
0051     REQUIRE(output->at(3).getEnergy() == 3.0f);
0052     REQUIRE(output->at(1).getPosition().x == 3.0f);
0053     REQUIRE(output->at(2).getPosition().x == 2.0f);
0054   }
0055 
0056   SECTION("sorts by lambda") {
0057     auto sort_key = [](const edm4eic::Cluster& cluster) { return cluster.getPosition().x; };
0058     using AlgoT   = eicrecon::SortSubsetCollection<edm4eic::Cluster, decltype(sort_key)>;
0059 
0060     AlgoT algo("sort_by_lambda", sort_key);
0061     algo.level(algorithms::LogLevel::kDebug);
0062     algo.init();
0063 
0064     auto input  = make_input();
0065     auto output = std::make_unique<edm4eic::ClusterCollection>();
0066 
0067     algo.process({input.get()}, {output.get()});
0068 
0069     REQUIRE(output->size() == 4);
0070     REQUIRE(output->at(0).getPosition().x == 1.0f);
0071     REQUIRE(output->at(1).getPosition().x == 2.0f);
0072     REQUIRE(output->at(2).getPosition().x == 3.0f);
0073     REQUIRE(output->at(3).getPosition().x == 4.0f);
0074   }
0075 }