Skip to content

Commit

Permalink
refactor: Create particle/simhit to measurement maps in `Digitization…
Browse files Browse the repository at this point in the history
…Algorithm` in Examples (#3944)

We have a lot of cases where we need particle/simhit -> measurement relations but on the whiteboard we only have the opposite. I believe it makes more sense to store the relations in both directions on the whiteboard instead of inverting in the downstream algorithms.

I use this new map in a couple of occasions and cleaned out wrongly defined typedefs apart from my usual Examples cleanup.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit


- **New Features**
	- Added new output parameters for the digitization algorithm: `outputParticleMeasurementsMap` and `outputSimHitMeasurementsMap`.
- **Bug Fixes**
	- Enhanced error handling for configuration parameters related to output collections in the digitization algorithm.
- **Documentation**
	- Updated comments and variable names for clarity, particularly around measurements versus hits.
- **Refactor**
	- Simplified class inheritance and updated data types across several components to improve consistency and clarity.
- **Chores**
	- Removed unnecessary header files and type aliases to streamline the codebase.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
andiwand authored Dec 5, 2024
1 parent 0d5586e commit 5c41085
Show file tree
Hide file tree
Showing 26 changed files with 183 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class DigitizationAlgorithm final : public IAlgorithm {
std::string outputMeasurementParticlesMap = "measurement_particles_map";
/// Output collection to map measured hits to simulated hits.
std::string outputMeasurementSimHitsMap = "measurement_simhits_map";
/// Output collection to map particles to measurements.
std::string outputParticleMeasurementsMap = "particle_measurements_map";
/// Output collection to map particles to simulated hits.
std::string outputSimHitMeasurementsMap = "simhit_measurements_map";

/// Map of surface by identifier to allow local - to global
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface*>
Expand Down Expand Up @@ -140,6 +144,11 @@ class DigitizationAlgorithm final : public IAlgorithm {
WriteDataHandle<IndexMultimap<Index>> m_outputMeasurementSimHitsMap{
this, "OutputMeasurementSimHitsMap"};

WriteDataHandle<InverseMultimap<SimBarcode>> m_outputParticleMeasurementsMap{
this, "OutputParticleMeasurementsMap"};
WriteDataHandle<InverseMultimap<Index>> m_outputSimHitMeasurementsMap{
this, "OutputSimHitMeasurementsMap"};

/// Construct a fixed-size smearer from a configuration.
///
/// It's templated on the smearing dimension given by @tparam kSmearDIM
Expand Down
22 changes: 18 additions & 4 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
#include "ActsExamples/EventData/GeometryContainers.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Utilities/Range.hpp"
#include "ActsFatras/EventData/Barcode.hpp"

#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <ostream>
#include <stdexcept>
Expand Down Expand Up @@ -61,12 +58,23 @@ DigitizationAlgorithm::DigitizationAlgorithm(Config config,
throw std::invalid_argument(
"Missing hit-to-simulated-hits map output collection");
}
if (m_cfg.outputParticleMeasurementsMap.empty()) {
throw std::invalid_argument(
"Missing particle-to-measurements map output collection");
}
if (m_cfg.outputSimHitMeasurementsMap.empty()) {
throw std::invalid_argument(
"Missing particle-to-simulated-hits map output collection");
}

m_outputMeasurements.initialize(m_cfg.outputMeasurements);
m_outputClusters.initialize(m_cfg.outputClusters);
m_outputMeasurementParticlesMap.initialize(
m_cfg.outputMeasurementParticlesMap);
m_outputMeasurementSimHitsMap.initialize(m_cfg.outputMeasurementSimHitsMap);
m_outputParticleMeasurementsMap.initialize(
m_cfg.outputParticleMeasurementsMap);
m_outputSimHitMeasurementsMap.initialize(m_cfg.outputSimHitMeasurementsMap);
}

if (m_cfg.doOutputCells) {
Expand Down Expand Up @@ -141,7 +149,7 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
MeasurementContainer measurements;
ClusterContainer clusters;

IndexMultimap<ActsFatras::Barcode> measurementParticlesMap;
IndexMultimap<SimBarcode> measurementParticlesMap;
IndexMultimap<Index> measurementSimHitsMap;
measurements.reserve(simHits.size());
measurementParticlesMap.reserve(simHits.size());
Expand Down Expand Up @@ -302,6 +310,12 @@ ProcessCode DigitizationAlgorithm::execute(const AlgorithmContext& ctx) const {
m_outputMeasurements(ctx, std::move(measurements));
m_outputClusters(ctx, std::move(clusters));

// invert them before they are moved
m_outputParticleMeasurementsMap(
ctx, invertIndexMultimap(measurementParticlesMap));
m_outputSimHitMeasurementsMap(ctx,
invertIndexMultimap(measurementSimHitsMap));

m_outputMeasurementParticlesMap(ctx, std::move(measurementParticlesMap));
m_outputMeasurementSimHitsMap(ctx, std::move(measurementSimHitsMap));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@
#pragma once

#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"

#include <map>
#include <memory>
#include <string>
#include <vector>

namespace ActsExamples {
struct AlgorithmContext;

using TrackHitList = std::map<const double, const Index>;

class SurfaceSortingAlgorithm final : public IAlgorithm {
public:
Expand Down Expand Up @@ -55,7 +46,7 @@ class SurfaceSortingAlgorithm final : public IAlgorithm {
ReadDataHandle<ProtoTrackContainer> m_inputProtoTracks{this,
"InputProtoTracks"};
ReadDataHandle<SimHitContainer> m_inputSimHits{this, "InputSimHits"};
ReadDataHandle<HitSimHitsMap> m_inputMeasurementSimHitsMap{
ReadDataHandle<MeasurementSimHitsMap> m_inputMeasurementSimHitsMap{
this, "InputMeasurementSimHitsMap"};
WriteDataHandle<ProtoTrackContainer> m_outputProtoTracks{this,
"OutputProtoTracks"};
Expand Down
21 changes: 9 additions & 12 deletions Examples/Algorithms/TrackFitting/src/SurfaceSortingAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@
#include "ActsExamples/TrackFitting/SurfaceSortingAlgorithm.hpp"

#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsFatras/EventData/Hit.hpp"

#include <cstddef>
#include <memory>
#include <stdexcept>
#include <utility>
#include <vector>

namespace ActsExamples {
struct AlgorithmContext;
} // namespace ActsExamples

ActsExamples::SurfaceSortingAlgorithm::SurfaceSortingAlgorithm(
Config cfg, Acts::Logging::Level level)
: ActsExamples::IAlgorithm("SurfaceSortingAlgorithm", level),
m_cfg(std::move(cfg)) {
SurfaceSortingAlgorithm::SurfaceSortingAlgorithm(Config cfg,
Acts::Logging::Level level)
: IAlgorithm("SurfaceSortingAlgorithm", level), m_cfg(std::move(cfg)) {
if (m_cfg.inputProtoTracks.empty()) {
throw std::invalid_argument("Missing input proto track collection");
}
Expand All @@ -45,15 +40,15 @@ ActsExamples::SurfaceSortingAlgorithm::SurfaceSortingAlgorithm(
m_outputProtoTracks.initialize(m_cfg.outputProtoTracks);
}

ActsExamples::ProcessCode ActsExamples::SurfaceSortingAlgorithm::execute(
const ActsExamples::AlgorithmContext& ctx) const {
ProcessCode SurfaceSortingAlgorithm::execute(
const AlgorithmContext& ctx) const {
const auto& protoTracks = m_inputProtoTracks(ctx);
const auto& simHits = m_inputSimHits(ctx);
const auto& simHitsMap = m_inputMeasurementSimHitsMap(ctx);

ProtoTrackContainer sortedTracks;
sortedTracks.reserve(protoTracks.size());
TrackHitList trackHitList;
std::map<double, Index> trackHitList;

for (std::size_t itrack = 0; itrack < protoTracks.size(); ++itrack) {
const auto& protoTrack = protoTracks[itrack];
Expand Down Expand Up @@ -83,5 +78,7 @@ ActsExamples::ProcessCode ActsExamples::SurfaceSortingAlgorithm::execute(

m_outputProtoTracks(ctx, std::move(sortedTracks));

return ActsExamples::ProcessCode::SUCCESS;
return ProcessCode::SUCCESS;
}

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#pragma once

#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/EventData/TruthMatching.hpp"
Expand All @@ -21,8 +21,6 @@

namespace ActsExamples {

struct AlgorithmContext;

/// Matches tracks to truth particles and vice versa
class TrackTruthMatcher final : public IAlgorithm {
public:
Expand Down Expand Up @@ -56,7 +54,7 @@ class TrackTruthMatcher final : public IAlgorithm {

ReadDataHandle<ConstTrackContainer> m_inputTracks{this, "InputTracks"};
ReadDataHandle<SimParticleContainer> m_inputParticles{this, "InputParticles"};
ReadDataHandle<HitParticlesMap> m_inputMeasurementParticlesMap{
ReadDataHandle<MeasurementParticlesMap> m_inputMeasurementParticlesMap{
this, "InputMeasurementParticlesMap"};
WriteDataHandle<TrackParticleMatching> m_outputTrackParticleMatching{
this, "OutputTrackParticleMatching"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Utilities/Range.hpp"
#include "ActsFatras/EventData/Particle.hpp"

#include <algorithm>
#include <array>
Expand All @@ -25,18 +24,16 @@
#include <utility>

namespace ActsExamples {
struct AlgorithmContext;
} // namespace ActsExamples

ActsExamples::TruthSeedingAlgorithm::TruthSeedingAlgorithm(
ActsExamples::TruthSeedingAlgorithm::Config cfg, Acts::Logging::Level lvl)
: ActsExamples::IAlgorithm("TruthSeedingAlgorithm", lvl),
m_cfg(std::move(cfg)) {
TruthSeedingAlgorithm::TruthSeedingAlgorithm(Config cfg,
Acts::Logging::Level lvl)
: IAlgorithm("TruthSeedingAlgorithm", lvl), m_cfg(std::move(cfg)) {
if (m_cfg.inputParticles.empty()) {
throw std::invalid_argument("Missing input truth particles collection");
}
if (m_cfg.inputMeasurementParticlesMap.empty()) {
throw std::invalid_argument("Missing input hit-particles map collection");
if (m_cfg.inputParticleMeasurementsMap.empty()) {
throw std::invalid_argument(
"Missing input particle-measurements map collection");
}
if (m_cfg.inputSpacePoints.empty()) {
throw std::invalid_argument("Missing seeds or space point collection");
Expand Down Expand Up @@ -65,20 +62,16 @@ ActsExamples::TruthSeedingAlgorithm::TruthSeedingAlgorithm(
}

m_inputParticles.initialize(m_cfg.inputParticles);
m_inputMeasurementParticlesMap.initialize(m_cfg.inputMeasurementParticlesMap);
m_inputParticleMeasurementsMap.initialize(m_cfg.inputParticleMeasurementsMap);
m_outputParticles.initialize(m_cfg.outputParticles);
m_outputProtoTracks.initialize(m_cfg.outputProtoTracks);
m_outputSeeds.initialize(m_cfg.outputSeeds);
}

ActsExamples::ProcessCode ActsExamples::TruthSeedingAlgorithm::execute(
const ActsExamples::AlgorithmContext& ctx) const {
ProcessCode TruthSeedingAlgorithm::execute(const AlgorithmContext& ctx) const {
// prepare input collections
const auto& particles = m_inputParticles(ctx);
const auto& hitParticlesMap = m_inputMeasurementParticlesMap(ctx);
// compute particle_id -> {hit_id...} map from the
// hit_id -> {particle_id...} map on the fly.
const auto& particleHitsMap = invertIndexMultimap(hitParticlesMap);
const auto& particleMeasurementsMap = m_inputParticleMeasurementsMap(ctx);

// construct the combined input container of space point pointers from all
// configured input sources.
Expand Down Expand Up @@ -120,27 +113,28 @@ ActsExamples::ProcessCode ActsExamples::TruthSeedingAlgorithm::execute(
}

for (const auto& particle : particles) {
// find the corresponding hits for this particle
const auto& hits =
makeRange(particleHitsMap.equal_range(particle.particleId()));
// fill hit indices to create the proto track
// find the corresponding measurements for this particle
const auto& measurements =
makeRange(particleMeasurementsMap.equal_range(particle.particleId()));
// fill measurement indices to create the proto track
ProtoTrack track;
track.reserve(hits.size());
for (const auto& hit : hits) {
track.push_back(hit.second);
track.reserve(measurements.size());
for (const auto& measurement : measurements) {
track.push_back(measurement.second);
}

// The list of hits and the initial start parameters
// The list of measurements and the initial start parameters
if (track.size() < 3) {
ACTS_WARNING("Particle " << particle << " has less than 3 hits");
ACTS_WARNING("Particle " << particle << " has less than 3 measurements");
continue;
}
// Space points on the proto track
std::vector<const SimSpacePoint*> spacePointsOnTrack;
spacePointsOnTrack.reserve(track.size());
// Loop over the hit index on the proto track to find the space points
for (const auto& hitIndex : track) {
auto it = spMap.find(hitIndex);
// Loop over the measurement index on the proto track to find the space
// points
for (const auto& measurementIndex : track) {
auto it = spMap.find(measurementIndex);
if (it != spMap.end()) {
spacePointsOnTrack.push_back(it->second);
}
Expand Down Expand Up @@ -198,5 +192,7 @@ ActsExamples::ProcessCode ActsExamples::TruthSeedingAlgorithm::execute(
m_outputProtoTracks(ctx, std::move(tracks));
m_outputSeeds(ctx, std::move(seeds));

return ActsExamples::ProcessCode::SUCCESS;
return ProcessCode::SUCCESS;
}

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Acts/Definitions/Units.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/EventData/SimSeed.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"
Expand All @@ -23,25 +22,16 @@
#include <string>
#include <vector>

namespace ActsFatras {
class Barcode;
} // namespace ActsFatras

namespace Acts {
class TrackingGeometry;
}

namespace ActsExamples {
struct AlgorithmContext;

/// Construct track seeds from particles.
class TruthSeedingAlgorithm final : public IAlgorithm {
public:
struct Config {
/// The input truth particles that should be used for truth seeding.
std::string inputParticles;
/// The input hit-particles map collection.
std::string inputMeasurementParticlesMap;
/// The input particle-measurements map collection.
std::string inputParticleMeasurementsMap;
/// Input space point collections.
///
/// We allow multiple space point collections to allow different parts of
Expand Down Expand Up @@ -80,8 +70,8 @@ class TruthSeedingAlgorithm final : public IAlgorithm {
Config m_cfg;

ReadDataHandle<SimParticleContainer> m_inputParticles{this, "InputParticles"};
ReadDataHandle<HitParticlesMap> m_inputMeasurementParticlesMap{
this, "InputMeasurementParticlesMaps"};
ReadDataHandle<InverseMultimap<SimBarcode>> m_inputParticleMeasurementsMap{
this, "InputParticleMeasurementsMap"};
std::vector<std::unique_ptr<ReadDataHandle<SimSpacePointContainer>>>
m_inputSpacePoints{};

Expand Down
Loading

0 comments on commit 5c41085

Please sign in to comment.