Skip to content

Commit

Permalink
Traccc Plugin (conversion)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredevb committed Jun 12, 2024
1 parent c89fa00 commit 90eb609
Show file tree
Hide file tree
Showing 14 changed files with 746 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ set_option_if(ACTS_SETUP_COVFIE
ACTS_BUILD_EXAMPLES OR ACTS_BUILD_EVERYTHING)
set_option_if(ACTS_BUILD_PLUGIN_COVFIE
ACTS_BUILD_EXAMPLES OR ACTS_BUILD_EVERYTHING)
set_option_if(ACTS_SETUP_DETRAY
ACTS_BUILD_EXAMPLES OR ACTS_BUILD_EVERYTHING)
set_option_if(ACTS_SETUP_TRACCC
ACTS_BUILD_EXAMPLES OR ACTS_BUILD_EVERYTHING)
set_option_if(ACTS_BUILD_PLUGIN_TRACCC
ACTS_BUILD_EXAMPLES OR ACTS_BUILD_EVERYTHING)

# feature tests
include(CheckCXXSourceCompiles)
Expand Down Expand Up @@ -309,6 +315,8 @@ endmacro()
# the same package twice. This avoids having complex if/else trees to sort out
# when a particular package is actually needed.

add_definitions(-DALGEBRA_PLUGINS_INCLUDE_ARRAY)

# plugin dependencies
if(ACTS_SETUP_COVFIE)
if(ACTS_USE_SYSTEM_COVFIE)
Expand Down
10 changes: 8 additions & 2 deletions Core/include/Acts/EventData/MultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class TrackStateRange {
}
}

Iterator operator++(int) {
Iterator tmp(*this);
operator++();
return tmp;
}

