Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-06 07:55:53

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2024, Sebouh Paul
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/EDM4eicVersion.h>
0010 #include <edm4hep/Vector2i.h>
0011 #include <edm4hep/Vector3d.h>
0012 #if EDM4EIC_VERSION_MAJOR >= 7
0013 #include <edm4eic/MCRecoCalorimeterHitAssociationCollection.h>
0014 #endif
0015 #include <edm4eic/MCRecoClusterParticleAssociationCollection.h>
0016 #include <edm4eic/ProtoClusterCollection.h>
0017 #include <edm4eic/unit_system.h>
0018 #include <edm4hep/CaloHitContributionCollection.h>
0019 #include <edm4hep/MCParticleCollection.h>
0020 #include <edm4hep/RawCalorimeterHitCollection.h>
0021 #include <edm4hep/SimCalorimeterHitCollection.h>
0022 #include <edm4hep/Vector3f.h>
0023 #include <spdlog/common.h>
0024 #include <spdlog/logger.h>
0025 #include <spdlog/spdlog.h>
0026 #include <memory>
0027 #include <tuple>
0028 #include <vector>
0029 
0030 #include "algorithms/calorimetry/CalorimeterClusterRecoCoG.h"
0031 #include "algorithms/calorimetry/CalorimeterClusterRecoCoGConfig.h"
0032 
0033 using eicrecon::CalorimeterClusterRecoCoG;
0034 using eicrecon::CalorimeterClusterRecoCoGConfig;
0035 
0036 TEST_CASE("the calorimeter CoG algorithm runs", "[CalorimeterClusterRecoCoG]") {
0037   const float EPSILON = 1e-5;
0038 
0039   CalorimeterClusterRecoCoG algo("CalorimeterClusterRecoCoG");
0040 
0041   std::shared_ptr<spdlog::logger> logger =
0042       spdlog::default_logger()->clone("CalorimeterClusterRecoCoG");
0043   logger->set_level(spdlog::level::trace);
0044 
0045   CalorimeterClusterRecoCoGConfig cfg;
0046   cfg.energyWeight        = "log";
0047   cfg.sampFrac            = 0.0203;
0048   cfg.logWeightBaseCoeffs = {5.0, 0.65, 0.31};
0049   cfg.logWeightBase_Eref  = 50 * edm4eic::unit::GeV;
0050 
0051   algo.applyConfig(cfg);
0052   algo.init();
0053 
0054   edm4hep::RawCalorimeterHitCollection rawhits_coll;
0055   edm4eic::CalorimeterHitCollection hits_coll;
0056   edm4eic::ProtoClusterCollection pclust_coll;
0057   edm4hep::SimCalorimeterHitCollection simhits_coll;
0058 #if EDM4EIC_VERSION_MAJOR >= 7
0059   edm4eic::MCRecoCalorimeterHitAssociationCollection hitassocs_coll;
0060 #endif
0061   edm4hep::CaloHitContributionCollection contribs_coll;
0062   edm4hep::MCParticleCollection mcparts_coll;
0063   auto assoc_coll = std::make_unique<edm4eic::MCRecoClusterParticleAssociationCollection>();
0064   auto clust_coll = std::make_unique<edm4eic::ClusterCollection>();
0065 
0066   //create a protocluster with 3 hits
0067   auto pclust = pclust_coll.create();
0068   edm4hep::Vector3f position({0, 0, 1 * edm4eic::unit::mm});
0069 
0070   auto rawhit1 = rawhits_coll.create();
0071 
0072   auto hit1 = hits_coll.create();
0073   hit1.setCellID(0);
0074   hit1.setEnergy(0.1 * edm4eic::unit::GeV);
0075   hit1.setEnergyError(0);
0076   hit1.setTime(0);
0077   hit1.setTimeError(0);
0078   hit1.setPosition(position);
0079   hit1.setDimension({0, 0, 0});
0080   hit1.setLocal(position);
0081 #if EDM4EIC_VERSION_MAJOR >= 7
0082   hit1.setRawHit(rawhit1);
0083 #endif
0084   pclust.addToHits(hit1);
0085   pclust.addToWeights(1);
0086 
0087   auto mcpart11 = mcparts_coll.create(11,                  // std::int32_t PDG
0088                                       1,                   // std::int32_t generatorStatus
0089                                       0,                   // std::int32_t simulatorStatus
0090                                       -1.,                 // float charge
0091                                       0.,                  // float time
0092                                       0.,                  // double mass
0093                                       edm4hep::Vector3d(), // edm4hep::Vector3d vertex
0094                                       edm4hep::Vector3d(), // edm4hep::Vector3d endpoint
0095                                       edm4hep::Vector3f(), // edm4hep::Vector3f momentum
0096                                       edm4hep::Vector3f(), // edm4hep::Vector3f momentumAtEndpoint
0097                                       edm4hep::Vector3f(), // edm4hep::Vector3f spin
0098                                       edm4hep::Vector2i()  // edm4hep::Vector2i colorFlow
0099   );
0100 
0101   auto mcpart12 = mcparts_coll.create(
0102       22,                                                   // std::int32_t PDG
0103       0,                                                    // std::int32_t generatorStatus
0104       (0x1 << edm4hep::MCParticle::BITCreatedInSimulation), // std::int32_t simulatorStatus
0105       0.,                                                   // float charge
0106       0.,                                                   // float time
0107       0.,                                                   // double mass
0108       edm4hep::Vector3d(),                                  // edm4hep::Vector3d vertex
0109       edm4hep::Vector3d(),                                  // edm4hep::Vector3d endpoint
0110       edm4hep::Vector3f(),                                  // edm4hep::Vector3f momentum
0111       edm4hep::Vector3f(),                                  // edm4hep::Vector3f momentumAtEndpoint
0112       edm4hep::Vector3f(),                                  // edm4hep::Vector3f spin
0113       edm4hep::Vector2i()                                   // edm4hep::Vector2i colorFlow
0114   );
0115 
0116   mcpart12.addToParents(mcpart11);
0117   mcpart11.addToDaughters(mcpart12);
0118 
0119   auto contrib11 = contribs_coll.create(0,                         // int32_t PDG
0120                                         0.05 * edm4eic::unit::GeV, // float energy
0121                                         0.0,                       // float time
0122                                         edm4hep::Vector3f()        // edm4hep::Vector3f stepPosition
0123   );
0124   contrib11.setParticle(mcpart11);
0125   auto contrib12 = contribs_coll.create(0,                         // int32_t PDG
0126                                         0.05 * edm4eic::unit::GeV, // float energy
0127                                         0.0,                       // float time
0128                                         edm4hep::Vector3f()        // edm4hep::Vector3f stepPosition
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 #if EDM4EIC_VERSION_MAJOR >= 7
0140   auto hitassoc1 = hitassocs_coll->create();
0141   hitassoc1.setRawHit(rawhit1);
0142   hitassoc1.setSimHit(simhit1);
0143 #endif
0144 
0145   auto rawhit2 = rawhits_coll.create();
0146 
0147   position  = {-1 * edm4eic::unit::mm, 0, 2 * edm4eic::unit::mm};
0148   auto hit2 = hits_coll.create();
0149   hit2.setCellID(1);
0150   hit2.setEnergy(0.1 * edm4eic::unit::GeV);
0151   hit2.setEnergyError(0);
0152   hit2.setTime(0);
0153   hit2.setTimeError(0);
0154   hit2.setPosition(position);
0155   hit2.setDimension({0, 0, 0});
0156   hit2.setLocal(position);
0157 #if EDM4EIC_VERSION_MAJOR >= 7
0158   hit2.setRawHit(rawhit2);
0159 #endif
0160   pclust.addToHits(hit2);
0161   pclust.addToWeights(1);
0162 
0163   auto mcpart2 = mcparts_coll.create(
0164       211,                                                  // std::int32_t PDG
0165       0,                                                    // std::int32_t generatorStatus
0166       (0x1 << edm4hep::MCParticle::BITCreatedInSimulation), // std::int32_t simulatorStatus
0167       0.,                                                   // float charge
0168       0.,                                                   // float time
0169       0.,                                                   // double mass
0170       edm4hep::Vector3d(),                                  // edm4hep::Vector3d vertex
0171       edm4hep::Vector3d(),                                  // edm4hep::Vector3d endpoint
0172       edm4hep::Vector3f(),                                  // edm4hep::Vector3f momentum
0173       edm4hep::Vector3f(),                                  // edm4hep::Vector3f momentumAtEndpoint
0174       edm4hep::Vector3f(),                                  // edm4hep::Vector3f spin
0175       edm4hep::Vector2i()                                   // edm4hep::Vector2i colorFlow
0176   );
0177 
0178   auto contrib2 = contribs_coll.create(0,                        // int32_t PDG
0179                                        0.1 * edm4eic::unit::GeV, // float energy
0180                                        0.0,                      // float time
0181                                        edm4hep::Vector3f()       // edm4hep::Vector3f stepPosition
0182   );
0183   contrib2.setParticle(mcpart2);
0184 
0185   auto simhit2 = simhits_coll.create();
0186   simhit2.setCellID(hit2.getCellID());
0187   simhit2.setEnergy(0.1 * edm4eic::unit::GeV);
0188   simhit2.setPosition(hit2.getPosition());
0189   simhit2.addToContributions(contrib2);
0190 
0191 #if EDM4EIC_VERSION_MAJOR >= 7
0192   auto hitassoc2 = hitassocs_coll->create();
0193   hitassoc2.setRawHit(rawhit2);
0194   hitassoc2.setSimHit(simhit2);
0195 #endif
0196 
0197   // Constructing input and output as per the algorithm's expected signature
0198 #if EDM4EIC_VERSION_MAJOR >= 7
0199   auto input = std::make_tuple(&pclust_coll, &hitassocs_coll);
0200 #else
0201   auto input = std::make_tuple(&pclust_coll, &simhits_coll);
0202 #endif
0203   auto output = std::make_tuple(clust_coll.get(), assoc_coll.get());
0204 
0205   algo.process(input, output);
0206 
0207   REQUIRE(clust_coll->size() == 1);
0208   auto clust = (*clust_coll)[0];
0209 
0210   REQUIRE(assoc_coll->size() == 2);
0211 
0212   // Half of the energy comes from mcpart11 and its daughter mcpart12
0213   REQUIRE_THAT((*assoc_coll)[0].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0214   REQUIRE((*assoc_coll)[0].getRec() == clust);
0215   REQUIRE((*assoc_coll)[0].getSim() == mcpart11);
0216 
0217   // Half of the energy comes from mcpart2
0218   REQUIRE_THAT((*assoc_coll)[1].getWeight(), Catch::Matchers::WithinAbs(0.5, EPSILON));
0219   REQUIRE((*assoc_coll)[1].getRec() == clust);
0220   REQUIRE((*assoc_coll)[1].getSim() == mcpart2);
0221 }