Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:16:23

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