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