G4OCCT 0.1.0
Geant4 interface to Open CASCADE Technology (OCCT) geometry definitions
Loading...
Searching...
No Matches
exampleDD4hepSimpleDetector.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (C) 2026 G4OCCT Contributors
3
4#include <DD4hep/DetElement.h>
5#include <DD4hep/Detector.h>
6
7#include <dlfcn.h>
8
9#include <filesystem>
10#include <iostream>
11#include <stdexcept>
12#include <string>
13
14#ifndef G4OCCT_DD4HEP_SIMPLE_COMPACT
15#error "G4OCCT_DD4HEP_SIMPLE_COMPACT must be defined by CMake"
16#endif
17#ifndef G4OCCT_DD4HEP_PLUGIN_LIBRARY
18#error "G4OCCT_DD4HEP_PLUGIN_LIBRARY must be defined by CMake"
19#endif
20
21namespace {
22
23void RequireChild(const dd4hep::DetElement::Children& children, const std::string& name) {
24 if (children.find(name) == children.end()) {
25 throw std::runtime_error("Expected detector element not found: " + name);
26 }
27}
28
29void LoadDd4hepPlugin() {
30 void* handle = dlopen(G4OCCT_DD4HEP_PLUGIN_LIBRARY, RTLD_NOW | RTLD_GLOBAL);
31 if (!handle) {
32 throw std::runtime_error("Failed to load DD4hep plugin library '" +
33 std::string{G4OCCT_DD4HEP_PLUGIN_LIBRARY} + "': " + dlerror());
34 }
35}
36
37} // namespace
38
39int main(int argc, char** argv) {
40 const std::string compact_path = argc > 1 ? argv[1] : std::string{G4OCCT_DD4HEP_SIMPLE_COMPACT};
41
42 try {
43 if (!std::filesystem::exists(compact_path)) {
44 throw std::runtime_error("Compact XML not found: " + compact_path);
45 }
46
47 LoadDd4hepPlugin();
48
49 dd4hep::Detector& detector = dd4hep::Detector::getInstance();
50 detector.fromCompact(compact_path);
51
52 const auto& children = detector.world().children();
53 std::size_t support_count = 0;
54 std::size_t sensor_count = 0;
55
56 for (const auto& [name, child] : children) {
57 if (!child.isValid() || !child.placement().isValid()) {
58 throw std::runtime_error("Invalid detector element placement for: " + name);
59 }
60 if (name.rfind("VXDLayer0Support_", 0) == 0) {
61 ++support_count;
62 } else if (name.rfind("VXDLayer0Sensor_", 0) == 0) {
63 ++sensor_count;
64 }
65 }
66
67 RequireChild(children, "VXDLayer0Support_0");
68 RequireChild(children, "VXDLayer0Support_9");
69 RequireChild(children, "VXDLayer0Sensor_0");
70 RequireChild(children, "VXDLayer0Sensor_9");
71
72 if (support_count != 10 || sensor_count != 10) {
73 throw std::runtime_error("Expected 10 support ladders and 10 sensor ladders, got " +
74 std::to_string(support_count) + " support and " +
75 std::to_string(sensor_count) + " sensor");
76 }
77
78 std::cout << "Loaded " << compact_path << " with " << support_count
79 << " STEP support ladders and " << sensor_count
80 << " STEP sensor ladders from the SimpleDetector layer-0 port.\n";
81
82 dd4hep::Detector::destroyInstance();
83 return 0;
84 } catch (const std::exception& ex) {
85 dd4hep::Detector::destroyInstance();
86 std::cerr << "exampleDD4hepSimpleDetector: " << ex.what() << '\n';
87 return 1;
88 }
89}
int main(int argc, char **argv)