Skip to content

Commit

Permalink
Merge branch 'main' into ex-feat-copy-measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Nov 27, 2024
2 parents 53c4c26 + 1701308 commit 9509e5a
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include "ActsExamples/TruthTracking/HitSelector.hpp"

#include "Acts/Utilities/MathHelpers.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"

namespace ActsExamples {

HitSelector::HitSelector(const Config& config, Acts::Logging::Level level)
: IAlgorithm("HitSelector", level), m_cfg(config) {
if (m_cfg.minX >= m_cfg.maxX || m_cfg.minY >= m_cfg.maxY ||
m_cfg.minZ >= m_cfg.maxZ || m_cfg.minR >= m_cfg.maxR ||
m_cfg.minTime >= m_cfg.maxTime ||
m_cfg.minEnergyLoss >= m_cfg.maxEnergyLoss ||
m_cfg.minPrimaryVertexId >= m_cfg.maxPrimaryVertexId) {
throw std::invalid_argument(
"Invalid bounds configuration: min values must be less than max "
"values");
}
m_inputHits.initialize(m_cfg.inputHits);
m_inputParticlesSelected.maybeInitialize(m_cfg.inputParticlesSelected);
m_outputHits.initialize(m_cfg.outputHits);

ACTS_DEBUG("selection particles " << m_cfg.inputParticlesSelected);
ACTS_DEBUG("selection hit x [" << m_cfg.minX << "," << m_cfg.maxX << ")");
ACTS_DEBUG("selection hit y [" << m_cfg.minY << "," << m_cfg.maxY << ")");
ACTS_DEBUG("selection hit z [" << m_cfg.minZ << "," << m_cfg.maxZ << ")");
ACTS_DEBUG("selection hit r [" << m_cfg.minR << "," << m_cfg.maxR << ")");
ACTS_DEBUG("selection hit time [" << m_cfg.minTime << "," << m_cfg.maxTime
<< ")");
ACTS_DEBUG("selection hit energy loss [" << m_cfg.minEnergyLoss << ","
<< m_cfg.maxEnergyLoss << ")");
ACTS_DEBUG("selection primary vertex ID [" << m_cfg.minPrimaryVertexId << ","
<< m_cfg.maxPrimaryVertexId
<< ")");
}

ProcessCode HitSelector::execute(const AlgorithmContext& ctx) const {
const SimHitContainer& hits = m_inputHits(ctx);
const SimParticleContainer* particlesSelected =
m_inputParticlesSelected.isInitialized() ? &m_inputParticlesSelected(ctx)
: nullptr;

std::vector<SimHit> unorderedHits;
unorderedHits.reserve(hits.size());

for (const auto& hit : hits) {
const double r = Acts::fastHypot(hit.position().x(), hit.position().y());
const std::uint64_t primaryVertexId = hit.particleId().vertexPrimary();

const bool validParticle = (particlesSelected == nullptr) ||
particlesSelected->contains(hit.particleId());
const bool validX =
(m_cfg.minX <= hit.position().x()) && (hit.position().x() < m_cfg.maxX);
const bool validY =
(m_cfg.minY <= hit.position().y()) && (hit.position().y() < m_cfg.maxY);
const bool validZ =
(m_cfg.minZ <= hit.position().z()) && (hit.position().z() < m_cfg.maxZ);
const bool validR = (m_cfg.minR <= r) && (r < m_cfg.maxR);
const bool validTime =
(m_cfg.minTime <= hit.time()) && (hit.time() < m_cfg.maxTime);
const bool validEnergyLoss =
(m_cfg.minEnergyLoss <= hit.depositedEnergy()) &&
(hit.depositedEnergy() < m_cfg.maxEnergyLoss);
const bool validPrimaryVertexId =
(m_cfg.minPrimaryVertexId <= primaryVertexId) &&
(primaryVertexId < m_cfg.maxPrimaryVertexId);

const bool validHit = validParticle && validX && validY && validZ &&
validR && validTime && validEnergyLoss &&
validPrimaryVertexId;
if (validHit) {
unorderedHits.push_back(hit);
}
}

// hits are still sorted after filtering
SimHitContainer selectedHits(boost::container::ordered_range_t{},
unorderedHits.begin(), unorderedHits.end());

ACTS_DEBUG("selected " << selectedHits.size() << " from " << hits.size()
<< " hits");

m_outputHits(ctx, std::move(selectedHits));

return ProcessCode::SUCCESS;
}

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"

#include <limits>
#include <string>

namespace ActsExamples {

/// Select hits by applying some selection cuts.
class HitSelector final : public IAlgorithm {
public:
struct Config {
/// Input hit collection.
std::string inputHits;
/// Optional input particle collection.
std::string inputParticlesSelected;
/// Output hit collection
std::string outputHits;

/// Min x cut
double minX = -std::numeric_limits<double>::max();
/// Max x cut
double maxX = std::numeric_limits<double>::max();

/// Min y cut
double minY = -std::numeric_limits<double>::max();
/// Max y cut
double maxY = std::numeric_limits<double>::max();

/// Min z cut
double minZ = -std::numeric_limits<double>::max();
/// Max z cut
double maxZ = std::numeric_limits<double>::max();

/// Min r cut
double minR = 0.0;
/// Max r cut
double maxR = std::numeric_limits<double>::max();

/// Min time cut
double minTime = -std::numeric_limits<double>::max();
/// Max time cut
double maxTime = std::numeric_limits<double>::max();

/// Min energy loss cut
double minEnergyLoss = 0;
/// Max energy loss cut
double maxEnergyLoss = std::numeric_limits<double>::max();

/// Min primary vertex ID cut
std::uint64_t minPrimaryVertexId = 0;
/// Max primary vertex ID cut
std::uint64_t maxPrimaryVertexId =
std::numeric_limits<std::uint64_t>::max();
};

HitSelector(const Config& config, Acts::Logging::Level level);

ProcessCode execute(const AlgorithmContext& ctx) const final;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
Config m_cfg;

ReadDataHandle<SimHitContainer> m_inputHits{this, "InputHits"};
ReadDataHandle<SimParticleContainer> m_inputParticlesSelected{
this, "InputParticlesSelected"};
WriteDataHandle<SimHitContainer> m_outputHits{this, "OutputHits"};
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
<< ")");
ACTS_DEBUG("selection particle m [" << m_cfg.mMin << "," << m_cfg.mMax
<< ")");
ACTS_DEBUG("selection particle measurements ["
<< m_cfg.measurementsMin << "," << m_cfg.measurementsMax << ")");
ACTS_DEBUG("remove charged particles " << m_cfg.removeCharged);
ACTS_DEBUG("remove neutral particles " << m_cfg.removeNeutral);
ACTS_DEBUG("remove secondary particles " << m_cfg.removeSecondaries);
ACTS_DEBUG("exclude pdgs: ");
for (auto pdg : m_cfg.excludeAbsPdgs) {
ACTS_DEBUG(" " << pdg);
}
ACTS_DEBUG("primary vertex ID [" << m_cfg.minPrimaryVertexId << ","
<< m_cfg.maxPrimaryVertexId << ")");
}

ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
Expand All @@ -73,6 +81,9 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
const bool validCharged = (p.charge() != 0) && !m_cfg.removeCharged;
const bool validCharge = validNeutral || validCharged;
const bool validSecondary = !m_cfg.removeSecondaries || !p.isSecondary();
const bool validPrimaryVertexId =
within(p.particleId().vertexPrimary(), m_cfg.minPrimaryVertexId,
m_cfg.maxPrimaryVertexId);

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

Expand All @@ -91,7 +102,8 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
}
}

