Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4GDMLRead.cc
이 파일의 문서화 페이지로 가기
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 108895 2018-03-15 10:27:25Z 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 #ifdef G4VERBOSE
325  G4cout << "G4GDML: Reading userinfo..." << G4endl;
326 #endif
327  for (xercesc::DOMNode* iter = userinfoElement->getFirstChild();
328  iter != 0; iter = iter->getNextSibling())
329  {
330  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
331 
332  const xercesc::DOMElement* const child
333  = dynamic_cast<xercesc::DOMElement*>(iter);
334  if (!child)
335  {
336  G4Exception("G4GDMLRead::UserinfoRead()",
337  "InvalidRead", FatalException, "No child found!");
338  return;
339  }
340  const G4String tag = Transcode(child->getTagName());
341 
342  if (tag=="auxiliary")
343  {
344  auxGlobalList.push_back(AuxiliaryRead(child));
345  }
346  else
347  {
348  G4String error_msg = "Unknown tag in structure: " + tag;
349  G4Exception("G4GDMLRead::UserinfoRead()",
350  "ReadError", FatalException, error_msg);
351  }
352  }
353 }
354 
355 void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
356 {
357  G4String error_msg = "No handle to user-code for parsing extensions!";
358  G4Exception("G4GDMLRead::ExtensionRead()",
359  "NotImplemented", JustWarning, error_msg);
360 }
361 
362 void G4GDMLRead::Read(const G4String& fileName,
363  G4bool validation,
364  G4bool isModule,
365  G4bool strip)
366 {
367  dostrip = strip;
368 #ifdef G4VERBOSE
369  if (isModule)
370  {
371  G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
372  }
373  else
374  {
375  G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
376  }
377 #endif
378  inLoop = 0;
379  validate = validation;
380 
381  xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
382  xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
383 
384  if (validate)
385  {
386  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
387  }
388  parser->setValidationSchemaFullChecking(validate);
389  parser->setCreateEntityReferenceNodes(false);
390  // Entities will be automatically resolved by Xerces
391 
392  parser->setDoNamespaces(true);
393  parser->setDoSchema(validate);
394  parser->setErrorHandler(handler);
395 
396  try { parser->parse(fileName.c_str()); }
397  catch (const xercesc::XMLException &e)
398  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
399  catch (const xercesc::DOMException &e)
400  { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
401 
402  xercesc::DOMDocument* doc = parser->getDocument();
403 
404  if (!doc)
405  {
406  G4String error_msg = "Unable to open document: " + fileName;
407  G4Exception("G4GDMLRead::Read()", "InvalidRead",
408  FatalException, error_msg);
409  return;
410  }
411  xercesc::DOMElement* element = doc->getDocumentElement();
412 
413  if (!element)
414  {
415  std::ostringstream message;
416  message << "ERROR - Empty document or unable to validate schema!" << G4endl
417  << " Check Internet connection is ON in case of schema"
418  << G4endl
419  << " validation enabled and location defined as URL in"
420  << G4endl
421  << " the GDML file - " << fileName << " - being imported!"
422  << G4endl
423  << " Otherwise, verify GDML schema server is reachable!";
424  G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException, message);
425  return;
426  }
427 
428  for (xercesc::DOMNode* iter = element->getFirstChild();
429  iter != 0; iter = iter->getNextSibling())
430  {
431  if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
432 
433  const xercesc::DOMElement* const child
434  = dynamic_cast<xercesc::DOMElement*>(iter);
435  if (!child)
436  {
437  G4Exception("G4GDMLRead::Read()", "InvalidRead",
438  FatalException, "No child found!");
439  return;
440  }
441  const G4String tag = Transcode(child->getTagName());
442 
443  if (tag=="define") { DefineRead(child); } else
444  if (tag=="materials") { MaterialsRead(child); } else
445  if (tag=="solids") { SolidsRead(child); } else
446  if (tag=="setup") { SetupRead(child); } else
447  if (tag=="structure") { StructureRead(child); } else
448  if (tag=="userinfo") { UserinfoRead(child); } else
449  if (tag=="extension") { ExtensionRead(child); }
450  else
451  {
452  G4String error_msg = "Unknown tag in gdml: " + tag;
453  G4Exception("G4GDMLRead::Read()", "InvalidRead",
454  FatalException, error_msg);
455  }
456  }
457 
458  delete parser;
459  delete handler;
460 
461  if (isModule)
462  {
463 #ifdef G4VERBOSE
464  G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
465 #endif
466  }
467  else
468  {
469  G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
470  if (strip) { StripNames(); }
471  }
472 }
473 
475 {
476  return &auxGlobalList;
477 }
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:155
void SetName(const G4String &name)
Definition: G4Element.hh:214
G4String & remove(str_size)
const XML_Char * name
Definition: expat.h:151
void SetName(const G4String &pName)
G4LogicalVolume * GetLogicalVolume() const
G4bool validate
Definition: G4GDMLRead.hh:156
void Read(const G4String &, G4bool validation, G4bool isModule, G4bool strip=true)
Definition: G4GDMLRead.cc:362
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:593
const G4String & GetName() const
Definition: G4Element.hh:127
void StripNames() const
Definition: G4GDMLRead.cc:108
const G4GDMLAuxListType * GetAuxList() const
Definition: G4GDMLRead.cc:474
void SetName(const G4String &name)
Definition: G4Material.hh:279
#define G4endl
Definition: G4ios.hh:61
void LoopRead(const xercesc::DOMElement *const, void(G4GDMLRead::*)(const xercesc::DOMElement *const))
Definition: G4GDMLRead.cc:176
virtual ~G4GDMLRead()
Definition: G4GDMLRead.cc:51
Definition: xmlparse.cc:187
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
G4bool dostrip
Definition: G4GDMLRead.hh:158
G4String GenerateName(const G4String &name, G4bool strip=false)
Definition: G4GDMLRead.cc:68
virtual void DefineRead(const xercesc::DOMElement *const)=0
virtual void SolidsRead(const xercesc::DOMElement *const)=0
virtual void StructureRead(const xercesc::DOMElement *const)=0
const G4String & GetName() const
Definition: G4Material.hh:179
G4GDMLAuxStructType AuxiliaryRead(const xercesc::DOMElement *const auxElem)
Definition: G4GDMLRead.cc:262
void GeneratePhysvolName(const G4String &, G4VPhysicalVolume *)
Definition: G4GDMLRead.cc:81
bool G4bool
Definition: G4Types.hh:79
G4int loopCount
Definition: G4GDMLRead.hh:162
G4String Strip(const G4String &) const
Definition: G4GDMLRead.cc:97
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
std::vector< G4Element * > G4ElementTable
std::vector< G4GDMLAuxStructType > * auxList
void SetName(const G4String &name)
CommandLineParser * parser(0)
G4String SolveBrackets(const G4String &)
void StripName(G4String &) const
Definition: G4GDMLRead.cc:103
std::vector< G4Material * > G4MaterialTable
static G4SolidStore * GetInstance()
G4GDMLAuxListType auxGlobalList
Definition: G4GDMLRead.hh:163
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
void OverlapCheck(G4bool)
Definition: G4GDMLRead.cc:63
G4bool check
Definition: G4GDMLRead.hh:157
static G4PhysicalVolumeStore * GetInstance()
int G4int
Definition: G4Types.hh:78
virtual void SetupRead(const xercesc::DOMElement *const)=0
G4String GetName() const
void SetName(const G4String &pName)
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
G4GLOB_DLL std::ostream G4cout
G4bool IsVariable(const G4String &) const
G4int EvaluateInteger(const G4String &)
virtual void MaterialsRead(const xercesc::DOMElement *const)=0
G4int inLoop
Definition: G4GDMLRead.hh:162
void SetVariable(const G4String &, G4double)
static G4LogicalVolumeStore * GetInstance()
const G4String & GetName() const
virtual void ExtensionRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:355
virtual void UserinfoRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:322
const G4String & GetName() const
static void BuildUnitsTable()
static G4ElementTable * GetElementTable()
Definition: G4Element.cc:398