SHiP Geometry
SHiP experiment geometry implementation using GeoModel.
Loading...
Searching...
No Matches
SubsystemRegistry.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (C) CERN for the benefit of the SHiP Collaboration
3
4#pragma once
5
7
8#include <GeoModelKernel/GeoPhysVol.h> // complete GeoPhysVol/GeoVPhysVol for the macro's upcast
9
10#include <cstdio>
11#include <cstdlib>
12#include <functional>
13#include <map>
14#include <stdexcept>
15#include <string>
16#include <utility>
17#include <vector>
18
19namespace SHiP::geometry {
20
21class Materials;
22
32 std::function<GeoVPhysVol*(Materials&)> build;
33};
34
43inline std::map<std::string, SubsystemInfo>& registry() {
44 static std::map<std::string, SubsystemInfo> instance;
45 return instance;
46}
47
62inline bool registerSubsystem(const SubsystemDescriptor& desc,
63 std::function<GeoVPhysVol*(Materials&)> build) {
64 const auto result =
65 registry().emplace(desc.name, SubsystemInfo{.desc = desc, .build = std::move(build)});
66 if (!result.second) {
67 std::fprintf(stderr,
68 "SHiP::geometry: duplicate subsystem name '%s' registered; "
69 "each subsystem's descriptor() must return a unique name.\n",
70 desc.name);
71 std::abort();
72 }
73 return true;
74}
75
82class UnknownSubsystem : public std::runtime_error {
83 public:
85 explicit UnknownSubsystem(const std::string& name)
86 : std::runtime_error("Unknown subsystem: '" + name + "'"), m_name(name) {}
87
89 [[nodiscard]] const std::string& name() const noexcept { return m_name; }
90
91 private:
92 std::string m_name;
93};
94
95// ── Generic consumers — these name no subsystem ─────────────────────────────
96
111GeoPhysVol* assembleGeometry(const std::vector<std::string>& only = {});
112
123GeoVPhysVol* buildSubsystem(const std::string& name);
124
133std::vector<std::string> subsystemNames();
134
135} // namespace SHiP::geometry
136
151#define REGISTER_SUBSYSTEM(FACTORY) \
152 namespace { \
153 [[maybe_unused]] const bool FACTORY##_registered = ::SHiP::geometry::registerSubsystem( \
154 FACTORY::descriptor(), [](::SHiP::geometry::Materials& materials) -> ::GeoVPhysVol* { \
155 return FACTORY(materials).build(); \
156 }); \
157 } \
158 void FACTORY##_linkAnchor(); \
159 void FACTORY##_linkAnchor() {}
Central material manager for the SHiP detector.
Definition Materials.h:22
UnknownSubsystem(const std::string &name)
Definition SubsystemRegistry.h:85
const std::string & name() const noexcept
Definition SubsystemRegistry.h:89
Definition DetectorBuilder.h:8
GeoVPhysVol * buildSubsystem(const std::string &name)
Build a single subsystem on its own, in its local frame.
Definition DetectorBuilder.cpp:105
bool registerSubsystem(const SubsystemDescriptor &desc, std::function< GeoVPhysVol *(Materials &)> build)
Add a subsystem to the registry.
Definition SubsystemRegistry.h:62
std::map< std::string, SubsystemInfo > & registry()
The global subsystem registry.
Definition SubsystemRegistry.h:43
GeoPhysVol * assembleGeometry(const std::vector< std::string > &only={})
Assemble the world plus a selection of registered subsystems.
Definition DetectorBuilder.cpp:26
std::vector< std::string > subsystemNames()
The names of every registered subsystem, sorted.
Definition DetectorBuilder.cpp:115
A subsystem's self-description: everything the assembler needs.
Definition SubsystemDescriptor.h:20
const char * name
registry key / CLI name, e.g. "Calorimeter"
Definition SubsystemDescriptor.h:21
A registered subsystem: its descriptor plus how to build it.
Definition SubsystemRegistry.h:30
std::function< GeoVPhysVol *(Materials &)> build
builds it in its local frame
Definition SubsystemRegistry.h:32
SubsystemDescriptor desc
name, tree node, id and placement
Definition SubsystemRegistry.h:31