Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Particle selector works on measurements, not simhits #3856

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
Comment on lines +65 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this inverted map is somewhere generated already and put on the whiteboard?

Also, isn't there a utility to do this kind of transformation? I can also try to look it up


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that went in accidentally

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
Loading