Skip to content

feat: use algorithms logger service in IrtCherenkovParticleID #1839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c3fcbf0
feat: use algorithms logger service in IrtCherenkovParticleID
wdconinc Apr 24, 2025
1825ab8
fix: include headers with full path under src
wdconinc Apr 25, 2025
b951441
fix: define private forwarding function log with friends
wdconinc Apr 25, 2025
ad98320
fix: move config print functions into algorithm as friends
wdconinc Apr 25, 2025
501e687
feat: Tools as private CRTP mixin
wdconinc Apr 25, 2025
31d9b7c
fix: use algorithms::logger in MergeParticleID
wdconinc Apr 25, 2025
cac5b07
fix: use Tools functions, make them const and not static
wdconinc Apr 25, 2025
b2f789c
fix: adapt tests/.../pid_MergeParticleID.cc
wdconinc Apr 25, 2025
6f0fc19
fix: ignore warning for init(CherenkovDetectorCollection*)
wdconinc Apr 28, 2025
5090e8f
fix: be more explicit in which log<lvl> to fall back on
wdconinc Apr 28, 2025
7da9d01
fix: rename log<lvl> back to report_fmt<lvl>
wdconinc Apr 30, 2025
a3430d3
fix: allow for older algorithms (route to info in that case)
wdconinc May 1, 2025
803c74c
fix: also add ifdef in MergeParticleID
wdconinc May 2, 2025
db36648
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
f0e3ea2
fix: iwyu
wdconinc May 2, 2025
f1d4be1
fix: get rid of IrtCherenkovParticleIDConfig::Print
wdconinc May 2, 2025
92ff5ab
fix: get rid of MergeParticleIDConfig::Print
wdconinc May 2, 2025
f127c3e
fix: get rid of IrtCherenkovParticleIDConfig::PrintCheats
wdconinc May 2, 2025
9c4c73c
fix: rm forward declares
wdconinc May 2, 2025
bf199b8
fix: iwyu
wdconinc May 2, 2025
8e5d6fa
fix: rm unused static
wdconinc May 2, 2025
b1f5c7f
fix: have Print* functions return formatted string only, caller must …
wdconinc May 2, 2025
33eaf6f
fix: make Tools a namespace again
wdconinc May 2, 2025
8bcccba
fix: rm report_fmt interface (hindsight 20/20)
wdconinc May 2, 2025
39fc319
fix: iwyu
wdconinc May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmake/jana_plugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ macro(plugin_add_algorithms _name)
find_package(algorithms REQUIRED)
endif()

if(${_name}_WITH_LIBRARY)
target_compile_definitions(
${PLUGIN_NAME}_library
PRIVATE "algorithms_VERSION_MAJOR=${algorithms_VERSION_MAJOR}"
"algorithms_VERSION_MINOR=${algorithms_VERSION_MINOR}")
endif()
if(${_name}_WITH_PLUGIN)
target_compile_definitions(
${PLUGIN_NAME}_plugin
PRIVATE "algorithms_VERSION_MAJOR=${algorithms_VERSION_MAJOR}"
"algorithms_VERSION_MINOR=${algorithms_VERSION_MINOR}")
endif()

plugin_link_libraries(${_name} algorithms::algocore)

endmacro()
Expand Down
146 changes: 84 additions & 62 deletions src/algorithms/pid/IrtCherenkovParticleID.cc

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions src/algorithms/pid/IrtCherenkovParticleID.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
#include <edm4eic/MCRecoTrackerHitAssociationCollection.h>
#include <edm4eic/RawTrackerHitCollection.h>
#include <edm4eic/TrackSegmentCollection.h>
#include <spdlog/logger.h>
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>

// EICrecon
#include "IrtCherenkovParticleIDConfig.h"
#include "algorithms/interfaces/ParticleSvc.h"
#include "algorithms/interfaces/WithPodConfig.h"
#include "algorithms/pid/IrtCherenkovParticleIDConfig.h"

namespace eicrecon {

Expand Down Expand Up @@ -51,12 +49,18 @@ class IrtCherenkovParticleID : public IrtCherenkovParticleIDAlgorithm,
{"outputAerogelParticleIDs", "outputGasParticleIDs"},
"Effectively 'zip' the input particle IDs"} {}

