Geant4_10
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 68053 2013-03-13 14:39:51Z 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), 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 // std::stringstream stream;
76 // stream << "0x" << loopCount;
77 // nameOut = nameOut + stream.str();
78  }
79  if (strip) { StripName(nameOut); }
80 
81  return nameOut;
82 }
83 
85  G4VPhysicalVolume* physvol)
86 {
87  G4String nameOut(nameIn);
88 
89  if (nameIn.empty())
90  {
91  std::stringstream stream;
92  stream << physvol->GetLogicalVolume()->GetName() << "_PV";
93  nameOut = stream.str();
94  }
95  nameOut = eval.SolveBrackets(nameOut);
96 
97  physvol->SetName(nameOut);
98 }
99 
101 {
102  G4String sname(name);
103  return sname.remove(sname.find("0x"));
104 }
105 
107 {
108  name.remove(name.find("0x"));
109 }
110 
112 {
113  // Strips off names of volumes, solids elements and materials from possible
114  // reference pointers or IDs attached to their original identifiers.
115 
119  const G4ElementTable* elements = G4Element::GetElementTable();
120  const G4MaterialTable* materials = G4Material::GetMaterialTable();
121 
122  G4cout << "Stripping off GDML names of materials, solids and volumes ..."
123  << G4endl;
124 
125  G4String sname;
126  register size_t i;
127 
128  // Solids...
129  //
130  for (i=0; i<solids->size(); i++)
131  {
132  G4VSolid* psol = (*solids)[i];
133  sname = psol->GetName();
134  StripName(sname);
135  psol->SetName(sname);
136  }
137 
138  // Logical volumes...
139  //
140  for (i=0; i<lvols->size(); i++)
141  {
142  G4LogicalVolume* lvol = (*lvols)[i];
143  sname = lvol->GetName();
144  StripName(sname);
145  lvol->SetName(sname);
146  }
147 
148  // Physical volumes...
149  //
150  for (i=0; i<pvols->size(); i++)
151  {
152  G4VPhysicalVolume* pvol = (*pvols)[i];
153  sname = pvol->GetName();
154  StripName(sname);
155  pvol->SetName(sname);
156  }
157 
158  // Materials...
159  //
160  for (i=0; i<materials->size(); i++)
161  {
162  G4Material* pmat = (*materials)[i];
163  sname = pmat->GetName();
164  StripName(sname);
165  pmat->SetName(sname);
166  }
167 
168  // Elements...
169  //
170  for (i=0; i<elements->size(); i++)
171  {
172  G4Element* pelm = (*elements)[i];
173  sname = pelm->GetName();
174  StripName(sname);
175  pelm->SetName(sname);
176  }
177 }
178 
179 void G4GDMLRead::LoopRead(const xercesc::DOMElement* const element,
180  void(G4GDMLRead::*func)(const xercesc::DOMElement* const))
181 {
182  G4String var;
183  G4String from;
184  G4String to;
185  G4String step;
186 
187  const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
188  XMLSize_t attributeCount = attributes->getLength();
189 
190  for (XMLSize_t attribute_index=0;
191  attribute_index<attributeCount;attribute_index++)
192  {
193  xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
194 
195  if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
196  { continue; }
197 
198  const xercesc::DOMAttr* const attribute
199  = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
200  if (!attribute)
201  {
202  G4Exception("G4GDMLRead::LoopRead()", "InvalidRead",
203  FatalException, "No attribute found!");
204  return;
205  }
206  const G4String attribute_name = Transcode(attribute->getName());
207  const G4String attribute_value = Transcode(attribute->getValue());
208 
209  if (attribute_name=="for") { var = attribute_value; } else
210  if (attribute_name=="from") { from = attribute_value; } else
211  if (attribute_name=="to") { to = attribute_value; } else
212  if (attribute_name=="step") { step = attribute_value; }
213  }
214 
215  if (var.empty())
216  {
217  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
218  FatalException, "No variable is determined for loop!");
219  }
220 
221  if (!eval.IsVariable(var))
222  {
223  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
224  FatalException, "Variable is not defined in loop!");
225  }
226 
227  G4int _var = eval.EvaluateInteger(var);
228  G4int _from = eval.EvaluateInteger(from);
229  G4int _to = eval.EvaluateInteger(to);
230  G4int _step = eval.EvaluateInteger(step);
231 
232  if (!from.empty()) { _var = _from; }
233 
234  if (_from == _to)
235  {
236  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
237  FatalException, "Empty loop!");
238  }
239  if ((_from < _to) && (_step <= 0))
240  {
241  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
242  FatalException, "Infinite loop!");
243  }
244  if ((_from > _to) && (_step >= 0))
245  {
246  G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
247  FatalException, "Infinite loop!");
248  }
249 
250  inLoop++;
251 
252  while (_var <= _to)
253  {
254  eval.SetVariable(var,_var);
255  (this->*func)(element);
256  _var += _step;
257  loopCount++;
258  }
259 
260  inLoop--;
261  if (!inLoop) { loopCount = 0; }
262 }
263 
264 void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
265 {
266  G4String error_msg = "No handle to user-code for parsing extensions!";
267  G4Exception("G4GDMLRead::ExtensionRead()",
268  "NotImplemented", JustWarning, error_msg);
269 }
270 
271 void G4GDMLRead::Read(const G4String& fileName,
272  G4bool validation,
273  G4bool isModule,
274  G4bool strip)
275 {
276  if (isModule)
277  {
278  G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
279  }
280  else
281  {
282  G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
283  }
284 
285  inLoop = 0;
286  validate = validation;
287 
288  xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
289  xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
290 
291  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
292  parser->setValidationSchemaFullChecking(true);
293  parser->setCreateEntityReferenceNodes(false);
294  // Entities will be automatically resolved by Xerces
295 
296  parser->setDoNamespaces(true);
297  parser->setDoSchema(true);
298  parser->setErrorHandler(handler);
299 
300  try { parser->parse(fileName.c_str()); }
301  catch (const xercesc::XMLException &e)
302  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
303  catch (const xercesc::DOMException &e)
304  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
305 
306  xercesc::DOMDocument* doc = parser->getDocument();
307 
308  if (!doc)
309  {
310  G4String error_msg = "Unable to open document: " + fileName;
311  G4Exception("G4GDMLRead::Read()", "InvalidRead",
312  FatalException, error_msg);
313  return;
314  }
315  xercesc::DOMElement* element = doc->getDocumentElement();
316 
317  if (!element)
318  {
319  G4Exception("G4GDMLRead::Read()", "InvalidRead",
320  FatalException, "Empty document!");
321  return;
322  }
323 
324  for (xercesc::DOMNode* iter = element->getFirstChild();
325  iter != 0; iter = iter->getNextSibling())
326  {
327  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
328 
329  const xercesc::DOMElement* const child
330  = dynamic_cast<xercesc::DOMElement*>(iter);
331  if (!child)
332  {
333  G4Exception("G4GDMLRead::Read()", "InvalidRead",
334  FatalException, "No child found!");
335  return;
336  }
337  const G4String tag = Transcode(child->getTagName());
338 
339  if (tag=="define") { DefineRead(child); } else
340  if (tag=="materials") { MaterialsRead(child); } else
341  if (tag=="solids") { SolidsRead(child); } else
342  if (tag=="setup") { SetupRead(child); } else
343  if (tag=="structure") { StructureRead(child); } else
344  if (tag=="extension") { ExtensionRead(child); }
345  else
346  {
347  G4String error_msg = "Unknown tag in gdml: " + tag;
348  G4Exception("G4GDMLRead::Read()", "InvalidRead",
349  FatalException, error_msg);
350  }
351  }
352 
353  delete parser;
354  delete handler;
355 
356  if (isModule)
357  {
358  G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
359  }
360  else
361  {
362  G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
363  if (strip) { StripNames(); }
364  }
365 }
G4String GetName() const
static void BuildUnitsTable()
G4int EvaluateInteger(const G4String &)
void SetName(const G4String &name)
Definition: G4Material.hh:281
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:144
G4String GetName() const
void OverlapCheck(G4bool)
Definition: G4GDMLRead.cc:63
G4String & remove(str_size)
const G4String & GetName() const
Definition: G4Material.hh:176
Definition: xmlparse.cc:179
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:564
std::vector< G4Material * > G4MaterialTable
virtual void MaterialsRead(const xercesc::DOMElement *const)=0
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
const XML_Char * name
Definition: expat.h:151
void StripNames() const
Definition: G4GDMLRead.cc:111
void SetName(const G4String &pName)
void GeneratePhysvolName(const G4String &, G4VPhysicalVolume *)
Definition: G4GDMLRead.cc:84
virtual ~G4GDMLRead()
Definition: G4GDMLRead.cc:51
int G4int
Definition: G4Types.hh:78
virtual void StructureRead(const xercesc::DOMElement *const)=0
static G4PhysicalVolumeStore * GetInstance()
REAL *8 function var(A, B, C, D)
Definition: dpm25nuc1.f:4649
void SetName(const G4String &name)
void LoopRead(const xercesc::DOMElement *const, void(G4GDMLRead::*)(const xercesc::DOMElement *const))
Definition: G4GDMLRead.cc:179
G4GLOB_DLL std::ostream G4cout
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
static G4LogicalVolumeStore * GetInstance()
static G4SolidStore * GetInstance()
G4bool IsVariable(const G4String &) const
void StripName(G4String &) const
Definition: G4GDMLRead.cc:106
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:264
G4String GenerateName(const G4String &name, G4bool strip=false)
Definition: G4GDMLRead.cc:68
G4LogicalVolume * GetLogicalVolume() const
void SetVariable(const G4String &, G4double)
G4String Strip(const G4String &) const
Definition: G4GDMLRead.cc:100
G4bool validate
Definition: G4GDMLRead.hh:145
void SetName(const G4String &pName)
#define G4endl
Definition: G4ios.hh:61
std::vector< G4Element * > G4ElementTable
const G4String & GetName() const
Definition: G4Element.hh:127
virtual void SetupRead(const xercesc::DOMElement *const)=0
static G4ElementTable * GetElementTable()
Definition: G4Element.cc:395
G4bool check
Definition: G4GDMLRead.hh:146
virtual void SolidsRead(const xercesc::DOMElement *const)=0
void SetName(const G4String &name)
Definition: G4Element.hh:216
void Read(const G4String &, G4bool validation, G4bool isModule, G4bool strip=true)
Definition: G4GDMLRead.cc:271