Skip to content
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

refactor: Stable RNG for smeared digitization in Examples #3849

Closed
wants to merge 11 commits into from
27 changes: 25 additions & 2 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
measurementParticlesMap.reserve(simHits.size());
measurementSimHitsMap.reserve(simHits.size());

// Setup random number generator
auto rng = m_cfg.randomNumbers->spawnGenerator(ctx);
RandomSeed eventSeed = m_cfg.randomNumbers->generateSeed(ctx);

// Some statistics
std::size_t skippedHits = 0;
Expand All @@ -153,6 +152,10 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
// Thus we need to store the cell data from the simulation.
CellsMap cellsMap;

// Count the number of hits per particle and module. Cleared before each
// module.
std::map<SimBarcode, std::uint32_t> particleOnModuleHitCount;

ACTS_DEBUG("Starting loop over modules ...");
for (const auto& simHitsGroup : groupByModule(simHits)) {
// Manual pair unpacking instead of using
Expand Down Expand Up @@ -182,6 +185,8 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
ACTS_VERBOSE("Digitizer found for module " << moduleGeoId);
}

particleOnModuleHitCount.clear();

// Run the digitizer. Iterate over the hits for this surface inside the
// visitor so we do not need to lookup the variant object per-hit.
std::visit(
Expand All @@ -194,6 +199,24 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
const auto& simHit = *h;
const auto simHitIdx = simHits.index_of(h);

const auto hitIndex =
particleOnModuleHitCount[simHit.particleId()]++;

if (hitIndex > 0) {
ACTS_DEBUG("more than one hit for particle "
<< simHit.particleId() << " on module " << moduleGeoId
<< " - still forwarding it to the digitizer");
}

RandomSeed moduleSeed = (moduleGeoId.value() & 0xFFFFFFFF) ^
((moduleGeoId.value() >> 32) & 0xFFFFFFFF);
RandomSeed particleSeed =
(simHit.particleId().value() & 0xFFFFFFFF) ^
((simHit.particleId().value() >> 32) & 0xFFFFFFFF);
RandomSeed hitSeed =
eventSeed + moduleSeed + particleSeed + hitIndex;
RandomEngine rng(hitSeed);

DigitizedParameters dParameters;

if (simHit.depositedEnergy() < m_cfg.minEnergyDeposit) {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Framework/src/Framework/RandomNumbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RandomEngine RandomNumbers::spawnGenerator(
}

RandomSeed RandomNumbers::generateSeed(const AlgorithmContext& context) const {
return m_cfg.seed + context.eventNumber;
return m_cfg.seed + static_cast<RandomSeed>(context.eventNumber);
}

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(GaussianSmearing) {
if (s.forcePositive[i]) {
ref = std::abs(ref);
}
CHECK_CLOSE_REL(par[i], ref, 0.15);
CHECK_CLOSE_ABS(par[i], ref, 0.15);
}
}

Expand Down
Loading