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_back( 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.push_back( 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  m_name = m_context->namespaces.back();
42 }
43 
45  : m_context( &ctx )
46 {
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","+++ Current namespace is now: %s", m_context->ns().c_str());
55  }
56 }
57 
58 string
59 DDNamespace::prepend( const string& n ) const
60 {
61  return m_name + n;
62 }
63 
64 string
65 DDNamespace::realName( const string& v ) const
66 {
67  size_t idx, idq, idp;
68  string val = v;
69  while(( idx = val.find('[')) != string::npos )
70  {
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
84 DDNamespace::nsName( const string& nam )
85 {
86  size_t idx;
87  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
88  return nam.substr( 0, idx );
89  return "";
90 }
91 
92 string
93 DDNamespace::objName( const string& nam )
94 {
95  size_t idx;
96  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
97  return nam.substr( idx + 1 );
98  return "";
99 }
100 
101 void
102 DDNamespace::addConstant( const string& nam, const string& val, const string& typ ) const
103 {
104  addConstantNS( prepend( nam ), val, typ );
105 }
106 
107 void
108 DDNamespace::addConstantNS( const string& nam, const string& val, const string& typ ) const
109 {
110  const string& v = val;
111  const string& n = nam;
112  dd4hep::printout( m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
113  "DD4CMS","+++ Add constant object: %-40s = %s [type:%s]",
114  n.c_str(), v.c_str(), typ.c_str());
115  dd4hep::_toDictionary( n, v, typ );
116  dd4hep::Constant c( n, v, typ );
117  m_context->description->addConstant( c );
118 }
119 
121 DDNamespace::material( const string& name ) const
122 {
123  return m_context->description->material( realName( name ));
124 }
125 
126 void
127 DDNamespace::addRotation( const string& name, const dd4hep::Rotation3D& rot ) const
128 {
129  string n = prepend( name );
130  m_context->rotations[n] = rot;
131 }
132 
133 const dd4hep::Rotation3D&
134 DDNamespace::rotation( const string& nam ) const
135 {
136  static dd4hep::Rotation3D s_null;
137  size_t idx;
138  auto i = m_context->rotations.find( nam );
139  if( i != m_context->rotations.end())
140  return (*i).second;
141  else if( nam == "NULL" )
142  return s_null;
143  else if( nam.find(":NULL") == nam.length() - 5 )
144  return s_null;
145  string n = nam;
146  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
147  {
148  n[idx] = NAMESPACE_SEP;
149  i = m_context->rotations.find(n);
150  if( i != m_context->rotations.end() )
151  return (*i).second;
152  }
153  for( const auto& r : m_context->rotations ) {
154  cout << r.first << endl;
155  }
156  throw runtime_error("Unknown rotation identifier:"+nam);
157 }
158 
161 {
162  string n = vol.name();
163  dd4hep::Solid s = vol.solid();
164  dd4hep::Material m = vol.material();
165  vol->SetName(n.c_str());
166  m_context->volumes[n] = vol;
167  dd4hep::printout( m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
168  "+++ Add volume:%-38s Solid:%-26s[%-16s] Material:%s",
169  vol.name(), s.name(), s.type(), m.name());
170  return vol;
171 }
172 
176 {
177  string n = prepend(vol.name());
178  dd4hep::Solid s = vol.solid();
179  dd4hep::Material m = vol.material();
180  vol->SetName(n.c_str());
181  m_context->volumes[n] = vol;
182  dd4hep::printout( m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
183  "+++ Add volume:%-38s Solid:%-26s[%-16s] Material:%s",
184  vol.name(), s.name(), s.type(), m.name());
185  return vol;
186 }
187 
189 DDNamespace::volume( const string& name, bool exc ) const
190 {
191  size_t idx;
192  auto i = m_context->volumes.find( name );
193  if( i != m_context->volumes.end()) {
194  return (*i).second;
195  }
196  if(( idx = name.find( NAMESPACE_SEP )) != string::npos ) {
197  string n = name;
198  n[idx] = NAMESPACE_SEP;
199  i = m_context->volumes.find( n );
200  if( i != m_context->volumes.end())
201  return (*i).second;
202  }
203  if( exc ) {
204  throw runtime_error( "Unknown volume identifier:" + name );
205  }
206  return nullptr;
207 }
208 
209 dd4hep::Solid
210 DDNamespace::addSolidNS( const string& name, dd4hep::Solid solid ) const
211 {
212  dd4hep::printout( m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
213  "+++ Add shape of type %s : %s", solid->IsA()->GetName(), name.c_str());
214 
215  m_context->shapes.try_emplace( name, solid.setName( name ));
216 
217  return solid;
218 }
219 
220 dd4hep::Solid
221 DDNamespace::addSolid( const string& name, dd4hep::Solid sol ) const
222 {
223  return addSolidNS( prepend( name ), sol );
224 }
225 
226 dd4hep::Solid
227 DDNamespace::solid( const string& nam ) const
228 {
229  size_t idx;
230  string n = m_context->namespaces.back() + nam;
231  auto i = m_context->shapes.find( n );
232  if( i != m_context->shapes.end())
233  return (*i).second;
234  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos ) {
235  n = realName( nam );
236  n[idx] = NAMESPACE_SEP;
237  i = m_context->shapes.find( n );
238  if ( i != m_context->shapes.end() )
239  return (*i).second;
240  }
241  i = m_context->shapes.find(nam);
242  if( i != m_context->shapes.end()) return (*i).second;
243  throw runtime_error( "Unknown shape identifier:" + nam );
244 }
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:210
#define DEBUG
const std::string & ns() const
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:189
std::vector< std::string > namespaces
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:160
dd4hep::Detector * description
std::string m_name
Definition: DDNamespace.h:72
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:175
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:227
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:108
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:84
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:127
std::string realName(const std::string &) const
Definition: DDNamespace.cc:65
std::unordered_map< std::string, dd4hep::Rotation3D > rotations
std::unordered_map< std::string, dd4hep::Volume > volumes
static std::string objName(const std::string &)
Definition: DDNamespace.cc:93
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:121
DDParsingContext * m_context
Definition: DDNamespace.h:71
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:59
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:221
const std::string & name() const
Definition: DDNamespace.h:65
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:134
std::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:102
#define NAMESPACE_SEP
Definition: DDNamespace.h:77