Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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// G4GDMLRead implementation
27//
28// Author: Zoltan Torzsok, November 2007
29// --------------------------------------------------------------------
30
31#include "globals.hh"
32
33#include "G4GDMLRead.hh"
34
35#include "G4UnitsTable.hh"
36#include "G4Element.hh"
37#include "G4Material.hh"
38#include "G4SolidStore.hh"
41
42// --------------------------------------------------------------------
44{
45 // Make sure units are defined.
47}
48
49// --------------------------------------------------------------------
51{
52}
53
54// --------------------------------------------------------------------
55G4String 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
63// --------------------------------------------------------------------
65{
66 check = flag;
67}
68
69// --------------------------------------------------------------------
71{
72 G4String nameOut(nameIn);
73
74 if(inLoop > 0)
75 {
76 nameOut = eval.SolveBrackets(nameOut);
77 }
78 if(strip)
79 {
80 StripName(nameOut);
81 }
82
83 return nameOut;
84}
85
86// --------------------------------------------------------------------
88 G4VPhysicalVolume* physvol)
89{
90 G4String nameOut(nameIn);
91
92 if(nameIn.empty())
93 {
94 std::stringstream stream;
95 stream << physvol->GetLogicalVolume()->GetName() << "_PV";
96 nameOut = stream.str();
97 }
98 nameOut = eval.SolveBrackets(nameOut);
99
100 physvol->SetName(nameOut);
101}
102
103// --------------------------------------------------------------------
105{
106 G4String sname(name);
107 return sname.remove(sname.find("0x"));
108}
109
110// --------------------------------------------------------------------
112{
113 name.remove(name.find("0x"));
114}
115
116// --------------------------------------------------------------------
118{
119 // Strips off names of volumes, solids elements and materials from possible
120 // reference pointers or IDs attached to their original identifiers.
121
125 const G4ElementTable* elements = G4Element::GetElementTable();
127
128 G4cout << "Stripping off GDML names of materials, solids and volumes ..."
129 << G4endl;
130
131 G4String sname;
132 std::size_t i;
133
134 // Solids...
135 //
136 for(i = 0; i < solids->size(); ++i)
137 {
138 G4VSolid* psol = (*solids)[i];
139 sname = psol->GetName();
140 StripName(sname);
141 psol->SetName(sname);
142 }
143
144 // Logical volumes...
145 //
146 for(i = 0; i < lvols->size(); ++i)
147 {
148 G4LogicalVolume* lvol = (*lvols)[i];
149 sname = lvol->GetName();
150 StripName(sname);
151 lvol->SetName(sname);
152 }
153
154 // Physical volumes...
155 //
156 for(i = 0; i < pvols->size(); ++i)
157 {
158 G4VPhysicalVolume* pvol = (*pvols)[i];
159 sname = pvol->GetName();
160 StripName(sname);
161 pvol->SetName(sname);
162 }
163
164 // Materials...
165 //
166 for(i = 0; i < materials->size(); ++i)
167 {
168 G4Material* pmat = (*materials)[i];
169 sname = pmat->GetName();
170 StripName(sname);
171 pmat->SetName(sname);
172 }
173
174 // Elements...
175 //
176 for(i = 0; i < elements->size(); ++i)
177 {
178 G4Element* pelm = (*elements)[i];
179 sname = pelm->GetName();
180 StripName(sname);
181 pelm->SetName(sname);
182 }
183}
184
185// --------------------------------------------------------------------
186void G4GDMLRead::LoopRead( const xercesc::DOMElement* const element,
187 void (G4GDMLRead::*func)(const xercesc::DOMElement* const))
188{
189 G4String var;
190 G4String from;
191 G4String to;
192 G4String step;
193
194 const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
195 XMLSize_t attributeCount = attributes->getLength();
196
197 for(XMLSize_t attribute_index = 0; attribute_index < attributeCount;
198 ++attribute_index)
199 {
200 xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
201
202 if(attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
203 {
204 continue;
205 }
206
207 const xercesc::DOMAttr* const attribute =
208 dynamic_cast<xercesc::DOMAttr*>(attribute_node);
209 if(!attribute)
210 {
211 G4Exception("G4GDMLRead::LoopRead()", "InvalidRead", FatalException,
212 "No attribute found!");
213 return;
214 }
215 const G4String attribute_name = Transcode(attribute->getName());
216 const G4String attribute_value = Transcode(attribute->getValue());
217
218 if(attribute_name == "for")
219 {
220 var = attribute_value;
221 }
222 else if(attribute_name == "from")
223 {
224 from = attribute_value;
225 }
226 else if(attribute_name == "to")
227 {
228 to = attribute_value;
229 }
230 else if(attribute_name == "step")
231 {
232 step = attribute_value;
233 }
234 }
235
236 if(var.empty())
237 {
238 G4Exception("G4GDMLRead::loopRead()", "InvalidRead", FatalException,
239 "No variable is determined for loop!");
240 }
241
242 if(!eval.IsVariable(var))
243 {
244 G4Exception("G4GDMLRead::loopRead()", "InvalidRead", FatalException,
245 "Variable is not defined in loop!");
246 }
247
248 G4int _var = eval.EvaluateInteger(var);
249 G4int _from = eval.EvaluateInteger(from);
250 G4int _to = eval.EvaluateInteger(to);
251 G4int _step = eval.EvaluateInteger(step);
252
253 if(!from.empty())
254 {
255 _var = _from;
256 }
257
258 if((_from < _to) && (_step <= 0))
259 {
260 G4Exception("G4GDMLRead::loopRead()", "InvalidRead", FatalException,
261 "Infinite loop!");
262 }
263 if((_from > _to) && (_step >= 0))
264 {
265 G4Exception("G4GDMLRead::loopRead()", "InvalidRead", FatalException,
266 "Infinite loop!");
267 }
268
269 ++inLoop;
270
271 while(_var <= _to)
272 {
273 eval.SetVariable(var, _var);
274 (this->*func)(element);
275 _var += _step;
276 ++loopCount;
277 }
278
279 --inLoop;
280 if(!inLoop)
281 {
282 loopCount = 0;
283 }
284}
285
286// --------------------------------------------------------------------
288 const xercesc::DOMElement* const auxiliaryElement)
289{
290 G4GDMLAuxStructType auxstruct = { "", "", "", 0 };
291 G4GDMLAuxListType* auxList = nullptr;
292
293 const xercesc::DOMNamedNodeMap* const attributes =
294 auxiliaryElement->getAttributes();
295 XMLSize_t attributeCount = attributes->getLength();
296
297 for(XMLSize_t attribute_index = 0; attribute_index < attributeCount;
298 ++attribute_index)
299 {
300 xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
301
302 if(attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
303 {
304 continue;
305 }
306
307 const xercesc::DOMAttr* const attribute =
308 dynamic_cast<xercesc::DOMAttr*>(attribute_node);
309 if(!attribute)
310 {
311 G4Exception("G4GDMLRead::AuxiliaryRead()", "InvalidRead", FatalException,
312 "No attribute found!");
313 return auxstruct;
314 }
315 const G4String attName = Transcode(attribute->getName());
316 const G4String attValue = Transcode(attribute->getValue());
317
318 if(attName == "auxtype")
319 {
320 auxstruct.type = attValue;
321 }
322 else if(attName == "auxvalue")
323 {
324 auxstruct.value = attValue;
325 }
326 else if(attName == "auxunit")
327 {
328 auxstruct.unit = attValue;
329 }
330 }
331
332 for(xercesc::DOMNode* iter = auxiliaryElement->getFirstChild();
333 iter != nullptr; iter = iter->getNextSibling())
334 {
335 if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
336 {
337 continue;
338 }
339
340 const xercesc::DOMElement* const child =
341 dynamic_cast<xercesc::DOMElement*>(iter);
342 if(!child)
343 {
344 G4Exception("G4GDMLRead::AuxiliaryRead()", "InvalidRead", FatalException,
345 "No child found!");
346 break;
347 }
348 const G4String tag = Transcode(child->getTagName());
349
350 if(tag == "auxiliary")
351 {
352 if(!auxList)
353 {
354 auxList = new G4GDMLAuxListType;
355 }
356 auxList->push_back(AuxiliaryRead(child));
357 }
358 }
359
360 if(auxList)
361 {
362 auxstruct.auxList = auxList;
363 }
364
365 return auxstruct;
366}
367
368// --------------------------------------------------------------------
369void G4GDMLRead::UserinfoRead(const xercesc::DOMElement* const userinfoElement)
370{
371#ifdef G4VERBOSE
372 G4cout << "G4GDML: Reading userinfo..." << G4endl;
373#endif
374 for(xercesc::DOMNode* iter = userinfoElement->getFirstChild();
375 iter != nullptr; iter = iter->getNextSibling())
376 {
377 if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
378 {
379 continue;
380 }
381
382 const xercesc::DOMElement* const child =
383 dynamic_cast<xercesc::DOMElement*>(iter);
384 if(!child)
385 {
386 G4Exception("G4GDMLRead::UserinfoRead()", "InvalidRead", FatalException,
387 "No child found!");
388 return;
389 }
390 const G4String tag = Transcode(child->getTagName());
391
392 if(tag == "auxiliary")
393 {
394 auxGlobalList.push_back(AuxiliaryRead(child));
395 }
396 else
397 {
398 G4String error_msg = "Unknown tag in structure: " + tag;
399 G4Exception("G4GDMLRead::UserinfoRead()", "ReadError", FatalException,
400 error_msg);
401 }
402 }
403}
404
405// --------------------------------------------------------------------
406void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
407{
408 G4String error_msg = "No handle to user-code for parsing extensions!";
409 G4Exception("G4GDMLRead::ExtensionRead()", "NotImplemented", JustWarning,
410 error_msg);
411}
412
413// --------------------------------------------------------------------
414void G4GDMLRead::Read(const G4String& fileName, G4bool validation,
415 G4bool isModule, G4bool strip)
416{
417 dostrip = strip;
418#ifdef G4VERBOSE
419 if(isModule)
420 {
421 G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
422 }
423 else
424 {
425 G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
426 }
427#endif
428 inLoop = 0;
429 validate = validation;
430
431 xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
432 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
433
434 if(validate)
435 {
436 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
437 }
438 parser->setValidationSchemaFullChecking(validate);
439 parser->setCreateEntityReferenceNodes(false);
440 // Entities will be automatically resolved by Xerces
441
442 parser->setDoNamespaces(true);
443 parser->setDoSchema(validate);
444 parser->setErrorHandler(handler);
445
446 try
447 {
448 parser->parse(fileName.c_str());
449 } catch(const xercesc::XMLException& e)
450 {
451 G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl;
452 } catch(const xercesc::DOMException& e)
453 {
454 G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl;
455 }
456
457 xercesc::DOMDocument* doc = parser->getDocument();
458
459 if(doc == nullptr)
460 {
461 G4String error_msg = "Unable to open document: " + fileName;
462 G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException, error_msg);
463 return;
464 }
465 xercesc::DOMElement* element = doc->getDocumentElement();
466
467 if(element == nullptr )
468 {
469 std::ostringstream message;
470 message << "ERROR - Empty document or unable to validate schema!" << G4endl
471 << " Check Internet connection is ON in case of schema"
472 << G4endl
473 << " validation enabled and location defined as URL in"
474 << G4endl << " the GDML file - " << fileName
475 << " - being imported!" << G4endl
476 << " Otherwise, verify GDML schema server is reachable!";
477 G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException, message);
478 return;
479 }
480
481 for(xercesc::DOMNode* iter = element->getFirstChild(); iter != nullptr;
482 iter = iter->getNextSibling())
483 {
484 if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
485 {
486 continue;
487 }
488
489 const xercesc::DOMElement* const child =
490 dynamic_cast<xercesc::DOMElement*>(iter);
491 if(child == nullptr)
492 {
493 G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException,
494 "No child found!");
495 return;
496 }
497 const G4String tag = Transcode(child->getTagName());
498
499 if(tag == "define")
500 {
501 DefineRead(child);
502 }
503 else if(tag == "materials")
504 {
505 MaterialsRead(child);
506 }
507 else if(tag == "solids")
508 {
509 SolidsRead(child);
510 }
511 else if(tag == "setup")
512 {
513 SetupRead(child);
514 }
515 else if(tag == "structure")
516 {
517 StructureRead(child);
518 }
519 else if(tag == "userinfo")
520 {
521 UserinfoRead(child);
522 }
523 else if(tag == "extension")
524 {
525 ExtensionRead(child);
526 }
527 else
528 {
529 G4String error_msg = "Unknown tag in gdml: " + tag;
530 G4Exception("G4GDMLRead::Read()", "InvalidRead", FatalException,
531 error_msg);
532 }
533 }
534
535 delete parser;
536 delete handler;
537
538 if(isModule)
539 {
540#ifdef G4VERBOSE
541 G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
542#endif
543 }
544 else
545 {
546 G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
547 if(strip)
548 {
549 StripNames();
550 }
551 }
552}
553
554// --------------------------------------------------------------------
556{
557 return &auxGlobalList;
558}
std::vector< G4Element * > G4ElementTable
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
std::vector< G4Material * > G4MaterialTable
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static G4ElementTable * GetElementTable()
Definition: G4Element.cc:397
void SetName(const G4String &name)
Definition: G4Element.hh:213
const G4String & GetName() const
Definition: G4Element.hh:126
void SetVariable(const G4String &, G4double)
G4bool IsVariable(const G4String &) const
G4int EvaluateInteger(const G4String &)
G4String SolveBrackets(const G4String &)
G4bool check
Definition: G4GDMLRead.hh:158
G4String Strip(const G4String &) const
Definition: G4GDMLRead.cc:104
G4bool validate
Definition: G4GDMLRead.hh:157
G4GDMLAuxStructType AuxiliaryRead(const xercesc::DOMElement *const auxElem)
Definition: G4GDMLRead.cc:287
G4bool dostrip
Definition: G4GDMLRead.hh:159
void GeneratePhysvolName(const G4String &, G4VPhysicalVolume *)
Definition: G4GDMLRead.cc:87
virtual void SolidsRead(const xercesc::DOMElement *const)=0
G4String GenerateName(const G4String &name, G4bool strip=false)
Definition: G4GDMLRead.cc:70
const G4GDMLAuxListType * GetAuxList() const
Definition: G4GDMLRead.cc:555
virtual void MaterialsRead(const xercesc::DOMElement *const)=0
void LoopRead(const xercesc::DOMElement *const, void(G4GDMLRead::*)(const xercesc::DOMElement *const))
Definition: G4GDMLRead.cc:186
void Read(const G4String &, G4bool validation, G4bool isModule, G4bool strip=true)
Definition: G4GDMLRead.cc:414
virtual void UserinfoRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:369
virtual void SetupRead(const xercesc::DOMElement *const)=0
G4String Transcode(const XMLCh *const)
Definition: G4GDMLRead.cc:55
void StripName(G4String &) const
Definition: G4GDMLRead.cc:111
virtual void ExtensionRead(const xercesc::DOMElement *const)
Definition: G4GDMLRead.cc:406
virtual void DefineRead(const xercesc::DOMElement *const)=0
virtual void StructureRead(const xercesc::DOMElement *const)=0
virtual ~G4GDMLRead()
Definition: G4GDMLRead.cc:50
G4GDMLEvaluator eval
Definition: G4GDMLRead.hh:156
void OverlapCheck(G4bool)
Definition: G4GDMLRead.cc:64
void StripNames() const
Definition: G4GDMLRead.cc:117
static G4LogicalVolumeStore * GetInstance()
void SetName(const G4String &pName)
const G4String & GetName() const
void SetName(const G4String &name)
Definition: G4Material.hh:287
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:637
const G4String & GetName() const
Definition: G4Material.hh:175
static G4PhysicalVolumeStore * GetInstance()
static G4SolidStore * GetInstance()
G4String & remove(str_size)
static G4UnitsTable & GetUnitsTable()
G4LogicalVolume * GetLogicalVolume() const
const G4String & GetName() const
void SetName(const G4String &pName)
G4String GetName() const
void SetName(const G4String &name)
std::vector< G4GDMLAuxStructType > * auxList
Definition: xmlparse.cc:187