Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4GDMLWrite.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 //
27 // $Id: G4GDMLWrite.cc 108895 2018-03-15 10:27:25Z 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 
61 {
62  static VolumeMapType instance;
63  return instance;
64 }
65 
67 {
69  return instance;
70 }
71 
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 #ifdef G4VERBOSE
116  G4cout << "G4GDML: Writing userinfo..." << G4endl;
117 #endif
118  userinfoElement = NewElement("userinfo");
119  gdmlElement->appendChild(userinfoElement);
121  }
122 }
123 
124 G4String G4GDMLWrite::GenerateName(const G4String& name, const void* const ptr)
125 {
126  G4String nameOut;
127  std::stringstream stream; stream << name;
128  if (addPointerToName) { stream << ptr; };
129 
130  nameOut=G4String(stream.str());
131  if(nameOut.contains(' '))
132  nameOut.erase(std::remove(nameOut.begin(),nameOut.end(),' '),nameOut.end());
133 
134  return nameOut;
135 }
136 
137 xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
138  const G4String& value)
139 {
140  xercesc::XMLString::transcode(name,tempStr,9999);
141  xercesc::DOMAttr* att = doc->createAttribute(tempStr);
142  xercesc::XMLString::transcode(value,tempStr,9999);
143  att->setValue(tempStr);
144  return att;
145 }
146 
147 xercesc::DOMAttr* G4GDMLWrite::NewAttribute(const G4String& name,
148  const G4double& value)
149 {
150  xercesc::XMLString::transcode(name,tempStr,9999);
151  xercesc::DOMAttr* att = doc->createAttribute(tempStr);
152  std::ostringstream ostream;
153  ostream.precision(15);
154  ostream << value;
155  G4String str = ostream.str();
156  xercesc::XMLString::transcode(str,tempStr,9999);
157  att->setValue(tempStr);
158  return att;
159 }
160 
161 xercesc::DOMElement* G4GDMLWrite::NewElement(const G4String& name)
162 {
163  xercesc::XMLString::transcode(name,tempStr,9999);
164  return doc->createElement(tempStr);
165 }
166 
168  const G4LogicalVolume* const logvol,
169  const G4String& setSchemaLocation,
170  const G4int depth,
171  G4bool refs)
172 {
173  SchemaLocation = setSchemaLocation;
174  addPointerToName = refs;
175 #ifdef G4VERBOSE
176  if (depth==0) { G4cout << "G4GDML: Writing '" << fname << "'..." << G4endl; }
177  else { G4cout << "G4GDML: Writing module '" << fname << "'..." << G4endl; }
178 #endif
179  if (FileExists(fname))
180  {
181  G4String ErrorMessage = "File '"+fname+"' already exists!";
182  G4Exception("G4GDMLWrite::Write()", "InvalidSetup",
183  FatalException, ErrorMessage);
184  }
185 
186  VolumeMap().clear(); // The module map is global for all modules,
187  // so clear it only at once!
188 
189  xercesc::XMLString::transcode("LS", tempStr, 9999);
190  xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
191  xercesc::XMLString::transcode("Range", tempStr, 9999);
192  xercesc::DOMImplementation* impl =
193  xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
194  xercesc::XMLString::transcode("gdml", tempStr, 9999);
195  doc = impl->createDocument(0,tempStr,0);
196  xercesc::DOMElement* gdml = doc->getDocumentElement();
197 
198 #if XERCES_VERSION_MAJOR >= 3
199  // DOM L3 as per Xerces 3.0 API
200  xercesc::DOMLSSerializer* writer =
201  ((xercesc::DOMImplementationLS*)impl)->createLSSerializer();
202 
203  xercesc::DOMConfiguration *dc = writer->getDomConfig();
204  dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
205 
206 #else
207 
208  xercesc::DOMWriter* writer =
209  ((xercesc::DOMImplementationLS*)impl)->createDOMWriter();
210 
211  if (writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
212  writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
213 
214 #endif
215 
216  gdml->setAttributeNode(NewAttribute("xmlns:xsi",
217  "http://www.w3.org/2001/XMLSchema-instance"));
218  gdml->setAttributeNode(NewAttribute("xsi:noNamespaceSchemaLocation",
219  SchemaLocation));
220 
221  ExtensionWrite(gdml);
222  DefineWrite(gdml);
223  MaterialsWrite(gdml);
224  SolidsWrite(gdml);
225  StructureWrite(gdml);
226  UserinfoWrite(gdml);
227  SetupWrite(gdml,logvol);
228 
229  G4Transform3D R = TraverseVolumeTree(logvol,depth);
230 
231  SurfacesWrite();
232  xercesc::XMLFormatTarget *myFormTarget =
233  new xercesc::LocalFileFormatTarget(fname.c_str());
234 
235  try
236  {
237 #if XERCES_VERSION_MAJOR >= 3
238  // DOM L3 as per Xerces 3.0 API
239  xercesc::DOMLSOutput *theOutput =
240  ((xercesc::DOMImplementationLS*)impl)->createLSOutput();
241  theOutput->setByteStream(myFormTarget);
242  writer->write(doc, theOutput);
243 #else
244  writer->writeNode(myFormTarget, *doc);
245 #endif
246  }
247  catch (const xercesc::XMLException& toCatch)
248  {
249  char* message = xercesc::XMLString::transcode(toCatch.getMessage());
250  G4cout << "G4GDML: Exception message is: " << message << G4endl;
251  xercesc::XMLString::release(&message);
253  }
254  catch (const xercesc::DOMException& toCatch)
255  {
256  char* message = xercesc::XMLString::transcode(toCatch.msg);
257  G4cout << "G4GDML: Exception message is: " << message << G4endl;
258  xercesc::XMLString::release(&message);
260  }
261  catch (...)
262  {
263  G4cout << "G4GDML: Unexpected Exception!" << G4endl;
265  }
266 
267  delete myFormTarget;
268  writer->release();
269 
270  if (depth==0)
271  {
272  G4cout << "G4GDML: Writing '" << fname << "' done !" << G4endl;
273  }
274  else
275  {
276 #ifdef G4VERBOSE
277  G4cout << "G4GDML: Writing module '" << fname << "' done !" << G4endl;
278 #endif
279  }
280 
281  return R;
282 }
283 
284 void G4GDMLWrite::AddModule(const G4VPhysicalVolume* const physvol)
285 {
286  G4String fname = GenerateName(physvol->GetName(),physvol);
287  G4cout << "G4GDML: Adding module '" << fname << "'..." << G4endl;
288 
289  if (physvol == 0)
290  {
291  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
292  "Invalid NULL pointer is specified for modularization!");
293  return;
294  }
295  if (dynamic_cast<const G4PVDivision*>(physvol))
296  {
297  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
298  "It is not possible to modularize by divisionvol!");
299  return;
300  }
301  if (physvol->IsParameterised())
302  {
303  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
304  "It is not possible to modularize by parameterised volume!");
305  return;
306  }
307  if (physvol->IsReplicated())
308  {
309  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
310  "It is not possible to modularize by replicated volume!");
311  return;
312  }
313 
314  PvolumeMap()[physvol] = fname;
315 }
316 
317 void G4GDMLWrite::AddModule(const G4int depth)
318 {
319  if (depth<0)
320  {
321  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
322  "Depth must be a positive number!");
323  }
324  if (DepthMap().find(depth) != DepthMap().end())
325  {
326  G4Exception("G4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
327  "Adding module(s) at this depth is already requested!");
328  }
329  DepthMap()[depth] = 0;
330 }
331 
333  const G4int depth )
334 {
335  if (PvolumeMap().find(physvol) != PvolumeMap().end())
336  {
337  return PvolumeMap()[physvol]; // Modularize via physvol
338  }
339 
340  if (DepthMap().find(depth) != DepthMap().end()) // Modularize via depth
341  {
342  std::stringstream stream;
343  stream << "depth" << depth << "_module" << DepthMap()[depth] << ".gdml";
344  DepthMap()[depth]++; // There can be more modules at this depth!
345  return G4String(stream.str());
346  }
347 
348  return G4String(""); // Empty string for module name = no modularization
349  // was requested at that level/physvol!
350 }
351 
353 {
354  auxList.push_back(myaux);
355 }
356 
358 {
359  addPointerToName = set;
360 }
DepthMapType & DepthMap()
Definition: G4GDMLWrite.cc:72
const XML_Char * name
Definition: expat.h:151
G4String Modularize(const G4VPhysicalVolume *const topvol, const G4int depth)
Definition: G4GDMLWrite.cc:332
virtual ~G4GDMLWrite()
Definition: G4GDMLWrite.cc:50
void AddModule(const G4VPhysicalVolume *const topVol)
Definition: G4GDMLWrite.cc:284
virtual G4bool IsParameterised() const =0
virtual void MaterialsWrite(xercesc::DOMElement *)=0
G4String GenerateName(const G4String &, const void *const)
Definition: G4GDMLWrite.cc:124
static void SetAddPointerToName(G4bool)
Definition: G4GDMLWrite.cc:357
#define G4endl
Definition: G4ios.hh:61
virtual void SetupWrite(xercesc::DOMElement *, const G4LogicalVolume *const)=0
xercesc::DOMElement * NewElement(const G4String &)
Definition: G4GDMLWrite.cc:161
virtual void StructureWrite(xercesc::DOMElement *)=0
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
virtual void DefineWrite(xercesc::DOMElement *)=0
virtual void ExtensionWrite(xercesc::DOMElement *)
Definition: G4GDMLWrite.cc:85
virtual G4Transform3D TraverseVolumeTree(const G4LogicalVolume *const, const G4int)=0
G4bool FileExists(const G4String &) const
Definition: G4GDMLWrite.cc:54
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
void AddAuxInfo(G4GDMLAuxListType *auxInfoList, xercesc::DOMElement *element)
Definition: G4GDMLWrite.cc:90
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual void SurfacesWrite()=0
Double_t R
G4bool contains(const std::string &) const
std::map< G4int, G4int > DepthMapType
Definition: G4GDMLWrite.hh:61
static G4bool addPointerToName
Definition: G4GDMLWrite.hh:132
static DLL_API const Transform3D Identity
Definition: Transform3D.h:197
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
xercesc::DOMDocument * doc
Definition: G4GDMLWrite.hh:133
std::map< const G4VPhysicalVolume *, G4String > PhysVolumeMapType
Definition: G4GDMLWrite.hh:60
int G4int
Definition: G4Types.hh:78
VolumeMapType & VolumeMap()
Definition: G4GDMLWrite.cc:60
xercesc::DOMElement * userinfoElement
Definition: G4GDMLWrite.hh:135
static MCTruthManager * instance
std::vector< G4GDMLAuxStructType > G4GDMLAuxListType
virtual void UserinfoWrite(xercesc::DOMElement *)
Definition: G4GDMLWrite.cc:111
void AddAuxiliary(G4GDMLAuxStructType myaux)
Definition: G4GDMLWrite.cc:352
virtual void AddExtension(xercesc::DOMElement *, const G4LogicalVolume *const)
Definition: G4GDMLWrite.cc:78
PhysVolumeMapType & PvolumeMap()
Definition: G4GDMLWrite.cc:66
G4GLOB_DLL std::ostream G4cout
XMLCh tempStr[10000]
Definition: G4GDMLWrite.hh:136
virtual G4bool IsReplicated() const =0
xercesc::DOMAttr * NewAttribute(const G4String &, const G4String &)
Definition: G4GDMLWrite.cc:137
G4String SchemaLocation
Definition: G4GDMLWrite.hh:131
std::map< const G4LogicalVolume *, G4Transform3D > VolumeMapType
Definition: G4GDMLWrite.hh:59
virtual void SolidsWrite(xercesc::DOMElement *)=0
G4Transform3D Write(const G4String &filename, const G4LogicalVolume *const topLog, const G4String &schemaPath, const G4int depth, G4bool storeReferences=true)
Definition: G4GDMLWrite.cc:167
const G4String & GetName() const
G4GDMLAuxListType auxList
Definition: G4GDMLWrite.hh:137