CMS 3D CMS Logo

DDNamespace.cc
Go to the documentation of this file.
4 #include "DD4hep/Path.h"
5 #include "DD4hep/Printout.h"
6 #include "Evaluator/Evaluator.h"
7 #include "XML/XML.h"
8 
9 #include <TClass.h>
10 #include <iomanip>
11 #include <sstream>
12 #include <unordered_map>
13 #include <vector>
14 
15 using namespace std;
16 using namespace cms;
17 
20  static constexpr double roundingVal = 1 << 24;
21  value = (round(value * roundingVal) / roundingVal);
22  // Set -0 to 0
24 }
25 
27  std::string hashVal;
28  for (int row = 0; row <= 2; ++row) {
29  for (int col = 0; col <= 2; ++col) {
30  std::ostringstream numStream;
31  numStream << std::fixed << std::setprecision(7);
32  numStream << roundBinary(rot[(3 * row) + col]);
33  hashVal += numStream.str();
34  }
35  }
36  return (hashVal);
37 }
38 
39 std::string cms::rotation_utils::rotHash(const dd4hep::Rotation3D& rot) {
40  std::string hashVal;
41  std::vector<double> matrix;
42  matrix.assign(9, 0.);
43  rot.GetComponents(matrix.begin());
44  for (double val : matrix) {
45  std::ostringstream numStream;
46  numStream << std::fixed << std::setprecision(7);
47  numStream << roundBinary(val);
48  hashVal += numStream.str();
49  }
50  return (hashVal);
51 }
52 
53 DDNamespace::DDNamespace(DDParsingContext* context, xml_h element) : m_context(context) {
54  dd4hep::Path path(xml_handler_t::system_path(element));
55  m_name = path.filename().substr(0, path.filename().rfind('.'));
56  if (!m_name.empty())
58  m_context->namespaces.emplace_back(m_name);
59  m_pop = true;
60  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
61  "DD4CMS",
62  "+++ Current namespace is now: %s",
63  m_name.c_str());
64 }
65 
66 DDNamespace::DDNamespace(DDParsingContext& ctx, xml_h element, bool) : m_context(&ctx) {
67  dd4hep::Path path(xml_handler_t::system_path(element));
68  m_name = path.filename().substr(0, path.filename().rfind('.'));
69  if (!m_name.empty())
71  m_context->namespaces.emplace_back(m_name);
72  m_pop = true;
73  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
74  "DD4CMS",
75  "+++ Current namespace is now: %s",
76  m_name.c_str());
77 }
78 
80  if (!m_context->namespaces.empty())
81  m_name = m_context->namespaces.back();
82 }
83 
84 DDNamespace::DDNamespace(DDParsingContext& ctx) : m_context(&ctx) {
85  if (!m_context->namespaces.empty())
86  m_name = m_context->namespaces.back();
87 }
88 
90  if (m_pop) {
91  m_context->namespaces.pop_back();
92  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
93  "DD4CMS",
94  "+++ Current namespace is now: %s",
95  m_context->ns().c_str());
96  }
97 }
98 
99 string DDNamespace::prepend(const string& n) const {
100  if (strchr(n.c_str(), NAMESPACE_SEP) == nullptr)
101  return m_name + n;
102  else
103  return n;
104 }
105 
106 string DDNamespace::realName(const string& v) const {
107  size_t idx, idq, idp;
108  string val = v;
109  while ((idx = val.find('[')) != string::npos) {
110  val.erase(idx, 1);
111  idp = val.find(NAMESPACE_SEP, idx);
112  idq = val.find(']', idx);
113  val.erase(idq, 1);
114  if (idp == string::npos || idp > idq)
115  val.insert(idx, m_name);
116  else if (idp != string::npos && idp < idq)
117  val[idp] = NAMESPACE_SEP;
118  }
119  return val;
120 }
121 
122 string DDNamespace::nsName(const string& name) {
123  size_t idx;
124  if ((idx = name.find(NAMESPACE_SEP)) != string::npos)
125  return name.substr(0, idx);
126  return "";
127 }
128 
129 string DDNamespace::objName(const string& name) {
130  size_t idx;
131  if ((idx = name.find(NAMESPACE_SEP)) != string::npos)
132  return name.substr(idx + 1);
133  return "";
134 }
135 
136 void DDNamespace::addConstant(const string& name, const string& val, const string& type) const {
138 }
139 
140 namespace dd4hep {
141  dd4hep::tools::Evaluator& evaluator();
142 }
143 
144 void DDNamespace::addConstantNS(const string& name, const string& val, const string& type) const {
145  const string& v = val;
146  const string& n = name;
147  dd4hep::printout(m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
148  "DD4CMS",
149  "+++ Add constant object: %-40s = %s [type:%s]",
150  n.c_str(),
151  v.c_str(),
152  type.c_str());
153  const dd4hep::tools::Evaluator& eval(dd4hep::evaluator());
154  bool constExists = eval.findVariable(n);
155  dd4hep::printout(
156  m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS", "findVariable result = %d", constExists);
157  if (constExists == false) {
158  // Only add it to the dictionary if it is not yet defined
159  dd4hep::_toDictionary(n, v, type);
160  }
162 
163  m_context->description.addConstant(c);
164 }
165 
167  return m_context->description.material(realName(name));
168 }
169 
170 void DDNamespace::addRotation(const string& name, const dd4hep::Rotation3D& rot) const {
171  string n = prepend(name);
172  m_context->rotations[n] = rot;
173  if (m_context->makePayload) {
174  string hashVal = cms::rotation_utils::rotHash(rot);
175  if (m_context->rotRevMap.find(hashVal) == m_context->rotRevMap.end()) {
176  // Only set a rotation that is not already in the map
177  m_context->rotRevMap[hashVal] = n;
178  }
179  }
180 }
181 
182 const dd4hep::Rotation3D& DDNamespace::rotation(const string& name) const {
183  static const dd4hep::Rotation3D s_null;
184  size_t idx;
185  auto i = m_context->rotations.find(name);
186  if (i != m_context->rotations.end())
187  return (*i).second;
188  else if (name == "NULL")
189  return s_null;
190  else if (name.find(":NULL") == name.length() - 5)
191  return s_null;
192  string n = name;
193  if ((idx = name.find(NAMESPACE_SEP)) != string::npos) {
194  n[idx] = NAMESPACE_SEP;
195  i = m_context->rotations.find(n);
196  if (i != m_context->rotations.end())
197  return (*i).second;
198  }
199  throw runtime_error("Unknown rotation identifier:" + name);
200 }
201 
203  string n = prepend(vol.name());
204  dd4hep::Solid s = vol.solid();
205  dd4hep::Material m = vol.material();
206  vol->SetName(n.c_str());
207  m_context->volumes[n] = vol;
208  const char* solidName = "Invalid solid";
209  if (s.isValid()) // Protect against seg fault
210  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
211  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
212  "DD4CMS",
213  "+++ Add volumeNS:%-38s Solid:%-26s[%-16s] Material:%s",
214  vol.name(),
215  solidName,
216  s.type(),
217  m.name());
218  return vol;
219 }
220 
222  string n = prepend(vol.name());
223  dd4hep::Solid s = vol.solid();
224  dd4hep::Material m = vol.material();
225  m_context->volumes[n] = vol;
226  const char* solidName = "Invalid solid";
227  if (s.isValid()) // Protect against seg fault
228  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
229  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
230  "DD4CMS",
231  "+++ Add volume:%-38s as [%s] Solid:%-26s[%-16s] Material:%s",
232  vol.name(),
233  n.c_str(),
234  solidName,
235  s.type(),
236  m.name());
237  return vol;
238 }
239 
240 dd4hep::Assembly DDNamespace::addAssembly(dd4hep::Assembly assembly, bool addSolid) const {
241  string n = assembly.name();
243  if (addSolid) { // In algorithms, Assembly solids are not added separately, so add it here
244  m_context->assemblySolids.emplace(n);
245  }
246  dd4hep::printout(
247  m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS", "+++ Add assembly: %-38s", n.c_str());
248  return assembly;
249 }
250 
251 dd4hep::Assembly DDNamespace::addAssemblySolid(dd4hep::Assembly assembly) const {
252  string n = prepend(assembly.name());
253  m_context->assemblySolids.emplace(n);
254  dd4hep::printout(
255  m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS", "+++ Add assembly solid: %-38s", n.c_str());
256  return assembly;
257 }
258 
259 dd4hep::Assembly DDNamespace::assembly(const std::string& name, bool exception) const {
260  auto i = m_context->assemblies.find(name);
261  if (i != m_context->assemblies.end()) {
262  return (*i).second;
263  }
264  if (name.front() == NAMESPACE_SEP) {
265  i = m_context->assemblies.find(name.substr(1, name.size()));
266  if (i != m_context->assemblies.end())
267  return (*i).second;
268  }
269  if (exception) {
270  throw runtime_error("Unknown assembly identifier: " + name);
271  }
272  dd4hep::Volume nullVol(nullptr);
273  return nullVol;
274 }
275 
276 dd4hep::Volume DDNamespace::volume(const string& name, bool exc) const {
277  auto i = m_context->volumes.find(name);
278  if (i != m_context->volumes.end()) {
279  return (*i).second;
280  }
281  if (name.front() == NAMESPACE_SEP) {
282  i = m_context->volumes.find(name.substr(1, name.size()));
283  if (i != m_context->volumes.end())
284  return (*i).second;
285  }
286  if (exc) {
287  throw runtime_error("Unknown volume identifier:" + name);
288  }
289  return nullptr;
290 }
291 
292 dd4hep::Solid DDNamespace::addSolidNS(const string& name, dd4hep::Solid solid) const {
293  dd4hep::printout(m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG,
294  "DD4CMS",
295  "+++ Add shape of type %s : %s",
296  solid->IsA()->GetName(),
297  name.c_str());
298 
299  auto shape = m_context->shapes.emplace(name, solid.setName(name));
300  if (!shape.second) {
301  m_context->shapes[name] = solid.setName(name);
302  }
303 
304  return solid;
305 }
306 
307 dd4hep::Solid DDNamespace::addSolid(const string& name, dd4hep::Solid sol) const {
308  return addSolidNS(prepend(name), sol);
309 }
310 
311 dd4hep::Solid DDNamespace::solid(const string& nam) const {
312  size_t idx;
313  string n = m_name + nam;
314  auto i = m_context->shapes.find(n);
315  if (i != m_context->shapes.end())
316  return (*i).second;
317  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos) {
318  n = realName(nam);
319  n[idx] = NAMESPACE_SEP;
320  i = m_context->shapes.find(n);
321  if (i != m_context->shapes.end())
322  return (*i).second;
323  }
324  i = m_context->shapes.find(nam);
325  if (i != m_context->shapes.end())
326  return (*i).second;
327  // Register a temporary shape
328  auto tmpShape = m_context->shapes.emplace(nam, dd4hep::Solid(nullptr));
329  return (*tmpShape.first).second;
330 }
331 
332 std::vector<double> DDNamespace::vecDbl(const std::string& name) const {
333  cms::DDVectorsMap* registry = m_context->description.extension<cms::DDVectorsMap>();
334  auto it = registry->find(name);
335  if (it != registry->end()) {
336  return {begin(it->second), end(it->second)};
337  } else
338  return std::vector<double>();
339 }
340 
341 std::vector<float> DDNamespace::vecFloat(const std::string& name) const {
342  cms::DDVectorsMap* registry = m_context->description.extension<cms::DDVectorsMap>();
343  auto it = registry->find(name);
344  if (it != registry->end()) {
345  std::vector<float> result;
347  begin(it->second), end(it->second), std::back_inserter(result), [](double i) -> float { return (float)i; });
348  return result;
349  } else
350  return std::vector<float>();
351 }
352 
355  auto n = result.find(':');
356  if (n == std::string::npos) {
357  return result;
358  } else {
359  return result.substr(n + 1);
360  }
361 }
std::string rotHash(const Double_t *rot)
Definition: DDNamespace.cc:26
std::unordered_map< std::string, dd4hep::Volume > volumes
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:182
const std::string & ns() const
dd4hep::Assembly addAssembly(dd4hep::Assembly asmb, bool addSolid=true) const
Definition: DDNamespace.cc:240
std::unordered_map< std::string, dd4hep::Rotation3D > rotations
std::string realName(const std::string &) const
Definition: DDNamespace.cc:106
std::string m_name
Definition: DDNamespace.h:87
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:144
std::unordered_map< std::string, std::string > rotRevMap
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:166
std::string noNamespace(const std::string &) const
Definition: DDNamespace.cc:353
std::unordered_map< std::string, std::vector< double > > DDVectorsMap
Definition: DDNamespace.h:20
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:122
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:170
std::vector< float > vecFloat(const std::string &name) const
Definition: DDNamespace.cc:341
std::string_view name() const
Definition: DDNamespace.h:79
static std::string objName(const std::string &)
Definition: DDNamespace.cc:129
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Definition: DDNamespace.cc:221
std::unordered_map< std::string, dd4hep::Solid > shapes
Definition: value.py:1
DDParsingContext * m_context
Definition: DDNamespace.h:86
constexpr valType roundIfNear0(valType value, double tolerance=1.e-7)
Definition: Rounding.h:11
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:311
dd4hep::Volume Volume
Namespace of DDCMS conversion namespace.
Polynomial< 0 > Constant
Definition: Constant.h:6
std::vector< std::string > namespaces
double roundBinary(double value)
Definition: DDNamespace.cc:18
constexpr float sol
Definition: Config.h:48
dd4hep::Assembly addAssemblySolid(dd4hep::Assembly assembly) const
Definition: DDNamespace.cc:251
std::unordered_map< std::string, dd4hep::Assembly > assemblies
#define DEBUG
Definition: DMRChecker.cc:120
col
Definition: cuy.py:1009
dd4hep::Detector & description
void addConstant(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:136
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:307
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:202
DDNamespace()=delete
std::unordered_set< std::string > assemblySolids
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:276
std::vector< double > vecDbl(const std::string &name) const
Definition: DDNamespace.cc:332
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:292
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:99
#define NAMESPACE_SEP
Definition: DDNamespace.h:92
dd4hep::tools::Evaluator & evaluator()
dd4hep::Assembly assembly(const std::string &name, bool exception=true) const
Definition: DDNamespace.cc:259
unsigned transform(const HcalDetId &id, unsigned transformCode)