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  : 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( !context->namespaces.empty()) {
20  m_name = context->namespaces.back();
21  }
22  dd4hep::printout( context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
23  "MyDDCMS", "+++ 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  context->namespaces.push_back( m_name );
36  m_pop = true;
37  dd4hep::printout( context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
38  "MyDDCMS","+++ Current namespace is now: %s", m_name.c_str());
39  return;
40 }
41 
42 DDNamespace::DDNamespace( DDParsingContext& ctx, xml_h element, bool )
43  : 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  context->namespaces.push_back( m_name );
58  m_pop = true;
59  dd4hep::printout( context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
60  "MyDDCMS","+++ Current namespace is now: %s", m_name.c_str());
61  return;
62 }
63 
65  : context( ctx )
66 {
67  m_name = context->namespaces.back();
68 }
69 
71  : context( &ctx )
72 {
73  m_name = context->namespaces.back();
74 }
75 
77  if( m_pop ) {
78  context->namespaces.pop_back();
79  dd4hep::printout( context->debug_namespaces ? dd4hep::ALWAYS : dd4hep::DEBUG,
80  "MyDDCMS","+++ Current namespace is now: %s", 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( context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
139  "MyDDCMS","+++ 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  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( context->debug_constants ? dd4hep::ALWAYS : dd4hep::DEBUG,
152  "MyDDCMS","+++ Add constant object: %-40s = %s ",
153  n.c_str(), "vector<double>");
154  context->addVector( n, v );
155 }
156 
158 DDNamespace::material( const string& name ) const
159 {
160  return 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  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 = context->rotations.find( nam );
176  if( i != 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 = context->rotations.find(n);
187  if( i != context->rotations.end() )
188  return (*i).second;
189  }
190  for( const auto& r : 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  context->volumes[n] = vol;
204  dd4hep::printout(context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "MyDDCMS",
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  context->volumes[n] = vol;
219  dd4hep::printout(context->debug_volumes ? dd4hep::ALWAYS : dd4hep::DEBUG, "MyDDCMS",
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& nam, bool exc ) const
227 {
228  size_t idx;
229  auto i = context->volumes.find(nam);
230  if ( i != context->volumes.end() ) {
231  return (*i).second;
232  }
233  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos ) {
234  string n = nam;
235  n[idx] = NAMESPACE_SEP;
236  i = context->volumes.find( n );
237  if( i != context->volumes.end())
238  return (*i).second;
239  }
240  if( exc ) {
241  throw runtime_error("Unknown volume identifier:"+nam);
242  }
243  return nullptr;
244 }
245 
246 dd4hep::Solid
247 DDNamespace::addSolidNS( const string& nam, dd4hep::Solid sol ) const
248 {
249  dd4hep::printout(context->debug_shapes ? dd4hep::ALWAYS : dd4hep::DEBUG, "MyDDCMS",
250  "+++ Add shape of type %s : %s",sol->IsA()->GetName(), nam.c_str());
251  context->shapes[nam] = sol.setName(nam);
252 
253  return sol;
254 }
255 
256 dd4hep::Solid
257 DDNamespace::addSolid( const string& nam, dd4hep::Solid sol ) const
258 {
259  return addSolidNS( prepend(nam), sol );
260 }
261 
262 dd4hep::Solid
263 DDNamespace::solid( const string& nam ) const
264 {
265  size_t idx;
266  string n = context->namespaces.back() + nam;
267  auto i = context->shapes.find( n );
268  if( i != context->shapes.end())
269  return (*i).second;
270  if(( idx = nam.find( NAMESPACE_SEP )) != string::npos ) {
271  n = realName( nam );
272  n[idx] = NAMESPACE_SEP;
273  i = context->shapes.find( n );
274  if ( i != context->shapes.end() )
275  return (*i).second;
276  }
277  i = context->shapes.find(nam);
278  if( i != context->shapes.end()) return (*i).second;
279  throw runtime_error( "Unknown shape identifier:" + nam );
280 }
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::map< std::string, dd4hep::Solid > shapes
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:197
dd4hep::Detector * description
std::string m_name
Definition: DDNamespace.h:70
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:263
void addConstantNS(const std::string &name, const std::string &value, const std::string &type) const
Definition: DDNamespace.cc:134
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
static std::string objName(const std::string &)
Definition: DDNamespace.cc:119
std::map< std::string, dd4hep::Volume > volumes
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:158
Definition: value.py:1
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:85
Namespace of DDCMS conversion namespace.
Polynomial< 0 > Constant
Definition: Constant.h:6
std::vector< std::string > namespaces
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:257
DDParsingContext * context
Definition: DDNamespace.h:63
const std::string & name() const
Definition: DDNamespace.h:65
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:171
std::map< std::string, dd4hep::Rotation3D > rotations
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:75