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 
47 DDNamespace::DDNamespace(DDParsingContext& ctx) : m_context(&ctx) {
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  const char* solidName = "Invalid solid";
157  if (s.isValid()) // Protect against seg fault
158  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
159  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
160  "DD4CMS",
161  "+++ Add volumeNS:%-38s Solid:%-26s[%-16s] Material:%s",
162  vol.name(),
163  solidName,
164  s.type(),
165  m.name());
166  return vol;
167 }
168 
171  string n = prepend(vol.name());
172  dd4hep::Solid s = vol.solid();
173  dd4hep::Material m = vol.material();
174  //vol->SetName(n.c_str());
175  m_context->volumes[n] = vol;
176  const char* solidName = "Invalid solid";
177  if (s.isValid()) // Protect against seg fault
178  solidName = s.name(); // If Solid is not valid, s.name() will seg fault.
179  dd4hep::printout(m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG,
180  "DD4CMS",
181  "+++ Add volume:%-38s as [%s] Solid:%-26s[%-16s] Material:%s",
182  vol.name(),
183  n.c_str(),
184  solidName,
185  s.type(),
186  m.name());
187  return vol;
188 }
189 
190 dd4hep::Volume DDNamespace::volume(const string& name, bool exc) const {
191  auto i = m_context->volumes.find(name);
192  if (i != m_context->volumes.end()) {
193  return (*i).second;
194  }
195  if (name.front() == NAMESPACE_SEP) {
196  i = m_context->volumes.find(name.substr(1, name.size()));
197  if (i != m_context->volumes.end())
198  return (*i).second;
199  }
200  if (exc) {
201  throw runtime_error("Unknown volume identifier:" + name);
202  }
203  return nullptr;
204 }
205 
206 dd4hep::Solid DDNamespace::addSolidNS(const string& name, dd4hep::Solid solid) const {
207  dd4hep::printout(m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG,
208  "DD4CMS",
209  "+++ Add shape of type %s : %s",
210  solid->IsA()->GetName(),
211  name.c_str());
212 
213  auto shape = m_context->shapes.emplace(name, solid.setName(name));
214  if (!shape.second) {
215  m_context->shapes[name] = solid.setName(name);
216  }
217 
218  return solid;
219 }
220 
221 dd4hep::Solid DDNamespace::addSolid(const string& name, dd4hep::Solid sol) const {
222  return addSolidNS(prepend(name), sol);
223 }
224 
225 dd4hep::Solid DDNamespace::solid(const string& nam) const {
226  size_t idx;
227  string n = m_name + nam;
228  auto i = m_context->shapes.find(n);
229  if (i != m_context->shapes.end())
230  return (*i).second;
231  if ((idx = nam.find(NAMESPACE_SEP)) != string::npos) {
232  n = realName(nam);
233  n[idx] = NAMESPACE_SEP;
234  i = m_context->shapes.find(n);
235  if (i != m_context->shapes.end())
236  return (*i).second;
237  }
238  i = m_context->shapes.find(nam);
239  if (i != m_context->shapes.end())
240  return (*i).second;
241  // Register a temporary shape
242  auto tmpShape = m_context->shapes.emplace(nam, dd4hep::Solid(nullptr));
243  return (*tmpShape.first).second;
244 }
245 
246 std::vector<double> DDNamespace::vecDbl(const std::string& name) const {
247  cms::DDVectorsMap* registry = m_context->description.load()->extension<cms::DDVectorsMap>();
248  auto it = registry->find(name);
249  if (it != registry->end()) {
250  std::vector<double> result;
251  for (auto in : it->second)
252  result.emplace_back(in);
253  return result;
254  } else
255  return std::vector<double>();
256 }
cms::DDParsingContext::debug_namespaces
bool debug_namespaces
Definition: DDParsingContext.h:83
cms::DDNamespace::~DDNamespace
~DDNamespace()
Definition: DDNamespace.cc:52
cms::DDNamespace::realName
std::string realName(const std::string &) const
Definition: DDNamespace.cc:73
mps_fire.i
i
Definition: mps_fire.py:355
cms::DDNamespace::m_context
DDParsingContext * m_context
Definition: DDNamespace.h:73
g4SimHits_cfi.Material
Material
Definition: g4SimHits_cfi.py:547
cms::DDNamespace::addRotation
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:125
cms::DDNamespace::addConstant
void addConstant(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:103
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:225
cms::DDNamespace::material
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:121
DDParsingContext.h
cms::DDNamespace::m_pop
bool m_pop
Definition: DDNamespace.h:75
cms::DDVectorsMap
tbb::concurrent_unordered_map< std::string, tbb::concurrent_vector< double > > DDVectorsMap
Definition: DDNamespace.h:14
cms::DDParsingContext
Definition: DDParsingContext.h:14
cms::DDNamespace::addConstantNS
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:107
findQualityFiles.v
v
Definition: findQualityFiles.py:179
cms::DDNamespace::objName
static std::string objName(const std::string &)
Definition: DDNamespace.cc:96
training_settings.idx
idx
Definition: training_settings.py:16
cms::DDParsingContext::description
std::atomic< dd4hep::Detector * > description
Definition: DDParsingContext.h:36
alignCSCRings.s
s
Definition: alignCSCRings.py:92
cms::DDParsingContext::volumes
tbb::concurrent_unordered_map< std::string, dd4hep::Volume > volumes
Definition: DDParsingContext.h:39
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
cms::DDParsingContext::rotations
tbb::concurrent_unordered_map< std::string, dd4hep::Rotation3D > rotations
Definition: DDParsingContext.h:37
cms::DDNamespace::prepend
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:66
cms::DDNamespace::rotation
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:130
DEBUG
#define DEBUG
Definition: GeneralPurposeTrackAnalyzer.cc:79
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cms::Volume
dd4hep::Volume Volume
Definition: DDFilteredView.h:45
recoMuon::in
Definition: RecoMuonEnumerators.h:6
cms::DDNamespace::addVolume
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:170
cms::DDNamespace::DDNamespace
DDNamespace()=delete
cms::DDNamespace::addSolidNS
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:206
cms::DDNamespace::addSolid
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:221
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
cms::DDNamespace::addVolumeNS
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:150
cms::DDNamespace::nsName
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:89
mps_fire.Path
Path
Definition: mps_fire.py:290
heppy_batch.val
val
Definition: heppy_batch.py:351
std
Definition: JetResolutionObject.h:76
cms::DDParsingContext::debug_constants
bool debug_constants
Definition: DDParsingContext.h:77
cms::DDParsingContext::shapes
tbb::concurrent_unordered_map< std::string, dd4hep::Solid > shapes
Definition: DDParsingContext.h:38
cms::DDParsingContext::namespaces
tbb::concurrent_queue< std::string > namespaces
Definition: DDParsingContext.h:41
makeMuonMisalignmentScenario.rot
rot
Definition: makeMuonMisalignmentScenario.py:322
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
DDNamespace.h
cms::DDNamespace::vecDbl
std::vector< double > vecDbl(const std::string &name) const
Definition: DDNamespace.cc:246
NAMESPACE_SEP
#define NAMESPACE_SEP
Definition: DDNamespace.h:79
cms::DDNamespace::m_name
std::string m_name
Definition: DDNamespace.h:74
mps_fire.result
result
Definition: mps_fire.py:303
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
cms::DDParsingContext::debug_volumes
bool debug_volumes
Definition: DDParsingContext.h:81
cms::DDNamespace::name
std::string_view name() const
Definition: DDNamespace.h:68
cms::DDParsingContext::debug_shapes
bool debug_shapes
Definition: DDParsingContext.h:80
cms::DDNamespace::volume
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:190
cms::DDParsingContext::ns
const bool ns(std::string &result)
Definition: DDParsingContext.h:26
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21