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: Relate measurements to truth in Examples #3838

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -145,6 +140,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 +153,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 = 3.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 Down
71 changes: 31 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,24 @@ 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) {

measurement.allocateHitIndices(simHitsIdxs.size());
measurement.allocateParticleIds(simHitsIdxs.size());
for (auto [i, simHitIdx] : Acts::enumerate(simHitsIdxs)) {
measurement.hitIndices()[i] = simHitIdx;
measurement.particleIds()[i] =
simHits.nth(simHitIdx)->particleId();

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 +300,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
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
Loading