Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GDMLRead.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: G4GDMLRead.cc 89819 2015-04-30 14:53:35Z gcosmo $
27 //
28 // class G4GDMLRead Implementation
29 //
30 // History:
31 // - Created. Zoltan Torzsok, November 2007
32 // -------------------------------------------------------------------------
33 
34 #include "globals.hh"
35 
36 #include "G4GDMLRead.hh"
37 
38 #include "G4UnitsTable.hh"
39 #include "G4Element.hh"
40 #include "G4Material.hh"
41 #include "G4SolidStore.hh"
42 #include "G4LogicalVolumeStore.hh"
43 #include "G4PhysicalVolumeStore.hh"
44 
46  : validate(true), check(false), dostrip(true), inLoop(0), loopCount(0)
47 {
49 }
50 
52 {
53 }
54 
55 G4String G4GDMLRead::Transcode(const XMLCh* const toTranscode)
56 {
57  char* char_str = xercesc::XMLString::transcode(toTranscode);
58  G4String my_str(char_str);
59  xercesc::XMLString::release(&char_str);
60  return my_str;
61 }
62 
64 {
65  check = flag;
66 }
67 
69 {
70  G4String nameOut(nameIn);
71 
72  if (inLoop>0)
73  {
74  nameOut = eval.SolveBrackets(nameOut);
75  }
76  if (strip) { StripName(nameOut); }
77 
78  return nameOut;
79 }
80 
82  G4VPhysicalVolume* physvol)
83 {
84  G4String nameOut(nameIn);
85 
86  if (nameIn.empty())
87  {
88  std::stringstream stream;
89  stream << physvol->GetLogicalVolume()->GetName() << "_PV";
90  nameOut = stream.str();
91  }
92  nameOut = eval.SolveBrackets(nameOut);
93 
94  physvol->SetName(nameOut);
95 }
96 
98 {
99  G4String sname(name);
100  return sname.remove(sname.find("0x"));
101 }
102 
104 {
105  name.remove(name.find("0x"));
106 }
107 
109 {
110  // Strips off names of volumes, solids elements and materials from possible
111  // reference pointers or IDs attached to their original identifiers.
112 
116  const G4ElementTable* elements = G4Element::GetElementTable();
117  const G4MaterialTable* materials = G4Material::GetMaterialTable();
118 
119  G4cout << "Stripping off GDML names of materials, solids and volumes ..."
120  << G4endl;
121 
122  G4String sname;
123  size_t i;
124 
125  // Solids...
126  //
127  for (i=0; i<solids->size(); ++i)
128  {
129  G4VSolid* psol = (*solids)[i];
130  sname = psol->GetName();
131  StripName(sname);
132  psol->SetName(sname);
133  }
134 
135  // Logical volumes...
136  //
137  for (i=0; i<lvols->size(); ++i)
138  {
139  G4LogicalVolume* lvol = (*lvols)[i];
140  sname = lvol->GetName();
141  StripName(sname);
142  lvol->SetName(sname);
143  }
144 
145  // Physical volumes...
146  //
147  for (i=0; i<pvols->size(); ++i)
148  {
149  G4VPhysicalVolume* pvol = (*pvols)[i];
150  sname = pvol->GetName();
151  StripName(sname);
152  pvol->SetName(sname);
153  }
154 
155  // Materials...
156  //
157  for (i=0; i<materials->size(); ++i)
158  {
159  G4Material* pmat = (*materials)[i];
160  sname = pmat->GetName();
161  StripName(sname);
162  pmat->SetName(sname);
163  }
164 
165  // Elements...
166  //
167  for (i=0; i<elements->size(); ++i)
168  {
169  G4Element* pelm = (*elements)[i];
170  sname = pelm->GetName();
171  StripName(sname);
172  pelm->SetName(sname);
173  }
174 }
175 
176 void G4GDMLRead::LoopRead(const xercesc::DOMElement* const element,
177  void(G4GDMLRead::*func)(const xercesc::DOMElement* const))
178 {
179  G4String var;
180  G4String from;
181  G4String to;
182  G4String step;
183 
184  const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
185  XMLSize_t attributeCount = attributes->getLength();
186 
187  for (XMLSize_t attribute_index=0;
188  attribute_index<attributeCount;attribute_index++)
189  {
190  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
191 
192  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
193  { continue; }
194 
195  const xercesc::DOMAttr* const attribute
196  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
197  if (!attribute)
198  {
199  G4Exception("G4GDMLRead::LoopRead()", "InvalidRead",
200  FatalException, "No attribute found!");
201  return;
202  }
203  const G4String attribute_name = Transcode(attribute->getName());
204  const G4String attribute_value = Transcode(attribute->getValue());
205 
206  if (attribute_name=="for") { var = attribute_value; } else
207  if (attribute_name=="from") { from = attribute_value; } else
208  if (attribute_name=="to") { to = attribute_value; } else
209  if (attribute_name=="step") { step = attribute_value; }
210  }
211 
212  if (var.empty())
213  {
214  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
215  FatalException, "No variable is determined for loop!");
216  }
217 
218  if (!eval.IsVariable(var))
219  {
220  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
221  FatalException, "Variable is not defined in loop!");
222  }
223 
224  G4int _var = eval.EvaluateInteger(var);
225  G4int _from = eval.EvaluateInteger(from);
226  G4int _to = eval.EvaluateInteger(to);
227  G4int _step = eval.EvaluateInteger(step);
228 
229  if (!from.empty()) { _var = _from; }
230 
231  if (_from == _to)
232  {
233  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
234  FatalException, "Empty loop!");
235  }
236  if ((_from < _to) && (_step <= 0))
237  {
238  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
239  FatalException, "Infinite loop!");
240  }
241  if ((_from > _to) && (_step >= 0))
242  {
243  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
244  FatalException, "Infinite loop!");
245  }
246 
247  inLoop++;
248 
249  while (_var <= _to)
250  {
251  eval.SetVariable(var,_var);
252  (this->*func)(element);
253  _var += _step;
254  loopCount++;
255  }
256 
257  inLoop--;
258  if (!inLoop) { loopCount = 0; }
259 }
260 
262 AuxiliaryRead(const xercesc::DOMElement* const auxiliaryElement)
263 {
264  G4GDMLAuxStructType auxstruct = {"","","",0};
265  G4GDMLAuxListType* auxList=0;
266 
267  const xercesc::DOMNamedNodeMap* const attributes
268  = auxiliaryElement->getAttributes();
269  XMLSize_t attributeCount = attributes->getLength();
270 
271  for (XMLSize_t attribute_index=0;
272  attribute_index<attributeCount; attribute_index++)
273  {
274  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
275 
276  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
277  { continue; }
278 
279  const xercesc::DOMAttr* const attribute
280  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
281  if (!attribute)
282  {
283  G4Exception("G4GDMLRead::AuxiliaryRead()",
284  "InvalidRead", FatalException, "No attribute found!");
285  return auxstruct;
286  }
287  const G4String attName = Transcode(attribute->getName());
288  const G4String attValue = Transcode(attribute->getValue());
289 
290  if (attName=="auxtype") { auxstruct.type = attValue; } else
291  if (attName=="auxvalue") { auxstruct.value = attValue; } else
292  if (attName=="auxunit") { auxstruct.unit = attValue; }
293  }
294 
295  for (xercesc::DOMNode* iter = auxiliaryElement->getFirstChild();
296  iter != 0; iter = iter->getNextSibling())
297  {
298  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
299 
300  const xercesc::DOMElement* const child
301  = dynamic_cast<xercesc::DOMElement*>(iter);
302  if (!child)
303  {
304  G4Exception("G4GDMLRead::AuxiliaryRead()",
305  "InvalidRead", FatalException, "No child found!");
306  break;
307  }
308  const G4String tag = Transcode(child->getTagName());
309 
310  if (tag=="auxiliary")
311  {
312  if(!auxList) { auxList = new G4GDMLAuxListType; }
313  auxList->push_back(AuxiliaryRead(child));
314  }
315  }
316 
317  if (auxList) { auxstruct.auxList = auxList; }
318 
319  return auxstruct;
320 }
321 
322 void G4GDMLRead::UserinfoRead(const xercesc::DOMElement* const userinfoElement)
323 {
324  G4cout << "G4GDML: Reading userinfo..." << G4endl;
325 
326  for (xercesc::DOMNode* iter = userinfoElement->getFirstChild();
327  iter != 0; iter = iter->getNextSibling())
328  {
329  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
330 
331  const xercesc::DOMElement* const child
332  = dynamic_cast<xercesc::DOMElement*>(iter);
333  if (!child)
334  {
335  G4Exception("G4GDMLRead::UserinfoRead()",
336  "InvalidRead", FatalException, "No child found!");
337  return;
338  }
339  const G4String tag = Transcode(child->getTagName());
340 
341  if (tag=="auxiliary")
342  {
343  auxGlobalList.push_back(AuxiliaryRead(child));
344  }
345  else
346  {
347  G4String error_msg = "Unknown tag in structure: " + tag;
348  G4Exception("G4GDMLRead::UserinfoRead()",
349  "ReadError", FatalException, error_msg);
350  }
351  }
352 }
353 
354 void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
355 {
356  G4String error_msg = "No handle to user-code for parsing extensions!";
357  G4Exception("G4GDMLRead::ExtensionRead()",
358  "NotImplemented", JustWarning, error_msg);
359 }
360 
361 void G4GDMLRead::Read(const G4String& fileName,
362  G4bool validation,
363  G4bool isModule,
364  G4bool strip)
365 {
366  dostrip = strip;
367  if (isModule)
368  {
369  G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
370  }
371  else
372  {
373  G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
374  }
375 
376  inLoop = 0;
377  validate = validation;
378 
379  xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
380  xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
381 
382  if (validate)
383  {
384  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
385  }
386  parser->setValidationSchemaFullChecking(validate);
387  parser->setCreateEntityReferenceNodes(false);
388  // Entities will be automatically resolved by Xerces
389 
390  parser->setDoNamespaces(true);
391  parser->setDoSchema(validate);
392  parser->setErrorHandler(handler);
393 
394  try { parser->parse(fileName.c_str()); }
395  catch (const xercesc::XMLException &e)
396  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
397  catch (const xercesc::DOMException &e)
398  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
399 
400  xercesc::DOMDocument* doc = parser->getDocument();
401 
402  if (!doc)
403  {
404  G4String error_msg = "Unable to open document: " + fileName;
405  G4Exception("G4GDMLRead::Read()", "InvalidRead",
406  FatalException, error_msg);
407  return;
408  }
409  xercesc::DOMElement* element = doc->getDocumentElement();
410 
411  if (!element)
412  {
413  std::ostringstream message;
414  message << "ERROR - Empty document or unable to validate schema!" << G4endl
415  << " Check Internet connection is ON in case of schema"
416  << G4endl
417  << " validation enabled and location defined as URL in"
418  << G4endl
419  << " the GDML file - " << fileName << " - being imported!"
420  << G4endl
421  << " Otherwise, verify GDML schema server is reachable!";
422  G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException, message);
423  return;
424  }
425 
426  for (xercesc::DOMNode* iter = element->getFirstChild();
427  iter != 0; iter = iter->getNextSibling())
428  {
429  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
430 
431  const xercesc::DOMElement* const child
432  = dynamic_cast<xercesc::DOMElement*>(iter);
433  if (!child)
434  {
435  G4Exception("G4GDMLRead::Read()", "InvalidRead",
436  FatalException, "No child found!");
437  return;
438  }
439  const G4String tag = Transcode(child->getTagName());
440 
441  if (tag=="define") { DefineRead(child); } else
442  if (tag=="materials") { MaterialsRead(child); } else
443  if (tag=="solids") { SolidsRead(child); } else
444  if (tag=="setup") { SetupRead(child); } else
445  if (tag=="structure") { StructureRead(child); } else
446  if (tag=="userinfo") { UserinfoRead(child); } else
447  if (tag=="extension") { ExtensionRead(child); }
448  else
449  {
450  G4String error_msg = "Unknown tag in gdml: " + tag;
451  G4Exception("G4GDMLRead::Read()", "InvalidRead",
452  FatalException, error_msg);
453  }
454  }
455 
456  delete parser;
457  delete handler;
458 
459  if (isModule)
460  {
461  G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
462  }
463  else
464  {
465  G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
466  if (strip) { StripNames(); }
467  }
468 }
469 
471 {
472  return &auxGlobalList;
473 }
G4String GetName() const
const XML_Char * name
Definition: expat.h:151
std::vector< G4GDMLAuxStructType > * auxList
const G4GDMLAuxListType * GetAuxList() const
Definition: G4GDMLRead.cc:470
static void BuildUnitsTable()
G4int EvaluateInteger(const G4String &)
void SetName(const G4String &name)
Definition: G4Material.hh:279
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:155
void OverlapCheck(G4bool)
Definition: G4GDMLRead.cc:63
G4String & remove(str_size)
const G4String & GetName() const
Definition: G4Material.hh:178
Definition: xmlparse.cc:187
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:587
G4bool dostrip
Definition: G4GDMLRead.hh:158
std::vector< G4Material * > G4MaterialTable
virtual void MaterialsRead(const xercesc::DOMElement *const)=0
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
void StripNames() const
Definition: G4GDMLRead.cc:108
void SetName(const G4String &pName)
void GeneratePhysvolName(const G4String &, G4VPhysicalVolume *)
Definition: G4GDMLRead.cc:81
virtual ~G4GDMLRead()
Definition: G4GDMLRead.cc:51
int G4int
Definition: G4Types.hh:78
virtual void StructureRead(const xercesc::DOMElement *const)=0
static G4PhysicalVolumeStore * GetInstance()
void SetName(const G4String &name)
void LoopRead(const xercesc::DOMElement *const, void(G4GDMLRead::*)(const xercesc::DOMElement *const))
Definition: G4GDMLRead.cc:176
G4GLOB_DLL std::ostream G4cout
virtual void UserinfoRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:322
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
static G4LogicalVolumeStore * GetInstance()
static G4SolidStore * GetInstance()
G4bool IsVariable(const G4String &) const
CommandLineParser * parser(0)
void StripName(G4String &) const
Definition: G4GDMLRead.cc:103
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4String SolveBrackets(const G4String &)
virtual void DefineRead(const xercesc::DOMElement *const)=0
virtual void ExtensionRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:354
G4String GenerateName(const G4String &name, G4bool strip=false)
Definition: G4GDMLRead.cc:68
G4LogicalVolume * GetLogicalVolume() const
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
void SetVariable(const G4String &, G4double)
G4String Strip(const G4String &) const
Definition: G4GDMLRead.cc:97
G4bool validate
Definition: G4GDMLRead.hh:156
G4GDMLAuxStructType AuxiliaryRead(const xercesc::DOMElement *const auxElem)
Definition: G4GDMLRead.cc:262
void SetName(const G4String &pName)
#define G4endl
Definition: G4ios.hh:61
std::vector< G4Element * > G4ElementTable
const G4String & GetName() const
const G4String & GetName() const
Definition: G4Element.hh:127
virtual void SetupRead(const xercesc::DOMElement *const)=0
static G4ElementTable * GetElementTable()
Definition: G4Element.cc:398
G4bool check
Definition: G4GDMLRead.hh:157
virtual void SolidsRead(const xercesc::DOMElement *const)=0
void SetName(const G4String &name)
Definition: G4Element.hh:214
void Read(const G4String &, G4bool validation, G4bool isModule, G4bool strip=true)
Definition: G4GDMLRead.cc:361