Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:20

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
0014 #define DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
0015 
0016 // Framework include files
0017 #include <DDG4/Geant4Action.h>
0018 
0019 /// Namespace for the AIDA detector description toolkit
0020 namespace dd4hep {
0021 
0022   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0023   namespace sim {
0024 
0025     /// Class to measure the energy of escaping tracks
0026     /** Class to dump Geant4 geometry to GDML
0027      *
0028      *  Please note: 
0029      *  The Geant4 physics list must be initialized BEFORE
0030      *  invoking the writer with options. Otherwise the particle definitions
0031      *  are missing!
0032      *  If you ONLY want to dump the geometry to GDML you must call
0033      *  /run/beamOn 0
0034      *  before writing the GDML file!
0035      *  You also need to setup a minimal generation action like:
0036      *  sid.geant4.setupGun('Gun','pi-',10*GeV,Standalone=True)
0037      *
0038      *  \author  M.Frank
0039      *  \version 1.0
0040      *  \ingroup DD4HEP_SIMULATION
0041      */
0042     class Geant4GDMLWriteAction : public Geant4Action {
0043     public:
0044       /// Property: collection names to be dumped 
0045       std::string m_output;
0046       /// Poprerty: Flag to overwrite existing files
0047       int         m_overWrite;
0048       /// Property: Export region information to the GDML
0049       int         m_exportRegions;
0050       /// Property: Export energy cut information to the GDML
0051       int         m_exportEnergyCuts;
0052       /// Property: Export sensitive detector information to the GDML
0053       int         m_exportSensitiveDetectors;
0054 
0055     public:
0056       /// Standard constructor
0057       Geant4GDMLWriteAction(Geant4Context* context, const std::string& nam);
0058       /// Default destructor
0059       virtual ~Geant4GDMLWriteAction();
0060       /// Install command control messenger if wanted
0061       virtual void installCommandMessenger()  override;
0062       /// Write geometry to GDML
0063       virtual void writeGDML();
0064     };
0065 
0066   }    // End namespace sim
0067 }      // End namespace dd4hep
0068 
0069 #endif /* DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H */
0070 
0071 //====================================================================
0072 //  AIDA Detector description implementation 
0073 //--------------------------------------------------------------------
0074 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0075 // All rights reserved.
0076 //
0077 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0078 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0079 //
0080 // Author     : M.Frank
0081 //
0082 //====================================================================
0083 
0084 // Framework include files
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 // Geant 4 includes
0092 #ifndef GEANT4_NO_GDML
0093 #include <G4GDMLParser.hh>
0094 #endif
0095 #include <G4Version.hh>
0096 
0097 // C/C++ include files
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 /// Standard constructor
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 /// Default destructor
0119 Geant4GDMLWriteAction::~Geant4GDMLWriteAction() {
0120   InstanceCount::decrement(this);
0121 }
0122 
0123 /// Install command control messenger if wanted
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 /// Write geometry to GDML
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)