ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
detector_builder.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "detray/builders/volume_builder.hpp"
13#include "detray/builders/volume_builder_interface.hpp"
14#include "detray/core/concepts.hpp"
15#include "detray/core/detector.hpp"
16#include "detray/utils/detector_statistics.hpp"
17#include "detray/utils/logging.hpp"
18
19// Vecmem include(s)
20#include <vecmem/memory/memory_resource.hpp>
21
22// System include(s)
23#include <ios>
24#include <memory>
25#include <string>
26#include <string_view>
27#include <vector>
28
29namespace detray {
30
37template <concepts::metadata metadata_t,
38 template <typename> class volume_builder_t = volume_builder,
39 template <typename...> class volume_data_t = std::vector>
40class detector_builder {
41 public:
42 // Cannot use the host::detector type, because the friendship relation with
43 // the volume builders cannot be resolved through the type alias
50
51 DETRAY_HOST detector_builder() { DETRAY_VERBOSE_HOST("New builder created"); }
52
55 DETRAY_HOST void set_name(std::string det_name) {
56 m_detector_name = std::move(det_name);
57 DETRAY_VERBOSE_HOST("Set detector name: " << m_detector_name);
58 }
59
61 DETRAY_HOST std::string_view name() const { return m_detector_name; }
62
68 template <typename... Args>
69 DETRAY_HOST auto new_volume(const volume_id id, Args&&... args)
71 DETRAY_VERBOSE_HOST("Adding new volume: " << m_volumes.size());
72
73 m_volumes.push_back(std::make_unique<volume_builder_t<detector_type>>(
74 id, static_cast<dindex>(m_volumes.size()),
75 std::forward<Args>(args)...));
76
77 return m_volumes.back().get();
78 }
79
81 DETRAY_HOST auto n_volumes() const -> dindex {
82 return static_cast<dindex>(m_volumes.size());
83 }
84
88 DETRAY_HOST bool has_volume(const std::size_t volume_idx) const {
89 return volume_idx < m_volumes.size();
90 }
91
96 template <class builder_t>
97 DETRAY_HOST auto decorate(dindex volume_idx) -> builder_t* {
98 assert(has_volume(volume_idx));
99
100 m_volumes[volume_idx] =
101 std::make_unique<builder_t>(std::move(m_volumes[volume_idx]));
102
103 // Always works, we set it as this type in the line above
104 return dynamic_cast<builder_t*>(m_volumes[volume_idx].get());
105 }
106
110 template <class builder_t>
111 DETRAY_HOST auto decorate(
112 const volume_builder_interface<detector_type>* v_builder) -> builder_t* {
113 assert(v_builder != nullptr);
114
115 return decorate<builder_t>(v_builder->vol_index());
116 }
117
121 DETRAY_HOST
122 auto operator[](dindex volume_idx)
124 return m_volumes[volume_idx].get();
125 }
126
132 DETRAY_HOST
133 auto build(vecmem::memory_resource& resource,
135 DETRAY_INFO_HOST("Building detector: \"" << name() << "\"... ");
136 DETRAY_INFO_HOST("-> type: " << DETRAY_TYPENAME(metadata_t));
137
138 detector_type det{resource};
139
140 DETRAY_VERBOSE_HOST("Have " << m_volumes.size()
141 << " configured volume builders");
142 DETRAY_VERBOSE_HOST("Start building the volumes...");
143 for (auto& vol_builder : m_volumes) {
144 DETRAY_VERBOSE_HOST("-> Build: " << vol_builder->name());
145 vol_builder->build(det, opt);
146 }
147
148 // TODO: Make fully generic for more volume accelerator types
149
150 // TODO: Add sorting, data deduplication etc. here later...
151
152 DETRAY_INFO_HOST("-> Built " << det.volumes().size() << " volumes");
153 DETRAY_INFO_HOST("-> Built " << det.surfaces().size() << " surfaces:");
154 DETRAY_INFO_HOST("--> portals: " << detray::n_portals(det));
155 DETRAY_INFO_HOST("--> sensitives: " << detray::n_sensitives(det));
156 DETRAY_INFO_HOST("--> passives: " << detray::n_passives(det));
157
159 DETRAY_INFO_HOST("-> Built " << detray::n_surface_grids(det)
160 << " surface grids");
161 }
162
164 DETRAY_INFO_HOST("-> Built " << detray::n_material_maps(det)
165 << " material maps");
166 }
167
169 DETRAY_INFO_HOST("-> Built homogeneous material:");
170 DETRAY_INFO_HOST("--> slabs: " << detray::n_material_slabs(det));
171 DETRAY_INFO_HOST("--> rods: " << detray::n_material_rods(det));
172 }
173 DETRAY_INFO_HOST("Detector building complete: " << name());
174
175 return det;
176 }
177
184 DETRAY_HOST
185 auto build(vecmem::memory_resource& resource,
186 const volume_builder_options& opt,
188 DETRAY_VERBOSE_HOST("detray: filling names for detector " << name());
189
190 assert(name_map.empty());
191
192 // By convention the name of the detector is at position 0
193 name_map.set_detector_name(m_detector_name);
194
195 for (auto& vol_builder : m_volumes) {
196 name_map.emplace(vol_builder->vol_index(),
197 std::string{vol_builder->name()});
198 }
199
200 return build(resource, opt);
201 }
202
203 private:
205 std::string m_detector_name{"detray_detector"};
207 volume_data_t<std::unique_ptr<volume_builder_interface<detector_type>>>
208 m_volumes{};
209};
210
211} // namespace detray
The detector definition.
Definition detector.hpp:59
typename metadata::algebra_type algebra_type
Definition detector.hpp:87
auto volumes() const -> const vector_type< volume_type > &
Definition detector.hpp:234
detray::name_map name_map
Definition detector.hpp:101
auto surfaces() const -> const surface_lookup_container &
Definition detector.hpp:286
auto build(vecmem::memory_resource &resource, const volume_builder_options &opt) -> detector_type
Assembles the final detector from the volumes builders and allocates the detector containers with the...
Definition detector_builder.hpp:133
auto n_volumes() const -> dindex
Definition detector_builder.hpp:81
dscalar< algebra_type > scalar_type
Scalar type used for geometry values.
Definition detector_builder.hpp:49
void set_name(std::string det_name)
Set the name of the detector under construction to det_name.
Definition detector_builder.hpp:55
typename detector_type::algebra_type algebra_type
Algebra backend used by the detector geometry.
Definition detector_builder.hpp:47
bool has_volume(const std::size_t volume_idx) const
Definition detector_builder.hpp:88
detail::detector< metadata_t, host_container_types > detector_type
Detector type produced from the configured metadata.
Definition detector_builder.hpp:45
std::string_view name() const
Definition detector_builder.hpp:61
auto new_volume(const volume_id id, Args &&... args) -> volume_builder_interface< detector_type > *
Add a new volume builder that will build a volume of the shape given by id.
Definition detector_builder.hpp:69
auto decorate(dindex volume_idx) -> builder_t *
Decorate a volume builder at position volume_idx with more functionality.
Definition detector_builder.hpp:97
auto operator[](dindex volume_idx) -> volume_builder_interface< detector_type > *
Access a particular volume builder by volume index volume_idx.
Definition detector_builder.hpp:122
auto build(vecmem::memory_resource &resource, const volume_builder_options &opt, typename detector_type::name_map &name_map) -> detector_type
Assembles the final detector and fill an externally provided name map name_map.
Definition detector_builder.hpp:185
auto decorate(const volume_builder_interface< detector_type > *v_builder) -> builder_t *
Decorate a volume builder v_builder with more functionality.
Definition detector_builder.hpp:111
Interface for volume builders (and volume builder decorators).
Definition volume_builder_interface.hpp:66
Provides basic functionality to build detector volumes.
Definition volume_builder.hpp:36
Check for the the presence of homogeneous material types in a detector definition.
Definition concepts.hpp:152
Check for the the presence of material maps in a detector definition.
Definition concepts.hpp:158
Check for the the presence of surface grids in a detector definition.
Definition concepts.hpp:136
Check if a type can be used as a metadata.
Definition concepts.hpp:25
Detray include(s).
Definition array_operators.hpp:18
std::size_t n_material_rods(const detector_t &det)
Definition detector_statistics.hpp:105
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
volume_id
Shape of a detector volume.
Definition geometry.hpp:27
std::size_t n_material_slabs(const detector_t &det)
Definition detector_statistics.hpp:94
std::size_t n_sensitives(const detector_t &det)
Definition detector_statistics.hpp:36
unsigned int dindex
Default index type.
Definition indexing.hpp:18
std::size_t n_passives(const detector_t &det)
Definition detector_statistics.hpp:48
std::size_t n_portals(const detector_t &det)
Definition detector_statistics.hpp:24
std::size_t n_surface_grids(const detector_t &det)
Definition detector_statistics.hpp:81
std::size_t n_material_maps(const detector_t &det)
Definition detector_statistics.hpp:87
Map volume indices to volume names and vice versa.
Definition name_map.hpp:23
void set_detector_name(const std::string_view name)
set the name of the detector
Definition name_map.hpp:26
void emplace(dindex idx, const std::string &name)
emplace a new index <-> name mapping
Definition name_map.hpp:56
bool empty() const
Definition name_map.hpp:36
Options for the volume builder classes.
Definition volume_builder_interface.hpp:29