return validPdg && validCharge && validSecondary && validMeasurementCount &&
return validPdg && validCharge && validSecondary && validPrimaryVertexId &&
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class ParticleSelector final : public IAlgorithm {
bool removeSecondaries = false;
/// Exclude particles depending on absolute pdg value
std::vector<int> excludeAbsPdgs;

/// Min primary vertex ID cut
std::uint64_t minPrimaryVertexId = 0;
/// Max primary vertex ID cut
std::uint64_t maxPrimaryVertexId =
std::numeric_limits<std::uint64_t>::max();
};

ParticleSelector(const Config& config, Acts::Logging::Level level);
Expand Down
1 change: 1 addition & 0 deletions Examples/Algorithms/TruthTracking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(
ActsExamples/TruthTracking/TruthTrackFinder.cpp
ActsExamples/TruthTracking/TruthVertexFinder.cpp
ActsExamples/TruthTracking/TruthSeedingAlgorithm.cpp
ActsExamples/TruthTracking/HitSelector.cpp
)
target_include_directories(
ActsExamplesTruthTracking
Expand Down
1 change: 0 additions & 1 deletion Examples/Algorithms/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ add_library(
src/TrackSelectorAlgorithm.cpp
src/TracksToTrajectories.cpp
src/PrototracksToTracks.cpp
src/HitSelector.cpp
src/TracksToParameters.cpp
)
target_include_directories(
Expand Down

This file was deleted.

33 changes: 0 additions & 33 deletions Examples/Algorithms/Utilities/src/HitSelector.cpp

This file was deleted.

11 changes: 9 additions & 2 deletions Examples/Python/src/TruthTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Acts/Plugins/Python/Utilities.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/TruthTracking/HitSelector.hpp"
#include "ActsExamples/TruthTracking/ParticleSelector.hpp"
#include "ActsExamples/TruthTracking/ParticleSmearing.hpp"
#include "ActsExamples/TruthTracking/TrackModifier.hpp"
Expand All @@ -16,7 +17,6 @@
#include "ActsExamples/TruthTracking/TruthSeedingAlgorithm.hpp"
#include "ActsExamples/TruthTracking/TruthTrackFinder.hpp"
#include "ActsExamples/TruthTracking/TruthVertexFinder.hpp"
#include "ActsExamples/Utilities/HitSelector.hpp"

#include <memory>

Expand Down Expand Up @@ -85,6 +85,8 @@ void addTruthTracking(Context& ctx) {
ACTS_PYTHON_MEMBER(removeNeutral);
ACTS_PYTHON_MEMBER(removeSecondaries);
ACTS_PYTHON_MEMBER(excludeAbsPdgs);
ACTS_PYTHON_MEMBER(minPrimaryVertexId);
ACTS_PYTHON_MEMBER(maxPrimaryVertexId);
ACTS_PYTHON_STRUCT_END();

pythonRangeProperty(c, "rho", &Config::rhoMin, &Config::rhoMax);
Expand All @@ -97,6 +99,8 @@ void addTruthTracking(Context& ctx) {
pythonRangeProperty(c, "pt", &Config::ptMin, &Config::ptMax);
pythonRangeProperty(c, "measurements", &Config::measurementsMin,
&Config::measurementsMax);
pythonRangeProperty(c, "primaryVertexId", &Config::minPrimaryVertexId,
&Config::maxPrimaryVertexId);
}

{
Expand Down Expand Up @@ -154,7 +158,10 @@ void addTruthTracking(Context& ctx) {
outputParticles, outputSeeds, outputProtoTracks, deltaRMin, deltaRMax);

ACTS_PYTHON_DECLARE_ALGORITHM(ActsExamples::HitSelector, mex, "HitSelector",
inputHits, outputHits, maxTime);
inputHits, inputParticlesSelected, outputHits,
minX, maxX, minY, maxY, minZ, maxZ, minR, maxR,
minTime, maxTime, minEnergyLoss, maxEnergyLoss,
minPrimaryVertexId, maxPrimaryVertexId);

ACTS_PYTHON_DECLARE_ALGORITHM(
ActsExamples::TrackTruthMatcher, mex, "TrackTruthMatcher", inputTracks,
Expand Down

0 comments on commit 9509e5a

Please sign in to comment.