source: trunk/source/persistency/gdml/src/G4GDMLRead.cc @ 1347

Last change on this file since 1347 was 1347, checked in by garnier, 13 years ago

geant4 tag 9.4

File size: 10.7 KB
Line 
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,v 1.50 2010/11/17 10:47:02 gcosmo Exp $
27// GEANT4 tag $Name: gdml-V09-03-09 $
28//
29// class G4GDMLRead Implementation
30//
31// History:
32// - Created.                                  Zoltan Torzsok, November 2007
33// -------------------------------------------------------------------------
34
35#include "globals.hh"
36
37#include "G4GDMLRead.hh"
38
39#include "G4UnitsTable.hh"
40#include "G4Element.hh"
41#include "G4Material.hh"
42#include "G4SolidStore.hh"
43#include "G4LogicalVolumeStore.hh"
44#include "G4PhysicalVolumeStore.hh"
45
46G4GDMLRead::G4GDMLRead()
47  : validate(true), check(false), inLoop(0), loopCount(0)
48{
49   G4UnitDefinition::BuildUnitsTable();
50}
51
52G4GDMLRead::~G4GDMLRead()
53{
54}
55
56G4String G4GDMLRead::Transcode(const XMLCh* const toTranscode)
57{
58   char* char_str = xercesc::XMLString::transcode(toTranscode);
59   G4String my_str(char_str);
60   xercesc::XMLString::release(&char_str);
61   return my_str;
62}
63
64void G4GDMLRead::OverlapCheck(G4bool flag)
65{
66   check = flag;
67}
68
69G4String G4GDMLRead::GenerateName(const G4String& nameIn, G4bool strip)
70{
71   G4String nameOut(nameIn);
72
73   if (inLoop>0)
74   {
75     nameOut = eval.SolveBrackets(nameOut);
76//     std::stringstream stream;
77//     stream << "0x" << loopCount;
78//     nameOut = nameOut + stream.str();
79   }
80   if (strip) { StripName(nameOut); }
81
82   return nameOut;
83}
84
85void G4GDMLRead::GeneratePhysvolName(const G4String& nameIn,
86                                     G4VPhysicalVolume* physvol)
87{
88   G4String nameOut(nameIn);
89
90   if (nameIn.empty())
91   {
92     std::stringstream stream;
93     stream << physvol->GetLogicalVolume()->GetName() << "_PV";
94     nameOut = stream.str();
95   }
96   nameOut = eval.SolveBrackets(nameOut);
97
98   physvol->SetName(nameOut);
99}
100
101G4String G4GDMLRead::Strip(const G4String& name) const
102{
103  G4String sname(name);
104  return sname.remove(sname.find("0x"));
105}
106
107void G4GDMLRead::StripName(G4String& name) const
108{
109  name.remove(name.find("0x"));
110}
111
112void G4GDMLRead::StripNames() const
113{
114  // Strips off names of volumes, solids elements and materials from possible
115  // reference pointers or IDs attached to their original identifiers.
116
117  G4PhysicalVolumeStore* pvols = G4PhysicalVolumeStore::GetInstance();
118  G4LogicalVolumeStore* lvols = G4LogicalVolumeStore::GetInstance();
119  G4SolidStore* solids = G4SolidStore::GetInstance();
120  const G4ElementTable* elements = G4Element::GetElementTable();
121  const G4MaterialTable* materials = G4Material::GetMaterialTable();
122
123  G4cout << "Stripping off GDML names of materials, solids and volumes ..."
124         << G4endl;
125
126  G4String sname;
127  register size_t i;
128
129  // Solids...
130  //
131  for (i=0; i<solids->size(); i++)
132  {
133    G4VSolid* psol = (*solids)[i];
134    sname = psol->GetName();
135    StripName(sname);
136    psol->SetName(sname);
137  }
138
139  // Logical volumes...
140  //
141  for (i=0; i<lvols->size(); i++)
142  {
143    G4LogicalVolume* lvol = (*lvols)[i];
144    sname = lvol->GetName();
145    StripName(sname);
146    lvol->SetName(sname);
147  }
148
149  // Physical volumes...
150  //
151  for (i=0; i<pvols->size(); i++)
152  {
153    G4VPhysicalVolume* pvol = (*pvols)[i];
154    sname = pvol->GetName();
155    StripName(sname);
156    pvol->SetName(sname);
157  }
158
159  // Materials...
160  //
161  for (i=0; i<materials->size(); i++)
162  {
163    G4Material* pmat = (*materials)[i];
164    sname = pmat->GetName();
165    StripName(sname);
166    pmat->SetName(sname);
167  }
168
169  // Elements...
170  //
171  for (i=0; i<elements->size(); i++)
172  {
173    G4Element* pelm = (*elements)[i];
174    sname = pelm->GetName();
175    StripName(sname);
176    pelm->SetName(sname);
177  }
178}
179
180void G4GDMLRead::LoopRead(const xercesc::DOMElement* const element,
181     void(G4GDMLRead::*func)(const xercesc::DOMElement* const))
182{
183   G4String var;
184   G4String from;
185   G4String to;
186   G4String step;
187
188   const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
189   XMLSize_t attributeCount = attributes->getLength();
190
191   for (XMLSize_t attribute_index=0;
192        attribute_index<attributeCount;attribute_index++)
193   {
194      xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
195
196      if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
197      { continue; }
198
199      const xercesc::DOMAttr* const attribute
200            = dynamic_cast<xercesc::DOMAttr*>(attribute_node);   
201      if (!attribute)
202      {
203        G4Exception("G4GDMLRead::LoopRead()", "InvalidRead",
204                    FatalException, "No attribute found!");
205        return;
206      }
207      const G4String attribute_name = Transcode(attribute->getName());
208      const G4String attribute_value = Transcode(attribute->getValue());
209
210      if (attribute_name=="for")  { var = attribute_value; }  else
211      if (attribute_name=="from") { from = attribute_value; } else
212      if (attribute_name=="to")   { to = attribute_value; }   else
213      if (attribute_name=="step") { step = attribute_value; }
214   }
215
216   if (var.empty())
217   {
218     G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
219                 FatalException, "No variable is determined for loop!");
220   }
221
222   if (!eval.IsVariable(var))
223   {
224     G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
225                 FatalException, "Variable is not defined in loop!");
226   }
227
228   G4int _var = eval.EvaluateInteger(var);
229   G4int _from = eval.EvaluateInteger(from);
230   G4int _to = eval.EvaluateInteger(to);
231   G4int _step = eval.EvaluateInteger(step);
232   
233   if (!from.empty()) { _var = _from; }
234
235   if (_from == _to)
236   {
237     G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
238                 FatalException, "Empty loop!");
239   }
240   if ((_from < _to) && (_step <= 0))
241   {
242     G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
243                 FatalException, "Infinite loop!");
244   }
245   if ((_from > _to) && (_step >= 0))
246   {
247     G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
248                 FatalException, "Infinite loop!");
249   }
250
251   inLoop++;
252
253   while (_var <= _to)
254   {
255      eval.SetVariable(var,_var);
256      (this->*func)(element);
257      _var += _step;
258      loopCount++;
259   }
260
261   inLoop--;
262   if (!inLoop) { loopCount = 0; }
263}
264
265void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
266{
267   G4String error_msg = "No handle to user-code for parsing extensions!";
268   G4Exception("G4GDMLRead::ExtensionRead()",
269               "NotImplemented", JustWarning, error_msg);
270}
271
272void G4GDMLRead::Read(const G4String& fileName,
273                            G4bool validation,
274                            G4bool isModule,
275                            G4bool strip)
276{
277   if (isModule)
278   {
279      G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
280   }
281   else
282   {
283      G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
284   }
285
286   inLoop = 0;
287   validate = validation;
288
289   xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
290   xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
291
292   parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
293   parser->setValidationSchemaFullChecking(true);
294   parser->setCreateEntityReferenceNodes(false); 
295     // Entities will be automatically resolved by Xerces
296
297   parser->setDoNamespaces(true);
298   parser->setDoSchema(true);
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     G4Exception("G4GDMLRead::Read()", "InvalidRead",
321                 FatalException, "Empty document!");
322     return;
323   }
324
325   for (xercesc::DOMNode* iter = element->getFirstChild();
326        iter != 0; iter = iter->getNextSibling())
327   {
328      if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)  { continue; }
329
330      const xercesc::DOMElement* const child
331            = dynamic_cast<xercesc::DOMElement*>(iter);
332      if (!child)
333      {
334        G4Exception("G4GDMLRead::Read()", "InvalidRead",
335                    FatalException, "No child found!");
336        return;
337      }
338      const G4String tag = Transcode(child->getTagName());
339
340      if (tag=="define")    { DefineRead(child);    } else
341      if (tag=="materials") { MaterialsRead(child); } else
342      if (tag=="solids")    { SolidsRead(child);    } else
343      if (tag=="setup")     { SetupRead(child);     } else
344      if (tag=="structure") { StructureRead(child); } else
345      if (tag=="extension") { ExtensionRead(child); }
346      else
347      {
348        G4String error_msg = "Unknown tag in gdml: " + tag;
349        G4Exception("G4GDMLRead::Read()", "InvalidRead",
350                    FatalException, error_msg);
351      }
352   }
353
354   delete parser;
355   delete handler;
356
357   if (isModule)
358   {
359      G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
360   }
361   else
362   {
363      G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
364      if (strip)  { StripNames(); }
365   }
366}
Note: See TracBrowser for help on using the repository browser.