Skip to content
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
7 changes: 7 additions & 0 deletions Core/include/Acts/Geometry/TrackingGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ class TrackingGeometry {
/// @retval pointer to the found volume otherwise.
const TrackingVolume* findVolume(GeometryIdentifier id) const;

/// Search for the first volume with the given name.
///
/// @param name is the volume name to search for
/// @retval nullptr if no volume carries the name
/// @retval pointer to the first matching volume otherwise.
const TrackingVolume* findVolumeByName(std::string_view name) const;

/// Search for a surface with the given identifier.
///
/// @param id is the geometry identifier of the surface
Expand Down
11 changes: 11 additions & 0 deletions Core/src/Geometry/TrackingGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ const TrackingVolume* TrackingGeometry::findVolume(
return vol->second;
}

const TrackingVolume* TrackingGeometry::findVolumeByName(
std::string_view name) const {
const TrackingVolume* found = nullptr;
apply([&](const TrackingVolume& volume) {
if (found == nullptr && volume.volumeName() == name) {
found = &volume;
}
});
return found;
}

const Surface* TrackingGeometry::findSurface(GeometryIdentifier id) const {
auto srf = m_surfacesById.find(id);
if (srf == m_surfacesById.end()) {
Expand Down
57 changes: 57 additions & 0 deletions Detray/detectors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,60 @@ if(NOT "${DETRAY_GENERATE_METADATA}" STREQUAL "")
message(STATUS "Creating metadata: ${GENERATOR}")
endforeach()
endif()

# ── Explicit IO instantiations for the array-algebra detectors ──────────────
#
# The heavy detray IO operations (write / read / consistency check / payload
# assembly) are explicitly instantiated for the closed set of shipped metadata
# types bound to the array algebra, one translation unit per metadata, so that
# each `detector<meta<array>>` template tree is compiled exactly once here
# instead of in every consumer. Consumers include detector_io_array.hpp for the
# matching `extern template` declarations and link detray::detector_io_array.
#
# The metadata list mirrors DETRAY_IO_METADATA_FOR_EACH in
# include/detray/detectors/detail/detector_io_instantiations.hpp.
include(detray-compiler-options-cpp)

set(_detray_io_instantiation_metadata
default_metadata
odd_metadata
toy_metadata
telescope_metadata
wire_chamber_metadata
)

set(_detray_io_instantiation_sources)
foreach(META IN LISTS _detray_io_instantiation_metadata)
set(DETRAY_META ${META})
set(_generated
"${CMAKE_CURRENT_BINARY_DIR}/generated/DetrayIoInstantiate_${META}.cpp"
)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/src/DetrayIoInstantiate.cpp.in"
"${_generated}"
@ONLY
)
list(APPEND _detray_io_instantiation_sources "${_generated}")
endforeach()

add_library(detray_detector_io_array STATIC ${_detray_io_instantiation_sources})
add_library(detray::detector_io_array ALIAS detray_detector_io_array)
set_target_properties(
detray_detector_io_array
PROPERTIES EXPORT_NAME detector_io_array
)
target_link_libraries(
detray_detector_io_array
PUBLIC
detray::detectors
detray::io
detray::core_array
detray::algebra_array
vecmem::core
)
add_dependencies(detray_detector_io_array detray_detectors)

install(TARGETS detray_detector_io_array EXPORT detray-exports)

unset(_detray_io_instantiation_metadata)
unset(_detray_io_instantiation_sources)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This file is part of the ACTS project.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should into the IO lib?

//
// 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

/// X-macro over the closed set of shipped detray metadata types for which the
/// heavy IO operations (write / read / consistency check / payload assembly)
/// are explicitly instantiated.
///
/// Invoke with a single-argument macro; it is expanded once per metadata name.
/// This is the single source of truth for the closed set and is used both to
/// generate the `extern template` declarations (see @c detector_io_array.hpp)
/// and the matching explicit instantiations (one translation unit per metadata,
/// see detectors/CMakeLists.txt). The algebra plugin is supplied by the
/// including header, so the same list serves every algebra variant.
///
/// To add a metadata type: add its header and one line here, then add the same
/// name to the CMake instantiation list.
#define DETRAY_IO_METADATA_FOR_EACH(MACRO) \
MACRO(default_metadata) \
MACRO(odd_metadata) \
MACRO(toy_metadata) \
MACRO(telescope_metadata) \
MACRO(wire_chamber_metadata)
80 changes: 80 additions & 0 deletions Detray/detectors/include/detray/detectors/detector_io_array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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

// Extern-template facade for the array-algebra detray detectors.
//
// Including this header opts every shipped `detector<meta<array<float>>>` into
// the pre-built IO operations: the heavy `write_detector`, `read_detector`,
// `check_consistency` and `assemble_detector` template trees are compiled once
// in the `detray::detector_io_array` library and merely linked here, instead of
// being instantiated in every consumer.
//
// This is the array counterpart of the `detray::io` vs `detray::io_array`
// split: the frontend/core headers cannot carry these declarations themselves
// because they live below the `detectors` layer and cannot name the concrete
// metadata types. Consumers that need a non-shipped metadata or a different
// algebra simply include the frontend headers directly and instantiate on use.

// Project include(s)
#include "detray/core/detector.hpp"
#include "detray/definitions/algebra.hpp"
#include "detray/detectors/default_metadata.hpp"
#include "detray/detectors/detail/detector_io_instantiations.hpp"
#include "detray/detectors/odd_metadata.hpp"
#include "detray/detectors/telescope_metadata.hpp"
#include "detray/detectors/toy_metadata.hpp"
#include "detray/detectors/wire_chamber_metadata.hpp"
#include "detray/io/frontend/definitions.hpp"
#include "detray/io/frontend/detector_assembler.hpp"
#include "detray/io/frontend/detector_reader.hpp"
#include "detray/io/frontend/detector_writer.hpp"
#include "detray/io/frontend/payloads.hpp"
#include "detray/utils/consistency_checker.hpp"

// Vecmem include(s)
#include <vecmem/memory/memory_resource.hpp>

// System include(s)
#include <string_view>
#include <utility>

// clang-format off
#define DETRAY_DETECTOR_IO_EXTERN(META) \
extern template void detray::io::write_detector< \
detray::detector<detray::META<detray::array<float>>>>( \
detray::detector<detray::META<detray::array<float>>>&, \
const detray::detector<detray::META<detray::array<float>>>::name_map&, \
detray::io::detector_writer_config&); \
extern template std::pair< \
detray::detector<detray::META<detray::array<float>>>, \
detray::detector<detray::META<detray::array<float>>>::name_map> \
detray::io::read_detector< \
detray::detector<detray::META<detray::array<float>>>>( \
vecmem::memory_resource&, const detray::io::detector_reader_config&); \
extern template bool detray::detail::check_consistency< \
detray::detector<detray::META<detray::array<float>>>>( \
const detray::detector<detray::META<detray::array<float>>>&, bool, \
const detray::detector<detray::META<detray::array<float>>>::name_map&); \
extern template detray::detector<detray::META<detray::array<float>>> \
detray::io::assemble_detector< \
detray::detector<detray::META<detray::array<float>>>>( \
vecmem::memory_resource&, const detray::io::detector_payload&, \
const detray::io::detector_homogeneous_material_payload*, \
detray::io::detector_grids_payload< \
detray::io::surface_material_payload, detray::io::material_id>*, \
const detray::io::detector_grids_payload<std::size_t, \
detray::io::accel_id>*, \
std::string_view, \
detray::detector<detray::META<detray::array<float>>>::name_map&);
// clang-format on

DETRAY_IO_METADATA_FOR_EACH(DETRAY_DETECTOR_IO_EXTERN)

#undef DETRAY_DETECTOR_IO_EXTERN
63 changes: 63 additions & 0 deletions Detray/detectors/src/DetrayIoInstantiate.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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/.

// Generated from DetrayIoInstantiate.cpp.in by CMake — do not edit.
//
// Explicit instantiation of the heavy detray IO operations for a single
// (metadata, array-algebra) detector. One such translation unit is compiled per
// shipped metadata into the detray::detector_io_array library; consumers see
// the matching `extern template` declarations via detector_io_array.hpp.

// Project include(s)
#include "detray/core/detector.hpp"
#include "detray/definitions/algebra.hpp"
#include "detray/detectors/@DETRAY_META@.hpp"
#include "detray/io/frontend/definitions.hpp"
#include "detray/io/frontend/detector_assembler.hpp"
#include "detray/io/frontend/detector_reader.hpp"
#include "detray/io/frontend/detector_writer.hpp"
#include "detray/io/frontend/payloads.hpp"
#include "detray/utils/consistency_checker.hpp"

// Vecmem include(s)
#include <vecmem/memory/memory_resource.hpp>

// System include(s)
#include <string_view>
#include <utility>

namespace detray {

using io_detector_t = detector<@DETRAY_META@<array<float>>>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This needs the detray scalar type, so that we can build double precision detectors in double precision tests


namespace io {

template void write_detector<io_detector_t>(
io_detector_t&, const io_detector_t::name_map&, detector_writer_config&);

template std::pair<io_detector_t, io_detector_t::name_map>
read_detector<io_detector_t>(vecmem::memory_resource&,
const detector_reader_config&);

template io_detector_t assemble_detector<io_detector_t>(
vecmem::memory_resource&, const detector_payload&,
const detector_homogeneous_material_payload*,
detector_grids_payload<surface_material_payload, material_id>*,
const detector_grids_payload<std::size_t, accel_id>*, std::string_view,
io_detector_t::name_map&);

} // namespace io

namespace detail {

template bool check_consistency<io_detector_t>(const io_detector_t&, bool,
const io_detector_t::name_map&);

} // namespace detail

} // namespace detray
89 changes: 89 additions & 0 deletions Detray/io/include/detray/io/frontend/detector_assembler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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

// Project include(s)
#include "detray/builders/detector_builder.hpp"
#include "detray/io/backend/geometry_reader.hpp"
#include "detray/io/backend/homogeneous_material_reader.hpp"
#include "detray/io/backend/material_map_reader.hpp"
#include "detray/io/backend/surface_grid_reader.hpp"
#include "detray/io/frontend/definitions.hpp"
#include "detray/io/frontend/payloads.hpp"

// Vecmem include(s)
#include <vecmem/memory/memory_resource.hpp>

// System include(s)
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

namespace detray::io {

/// @brief Assemble a detray detector from its individual IO payloads.
///
/// This is the payload-driven counterpart of @c read_detector: instead of
/// reading the components from files, it consumes payloads that a client (e.g.
/// the ACTS geometry converter) has produced in memory and drives the detray
/// @c detector_builder to construct the detector. It is a single, extern-able
/// entry point for the otherwise heavy detector-building template tree.
///
/// @tparam detector_t the type of detector to be built
///
/// @param mr the memory resource used for the detector container allocations
/// @param geometry the geometry payload (always required)
/// @param homogeneous_material homogeneous material payload, or @c nullptr to
/// skip homogeneous material
/// @param material_grids material map payload (moved-from), or @c nullptr to
/// skip material maps
/// @param surface_grids surface grid payload, or @c nullptr to skip surface
/// grids
/// @param name detector name to set, or empty to leave the builder default
/// @param names filled with the volume/surface name map during the build
///
/// @returns the assembled detector
template <class detector_t>
detector_t assemble_detector(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should merge with the detector_components_reader which so far only handles file readers, but we could add overloads for e.g. add_geometry_reader that takes a payload and adds a bare reader without file frontend (might need to adjust the internal map for that). And then we add a new function read_components_from_payload that does what read_components_from_file does

vecmem::memory_resource& mr, const detector_payload& geometry,
const detector_homogeneous_material_payload* homogeneous_material,
detector_grids_payload<surface_material_payload, material_id>*
material_grids,
const detector_grids_payload<std::size_t, accel_id>* surface_grids,
std::string_view name, typename detector_t::name_map& names) {
detector_builder<typename detector_t::metadata> det_builder{};

geometry_reader::from_payload<detector_t>(det_builder, geometry);

if (homogeneous_material != nullptr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (homogeneous_material != nullptr) {
if (homogeneous_material != nullptr) {
if constexpr (detray::concepts::has_homogeneous_material<detector_t>) {

There is a concept for every detector component to turn off stuff that the detector type cannot hold

homogeneous_material_reader::from_payload<detector_t>(
det_builder, *homogeneous_material);
}

if (material_grids != nullptr) {
material_map_reader<std::integral_constant<std::size_t, 2>>::from_payload<
detector_t>(det_builder, std::move(*material_grids));
}

if (surface_grids != nullptr) {
surface_grid_reader<typename detector_t::surface_type,
std::integral_constant<std::size_t, 0>,
std::integral_constant<std::size_t, 2>>::
template from_payload<detector_t>(det_builder, *surface_grids);
}

if (!name.empty()) {
det_builder.set_name(std::string{name});
}

return det_builder.build(mr, names);
}

} // namespace detray::io
5 changes: 3 additions & 2 deletions Detray/io/include/detray/io/frontend/detector_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ void read_components_from_file(const std::vector<std::string>& file_names,
/// @returns a complete detector object + a map that contains the volume names
template <class detector_t, std::size_t CAP = 0u, std::size_t DIM = 2u,
template <typename> class volume_builder_t = volume_builder>
auto read_detector(vecmem::memory_resource& resc,
const detector_reader_config& cfg) noexcept(false) {
std::pair<detector_t, typename detector_t::name_map> read_detector(
vecmem::memory_resource& resc,
const detector_reader_config& cfg) noexcept(false) {
// Map the volume names to their indices
typename detector_t::name_map names{};

Expand Down
Loading
Loading