Skip to content

Commit 5f19d59

Browse files
committed
unified and deduplicated get_primary/lookup_primary
1 parent 6eda068 commit 5f19d59

File tree

6 files changed

+47
-71
lines changed

6 files changed

+47
-71
lines changed

src/algorithms/calorimetry/CalorimeterClusterRecoCoG.cc

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "CalorimeterClusterRecoCoG.h"
3333
#include "algorithms/calorimetry/CalorimeterClusterRecoCoGConfig.h"
34+
#include "MCTools.h"
3435

3536
namespace eicrecon {
3637

@@ -219,7 +220,7 @@ void CalorimeterClusterRecoCoG::associate(
219220
// --------------------------------------------------------------------
220221
// grab primary responsible for contribution & increment relevant sum
221222
// --------------------------------------------------------------------
222-
edm4hep::MCParticle primary = get_primary(contrib);
223+
edm4hep::MCParticle primary = MCTools::lookup_primary(contrib);
223224
mapMCParToContrib[primary] += contrib.getEnergy();
224225

225226
trace("Identified primary: id = {}, pid = {}, total energy = {}, contributed = {}",
@@ -251,22 +252,4 @@ void CalorimeterClusterRecoCoG::associate(
251252
}
252253
}
253254

254-
edm4hep::MCParticle
255-
CalorimeterClusterRecoCoG::get_primary(const edm4hep::CaloHitContribution& contrib) {
256-
// get contributing particle
257-
const auto contributor = contrib.getParticle();
258-
259-
// walk back through parents to find primary
260-
// - TODO finalize primary selection. This
261-
// can be improved!!
262-
edm4hep::MCParticle primary = contributor;
263-
while (primary.parents_size() > 0) {
264-
if (primary.getGeneratorStatus() != 0) {
265-
break;
266-
}
267-
primary = primary.getParents(0);
268-
}
269-
return primary;
270-
}
271-
272255
} // namespace eicrecon

src/algorithms/calorimetry/CalorimeterClusterRecoCoG.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class CalorimeterClusterRecoCoG : public CalorimeterClusterRecoCoGAlgorithm,
8181
void associate(const edm4eic::Cluster& cl,
8282
const edm4eic::MCRecoCalorimeterHitAssociationCollection* mchitassociations,
8383
edm4eic::MCRecoClusterParticleAssociationCollection* assocs) const;
84-
static edm4hep::MCParticle get_primary(const edm4hep::CaloHitContribution& contrib);
8584
};
8685

8786
} // namespace eicrecon

src/algorithms/calorimetry/ImagingClusterReco.cc

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "algorithms/calorimetry/ClusterTypes.h"
3232
#include "algorithms/calorimetry/ImagingClusterRecoConfig.h"
33+
#include "MCTools.h"
3334

3435
namespace eicrecon {
3536

@@ -277,7 +278,7 @@ void ImagingClusterReco::associate_mc_particles(
277278
// --------------------------------------------------------------------
278279
// grab primary responsible for contribution & increment relevant sum
279280
// --------------------------------------------------------------------
280-
edm4hep::MCParticle primary = get_primary(contrib);
281+
edm4hep::MCParticle primary = MCTools::lookup_primary(contrib);
281282
mapMCParToContrib[primary] += contrib.getEnergy();
282283

283284
trace("Identified primary: id = {}, pid = {}, total energy = {}, contributed = {}",
@@ -309,22 +310,4 @@ void ImagingClusterReco::associate_mc_particles(
309310
}
310311
}
311312

312-
edm4hep::MCParticle
313-
ImagingClusterReco::get_primary(const edm4hep::CaloHitContribution& contrib) const {
314-
// get contributing particle
315-
const auto contributor = contrib.getParticle();
316-
317-
// walk back through parents to find primary
318-
// - TODO finalize primary selection. This
319-
// can be improved!!
320-
edm4hep::MCParticle primary = contributor;
321-
while (primary.parents_size() > 0) {
322-
if (primary.getGeneratorStatus() != 0) {
323-
break;
324-
}
325-
primary = primary.getParents(0);
326-
}
327-
return primary;
328-
}
329-
330313
} // namespace eicrecon

src/algorithms/calorimetry/ImagingClusterReco.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ class ImagingClusterReco : public ImagingClusterRecoAlgorithm,
7676
const edm4eic::Cluster& cl,
7777
const edm4eic::MCRecoCalorimeterHitAssociationCollection* mchitassociations,
7878
edm4eic::MCRecoClusterParticleAssociationCollection* assocs) const;
79-
80-
edm4hep::MCParticle get_primary(const edm4hep::CaloHitContribution& contrib) const;
8179
};
8280

