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: Digitization cleanup in Examples #3865

Merged
merged 15 commits into from
Nov 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@

#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/Digitization/DigitizationConfig.hpp"
#include "ActsExamples/Digitization/MeasurementCreation.hpp"
#include "ActsExamples/Digitization/SmearingConfig.hpp"
#include "ActsExamples/EventData/Cluster.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"
Expand All @@ -29,17 +26,10 @@
#include "ActsFatras/Digitization/UncorrelatedHitSmearer.hpp"

#include <cstddef>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>

namespace ActsFatras {
class Barcode;
} // namespace ActsFatras

namespace ActsExamples {
struct AlgorithmContext;

Expand Down Expand Up @@ -98,17 +88,17 @@ class DigitizationAlgorithm final : public IAlgorithm {
using CellsMap =
std::map<Acts::GeometryIdentifier, std::vector<Cluster::Cell>>;

ReadDataHandle<SimHitContainer> m_simContainerReadHandle{this,
"SimHitContainer"};

WriteDataHandle<MeasurementContainer> m_measurementWriteHandle{
this, "Measurements"};
WriteDataHandle<CellsMap> m_cellsWriteHandle{this, "Cells"};
WriteDataHandle<ClusterContainer> m_clusterWriteHandle{this, "Clusters"};
WriteDataHandle<IndexMultimap<ActsFatras::Barcode>>
m_measurementParticlesMapWriteHandle{this, "MeasurementParticlesMap"};
WriteDataHandle<IndexMultimap<Index>> m_measurementSimHitsMapWriteHandle{
this, "MeasurementSimHitsMap"};
ReadDataHandle<SimHitContainer> m_inputHits{this, "InputHits"};

WriteDataHandle<MeasurementContainer> m_outputMeasurements{
this, "OutputMeasurements"};
WriteDataHandle<CellsMap> m_outputCells{this, "OutputCells"};
WriteDataHandle<ClusterContainer> m_outputClusters{this, "OutputClusters"};

WriteDataHandle<IndexMultimap<SimBarcode>> m_outputMeasurementParticlesMap{
this, "OutputMeasurementParticlesMap"};
WriteDataHandle<IndexMultimap<Index>> m_outputMeasurementSimHitsMap{
this, "OutputMeasurementSimHitsMap"};

/// Construct a fixed-size smearer from a configuration.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Utilities/BinUtility.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/Result.hpp"
#include "ActsExamples/Digitization/DigitizationConfig.hpp"
#include "ActsExamples/Digitization/Smearers.hpp"
#include "ActsExamples/Digitization/SmearingConfig.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsFatras/Digitization/UncorrelatedHitSmearer.hpp"

#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -121,17 +116,15 @@ struct DigiComponentsConfig {

class DigitizationConfig {
andiwand marked this conversation as resolved.
Show resolved Hide resolved
public:
DigitizationConfig(bool merge, double sigma, bool commonCorner)
: DigitizationConfig(merge, sigma, commonCorner,
Acts::GeometryHierarchyMap<DigiComponentsConfig>()) {
}
DigitizationConfig();
andiwand marked this conversation as resolved.
Show resolved Hide resolved

DigitizationConfig(bool merge, double sigma, bool commonCorner);

DigitizationConfig(
bool doMerge, double mergeNsigma, bool mergeCommonCorner,
Acts::GeometryHierarchyMap<DigiComponentsConfig> &&digiCfgs);
DigitizationConfig(bool doMerge, double mergeNsigma, bool mergeCommonCorner,
Acts::GeometryHierarchyMap<DigiComponentsConfig> digiCfgs);

explicit DigitizationConfig(
Acts::GeometryHierarchyMap<DigiComponentsConfig> &&digiCfgs);
Acts::GeometryHierarchyMap<DigiComponentsConfig> digiCfgs);

/// Input collection of simulated hits.
std::string inputSimHits = "simhits";
Expand All @@ -145,6 +138,7 @@ class DigitizationConfig {
std::string outputMeasurementParticlesMap = "measurement_particles_map";
/// Output collection to map measured hits to simulated hits.
std::string outputMeasurementSimHitsMap = "measurement_simhits_map";

/// Map of surface by identifier to allow local - to global
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface *>
surfaceByIdentifier;
Expand All @@ -157,11 +151,11 @@ class DigitizationConfig {
/// clusters, measurements, and sim-hit-maps are output.
bool doClusterization = true;
/// Do we merge hits or not
bool doMerge;
bool doMerge = false;
/// How close do parameters have to be to consider merged
const double mergeNsigma;
double mergeNsigma = 1.0;
/// Consider clusters that share a corner as merged (8-cell connectivity)
const bool mergeCommonCorner;
bool mergeCommonCorner = false;
/// Energy deposit threshold for accepting a hit
/// For a generic readout frontend we assume 1000 e/h pairs, in Si each
/// e/h-pair requiers on average an energy of 3.65 eV (PDG review 2023,
Expand All @@ -175,4 +169,5 @@ class DigitizationConfig {
std::pair<Acts::GeometryIdentifier, std::vector<Acts::BoundIndices>>>
andiwand marked this conversation as resolved.
Show resolved Hide resolved
getBoundIndices() const;
};

} // namespace ActsExamples
65 changes: 25 additions & 40 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,15 @@
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"
#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/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Utilities/GroupBy.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 @@ -69,23 +57,22 @@ ActsExamples::DigitizationAlgorithm::DigitizationAlgorithm(
"Missing hit-to-simulated-hits map output collection");
}

m_measurementWriteHandle.initialize(m_cfg.outputMeasurements);
m_clusterWriteHandle.initialize(m_cfg.outputClusters);
m_measurementParticlesMapWriteHandle.initialize(
m_outputMeasurements.initialize(m_cfg.outputMeasurements);
m_outputClusters.initialize(m_cfg.outputClusters);
m_outputMeasurementParticlesMap.initialize(
m_cfg.outputMeasurementParticlesMap);
m_measurementSimHitsMapWriteHandle.initialize(
m_cfg.outputMeasurementSimHitsMap);
m_outputMeasurementSimHitsMap.initialize(m_cfg.outputMeasurementSimHitsMap);
}

if (m_cfg.doOutputCells) {
if (m_cfg.outputCells.empty()) {
throw std::invalid_argument("Missing cell output collection");
}

m_cellsWriteHandle.initialize(m_cfg.outputCells);
m_outputCells.initialize(m_cfg.outputCells);
}

m_simContainerReadHandle.initialize(m_cfg.inputSimHits);
m_inputHits.initialize(m_cfg.inputSimHits);

// Create the digitizers from the configuration
std::vector<std::pair<Acts::GeometryIdentifier, Digitizer>> digitizerInput;
Expand Down Expand Up @@ -142,13 +129,14 @@ ActsExamples::DigitizationAlgorithm::DigitizationAlgorithm(
ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
const AlgorithmContext& ctx) const {
// Retrieve input
const auto& simHits = m_simContainerReadHandle(ctx);
const auto& simHits = m_inputHits(ctx);
ACTS_DEBUG("Loaded " << simHits.size() << " sim hits");

// Prepare output containers
// need list here for stable addresses
MeasurementContainer measurements;
ClusterContainer clusters;

IndexMultimap<ActsFatras::Barcode> measurementParticlesMap;
IndexMultimap<Index> measurementSimHitsMap;
measurements.reserve(simHits.size());
Expand Down Expand Up @@ -271,7 +259,7 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
// Store the cell data into a map.
if (m_cfg.doOutputCells) {
std::vector<Cluster::Cell> cells;
for (const auto& [dParameters, simhits] :
for (const auto& [dParameters, simHitsIdxs] :
digitizeParametersResult) {
for (const auto& cell : dParameters.cluster.channels) {
cells.push_back(cell);
Expand All @@ -281,21 +269,18 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
}

if (m_cfg.doClusterization) {
for (auto& [dParameters, simhits] : digitizeParametersResult) {
// The measurement container is unordered and the index under
// which the measurement will be stored is known before adding it.
Index measurementIdx = measurements.size();

createMeasurement(measurements, moduleGeoId, dParameters);
for (auto& [dParameters, simHitsIdxs] : digitizeParametersResult) {
auto measurement =
createMeasurement(measurements, moduleGeoId, dParameters);
clusters.emplace_back(std::move(dParameters.cluster));
// this digitization does hit merging so there can be more than
// one mapping entry for each digitized hit.
for (auto simHitIdx : simhits) {

for (auto [i, simHitIdx] : Acts::enumerate(simHitsIdxs)) {
measurementParticlesMap.emplace_hint(
measurementParticlesMap.end(), measurementIdx,
measurementParticlesMap.end(), measurement.index(),
simHits.nth(simHitIdx)->particleId());
measurementSimHitsMap.emplace_hint(measurementSimHitsMap.end(),
measurementIdx, simHitIdx);
measurement.index(),
simHitIdx);
}
}
}
Expand All @@ -309,16 +294,16 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
<< " skipped in Digitization. Enable DEBUG mode to see more details.");
}

if (m_cfg.doOutputCells) {
m_cellsWriteHandle(ctx, std::move(cellsMap));
if (m_cfg.doClusterization) {
m_outputMeasurements(ctx, std::move(measurements));
m_outputClusters(ctx, std::move(clusters));

m_outputMeasurementParticlesMap(ctx, std::move(measurementParticlesMap));
m_outputMeasurementSimHitsMap(ctx, std::move(measurementSimHitsMap));
}

if (m_cfg.doClusterization) {
m_measurementWriteHandle(ctx, std::move(measurements));
m_clusterWriteHandle(ctx, std::move(clusters));
m_measurementParticlesMapWriteHandle(ctx,
std::move(measurementParticlesMap));
m_measurementSimHitsMapWriteHandle(ctx, std::move(measurementSimHitsMap));
if (m_cfg.doOutputCells) {
m_outputCells(ctx, std::move(cellsMap));
}

return ProcessCode::SUCCESS;
Expand Down
40 changes: 19 additions & 21 deletions Examples/Algorithms/Digitization/src/DigitizationConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,30 @@
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "ActsExamples/Digitization/SmearingConfig.hpp"

namespace {
namespace ActsExamples {

enum SmearingTypes : int {
eGauss = 0,
eGaussTruncated = 1,
eGaussClipped = 2,
eUniform = 3,
eDigital = 4,
};
DigitizationConfig::DigitizationConfig() = default;

} // namespace
DigitizationConfig::DigitizationConfig(bool merge, double sigma,
bool commonCorner)
: DigitizationConfig(merge, sigma, commonCorner,
Acts::GeometryHierarchyMap<DigiComponentsConfig>()) {}

ActsExamples::DigitizationConfig::DigitizationConfig(
DigitizationConfig::DigitizationConfig(
bool merge, double sigma, bool commonCorner,
Acts::GeometryHierarchyMap<DigiComponentsConfig>&& digiCfgs)
: doMerge(merge), mergeNsigma(sigma), mergeCommonCorner(commonCorner) {
digitizationConfigs = std::move(digiCfgs);
}
Acts::GeometryHierarchyMap<DigiComponentsConfig> digiCfgs)
: doMerge(merge),
mergeNsigma(sigma),
mergeCommonCorner(commonCorner),
digitizationConfigs(std::move(digiCfgs)) {}

ActsExamples::DigitizationConfig::DigitizationConfig(
Acts::GeometryHierarchyMap<DigiComponentsConfig>&& digiCfgs)
: doMerge(false), mergeNsigma(1.0), mergeCommonCorner(false) {
digitizationConfigs = std::move(digiCfgs);
}
DigitizationConfig::DigitizationConfig(
Acts::GeometryHierarchyMap<DigiComponentsConfig> digiCfgs)
: digitizationConfigs(std::move(digiCfgs)) {}

std::vector<
std::pair<Acts::GeometryIdentifier, std::vector<Acts::BoundIndices>>>
ActsExamples::DigitizationConfig::getBoundIndices() const {
DigitizationConfig::getBoundIndices() const {
std::vector<
std::pair<Acts::GeometryIdentifier, std::vector<Acts::BoundIndices>>>
bIndexInput;
Expand All @@ -60,7 +56,7 @@ ActsExamples::DigitizationConfig::getBoundIndices() const {
return bIndexInput;
}

std::vector<Acts::ActsScalar> ActsExamples::GeometricConfig::variances(
std::vector<Acts::ActsScalar> GeometricConfig::variances(
const std::array<std::size_t, 2u>& csizes,
const std::array<std::size_t, 2u>& cmins) const {
std::vector<Acts::ActsScalar> rVariances;
Expand All @@ -80,3 +76,5 @@ std::vector<Acts::ActsScalar> ActsExamples::GeometricConfig::variances(
}
return rVariances;
}

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "ActsFatras/Digitization/Segmentizer.hpp"

#include <numeric>
#include <optional>
#include <vector>

namespace ActsExamples {
Expand Down
Loading