Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GDMLWrite.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 //
27 // $Id: G4GDMLWrite.cc 96170 2016-03-22 09:17:29Z gcosmo $
28 //
29 // class G4GDMLWrite Implementation
30 //
31 // Original author: Zoltan Torzsok, November 2007
32 //
33 // --------------------------------------------------------------------
34 
35 #include <sys/stat.h>
36 #include <iostream>
37 
38 #include "G4GDMLWrite.hh"
39 
40 #include "G4LogicalVolume.hh"
41 #include "G4Transform3D.hh"
42 #include "G4PVDivision.hh"
43 
45 
46 G4GDMLWrite::G4GDMLWrite() : doc(0), extElement(0)
47 {
48 }
49 
51 {
52 }
53 
55 {
56  struct stat FileInfo;
57  return (stat(fname.c_str(),&FileInfo) == 0);
58 }
59 
60 G4GDMLWrite::VolumeMapType& G4GDMLWrite::VolumeMap()
61 {
62  static VolumeMapType instance;
63  return instance;
64 }
65 
66 G4GDMLWrite::PhysVolumeMapType& G4GDMLWrite::PvolumeMap()
67 {
68  static PhysVolumeMapType instance;
69  return instance;
70 }
71 
72 G4GDMLWrite::DepthMapType& G4GDMLWrite::DepthMap()
73 {
74  static DepthMapType instance;
75  return instance;
76 }
77 
78 void G4GDMLWrite::AddExtension(xercesc::DOMElement*,
79  const G4LogicalVolume* const)
80 {
81  // Empty implementation. To be overwritten by user for specific extensions
82  // related to attributes associated to volumes
83 }
84 
85 void G4GDMLWrite::ExtensionWrite(xercesc::DOMElement*)
86 {
87  // Empty implementation. To be overwritten by user for specific extensions
88 }
89 
91  xercesc::DOMElement* element)
92 {
93  for(std::vector<G4GDMLAuxStructType>::const_iterator
94  iaux = auxInfoList->begin(); iaux != auxInfoList->end(); iaux++ )
95  {
96  xercesc::DOMElement* auxiliaryElement = NewElement("auxiliary");
97  element->appendChild(auxiliaryElement);
98 
99  auxiliaryElement->setAttributeNode(NewAttribute("auxtype", (*iaux).type));
100  auxiliaryElement->setAttributeNode(NewAttribute("auxvalue", (*iaux).value));
101  if (((*iaux).unit)!="")
102  {
103  auxiliaryElement->setAttributeNode(NewAttribute("auxunit", (*iaux).unit));
104  }
105 
106  if (iaux->auxList) { AddAuxInfo(iaux->auxList, auxiliaryElement); }
107  }
108  return;
109 }
110 
111 void G4GDMLWrite::UserinfoWrite(xercesc::DOMElement* gdmlElement)
112 {
113  if(auxList.size()>0)
114  {
115  G4cout << "G4GDML: Writing userinfo..." << G4endl;
116 
117  userinfoElement = NewElement("userinfo");
118  gdmlElement->appendChild(userinfoElement);
120  }
121 }
122 
123 G4String G4GDMLWrite::GenerateName(const G4String& name, const void* const ptr)
124 {
125  G4String nameOut;
126  std::stringstream stream; stream << name;
127  if (addPointerToName) { stream << ptr; };
128 
129  nameOut=G4String(stream.str());
130  if(nameOut.contains(' '))
131  nameOut.erase(std::remove(nameOut.begin(),nameOut.end(),' '),nameOut.end());
132 
133  return nameOut;
134 }
135 
136 xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
137  const G4String& value)
138 {
139  xercesc::XMLString::transcode(name,tempStr,9999);
140  xercesc::DOMAttr* att = doc->createAttribute(tempStr);
141  xercesc::XMLString::transcode(value,tempStr,9999);
142  att->setValue(tempStr);
143  return att;
144 }
145 
146 xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
147  const G4double& value)
148 {
149  xercesc::XMLString::transcode(name,tempStr,9999);
150  xercesc::DOMAttr* att = doc->createAttribute(tempStr);
151  std::ostringstream ostream;
152  ostream.precision(15);
153  ostream << value;
154  G4String str = ostream.str();
155  xercesc::XMLString::transcode(str,tempStr,9999);
156  att->setValue(tempStr);
157  return att;
158 }
159 
160 xercesc::DOMElement* G4GDMLWrite::NewElement(const G4String& name)
161 {
162  xercesc::XMLString::transcode(name,tempStr,9999);
163  return doc->createElement(tempStr);
164 }
165 
167  const G4LogicalVolume* const logvol,
168  const G4String& setSchemaLocation,
169  const G4int depth,
170  G4bool refs)
171 {
172  SchemaLocation = setSchemaLocation;
173  addPointerToName = refs;
174 
175  if (depth==0) { G4cout << "G4GDML: Writing '" << fname << "'..." << G4endl; }
176  else { G4cout << "G4GDML: Writing module '" << fname << "'..." << G4endl; }
177 
178  if (FileExists(fname))
179  {
180  G4String ErrorMessage = "File '"+fname+"' already exists!";
181  G4Exception("G4GDMLWrite::Write()", "InvalidSetup",
182  FatalException, ErrorMessage);
183  }
184 
185  VolumeMap().clear(); // The module map is global for all modules,
186  // so clear it only at once!
187 
188  xercesc::XMLString::transcode("LS", tempStr, 9999);
189  xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
190  xercesc::XMLString::transcode("Range", tempStr, 9999);
191  xercesc::DOMImplementation* impl =
192  xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
193  xercesc::XMLString::transcode("gdml", tempStr, 9999);
194  doc = impl->createDocument(0,tempStr,0);
195  xercesc::DOMElement* gdml = doc->getDocumentElement();
196 
197 #if XERCES_VERSION_MAJOR >= 3
198  // DOM L3 as per Xerces 3.0 API
199  xercesc::DOMLSSerializer* writer =
200  ((xercesc::DOMImplementationLS*)impl)->createLSSerializer();
201 
202  xercesc::DOMConfiguration *dc = writer->getDomConfig();
203  dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
204 
205 #else
206 
207  xercesc::DOMWriter* writer =
208  ((xercesc::DOMImplementationLS*)impl)->createDOMWriter();
209 
210  if (writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
211  writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
212 
213 #endif
214 
215  gdml->setAttributeNode(NewAttribute("xmlns:xsi",
216  "http://www.w3.org/2001/XMLSchema-instance"));
217  gdml->setAttributeNode(NewAttribute("xsi:noNamespaceSchemaLocation",
218  SchemaLocation));
219 
220  ExtensionWrite(gdml);
221  DefineWrite(gdml);
222  MaterialsWrite(gdml);
223  SolidsWrite(gdml);
224  StructureWrite(gdml);
225  UserinfoWrite(gdml);
226  SetupWrite(gdml,logvol);
227 
228  G4Transform3D R = TraverseVolumeTree(logvol,depth);
229 
230  SurfacesWrite();
231  xercesc::XMLFormatTarget *myFormTarget =
232  new xercesc::LocalFileFormatTarget(fname.c_str());
233 
234  try
235  {
236 #if XERCES_VERSION_MAJOR >= 3
237  // DOM L3 as per Xerces 3.0 API
238  xercesc::DOMLSOutput *theOutput =
239  ((xercesc::DOMImplementationLS*)impl)->createLSOutput();
240  theOutput->setByteStream(myFormTarget);
241  writer->write(doc, theOutput);
242 #else
243  writer->writeNode(myFormTarget, *doc);
244 #endif
245  }
246  catch (const xercesc::XMLException& toCatch)
247  {
248  char* message = xercesc::XMLString::transcode(toCatch.getMessage());
249  G4cout << "G4GDML: Exception message is: " << message << G4endl;
250  xercesc::XMLString::release(&message);
252  }
253  catch (const xercesc::DOMException& toCatch)
254  {
255  char* message = xercesc::XMLString::transcode(toCatch.msg);
256  G4cout << "G4GDML: Exception message is: " << message << G4endl;
257  xercesc::XMLString::release(&message);
259  }
260  catch (...)
261  {
262  G4cout << "G4GDML: Unexpected Exception!" << G4endl;
264  }
265 
266  delete myFormTarget;
267  writer->release();
268 
269  if (depth==0)
270  {
271  G4cout << "G4GDML: Writing '" << fname << "' done !" << G4endl;
272  }
273  else
274  {
275  G4cout << "G4GDML: Writing module '" << fname << "' done !" << G4endl;
276  }
277 
278  return R;
279 }
280 
281 void G4GDMLWrite::AddModule(const G4VPhysicalVolume* const physvol)
282 {
283  G4String fname = GenerateName(physvol->GetName(),physvol);
284  G4cout << "G4GDML: Adding module '" << fname << "'..." << G4endl;
285 
286  if (physvol == 0)
287  {
288  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
289  "Invalid NULL pointer is specified for modularization!");
290  return;
291  }
292  if (dynamic_cast<const G4PVDivision*>(physvol))
293  {
294  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
295  "It is not possible to modularize by divisionvol!");
296  return;
297  }
298  if (physvol->IsParameterised())
299  {
300  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
301  "It is not possible to modularize by parameterised volume!");
302  return;
303  }
304  if (physvol->IsReplicated())
305  {
306  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
307  "It is not possible to modularize by replicated volume!");
308  return;
309  }
310 
311  PvolumeMap()[physvol] = fname;
312 }
313 
314 void G4GDMLWrite::AddModule(const G4int depth)
315 {
316  if (depth<0)
317  {
318  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
319  "Depth must be a positive number!");
320  }
321  if (DepthMap().find(depth) != DepthMap().end())
322  {
323  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
324  "Adding module(s) at this depth is already requested!");
325  }
326  DepthMap()[depth] = 0;
327 }
328 
330  const G4int depth )
331 {
332  if (PvolumeMap().find(physvol) != PvolumeMap().end())
333  {
334  return PvolumeMap()[physvol]; // Modularize via physvol
335  }
336 
337  if (DepthMap().find(depth) != DepthMap().end()) // Modularize via depth
338  {
339  std::stringstream stream;
340  stream << "depth" << depth << "_module" << DepthMap()[depth] << ".gdml";
341  DepthMap()[depth]++; // There can be more modules at this depth!
342  return G4String(stream.str());
343  }
344 
345  return G4String(""); // Empty string for module name = no modularization
346  // was requested at that level/physvol!
347 }
348 
350 {
351  auxList.push_back(myaux);
352 }
353 
355 {
356  addPointerToName = set;
357 }
const XML_Char * name
Definition: expat.h:151
virtual ~G4GDMLWrite()
Definition: G4GDMLWrite.cc:50
virtual void ExtensionWrite(xercesc::DOMElement *)
Definition: G4GDMLWrite.cc:85
void AddAuxiliary(G4GDMLAuxStructType myaux)
Definition: G4GDMLWrite.cc:349
void AddAuxInfo(G4GDMLAuxListType *auxInfoList, xercesc::DOMElement *element)
Definition: G4GDMLWrite.cc:90
xercesc::DOMDocument * doc
Definition: G4GDMLWrite.hh:133
G4String SchemaLocation
Definition: G4GDMLWrite.hh:131
virtual G4bool IsReplicated() const =0
G4bool FileExists(const G4String &) const
Definition: G4GDMLWrite.cc:54
PhysVolumeMapType & PvolumeMap()
Definition: G4GDMLWrite.cc:66
xercesc::DOMElement * NewElement(const G4String &)
Definition: G4GDMLWrite.cc:160
int G4int
Definition: G4Types.hh:78
DepthMapType & DepthMap()
Definition: G4GDMLWrite.cc:72
G4String Modularize(const G4VPhysicalVolume *const topvol, const G4int depth)
Definition: G4GDMLWrite.cc:329
G4Transform3D Write(const G4String &filename, const G4LogicalVolume *const topLog, const G4String &schemaPath, const G4int depth, G4bool storeReferences=true)
Definition: G4GDMLWrite.cc:166
virtual void SetupWrite(xercesc::DOMElement *, const G4LogicalVolume *const)=0
G4GLOB_DLL std::ostream G4cout
virtual void StructureWrite(xercesc::DOMElement *)=0
const XML_Char int const XML_Char * value
Definition: expat.h:331
const G4String & GetName() const
bool G4bool
Definition: G4Types.hh:79
virtual void DefineWrite(xercesc::DOMElement *)=0
virtual void MaterialsWrite(xercesc::DOMElement *)=0
G4String GenerateName(const G4String &, const void *const)
Definition: G4GDMLWrite.cc:123
static void SetAddPointerToName(G4bool)
Definition: G4GDMLWrite.cc:354
static G4bool addPointerToName
Definition: G4GDMLWrite.hh:132
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual G4bool IsParameterised() const =0
G4bool contains(const std::string &) const
void AddModule(const G4VPhysicalVolume *const topVol)
Definition: G4GDMLWrite.cc:281
xercesc::DOMAttr * NewAttribute(const G4String &, const G4String &)
Definition: G4GDMLWrite.cc:136
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
virtual G4Transform3D TraverseVolumeTree(const G4LogicalVolume *const, const G4int)=0
virtual void UserinfoWrite(xercesc::DOMElement *)
Definition: G4GDMLWrite.cc:111
virtual void SolidsWrite(xercesc::DOMElement *)=0
virtual void AddExtension(xercesc::DOMElement *, const G4LogicalVolume *const)
Definition: G4GDMLWrite.cc:78
#define G4endl
Definition: G4ios.hh:61
static MCTruthManager * instance
string fname
Definition: test.py:308
G4GDMLAuxListType auxList
Definition: G4GDMLWrite.hh:137
double G4double
Definition: G4Types.hh:76
virtual void SurfacesWrite()=0
XMLCh tempStr[10000]
Definition: G4GDMLWrite.hh:136
VolumeMapType & VolumeMap()
Definition: G4GDMLWrite.cc:60
static const Transform3D Identity
Definition: Transform3D.h:197
xercesc::DOMElement * userinfoElement
Definition: G4GDMLWrite.hh:135