CMS 3D CMS Logo

DDNamespace.cc
Go to the documentation of this file.
3 #include "DD4hep/Path.h"
4 #include "DD4hep/Printout.h"
5 #include "XML/XML.h"
6 
7 #include <TClass.h>
8 #include "tbb/concurrent_unordered_map.h"
9 #include "tbb/concurrent_vector.h"
10 
11 using namespace std;
12 using namespace cms;
13 
14 DDNamespace::DDNamespace(DDParsingContext* context, xml_h element) : m_context(context) {
15  dd4hep::Path path(xml_handler_t::system_path(element));
16  m_name = path.filename().substr(0, path.filename().rfind('.'));
17  if (!m_name.empty())
19  m_context->namespaces.emplace(m_name);
20  m_pop = true;
21  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
22  "DD4CMS",
23  "+++ Current namespace is now: %s",
24  m_name.c_str());
25  return;
26 }
27 
28 DDNamespace::DDNamespace(DDParsingContext& ctx, xml_h element, bool) : m_context(&ctx) {
29  dd4hep::Path path(xml_handler_t::system_path(element));
30  m_name = path.filename().substr(0, path.filename().rfind('.'));
31  if (!m_name.empty())
33  m_context->namespaces.emplace(m_name);
34  m_pop = true;
35  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
36  "DD4CMS",
37  "+++ Current namespace is now: %s",
38  m_name.c_str());
39  return;
40 }
41 
43  if (!m_context->ns(m_name))
44  m_name.clear();
45 }
46 
48  if (!m_context->ns(m_name))
49  m_name.clear();
50 }
51 
53  if (m_pop) {
54  string result("");
55  if (m_context->namespaces.try_pop(result))
56  m_name = result;
57  else
58  m_name.clear();
59  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
60  "DD4CMS",
61  "+++ Current namespace is now: %s",
62  m_name.c_str());
63  }
64 }
65 
66 string DDNamespace::prepend(const string& n) const {
67  if (strchr(n.c_str(), NAMESPACE_SEP) == nullptr)
68  return m_name + n;
69  else
70  return n;
71 }
72 
73 string DDNamespace::realName(const string& v) const {
74  size_t idx, idq, idp;
75  string val = v;
76  while ((idx = val.find('[')) != string::npos) {
77  val.erase(idx, 1);
78  idp = val.find(NAMESPACE_SEP, idx);
79  idq = val.find(']', idx);
80  val.erase(idq, 1);
81  if (idp == string::npos || idp > idq)
82  val.insert(idx, m_name);
83  else if (idp != string::npos && idp < idq)
84  val[idp] = NAMESPACE_SEP;
85  }
86  return val;
87 }
88 
89 string DDNamespace::nsName(const string& nam) {
90  size_t idx;
91  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos)
92  return nam.substr(0, idx);
93  return "";
94 }
95 
96 string DDNamespace::objName(const string& nam) {
97  size_t idx;
98  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos)
99  return nam.substr(idx + 1);
100  return "";
101 }
102 
103 void DDNamespace::addConstant(const string& nam, const string& val, const string& typ) const {
104  addConstantNS(prepend(nam), val, typ);
105 }
106 
107 void DDNamespace::addConstantNS(const string& nam, const string& val, const string& typ) const {
108  const string& v = val;
109  const string& n = nam;
110  dd4hep::printout(m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
111  "DD4CMS",
112  "+++ Add constant object: %-40s = %s [type:%s]",
113  n.c_str(),
114  v.c_str(),
115  typ.c_str());
116  dd4hep::_toDictionary(n, v, typ);
117  dd4hep::Constant c(n, v, typ);
118  m_context->description.load()->addConstant(c);
119 }
120 
122  return m_context->description.load()->material(realName(name));
123 }
124 
125 void DDNamespace::addRotation(const string& name, const dd4hep::Rotation3D& rot) const {
126  string n = prepend(name);
127  m_context->rotations[n] = rot;
128 }
129 
130 const dd4hep::Rotation3D& DDNamespace::rotation(const string& nam) const {
131  static const dd4hep::Rotation3D s_null;
132  size_t idx;
133  auto i = m_context->rotations.find(nam);
134  if (i != m_context->rotations.end())
135  return (*i).second;
136  else if (nam == "NULL")
137  return s_null;
138  else if (nam.find(":NULL") == nam.length() - 5)
139  return s_null;
140  string n = nam;
141  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos) {
142  n[idx] = NAMESPACE_SEP;
143  i = m_context->rotations.find(n);
144  if (i != m_context->rotations.end())
145  return (*i).second;
146  }
147  throw runtime_error("Unknown rotation identifier:" + nam);
148 }
149 
151  string n = vol.name();
152  dd4hep::Solid s = vol.solid();
153  dd4hep::Material m = vol.material();
154  vol->SetName(n.c_str());
155  m_context->volumes[n] = vol;
156  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
157  "DD4CMS",
158  "+++ Add volume:%-38s Solid:%-26s[%-16s] Material:%s",
159  vol.name(),
160  s.name(),
161  s.type(),
162  m.name());
163  return vol;
164 }
165 
168  string n = prepend(vol.name());
169  dd4hep::Solid s = vol.solid();
170  dd4hep::Material m = vol.material();
171  //vol->SetName(n.c_str());
172  m_context->volumes[n] = vol;
173  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
174  "DD4CMS",
175  "+++ Add volume:%-38s as [%s] Solid:%-26s[%-16s] Material:%s",
176  vol.name(),
177  n.c_str(),
178  s.name(),
179  s.type(),
180  m.name());
181  return vol;
182 }
183 
184 dd4hep::Volume DDNamespace::volume(const string& name, bool exc) const {
185  auto i = m_context->volumes.find(name);
186  if (i != m_context->volumes.end()) {
187  return (*i).second;
188  }
189  if (name.front() == NAMESPACE_SEP) {
190  i = m_context->volumes.find(name.substr(1, name.size()));
191  if (i != m_context->volumes.end())
192  return (*i).second;
193  }
194  if (exc) {
195  throw runtime_error("Unknown volume identifier:" + name);
196  }
197  return nullptr;
198 }
199 
200 dd4hep::Solid DDNamespace::addSolidNS(const string& name, dd4hep::Solid solid) const {
201  dd4hep::printout(m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG,
202  "DD4CMS",
203  "+++ Add shape of type %s : %s",
204  solid->IsA()->GetName(),
205  name.c_str());
206 
207  auto shape = m_context->shapes.emplace(name, solid.setName(name));
208  if (!shape.second) {
209  m_context->shapes[name] = solid.setName(name);
210  }
211 
212  return solid;
213 }
214 
215 dd4hep::Solid DDNamespace::addSolid(const string& name, dd4hep::Solid sol) const {
216  return addSolidNS(prepend(name), sol);
217 }
218 
219 dd4hep::Solid DDNamespace::solid(const string& nam) const {
220  size_t idx;
221  string n = m_name + nam;
222  auto i = m_context->shapes.find(n);
223  if (i != m_context->shapes.end())
224  return (*i).second;
225  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos) {
226  n = realName(nam);
227  n[idx] = NAMESPACE_SEP;
228  i = m_context->shapes.find(n);
229  if (i != m_context->shapes.end())
230  return (*i).second;
231  }
232  i = m_context->shapes.find(nam);
233  if (i != m_context->shapes.end())
234  return (*i).second;
235  // Register a temporary shape
236  auto tmpShape = m_context->shapes.emplace(nam, dd4hep::Solid(nullptr));
237  return (*tmpShape.first).second;
238 }
239 
240 std::vector<double> DDNamespace::vecDbl(const std::string& name) const {
241  cms::DDVectorsMap* registry = m_context->description.load()->extension<cms::DDVectorsMap>();
242  auto it = registry->find(name);
243  if (it != registry->end()) {
244  std::vector<double> result;
245  for (auto in : it->second)
246  result.emplace_back(in);
247  return result;
248  } else
249  return std::vector<double>();
250 }
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:200
#define DEBUG
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:184
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:150
std::string m_name
Definition: DDNamespace.h:74
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:167
bool const ns(std::string &result)
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:219
std::atomic< dd4hep::Detector * > description
std::string_view name() const
Definition: DDNamespace.h:68
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:107
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:89
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:125
std::string realName(const std::string &) const
Definition: DDNamespace.cc:73
static std::string objName(const std::string &)
Definition: DDNamespace.cc:96
tbb::concurrent_unordered_map< std::string, tbb::concurrent_vector< double >> DDVectorsMap
Definition: DDNamespace.h:14
tbb::concurrent_unordered_map< std::string, dd4hep::Rotation3D > rotations
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:121
tbb::concurrent_unordered_map< std::string, dd4hep::Volume > volumes
std::vector< double > vecDbl(const std::string &name) const
Definition: DDNamespace.cc:240
DDParsingContext * m_context
Definition: DDNamespace.h:73
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:66
dd4hep::Volume Volume
Namespace of DDCMS conversion namespace.
Polynomial< 0 > Constant
Definition: Constant.h:6
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:215
tbb::concurrent_unordered_map< std::string, dd4hep::Solid > shapes
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:130
DDNamespace()=delete
tbb::concurrent_queue< std::string > namespaces
void addConstant(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:103
#define NAMESPACE_SEP
Definition: DDNamespace.h:79