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