Back to home page

EIC code displayed by LXR

 
 

    


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