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 <unordered_map>
9 #include <vector>
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_back(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 }
26 
27 DDNamespace::DDNamespace(DDParsingContext& ctx, xml_h element, bool) : m_context(&ctx) {
28  dd4hep::Path path(xml_handler_t::system_path(element));
29  m_name = path.filename().substr(0, path.filename().rfind('.'));
30  if (!m_name.empty())
32  m_context->namespaces.emplace_back(m_name);
33  m_pop = true;
34  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
35  "DD4CMS",
36  "+++ Current namespace is now: %s",
37  m_name.c_str());
38 }
39 
41  if (!m_context->namespaces.empty())
42  m_name = m_context->namespaces.back();
43 }
44 
45 DDNamespace::DDNamespace(DDParsingContext& ctx) : m_context(&ctx) {
46  if (!m_context->namespaces.empty())
47  m_name = m_context->namespaces.back();
48 }
49 
51  if (m_pop) {
52  m_context->namespaces.pop_back();
53  dd4hep::printout(m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
54  "DD4CMS",
55  "+++ Current namespace is now: %s",
56  m_context->ns().c_str());
57  }
58 }
59 
60 string DDNamespace::prepend(const string& n) const {
61  if (strchr(n.c_str(), NAMESPACE_SEP) == nullptr)
62  return m_name + n;
63  else
64  return n;
65 }
66 
67 string DDNamespace::realName(const string& v) const {
68  size_t idx, idq, idp;
69  string val = v;
70  while ((idx = val.find('[')) != string::npos) {
71  val.erase(idx, 1);
72  idp = val.find(NAMESPACE_SEP, idx);
73  idq = val.find(']', idx);
74  val.erase(idq, 1);
75  if (idp == string::npos || idp > idq)
76  val.insert(idx, m_name);
77  else if (idp != string::npos && idp < idq)
78  val[idp] = NAMESPACE_SEP;
79  }
80  return val;
81 }
82 
83 string DDNamespace::nsName(const string& name) {
84  size_t idx;
85  if ((idx = name.find(NAMESPACE_SEP)) != string::npos)
86  return name.substr(0, idx);
87  return "";
88 }
89 
90 string DDNamespace::objName(const string& name) {
91  size_t idx;
92  if ((idx = name.find(NAMESPACE_SEP)) != string::npos)
93  return name.substr(idx + 1);
94  return "";
95 }
96 
97 void DDNamespace::addConstant(const string& name, const string& val, const string& type) const {
99 }
100 
101 void DDNamespace::addConstantNS(const string& name, const string& val, const string& type) const {
102  const string& v = val;
103  const string& n = name;
104  dd4hep::printout(m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
105  "DD4CMS",
106  "+++ Add constant object: %-40s = %s [type:%s]",
107  n.c_str(),
108  v.c_str(),
109  type.c_str());
110  dd4hep::_toDictionary(n, v, type);
112 
113  m_context->description.addConstant(c);
114 }
115 
117  return m_context->description.material(realName(name));
118 }
119 
120 void DDNamespace::addRotation(const string& name, const dd4hep::Rotation3D& rot) const {
121  string n = prepend(name);
122  m_context->rotations[n] = rot;
123 }
124 
125 const dd4hep::Rotation3D& DDNamespace::rotation(const string& name) const {
126  static const dd4hep::Rotation3D s_null;
127  size_t idx;
128  auto i = m_context->rotations.find(name);
129  if (i != m_context->rotations.end())
130  return (*i).second;
131  else if (name == "NULL")
132  return s_null;
133  else if (name.find(":NULL") == name.length() - 5)
134  return s_null;
135  string n = name;
136  if ((idx = name.find(NAMESPACE_SEP)) != string::npos) {
137  n[idx] = NAMESPACE_SEP;
138  i = m_context->rotations.find(n);
139  if (i != m_context->rotations.end())
140  return (*i).second;
141  }
142  throw runtime_error("Unknown rotation identifier:" + name);
143 }
144 
146  string n = prepend(vol.name());
147  dd4hep::Solid s = vol.solid();
148  dd4hep::Material m = vol.material();
149  vol->SetName(n.c_str());
150  m_context->volumes[n] = vol;
151  const char* solidName = "Invalid solid";
152  if (s.isValid()) // Protect against seg fault
153  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
154  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
155  "DD4CMS",
156  "+++ Add volumeNS:%-38s Solid:%-26s[%-16s] Material:%s",
157  vol.name(),
158  solidName,
159  s.type(),
160  m.name());
161  return vol;
162 }
163 
166  string n = prepend(vol.name());
167  dd4hep::Solid s = vol.solid();
168  dd4hep::Material m = vol.material();
169  m_context->volumes[n] = vol;
170  const char* solidName = "Invalid solid";
171  if (s.isValid()) // Protect against seg fault
172  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
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  solidName,
179  s.type(),
180  m.name());
181  return vol;
182 }
183 
184 dd4hep::Assembly DDNamespace::addAssembly(dd4hep::Assembly assembly) const {
185  string n = assembly.name();
187  dd4hep::printout(
188  m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS", "+++ Add assembly:%-38s", assembly.name());
189  return assembly;
190 }
191 
192 dd4hep::Assembly DDNamespace::assembly(const std::string& name) const {
193  auto i = m_context->assemblies.find(name);
194  if (i != m_context->assemblies.end()) {
195  return (*i).second;
196  }
197  if (name.front() == NAMESPACE_SEP) {
198  i = m_context->assemblies.find(name.substr(1, name.size()));
199  if (i != m_context->assemblies.end())
200  return (*i).second;
201  }
202  throw runtime_error("Unknown assembly identifier:" + name);
203 }
204 
205 dd4hep::Volume DDNamespace::volume(const string& name, bool exc) const {
206  auto i = m_context->volumes.find(name);
207  if (i != m_context->volumes.end()) {
208  return (*i).second;
209  }
210  if (name.front() == NAMESPACE_SEP) {
211  i = m_context->volumes.find(name.substr(1, name.size()));
212  if (i != m_context->volumes.end())
213  return (*i).second;
214  }
215  if (exc) {
216  throw runtime_error("Unknown volume identifier:" + name);
217  }
218  return nullptr;
219 }
220 
221 dd4hep::Solid DDNamespace::addSolidNS(const string& name, dd4hep::Solid solid) const {
222  dd4hep::printout(m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG,
223  "DD4CMS",
224  "+++ Add shape of type %s : %s",
225  solid->IsA()->GetName(),
226  name.c_str());
227 
228  auto shape = m_context->shapes.emplace(name, solid.setName(name));
229  if (!shape.second) {
230  m_context->shapes[name] = solid.setName(name);
231  }
232 
233  return solid;
234 }
235 
236 dd4hep::Solid DDNamespace::addSolid(const string& name, dd4hep::Solid sol) const {
237  return addSolidNS(prepend(name), sol);
238 }
239 
240 dd4hep::Solid DDNamespace::solid(const string& nam) const {
241  size_t idx;
242  string n = m_name + nam;
243  auto i = m_context->shapes.find(n);
244  if (i != m_context->shapes.end())
245  return (*i).second;
246  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos) {
247  n = realName(nam);
248  n[idx] = NAMESPACE_SEP;
249  i = m_context->shapes.find(n);
250  if (i != m_context->shapes.end())
251  return (*i).second;
252  }
253  i = m_context->shapes.find(nam);
254  if (i != m_context->shapes.end())
255  return (*i).second;
256  // Register a temporary shape
257  auto tmpShape = m_context->shapes.emplace(nam, dd4hep::Solid(nullptr));
258  return (*tmpShape.first).second;
259 }
260 
261 std::vector<double> DDNamespace::vecDbl(const std::string& name) const {
262  cms::DDVectorsMap* registry = m_context->description.extension<cms::DDVectorsMap>();
263  auto it = registry->find(name);
264  if (it != registry->end()) {
265  return {begin(it->second), end(it->second)};
266  } else
267  return std::vector<double>();
268 }
269 
270 std::vector<float> DDNamespace::vecFloat(const std::string& name) const {
271  cms::DDVectorsMap* registry = m_context->description.extension<cms::DDVectorsMap>();
272  auto it = registry->find(name);
273  if (it != registry->end()) {
274  std::vector<float> result;
276  begin(it->second), end(it->second), std::back_inserter(result), [](double i) -> float { return (float)i; });
277  return result;
278  } else
279  return std::vector<float>();
280 }
281 
284  auto n = result.find(':');
285  if (n == std::string::npos) {
286  return result;
287  } else {
288  return result.substr(n + 1);
289  }
290 }
cms::DDParsingContext::debug_namespaces
bool debug_namespaces
Definition: DDParsingContext.h:64
cms::DDNamespace::~DDNamespace
~DDNamespace()
Definition: DDNamespace.cc:50
cms::DDNamespace::realName
std::string realName(const std::string &) const
Definition: DDNamespace.cc:67
mps_fire.i
i
Definition: mps_fire.py:428
cms::DDNamespace::m_context
DDParsingContext * m_context
Definition: DDNamespace.h:79
g4SimHits_cfi.Material
Material
Definition: g4SimHits_cfi.py:582
cms::DDNamespace::addRotation
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:120
cms::DDNamespace::addConstant
void addConstant(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:97
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:240
cms::DDNamespace::material
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:116
cms::DDNamespace::noNamespace
std::string noNamespace(const std::string &) const
Definition: DDNamespace.cc:282
DDParsingContext.h
cms::DDNamespace::vecFloat
std::vector< float > vecFloat(const std::string &name) const
Definition: DDNamespace.cc:270
cms::DDNamespace::m_pop
bool m_pop
Definition: DDNamespace.h:81
cms::DDParsingContext
Definition: DDParsingContext.h:13
if
if(0==first)
Definition: CAHitNtupletGeneratorKernelsImpl.h:48
cms::DDNamespace::addConstantNS
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:101
cms::DDParsingContext::ns
const std::string & ns() const
Definition: DDParsingContext.h:33
findQualityFiles.v
v
Definition: findQualityFiles.py:179
cms::DDNamespace::objName
static std::string objName(const std::string &)
Definition: DDNamespace.cc:90
cms::DDParsingContext::rotations
std::unordered_map< std::string, dd4hep::Rotation3D > rotations
Definition: DDParsingContext.h:71
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
cms::DDNamespace::assembly
dd4hep::Assembly assembly(const std::string &name) const
Definition: DDNamespace.cc:192
alignCSCRings.s
s
Definition: alignCSCRings.py:92
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
mps_fire.end
end
Definition: mps_fire.py:242
cms::DDNamespace::prepend
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:60
cms::DDNamespace::addAssembly
dd4hep::Assembly addAssembly(dd4hep::Assembly asmb) const
Definition: DDNamespace.cc:184
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:125
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
newFWLiteAna.fullName
fullName
Definition: newFWLiteAna.py:122
cms::DDParsingContext::shapes
std::unordered_map< std::string, dd4hep::Solid > shapes
Definition: DDParsingContext.h:72
cms::Volume
dd4hep::Volume Volume
Definition: DDFilteredView.h:47
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
cms::DDParsingContext::assemblies
std::unordered_map< std::string, dd4hep::Assembly > assemblies
Definition: DDParsingContext.h:70
cms::DDNamespace::addVolume
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:165
cms::DDNamespace::DDNamespace
DDNamespace()=delete
cms::DDNamespace::addSolidNS
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:221
cms::DDNamespace::addSolid
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:236
cms::DDNamespace::addVolumeNS
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:145
cms::DDParsingContext::namespaces
std::vector< std::string > namespaces
Definition: DDParsingContext.h:74
cms::DDNamespace::nsName
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:83
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:58
cms::DDVectorsMap
std::unordered_map< std::string, std::vector< double > > DDVectorsMap
Definition: DDNamespace.h:14
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:261
NAMESPACE_SEP
#define NAMESPACE_SEP
Definition: DDNamespace.h:85
cms::DDNamespace::m_name
std::string m_name
Definition: DDNamespace.h:80
mps_fire.result
result
Definition: mps_fire.py:311
cms::DDParsingContext::volumes
std::unordered_map< std::string, dd4hep::Volume > volumes
Definition: DDParsingContext.h:73
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
cms::DDParsingContext::debug_volumes
bool debug_volumes
Definition: DDParsingContext.h:62
cms::DDNamespace::name
std::string_view name() const
Definition: DDNamespace.h:72
cms::DDParsingContext::debug_shapes
bool debug_shapes
Definition: DDParsingContext.h:61
cms::DDNamespace::volume
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:205
cms::DDParsingContext::description
dd4hep::Detector & description
Definition: DDParsingContext.h:68
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21