Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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// G4GDMLWrite implementation
27//
28// Author: Zoltan Torzsok, November 2007
29// --------------------------------------------------------------------
30
31#include <sys/stat.h>
32#include <iostream>
33
34#include "G4GDMLWrite.hh"
35
36#include "G4LogicalVolume.hh"
37#include "G4Transform3D.hh"
38#include "G4PVDivision.hh"
39
41
42// --------------------------------------------------------------------
46
47// --------------------------------------------------------------------
51
52// --------------------------------------------------------------------
54{
55 struct stat FileInfo;
56 return (stat(fname.c_str(), &FileInfo) == 0);
57}
58
59// --------------------------------------------------------------------
60G4GDMLWrite::VolumeMapType& G4GDMLWrite::VolumeMap()
61{
62 static VolumeMapType instance;
63 return instance;
64}
65
66G4GDMLWrite::PhysVolumeMapType& G4GDMLWrite::PvolumeMap()
67{
68 static PhysVolumeMapType instance;
69 return instance;
70}
71
72// --------------------------------------------------------------------
73G4GDMLWrite::DepthMapType& G4GDMLWrite::DepthMap()
74{
75 static DepthMapType instance;
76 return instance;
77}
78
79// --------------------------------------------------------------------
80void G4GDMLWrite::AddExtension(xercesc::DOMElement*,
81 const G4LogicalVolume* const)
82{
83 // Empty implementation. To be overwritten by user for specific extensions
84 // related to attributes associated to volumes
85}
86
87// --------------------------------------------------------------------
88void G4GDMLWrite::ExtensionWrite(xercesc::DOMElement*)
89{
90 // Empty implementation. To be overwritten by user for specific extensions
91}
92
93// --------------------------------------------------------------------
95 xercesc::DOMElement* element)
96{
97 for(auto iaux = auxInfoList->cbegin(); iaux != auxInfoList->cend(); ++iaux)
98 {
99 xercesc::DOMElement* auxiliaryElement = NewElement("auxiliary");
100 element->appendChild(auxiliaryElement);
101
102 auxiliaryElement->setAttributeNode(NewAttribute("auxtype", (*iaux).type));
103 auxiliaryElement->setAttributeNode(NewAttribute("auxvalue", (*iaux).value));
104 if(((*iaux).unit) != "")
105 {
106 auxiliaryElement->setAttributeNode(NewAttribute("auxunit", (*iaux).unit));
107 }
108
109 if(iaux->auxList)
110 {
111 AddAuxInfo(iaux->auxList, auxiliaryElement);
112 }
113 }
114 return;
115}
116
117// --------------------------------------------------------------------
118void G4GDMLWrite::UserinfoWrite(xercesc::DOMElement* gdmlElement)
119{
120 if(auxList.size() > 0)
121 {
122#ifdef G4VERBOSE
123 G4cout << "G4GDML: Writing userinfo..." << G4endl;
124#endif
125 userinfoElement = NewElement("userinfo");
126 gdmlElement->appendChild(userinfoElement);
128 }
129}
130
131// --------------------------------------------------------------------
132G4String G4GDMLWrite::GenerateName(const G4String& name, const void* const ptr)
133{
134 G4String nameOut;
135 std::stringstream stream;
136 stream << name;
138 {
139#ifdef WIN32
140 stream << "0x";
141#endif
142 stream << ptr;
143 };
144
145 nameOut = G4String(stream.str());
146 std::vector<char> toremove = { ' ', '/', ':', '#', '+' };
147 for(auto c : toremove)
148 {
149 if(G4StrUtil::contains(nameOut, c))
150 {
151 std::replace(nameOut.begin(), nameOut.end(), c, '_');
152 }
153 }
154 return nameOut;
155}
156
157// --------------------------------------------------------------------
158xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
159 const G4String& value)
160{
161 XMLCh* tempStr = NULL;
162 tempStr = xercesc::XMLString::transcode(name);
163 xercesc::DOMAttr* att = doc->createAttribute(tempStr);
164 xercesc::XMLString::release(&tempStr);
165
166 tempStr = xercesc::XMLString::transcode(value);
167 att->setValue(tempStr);
168 xercesc::XMLString::release(&tempStr);
169
170 return att;
171}
172
173// --------------------------------------------------------------------
174xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
175 const G4double& value)
176{
177 XMLCh* tempStr = NULL;
178 tempStr = xercesc::XMLString::transcode(name);
179 xercesc::DOMAttr* att = doc->createAttribute(tempStr);
180 xercesc::XMLString::release(&tempStr);
181
182 std::ostringstream ostream;
183 ostream.precision(15);
184 ostream << value;
185 G4String str = ostream.str();
186
187 tempStr = xercesc::XMLString::transcode(str);
188 att->setValue(tempStr);
189 xercesc::XMLString::release(&tempStr);
190
191 return att;
192}
193
194// --------------------------------------------------------------------
195xercesc::DOMElement* G4GDMLWrite::NewElement(const G4String& name)
196{
197 XMLCh* tempStr = NULL;
198 tempStr = xercesc::XMLString::transcode(name);
199 xercesc::DOMElement* elem = doc->createElement(tempStr);
200 xercesc::XMLString::release(&tempStr);
201
202 return elem;
203}
204
205// --------------------------------------------------------------------
207 const G4LogicalVolume* const logvol,
208 const G4String& setSchemaLocation,
209 const G4int depth, G4bool refs)
210{
211 SchemaLocation = setSchemaLocation;
212 addPointerToName = refs;
213#ifdef G4VERBOSE
214 if(depth == 0)
215 {
216 G4cout << "G4GDML: Writing '" << fname << "'..." << G4endl;
217 }
218 else
219 {
220 G4cout << "G4GDML: Writing module '" << fname << "'..." << G4endl;
221 }
222#endif
223 if(!overwriteOutputFile && FileExists(fname))
224 {
225 G4String ErrorMessage = "File '" + fname + "' already exists!";
226 G4Exception("G4GDMLWrite::Write()", "InvalidSetup", FatalException,
227 ErrorMessage);
228 }
229
230 VolumeMap().clear(); // The module map is global for all modules,
231 // so clear it only at once!
232
233 XMLCh* tempStr = NULL;
234 tempStr = xercesc::XMLString::transcode("LS");
235 xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
236 xercesc::XMLString::release(&tempStr);
237 tempStr = xercesc::XMLString::transcode("Range");
238 xercesc::DOMImplementation* impl =
239 xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
240 xercesc::XMLString::release(&tempStr);
241 tempStr = xercesc::XMLString::transcode("gdml");
242 doc = impl->createDocument(0, tempStr, 0);
243 xercesc::XMLString::release(&tempStr);
244 xercesc::DOMElement* gdml = doc->getDocumentElement();
245
246#if XERCES_VERSION_MAJOR >= 3
247 // DOM L3 as per Xerces 3.0 API
248 xercesc::DOMLSSerializer* writer =
249 ((xercesc::DOMImplementationLS*) impl)->createLSSerializer();
250
251 xercesc::DOMConfiguration* dc = writer->getDomConfig();
252 dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
253
254#else
255
256 xercesc::DOMWriter* writer =
257 ((xercesc::DOMImplementationLS*) impl)->createDOMWriter();
258
259 if(writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
260 writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
261
262#endif
263
264 gdml->setAttributeNode(
265 NewAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));
266 gdml->setAttributeNode(
267 NewAttribute("xsi:noNamespaceSchemaLocation", SchemaLocation));
268
269 ExtensionWrite(gdml);
270 DefineWrite(gdml);
271 MaterialsWrite(gdml);
272 SolidsWrite(gdml);
273 StructureWrite(gdml);
274 UserinfoWrite(gdml);
275 SetupWrite(gdml, logvol);
276
277 G4Transform3D R = TraverseVolumeTree(logvol, depth);
278
280 xercesc::XMLFormatTarget* myFormTarget =
281 new xercesc::LocalFileFormatTarget(fname.c_str());
282
283 try
284 {
285#if XERCES_VERSION_MAJOR >= 3
286 // DOM L3 as per Xerces 3.0 API
287 xercesc::DOMLSOutput* theOutput =
288 ((xercesc::DOMImplementationLS*) impl)->createLSOutput();
289 theOutput->setByteStream(myFormTarget);
290 writer->write(doc, theOutput);
291#else
292 writer->writeNode(myFormTarget, *doc);
293#endif
294 } catch(const xercesc::XMLException& toCatch)
295 {
296 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
297 G4cout << "G4GDML: Exception message is: " << message << G4endl;
298 xercesc::XMLString::release(&message);
300 } catch(const xercesc::DOMException& toCatch)
301 {
302 char* message = xercesc::XMLString::transcode(toCatch.msg);
303 G4cout << "G4GDML: Exception message is: " << message << G4endl;
304 xercesc::XMLString::release(&message);
306 } catch(...)
307 {
308 G4cout << "G4GDML: Unexpected Exception!" << G4endl;
310 }
311
312 delete myFormTarget;
313 writer->release();
314
315 if(depth == 0)
316 {
317 G4cout << "G4GDML: Writing '" << fname << "' done !" << G4endl;
318 }
319 else
320 {
321#ifdef G4VERBOSE
322 G4cout << "G4GDML: Writing module '" << fname << "' done !" << G4endl;
323#endif
324 }
325
326 return R;
327}
328
329// --------------------------------------------------------------------
331{
332 G4String fname = GenerateName(physvol->GetName(), physvol);
333 G4cout << "G4GDML: Adding module '" << fname << "'..." << G4endl;
334
335 if(physvol == nullptr)
336 {
337 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
338 "Invalid NULL pointer is specified for modularization!");
339 return;
340 }
341 if(dynamic_cast<const G4PVDivision*>(physvol))
342 {
343 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
344 "It is not possible to modularize by divisionvol!");
345 return;
346 }
347 if(physvol->IsParameterised())
348 {
349 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
350 "It is not possible to modularize by parameterised volume!");
351 return;
352 }
353 if(physvol->IsReplicated())
354 {
355 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
356 "It is not possible to modularize by replicated volume!");
357 return;
358 }
359
360 PvolumeMap()[physvol] = fname;
361}
362
363// --------------------------------------------------------------------
365{
366 if(depth < 0)
367 {
368 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
369 "Depth must be a positive number!");
370 }
371 if(DepthMap().find(depth) != DepthMap().end())
372 {
373 G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
374 "Adding module(s) at this depth is already requested!");
375 }
376 DepthMap()[depth] = 0;
377}
378
379// --------------------------------------------------------------------
381 const G4int depth)
382{
383 if(PvolumeMap().find(physvol) != PvolumeMap().cend())
384 {
385 return PvolumeMap()[physvol]; // Modularize via physvol
386 }
387
388 if(DepthMap().find(depth) != DepthMap().cend()) // Modularize via depth
389 {
390 std::stringstream stream;
391 stream << "depth" << depth << "_module" << DepthMap()[depth] << ".gdml";
392 DepthMap()[depth]++; // There can be more modules at this depth!
393 return G4String(stream.str());
394 }
395
396 return G4String(""); // Empty string for module name = no modularization
397 // was requested at that level/physvol!
398}
399
400// --------------------------------------------------------------------
402{
403 auxList.push_back(myaux);
404}
405
406// --------------------------------------------------------------------
411
412// --------------------------------------------------------------------
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
#define elem(i, j)
G4PVDivision(const G4String &pName, G4LogicalVolume *pLogical, G4LogicalVolume *pMother, const EAxis pAxis, const G4int nReplicas, const G4double width, const G4double offset)
G4PVDivision represents many touchable detector elements differing only in their positioning....
G4TemplateRNGHelper< G4long > * G4TemplateRNGHelper< G4long >::instance
HepGeom::Transform3D G4Transform3D
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
virtual void SurfacesWrite()=0
virtual void MaterialsWrite(xercesc::DOMElement *)=0
DepthMapType & DepthMap()
G4bool FileExists(const G4String &) const
static void SetAddPointerToName(G4bool)
G4GDMLAuxListType auxList
void AddModule(const G4VPhysicalVolume *const topVol)
xercesc::DOMElement * NewElement(const G4String &)
G4String GenerateName(const G4String &, const void *const)
G4String SchemaLocation
void AddAuxiliary(G4GDMLAuxStructType myaux)
G4String Modularize(const G4VPhysicalVolume *const topvol, const G4int depth)
virtual void AddExtension(xercesc::DOMElement *, const G4LogicalVolume *const)
virtual void SetupWrite(xercesc::DOMElement *, const G4LogicalVolume *const)=0
G4Transform3D Write(const G4String &filename, const G4LogicalVolume *const topLog, const G4String &schemaPath, const G4int depth, G4bool storeReferences=true)
virtual G4Transform3D TraverseVolumeTree(const G4LogicalVolume *const, const G4int)=0
virtual ~G4GDMLWrite()
virtual void DefineWrite(xercesc::DOMElement *)=0
xercesc::DOMDocument * doc
xercesc::DOMAttr * NewAttribute(const G4String &, const G4String &)
virtual void UserinfoWrite(xercesc::DOMElement *)
void AddAuxInfo(G4GDMLAuxListType *auxInfoList, xercesc::DOMElement *element)
static G4bool addPointerToName
xercesc::DOMElement * userinfoElement
virtual void ExtensionWrite(xercesc::DOMElement *)
virtual void SolidsWrite(xercesc::DOMElement *)=0
VolumeMapType & VolumeMap()
virtual void StructureWrite(xercesc::DOMElement *)=0
G4bool overwriteOutputFile
void SetOutputFileOverwrite(G4bool flag)
PhysVolumeMapType & PvolumeMap()
G4LogicalVolume represents a leaf node or unpositioned subtree in the geometry hierarchy....
G4VPhysicalVolume is an abstract base class for the representation of a positioned volume....
virtual G4bool IsReplicated() const =0
const G4String & GetName() const
virtual G4bool IsParameterised() const =0
static DLL_API const Transform3D Identity
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.