8381
} // namespace eicrecon
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
// Copyright (C) 2025 Minho Kim, Sylvester Joosten, Derek Anderson, Wouter Deconinck
3+
#pragma once
4+
5+
//
6+
// @TODO should be migrated to a shared utiliy function in edm4xxx
7+
//
8+
9+
#include <edm4hep/MCParticleCollection.h>
10+
#include <edm4hep/CaloHitContributionCollection.h>
11+
12+
namespace eicrecon::MCTools {
13+
14+
// Lookup primary MCParticle
15+
// we stop looking if we find the parent has status 1
16+
// so we don't merge e.g. radiative photons with the primary electron as this prevents us
17+
// from properly linking the clusters back to the event geometry. Note that we also
18+
// enforce for this case that no steps back towards the primary were taken to avoid
19+
// storing the first pair of calorimetric showers that start inside the tracking volume.
20+
// Hence, this algorithm will return:
21+
// - Contribution came from primary: primary
22+
// - Contribution came from immediate daughter of primary and has no childern -> daughter
23+
// - All other cases (i.e. early showers, multi-radiation): primary
24+
// libraries
25+
inline edm4hep::MCParticle lookup_primary(const edm4hep::CaloHitContribution& contrib) {
26+
const auto contributor = contrib.getParticle();
27+
28+
edm4hep::MCParticle primary = contributor;
29+
size_t steps_taken = 0; // The number of steps taken looking for the primary
30+
while (primary.parents_size() > 0) {
31+
auto parent = primary.getParents(0);
32+
if (primary.getGeneratorStatus() != 0 ||
33+
(parent.getGeneratorStatus() != 0 && steps_taken == 0)) {
34+
break;
35+
}
36+
primary = parent;
37+
steps_taken += 1;
38+
}
39+
return primary;
40+
}
41+
} // namespace eicrecon::MCTools

src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <vector>
2828

2929
#include "algorithms/calorimetry/SimCalorimeterHitProcessorConfig.h"
30+
#include "MCTools.h"
3031

3132
using namespace dd4hep;
3233

@@ -70,35 +71,6 @@ template <> struct hash<std::tuple<edm4hep::MCParticle, uint64_t, int>> {
7071

7172
// unnamed namespace for internal utility
7273
namespace {
73-
// Lookup primary MCParticle
74-
// we stop looking if we find the parent has status 1 but with only 2 daughters
75-
// so we don't merge radiative photons with the primary electron as this prevents us
76-
// from properly linking the clusters back to the event geometry. Note that we also
77-
// enforce for this case that no steps back towards the primary were taken to avoid
78-
// storing the first pair of calorimetric showers that start inside the tracking volume.
79-
// Hence, this algorithm will return:
80-
// - Contribution came from primary: primary
81-
// - Contribution came from immediate daughter of primary and has no childern -> daughter
82-
// - All other cases (i.e. early showers, multi-radiation): primary
83-
//
84-
// @TODO this should be a shared utiliy function in the edm4xxx
85-
// libraries
86-
edm4hep::MCParticle lookup_primary(const edm4hep::CaloHitContribution& contrib) {
87-
const auto contributor = contrib.getParticle();
88-
89-
edm4hep::MCParticle primary = contributor;
90-
size_t steps_taken = 0; // The number of steps taken looking for the primary
91-
while (primary.parents_size() > 0) {
92-
auto parent = primary.getParents(0);
93-
if (primary.getGeneratorStatus() != 0 ||
94-
(parent.getGeneratorStatus() != 0 && parent.daughters_size() == 2 && steps_taken == 0)) {
95-
break;
96-
}
97-
primary = parent;
98-
steps_taken += 1;
99-
}
100-
return primary;
101-
}
10274
class HitContributionAccumulator {
10375
private:
10476
float m_energy{0};
@@ -206,7 +178,7 @@ void SimCalorimeterHitProcessor::process(const SimCalorimeterHitProcessor::Input
206178
m_attenuationReferencePosition ? get_attenuation(ih.getPosition().z) : 1.;
207179
// Use primary particle (traced back through parents) to group contributions
208180
for (const auto& contrib : ih.getContributions()) {
209-
edm4hep::MCParticle primary = lookup_primary(contrib);
181+
edm4hep::MCParticle primary = MCTools::lookup_primary(contrib);
210182
const double propagationTime =
211183
m_attenuationReferencePosition
212184
? std::abs(m_attenuationReferencePosition.value() - ih.getPosition().z) *

0 commit comments

Comments
 (0)