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  xml_dim_t elt( element );
16  bool has_label = elt.hasAttr(_U(label));
17  m_name = has_label ? elt.labelStr() : "";
18  if( !has_label ) {
19  if( !m_context->namespaces.empty()) {
20  m_name = m_context->namespaces.back();
21  }
22  dd4hep::printout( m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
23  "DD4CMS", "+++ Current namespace is now: %s", m_name.c_str());
24  return;
25  }
26  if( has_label ) {
27  size_t idx = m_name.find('.');
28  m_name = m_name.substr( 0, idx );
29  }
30  else {
31  dd4hep::Path path( xml_handler_t::system_path( element ));
32  m_name = path.filename().substr( 0, path.filename().rfind('.'));
33  }
34  if ( !m_name.empty()) m_name += NAMESPACE_SEP;
35  m_context->namespaces.emplace_back( m_name );
36  m_pop = true;
37  dd4hep::printout( m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
38  "DD4CMS","+++ Current namespace is now: %s", m_name.c_str());
39  return;
40 }
41 
42 DDNamespace::DDNamespace( DDParsingContext& ctx, xml_h element, bool )
43  : m_context( &ctx )
44 {
45  xml_dim_t elt(element);
46  bool has_label = elt.hasAttr(_U(label));
47  m_name = has_label ? elt.labelStr() : "";
48  if( has_label ) {
49  size_t idx = m_name.find('.');
50  m_name = m_name.substr(0,idx);
51  }
52  else {
53  dd4hep::Path path( xml_handler_t::system_path( element ));
54  m_name = path.filename().substr( 0, path.filename().rfind('.'));
55  }
56  if( !m_name.empty()) m_name += NAMESPACE_SEP;
57  m_context->namespaces.push_back( m_name );
58  m_pop = true;
59  dd4hep::printout( m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
60  "DD4CMS","+++ Current namespace is now: %s", m_name.c_str());
61  return;
62 }
63 
65  : m_context( ctx )
66 {
67  m_name = m_context->namespaces.back();
68 }
69 
71  : m_context( &ctx )
72 {
73  m_name = m_context->namespaces.back();
74 }
75 
77  if( m_pop ) {
78  m_context->namespaces.pop_back();
79  dd4hep::printout( m_context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
80  "DD4CMS","+++ Current namespace is now: %s", m_context->ns().c_str());
81  }
82 }
83 
84 string
85 DDNamespace::prepend( const string& n ) const
86 {
87  return m_name + n;
88 }
89 
90 string
91 DDNamespace::realName( const string& v ) const
92 {
93  size_t idx, idq, idp;
94  string val = v;
95  while(( idx = val.find('[')) != string::npos )
96  {
97  val.erase( idx, 1 );
98  idp = val.find( NAMESPACE_SEP, idx );
99  idq = val.find( ']', idx );
100  val.erase( idq, 1 );
101  if( idp == string::npos || idp > idq )
102  val.insert( idx, m_name );
103  else if ( idp != string::npos && idp < idq )
104  val[idp] = NAMESPACE_SEP;
105  }
106  return val;
107 }
108 
109 string
110 DDNamespace::nsName( const string& nam )
111 {
112  size_t idx;
113  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
114  return nam.substr( 0, idx );
115  return "";
116 }
117 
118 string
119 DDNamespace::objName( const string& nam )
120 {
121  size_t idx;
122  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
123  return nam.substr( idx + 1 );
124  return "";
125 }
126 
127 void
128 DDNamespace::addConstant( const string& nam, const string& val, const string& typ ) const
129 {
130  addConstantNS( prepend( nam ), val, typ );
131 }
132 
133 void
134 DDNamespace::addConstantNS( const string& nam, const string& val, const string& typ ) const
135 {
136  const string& v = val;
137  const string& n = nam;
138  dd4hep::printout( m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
139  "DD4CMS","+++ Add constant object: %-40s = %s [type:%s]",
140  n.c_str(), v.c_str(), typ.c_str());
141  dd4hep::_toDictionary( n, v, typ );
142  dd4hep::Constant c( n, v, typ );
143  m_context->description->addConstant( c );
144 }
145 
146 void
147 DDNamespace::addVector( const string& name, const vector<double>& value ) const
148 {
149  const vector<double>& v = value;
150  const string& n = name;
151  dd4hep::printout( m_context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
152  "DD4CMS","+++ Add constant object: %-40s = %s ",
153  n.c_str(), "vector<double>");
154  m_context->addVector( n, v );
155 }
156 
158 DDNamespace::material( const string& name ) const
159 {
160  return m_context->description->material( realName( name ));
161 }
162 
163 void
164 DDNamespace::addRotation( const string& name, const dd4hep::Rotation3D& rot ) const
165 {
166  string n = prepend( name );
167  m_context->rotations[n] = rot;
168 }
169 
170 const dd4hep::Rotation3D&
171 DDNamespace::rotation( const string& nam ) const
172 {
173  static dd4hep::Rotation3D s_null;
174  size_t idx;
175  auto i = m_context->rotations.find( nam );
176  if( i != m_context->rotations.end())
177  return (*i).second;
178  else if( nam == "NULL" )
179  return s_null;
180  else if( nam.find(":NULL") == nam.length() - 5 )
181  return s_null;
182  string n = nam;
183  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos )
184  {
185  n[idx] = NAMESPACE_SEP;
186  i = m_context->rotations.find(n);
187  if( i != m_context->rotations.end() )
188  return (*i).second;
189  }
190  for( const auto& r : m_context->rotations ) {
191  cout << r.first << endl;
192  }
193  throw runtime_error("Unknown rotation identifier:"+nam);
194 }
195 
198 {
199  string n = vol.name();
200  dd4hep::Solid s = vol.solid();
201  dd4hep::Material m = vol.material();
202  vol->SetName(n.c_str());
203  m_context->volumes[n] = vol;
204  dd4hep::printout( m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
205  "+++ Add volume:%-38s Solid:%-26s[%-16s] Material:%s",
206  vol.name(), s.name(), s.type(), m.name());
207  return vol;
208 }
209 
213 {
214  string n = prepend(vol.name());
215  dd4hep::Solid s = vol.solid();
216  dd4hep::Material m = vol.material();
217  vol->SetName(n.c_str());
218  m_context->volumes[n] = vol;
219  dd4hep::printout( m_context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
220  "+++ Add volume:%-38s Solid:%-26s[%-16s] Material:%s",
221  vol.name(), s.name(), s.type(), m.name());
222  return vol;
223 }
224 
226 DDNamespace::volume( const string& name, bool exc ) const
227 {
228  size_t idx;
229  auto i = m_context->volumes.find( name );
230  if( i != m_context->volumes.end()) {
231  return (*i).second;
232  }
233  if(( idx = name.find( NAMESPACE_SEP )) != string::npos ) {
234  string n = name;
235  n[idx] = NAMESPACE_SEP;
236  i = m_context->volumes.find( n );
237  if( i != m_context->volumes.end())
238  return (*i).second;
239  }
240  if( exc ) {
241  throw runtime_error( "Unknown volume identifier:" + name );
242  }
243  return nullptr;
244 }
245 
246 dd4hep::Solid
247 DDNamespace::addSolidNS( const string& name, dd4hep::Solid solid ) const
248 {
249  dd4hep::printout( m_context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG, "DD4CMS",
250  "+++ Add shape of type %s : %s", solid->IsA()->GetName(), name.c_str());
251 
252  m_context->shapes.try_emplace( name, solid.setName( name ));
253 
254  return solid;
255 }
256 
257 dd4hep::Solid
258 DDNamespace::addSolid( const string& name, dd4hep::Solid sol ) const
259 {
260  return addSolidNS( prepend( name ), sol );
261 }
262 
263 dd4hep::Solid
264 DDNamespace::solid( const string& nam ) const
265 {
266  size_t idx;
267  string n = m_context->namespaces.back() + nam;
268  auto i = m_context->shapes.find( n );
269  if( i != m_context->shapes.end())
270  return (*i).second;
271  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos ) {
272  n = realName( nam );
273  n[idx] = NAMESPACE_SEP;
274  i = m_context->shapes.find( n );
275  if ( i != m_context->shapes.end() )
276  return (*i).second;
277  }
278  i = m_context->shapes.find(nam);
279  if( i != m_context->shapes.end()) return (*i).second;
280  throw runtime_error( "Unknown shape identifier:" + nam );
281 }
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:247
#define DEBUG
const std::string & ns() const
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:226
void addVector(const std::string &name, const VecDouble &value)
std::vector< std::string > namespaces
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:197
dd4hep::Detector * description
std::string m_name
Definition: DDNamespace.h:73
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:212
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:264
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:134
char const * label
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:110
void addVector(const std::string &name, const std::vector< double > &value) const
Definition: DDNamespace.cc:147
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:164
std::string realName(const std::string &) const
Definition: DDNamespace.cc:91
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:119
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:158
Definition: value.py:1
DDParsingContext * m_context
Definition: DDNamespace.h:72
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:85
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:258
const std::string & name() const
Definition: DDNamespace.h:66
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:171
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:128
#define NAMESPACE_SEP
Definition: DDNamespace.h:78