Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DD4hepGeometryService.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DD4hepGeometryService.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 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 http://mozilla.org/MPL/2.0/.
8 
10 
14 
15 #include <algorithm>
16 #include <memory>
17 #include <stdexcept>
18 #include <utility>
19 
20 #include <DD4hep/Detector.h>
21 #include <DD4hep/Handle.h>
22 #include <DD4hep/Volumes.h>
23 #include <Parsers/Printout.h>
24 
25 class TGeoNode;
26 
29  : m_cfg(cfg),
30  m_logger{Acts::getDefaultLogger("DD4hepGeometryService", cfg.logLevel)} {
31  if (m_cfg.xmlFileNames.empty()) {
32  throw std::invalid_argument("Missing DD4hep XML filenames");
33  }
34 }
35 
37  if (m_detector != nullptr) {
38  m_detector->destroyInstance();
39  }
40 }
41 
44  switch (m_cfg.dd4hepLogLevel) {
46  dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
47  break;
49  dd4hep::setPrintLevel(dd4hep::PrintLevel::DEBUG);
50  break;
52  dd4hep::setPrintLevel(dd4hep::PrintLevel::INFO);
53  break;
55  dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
56  break;
58  dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
59  break;
61  dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
62  break;
64  dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
65  break;
66  }
67  m_detector = &dd4hep::Detector::getInstance();
68  for (auto& file : m_cfg.xmlFileNames) {
69  m_detector->fromCompact(file.c_str());
70  }
71  m_detector->volumeManager();
72  m_detector->apply("DD4hepVolumeManager", 0, nullptr);
73  m_geometry = m_detector->world();
74 
76 }
77 
78 dd4hep::Detector&
80  if (m_detector == nullptr) {
81  buildDD4hepGeometry();
82  }
83  return *m_detector;
84 }
85 
87  if (!m_geometry) {
88  buildDD4hepGeometry();
89  }
90  return m_geometry;
91 }
92 
94  if (!m_geometry) {
95  buildDD4hepGeometry();
96  }
97  return *m_geometry.placement().ptr();
98 }
99 
102  const Acts::GeometryContext& gctx) {
103  // Set the tracking geometry
104  auto logger = Acts::getDefaultLogger("DD4hepConversion", m_cfg.logLevel);
105  m_trackingGeometry = Acts::convertDD4hepDetector(
106  geometry(), *logger, m_cfg.bTypePhi, m_cfg.bTypeR, m_cfg.bTypeZ,
107  m_cfg.envelopeR, m_cfg.envelopeZ, m_cfg.defaultLayerThickness,
108  m_cfg.sortDetectors, gctx, m_cfg.matDecorator,
109  m_cfg.geometryIdentifierHook);
111 }
112 
113 std::shared_ptr<const Acts::TrackingGeometry>
115  const Acts::GeometryContext& gctx) {
116  if (!m_trackingGeometry) {
117  buildTrackingGeometry(gctx);
118  }
119  return m_trackingGeometry;
120 }
121 
123  std::vector<dd4hep::DetElement>& det) {
124  std::vector<dd4hep::DetElement> tracker;
125  std::vector<dd4hep::DetElement> eCal;
126  std::vector<dd4hep::DetElement> hCal;
127  std::vector<dd4hep::DetElement> muon;
128  for (auto& detElement : det) {
129  std::string detName = detElement.name();
130  if (detName.find("Muon") != std::string::npos) {
131  muon.push_back(detElement);
132  } else if (detName.find("ECal") != std::string::npos) {
133  eCal.push_back(detElement);
134  } else if (detName.find("HCal") != std::string::npos) {
135  hCal.push_back(detElement);
136  } else {
137  tracker.push_back(detElement);
138  }
139  }
140  sort(muon.begin(), muon.end(),
141  [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
142  return (a.id() < b.id());
143  });
144  sort(eCal.begin(), eCal.end(),
145  [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
146  return (a.id() < b.id());
147  });
148  sort(hCal.begin(), hCal.end(),
149  [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
150  return (a.id() < b.id());
151  });
152  sort(tracker.begin(), tracker.end(),
153  [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
154  return (a.id() < b.id());
155  });
156  det.clear();
157  det = tracker;
158 
159  det.insert(det.end(), eCal.begin(), eCal.end());
160  det.insert(det.end(), hCal.begin(), hCal.end());
161  det.insert(det.end(), muon.begin(), muon.end());
162 }