Geant4  10.01
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 81112 2014-05-21 08:50:44Z 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  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  if (validate)
292  {
293  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
294  }
295  parser->setValidationSchemaFullChecking(validate);
296  parser->setCreateEntityReferenceNodes(false);
297  // Entities will be automatically resolved by Xerces
298 
299  parser->setDoNamespaces(true);
300  parser->setDoSchema(validate);
301  parser->setErrorHandler(handler);
302 
303  try { parser->parse(fileName.c_str()); }
304  catch (const xercesc::XMLException &e)
305  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
306  catch (const xercesc::DOMException &e)
307  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
308 
309  xercesc::DOMDocument* doc = parser->getDocument();
310 
311  if (!doc)
312  {
313  G4String error_msg = "Unable to open document: " + fileName;
314  G4Exception("G4GDMLRead::Read()", "InvalidRead",
315  FatalException, error_msg);
316  return;
317  }
318  xercesc::DOMElement* element = doc->getDocumentElement();
319 
320  if (!element)
321  {
322  std::ostringstream message;
323  message << "ERROR - Empty document!" << G4endl
324  << " Check Internet connection is ON in case of schema"
325  << G4endl
326  << " validation enabled and location defined as URL in"
327  << G4endl
328  << " the GDML file - " << fileName << " - being imported!";
329  G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException, message);
330  return;
331  }
332 
333  for (xercesc::DOMNode* iter = element->getFirstChild();
334  iter != 0; iter = iter->getNextSibling())
335  {
336  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
337 
338  const xercesc::DOMElement* const child
339  = dynamic_cast<xercesc::DOMElement*>(iter);
340  if (!child)
341  {
342  G4Exception("G4GDMLRead::Read()", "InvalidRead",
343  FatalException, "No child found!");
344  return;
345  }
346  const G4String tag = Transcode(child->getTagName());
347 
348  if (tag=="define") { DefineRead(child); } else
349  if (tag=="materials") { MaterialsRead(child); } else
350  if (tag=="solids") { SolidsRead(child); } else
351  if (tag=="setup") { SetupRead(child); } else
352  if (tag=="structure") { StructureRead(child); } else
353  if (tag=="extension") { ExtensionRead(child); }
354  else
355  {
356  G4String error_msg = "Unknown tag in gdml: " + tag;
357  G4Exception("G4GDMLRead::Read()", "InvalidRead",
358  FatalException, error_msg);
359  }
360  }
361 
362  delete parser;
363  delete handler;
364 
365  if (isModule)
366  {
367  G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
368  }
369  else
370  {
371  G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
372  if (strip) { StripNames(); }
373  }
374 }
static int release(statusMessageReporting *smr, xDataType *xDT)
Definition: xData_1d_x.cc:179
G4String GetName() const
static void BuildUnitsTable()
G4int EvaluateInteger(const G4String &)
void SetName(const G4String &name)
Definition: G4Material.hh:280
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:144
G4String GetName() const
void OverlapCheck(G4bool)
Definition: G4GDMLRead.cc:63
G4String & remove(str_size)
G4String name
Definition: TRTMaterials.hh:40
const G4String & GetName() const
Definition: G4Material.hh:176
Definition: xmlparse.cc:179
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:588
std::vector< G4Material * > G4MaterialTable
virtual void MaterialsRead(const xercesc::DOMElement *const)=0
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
G4int loopCount
Definition: G4GDMLRead.hh:150
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()
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
CommandLineParser * parser(0)
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
G4int inLoop
Definition: G4GDMLRead.hh:150
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:403
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