void init(CherenkovDetectorCollection* irt_det_coll, std::shared_ptr<spdlog::logger>& logger);
// FIXME: init() must not take arguments
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Woverloaded-virtual"
void init(CherenkovDetectorCollection* irt_det_coll);
#pragma clang diagnostic pop
#pragma GCC diagnostic pop

void process(const Input&, const Output&) const;

private:
std::shared_ptr<spdlog::logger> m_log;
CherenkovDetectorCollection* m_irt_det_coll;
CherenkovDetector* m_irt_det;

Expand All @@ -67,4 +71,5 @@ class IrtCherenkovParticleID : public IrtCherenkovParticleIDAlgorithm,
std::unordered_map<int, double> m_pdg_mass;
std::map<std::string, CherenkovRadiator*> m_pid_radiators;
};

} // namespace eicrecon
47 changes: 18 additions & 29 deletions src/algorithms/pid/IrtCherenkovParticleIDConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <Evaluator/DD4hepUnits.h>
#include <algorithms/logger.h>
#include <spdlog/spdlog.h>

namespace eicrecon {
Expand Down Expand Up @@ -45,43 +46,31 @@ class IrtCherenkovParticleIDConfig {
//
/////////////////////////////////////////////////////

// print warnings about cheat modes
void PrintCheats(std::shared_ptr<spdlog::logger> m_log,
spdlog::level::level_enum lvl = spdlog::level::debug, bool printAll = false) {
auto print_param = [&m_log, &lvl, &printAll](auto name, bool val, auto desc) {
if (printAll)
m_log->log(lvl, " {:>20} = {:<}", name, val);
else if (val)
m_log->warn("CHEAT MODE '{}' ENABLED: {}", name, desc);
};
print_param("cheatPhotonVertex", cheatPhotonVertex,
"use MC photon vertex, wavelength, refractive index");
print_param("cheatTrueRadiator", cheatTrueRadiator, "use MC truth to obtain true radiator");
}

// boolean: true if any cheat mode is enabled
bool CheatModeEnabled() const { return cheatPhotonVertex || cheatTrueRadiator; }

// print all parameters
void Print(std::shared_ptr<spdlog::logger> m_log,
spdlog::level::level_enum lvl = spdlog::level::debug) {
m_log->log(lvl, "{:=^60}", " IrtCherenkovParticleIDConfig Settings ");
auto print_param = [&m_log, &lvl](auto name, auto val) {
m_log->log(lvl, " {:>20} = {:<}", name, val);
// stream all parameters
friend std::ostream& operator<<(std::ostream& os, const IrtCherenkovParticleIDConfig& cfg) {
os << fmt::format("{:=^60}", " IrtCherenkovParticleIDConfig Settings ") << std::endl;
auto print_param = [&os](auto name, auto val) {
os << fmt::format(" {:>20} = {:<}", name, val) << std::endl;
};
print_param("numRIndexBins", numRIndexBins);
PrintCheats(m_log, lvl, true);
m_log->log(lvl, "pdgList:");
for (const auto& pdg : pdgList)
m_log->log(lvl, " {}", pdg);
for (const auto& [name, rad] : radiators) {
m_log->log(lvl, "{:-<60}", fmt::format("--- {} config ", name));
print_param("numRIndexBins", cfg.numRIndexBins);
print_param("cheatPhotonVertex", cfg.cheatPhotonVertex);
print_param("cheatTrueRadiator", cfg.cheatTrueRadiator);
os << "pdgList:" << std::endl;
for (const auto& pdg : cfg.pdgList)
os << fmt::format(" {}", pdg) << std::endl;
for (const auto& [name, rad] : cfg.radiators) {
os << fmt::format("{:-<60}", fmt::format("--- {} config ", name)) << std::endl;
print_param("smearingMode", rad.smearingMode);
print_param("smearing", rad.smearing);
print_param("referenceRIndex", rad.referenceRIndex);
print_param("attenuation", rad.attenuation);
}
m_log->log(lvl, "{:=^60}", "");
}
os << fmt::format("{:=^60}", "") << std::endl;
return os;
};
};

} // namespace eicrecon
44 changes: 20 additions & 24 deletions src/algorithms/pid/MergeParticleID.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include "MergeParticleID.h"

#include <algorithms/logger.h>
#include <edm4eic/CherenkovParticleIDHypothesis.h>
#include <edm4eic/TrackSegmentCollection.h>
#include <edm4hep/Vector2f.h>
#include <fmt/core.h>
#include <podio/ObjectID.h>
#include <podio/RelationRange.h>
#include <spdlog/common.h>
#include <cstddef>
#include <gsl/pointers>
#include <stdexcept>
Expand All @@ -21,10 +21,7 @@

namespace eicrecon {

void MergeParticleID::init(std::shared_ptr<spdlog::logger>& logger) {
m_log = logger;
m_cfg.Print(m_log, spdlog::level::debug);
}
void MergeParticleID::init() { debug() << m_cfg << endmsg; }

void MergeParticleID::process(const MergeParticleID::Input& input,
const MergeParticleID::Output& output) const {
Expand Down Expand Up @@ -68,25 +65,25 @@ void MergeParticleID::process(const MergeParticleID::Input& input,
std::unordered_map<decltype(podio::ObjectID::index),
std::vector<std::pair<std::size_t, std::size_t>>>
particle_pids;
m_log->trace("{:-<70}", "Build `particle_pids` indexing data structure ");
trace("{:-<70}", "Build `particle_pids` indexing data structure ");

// loop over list of PID collections
for (std::size_t idx_coll = 0; idx_coll < in_pid_collections_list.size(); idx_coll++) {
const auto& in_pid_collection = in_pid_collections_list.at(idx_coll);
m_log->trace("idx_col={}", idx_coll);
trace("idx_col={}", idx_coll);

// loop over this PID collection
for (std::size_t idx_pid = 0; idx_pid < in_pid_collection->size(); idx_pid++) {
// make the index pair
const auto& in_pid = in_pid_collection->at(idx_pid);
auto& charged_particle_track_segment = in_pid.getChargedParticle();
if (!charged_particle_track_segment.isAvailable()) {
m_log->error("PID object found with no charged particle");
error("PID object found with no charged particle");
continue;
}
auto id_particle = charged_particle_track_segment.getObjectID().index;
auto idx_paired = std::make_pair(idx_coll, idx_pid);
m_log->trace(" idx_pid={} id_particle={}", idx_pid, id_particle);
trace(" idx_pid={} id_particle={}", idx_pid, id_particle);

// insert in `particle_pids`
auto it = particle_pids.find(id_particle);
Expand All @@ -98,26 +95,26 @@ void MergeParticleID::process(const MergeParticleID::Input& input,
}

// trace logging
if (m_log->level() <= spdlog::level::trace) {
m_log->trace("{:-<70}", "Resulting `particle_pids` ");
if (level() <= algorithms::LogLevel::kTrace) {
trace("{:-<70}", "Resulting `particle_pids` ");
for (auto& [id_particle, idx_paired_list] : particle_pids) {
m_log->trace("id_particle={}", id_particle);
trace("id_particle={}", id_particle);
for (auto& [idx_coll, idx_pid] : idx_paired_list)
m_log->trace(" (idx_coll, idx_pid) = ({}, {})", idx_coll, idx_pid);
trace(" (idx_coll, idx_pid) = ({}, {})", idx_coll, idx_pid);
}
}

// --------------------------------------------------------------------------------

// loop over charged particles, combine weights from the associated `CherenkovParticleID` objects
// and create a merged output `CherenkovParticleID` object
m_log->trace("{:-<70}", "Begin Merging PID Objects ");
trace("{:-<70}", "Begin Merging PID Objects ");
for (auto& [id_particle, idx_paired_list] : particle_pids) {

// trace logging
m_log->trace("Charged Particle:");
m_log->trace(" id = {}", id_particle);
m_log->trace(" PID Hypotheses:");
trace("Charged Particle:");
trace(" id = {}", id_particle);
trace(" PID Hypotheses:");

// create mutable output `CherenkovParticleID` object `out_pid`
auto out_pid = out_pids->create();
Expand All @@ -135,9 +132,8 @@ void MergeParticleID::process(const MergeParticleID::Input& input,
const auto& in_pid = in_pid_collections_list.at(idx_coll)->at(idx_pid);

// logging
m_log->trace(" Hypotheses for PID result (idx_coll, idx_pid) = ({}, {}):", idx_coll,
idx_pid);
Tools::PrintHypothesisTableHead(m_log, 6);
trace(" Hypotheses for PID result (idx_coll, idx_pid) = ({}, {}):", idx_coll, idx_pid);
trace(Tools::HypothesisTableHead(6));

// merge scalar members
out_npe += in_pid.getNpe(); // sum
Expand All @@ -154,7 +150,7 @@ void MergeParticleID::process(const MergeParticleID::Input& input,

// merge PDG hypotheses, combining their weights and other members
for (auto in_hyp : in_pid.getHypotheses()) {
Tools::PrintHypothesisTableLine(m_log, in_hyp, 6);
trace(Tools::HypothesisTableLine(in_hyp, 6));
auto out_hyp_it = pdg_2_out_hyp.find(in_hyp.PDG);
if (out_hyp_it == pdg_2_out_hyp.end()) {
edm4eic::CherenkovParticleIDHypothesis out_hyp;
Expand Down Expand Up @@ -194,10 +190,10 @@ void MergeParticleID::process(const MergeParticleID::Input& input,
out_pid.addToHypotheses(out_hyp);

// logging: print merged hypothesis table
m_log->trace(" => merged hypothesis weights:");
Tools::PrintHypothesisTableHead(m_log, 6);
trace(" => merged hypothesis weights:");
trace(Tools::HypothesisTableHead(6));
for (auto out_hyp : out_pid.getHypotheses())
Tools::PrintHypothesisTableLine(m_log, out_hyp, 6);
trace(Tools::HypothesisTableLine(out_hyp, 6));

} // end `particle_pids` loop over charged particles
}
Expand Down
10 changes: 3 additions & 7 deletions src/algorithms/pid/MergeParticleID.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
// data model
#include <algorithms/algorithm.h>
#include <edm4eic/CherenkovParticleIDCollection.h>
#include <spdlog/logger.h>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

// EICrecon
#include "MergeParticleIDConfig.h"
#include "algorithms/interfaces/WithPodConfig.h"
#include "algorithms/pid/MergeParticleIDConfig.h"

namespace eicrecon {

Expand All @@ -34,15 +32,13 @@ class MergeParticleID : public MergeParticleIDAlgorithm,
{"outputTrackSegments"},
"Effectively 'zip' the input particle IDs"} {}

void init(std::shared_ptr<spdlog::logger>& logger);
void init();

// - input: a list of particle ID collections, which we want to merge together
// - output: the merged particle ID collection
// - overload this function to support different collections from other PID subsystems, or to support
// merging PID results from overlapping subsystems
void process(const Input&, const Output&) const final;

private:
std::shared_ptr<spdlog::logger> m_log;
};

} // namespace eicrecon
19 changes: 11 additions & 8 deletions src/algorithms/pid/MergeParticleIDConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <algorithms/logger.h>
#include <spdlog/spdlog.h>

namespace eicrecon {
Expand All @@ -20,15 +21,17 @@ class MergeParticleIDConfig {
//
/////////////////////////////////////////////////////

// print all parameters
void Print(std::shared_ptr<spdlog::logger> m_log,
spdlog::level::level_enum lvl = spdlog::level::debug) {
m_log->log(lvl, "{:=^60}", " MergeParticleIDConfig Settings ");
auto print_param = [&m_log, &lvl](auto name, auto val) {
m_log->log(lvl, " {:>20} = {:<}", name, val);
// stream all parameters
friend std::ostream& operator<<(std::ostream& os, const MergeParticleIDConfig& cfg) {
// print all parameters
os << fmt::format("{:=^60}", " MergeParticleIDConfig Settings ") << std::endl;
auto print_param = [&os](auto name, auto val) {
os << fmt::format(" {:>20} = {:<}", name, val) << std::endl;
};
print_param("mergeMode", mergeMode);
m_log->log(lvl, "{:=^60}", "");
print_param("mergeMode", cfg.mergeMode);
os << fmt::format("{:=^60}", "") << std::endl;
return os;
}
};

} // namespace eicrecon
Loading
Loading