Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-17 07:50:57

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Derek Anderson
0003 
0004 #include <algorithms/logger.h>
0005 #include <catch2/catch_test_macros.hpp>
0006 #include <edm4eic/CalorimeterHitCollection.h>
0007 #include <edm4eic/ClusterCollection.h>
0008 #include <edm4eic/EDM4eicVersion.h>
0009 #include <edm4eic/MCRecoCalorimeterHitAssociationCollection.h>
0010 #include <edm4eic/MCRecoClusterParticleAssociationCollection.h>
0011 #include <podio/detail/Link.h>
0012 #include <podio/detail/LinkCollectionImpl.h>
0013 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0014 #include <edm4eic/MCRecoCalorimeterHitLinkCollection.h>
0015 #include <edm4eic/MCRecoClusterParticleLinkCollection.h>
0016 #endif
0017 #include <edm4eic/ProtoClusterCollection.h>
0018 #include <edm4eic/TrackClusterMatchCollection.h>
0019 #include <edm4eic/TrackCollection.h>
0020 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0021 #include <edm4eic/TrackProtoClusterLinkCollection.h>
0022 #elif EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 4, 0)
0023 #include <edm4eic/TrackProtoClusterMatchCollection.h>
0024 #endif
0025 #include <edm4hep/Vector3f.h>
0026 #include <cstddef>
0027 #include <deque>
0028 #include <memory>
0029 #include <string>
0030 #include <tuple>
0031 
0032 #include "algorithms/calorimetry/CalorimeterClusterRecoCoG.h"
0033 #include "algorithms/calorimetry/CalorimeterClusterRecoCoGConfig.h"
0034 #include "algorithms/particle_flow/TrackProtoClusterMatchPromoter.h"
0035 
0036 TEST_CASE("the TrackProtoClusterMatchPromoter algorithm runs", "[TrackProtoClusterMatchPromoter]") {
0037 
0038   eicrecon::TrackProtoClusterMatchPromoter algo_promote("trackProtoClusterMatchPromoter");
0039   algo_promote.level(algorithms::LogLevel::kDebug);
0040   algo_promote.init();
0041 
0042   SECTION("empty input produces empty output") {
0043 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0044     auto empty_proto_link_coll  = std::make_unique<edm4eic::TrackProtoClusterLinkCollection>();
0045     auto empty_proto_coll       = std::make_unique<edm4eic::ProtoClusterCollection>();
0046     auto empty_clust_coll       = std::make_unique<edm4eic::ClusterCollection>();
0047     auto empty_clust_match_coll = std::make_unique<edm4eic::TrackClusterMatchCollection>();
0048     algo_promote.process(
0049         {empty_proto_link_coll.get(), empty_proto_coll.get(), empty_clust_coll.get()},
0050         {empty_clust_match_coll.get()});
0051     REQUIRE(empty_clust_match_coll->size() == 0);
0052 #elif EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 4, 0)
0053     auto empty_proto_match_coll = std::make_unique<edm4eic::TrackProtoClusterMatchCollection>();
0054     auto empty_proto_coll       = std::make_unique<edm4eic::ProtoClusterCollection>();
0055     auto empty_clust_coll       = std::make_unique<edm4eic::ClusterCollection>();
0056     auto empty_clust_match_coll = std::make_unique<edm4eic::TrackClusterMatchCollection>();
0057     algo_promote.process(
0058         {empty_proto_match_coll.get(), empty_proto_coll.get(), empty_clust_coll.get()},
0059         {empty_clust_match_coll.get()});
0060     REQUIRE(empty_clust_match_coll->size() == 0);
0061 #else
0062     auto empty_proto_coll       = std::make_unique<edm4eic::ProtoClusterCollection>();
0063     auto empty_clust_coll       = std::make_unique<edm4eic::ClusterCollection>();
0064     auto empty_clust_match_coll = std::make_unique<edm4eic::TrackClusterMatchCollection>();
0065     algo_promote.process({empty_proto_coll.get(), empty_clust_coll.get()},
0066                          {empty_clust_match_coll.get()});
0067     REQUIRE(empty_clust_match_coll->size() == 0);
0068 #endif
0069   }
0070 
0071   auto hit_coll = std::make_unique<edm4eic::CalorimeterHitCollection>();
0072   auto hit1     = hit_coll->create(0, 1.0);
0073   auto hit2     = hit_coll->create(1, 1.5);
0074   auto hit3     = hit_coll->create(2, 2.0);
0075   auto hit4     = hit_coll->create(3, 2.0);
0076   auto hit5     = hit_coll->create(4, 2.0);
0077   auto hit6     = hit_coll->create(5, 3.0);
0078 
0079   // group hits into proto/clusters
0080   //   - proto/clust 1 = {hit 1, hit 2, hit 3}
0081   //   - proto/clust 2 = {hit 4, hit 5}
0082   //   - proto/clust 3 = {hit 6}
0083   auto proto_coll = std::make_unique<edm4eic::ProtoClusterCollection>();
0084   auto proto1     = proto_coll->create();
0085   auto proto2     = proto_coll->create();
0086   auto proto3     = proto_coll->create();
0087   proto1.addToHits(hit1);
0088   proto1.addToWeights(1.0);
0089   proto1.addToHits(hit2);
0090   proto1.addToWeights(1.0);
0091   proto1.addToHits(hit3);
0092   proto1.addToWeights(1.0);
0093   proto2.addToHits(hit4);
0094   proto2.addToWeights(1.0);
0095   proto2.addToHits(hit5);
0096   proto2.addToWeights(1.0);
0097   proto3.addToHits(hit6);
0098   proto3.addToWeights(1.0);
0099 
0100   auto clust_coll = std::make_unique<edm4eic::ClusterCollection>();
0101   auto clust1     = clust_coll->create();
0102   auto clust2     = clust_coll->create();
0103   auto clust3     = clust_coll->create();
0104   clust1.addToHits(hit1);
0105   clust1.addToHits(hit2);
0106   clust1.addToHits(hit3);
0107   clust2.addToHits(hit4);
0108   clust2.addToHits(hit5);
0109   clust3.addToHits(hit6);
0110 
0111   auto track_coll = std::make_unique<edm4eic::TrackCollection>();
0112   auto track1 =
0113       track_coll->create(0, edm4hep::Vector3f(0.0, 0.0, 0.0), edm4hep::Vector3f(-1.0, -1.0, -2.5));
0114   auto track2 =
0115       track_coll->create(0, edm4hep::Vector3f(0.0, 0.0, 0.0), edm4hep::Vector3f(-0.5, -0.5, -2.0));
0116   auto track3 =
0117       track_coll->create(0, edm4hep::Vector3f(0.0, 0.0, 0.0), edm4hep::Vector3f(0.0, -0.5, -2.0));
0118   auto track4 =
0119       track_coll->create(0, edm4hep::Vector3f(0.0, 0.0, 0.0), edm4hep::Vector3f(0.0, 0.0, -3.0));
0120 
0121   // link tracks and proto/clusters
0122   //   - proto/clust 1 <--- {track 1, track 2}
0123   //   - proto/clust 2 <--- {track 3}
0124   //   - proto/clust 3 <--- {track 4}
0125 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0126   auto proto_link_coll = std::make_unique<edm4eic::TrackProtoClusterLinkCollection>();
0127   auto protolink1      = proto_link_coll->create();
0128   auto protolink2      = proto_link_coll->create();
0129   auto protolink3      = proto_link_coll->create();
0130   auto protolink4      = proto_link_coll->create();
0131   protolink1.setFrom(track1);
0132   protolink1.setTo(proto1);
0133   protolink2.setFrom(track2);
0134   protolink2.setTo(proto1);
0135   protolink3.setFrom(track3);
0136   protolink3.setTo(proto2);
0137   protolink4.setFrom(track4);
0138   protolink4.setTo(proto3);
0139 #elif EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 4, 0)
0140   auto proto_match_coll = std::make_unique<edm4eic::TrackProtoClusterMatchCollection>();
0141   auto protomatch1      = proto_match_coll->create();
0142   auto protomatch2      = proto_match_coll->create();
0143   auto protomatch3      = proto_match_coll->create();
0144   auto protomatch4      = proto_match_coll->create();
0145   protomatch1.setFrom(track1);
0146   protomatch1.setTo(proto1);
0147   protomatch2.setFrom(track2);
0148   protomatch2.setTo(proto1);
0149   protomatch3.setFrom(track3);
0150   protomatch3.setTo(proto2);
0151   protomatch4.setFrom(track4);
0152   protomatch4.setTo(proto3);
0153 #endif
0154 
0155   auto clust_match_coll = std::make_unique<edm4eic::TrackClusterMatchCollection>();
0156   auto clustmatch1      = clust_match_coll->create();
0157   auto clustmatch2      = clust_match_coll->create();
0158   auto clustmatch3      = clust_match_coll->create();
0159   auto clustmatch4      = clust_match_coll->create();
0160   clustmatch1.setTrack(track1);
0161   clustmatch1.setCluster(clust1);
0162   clustmatch2.setTrack(track2);
0163   clustmatch2.setCluster(clust1);
0164   clustmatch3.setTrack(track3);
0165   clustmatch3.setCluster(clust2);
0166   clustmatch4.setTrack(track4);
0167   clustmatch4.setCluster(clust3);
0168 
0169   // configure reco algorithm to match EEEMCAL
0170   eicrecon::CalorimeterClusterRecoCoGConfig cfg_reco;
0171   cfg_reco.energyWeight    = "log";
0172   cfg_reco.sampFrac        = 1.0;
0173   cfg_reco.logWeightBase   = 3.6;
0174   cfg_reco.enableEtaBounds = false;
0175 
0176   eicrecon::CalorimeterClusterRecoCoG algo_reco("calorimeterClusterRecoCoG");
0177   algo_reco.level(algorithms::LogLevel::kDebug);
0178   algo_reco.applyConfig(cfg_reco);
0179   algo_reco.init();
0180 
0181   auto reco_coll      = std::make_unique<edm4eic::ClusterCollection>();
0182   auto hit_assoc_coll = std::make_unique<edm4eic::MCRecoCalorimeterHitAssociationCollection>();
0183   auto par_assoc_coll = std::make_unique<edm4eic::MCRecoClusterParticleAssociationCollection>();
0184 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0185   auto hit_link_coll = std::make_unique<edm4eic::MCRecoCalorimeterHitLinkCollection>();
0186   auto par_link_coll = std::make_unique<edm4eic::MCRecoClusterParticleLinkCollection>();
0187   auto input_reco    = std::make_tuple(proto_coll.get(), hit_link_coll.get(), hit_assoc_coll.get());
0188   auto output_reco   = std::make_tuple(reco_coll.get(), par_link_coll.get(), par_assoc_coll.get());
0189 #else
0190   auto input_reco  = std::make_tuple(proto_coll.get(), hit_assoc_coll.get());
0191   auto output_reco = std::make_tuple(reco_coll.get(), par_assoc_coll.get());
0192 #endif
0193   algo_reco.process(input_reco, output_reco);
0194 
0195   // output for next two tests
0196   auto reco_match_coll = std::make_unique<edm4eic::TrackClusterMatchCollection>();
0197 
0198   SECTION("algorithm produces correct number of outputs") {
0199 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 7, 0)
0200     algo_promote.process({proto_link_coll.get(), proto_coll.get(), clust_coll.get()},
0201                          {reco_match_coll.get()});
0202     REQUIRE(reco_match_coll->size() == clust_match_coll->size());
0203 #elif EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 4, 0)
0204     algo_promote.process({proto_match_coll.get(), proto_coll.get(), clust_coll.get()},
0205                          {reco_match_coll.get()});
0206     REQUIRE(reco_match_coll->size() == clust_match_coll->size());
0207 #else
0208     algo_promote.process({proto_coll.get(), clust_coll.get()}, {reco_match_coll.get()});
0209     REQUIRE(reco_match_coll->size() == 0);
0210 #endif
0211   }
0212 
0213 #if EDM4EIC_BUILD_VERSION >= EDM4EIC_VERSION(8, 4, 0)
0214   SECTION("algorithm correctly matches clusters to tracks") {
0215     for (std::size_t idx = 0; idx < reco_match_coll->size(); ++idx) {
0216       REQUIRE((*reco_match_coll)[idx].getCluster() == (*clust_match_coll)[idx].getCluster());
0217       REQUIRE((*reco_match_coll)[idx].getTrack() == (*clust_match_coll)[idx].getTrack());
0218     }
0219   }
0220 #endif
0221 }