From 3d0e716343510764ae54beddd5095af628a84870 Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Mon, 27 Nov 2023 22:42:35 -0600 Subject: [PATCH 1/4] fix: propagate string,variant map to all; rename sampFrac -> samplingFraction --- src/detectors/B0ECAL/B0ECAL.cc | 16 ++++++------ src/detectors/BEMC/BEMC.cc | 8 +++--- src/detectors/BHCAL/BHCAL.cc | 16 ++++++------ src/detectors/EEMC/EEMC.cc | 16 ++++++------ src/detectors/EHCAL/EHCAL.cc | 16 ++++++------ src/detectors/FEMC/FEMC.cc | 32 ++++++++++++------------ src/detectors/FHCAL/FHCAL.cc | 32 ++++++++++++------------ src/detectors/LUMISPECCAL/LUMISPECCAL.cc | 16 ++++++------ src/detectors/ZDC/ZDC.cc | 16 ++++++------ 9 files changed, 84 insertions(+), 84 deletions(-) diff --git a/src/detectors/B0ECAL/B0ECAL.cc b/src/detectors/B0ECAL/B0ECAL.cc index 6b4f8e211a..4ba88ac2bd 100644 --- a/src/detectors/B0ECAL/B0ECAL.cc +++ b/src/detectors/B0ECAL/B0ECAL.cc @@ -80,10 +80,10 @@ extern "C" { {"B0ECalClusters", // edm4eic::Cluster "B0ECalClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app ) @@ -97,10 +97,10 @@ extern "C" { {"B0ECalTruthClusters", // edm4eic::Cluster "B0ECalTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app ) diff --git a/src/detectors/BEMC/BEMC.cc b/src/detectors/BEMC/BEMC.cc index 2802e780df..22c1d82e75 100644 --- a/src/detectors/BEMC/BEMC.cc +++ b/src/detectors/BEMC/BEMC.cc @@ -94,10 +94,10 @@ extern "C" { {"EcalBarrelScFiClusters", // edm4eic::Cluster "EcalBarrelScFiClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/BHCAL/BHCAL.cc b/src/detectors/BHCAL/BHCAL.cc index 57ccd686b1..6ebb95cccc 100644 --- a/src/detectors/BHCAL/BHCAL.cc +++ b/src/detectors/BHCAL/BHCAL.cc @@ -102,10 +102,10 @@ extern "C" { {"HcalBarrelClusters", // edm4eic::Cluster "HcalBarrelClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -119,10 +119,10 @@ extern "C" { {"HcalBarrelTruthClusters", // edm4eic::Cluster "HcalBarrelTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/EEMC/EEMC.cc b/src/detectors/EEMC/EEMC.cc index 48d9311b23..7b934a773e 100644 --- a/src/detectors/EEMC/EEMC.cc +++ b/src/detectors/EEMC/EEMC.cc @@ -87,10 +87,10 @@ extern "C" { {"EcalEndcapNTruthClusters", // edm4eic::Cluster "EcalEndcapNTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 4.6, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 4.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -104,10 +104,10 @@ extern "C" { {"EcalEndcapNClusters", // edm4eic::Cluster "EcalEndcapNClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/EHCAL/EHCAL.cc b/src/detectors/EHCAL/EHCAL.cc index 5ffed8696b..3b4c943155 100644 --- a/src/detectors/EHCAL/EHCAL.cc +++ b/src/detectors/EHCAL/EHCAL.cc @@ -91,10 +91,10 @@ extern "C" { {"HcalEndcapNTruthClusters", // edm4eic::Cluster "HcalEndcapNTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -108,10 +108,10 @@ extern "C" { {"HcalEndcapNClusters", // edm4eic::Cluster "HcalEndcapNClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/FEMC/FEMC.cc b/src/detectors/FEMC/FEMC.cc index 2921c3c593..f15b73ef2d 100644 --- a/src/detectors/FEMC/FEMC.cc +++ b/src/detectors/FEMC/FEMC.cc @@ -87,10 +87,10 @@ extern "C" { {"EcalEndcapPTruthClusters", // edm4eic::Cluster "EcalEndcapPTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = true + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", true} }, app // TODO: Remove me once fixed ) @@ -104,10 +104,10 @@ extern "C" { {"EcalEndcapPClusters", // edm4eic::Cluster "EcalEndcapPClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -173,10 +173,10 @@ extern "C" { {"EcalEndcapPInsertTruthClusters", // edm4eic::Cluster "EcalEndcapPInsertTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = true + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", true} }, app // TODO: Remove me once fixed ) @@ -190,10 +190,10 @@ extern "C" { {"EcalEndcapPInsertClusters", // edm4eic::Cluster "EcalEndcapPInsertClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/FHCAL/FHCAL.cc b/src/detectors/FHCAL/FHCAL.cc index 8157cb0124..ad9fcd717d 100644 --- a/src/detectors/FHCAL/FHCAL.cc +++ b/src/detectors/FHCAL/FHCAL.cc @@ -95,10 +95,10 @@ extern "C" { {"HcalEndcapPInsertTruthClusters", // edm4eic::Cluster "HcalEndcapPInsertTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = true + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", true} }, app // TODO: Remove me once fixed ) @@ -112,10 +112,10 @@ extern "C" { {"HcalEndcapPInsertClusters", // edm4eic::Cluster "HcalEndcapPInsertClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -223,10 +223,10 @@ extern "C" { {"LFHCALTruthClusters", // edm4eic::Cluster "LFHCALTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 4.5, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 4.5}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -240,10 +240,10 @@ extern "C" { {"LFHCALClusters", // edm4eic::Cluster "LFHCALClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 4.5, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 4.5}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/LUMISPECCAL/LUMISPECCAL.cc b/src/detectors/LUMISPECCAL/LUMISPECCAL.cc index f5937494aa..a5553e7496 100644 --- a/src/detectors/LUMISPECCAL/LUMISPECCAL.cc +++ b/src/detectors/LUMISPECCAL/LUMISPECCAL.cc @@ -80,10 +80,10 @@ extern "C" { {"EcalLumiSpecClusters", // edm4eic::Cluster "EcalLumiSpecClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -97,10 +97,10 @@ extern "C" { {"EcalLumiSpecTruthClusters", // edm4eic::Cluster "EcalLumiSpecTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 4.6, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 4.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) diff --git a/src/detectors/ZDC/ZDC.cc b/src/detectors/ZDC/ZDC.cc index 0506b3e92e..8e3b6e6035 100644 --- a/src/detectors/ZDC/ZDC.cc +++ b/src/detectors/ZDC/ZDC.cc @@ -77,10 +77,10 @@ extern "C" { {"ZDCEcalTruthClusters", // edm4eic::Cluster "ZDCEcalTruthClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 3.6, - .enableEtaBounds = false + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 3.6}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) @@ -94,10 +94,10 @@ extern "C" { {"ZDCEcalClusters", // edm4eic::Cluster "ZDCEcalClusterAssociations"}, // edm4eic::MCRecoClusterParticleAssociation { - .energyWeight = "log", - .sampFrac = 1.0, - .logWeightBase = 6.2, - .enableEtaBounds = false, + {"energyWeight", "log"}, + {"samplingFraction", 1.0}, + {"logWeightBase", 6.2}, + {"enableEtaBounds", false} }, app // TODO: Remove me once fixed ) From 172eaf1e01c9474859d42638121c917e19fddc8e Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Sun, 26 Nov 2023 21:04:25 -0600 Subject: [PATCH 2/4] feat: use algorithms::Property in CalorimeterClusterRecoCoG --- .../calorimetry/CalorimeterClusterRecoCoG.cc | 13 +++++------ .../calorimetry/CalorimeterClusterRecoCoG.h | 14 +++++++---- .../CalorimeterClusterRecoCoG_factory.h | 23 +++++++++++++------ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/algorithms/calorimetry/CalorimeterClusterRecoCoG.cc b/src/algorithms/calorimetry/CalorimeterClusterRecoCoG.cc index fd43506acf..8624280c5e 100644 --- a/src/algorithms/calorimetry/CalorimeterClusterRecoCoG.cc +++ b/src/algorithms/calorimetry/CalorimeterClusterRecoCoG.cc @@ -30,7 +30,6 @@ #include #include "CalorimeterClusterRecoCoG.h" -#include "algorithms/calorimetry/CalorimeterClusterRecoCoGConfig.h" namespace eicrecon { @@ -40,12 +39,12 @@ namespace eicrecon { m_log = logger; // select weighting method - std::string ew = m_cfg.energyWeight; + std::string ew = m_energyWeight; // make it case-insensitive std::transform(ew.begin(), ew.end(), ew.begin(), [](char s) { return std::tolower(s); }); auto it = weightMethods.find(ew); if (it == weightMethods.end()) { - m_log->error("Cannot find energy weighting method {}, choose one from [{}]", m_cfg.energyWeight, boost::algorithm::join(weightMethods | boost::adaptors::map_keys, ", ")); + m_log->error("Cannot find energy weighting method {}, choose one from [{}]", m_energyWeight, boost::algorithm::join(weightMethods | boost::adaptors::map_keys, ", ")); return; } weightFunc = it->second; @@ -185,7 +184,7 @@ std::optional CalorimeterClusterRecoCoG::reconstruct(const edm maxHitEta = eta; } } - cl.setEnergy(totalE / m_cfg.sampFrac); + cl.setEnergy(totalE / m_samplingFraction); cl.setEnergyError(0.); cl.setTime(time); cl.setTimeError(timeError); @@ -197,7 +196,7 @@ std::optional CalorimeterClusterRecoCoG::reconstruct(const edm const auto& hit = pcl.getHits()[i]; const auto weight = pcl.getWeights()[i]; // _DBG_<<" -- weight = " << weight << " E=" << hit.getEnergy() << " totalE=" <; class CalorimeterClusterRecoCoG - : public CalorimeterClusterRecoCoGAlgorithm, - public WithPodConfig { + : public CalorimeterClusterRecoCoGAlgorithm { public: CalorimeterClusterRecoCoG(std::string_view name) @@ -87,6 +83,14 @@ namespace eicrecon { std::optional reconstruct(const edm4eic::ProtoCluster& pcl) const; + Property m_samplingFraction{this, "samplingFraction", 1.0, "Sampling fraction"}; + Property m_logWeightBase{this, "logWeightBase", 3.6, "Weight base for log weighting"}; + Property m_energyWeight{this, "energyWeight", "log", "Default hit weight method"}; + // Constrain the cluster position eta to be within + // the eta of the contributing hits. This is useful to avoid edge effects + // for endcaps. + Property m_enableEtaBounds{this, "enableEtaBounds", false, "Constrain cluster to hit eta?"}; + }; } // eicrecon diff --git a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h index f3c284b526..72a239e8c1 100644 --- a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h +++ b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h @@ -4,6 +4,7 @@ #pragma once +#include #include "algorithms/calorimetry/CalorimeterClusterRecoCoG.h" #include "services/geometry/dd4hep/DD4hep_service.h" #include "extensions/jana/JOmniFactory.h" @@ -11,7 +12,9 @@ namespace eicrecon { -class CalorimeterClusterRecoCoG_factory : public JOmniFactory { +using ConfigMap = std::map; + +class CalorimeterClusterRecoCoG_factory : public JOmniFactory { public: using AlgoT = eicrecon::CalorimeterClusterRecoCoG; @@ -23,17 +26,23 @@ class CalorimeterClusterRecoCoG_factory : public JOmniFactory m_cluster_output {this}; PodioOutput m_assoc_output {this}; - - ParameterRef m_energyWeight {this, "energyWeight", config().energyWeight}; - ParameterRef m_samplingFraction {this, "samplingFraction", config().sampFrac}; - ParameterRef m_logWeightBase {this, "logWeightBase", config().logWeightBase}; - ParameterRef m_enableEtaBounds {this, "enableEtaBounds", config().enableEtaBounds}; +/* + std::vector> m_parameters = [this](){ + std::vector> v; + v.push_back(std::make_unique>(this, "energyWeight", std::string("log"))); + return v; + }(); +*/ + ParameterRef m_energyWeight {this, "energyWeight", std::get(config()["energyWeight"])}; + ParameterRef m_samplingFraction {this, "samplingFraction", std::get(config()["samplingFraction"])}; + ParameterRef m_logWeightBase {this, "logWeightBase", std::get(config()["logWeightBase"])}; + ParameterRef m_enableEtaBounds {this, "enableEtaBounds", std::get(config()["enableEtaBounds"])}; public: void Configure() { m_algo = std::make_unique(GetPrefix()); - m_algo->applyConfig(config()); m_algo->init(logger()); + m_algo->init(); } void ChangeRun(int64_t run_number) { From 0282595c56220c9cee0a01c8fbc8195420f6c9f6 Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Fri, 1 Dec 2023 16:17:33 -0600 Subject: [PATCH 3/4] fix: automatically register ParameterRef upon Configure --- src/extensions/jana/JOmniFactory.h | 1 + .../CalorimeterClusterRecoCoG_factory.h | 46 +++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/extensions/jana/JOmniFactory.h b/src/extensions/jana/JOmniFactory.h index 379f73a480..46761d894b 100644 --- a/src/extensions/jana/JOmniFactory.h +++ b/src/extensions/jana/JOmniFactory.h @@ -181,6 +181,7 @@ class JOmniFactory : public JMultifactory { std::string m_description; virtual void Configure(JParameterManager& parman, const std::string& prefix) = 0; virtual void Configure(std::map fields) = 0; + virtual ~ParameterBase() { }; }; template diff --git a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h index 72a239e8c1..0980500caf 100644 --- a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h +++ b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h @@ -12,7 +12,7 @@ namespace eicrecon { -using ConfigMap = std::map; +using ConfigMap = std::map; class CalorimeterClusterRecoCoG_factory : public JOmniFactory { @@ -26,23 +26,39 @@ class CalorimeterClusterRecoCoG_factory : public JOmniFactory m_cluster_output {this}; PodioOutput m_assoc_output {this}; -/* - std::vector> m_parameters = [this](){ - std::vector> v; - v.push_back(std::make_unique>(this, "energyWeight", std::string("log"))); - return v; - }(); -*/ - ParameterRef m_energyWeight {this, "energyWeight", std::get(config()["energyWeight"])}; - ParameterRef m_samplingFraction {this, "samplingFraction", std::get(config()["samplingFraction"])}; - ParameterRef m_logWeightBase {this, "logWeightBase", std::get(config()["logWeightBase"])}; - ParameterRef m_enableEtaBounds {this, "enableEtaBounds", std::get(config()["enableEtaBounds"])}; + + std::vector> m_parameters; public: void Configure() { - m_algo = std::make_unique(GetPrefix()); - m_algo->init(logger()); - m_algo->init(); + m_algo = std::make_unique(GetPrefix()); + + for (const auto& [key, prop] : m_algo->getProperties()) { + std::visit( + [this, key = key](auto&& val) { + using T = std::decay_t; + if constexpr (std::is_fundamental_v + || std::is_same_v) { + // check if defined on factory generation + auto it = config().find(key); + if (it != config().end()) { + val = std::get(it->second); + config().erase(it); + } + this->m_parameters.push_back(std::move(std::make_unique>(this, std::string(key), val))); + logger()->debug("Adding parameter {} of type {} with default value {}", key, JTypeInfo::demangle(), val); + } else { + logger()->warn("No support for parsing {} of type {}", key, JTypeInfo::demangle()); + } + }, + prop.get() + ); + } + if (config().size() > 0) { + throw JException("Algorithm config contains unrecognized entries"); + } + + m_algo->init(); } void ChangeRun(int64_t run_number) { From 24be6e35afa9f2963adb72be25f27c632a0f4672 Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Sat, 2 Dec 2023 16:03:57 -0600 Subject: [PATCH 4/4] feat: move algorithms::Property registration into JOmniFactory --- src/extensions/jana/JOmniFactory.h | 40 +++++++++++++++++++ .../CalorimeterClusterRecoCoG_factory.h | 25 +----------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/extensions/jana/JOmniFactory.h b/src/extensions/jana/JOmniFactory.h index 46761d894b..413a89bdaa 100644 --- a/src/extensions/jana/JOmniFactory.h +++ b/src/extensions/jana/JOmniFactory.h @@ -18,12 +18,19 @@ #include "extensions/spdlog/SpdlogExtensions.h" #include "services/log/Log_service.h" +#include #include +#include #include using namespace eicrecon; struct EmptyConfig {}; +template +concept UsesAlgorithmsProperties = requires(AlgoT algo, ConfigT config) { + { algo.getProperties().at("key").get() } -> std::same_as; +}; + template class JOmniFactory : public JMultifactory { public: @@ -260,6 +267,39 @@ class JOmniFactory : public JMultifactory { m_parameters.push_back(parameter); } + template + requires UsesAlgorithmsProperties + void RegisterAllParameters(std::unique_ptr& algo, std::vector>& parameters) { + for (const auto& [key, prop] : algo->getProperties()) { + std::visit( + [this, key = key, ¶meters](auto&& val) { + using T = std::decay_t; + if constexpr (std::is_fundamental_v || std::is_same_v) { + // check if defined on factory generation + auto it = config().find(key); + if (it != config().end()) { + logger()->debug("Overriding parameter {} default value of {} with {}", key, val, std::get(it->second)); + val = std::get(it->second); + config().erase(it); + } + parameters.push_back(std::move(std::make_unique>(this, std::string(key), val))); + logger()->debug("Adding parameter {} of type {} with default value {}", key, JTypeInfo::demangle(), val); + } else { + auto msg = fmt::format("No support for parsing {} of type {}", key, JTypeInfo::demangle()); + logger()->critical(msg); + throw JException(msg); + } + }, + prop.get() + ); + } + if (config().size() > 0) { + auto msg = fmt::format("Algorithm {} config contains unrecognized parameters {}", GetPrefix(), std::views::keys(config())); + logger()->critical(msg); + throw JException(msg); + } + } + void ConfigureAllParameters(std::map fields) { for (auto* parameter : this->m_parameters) { parameter->Configure(fields); diff --git a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h index 0980500caf..9f755924c7 100644 --- a/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h +++ b/src/factories/calorimetry/CalorimeterClusterRecoCoG_factory.h @@ -33,30 +33,7 @@ class CalorimeterClusterRecoCoG_factory : public JOmniFactory(GetPrefix()); - for (const auto& [key, prop] : m_algo->getProperties()) { - std::visit( - [this, key = key](auto&& val) { - using T = std::decay_t; - if constexpr (std::is_fundamental_v - || std::is_same_v) { - // check if defined on factory generation - auto it = config().find(key); - if (it != config().end()) { - val = std::get(it->second); - config().erase(it); - } - this->m_parameters.push_back(std::move(std::make_unique>(this, std::string(key), val))); - logger()->debug("Adding parameter {} of type {} with default value {}", key, JTypeInfo::demangle(), val); - } else { - logger()->warn("No support for parsing {} of type {}", key, JTypeInfo::demangle()); - } - }, - prop.get() - ); - } - if (config().size() > 0) { - throw JException("Algorithm config contains unrecognized entries"); - } + RegisterAllParameters(m_algo, m_parameters); m_algo->init(); }