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
30 changes: 22 additions & 8 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/BinUtility.hpp"
#include "Acts/Utilities/Result.hpp"
#include "ActsExamples/Digitization/ModuleClusters.hpp"
#include "ActsExamples/EventData/GeometryContainers.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Utilities/GroupBy.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsExamples/Utilities/Range.hpp"
#include "ActsFatras/EventData/Barcode.hpp"
#include "ActsFatras/EventData/Hit.hpp"

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <limits>
#include <ostream>
#include <set>
#include <stdexcept>
#include <string>
#include <utility>
Expand Down Expand Up @@ -155,8 +151,7 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
measurementParticlesMap.reserve(simHits.size());
measurementSimHitsMap.reserve(simHits.size());

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

// Some statistics
std::size_t skippedHits = 0;
Expand All @@ -165,6 +160,10 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
// 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, int> particleOnModuleHitCount;

ACTS_DEBUG("Starting loop over modules ...");
for (const auto& simHitsGroup : groupByModule(simHits)) {
// Manual pair unpacking instead of using
Expand Down Expand Up @@ -194,6 +193,8 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
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 @@ -206,6 +207,19 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
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");
}

auto hitSeed = eventSeed + moduleGeoId.value() +
andiwand marked this conversation as resolved.
Show resolved Hide resolved
simHit.particleId().value() + hitIndex;
RandomEngine rng(hitSeed);

DigitizedParameters dParameters;

if (simHit.depositedEnergy() < m_cfg.minEnergyDeposit) {
Expand Down
Loading