Skip to content

Commit

Permalink
refactor: Examples sequence elements gain Read/WriteDataHandles (#1908)
Browse files Browse the repository at this point in the history
This locks in the type at algorithm construction, registers the handles and allows checking the data flow more thoroughly before running the event sequence. 

This PR only changes the components used in the ODD chain example, but we should probably extend this to the rest of the Examples components as well.

Blocked by:
- #1901
  • Loading branch information
paulgessinger authored Mar 10, 2023
1 parent e76e89b commit a235c2c
Show file tree
Hide file tree
Showing 40 changed files with 585 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ActsExamples/EventData/Cluster.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsFatras/Digitization/Channelizer.hpp"
Expand Down Expand Up @@ -104,6 +105,19 @@ class DigitizationAlgorithm final : public IAlgorithm {
ActsFatras::PlanarSurfaceMask m_surfaceMask;
ActsFatras::Channelizer m_channelizer;

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

WriteDataHandle<IndexSourceLinkContainer> m_sourceLinkWriteHandle{
this, "SourceLinks"};
WriteDataHandle<MeasurementContainer> m_measurementWriteHandle{
this, "Measurements"};
WriteDataHandle<ClusterContainer> m_clusterWriteHandle{this, "Clusters"};
WriteDataHandle<IndexMultimap<ActsFatras::Barcode>>
m_measurementParticlesMapWriteHandle{this, "MeasurementParticlesMap"};
WriteDataHandle<IndexMultimap<Index>> m_measurementSimHitsMapWriteHandle{
this, "MeasurementSimHitsMap"};

/// Construct a fixed-size smearer from a configuration.
///
/// It's templated on the smearing dimention given by @tparam kSmearDIM
Expand Down
27 changes: 17 additions & 10 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ ActsExamples::DigitizationAlgorithm::DigitizationAlgorithm(
throw std::invalid_argument("Missing digitization configuration");
}

m_simContainerReadHandle.initialize(m_cfg.inputSimHits);
m_sourceLinkWriteHandle.initialize(m_cfg.outputSourceLinks);
m_measurementWriteHandle.initialize(m_cfg.outputMeasurements);
m_clusterWriteHandle.initialize(m_cfg.outputClusters);
m_measurementParticlesMapWriteHandle.initialize(
m_cfg.outputMeasurementParticlesMap);
m_measurementSimHitsMapWriteHandle.initialize(
m_cfg.outputMeasurementSimHitsMap);

// Create the digitizers from the configuration
std::vector<std::pair<Acts::GeometryIdentifier, Digitizer>> digitizerInput;

Expand Down Expand Up @@ -112,7 +121,7 @@ ActsExamples::DigitizationAlgorithm::DigitizationAlgorithm(
ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
const AlgorithmContext& ctx) const {
// Retrieve input
const auto& simHits = ctx.eventStore.get<SimHitContainer>(m_cfg.inputSimHits);
const auto& simHits = m_simContainerReadHandle(ctx);
ACTS_DEBUG("Loaded " << simHits.size() << " sim hits");

// Prepare output containers
Expand Down Expand Up @@ -152,10 +161,10 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(

auto digitizerItr = m_digitizers.find(moduleGeoId);
if (digitizerItr == m_digitizers.end()) {
ACTS_DEBUG("No digitizer present for module " << moduleGeoId);
ACTS_VERBOSE("No digitizer present for module " << moduleGeoId);
continue;
} else {
ACTS_DEBUG("Digitizer found for module " << moduleGeoId);
ACTS_VERBOSE("Digitizer found for module " << moduleGeoId);
}

// Run the digitizer. Iterate over the hits for this surface inside the
Expand Down Expand Up @@ -248,13 +257,11 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
*digitizerItr);
}

ctx.eventStore.add(m_cfg.outputSourceLinks, std::move(sourceLinks));
ctx.eventStore.add(m_cfg.outputMeasurements, std::move(measurements));
ctx.eventStore.add(m_cfg.outputClusters, std::move(clusters));
ctx.eventStore.add(m_cfg.outputMeasurementParticlesMap,
std::move(measurementParticlesMap));
ctx.eventStore.add(m_cfg.outputMeasurementSimHitsMap,
std::move(measurementSimHitsMap));
m_sourceLinkWriteHandle(ctx, std::move(sourceLinks));
m_measurementWriteHandle(ctx, std::move(measurements));
m_clusterWriteHandle(ctx, std::move(clusters));
m_measurementParticlesMapWriteHandle(ctx, std::move(measurementParticlesMap));
m_measurementSimHitsMapWriteHandle(ctx, std::move(measurementSimHitsMap));
return ProcessCode::SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include "Acts/Definitions/Units.hpp"
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsExamples/MagneticField/MagneticField.hpp"
Expand Down Expand Up @@ -87,6 +90,15 @@ class FatrasSimulation final : public IAlgorithm {
/// Const access to the config
const Config& config() const { return m_cfg; }

ReadDataHandle<SimParticleContainer> m_inputParticles{this, "InputParticles"};

WriteDataHandle<SimHitContainer> m_outputSimHits{this, "OutputSimHits"};

WriteDataHandle<SimParticleContainer> m_outputParticlesInitial{
this, "OutputParticlesInitial"};
WriteDataHandle<SimParticleContainer> m_outputParticlesFinal{
this, "OutputParticlesFinal"};

private:
Config m_cfg;
std::unique_ptr<detail::FatrasSimulation> m_sim;
Expand Down
15 changes: 10 additions & 5 deletions Examples/Algorithms/Fatras/src/FatrasSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ ActsExamples::FatrasSimulation::FatrasSimulation(Config cfg,

// construct the simulation for the specific magnetic field
m_sim = std::make_unique<FatrasSimulationT>(m_cfg, lvl);

m_inputParticles.initialize(m_cfg.inputParticles);
m_outputParticlesInitial.initialize(m_cfg.outputParticlesInitial);
m_outputParticlesFinal.initialize(m_cfg.outputParticlesFinal);
m_outputSimHits.initialize(m_cfg.outputSimHits);
}

// explicit destructor needed for the PIMPL implementation to work
Expand All @@ -191,8 +196,7 @@ ActsExamples::FatrasSimulation::~FatrasSimulation() = default;
ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(
const AlgorithmContext &ctx) const {
// read input containers
const auto &inputParticles =
ctx.eventStore.get<SimParticleContainer>(m_cfg.inputParticles);
const auto &inputParticles = m_inputParticles(ctx);

ACTS_DEBUG(inputParticles.size() << " input particles");

Expand Down Expand Up @@ -242,9 +246,10 @@ ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(
particlesFinalUnordered.end());
simHits.insert(simHitsUnordered.begin(), simHitsUnordered.end());
// store ordered output containers
ctx.eventStore.add(m_cfg.outputParticlesInitial, std::move(particlesInitial));
ctx.eventStore.add(m_cfg.outputParticlesFinal, std::move(particlesFinal));
ctx.eventStore.add(m_cfg.outputSimHits, std::move(simHits));

m_outputParticlesInitial(ctx, std::move(particlesInitial));
m_outputParticlesFinal(ctx, std::move(particlesFinal));
m_outputSimHits(ctx, std::move(simHits));

return ActsExamples::ProcessCode::SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ ActsExamples::EventGenerator::EventGenerator(const Config& cfg,
if (!m_cfg.randomNumbers) {
throw std::invalid_argument("Missing random numbers service");
}

m_outputParticles.initialize(m_cfg.outputParticles);
}

std::string ActsExamples::EventGenerator::name() const {
Expand Down Expand Up @@ -88,6 +90,6 @@ ActsExamples::ProcessCode ActsExamples::EventGenerator::read(
<< " n_particles=" << particles.size());

// move generated event to the store
ctx.eventStore.add(m_cfg.outputParticles, std::move(particles));
m_outputParticles(ctx, std::move(particles));
return ProcessCode::SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IReader.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"

Expand Down Expand Up @@ -113,6 +114,9 @@ class EventGenerator final : public ActsExamples::IReader {

Config m_cfg;
std::unique_ptr<const Acts::Logger> m_logger;

WriteDataHandle<SimParticleContainer> m_outputParticles{this,
"OutputParticles"};
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
#include "Acts/Seeding/SeedFinder.hpp"
#include "Acts/Seeding/SeedFinderConfig.hpp"
#include "Acts/Seeding/SpacePointGrid.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/SimSeed.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"

Expand Down Expand Up @@ -73,6 +76,12 @@ class SeedingAlgorithm final : public IAlgorithm {
std::shared_ptr<const Acts::BinFinder<SimSpacePoint>> m_bottomBinFinder;
std::shared_ptr<const Acts::BinFinder<SimSpacePoint>> m_topBinFinder;
Config m_cfg;

std::vector<std::unique_ptr<ReadDataHandle<SimSpacePointContainer>>>
m_inputSpacePoints{};

WriteDataHandle<SimSeedContainer> m_outputSeeds{this, "OutputSeeds"};
WriteDataHandle<ProtoTrackContainer> m_outputProtoTracks{this, "ProtoTracks"};
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/SpacePointFormation/SpacePointBuilder.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"

#include <memory>
Expand Down Expand Up @@ -78,5 +80,14 @@ class SpacePointMaker final : public IAlgorithm {
Config m_cfg;

Acts::SpacePointBuilder<SimSpacePoint> m_spacePointBuilder;

ReadDataHandle<IndexSourceLinkContainer> m_inputSourceLinks{
this, "InputSourceLinks"};

ReadDataHandle<MeasurementContainer> m_inputMeasurements{this,
"InputMeasurements"};

WriteDataHandle<SimSpacePointContainer> m_outputSpacePoints{
this, "OutputSpacePoints"};
};
} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"
#include "ActsExamples/MagneticField/MagneticField.hpp"
Expand Down Expand Up @@ -102,6 +103,16 @@ class TrackFindingAlgorithm final : public IAlgorithm {
private:
Config m_cfg;

ReadDataHandle<MeasurementContainer> m_inputMeasurements{this,
"InputMeasurements"};
ReadDataHandle<IndexSourceLinkContainer> m_inputSourceLinks{
this, "InputSourceLinks"};

ReadDataHandle<TrackParametersContainer> m_inputInitialTrackParameters{
this, "InputInitialTrackParameters"};

WriteDataHandle<ConstTrackContainer> m_outputTracks{this, "OutputTracks"};

mutable std::atomic<size_t> m_nTotalSeeds{0};
mutable std::atomic<size_t> m_nFailedSeeds{0};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "Acts/MagneticField/InterpolatedBFieldMap.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/SimSeed.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/MagneticField/MagneticField.hpp"

Expand Down Expand Up @@ -89,6 +91,11 @@ class TrackParamsEstimationAlgorithm final : public IAlgorithm {
/// The track parameters covariance (assumed to be the same for all estimated
/// track parameters for the moment)
Acts::BoundSymMatrix m_covariance = Acts::BoundSymMatrix::Zero();

ReadDataHandle<SimSeedContainer> m_inputSeeds{this, "InputSeeds"};

WriteDataHandle<TrackParametersContainer> m_outputTrackParameters{
this, "OutputTrackParameters"};
};

} // namespace ActsExamples
24 changes: 17 additions & 7 deletions Examples/Algorithms/TrackFinding/src/SeedingAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,26 @@ ActsExamples::SeedingAlgorithm::SeedingAlgorithm(
if (m_cfg.inputSpacePoints.empty()) {
throw std::invalid_argument("Missing space point input collections");
}
for (const auto& i : m_cfg.inputSpacePoints) {
if (i.empty()) {

size_t isp = 0;
for (const auto& spName : m_cfg.inputSpacePoints) {
if (spName.empty()) {
throw std::invalid_argument("Invalid space point input collection");
}

auto& handle = m_inputSpacePoints.emplace_back(
std::make_unique<ReadDataHandle<SimSpacePointContainer>>(
this, "InputSpacePoints#" + std::to_string(isp)));
handle->initialize(spName);

isp++;
}
if (m_cfg.outputSeeds.empty()) {
throw std::invalid_argument("Missing seeds output collection");
}

m_outputSeeds.initialize(m_cfg.outputSeeds);

if (m_cfg.gridConfig.rMax != m_cfg.seedFinderConfig.rMax and
m_cfg.allowSeparateRMax == false) {
throw std::invalid_argument(
Expand Down Expand Up @@ -191,15 +202,14 @@ ActsExamples::ProcessCode ActsExamples::SeedingAlgorithm::execute(
// configured input sources.
// pre-compute the total size required so we only need to allocate once
size_t nSpacePoints = 0;
for (const auto& isp : m_cfg.inputSpacePoints) {
nSpacePoints += ctx.eventStore.get<SimSpacePointContainer>(isp).size();
for (const auto& isp : m_inputSpacePoints) {
nSpacePoints += (*isp)(ctx).size();
}

std::vector<const SimSpacePoint*> spacePointPtrs;
spacePointPtrs.reserve(nSpacePoints);
for (const auto& isp : m_cfg.inputSpacePoints) {
for (const auto& spacePoint :
ctx.eventStore.get<SimSpacePointContainer>(isp)) {
for (const auto& isp : m_inputSpacePoints) {
for (const auto& spacePoint : (*isp)(ctx)) {
// since the event store owns the space points, their pointers should be
// stable and we do not need to create local copies.
spacePointPtrs.push_back(&spacePoint);
Expand Down
13 changes: 8 additions & 5 deletions Examples/Algorithms/TrackFinding/src/SpacePointMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ ActsExamples::SpacePointMaker::SpacePointMaker(Config cfg,
if (m_cfg.geometrySelection.empty()) {
throw std::invalid_argument("Missing space point maker geometry selection");
}

m_inputSourceLinks.initialize(m_cfg.inputSourceLinks);
m_inputMeasurements.initialize(m_cfg.inputMeasurements);
m_outputSpacePoints.initialize(m_cfg.outputSpacePoints);

// ensure geometry selection contains only valid inputs
for (const auto& geoId : m_cfg.geometrySelection) {
if ((geoId.approach() != 0u) or (geoId.boundary() != 0u) or
Expand Down Expand Up @@ -98,10 +103,8 @@ ActsExamples::SpacePointMaker::SpacePointMaker(Config cfg,

ActsExamples::ProcessCode ActsExamples::SpacePointMaker::execute(
const AlgorithmContext& ctx) const {
const auto& sourceLinks =
ctx.eventStore.get<IndexSourceLinkContainer>(m_cfg.inputSourceLinks);
const auto& measurements =
ctx.eventStore.get<MeasurementContainer>(m_cfg.inputMeasurements);
const auto& sourceLinks = m_inputSourceLinks(ctx);
const auto& measurements = m_inputMeasurements(ctx);

// TODO Support strip measurements
Acts::SpacePointBuilderOptions spOpt;
Expand All @@ -126,7 +129,7 @@ ActsExamples::ProcessCode ActsExamples::SpacePointMaker::execute(
spacePoints.shrink_to_fit();

ACTS_DEBUG("Created " << spacePoints.size() << " space points");
ctx.eventStore.add(m_cfg.outputSpacePoints, std::move(spacePoints));
m_outputSpacePoints(ctx, std::move(spacePoints));

return ActsExamples::ProcessCode::SUCCESS;
}
16 changes: 9 additions & 7 deletions Examples/Algorithms/TrackFinding/src/TrackFindingAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ ActsExamples::TrackFindingAlgorithm::TrackFindingAlgorithm(
if (m_cfg.outputTracks.empty()) {
throw std::invalid_argument("Missing tracks output collection");
}

m_inputMeasurements.initialize(m_cfg.inputMeasurements);
m_inputSourceLinks.initialize(m_cfg.inputSourceLinks);
m_inputInitialTrackParameters.initialize(m_cfg.inputInitialTrackParameters);
m_outputTracks.initialize(m_cfg.outputTracks);
}

ActsExamples::ProcessCode ActsExamples::TrackFindingAlgorithm::execute(
const ActsExamples::AlgorithmContext& ctx) const {
// Read input data
const auto& measurements =
ctx.eventStore.get<MeasurementContainer>(m_cfg.inputMeasurements);
const auto& sourceLinks =
ctx.eventStore.get<IndexSourceLinkContainer>(m_cfg.inputSourceLinks);
const auto& initialParameters = ctx.eventStore.get<TrackParametersContainer>(
m_cfg.inputInitialTrackParameters);
const auto& measurements = m_inputMeasurements(ctx);
const auto& sourceLinks = m_inputSourceLinks(ctx);
const auto& initialParameters = m_inputInitialTrackParameters(ctx);

// Construct a perigee surface as the target surface
auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
Expand Down Expand Up @@ -144,7 +146,7 @@ ActsExamples::ProcessCode ActsExamples::TrackFindingAlgorithm::execute(
ConstTrackContainer constTracks{constTrackContainer,
constTrackStateContainer};

ctx.eventStore.add(m_cfg.outputTracks, std::move(constTracks));
m_outputTracks(ctx, std::move(constTracks));
return ActsExamples::ProcessCode::SUCCESS;
}

Expand Down
Loading

0 comments on commit a235c2c

Please sign in to comment.