Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Huth committed Nov 13, 2024
1 parent b14548d commit a95b77d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ ActsExamples::ParticleSelector::ParticleSelector(const Config& config,
}

m_inputParticles.initialize(m_cfg.inputParticles);

Config defaults;
if (m_cfg.measurementsMin > defaults.measurementsMin ||
m_cfg.measurementsMax < defaults.measurementsMax) {
m_inputMeasPartMap.initialize(m_cfg.inputMeasurementParticlesMap);
}
m_outputParticles.initialize(m_cfg.outputParticles);

ACTS_DEBUG("selection particle rho [" << m_cfg.rhoMin << "," << m_cfg.rhoMax
Expand Down Expand Up @@ -56,6 +62,21 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(
// prepare input/ output types
const SimParticleContainer& inputParticles = m_inputParticles(ctx);

// Optionally construct a particles->measurement map
std::optional<boost::container::flat_multimap<ActsFatras::Barcode, Index>>
partMeasMap;
if (m_inputMeasPartMap.isInitialized()) {
const auto& measPartMap = m_inputMeasPartMap(ctx);
using InvPair = std::pair<ActsFatras::Barcode, Index>;
std::vector<InvPair> v(measPartMap.size());
std::ranges::transform(measPartMap, v.begin(), [](const auto& p) {
return InvPair{p.second, p.first};
});
std::ranges::sort(v, std::less{}, &InvPair::first);
partMeasMap.emplace(boost::container::ordered_range_t{}, v.begin(),
v.end());
}

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

Expand All @@ -76,8 +97,13 @@ ActsExamples::ProcessCode ActsExamples::ParticleSelector::execute(

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

bool validMeasurementCount =
within(p.numberOfHits(), m_cfg.measurementsMin, m_cfg.measurementsMax);
bool validMeasurementCount = true;
if (partMeasMap) {
auto [begin, end] = partMeasMap->equal_range(p.particleId());
std::size_t nMeasurements = std::distance(begin, end);
validMeasurementCount =
within(nMeasurements, m_cfg.measurementsMin, m_cfg.measurementsMax);
}

nInvalidMeasurementCount +=
static_cast<std::size_t>(!validMeasurementCount);
Expand Down
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;
/// The input measurement particles map (optional, required if selection
/// based on number of measurements is requested).
std::string inputMeasurementParticlesMap;
/// The output particles collection.
std::string outputParticles;

Expand Down Expand Up @@ -75,6 +79,8 @@ class ParticleSelector final : public IAlgorithm {
Config m_cfg;

ReadDataHandle<SimParticleContainer> m_inputParticles{this, "InputParticles"};
ReadDataHandle<IndexMultimap<ActsFatras::Barcode>> m_inputMeasPartMap{
this, "InputMeasPartMap"};

WriteDataHandle<SimParticleContainer> m_outputParticles{this,
"OutputParticles"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class SimParticle final {
/// Accumulated path within material measured in interaction lengths.
double pathInL0() const { return final().pathInL0(); }

/// Number of hits.
std::uint32_t numberOfHits() const { return final().numberOfHits(); }
/// Number of simulated hits (not measurements).
std::uint32_t numberOfSimHits() const { return final().numberOfHits(); }

/// Particle outcome.
ActsFatras::ParticleOutcome outcome() const { return final().outcome(); }
Expand Down
37 changes: 37 additions & 0 deletions Examples/Io/Json/src/TestSurfaceJsonRoundtrip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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/Io/Json/JsonSurfacesReader.hpp"
#include <Acts/Plugins/Json/SurfaceJsonConverter.hpp>

#include <fstream>

#include <nlohmann/json.hpp>

void writeSurfaces(
std::ostream &os,
const std::vector<std::shared_ptr<Acts::Surface>> &jSurfaces) {
nlohmann::json j = nlohmann::json::array();

for (const auto &jSurface : jSurfaces) {
j.push_back(
Acts::SurfaceJsonConverter::toJson(Acts::GeometryContext{}, *jSurface));
}

os << j;
}

int main(int argc, char **argv) {
std::vector<std::string> args(argv, argv + argc);

auto surfaces =
ActsExamples::JsonSurfacesReader::readVector({args.at(1), {}});

std::ofstream outfile("output_surfaces.json");
writeSurfaces(outfile, surfaces);
}
2 changes: 1 addition & 1 deletion Examples/Io/Root/src/RootParticleWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ ActsExamples::ProcessCode ActsExamples::RootParticleWriter::writeT(
Acts::clampValue<float>(particle.pathInX0() / Acts::UnitConstants::mm));
m_pathInL0.push_back(
Acts::clampValue<float>(particle.pathInL0() / Acts::UnitConstants::mm));
m_numberOfHits.push_back(particle.numberOfHits());
m_numberOfHits.push_back(particle.numberOfSimHits());
m_outcome.push_back(static_cast<std::uint32_t>(particle.outcome()));
}

Expand Down
2 changes: 2 additions & 0 deletions Examples/Python/python/acts/examples/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def addParticleSelection(
config: ParticleSelectorConfig,
inputParticles: str,
outputParticles: str,
inputMeasurementParticlesMap: str = "",
logLevel: Optional[acts.logging.Level] = None,
) -> None:
"""
Expand Down Expand Up @@ -401,6 +402,7 @@ def addParticleSelection(
),
level=customLogLevel(),
inputParticles=inputParticles,
inputMeasurementParticlesMap=inputMeasurementParticlesMap,
outputParticles=outputParticles,
)
)
Expand Down
1 change: 1 addition & 0 deletions Examples/Python/src/TruthTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void addTruthTracking(Context& ctx) {

ACTS_PYTHON_STRUCT_BEGIN(c, Config);
ACTS_PYTHON_MEMBER(inputParticles);
ACTS_PYTHON_MEMBER(inputMeasurementParticlesMap);
ACTS_PYTHON_MEMBER(outputParticles);
ACTS_PYTHON_MEMBER(rhoMin);
ACTS_PYTHON_MEMBER(rhoMax);
Expand Down

0 comments on commit a95b77d

Please sign in to comment.