bool operator==(const Iterator& other) const {
if (!proxy && !other.proxy) {
return true;
Expand All @@ -109,8 +115,8 @@ class TrackStateRange {
TrackStateRange(ProxyType _begin) : m_begin{_begin} {}
TrackStateRange() : m_begin{std::nullopt} {}

Iterator begin() { return m_begin; }
Iterator end() { return Iterator{std::nullopt}; }
Iterator begin() const { return m_begin; }
Iterator end() const { return Iterator{std::nullopt}; }

private:
Iterator m_begin;
Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/python/acts/examples/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ def addAmbiguityResolutionMLDBScan(
addTrackWriters(
s,
name="ambiMLDBScan",
trajectories=alg.config.outputTracks,
tracks=alg.config.outputTracks,
outputDirRoot=outputDirRoot,
outputDirCsv=outputDirCsv,
writeStates=writeTrajectories,
Expand Down
1 change: 1 addition & 0 deletions Plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_component_if(Legacy PluginLegacy ACTS_BUILD_PLUGIN_LEGACY)
add_component_if(Onnx PluginOnnx ACTS_BUILD_PLUGIN_ONNX)
add_component_if(ExaTrkX PluginExaTrkX ACTS_BUILD_PLUGIN_EXATRKX)
add_component_if(Covfie PluginCovfie ACTS_BUILD_PLUGIN_COVFIE)
add_component_if(Traccc PluginTraccc ACTS_BUILD_PLUGIN_TRACCC)

# dependent plugins. depend either on a independent plugins or on one another
add_component_if(TGeo PluginTGeo ACTS_BUILD_PLUGIN_TGEO)
Expand Down
26 changes: 26 additions & 0 deletions Plugins/Traccc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
add_library(
ActsPluginTraccc INTERFACE)

target_include_directories(
ActsPluginTraccc
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(
ActsPluginTraccc
INTERFACE
ActsCore
traccc::core
traccc::io
vecmem::core
detray::utils
detray::io
ActsPluginCovfie)

install(
TARGETS ActsPluginTraccc
EXPORT ActsPluginTracccTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(
DIRECTORY include/Acts
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
41 changes: 41 additions & 0 deletions Plugins/Traccc/include/Acts/Plugins/Traccc/BarcodeMap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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/.

#pragma once

// Acts include(s)
#include "Acts/Geometry/GeometryIdentifier.hpp"

// Detray include(s)
#include "detray/core/detector.hpp"

// System include(s)
#include <cstdint>
#include <cstdlib>
#include <map>
#include <memory>
#include <tuple>
#include <utility>

namespace Acts::TracccPlugin {

/// @brief Creates a map from Acts geometry ID (value) to detray barcode.
/// @param detector the detray detector.
/// @return A map (key = geometry ID value, value = detray geometry barcode).
template <typename metadata_t, typename container_t>
inline std::map<std::uint64_t, detray::geometry::barcode> createBarcodeMap(
const detray::detector<metadata_t, container_t>& detector) {
// Construct a map from Acts surface identifiers to Detray barcodes.
std::map<std::uint64_t, detray::geometry::barcode> barcodeMap;
for (const auto& surface : detector.surfaces()) {
barcodeMap[surface.source] = surface.barcode();
}
return barcodeMap;
}

} // namespace Acts::TracccPlugin
96 changes: 96 additions & 0 deletions Plugins/Traccc/include/Acts/Plugins/Traccc/CellConversion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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/.

#pragma once

// Acts include(s)
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"

// Plugin include(s)
#include "Acts/Plugins/Traccc/BarcodeMap.hpp"
#include "Acts/Plugins/Traccc/Detail/Module.hpp"

// Detray include(s)
#include "detray/core/detector.hpp"

// Traccc include(s)
#include "traccc/edm/cell.hpp"
#include "traccc/geometry/geometry.hpp"
#include "traccc/io/digitization_config.hpp"
#include "traccc/io/read_geometry.hpp"
#include "traccc/io/reader_edm.hpp"

// System include(s)
#include <cstdint>
#include <cstdlib>
#include <map>
#include <memory>
#include <tuple>
#include <utility>

namespace {

/// Comparator used for sorting cells. This sorting is one of the assumptions
/// made in the clusterization algorithm
struct cell_order {
bool operator()(const traccc::cell& lhs, const traccc::cell& rhs) const {
if (lhs.module_link != rhs.module_link) {
return lhs.module_link < rhs.module_link;
} else if (lhs.channel1 != rhs.channel1) {
return (lhs.channel1 < rhs.channel1);
} else {
return (lhs.channel0 < rhs.channel0);
}
}
}; // struct cell_order

} // namespace

namespace Acts::TracccPlugin {

/// @brief Converts a "geometry ID -> traccc cells" map to traccc cells and modules.
/// @param mr The memory resource to use.
/// @param cellsMap A map from Acts geometry ID value to traccc cells.
/// @param geom The traccc geometry.
/// @param dconfig The traccc digitization configuration.
/// @param barcode_map A map from Acts geometry ID value to detray barcode.
/// @return A tuple containing the traccc cells (first item) and traccc modules (second item).
inline auto createCellsAndModules(
vecmem::memory_resource* mr,
std::map<std::uint64_t, std::vector<traccc::cell>> cellsMap,
const traccc::geometry* geom, const traccc::digitization_config* dconfig,
const std::map<std::uint64_t, detray::geometry::barcode>* barcodeMap) {
traccc::io::cell_reader_output out(mr);

// Sort the cells.
for (auto& [_, cells] : cellsMap) {
std::sort(cells.begin(), cells.end(), cell_order());
}

// Fill the output containers with the ordered cells and modules.
for (const auto& [originalGeometryID, cells] : cellsMap) {
// Modify the geometry ID of the module if a barcode map is
// provided.
std::uint64_t geometryID = (barcodeMap != nullptr)
? barcodeMap->at(originalGeometryID).value()
: originalGeometryID;

// Add the module and its cells to the output.
out.modules.push_back(
Detail::get_module(geometryID, geom, dconfig, originalGeometryID));
for (auto& cell : cells) {
out.cells.push_back(cell);
// Set the module link.
out.cells.back().module_link = out.modules.size() - 1;
}
}
return std::make_tuple(std::move(out.cells), std::move(out.modules));
}

} // namespace Acts::TracccPlugin
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/.

#pragma once

// Acts include(s)
#include "Acts/Definitions/Algebra.hpp"

// System include(s)
#include <cstddef>

namespace Acts::TracccPlugin::Detail {

/// @brief Creates a new Acts vector from another vector type.
template <std::size_t N, typename dvector_t>
inline Acts::ActsVector<N> newVector(const dvector_t& dvec) {
Acts::ActsVector<N> res;
for (std::size_t i = 0; i < N; i++) {
res(i) = static_cast<Acts::ActsScalar>(dvec[i]);
}
return res;
}
/// @brief Creates a new Acts square matrix from another square matrix type.
template <std::size_t N, typename matrixNxN_t>
inline Acts::ActsSquareMatrix<N> newSqaureMatrix(const matrixNxN_t& mat) {
Acts::ActsSquareMatrix<N> res;
for (std::size_t x = 0; x < N; x++) {
for (std::size_t y = 0; y < N; y++) {
res(x, y) = static_cast<Acts::ActsScalar>(mat[x][y]);
}
}
return res;
}

} // namespace Acts::TracccPlugin::Detail
84 changes: 84 additions & 0 deletions Plugins/Traccc/include/Acts/Plugins/Traccc/Detail/Module.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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/.

#pragma once

// Acts include(s)
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"

// Detray include(s)
#include "detray/core/detector.hpp"

// Traccc include(s)
#include "traccc/edm/cell.hpp"
#include "traccc/geometry/geometry.hpp"
#include "traccc/io/digitization_config.hpp"
#include "traccc/io/read_geometry.hpp"
#include "traccc/io/reader_edm.hpp"

// System include(s)
#include <cstdint>
#include <cstdlib>
#include <map>
#include <memory>
#include <tuple>
#include <utility>

namespace Acts::TracccPlugin::Detail {

/// Helper function which finds module from csv::cell in the geometry and
/// digitization config, and initializes the modules limits with the cell's
/// properties
traccc::cell_module get_module(const std::uint64_t geometryID,
const traccc::geometry* geom,
const traccc::digitization_config* dconfig,
const std::uint64_t originalGeometryID) {
traccc::cell_module result;
result.surface_link = detray::geometry::barcode{geometryID};

// Find/set the 3D position of the detector module.
if (geom != nullptr) {
// Check if the module ID is known.
if (!geom->contains(result.surface_link.value())) {
throw std::runtime_error("Could not find placement for geometry ID " +
std::to_string(result.surface_link.value()));
}

// Set the value on the module description.
result.placement = (*geom)[result.surface_link.value()];
}

// Find/set the digitization configuration of the detector module.
if (dconfig != nullptr) {
// Check if the module ID is known.
const traccc::digitization_config::Iterator geoIt =
dconfig->find(originalGeometryID);
if (geoIt == dconfig->end()) {
throw std::runtime_error(
"Could not find digitization config for geometry ID " +
std::to_string(originalGeometryID));
}

// Set the value on the module description.
const auto& binning_data = geoIt->segmentation.binningData();
assert(binning_data.size() > 0);
result.pixel.min_corner_x = binning_data[0].min;
result.pixel.pitch_x = binning_data[0].step;
if (binning_data.size() > 1) {
result.pixel.min_corner_y = binning_data[1].min;
result.pixel.pitch_y = binning_data[1].step;
}
// result.pixel.dimension = geoIt->dimensions;
// result.pixel.variance_y = geoIt->variance_y;
}

return result;
}

} // namespace Acts::TracccPlugin::Detail
Loading

0 comments on commit 90eb609

Please sign in to comment.