File indexing completed on 2025-01-18 09:14:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
0014 #define DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
0015
0016
0017 #include <DDG4/Geant4Action.h>
0018
0019
0020 namespace dd4hep {
0021
0022
0023 namespace sim {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 class Geant4GDMLWriteAction : public Geant4Action {
0043 public:
0044
0045 std::string m_output;
0046
0047 int m_overWrite;
0048
0049 int m_exportRegions;
0050
0051 int m_exportEnergyCuts;
0052
0053 int m_exportSensitiveDetectors;
0054
0055 public:
0056
0057 Geant4GDMLWriteAction(Geant4Context* context, const std::string& nam);
0058
0059 virtual ~Geant4GDMLWriteAction();
0060
0061 virtual void installCommandMessenger() override;
0062
0063 virtual void writeGDML();
0064 };
0065
0066 }
0067 }
0068
0069 #endif
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 #include <DD4hep/InstanceCount.h>
0086 #include <DD4hep/Printout.h>
0087 #include <DD4hep/Primitives.h>
0088 #include <DDG4/Geant4DataDump.h>
0089 #include <DDG4/Geant4UIMessenger.h>
0090
0091
0092 #ifndef GEANT4_NO_GDML
0093 #include <G4GDMLParser.hh>
0094 #endif
0095 #include <G4Version.hh>
0096
0097
0098 #include <sys/types.h>
0099 #include <sys/stat.h>
0100 #include <unistd.h>
0101 #include <memory>
0102
0103 using namespace dd4hep::sim;
0104
0105
0106 Geant4GDMLWriteAction::Geant4GDMLWriteAction(Geant4Context* ctxt, const std::string& nam)
0107 : Geant4Action(ctxt, nam)
0108 {
0109 m_needsControl = true;
0110 declareProperty("Output", m_output = "");
0111 declareProperty("OverWrite", m_overWrite = 1);
0112 declareProperty("exportRegions", m_exportRegions = 1);
0113 declareProperty("exportEnergyCuts", m_exportEnergyCuts = 1);
0114 declareProperty("exportSensitiveDetectors", m_exportSensitiveDetectors = 1);
0115 InstanceCount::increment(this);
0116 }
0117
0118
0119 Geant4GDMLWriteAction::~Geant4GDMLWriteAction() {
0120 InstanceCount::decrement(this);
0121 }
0122
0123
0124 void Geant4GDMLWriteAction::installCommandMessenger() {
0125 Callback cb = Callback(this).make(&Geant4GDMLWriteAction::writeGDML);
0126 m_control->addCall("write", "Write geometry to GDML file",cb);
0127 }
0128
0129
0130 void Geant4GDMLWriteAction::writeGDML() {
0131 std::string fname = m_output;
0132 struct stat buff;
0133 if ( fname.empty() ) {
0134 error("+++ No GDML file name given. Please set the output file (property Output)");
0135 return;
0136 }
0137 if ( (0 == ::stat(fname.c_str(), &buff)) && !m_overWrite ) {
0138 error("+++ GDML file %s elready exists. Please set another output file (property Output)",
0139 fname.c_str());
0140 return;
0141 }
0142 else if ( 0 == ::stat(fname.c_str(), &buff) ) {
0143 warning("+++ GDML file %s already exists. Overwriting existing file.", fname.c_str());
0144 ::unlink(fname.c_str());
0145 }
0146 #ifdef GEANT4_NO_GDML
0147 warning("+++ writeGDML: GDML not found in the present Geant4 build! Output: %s not written", fname.c_str());
0148 #else
0149 std::unique_ptr<G4GDMLParser> parser(new G4GDMLParser());
0150 parser->SetRegionExport(m_exportRegions != 0);
0151 parser->SetEnergyCutsExport(m_exportEnergyCuts != 0);
0152 #if G4VERSION_NUMBER>=1030
0153 parser->SetSDExport(m_exportSensitiveDetectors != 0);
0154 #endif
0155 info("+++ Writing GDML file: %s", fname.c_str());
0156 parser->Write(fname, context()->world());
0157 #endif
0158 }
0159
0160 #include <DDG4/Factories.h>
0161 DECLARE_GEANT4ACTION(Geant4GDMLWriteAction)