forked from sPHENIX-Collaboration/coresoftware
/
PHG4GDMLWrite.cc
357 lines (306 loc) · 11.6 KB
/
PHG4GDMLWrite.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: PHG4GDMLWrite.cc 69013 2013-04-15 09:41:13Z gcosmo $
//
// class PHG4GDMLWrite Implementation
//
// Original author: Zoltan Torzsok, November 2007
//
// --------------------------------------------------------------------
#include "PHG4GDMLWrite.hh"
#include <Geant4/G4LogicalVolume.hh>
#include <Geant4/G4PVDivision.hh>
#include <Geant4/G4Transform3D.hh>
#include <sys/stat.h>
#include <iostream>
G4bool PHG4GDMLWrite::addPointerToName = true;
PHG4GDMLWrite::PHG4GDMLWrite() : doc(nullptr), extElement(nullptr), userinfoElement(nullptr)
{
}
PHG4GDMLWrite::~PHG4GDMLWrite()
{
}
G4bool PHG4GDMLWrite::FileExists(const G4String& fname) const
{
struct stat FileInfo;
return (stat(fname.c_str(),&FileInfo) == 0);
}
PHG4GDMLWrite::VolumeMapType& PHG4GDMLWrite::VolumeMap()
{
static VolumeMapType instance;
return instance;
}
PHG4GDMLWrite::PhysVolumeMapType& PHG4GDMLWrite::PvolumeMap()
{
static PhysVolumeMapType instance;
return instance;
}
PHG4GDMLWrite::DepthMapType& PHG4GDMLWrite::DepthMap()
{
static DepthMapType instance;
return instance;
}
void PHG4GDMLWrite::AddExtension(xercesc::DOMElement*,
const G4LogicalVolume* const)
{
// Empty implementation. To be overwritten by user for specific extensions
// related to attributes associated to volumes
}
void PHG4GDMLWrite::ExtensionWrite(xercesc::DOMElement*)
{
// Empty implementation. To be overwritten by user for specific extensions
}
void PHG4GDMLWrite::AddAuxInfo(PHG4GDMLAuxListType* auxInfoList,
xercesc::DOMElement* element)
{
for(std::vector<PHG4GDMLAuxStructType>::const_iterator
iaux = auxInfoList->begin(); iaux != auxInfoList->end(); ++iaux )
{
xercesc::DOMElement* auxiliaryElement = NewElement("auxiliary");
element->appendChild(auxiliaryElement);
auxiliaryElement->setAttributeNode(NewAttribute("auxtype", (*iaux).type));
auxiliaryElement->setAttributeNode(NewAttribute("auxvalue", (*iaux).value));
if (((*iaux).unit)!="")
{
auxiliaryElement->setAttributeNode(NewAttribute("auxunit", (*iaux).unit));
}
if (iaux->auxList) { AddAuxInfo(iaux->auxList, auxiliaryElement); }
}
return;
}
void PHG4GDMLWrite::UserinfoWrite(xercesc::DOMElement* gdmlElement)
{
if(auxList.size()>0)
{
std::cout << "PHG4GDML: Writing userinfo..." << std::endl;
userinfoElement = NewElement("userinfo");
gdmlElement->appendChild(userinfoElement);
AddAuxInfo(&auxList, userinfoElement);
}
}
G4String PHG4GDMLWrite::GenerateName(const G4String& name, const void* const ptr)
{
G4String nameOut;
std::stringstream stream; stream << name;
if (addPointerToName) { stream << ptr; };
nameOut=G4String(stream.str());
if(nameOut.contains(' '))
nameOut.erase(std::remove(nameOut.begin(),nameOut.end(),' '),nameOut.end());
return nameOut;
}
xercesc::DOMAttr* PHG4GDMLWrite::NewAttribute(const G4String& name,
const G4String& value)
{
xercesc::XMLString::transcode(name,tempStr,9999);
xercesc::DOMAttr* att = doc->createAttribute(tempStr);
xercesc::XMLString::transcode(value,tempStr,9999);
att->setValue(tempStr);
return att;
}
xercesc::DOMAttr* PHG4GDMLWrite::NewAttribute(const G4String& name,
const G4double& value)
{
xercesc::XMLString::transcode(name,tempStr,9999);
xercesc::DOMAttr* att = doc->createAttribute(tempStr);
std::ostringstream ostream;
ostream.precision(15);
ostream << value;
G4String str = ostream.str();
xercesc::XMLString::transcode(str,tempStr,9999);
att->setValue(tempStr);
return att;
}
xercesc::DOMElement* PHG4GDMLWrite::NewElement(const G4String& name)
{
xercesc::XMLString::transcode(name,tempStr,9999);
return doc->createElement(tempStr);
}
G4Transform3D PHG4GDMLWrite::Write(const G4String& fname,
const G4LogicalVolume* const logvol,
const G4String& setSchemaLocation,
const G4int depth,
G4bool refs)
{
SchemaLocation = setSchemaLocation;
addPointerToName = refs;
if (depth==0) { std::cout << "PHG4GDML: Writing '" << fname << "'..." << std::endl; }
else { std::cout << "PHG4GDML: Writing module '" << fname << "'..." << std::endl; }
if (FileExists(fname))
{
G4String ErrorMessage = "File '"+fname+"' already exists!";
G4Exception("PHG4GDMLWrite::Write()", "InvalidSetup",
FatalException, ErrorMessage);
}
VolumeMap().clear(); // The module map is global for all modules,
// so clear it only at once!
xercesc::XMLString::transcode("LS", tempStr, 9999);
xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
xercesc::XMLString::transcode("Range", tempStr, 9999);
xercesc::DOMImplementation* impl =
xercesc::DOMImplementationRegistry::getDOMImplementation(tempStr);
xercesc::XMLString::transcode("gdml", tempStr, 9999);
doc = impl->createDocument(0,tempStr,0);
xercesc::DOMElement* gdml = doc->getDocumentElement();
#if XERCES_VERSION_MAJOR >= 3
// DOM L3 as per Xerces 3.0 API
xercesc::DOMLSSerializer* writer =
((xercesc::DOMImplementationLS*)impl)->createLSSerializer();
xercesc::DOMConfiguration *dc = writer->getDomConfig();
dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
#else
xercesc::DOMWriter* writer =
((xercesc::DOMImplementationLS*)impl)->createDOMWriter();
if (writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
#endif
gdml->setAttributeNode(NewAttribute("xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance"));
gdml->setAttributeNode(NewAttribute("xsi:noNamespaceSchemaLocation",
SchemaLocation));
ExtensionWrite(gdml);
DefineWrite(gdml);
MaterialsWrite(gdml);
SolidsWrite(gdml);
StructureWrite(gdml);
UserinfoWrite(gdml);
SetupWrite(gdml,logvol);
G4Transform3D R = TraverseVolumeTree(logvol,depth);
SurfacesWrite();
xercesc::XMLFormatTarget *myFormTarget =
new xercesc::LocalFileFormatTarget(fname.c_str());
try
{
#if XERCES_VERSION_MAJOR >= 3
// DOM L3 as per Xerces 3.0 API
xercesc::DOMLSOutput *theOutput =
((xercesc::DOMImplementationLS*)impl)->createLSOutput();
theOutput->setByteStream(myFormTarget);
writer->write(doc, theOutput);
#else
writer->writeNode(myFormTarget, *doc);
#endif
}
catch (const xercesc::XMLException& toCatch)
{
char* message = xercesc::XMLString::transcode(toCatch.getMessage());
std::cout << "PHG4GDML: Exception message is: " << message << std::endl;
xercesc::XMLString::release(&message);
return G4Transform3D::Identity;
}
catch (const xercesc::DOMException& toCatch)
{
char* message = xercesc::XMLString::transcode(toCatch.msg);
std::cout << "PHG4GDML: Exception message is: " << message << std::endl;
xercesc::XMLString::release(&message);
return G4Transform3D::Identity;
}
catch (...)
{
std::cout << "PHG4GDML: Unexpected Exception!" << std::endl;
return G4Transform3D::Identity;
}
delete myFormTarget;
writer->release();
if (depth==0)
{
std::cout << "PHG4GDML: Writing '" << fname << "' done !" << std::endl;
}
else
{
std::cout << "PHG4GDML: Writing module '" << fname << "' done !" << std::endl;
}
return R;
}
void PHG4GDMLWrite::AddModule(const G4VPhysicalVolume* const physvol)
{
if (physvol == 0)
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"Invalid NULL pointer is specified for modularization!");
return;
}
G4String fname = GenerateName(physvol->GetName(),physvol);
std::cout << "PHG4GDML: Adding module '" << fname << "'..." << std::endl;
if (dynamic_cast<const G4PVDivision*>(physvol))
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"It is not possible to modularize by divisionvol!");
return;
}
if (physvol->IsParameterised())
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"It is not possible to modularize by parameterised volume!");
return;
}
if (physvol->IsReplicated())
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"It is not possible to modularize by replicated volume!");
return;
}
PvolumeMap()[physvol] = fname;
}
void PHG4GDMLWrite::AddModule(const G4int depth)
{
if (depth<0)
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"Depth must be a positive number!");
}
if (DepthMap().find(depth) != DepthMap().end())
{
G4Exception("PHG4GDMLWrite::AddModule()", "InvalidSetup", FatalException,
"Adding module(s) at this depth is already requested!");
}
DepthMap()[depth] = 0;
}
G4String PHG4GDMLWrite::Modularize( const G4VPhysicalVolume* const physvol,
const G4int depth )
{
if (PvolumeMap().find(physvol) != PvolumeMap().end())
{
return PvolumeMap()[physvol]; // Modularize via physvol
}
if (DepthMap().find(depth) != DepthMap().end()) // Modularize via depth
{
std::stringstream stream;
stream << "depth" << depth << "_module" << DepthMap()[depth] << ".gdml";
DepthMap()[depth]++; // There can be more modules at this depth!
return G4String(stream.str());
}
return G4String(""); // Empty string for module name = no modularization
// was requested at that level/physvol!
}
void PHG4GDMLWrite::AddAuxiliary(PHG4GDMLAuxStructType myaux)
{
auxList.push_back(myaux);
}
void PHG4GDMLWrite::SetAddPointerToName(G4bool set)
{
addPointerToName = set;
}