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

feat: Conversion and chain code in traccc #3656

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions Examples/Algorithms/Traccc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ target_link_libraries(
ActsPluginDetray
)

add_subdirectory(Common)
target_link_libraries(ActsExamplesTraccc INTERFACE ActsExamplesTracccCommon)

add_subdirectory(Host)
target_link_libraries(ActsExamplesTraccc INTERFACE ActsExamplesTracccHost)

install(TARGETS ActsExamplesTraccc LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
28 changes: 28 additions & 0 deletions Examples/Algorithms/Traccc/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
add_library(
ActsExamplesTracccCommon
SHARED
src/Conversion/CellMapConversion.cpp
src/Conversion/DigitizationConversion.cpp
src/Conversion/MeasurementConversion.cpp
src/Conversion/SeedConversion.cpp
src/Conversion/SpacePointConversion.cpp
)

target_include_directories(
ActsExamplesTracccCommon
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_link_libraries(
ActsExamplesTracccCommon
PUBLIC
ActsPluginDetray
ActsPluginTraccc
ActsExamplesFramework
ActsExamplesDigitization
)

install(
TARGETS ActsExamplesTracccCommon
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/Geometry/GeometryIdentifier.hpp"
#include "ActsExamples/EventData/Cluster.hpp"

#include <cstdint>
#include <cstdlib>
#include <map>
#include <vector>

#include "traccc/edm/cell.hpp"

namespace ActsExamples::Traccc::Common::Conversion {
Copy link
Contributor

Choose a reason for hiding this comment

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

That looks quite deeply nested. I think we usually try to keep things flap. But nothing critical.


/// @brief Converts a "geometry ID -> generic cell collection type" map to a "geometry ID -> traccc cell collection" map.
/// @note The function sets the module link of the cells in the output to 0.
/// @return Map from geometry ID to its cell data (as a vector of traccc cell data)
std::map<Acts::GeometryIdentifier, std::vector<traccc::cell>> tracccCellsMap(
const std::map<Acts::GeometryIdentifier, std::vector<Cluster::Cell>>& map);

} // namespace ActsExamples::Traccc::Common::Conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Plugins/Traccc/DigitizationConfig.hpp"
#include "ActsExamples/Digitization/DigitizationConfig.hpp"

namespace ActsExamples::Traccc::Common::Conversion {

/// @brief Creates a traccc digitalization config from an Acts geometry hierarchy map
/// that contains the digitization configuration.
/// @param config the Acts geometry hierarchy map that contains the digitization configuration.
/// @return a traccc digitization config.
Acts::TracccPlugin::DigitizationConfig tracccConfig(
const Acts::GeometryHierarchyMap<DigiComponentsConfig>& config);

} // namespace ActsExamples::Traccc::Common::Conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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/Definitions/Algebra.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/EventData/SourceLink.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/Plugins/Traccc/Detail/AlgebraConversion.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"

#include <cstdint>
#include <cstdlib>
#include <iterator>
#include <memory>
#include <tuple>
#include <variant>
#include <vector>

#include "detray/core/detector.hpp"
#include "detray/tracks/bound_track_parameters.hpp"
#include "traccc/definitions/qualifiers.hpp"
#include "traccc/definitions/track_parametrization.hpp"
#include "traccc/edm/measurement.hpp"
#include "traccc/edm/track_state.hpp"

namespace ActsExamples::Traccc::Common::Conversion {

/// @brief Converts a traccc bound index to an Acts bound index.
/// @param tracccBoundIndex the traccc bound index.
/// @returns an Acts bound index.
Acts::BoundIndices boundIndex(const traccc::bound_indices tracccBoundIndex);

/// @brief Converts traccc measurements to acts measurements.
/// @param detector The detray detector,
/// @param measurements The traccc measurements,
/// @return A vector of Acts bound variant measurements.
/// @note The type IndexSourceLink is used for the measurements' source links.
template <typename detector_t, std::forward_iterator iterator_t>
inline MeasurementContainer convertTracccToActsMeasurements(
const detector_t& detector, iterator_t measurements_begin,
iterator_t measurements_end) {
Comment on lines +46 to +49
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: could be a range instead of begin and end

MeasurementContainer measurementContainer;

for (iterator_t i = measurements_begin; i != measurements_end; ++i) {
Acts::GeometryIdentifier moduleGeoId(
detector.surface((*i).surface_link).source);
Index measurementIdx = static_cast<Index>(measurementContainer.size());
IndexSourceLink idxSourceLink{moduleGeoId, measurementIdx};

Eigen::Matrix<MeasurementContainer::VariableProxy::SubspaceIndex,
Eigen::Dynamic, 1>
indices(2);

for (unsigned int j = 0; j < 2; j++) {
indices[j] =
boundIndex(traccc::bound_indices((*i).subs.get_indices()[j]));
}

measurementContainer.emplaceMeasurement<2>(
Acts::SourceLink{idxSourceLink}, indices,
Acts::TracccPlugin::detail::toActsVector<2>((*i).local),
Eigen::DiagonalMatrix<Acts::ActsScalar, 2>(
Acts::TracccPlugin::detail::toActsVector<2>((*i).variance))
.toDenseMatrix());
}

return measurementContainer;
}

} // namespace ActsExamples::Traccc::Common::Conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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/Geometry/GeometryIdentifier.hpp"
#include "ActsExamples/EventData/Cluster.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"

#include <cstdint>
#include <cstdlib>
#include <map>
#include <vector>

#include "traccc/edm/cell.hpp"
#include "traccc/edm/measurement.hpp"

namespace ActsExamples::Traccc::Common::Conversion {

namespace {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it intentional that everything lives in an anonymous namespace here?

template <typename C1, typename C2, typename M1GeoIdProj, typename M2GeoIdProj,
typename M1LocalProj, typename M2LocalProj>
std::map<std::size_t, std::size_t> makeGenericMeasurementIndexMap(
const C1& fromMeasurements, const C2& toMeasurements,
M1GeoIdProj&& fromMeasurementGeoIdProjector,
M2GeoIdProj&& toMeasurementGeoIdProjector,
M1LocalProj&& fromMeasurementLocalProjector,
M2LocalProj&& toMeasurementLocalProjector, Acts::ActsScalar tol = 0.001) {
std::map<std::size_t, std::size_t> outputMap;

std::map<std::size_t, std::vector<std::size_t>> geometryMap;

for (auto i = toMeasurements.cbegin(); i != toMeasurements.cend(); ++i) {
std::size_t key = toMeasurementGeoIdProjector(*i);

geometryMap[key].push_back(std::distance(toMeasurements.cbegin(), i));
}

auto toProjectX = [&toMeasurementLocalProjector,
&toMeasurements](const std::size_t& i) {
return toMeasurementLocalProjector(toMeasurements.at(i)).first;
};

for (auto& i : geometryMap) {
std::ranges::sort(i.second, std::less{}, toProjectX);
}

for (auto i = fromMeasurements.cbegin(); i != fromMeasurements.cend(); ++i) {
std::size_t key = fromMeasurementGeoIdProjector(*i);

const std::vector<std::size_t>& v = geometryMap[key];

auto targetX = fromMeasurementLocalProjector(*i).first;

auto lo =
std::ranges::lower_bound(v, targetX - tol, std::less{}, toProjectX);
auto hi =
std::ranges::upper_bound(v, targetX + tol, std::less{}, toProjectX);

bool found = false;

for (auto j = lo; j != hi && !found; ++j) {
decltype(auto) c = toMeasurements.at(*j);

if (std::abs(toMeasurementLocalProjector(c).first -
fromMeasurementLocalProjector(*i).first) <= tol &&
std::abs(toMeasurementLocalProjector(c).second -
fromMeasurementLocalProjector(*i).second) <= tol) {
outputMap[std::distance(fromMeasurements.cbegin(), i)] = *j;
found = true;
}
}
}

return outputMap;
}

template <typename detector_t>
std::map<std::size_t, std::size_t> makeTracccToActsMeasurementIndexMap(
const std::pmr::vector<traccc::measurement>& tracccMeasurements,
const ActsExamples::MeasurementContainer& actsMeasurements,
const detector_t& detector) {
return makeGenericMeasurementIndexMap(
tracccMeasurements, actsMeasurements,
[&detector](const traccc::measurement& i) {
return detector.surface(i.surface_link).source;
},
[](const ActsExamples::MeasurementContainer::ConstVariableProxy& i) {
return i.sourceLink()
.template get<ActsExamples::IndexSourceLink>()
.geometryId()
.value();
},
[](const traccc::measurement& i) {
return std::make_pair(i.local[0], i.local[1]);
},
[](const ActsExamples::MeasurementContainer::ConstVariableProxy& i) {
return std::make_pair(i.fullParameters()[0], i.fullParameters()[1]);
});
}

template <typename detector_t>
std::map<std::size_t, std::size_t> makeActsToTracccMeasurementIndexMap(
const ActsExamples::MeasurementContainer& actsMeasurements,
const std::pmr::vector<traccc::measurement>& tracccMeasurements,
const detector_t& detector) {
return makeGenericMeasurementIndexMap(
actsMeasurements, tracccMeasurements,
[](const ActsExamples::MeasurementContainer::ConstVariableProxy& i) {
return i.sourceLink()
.template get<ActsExamples::IndexSourceLink>()
.geometryId()
.value();
},
[&detector](const traccc::measurement& i) {
return detector.surface(i.surface_link).source;
},
[](const ActsExamples::MeasurementContainer::ConstVariableProxy& i) {
return std::make_pair(i.fullParameters()[0], i.fullParameters()[1]);
},
[](const traccc::measurement& i) {
return std::make_pair(i.local[0], i.local[1]);
});
}
} // namespace

} // namespace ActsExamples::Traccc::Common::Conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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/EventData/Seed.hpp"
#include "ActsExamples/EventData/SimSeed.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"

#include <cstdint>
#include <cstdlib>
#include <memory_resource>

#include "traccc/edm/seed.hpp"
#include "traccc/edm/spacepoint.hpp"
#include "vecmem/containers/vector.hpp"

namespace ActsExamples::Traccc::Common::Conversion {

/// @brief Converts a collection of traccc seeds and appends the result to the given outputContainer.
/// @param seeds the traccc seeds
/// @param SpacePointConv the spacepoint ConversionData (traccc space point -> acts space point).
/// @param outputContainer the container to put the converted space points into (an empty container is expected).
/// @returns The seed ConversionData (traccc seed -> acts seed).
SimSeedContainer convertTracccToActsSeeds(
std::vector<traccc::seed, std::pmr::polymorphic_allocator<traccc::seed>>&
seeds,
const std::map<std::size_t, std::size_t>& tracccToActsSpacepointIndexMap,
const std::vector<SimSpacePoint>& actsSpacepoints);

/// @brief Converts a collection of seeds to traccc seeds and appends the result to the given outputContainer.
/// @param seeds theseeds
/// @param SpacePointConv the spacepoint ConversionData (acts space point -> traccc space point).
/// @param outputContainer the container to put the converted space points into (an empty container is expected).
/// @returns The seed ConversionData (acts seed -> traccc seed).
std::vector<traccc::seed, std::pmr::polymorphic_allocator<traccc::seed>>
convertActsToTracccSeeds(
const SimSeedContainer& seeds,
const SimSpacePointContainer& actsSpacepoints,
const std::map<std::size_t, std::size_t>& actsToTracccSpacepointIndexMap);

} // namespace ActsExamples::Traccc::Common::Conversion
Loading
Loading