Geant4  10.01.p02
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 90766 2015-06-09 10:13:41Z 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 
261 void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
262 {
263  G4String error_msg = "No handle to user-code for parsing extensions!";
264  G4Exception("G4GDMLRead::ExtensionRead()",
265  "NotImplemented", JustWarning, error_msg);
266 }
267 
268 void G4GDMLRead::Read(const G4String& fileName,
269  G4bool validation,
270  G4bool isModule,
271  G4bool strip)
272 {
273  dostrip = strip;
274  if (isModule)
275  {
276  G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
277  }
278  else
279  {
280  G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
281  }
282 
283  inLoop = 0;
284  validate = validation;
285 
286  xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
287  xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
288 
289  if (validate)
290  {
291  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
292  }
293  parser->setValidationSchemaFullChecking(validate);
294  parser->setCreateEntityReferenceNodes(false);
295  // Entities will be automatically resolved by Xerces
296 
297  parser->setDoNamespaces(true);
298  parser->setDoSchema(validate);
299  parser->setErrorHandler(handler);
300 
301  try { parser->parse(fileName.c_str()); }
302  catch (const xercesc::XMLException &e)
303  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
304  catch (const xercesc::DOMException &e)
305  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
306 
307  xercesc::DOMDocument* doc = parser->getDocument();
308 
309  if (!doc)
310  {
311  G4String error_msg = "Unable to open document: " + fileName;
312  G4Exception("G4GDMLRead::Read()", "InvalidRead",
313  FatalException, error_msg);
314  return;
315  }
316  xercesc::DOMElement* element = doc->getDocumentElement();
317 
318  if (!element)
319  {
320  std::ostringstream message;
321  message << "ERROR - Empty document or unable to validate schema!" << G4endl
322  << " Check Internet connection is ON in case of schema"
323  << G4endl
324  << " validation enabled and location defined as URL in"
325  << G4endl
326  << " the GDML file - " << fileName << " - being imported!"
327  << G4endl
328  << " Otherwise, verify GDML schema server is reachable!";
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:282
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:178
Definition: xmlparse.cc:179
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:588
G4bool dostrip
Definition: G4GDMLRead.hh:147
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:151
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
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:261
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:97
G4bool validate
Definition: G4GDMLRead.hh:145
G4int inLoop
Definition: G4GDMLRead.hh:151
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:268