File indexing completed on 2026-11-23 09:28:57
0001
0002
0003
0004 #include <catch2/catch_test_macros.hpp>
0005 #include <catch2/matchers/catch_matchers.hpp>
0006 #include <catch2/matchers/catch_matchers_floating_point.hpp>
0007 #include <edm4eic/CalorimeterHitCollection.h>
0008 #include <edm4eic/ClusterCollection.h>
0009 #include <edm4eic/MCRecoCalorimeterHitAssociationCollection.h>
0010 #include <edm4eic/MCRecoCalorimeterHitLinkCollection.h>
0011 #include <edm4eic/MCRecoClusterParticleAssociationCollection.h>
0012 #include <edm4eic/MCRecoClusterParticleLinkCollection.h>
0013 #include <edm4eic/ProtoClusterCollection.h>
0014 #include <edm4eic/unit_system.h>
0015 #include <edm4hep/CaloHitContributionCollection.h>
0016 #include <edm4hep/MCParticleCollection.h>
0017 #include <edm4hep/RawCalorimeterHitCollection.h>
0018 #include <edm4hep/SimCalorimeterHitCollection.h>
0019 #include <edm4hep/Vector3d.h>
0020 #include <edm4hep/Vector3f.h>
0021 #include <podio/detail/Link.h>
0022 #include <podio/detail/LinkCollectionImpl.h>
0023 #include <spdlog/common.h>
0024 #include <spdlog/logger.h>
0025 #include <spdlog/spdlog.h>
0026 #include <cmath>
0027 #include <deque>
0028 #include <memory>
0029 #include <string>
0030 #include <tuple>
0031 #include <utility>
0032 #include <vector>
0033
0034 #include "algorithms/calorimetry/CalorimeterClusterRecoCoG.h"
0035 #include "algorithms/calorimetry/CalorimeterClusterRecoCoGConfig.h"
0036
0037 using eicrecon::CalorimeterClusterRecoCoG;
0038 using eicrecon::CalorimeterClusterRecoCoGConfig;
0039
0040 TEST_CASE("the calorimeter CoG algorithm runs", "[CalorimeterClusterRecoCoG]") {
0041 const float EPSILON = 1e-5;
0042
0043 CalorimeterClusterRecoCoG algo("CalorimeterClusterRecoCoG");
0044
0045 std::shared_ptr<spdlog::logger> logger =
0046 spdlog::default_logger()->clone("CalorimeterClusterRecoCoG");
0047 logger->set_level(spdlog::level::trace);
0048
0049 CalorimeterClusterRecoCoGConfig cfg;
0050 cfg.energyWeight = "log";
0051 cfg.sampFrac = 0.0203;
0052 cfg.logWeightBaseCoeffs = {5.0, 0.65, 0.31};
0053 cfg.logWeightBase_Eref = 50 * edm4eic::unit::GeV;
0054
0055 algo.applyConfig(cfg);
0056 algo.init();
0057
0058 edm4hep::RawCalorimeterHitCollection rawhits_coll;
0059 edm4eic::CalorimeterHitCollection hits_coll;
0060 edm4eic::ProtoClusterCollection pclust_coll;
0061 edm4hep::SimCalorimeterHitCollection simhits_coll;
0062 edm4eic::MCRecoCalorimeterHitAssociationCollection hitassocs_coll;
0063 edm4eic::MCRecoCalorimeterHitLinkCollection hitlinks_coll;
0064 edm4hep::CaloHitContributionCollection contribs_coll;
0065 edm4hep::MCParticleCollection mcparts_coll;
0066 auto assoc_coll = std::make_unique<edm4eic::MCRecoClusterParticleAssociationCollection>();
0067 auto link_coll = std::make_unique<edm4eic::MCRecoClusterParticleLinkCollection>();
0068 auto clust_coll = std::make_unique<edm4eic::ClusterCollection>();
0069
0070
0071 auto pclust = pclust_coll.create();
0072 edm4hep::Vector3f position({0, 0, 1 * edm4eic::unit::mm});
0073
0074 auto rawhit1 = rawhits_coll.create();
0075
0076 auto hit1 = hits_coll.create();
0077 hit1.setCellID(0);
0078 hit1.setEnergy(0.1 * edm4eic::unit::GeV);
0079 hit1.setEnergyError(0);
0080 hit1.setTime(0);
0081 hit1.setTimeError(0);
0082 hit1.setPosition(position);
0083 hit1.setDimension({0, 0, 0});
0084 hit1.setLocal(position);
0085 hit1.setRawHit(rawhit1);
0086 pclust.addToHits(hit1);
0087 pclust.addToWeights(1);
0088
0089 auto mcpart11 = mcparts_coll.create(11,
0090 1,
0091 0,
0092 -1.,
0093 0.,
0094 0.,
0095 edm4hep::Vector3d(),
0096 edm4hep::Vector3d(),
0097 edm4hep::Vector3d(),
0098 edm4hep::Vector3d(),
0099 9
0100 );
0101
0102 auto mcpart12 = mcparts_coll.create(
0103 22,
0104 0,
0105 (0x1 << edm4hep::MCParticle::BITCreatedInSimulation),
0106 0.,
0107 0.,
0108 0.,
0109 edm4hep::Vector3d(),
0110 edm4hep::Vector3d(),
0111 edm4hep::Vector3d(),
0112 edm4hep::Vector3d(),
0113 9
0114 );
0115
0116 mcpart12.addToParents(mcpart11);
0117 mcpart11.addToDaughters(mcpart12);
0118
0119 auto contrib11 = contribs_coll.create(0,
0120 0.05 * edm4eic::unit::GeV,
0121 0.0,
0122 edm4hep::Vector3f()
0123 );
0124 contrib11.setParticle(mcpart11);
0125 auto contrib12 = contribs_coll.create(0,
0126 0.05 * edm4eic::unit::GeV,
0127 0.0,
0128 edm4hep::Vector3f()
0129 );
0130 contrib12.setParticle(mcpart12);
0131
0132 auto simhit1 = simhits_coll.create();
0133 simhit1.setCellID(hit1.getCellID());
0134 simhit1.setEnergy(0.1 * edm4eic::unit::GeV);
0135 simhit1.setPosition(hit1.getPosition());
0136 simhit1.addToContributions(contrib11);
0137 simhit1.addToContributions(contrib12);
0138
0139 auto hitassoc1 = hitassocs_coll.create();
0140 hitassoc1.setRawHit(rawhit1);
0141 hitassoc1.setSimHit(simhit1);
0142 auto hitlink1 = hitlinks_coll.create();
0143 hitlink1.setFrom(rawhit1);
0144 hitlink1.setTo(simhit1);
0145
0146 auto rawhit2 = rawhits_coll.create();
0147
0148 position = {-1 * edm4eic::unit::mm, 0, 2 * edm4eic::unit::mm};
0149 auto hit2 = hits_coll.create();
0150 hit2.setCellID(1);
0151 hit2.setEnergy(0.1 * edm4eic::unit::GeV);
0152 hit2.setEnergyError(0);
0153 hit2.setTime(0);
0154 hit2.setTimeError(0);
0155 hit2.setPosition(position);
0156 hit2.setDimension({0, 0, 0});
0157 hit2.setLocal(position);
0158 hit2.setRawHit(rawhit2);
0159 pclust.addToHits(hit2);
0160 pclust.addToWeights(1);
0161
0162 auto mcpart2 = mcparts_coll.create(
0163 211,
0164 0,
0165 (0x1 << edm4hep::MCParticle::BITCreatedInSimulation),
0166 0.,
0167 0.,
0168 0.,
0169 edm4hep::Vector3d(),
0170 edm4hep::Vector3d(),
0171 edm4hep::Vector3d(),
0172 edm4hep::Vector3d(),
0173 9
0174 );
0175
0176 auto contrib2 = contribs_coll.create(0,
0177 0.1 * edm4eic::unit::GeV,
0178 0.0,
0179 edm4hep::Vector3f()
0180 );
0181 contrib2.setParticle(mcpart2);
0182
0183 auto simhit2 = simhits_coll.create();
0184 simhit2.setCellID(hit2.getCellID());
0185 simhit2.setEnergy(0.1 * edm4eic::unit::GeV);
0186 simhit2.setPosition(hit2.getPosition());
0187 simhit2.addToContributions(contrib2);
0188
0189 auto hitassoc2 = hitassocs_coll.create();
0190 hitassoc2.setRawHit(rawhit2);
0191 hitassoc2.setSimHit(simhit2);
0192
0193 auto hitlink2 = hitlinks_coll.create();
0194 hitlink2.setFrom(rawhit2);
0195 hitlink2.setTo(simhit2);
0196
0197 auto input = std::make_tuple(&pclust_coll, &hitlinks_coll, &hitassocs_coll);
0198 auto output = std::make_tuple(clust_coll.get(), link_coll.get(), assoc_coll.get());
0199
0200 algo.process(input, output);
0201
0202 REQUIRE(clust_coll->size() == 1);
0203 auto clust = (*clust_coll)[0];
0204
0205 REQUIRE(assoc_coll->size() == 2);
0206 REQUIRE(link_coll->size() == 2);
0207
0208
0209 REQUIRE_THAT((*assoc_coll)[0].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0210 REQUIRE((*assoc_coll)[0].getRec() == clust);
0211 REQUIRE((*assoc_coll)[0].getSim() == mcpart11);
0212
0213
0214 REQUIRE_THAT((*assoc_coll)[1].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0215 REQUIRE((*assoc_coll)[1].getRec() == clust);
0216 REQUIRE((*assoc_coll)[1].getSim() == mcpart2);
0217
0218
0219 REQUIRE_THAT((*link_coll)[0].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0220 REQUIRE((*link_coll)[0].getFrom() == clust);
0221 REQUIRE((*link_coll)[0].getTo() == mcpart11);
0222
0223
0224 REQUIRE_THAT((*link_coll)[1].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0225 REQUIRE((*link_coll)[1].getFrom() == clust);
0226 REQUIRE((*link_coll)[1].getTo() == mcpart2);
0227 }
0228
0229 TEST_CASE("the calorimeter CoG algorithm computes the cluster time",
0230 "[CalorimeterClusterRecoCoG]") {
0231 const float EPSILON = 1e-5;
0232
0233 CalorimeterClusterRecoCoG algo("CalorimeterClusterRecoCoG");
0234 CalorimeterClusterRecoCoGConfig cfg;
0235 cfg.energyWeight = "log";
0236 cfg.sampFrac = 1.0;
0237 algo.applyConfig(cfg);
0238 algo.init();
0239
0240
0241 auto run = [&](float terr1, float terr2) {
0242 edm4eic::CalorimeterHitCollection hits_coll;
0243 edm4eic::ProtoClusterCollection pclust_coll;
0244 edm4eic::MCRecoCalorimeterHitLinkCollection hitlinks_coll;
0245 edm4eic::MCRecoCalorimeterHitAssociationCollection hitassocs_coll;
0246 auto clust_coll = std::make_unique<edm4eic::ClusterCollection>();
0247 auto link_coll = std::make_unique<edm4eic::MCRecoClusterParticleLinkCollection>();
0248 auto assoc_coll = std::make_unique<edm4eic::MCRecoClusterParticleAssociationCollection>();
0249
0250 auto pclust = pclust_coll.create();
0251 auto hit1 = hits_coll.create();
0252 hit1.setCellID(0);
0253 hit1.setEnergy(0.3 * edm4eic::unit::GeV);
0254 hit1.setTime(1.25 * edm4eic::unit::ns);
0255 hit1.setTimeError(terr1);
0256 hit1.setPosition({0, 0, 1 * edm4eic::unit::mm});
0257 pclust.addToHits(hit1);
0258 pclust.addToWeights(1);
0259 auto hit2 = hits_coll.create();
0260 hit2.setCellID(1);
0261 hit2.setEnergy(0.1 * edm4eic::unit::GeV);
0262 hit2.setTime(2.75 * edm4eic::unit::ns);
0263 hit2.setTimeError(terr2);
0264 hit2.setPosition({1 * edm4eic::unit::mm, 0, 1 * edm4eic::unit::mm});
0265 pclust.addToHits(hit2);
0266 pclust.addToWeights(0.5);
0267
0268 auto input = std::make_tuple(&pclust_coll, &hitlinks_coll, &hitassocs_coll);
0269 auto output = std::make_tuple(clust_coll.get(), link_coll.get(), assoc_coll.get());
0270 algo.process(input, output);
0271 REQUIRE(clust_coll->size() == 1);
0272 return std::make_pair((*clust_coll)[0].getTime(), (*clust_coll)[0].getTimeError());
0273 };
0274
0275
0276 SECTION("energy weighted time and quadrature time error") {
0277 auto [time, timeError] = run(0.2 * edm4eic::unit::ns, 0.4 * edm4eic::unit::ns);
0278 REQUIRE_THAT(time, Catch::Matchers::WithinAbs((0.3 * 1.25 + 0.05 * 2.75) / 0.35, EPSILON));
0279 REQUIRE_THAT(timeError,
0280 Catch::Matchers::WithinAbs(std::hypot(0.2 * 0.3, 0.4 * 0.05) / 0.35, EPSILON));
0281 }
0282 SECTION("hits without time error") {
0283 auto [time, timeError] = run(0, 0);
0284 REQUIRE_THAT(time, Catch::Matchers::WithinAbs((0.3 * 1.25 + 0.05 * 2.75) / 0.35, EPSILON));
0285 REQUIRE_THAT(timeError, Catch::Matchers::WithinAbs(0, EPSILON));
0286 }
0287 }