Skip to content

Commit f22dbf2

Browse files
committed
Add Truthiness collection output if it exists
1 parent 2c5da0c commit f22dbf2

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

src/algorithms/reco/Truthiness.cc

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,38 @@
1313
#include <cmath>
1414
#include <set>
1515

16+
#if __has_include(<edm4eic/Truthiness.h>)
17+
#include <edm4eic/Truthiness.h>
18+
#include <edm4eic/TruthinessCollection.h>
19+
#endif
20+
1621
namespace eicrecon {
1722

18-
void Truthiness::process(const Truthiness::Input& input,
19-
const Truthiness::Output& /* output */) const {
23+
void Truthiness::process(const Truthiness::Input& input, const Truthiness::Output& output) const {
2024

2125
const auto [mc_particles, rc_particles, associations] = input;
2226

23-
double truthiness = 0.0;
27+
double truthiness = 0.0;
28+
double total_pid_contribution = 0.0;
29+
double total_energy_contribution = 0.0;
30+
double total_momentum_contribution = 0.0;
2431

2532
// Track which MC particles and reconstructed particles are covered by associations
2633
std::set<edm4hep::MCParticle> associated_mc_particles;
2734
std::set<edm4eic::ReconstructedParticle> associated_rc_particles;
2835

36+
#if __has_include(<edm4eic/Truthiness.h>)
37+
// Vectors to store per-association contributions
38+
std::vector<float> assoc_truthiness_vec;
39+
std::vector<float> assoc_pid_vec;
40+
std::vector<float> assoc_energy_vec;
41+
std::vector<float> assoc_momentum_vec;
42+
assoc_truthiness_vec.reserve(associations->size());
43+
assoc_pid_vec.reserve(associations->size());
44+
assoc_energy_vec.reserve(associations->size());
45+
assoc_momentum_vec.reserve(associations->size());
46+
#endif
47+
2948
// Process all associations
3049
for (const auto& assoc : *associations) {
3150
const auto& mc_part = assoc.getSim();
@@ -66,14 +85,30 @@ void Truthiness::process(const Truthiness::Input& input,
6685
momentum_penalty, pdg_penalty);
6786

6887
truthiness += assoc_penalty;
88+
total_pid_contribution += pdg_penalty;
89+
total_energy_contribution += energy_penalty;
90+
total_momentum_contribution += momentum_penalty;
91+
92+
#if __has_include(<edm4eic/Truthiness.h>)
93+
assoc_truthiness_vec.push_back(static_cast<float>(assoc_penalty));
94+
assoc_pid_vec.push_back(static_cast<float>(pdg_penalty));
95+
assoc_energy_vec.push_back(static_cast<float>(energy_penalty));
96+
assoc_momentum_vec.push_back(static_cast<float>(momentum_penalty));
97+
#endif
6998
}
7099

71100
// Penalty for unassociated charged MC particles with generator status 2
72101
int unassociated_mc_count = 0;
102+
#if __has_include(<edm4eic/Truthiness.h>)
103+
std::vector<edm4hep::MCParticle> unassociated_mc_vec;
104+
#endif
73105
for (const auto& mc_part : *mc_particles) {
74106
if (mc_part.getGeneratorStatus() == 2 && mc_part.getCharge() != 0.0) {
75107
if (associated_mc_particles.find(mc_part) == associated_mc_particles.end()) {
76108
unassociated_mc_count++;
109+
#if __has_include(<edm4eic/Truthiness.h>)
110+
unassociated_mc_vec.push_back(mc_part);
111+
#endif
77112
trace("Unassociated MC particle: PDG={}, charge={:.1f}, status={}", mc_part.getPDG(),
78113
mc_part.getCharge(), mc_part.getGeneratorStatus());
79114
}
@@ -86,9 +121,15 @@ void Truthiness::process(const Truthiness::Input& input,
86121

87122
// Penalty for unassociated reconstructed particles
88123
int unassociated_rc_count = 0;
124+
#if __has_include(<edm4eic/Truthiness.h>)
125+
std::vector<edm4eic::ReconstructedParticle> unassociated_rc_vec;
126+
#endif
89127
for (const auto& rc_part : *rc_particles) {
90128
if (associated_rc_particles.find(rc_part) == associated_rc_particles.end()) {
91129
unassociated_rc_count++;
130+
#if __has_include(<edm4eic/Truthiness.h>)
131+
unassociated_rc_vec.push_back(rc_part);
132+
#endif
92133
trace("Unassociated reconstructed particle: PDG={}, E={:.3f}, p=[{:.3f},{:.3f},{:.3f}]",
93134
rc_part.getPDG(), rc_part.getEnergy(), rc_part.getMomentum().x, rc_part.getMomentum().y,
94135
rc_part.getMomentum().z);
@@ -107,6 +148,45 @@ void Truthiness::process(const Truthiness::Input& input,
107148
// Report final truthiness
108149
debug("Event truthiness: {:.6f} (from {} associations, {} unassociated MC, {} unassociated RC)",
109150
truthiness, associations->size(), unassociated_mc_count, unassociated_rc_count);
151+
152+
#if __has_include(<edm4eic/Truthiness.h>)
153+
// Create output collection if available
154+
const auto [truthiness_output] = output;
155+
auto truthiness_obj = truthiness_output->create();
156+
157+
// Set scalar values
158+
truthiness_obj.setEvent_truthiness(static_cast<float>(truthiness));
159+
truthiness_obj.setEvent_pid_contribution(static_cast<float>(total_pid_contribution));
160+
truthiness_obj.setEvent_energy_contribution(static_cast<float>(total_energy_contribution));
161+
truthiness_obj.setEvent_momentum_contribution(static_cast<float>(total_momentum_contribution));
162+
truthiness_obj.setUnassociated_mc_particles_contribution(static_cast<float>(mc_penalty));
163+
truthiness_obj.setUnassociated_rc_particles_contribution(static_cast<float>(rc_penalty));
164+
165+
// Add associations and their contributions
166+
for (const auto& assoc : *associations) {
167+
truthiness_obj.addToAssociations(assoc);
168+
}
169+
for (const auto& val : assoc_truthiness_vec) {
170+
truthiness_obj.addToAssociation_truthiness(val);
171+
}
172+
for (const auto& val : assoc_pid_vec) {
173+
truthiness_obj.addToAssociation_pid_contribution(val);
174+
}
175+
for (const auto& val : assoc_energy_vec) {
176+
truthiness_obj.addToAssociation_energy_contribution(val);
177+
}
178+
for (const auto& val : assoc_momentum_vec) {
179+
truthiness_obj.addToAssociation_momentum_contribution(val);
180+
}
181+
182+
// Add unassociated particles
183+
for (const auto& mc_part : unassociated_mc_vec) {
184+
truthiness_obj.addToUnassociated_mc_particles(mc_part);
185+
}
186+
for (const auto& rc_part : unassociated_rc_vec) {
187+
truthiness_obj.addToUnassociated_rc_particles(rc_part);
188+
}
189+
#endif
110190
}
111191

112192
} // namespace eicrecon

src/algorithms/reco/Truthiness.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@
1010
#include <stdint.h>
1111
#include <string>
1212
#include <string_view>
13+
#if __has_include(<edm4eic/Truthiness.h>)
14+
#include <edm4eic/TruthinessCollection.h>
15+
#endif
1316

1417
#include "TruthinessConfig.h"
1518
#include "algorithms/interfaces/WithPodConfig.h"
1619

1720
namespace eicrecon {
1821

22+
#if __has_include(<edm4eic/Truthiness.h>)
23+
using TruthinessAlgorithm = algorithms::Algorithm<
24+
algorithms::Input<edm4hep::MCParticleCollection, edm4eic::ReconstructedParticleCollection,
25+
edm4eic::MCRecoParticleAssociationCollection>,
26+
algorithms::Output<edm4eic::TruthinessCollection>>;
27+
#else
1928
using TruthinessAlgorithm = algorithms::Algorithm<
2029
algorithms::Input<edm4hep::MCParticleCollection, edm4eic::ReconstructedParticleCollection,
2130
edm4eic::MCRecoParticleAssociationCollection>,
2231
algorithms::Output<>>;
32+
#endif
2333

2434
class Truthiness : public TruthinessAlgorithm, public WithPodConfig<TruthinessConfig> {
2535

@@ -32,9 +42,14 @@ class Truthiness : public TruthinessAlgorithm, public WithPodConfig<TruthinessCo
3242
: TruthinessAlgorithm{
3343
name,
3444
{"inputMCParticles", "inputReconstructedParticles", "inputAssociations"},
45+
#if __has_include(<edm4eic/Truthiness.h>)
46+
{"outputTruthiness"},
47+
#else
3548
{},
49+
#endif
3650
"Calculate truthiness metric comparing reconstructed particles to MC "
37-
"truth."} {}
51+
"truth."} {
52+
}
3853

3954
void init() final {};
4055
void process(const Input&, const Output&) const final;

src/factories/reco/Truthiness_factory.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <utility>
1010
#include <vector>
1111

12+
#if __has_include(<edm4eic/Truthiness.h>)
13+
#include <edm4eic/TruthinessCollection.h>
14+
#endif
15+
1216
#include "algorithms/reco/Truthiness.h"
1317
#include "extensions/jana/JOmniFactory.h"
1418
#include "services/algorithms_init/AlgorithmsInit_service.h"
@@ -27,6 +31,10 @@ class Truthiness_factory : public JOmniFactory<Truthiness_factory> {
2731
FactoryT::PodioInput<edm4eic::ReconstructedParticle> m_rc_particles_input{this};
2832
FactoryT::PodioInput<edm4eic::MCRecoParticleAssociation> m_associations_input{this};
2933

34+
#if __has_include(<edm4eic/Truthiness.h>)
35+
FactoryT::PodioOutput<edm4eic::Truthiness> m_truthiness_output{this};
36+
#endif
37+
3038
FactoryT::Service<AlgorithmsInit_service> m_algorithmsInit{this};
3139

3240
public:
@@ -37,7 +45,12 @@ class Truthiness_factory : public JOmniFactory<Truthiness_factory> {
3745
}
3846

3947
void Process(int32_t /* run_number */, uint64_t /* event_number */) {
48+
#if __has_include(<edm4eic/Truthiness.h>)
49+
m_algo->process({m_mc_particles_input(), m_rc_particles_input(), m_associations_input()},
50+
{m_truthiness_output().get()});
51+
#else
4052
m_algo->process({m_mc_particles_input(), m_rc_particles_input(), m_associations_input()}, {});
53+
#endif
4154
}
4255
};
4356

src/global/reco/Truthiness_processor.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <map>
1616
#include <string>
1717

18+
#if __has_include(<edm4eic/Truthiness.h>)
19+
#include <edm4eic/TruthinessCollection.h>
20+
#endif
21+
1822
#include "services/log/Log_service.h"
1923

2024
namespace eicrecon {
@@ -64,7 +68,12 @@ void Truthiness_processor::Process(const std::shared_ptr<const JEvent>& event) {
6468

6569
// Call the algorithm
6670
Truthiness::Input input{mc_particles, rc_particles, associations};
71+
#if __has_include(<edm4eic/Truthiness.h>)
72+
auto truthiness_coll = std::make_unique<edm4eic::TruthinessCollection>();
73+
Truthiness::Output output{truthiness_coll.get()};
74+
#else
6775
Truthiness::Output output{};
76+
#endif
6877
m_algo->process(input, output);
6978
}
7079

src/global/reco/reco.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <edm4eic/MCRecoClusterParticleAssociation.h>
1111
#include <edm4eic/MCRecoParticleAssociation.h>
1212
#include <edm4eic/ReconstructedParticle.h>
13+
#if __has_include(<edm4eic/Truthiness.h>)
14+
#include <edm4eic/Truthiness.h>
15+
#endif
1316
#include <edm4hep/MCParticle.h>
1417
#include <fmt/core.h>
1518
#include <map>
@@ -42,6 +45,9 @@
4245
#include "factories/reco/ScatteredElectronsTruth_factory.h"
4346
#include "factories/reco/TrackClusterMatch_factory.h"
4447
#include "factories/reco/TransformBreitFrame_factory.h"
48+
#if __has_include(<edm4eic/Truthiness.h>)
49+
#include "factories/reco/Truthiness_factory.h"
50+
#endif
4551
#include "factories/reco/UndoAfterBurnerMCParticles_factory.h"
4652
#include "global/reco/Truthiness_processor.h"
4753

@@ -269,7 +275,13 @@ void InitPlugin(JApplication* app) {
269275
app->Add(new JOmniFactoryGeneratorT<PrimaryVertices_factory>(
270276
"PrimaryVertices", {"CentralTrackVertices"}, {"PrimaryVertices"}, {}, app));
271277

272-
// Truthiness metric for event quality assessment
278+
#if __has_include(<edm4eic/Truthiness.h>)
279+
app->Add(new JOmniFactoryGeneratorT<Truthiness_factory>(
280+
"Truthiness", {"MCParticles", "ReconstructedParticles", "ReconstructedParticleAssociations"},
281+
{"Truthiness"}, {}, app));
282+
#else
283+
// Include as processor if Truthiness output is not available
273284
app->Add(new Truthiness_processor());
285+
#endif
274286
}
275287
} // extern "C"

0 commit comments

Comments
 (0)