-
Notifications
You must be signed in to change notification settings - Fork 31
1776 implementation of the bic attenuated hits #1783
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
mhkim-anl
wants to merge
86
commits into
main
Choose a base branch
from
1776-implementation-of-the-bic-attenuated-hits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
86 commits
Select commit
Hold shift + click to select a range
19a3507
A new factory for hit attenuation
mhkim-anl 6541250
Plugin for EcalBarrelScFiAttenuatedHits was added.
mhkim-anl 29553f5
"EcalBarrelScFiAttenuatedHits" was added.
mhkim-anl be98559
Algorithm for attenuating hits
mhkim-anl b6375ba
Delete CalorimeterHitAttenuation.cc
mhkim-anl 71471f6
Delete CalorimeterHitAttenuation.h
mhkim-anl 0f602fb
Delete CalorimeterHitAttenuationConfig.h
mhkim-anl 3f17ea4
Algorithm to attenuate hits
mhkim-anl 08e7db3
Updated for PR
mhkim-anl 45da5ec
Updated for PR
mhkim-anl b60dbc1
Updated for PR
mhkim-anl 06198a8
Updated for PR
mhkim-anl fa4edf3
Updated for PR
mhkim-anl 8504e56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9f70d35
Delete CalorimeterHitAttenuation_factory.h
mhkim-anl 7d9adba
Delete JEventProcessorPODIO.cc
mhkim-anl 87a1432
Delete BEMC.cc
mhkim-anl 9f4b3e5
Delete CalorimeterHitAttenuation.cc
mhkim-anl d316e55
Delete CalorimeterHitAttenuation.h
mhkim-anl 06fddd7
Delete CalorimeterHitAttenuationConfig.h
mhkim-anl 94f6e0c
Updated for PR
mhkim-anl df4d149
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a85bbd3
Updated for PR
mhkim-anl 5c7a332
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cf56b38
Updated for PR
mhkim-anl 6586a56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e5bfcb0
Updated for PR
mhkim-anl 54d122c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 81ca730
Add files via upload
mhkim-anl 72fdec7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c1b6e94
Comma was added.
mhkim-anl 21b43fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0a47e5d
TrackClusterMatches was back
mhkim-anl 8b0230e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 43f468a
CaloHitContribution output was added.
mhkim-anl d824cc2
CaloHitContribution output was added.
mhkim-anl deb77ef
CaloHitContribution output was added.
mhkim-anl ce4095f
CaloHitContribution output was added.
mhkim-anl d0c018e
Update src/algorithms/calorimetry/CalorimeterHitAttenuation.h
sly2j 071efd7
Update src/algorithms/calorimetry/CalorimeterHitAttenuation.cc
sly2j 8c24832
Update src/algorithms/calorimetry/CalorimeterHitAttenuation.cc
sly2j 2f241bf
Update src/algorithms/calorimetry/CalorimeterHitAttenuation.cc
sly2j a26579f
Update src/algorithms/calorimetry/CalorimeterHitAttenuation.cc
sly2j 9cf7dd8
Update src/detectors/BEMC/BEMC.cc
sly2j 7a6aecc
Delete src/factories/calorimetry/CalorimeterHitAttenuation_factory.h
mhkim-anl 912f8f9
Delete src/algorithms/calorimetry/CalorimeterHitAttenuation.cc
mhkim-anl e180d9c
Delete src/algorithms/calorimetry/CalorimeterHitAttenuation.h
mhkim-anl e43e358
Delete src/algorithms/calorimetry/CalorimeterHitAttenuationConfig.h
mhkim-anl ebc495e
Updated from CalorimeterHitAttenuation
mhkim-anl c438c9f
Updated from CalorimeterHitAttenuation
mhkim-anl 70f668a
Updated for review
mhkim-anl 3cd901c
CaloHitContribution outputs have been added.
mhkim-anl 46bdc85
Updated for review
mhkim-anl 1e620d1
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl ad762b5
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl 00338aa
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl 5e90b30
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl 1d31a5f
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl ad2563d
Update src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
mhkim-anl 39c9c05
Merge branch 'main' into 1776-implementation-of-the-bic-attenuated-hits
mhkim-anl ebea5e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a7df2d
field name for merging has been changed
mhkim-anl ed2209e
modified the comment to be more generic
mhkim-anl 1801d4b
braces that were removed by mistake were added again
mhkim-anl 5452d9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 29c1097
units were included in the position-related parameters
mhkim-anl 453a5e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90fad93
commented out unused arguments and changed the type to the correct one
mhkim-anl f925a46
changed the variable names to make them have more clear meaning
mhkim-anl cca45ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 993c770
used accmulate algorithm for edepSum and min/max algorithm for timeEar
mhkim-anl 5522f7f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0b5aefe
filled map elements using emplace_back
mhkim-anl a958681
type of the m_id_mask has been changed to std::optional. other minor …
mhkim-anl fd5569e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 56b9436
type of the m_attenuationReferencePosition has been changed to std::o…
mhkim-anl bacd90c
apply iwyu suggestions
mhkim-anl 8436b65
apply iwyu suggestions
mhkim-anl 4e4c971
apply iwyu suggestions
mhkim-anl d5e0281
added a comment on for-loop of the CaloHitContribution
mhkim-anl 63f7f49
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 924c599
added a comment on for-loop of the CaloHitContribution
mhkim-anl 2dfceae
declarations of the attenuation-related parameters and variables were…
mhkim-anl 2cebd20
attenuation is also applied when merging hits is not necessary
mhkim-anl 0f09573
now, this algorithm also includes merging hits for each grid cell
mhkim-anl 498dfe6
getContributions(0) was added for better understanding. attenuation p…
mhkim-anl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
224 changes: 224 additions & 0 deletions
224
src/algorithms/calorimetry/SimCalorimeterHitProcessor.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// Copyright (C) 2025 Minho Kim, Sylvester Joosten, Derek Anderson, Wouter Deconinck | ||
|
||
#include "SimCalorimeterHitProcessor.h" | ||
|
||
#include <DD4hep/Detector.h> | ||
#include <DD4hep/IDDescriptor.h> | ||
#include <DD4hep/Readout.h> | ||
#include <DD4hep/config.h> | ||
#include <DDSegmentation/BitFieldCoder.h> | ||
#include <Evaluator/DD4hepUnits.h> | ||
#include <edm4hep/Vector3f.h> | ||
#include <fmt/core.h> | ||
#include <podio/ObjectID.h> | ||
#include <podio/RelationRange.h> | ||
#include <edm4eic/unit_system.h> | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstddef> | ||
#include <gsl/pointers> | ||
#include <limits> | ||
#include <map> | ||
#include <numeric> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <ranges> | ||
|
||
#include "algorithms/calorimetry/SimCalorimeterHitProcessorConfig.h" | ||
|
||
using namespace dd4hep; | ||
|
||
namespace eicrecon { | ||
|
||
void SimCalorimeterHitProcessor::init() { | ||
|
||
// readout checks | ||
if (m_cfg.readout.empty()) { | ||
error("readoutClass is not provided, it is needed to know the fields in readout ids"); | ||
throw std::runtime_error("readoutClass is not provided"); | ||
} | ||
|
||
// get decoders | ||
try { | ||
m_id_spec = m_geo.detector()->readout(m_cfg.readout).idSpec(); | ||
} catch (...) { | ||
debug("Failed to load ID decoder for {}", m_cfg.readout); | ||
throw std::runtime_error(fmt::format("Failed to load ID decoder for {}", m_cfg.readout)); | ||
} | ||
|
||
// get m_hit_id_mask for adding up hits with the same dimensions that are merged over | ||
if (!m_cfg.hitMergeFields.empty()) { | ||
uint64_t id_inverse_mask = 0; | ||
for (auto& field : m_cfg.hitMergeFields) { | ||
id_inverse_mask |= m_id_spec.field(field)->mask(); | ||
} | ||
m_hit_id_mask = ~id_inverse_mask; | ||
if (m_hit_id_mask) | ||
debug("ID mask in {:s}: {:#064b}", m_cfg.readout, m_hit_id_mask.value()); | ||
} | ||
|
||
// get m_contribution_id_mask for adding up contributions with the same dimensions that are merged over | ||
if (!m_cfg.contributionMergeFields.empty()) { | ||
uint64_t id_inverse_mask = 0; | ||
for (auto& field : m_cfg.contributionMergeFields) { | ||
id_inverse_mask |= m_id_spec.field(field)->mask(); | ||
} | ||
m_contribution_id_mask = ~id_inverse_mask; | ||
if (m_contribution_id_mask) | ||
debug("ID mask in {:s}: {:#064b}", m_cfg.readout, m_contribution_id_mask.value()); | ||
} | ||
|
||
// get reference position for attenuating hits and contributions | ||
if (!m_cfg.attenuationReferencePositionName.empty()) { | ||
m_attenuationReferencePosition_mm = | ||
m_geo.detector()->constant<double>(m_cfg.attenuationReferencePositionName) * | ||
edm4eic::unit::mm / dd4hep::mm; | ||
} | ||
} | ||
|
||
void SimCalorimeterHitProcessor::process(const SimCalorimeterHitProcessor::Input& input, | ||
const SimCalorimeterHitProcessor::Output& output) const { | ||
|
||
const auto [in_hits] = input; | ||
auto [out_hits, out_hit_contribs] = output; | ||
|
||
// map for regrouping | ||
std::map<edm4hep::MCParticle, std::vector<edm4hep::MutableSimCalorimeterHit>> mapMCParToSimCalHit; | ||
|
||
// regroup the sim hits by mc particle | ||
// keep an eye on whether these for-loops grow large enough to affect memory limits | ||
// on the running of reconstruction, especially forward and backward calorimeters | ||
for (const auto& ih : *in_hits) { | ||
for (const auto& contrib : ih.getContributions()) { | ||
edm4hep::MCParticle primary = get_primary(contrib); | ||
|
||
auto& simhit = mapMCParToSimCalHit[primary].emplace_back(ih.getCellID(), contrib.getEnergy(), | ||
ih.getPosition()); | ||
simhit.addToContributions(contrib); | ||
|
||
trace("Identified primary: id = {}, pid = {}, total energy = {}, contributed = {}", | ||
primary.getObjectID().index, primary.getPDG(), primary.getEnergy(), | ||
mapMCParToSimCalHit[primary].back().getEnergy()); | ||
} | ||
} | ||
|
||
// Attenuate energies of the sim hits | ||
// 1. sum the hits if they have the same dimension | ||
// 2. attenuate the summed hits | ||
for (const auto& [par, hits] : mapMCParToSimCalHit) {< 8000 /span> | ||
double attFactor = 1.; | ||
|
||
// when merging hits is necessary | ||
if (m_hit_id_mask) { | ||
// map | ||
std::unordered_map<uint64_t, std::vector<std::size_t>> hit_merge_map; | ||
|
||
// map for merging hits | ||
std::size_t ix = 0; | ||
for (const auto& ahit : hits) { | ||
uint64_t hid = ahit.getCellID() & m_hit_id_mask.value(); | ||
hit_merge_map[hid].push_back(ix); | ||
ix++; | ||
} | ||
|
||
for (const auto& [id_hit, ixs_hit] : hit_merge_map) { | ||
std::unordered_map<uint64_t, std::vector<std::size_t>> contribution_merge_map; | ||
|
||
// map for merging contributions | ||
for (const auto& ix : ixs_hit) { | ||
uint64_t hid = hits[ix].getCellID() & m_contribution_id_mask.value(); | ||
contribution_merge_map[hid].push_back(ix); | ||
} | ||
|
||
auto out_hit = out_hits->create(); | ||
double edepSum = 0; | ||
|
||
for (const auto& [id_contrib, ixs_contrib] : contribution_merge_map) { | ||
auto leading_hit = hits[ixs_contrib[0]]; | ||
auto leading_contrib = hits[ixs_contrib[0]].getContributions(0); | ||
|
||
// add up the energy deposits considering the attenuation | ||
double edepSumLocal = std::accumulate( | ||
ixs_contrib.begin(), ixs_contrib.end(), 0.0, [&hits](double sum, size_t ix) { | ||
return sum + hits[ix].getContributions(0).getEnergy(); | ||
}); | ||
|
||
// attenuation | ||
if (m_attenuationReferencePosition_mm) { | ||
attFactor = get_attenuation(leading_hit.getPosition().z); | ||
edepSumLocal *= attFactor; | ||
} | ||
|
||
edepSum += edepSumLocal; | ||
|
||
// find the earliest time | ||
float timeEarLocal = | ||
std::ranges::min(ixs_contrib | std::views::transform([&hits](size_t ix) { | ||
return hits[ix].getContributions(0).getTime(); | ||
})); | ||
|
||
auto out_hit_contrib = out_hit_contribs->create(); | ||
out_hit_contrib.setPDG(leading_contrib.getPDG()); | ||
out_hit_contrib.setEnergy(static_cast<float>(edepSumLocal)); | ||
out_hit_contrib.setTime(timeEarLocal); | ||
out_hit_contrib.setStepPosition(leading_contrib.getStepPosition()); | ||
out_hit_contrib.setParticle(par); | ||
out_hit.addToContributions(out_hit_contrib); | ||
} | ||
|
||
auto leading_hit = hits[ixs_hit[0]]; | ||
|
||
out_hit.setCellID(leading_hit.getCellID()); | ||
out_hit.setEnergy(static_cast<float>(edepSum)); | ||
out_hit.setPosition(leading_hit.getPosition()); | ||
} | ||
// when merging hits are not necessary | ||
} else { | ||
for (const auto& hit : hits) { | ||
auto contrib = hit.getContributions(0); | ||
|
||
// attenuation | ||
if (m_attenuationReferencePosition_mm) { | ||
attFactor = get_attenuation(hit.getPosition().z); | ||
} | ||
|
||
auto out_hit_contrib = out_hit_contribs->create(); | ||
out_hit_contrib.setPDG(contrib.getPDG()); | ||
out_hit_contrib.setEnergy(attFactor * contrib.getEnergy()); | ||
out_hit_contrib.setTime(contrib.getTime()); | ||
out_hit_contrib.setStepPosition(contrib.getStepPosition()); | ||
out_hit_contrib.setParticle(par); | ||
|
||
auto out_hit = out_hits->create(); | ||
out_hit.setCellID(hit.getCellID()); | ||
out_hit.setEnergy(attFactor * hit.getEnergy()); | ||
out_hit.setPosition(hit.getPosition()); | ||
out_hit.addToContributions(out_hit_contrib); | ||
} | ||
} | ||
} | ||
} | ||
|
||
edm4hep::MCParticle | ||
SimCalorimeterHitProcessor::get_primary(const edm4hep::CaloHitContribution& contrib) const { | ||
const auto contributor = contrib.getParticle(); | ||
|
||
edm4hep::MCParticle primary = contributor; | ||
while (primary.parents_size() > 0) { | ||
if (primary.getGeneratorStatus() != 0) | ||
break; | ||
primary = primary.getParents(0); | ||
} | ||
return primary; | ||
} | ||
|
||
double SimCalorimeterHitProcessor::get_attenuation(double zpos) const { | ||
double length = std::abs(m_attenuationReferencePosition_mm.value() - zpos); | ||
9E81 td> | double factor = m_cfg.attPars[0] * std::exp(-length / m_cfg.attPars[1]) + | |
(1 - m_cfg.attPars[0]) * std::exp(-length / m_cfg.attPars[2]); | ||
return factor; | ||
} | ||
} // namespace eicrecon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// Copyright (C) 2025 Minho Kim, Sylvester Joosten, Derek Anderson, Wouter Deconinck | ||
|
||
#pragma once | ||
|
||
#include <DD4hep/IDDescriptor.h> | ||
#include <algorithms/algorithm.h> | ||
#include <algorithms/geo.h> | ||
#include <edm4hep/CaloHitContributionCollection.h> | ||
#include <edm4hep/MCParticleCollection.h> | ||
#include <edm4hep/SimCalorimeterHitCollection.h> | ||
#include <stdint.h> | ||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include "SimCalorimeterHitProcessorConfig.h" | ||
#include "algorithms/interfaces/WithPodConfig.h" | ||
|
||
namespace eicrecon { | ||
|
||
using SimCalorimeterHitProcessorAlgorithm = | ||
algorithms::Algorithm<algorithms::Input<edm4hep::SimCalorimeterHitCollection>, | ||
algorithms::Output<edm4hep::SimCalorimeterHitCollection, | ||
edm4hep::CaloHitContributionCollection>>; | ||
|
||
class SimCalorimeterHitProcessor : public SimCalorimeterHitProcessorAlgorithm, | ||
public WithPodConfig<SimCalorimeterHitProcessorConfig> { | ||
|
||
public: | ||
SimCalorimeterHitProcessor(std::string_view name) | ||
: SimCalorimeterHitProcessorAlgorithm{ | ||
name, | ||
{"inputHitCollection"}, | ||
{"outputHitCollection", "outputHitContributionCollection"}, | ||
"Regroup the hits by particle, add up the hits if" | ||
"they have the same z-segmentation, and attenuate."} {} | ||
|
||
void init() final; | ||
void process(const Input&, const Output&) const final; | ||
|
||
private: | ||
std::optional<uint64_t> m_hit_id_mask; | ||
std::optional<uint64_t> m_contribution_id_mask; | ||
|
||
dd4hep::IDDescriptor m_id_spec; | ||
|
||
F438 const algorithms::GeoSvc& m_geo = algorithms::GeoSvc::instance(); | ||
|
||
// a reference value for attenuation | ||
std::optional<double> m_attenuationReferencePosition_mm; | ||
|
||
private: | ||
edm4hep::MCParticle get_primary(const edm4hep::CaloHitContribution& contrib) const; | ||
|
||
// attenuation function | ||
double get_attenuation(double zpos) const; | ||
}; | ||
|
||
} // namespace eicrecon |
24 changes: 24 additions & 0 deletions
24
src/algorithms/calorimetry/SimCalorimeterHitProcessorConfig.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// Copyright (C) 2025 Minho Kim, Sylvester Joosten, Derek Anderson, Wouter Deconinck | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace eicrecon { | ||
|
||
struct SimCalorimeterHitProcessorConfig { | ||
|
||
// parameters for attenuation function | ||
std::vector<double> attPars; | ||
|
||
std::string readout{""}; | ||
std::string attenuationReferencePositionName{""}; | ||
// fields for merging hits | ||
std::vector < std::string > hitMergeFields{}; | ||
// fildes for merging contributions | ||
std::vector < std::string > contributionMergeFields{}; | ||
}; | ||
|
||
} // namespace eicrecon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.