Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-06 09:21:54

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