Skip to content

Commit

Permalink
fix: Distinguish between hits and measurements in ParticleSelector
Browse files Browse the repository at this point in the history
…in Examples
  • Loading branch information
andiwand committed Dec 6, 2024
1 parent feac686 commit e006ab6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@

#include "ActsExamples/TruthTracking/ParticleSelector.hpp"

#include "Acts/Definitions/Common.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"

#include <ostream>
#include <stdexcept>
#include <utility>

ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
Acts::Logging::Level level)
namespace ActsExamples {

ParticleSelector::ParticleSelector(const Config& config,
Acts::Logging::Level level)
: IAlgorithm("ParticleSelector", level), m_cfg(config) {
if (m_cfg.inputParticles.empty()) {
throw std::invalid_argument("Missing input particles collection");
Expand All @@ -28,8 +30,17 @@ ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
}

m_inputParticles.initialize(m_cfg.inputParticles);
m_inputParticleMeasurementsMap.maybeInitialize(
m_cfg.inputParticleMeasurementsMap);
m_outputParticles.initialize(m_cfg.outputParticles);

if (m_inputParticleMeasurementsMap.isInitialized() &&
(m_cfg.measurementsMin > 0 ||
m_cfg.measurementsMax < std::numeric_limits<std::size_t>::max())) {
throw std::invalid_argument(
"Measurement-based cuts require the inputMeasurementParticlesMap");
}

ACTS_DEBUG("selection particle rho [" << m_cfg.rhoMin << "," << m_cfg.rhoMax
<< ")");
ACTS_DEBUG("selection particle |z| [" << m_cfg.absZMin << "," << m_cfg.absZMax
Expand All @@ -46,6 +57,8 @@ ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
<< ")");
ACTS_DEBUG("selection particle m [" << m_cfg.mMin << "," << m_cfg.mMax
<< ")");
ACTS_DEBUG("selection particle hits [" << m_cfg.hitsMin << ","
<< m_cfg.hitsMax << ")");
ACTS_DEBUG("selection particle measurements ["
<< m_cfg.measurementsMin << "," << m_cfg.measurementsMax << ")");
ACTS_DEBUG("remove charged particles " << m_cfg.removeCharged);
Expand All @@ -59,12 +72,18 @@ ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
<< m_cfg.maxPrimaryVertexId << ")");
}

ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
const AlgorithmContext& ctx) const {
ProcessCode ParticleSelector::execute(const AlgorithmContext& ctx) const {
// prepare input/ output types
const SimParticleContainer& inputParticles = m_inputParticles(ctx);

const static InverseMultimap<SimBarcode> emptyMeasurementParticlesMap;
const InverseMultimap<SimBarcode>& inputMeasurementParticlesMap =
m_inputParticleMeasurementsMap.isInitialized()
? m_inputParticleMeasurementsMap(ctx)
: emptyMeasurementParticlesMap;

std::size_t nInvalidCharge = 0;
std::size_t nInvalidHitCount = 0;
std::size_t nInvalidMeasurementCount = 0;

// helper functions to select tracks
Expand All @@ -87,9 +106,14 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(

nInvalidCharge += static_cast<std::size_t>(!validCharge);

bool validMeasurementCount =
within(p.numberOfHits(), m_cfg.measurementsMin, m_cfg.measurementsMax);
const bool validHitCount =
within(p.numberOfHits(), m_cfg.hitsMin, m_cfg.hitsMax);
nInvalidHitCount += static_cast<std::size_t>(!validHitCount);

const std::size_t measurementCount =
inputMeasurementParticlesMap.count(p.particleId());
const bool validMeasurementCount =
within(measurementCount, m_cfg.measurementsMin, m_cfg.measurementsMax);
nInvalidMeasurementCount +=
static_cast<std::size_t>(!validMeasurementCount);

Expand All @@ -103,7 +127,7 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
}

return validPdg && validCharge && validSecondary && validPrimaryVertexId &&
validMeasurementCount &&
validHitCount && validMeasurementCount &&
within(p.transverseMomentum(), m_cfg.ptMin, m_cfg.ptMax) &&
within(std::abs(eta), m_cfg.absEtaMin, m_cfg.absEtaMax) &&
within(eta, m_cfg.etaMin, m_cfg.etaMax) &&
Expand Down Expand Up @@ -132,10 +156,13 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
<< outputParticles.size() << " from "
<< inputParticles.size() << " particles");
ACTS_DEBUG("filtered out because of charge: " << nInvalidCharge);
ACTS_DEBUG("filtered out because of hit count: " << nInvalidHitCount);
ACTS_DEBUG("filtered out because of measurement count: "
<< nInvalidMeasurementCount);

m_outputParticles(ctx, std::move(outputParticles));

return ProcessCode::SUCCESS;
}

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

#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
Expand All @@ -26,6 +27,9 @@ class ParticleSelector final : public IAlgorithm {
struct Config {
/// The input particles collection.
std::string inputParticles;
/// (Optionally) The input particle measurements map. Only required for
/// measurement-based cuts.
std::string inputParticleMeasurementsMap;
/// The output particles collection.
std::string outputParticles;

Expand All @@ -51,7 +55,10 @@ class ParticleSelector final : public IAlgorithm {
// Rest mass cuts
double mMin = 0;
double mMax = std::numeric_limits<double>::infinity();
/// Measurement number cuts
// Hit count cuts
std::size_t hitsMin = 0;
std::size_t hitsMax = std::numeric_limits<std::size_t>::max();
// Measurement number cuts
std::size_t measurementsMin = 0;
std::size_t measurementsMax = std::numeric_limits<std::size_t>::max();
/// Remove charged particles.
Expand Down Expand Up @@ -81,6 +88,8 @@ class ParticleSelector final : public IAlgorithm {
Config m_cfg;

ReadDataHandle<SimParticleContainer> m_inputParticles{this, "InputParticles"};
ReadDataHandle<InverseMultimap<SimBarcode>> m_inputParticleMeasurementsMap{
this, "InputParticleMeasurementsMap"};

WriteDataHandle<SimParticleContainer> m_outputParticles{this,
"OutputParticles"};
Expand Down
3 changes: 3 additions & 0 deletions Examples/Python/python/acts/examples/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"absEta", # (min,max)
"pt", # (min,max)
"m", # (min,max)
"hits", # (min,max)
"measurements", # (min,max)
"removeCharged", # bool
"removeNeutral", # bool
Expand Down Expand Up @@ -393,6 +394,8 @@ def addParticleSelection(
ptMax=config.pt[1],
mMin=config.m[0],
mMax=config.m[1],
hitsMin=config.hits[0],
hitsMax=config.hits[1],
measurementsMin=config.measurements[0],
measurementsMax=config.measurements[1],
removeCharged=config.removeCharged,
Expand Down
2 changes: 2 additions & 0 deletions Examples/Python/src/TruthTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ void addTruthTracking(Context& ctx) {
ACTS_PYTHON_MEMBER(mMax);
ACTS_PYTHON_MEMBER(ptMin);
ACTS_PYTHON_MEMBER(ptMax);
ACTS_PYTHON_MEMBER(hitsMin);
ACTS_PYTHON_MEMBER(hitsMax);
ACTS_PYTHON_MEMBER(measurementsMin);
ACTS_PYTHON_MEMBER(measurementsMax);
ACTS_PYTHON_MEMBER(removeCharged);
Expand Down

0 comments on commit e006ab6

Please sign in to comment.