DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4GDMLWriteAction.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
14 #define DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
15 
16 // Framework include files
17 #include <DDG4/Geant4Action.h>
18 
20 namespace dd4hep {
21 
23  namespace sim {
24 
26 
43  public:
45  std::string m_output;
54 
55  public:
57  Geant4GDMLWriteAction(Geant4Context* context, const std::string& nam);
59  virtual ~Geant4GDMLWriteAction();
61  virtual void installCommandMessenger() override;
63  virtual void writeGDML();
64  };
65 
66  } // End namespace sim
67 } // End namespace dd4hep
68 
69 #endif /* DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H */
70 
71 //====================================================================
72 // AIDA Detector description implementation
73 //--------------------------------------------------------------------
74 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
75 // All rights reserved.
76 //
77 // For the licensing terms see $DD4hepINSTALL/LICENSE.
78 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
79 //
80 // Author : M.Frank
81 //
82 //====================================================================
83 
84 // Framework include files
85 #include <DD4hep/InstanceCount.h>
86 #include <DD4hep/Printout.h>
87 #include <DD4hep/Primitives.h>
88 #include <DDG4/Geant4DataDump.h>
89 #include <DDG4/Geant4UIMessenger.h>
90 
91 // Geant 4 includes
92 #ifndef GEANT4_NO_GDML
93 #include <G4GDMLParser.hh>
94 #endif
95 #include <G4Version.hh>
96 
97 // C/C++ include files
98 #include <sys/types.h>
99 #include <sys/stat.h>
100 #include <unistd.h>
101 #include <memory>
102 
103 using namespace dd4hep::sim;
104 
107  : Geant4Action(ctxt, nam)
108 {
109  m_needsControl = true;
110  declareProperty("Output", m_output = "");
111  declareProperty("OverWrite", m_overWrite = 1);
112  declareProperty("exportRegions", m_exportRegions = 1);
113  declareProperty("exportEnergyCuts", m_exportEnergyCuts = 1);
114  declareProperty("exportSensitiveDetectors", m_exportSensitiveDetectors = 1);
116 }
117 
121 }
122 
126  m_control->addCall("write", "Write geometry to GDML file",cb);
127 }
128 
131  std::string fname = m_output;
132  struct stat buff;
133  if ( fname.empty() ) {
134  error("+++ No GDML file name given. Please set the output file (property Output)");
135  return;
136  }
137  if ( (0 == ::stat(fname.c_str(), &buff)) && !m_overWrite ) {
138  error("+++ GDML file %s elready exists. Please set another output file (property Output)",
139  fname.c_str());
140  return;
141  }
142  else if ( 0 == ::stat(fname.c_str(), &buff) ) {
143  warning("+++ GDML file %s already exists. Overwriting existing file.", fname.c_str());
144  ::unlink(fname.c_str());
145  }
146 #ifdef GEANT4_NO_GDML
147  warning("+++ writeGDML: GDML not found in the present Geant4 build! Output: %s not written", fname.c_str());
148 #else
149  std::unique_ptr<G4GDMLParser> parser(new G4GDMLParser());
150  parser->SetRegionExport(m_exportRegions != 0);
151  parser->SetEnergyCutsExport(m_exportEnergyCuts != 0);
152 #if G4VERSION_NUMBER>=1030
153  parser->SetSDExport(m_exportSensitiveDetectors != 0);
154 #endif
155  info("+++ Writing GDML file: %s", fname.c_str());
156  parser->Write(fname, context()->world());
157 #endif
158 }
159 
160 #include <DDG4/Factories.h>
dd4hep::sim::Geant4GDMLWriteAction::m_exportRegions
int m_exportRegions
Property: Export region information to the GDML.
Definition: Geant4GDMLWriteAction.cpp:49
dd4hep::sim::Geant4Action::m_needsControl
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:123
dd4hep::sim::Geant4GDMLWriteAction::m_exportEnergyCuts
int m_exportEnergyCuts
Property: Export energy cut information to the GDML.
Definition: Geant4GDMLWriteAction.cpp:51
dd4hep::sim::Geant4GDMLWriteAction::m_output
std::string m_output
Property: collection names to be dumped.
Definition: Geant4GDMLWriteAction.cpp:45
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4GDMLWriteAction::m_overWrite
int m_overWrite
Poprerty: Flag to overwrite existing files.
Definition: Geant4GDMLWriteAction.cpp:47
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4Action::warning
void warning(const char *fmt,...) const
Support of warning messages.
Definition: Geant4Action.cpp:223
Geant4DataDump.h
parser
dd4hepDOMParser * parser
Definition: DocumentHandler.cpp:159
dd4hep::Callback::make
const Callback & make(R(T::*pmf)())
Callback setup function for Callbacks with member functions with explicit return type taking no argum...
Definition: Callback.h:126
dd4hep::sim::Geant4GDMLWriteAction::m_exportSensitiveDetectors
int m_exportSensitiveDetectors
Property: Export sensitive detector information to the GDML.
Definition: Geant4GDMLWriteAction.cpp:53
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
Geant4UIMessenger.h
dd4hep::sim::Geant4Action::error
void error(const char *fmt,...) const
Support of error messages.
Definition: Geant4Action.cpp:231
dd4hep::sim::Geant4GDMLWriteAction::installCommandMessenger
virtual void installCommandMessenger() override
Install command control messenger if wanted.
Definition: Geant4GDMLWriteAction.cpp:124
dd4hep::Callback
Definition of the generic callback structure for member functions.
Definition: Callback.h:38
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4GDMLWriteAction::writeGDML
virtual void writeGDML()
Write geometry to GDML.
Definition: Geant4GDMLWriteAction.cpp:130
dd4hep::sim::Geant4GDMLWriteAction::~Geant4GDMLWriteAction
virtual ~Geant4GDMLWriteAction()
Default destructor.
Definition: Geant4GDMLWriteAction.cpp:119
dd4hep::sim::Geant4GDMLWriteAction::Geant4GDMLWriteAction
Geant4GDMLWriteAction(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4GDMLWriteAction.cpp:106
dd4hep::sim::Geant4Action
Default base class for all Geant 4 actions and derivates thereof.
Definition: Geant4Action.h:113
Factories.h
Primitives.h
dd4hep::sim::Geant4Action::m_control
Geant4UIMessenger * m_control
Control directory of this action.
Definition: Geant4Action.h:118
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4UIMessenger::addCall
void addCall(const std::string &name, const std::string &description, const Callback &cb, size_t npar=0)
Add a new callback structure.
Definition: Geant4UIMessenger.cpp:60
InstanceCount.h
Printout.h
Geant4Action.h
dd4hep::sim::Geant4GDMLWriteAction
Class to measure the energy of escaping tracks.
Definition: Geant4GDMLWriteAction.cpp:42
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270