37 #include <Geant4/G4LogicalVolume.hh>
38 #include <Geant4/G4PVDivision.hh>
39 #include <Geant4/G4Transform3D.hh>
40 #include <Geant4/G4Version.hh>
50 , userinfoElement(nullptr)
61 return (stat(fname.c_str(), &FileInfo) == 0);
83 const G4LogicalVolume*
const)
97 for (std::vector<PHG4GDMLAuxStructType>::const_iterator
98 iaux = auxInfoList->begin();
99 iaux != auxInfoList->end(); ++iaux)
101 xercesc::DOMElement* auxiliaryElement =
NewElement(
"auxiliary");
102 element->appendChild(auxiliaryElement);
104 auxiliaryElement->setAttributeNode(
NewAttribute(
"auxtype", (*iaux).type));
105 auxiliaryElement->setAttributeNode(
NewAttribute(
"auxvalue", (*iaux).value));
106 if (((*iaux).unit) !=
"")
108 auxiliaryElement->setAttributeNode(
NewAttribute(
"auxunit", (*iaux).unit));
123 std::cout <<
"PHG4GDML: Writing userinfo..." << std::endl;
131 #if G4VERSION_NUMBER >= 1100
135 std::stringstream stream;
142 nameOut = G4String(stream.str());
143 std::vector<char> toremove = {
' ',
'/',
':',
'#',
'+'};
144 for (
auto c : toremove)
146 if (G4StrUtil::contains(nameOut,
c))
148 std::replace(nameOut.begin(), nameOut.end(),
c,
'_');
157 std::stringstream stream;
164 nameOut = G4String(stream.str());
165 if (nameOut.contains(
' '))
166 nameOut.erase(std::remove(nameOut.begin(), nameOut.end(),
' '), nameOut.end());
173 const G4String&
value)
175 xercesc::XMLString::transcode(name,
tempStr, 9999);
176 xercesc::DOMAttr* att =
doc->createAttribute(
tempStr);
177 xercesc::XMLString::transcode(value,
tempStr, 9999);
183 const G4double&
value)
185 xercesc::XMLString::transcode(name,
tempStr, 9999);
186 xercesc::DOMAttr* att =
doc->createAttribute(
tempStr);
187 std::ostringstream ostream;
188 ostream.precision(15);
190 G4String
str = ostream.str();
191 xercesc::XMLString::transcode(str,
tempStr, 9999);
198 xercesc::XMLString::transcode(name,
tempStr, 9999);
203 const G4LogicalVolume*
const logvol,
204 const G4String& setSchemaLocation,
213 std::cout <<
"PHG4GDML: Writing '" << fname <<
"'..." << std::endl;
217 std::cout <<
"PHG4GDML: Writing module '" << fname <<
"'..." << std::endl;
222 G4String
ErrorMessage =
"File '" + fname +
"' already exists!";
223 G4Exception(
"PHG4GDMLWrite::Write()",
"InvalidSetup",
224 FatalException, ErrorMessage);
230 xercesc::XMLString::transcode(
"LS",
tempStr, 9999);
231 xercesc::DOMImplementationRegistry::getDOMImplementation(
tempStr);
232 xercesc::XMLString::transcode(
"Range",
tempStr, 9999);
233 xercesc::DOMImplementation* impl =
234 xercesc::DOMImplementationRegistry::getDOMImplementation(
tempStr);
235 xercesc::XMLString::transcode(
"gdml",
tempStr, 9999);
237 xercesc::DOMElement* gdml =
doc->getDocumentElement();
239 #if XERCES_VERSION_MAJOR >= 3
241 xercesc::DOMLSSerializer*
writer =
242 ((xercesc::DOMImplementationLS*) impl)->createLSSerializer();
244 xercesc::DOMConfiguration* dc = writer->getDomConfig();
245 dc->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint,
true);
249 xercesc::DOMWriter* writer =
250 ((xercesc::DOMImplementationLS*) impl)->createDOMWriter();
252 if (writer->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint,
true))
253 writer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint,
true);
258 "http://www.w3.org/2001/XMLSchema-instance"));
259 gdml->setAttributeNode(
NewAttribute(
"xsi:noNamespaceSchemaLocation",
273 xercesc::XMLFormatTarget* myFormTarget =
274 new xercesc::LocalFileFormatTarget(fname.c_str());
278 #if XERCES_VERSION_MAJOR >= 3
280 xercesc::DOMLSOutput* theOutput =
281 ((xercesc::DOMImplementationLS*) impl)->createLSOutput();
282 theOutput->setByteStream(myFormTarget);
283 writer->write(
doc, theOutput);
285 writer->writeNode(myFormTarget, *
doc);
288 catch (
const xercesc::XMLException& toCatch)
290 char*
message = xercesc::XMLString::transcode(toCatch.getMessage());
291 std::cout <<
"PHG4GDML: Exception message is: " << message << std::endl;
292 xercesc::XMLString::release(&message);
293 return G4Transform3D::Identity;
295 catch (
const xercesc::DOMException& toCatch)
297 char*
message = xercesc::XMLString::transcode(toCatch.msg);
298 std::cout <<
"PHG4GDML: Exception message is: " << message << std::endl;
299 xercesc::XMLString::release(&message);
300 return G4Transform3D::Identity;
304 std::cout <<
"PHG4GDML: Unexpected Exception!" << std::endl;
305 return G4Transform3D::Identity;
313 std::cout <<
"PHG4GDML: Writing '" << fname <<
"' done !" << std::endl;
317 std::cout <<
"PHG4GDML: Writing module '" << fname <<
"' done !" << std::endl;
327 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
328 "Invalid NULL pointer is specified for modularization!");
332 std::cout <<
"PHG4GDML: Adding module '" << fname <<
"'..." << std::endl;
334 if (dynamic_cast<const G4PVDivision*>(physvol))
336 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
337 "It is not possible to modularize by divisionvol!");
340 if (physvol->IsParameterised())
342 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
343 "It is not possible to modularize by parameterised volume!");
346 if (physvol->IsReplicated())
348 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
349 "It is not possible to modularize by replicated volume!");
360 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
361 "Depth must be a positive number!");
365 G4Exception(
"PHG4GDMLWrite::AddModule()",
"InvalidSetup", FatalException,
366 "Adding module(s) at this depth is already requested!");
381 std::stringstream stream;
382 stream <<
"depth" << depth <<
"_module" <<
DepthMap()[
depth] <<
".gdml";
384 return G4String(stream.str());