Skip to content

Commit

Permalink
feat: Add track to trajectory converter (#1857)
Browse files Browse the repository at this point in the history
Add a dedicated conversion algorithm from tracks to trajectories.

Blocked by:
- #1839 

Co-authored-by: Louis-Guillaume Gagnon <2743680+gagnonlg@users.noreply.github.com>
Co-authored-by: Pierfrancesco Butti <25535240+pbutti@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 14, 2023
1 parent 4fac4ae commit 1e06090
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Examples/Algorithms/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(
ActsExamplesUtilities SHARED
src/TrajectoriesToPrototracks.cpp)
src/TrajectoriesToPrototracks.cpp
src/TracksToTrajectories.cpp)
target_include_directories(
ActsExamplesUtilities
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.

#include "ActsExamples/Framework/BareAlgorithm.hpp"

namespace ActsExamples {

class TracksToTrajectories final : public BareAlgorithm {
public:
struct Config {
std::string inputTracks = "tracks";
std::string outputTrajectories = "trajectories-from-tracks";
};

/// Construct the algorithm.
///
/// @param cfg is the algorithm configuration
/// @param lvl is the logging level
TracksToTrajectories(Config cfg, Acts::Logging::Level lvl)
: BareAlgorithm("TracksToTrajectories", lvl), m_cfg(std::move(cfg)) {}

/// Run the algorithm.
///
/// @param ctx is the algorithm context with event information
/// @return a process code indication success or failure
ProcessCode execute(const AlgorithmContext& ctx) const final;

/// Const access to the config
const Config& config() const { return m_cfg; }

private:
Config m_cfg;
};

} // namespace ActsExamples
89 changes: 89 additions & 0 deletions Examples/Algorithms/Utilities/src/TracksToTrajectories.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.

#include "ActsExamples/Utilities/TracksToTrajectories.hpp"

#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/EventData/Trajectories.hpp"
#include "ActsExamples/Framework/WhiteBoard.hpp"

namespace ActsExamples {

ProcessCode TracksToTrajectories::execute(const AlgorithmContext& ctx) const {
const auto& tracks = ctx.eventStore.get<TrackContainer>(m_cfg.inputTracks);

// Prepare the output data with MultiTrajectory
TrajectoriesContainer trajectories;
trajectories.reserve(tracks.size());

static const Acts::ConstTrackAccessor<unsigned int> seedNumber("trackGroup");

if (tracks.hasColumn(Acts::hashString("trackGroup"))) {
// track group by seed is available, produce grouped trajectories
std::optional<unsigned int> lastSeed;

Trajectories::IndexedParameters parameters;
std::vector<Acts::MultiTrajectoryTraits::IndexType> tips;

for (const auto& track : tracks) {
if (!lastSeed) {
lastSeed = seedNumber(track);
}

if (seedNumber(track) != lastSeed.value()) {
// make copies and clear vectors
trajectories.emplace_back(tracks.trackStateContainer(), tips,
parameters);
tips.clear();
parameters.clear();
}

lastSeed = seedNumber(track);

tips.push_back(track.tipIndex());
parameters.emplace(
std::pair{track.tipIndex(),
TrackParameters{track.referenceSurface().getSharedPtr(),
track.parameters(), track.covariance()}});
}

if (tips.empty()) {
ACTS_ERROR("Last trajectory is empty");
}

// last entry: move vectors
trajectories.emplace_back(tracks.trackStateContainer(), std::move(tips),
std::move(parameters));

} else {
// no grouping by seed, make one trajectory per track

for (const auto& track : tracks) {
Trajectories::IndexedParameters parameters;
parameters.reserve(1);
std::vector<Acts::MultiTrajectoryTraits::IndexType> tips;
tips.reserve(1);

tips.push_back(track.tipIndex());
parameters.emplace(
std::pair{track.tipIndex(),
TrackParameters{track.referenceSurface().getSharedPtr(),
track.parameters(), track.covariance()}});

trajectories.emplace_back(tracks.trackStateContainer(), std::move(tips),
std::move(parameters));
}
}

ctx.eventStore.add(m_cfg.outputTrajectories, std::move(trajectories));

return ProcessCode::SUCCESS;
}
} // namespace ActsExamples
19 changes: 19 additions & 0 deletions Examples/Python/src/TrackFinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ActsExamples/TrackFinding/SpacePointMaker.hpp"
#include "ActsExamples/TrackFinding/TrackFindingAlgorithm.hpp"
#include "ActsExamples/TrackFinding/TrackParamsEstimationAlgorithm.hpp"
#include "ActsExamples/Utilities/TracksToTrajectories.hpp"
#include "ActsExamples/Utilities/TrajectoriesToPrototracks.hpp"

#include <memory>
Expand Down Expand Up @@ -287,6 +288,24 @@ void addTrackFinding(Context& ctx) {
ACTS_PYTHON_STRUCT_END();
}

{
using Alg = ActsExamples::TracksToTrajectories;
using Config = Alg::Config;

auto alg =
py::class_<Alg, ActsExamples::BareAlgorithm, std::shared_ptr<Alg>>(
mex, "TracksToTrajectories")
.def(py::init<const Config&, Acts::Logging::Level>(),
py::arg("config"), py::arg("level"))
.def_property_readonly("config", &Alg::config);

auto c = py::class_<Config>(alg, "Config").def(py::init<>());
ACTS_PYTHON_STRUCT_BEGIN(c, Config);
ACTS_PYTHON_MEMBER(inputTracks);
ACTS_PYTHON_MEMBER(outputTrajectories);
ACTS_PYTHON_STRUCT_END();
}

{
auto constructor = [](const std::vector<
std::pair<GeometryIdentifier,
Expand Down

0 comments on commit 1e06090

Please sign in to comment.