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