diff --git a/DDG4/include/DDG4/Geant4UIManager.h b/DDG4/include/DDG4/Geant4UIManager.h index 56b75757376f9bf3acea8e6c9d8ad8b2c762ca12..82d4f6820436c25ec38f12b75ffa16c3536d4dfe 100644 --- a/DDG4/include/DDG4/Geant4UIManager.h +++ b/DDG4/include/DDG4/Geant4UIManager.h @@ -60,6 +60,8 @@ namespace dd4hep { std::string m_visSetup; /// Property: Array of macro files to be chained std::vector<std::string> m_commands; + /// Property: Array of macro files to be chained and executed AFTER running + std::vector<std::string> m_postRunCommands; /// Property: Array of commands to be chained std::vector<std::string> m_macros; /// Property: New prompt if the user wants to change it. (Default is do nothing) diff --git a/DDG4/plugins/Geant4GDMLWriteAction.cpp b/DDG4/plugins/Geant4GDMLWriteAction.cpp index ffb5ca17491c4c91b927ca21374ce0243576da17..7f33ded5fac67f9ea54677300f46fb59af5f5feb 100644 --- a/DDG4/plugins/Geant4GDMLWriteAction.cpp +++ b/DDG4/plugins/Geant4GDMLWriteAction.cpp @@ -24,6 +24,16 @@ namespace dd4hep { /// Class to measure the energy of escaping tracks /** Class to dump Geant4 geometry to GDML + * + * Please note: + * The Geant4 physics list must be initialized BEFORE + * invoking the writer with options. Otherwise the particle definitions + * are missing! + * If you ONLY want to dump the geometry to GDML you must call + * /run/beamOn 0 + * before writing the GDML file! + * You also need to setup a minimal generation action like: + * sid.geant4.setupGun('Gun','pi-',10*GeV,Standalone=True) * * \author M.Frank * \version 1.0 @@ -35,6 +45,13 @@ namespace dd4hep { std::string m_output; /// Poprerty: Flag to overwrite existing files int m_overWrite; + /// Property: Export region information to the GDML + int m_exportRegions; + /// Property: Export energy cut information to the GDML + int m_exportEnergyCuts; + /// Property: Export sensitive detector information to the GDML + int m_exportSensitiveDetectors; + public: /// Standard constructor Geant4GDMLWriteAction(Geant4Context* context, const std::string& nam); @@ -89,8 +106,11 @@ Geant4GDMLWriteAction::Geant4GDMLWriteAction(Geant4Context* ctxt, const string& : Geant4Action(ctxt, nam) { m_needsControl = true; - declareProperty("Output", m_output = ""); - declareProperty("OverWrite", m_overWrite = 1); + declareProperty("Output", m_output = ""); + declareProperty("OverWrite", m_overWrite = 1); + declareProperty("exportRegions", m_exportRegions = 1); + declareProperty("exportEnergyCuts", m_exportEnergyCuts = 1); + declareProperty("exportSensitiveDetectors", m_exportSensitiveDetectors = 1); InstanceCount::increment(this); } @@ -121,6 +141,9 @@ void Geant4GDMLWriteAction::writeGDML() { ::unlink(m_output.c_str()); } unique_ptr<G4GDMLParser> parser(new G4GDMLParser()); + parser->SetRegionExport(m_exportRegions != 0); + parser->SetEnergyCutsExport(m_exportEnergyCuts != 0); + parser->SetSDExport(m_exportSensitiveDetectors != 0); info("+++ Writing GDML file: %s", m_output.c_str()); parser->Write(m_output, context()->world()); } diff --git a/DDG4/src/Geant4UIManager.cpp b/DDG4/src/Geant4UIManager.cpp index b27b4732626e950de13b58644d3d9bf8179dea20..3d6757a4d9b74e4c5b6f7df1f577a21a11bcfe28 100644 --- a/DDG4/src/Geant4UIManager.cpp +++ b/DDG4/src/Geant4UIManager.cpp @@ -38,14 +38,15 @@ namespace { Geant4UIManager::Geant4UIManager(Geant4Context* ctxt, const std::string& nam) : Geant4Action(ctxt,nam), m_vis(0), m_ui(0) { - declareProperty("SetupUI", m_uiSetup=""); - declareProperty("SetupVIS", m_visSetup=""); - declareProperty("SessionType", m_sessionType="tcsh"); - declareProperty("Macros", m_macros); - declareProperty("Commands", m_commands); - declareProperty("HaveVIS", m_haveVis=false); - declareProperty("HaveUI", m_haveUI=true); - declareProperty("Prompt", m_prompt); + declareProperty("SetupUI", m_uiSetup=""); + declareProperty("SetupVIS", m_visSetup=""); + declareProperty("SessionType", m_sessionType="tcsh"); + declareProperty("Macros", m_macros); + declareProperty("Commands", m_commands); + declareProperty("PostRunCommands",m_postRunCommands); + declareProperty("HaveVIS", m_haveVis=false); + declareProperty("HaveUI", m_haveUI=true); + declareProperty("Prompt", m_prompt); } /// Default destructor @@ -87,7 +88,6 @@ void Geant4UIManager::operator()(void* ) { /// Start manager & session void Geant4UIManager::start() { - typedef std::vector<std::string> _V; // Get the pointer to the User Interface manager G4UImanager* mgr = G4UImanager::GetUIpointer(); bool executed_statements = false; @@ -114,20 +114,26 @@ void Geant4UIManager::start() { executed_statements = true; } // Execute the chained macro files - for(_V::const_iterator i=m_macros.begin(); i!=m_macros.end(); ++i) { - printout(INFO,"Geant4UIManager","++ Executing Macro file:%s",(*i).c_str()); - mgr->ApplyCommand(make_cmd(*i).c_str()); + for(const auto& m : m_macros) { + printout(INFO,"Geant4UIManager","++ Executing Macro file:%s",m.c_str()); + mgr->ApplyCommand(make_cmd(m.c_str())); executed_statements = true; } // Execute the chained command statements - for(_V::const_iterator i=m_commands.begin(); i!=m_commands.end(); ++i) { - printout(INFO,"Geant4UIManager","++ Executing Command statement:%s",(*i).c_str()); - mgr->ApplyCommand((*i).c_str()); + for(const auto& c : m_commands) { + printout(INFO,"Geant4UIManager","++ Executing Command statement:%s",c.c_str()); + mgr->ApplyCommand(c.c_str()); executed_statements = true; } // Start UI session if present if ( m_haveUI && m_ui ) { m_ui->SessionStart(); + // Execute the chained command statements + for(const auto& c : m_postRunCommands) { + printout(INFO,"Geant4UIManager","++ Executing Command statement:%s",c.c_str()); + mgr->ApplyCommand(c.c_str()); + executed_statements = true; + } return; } else if ( m_haveUI ) { @@ -146,8 +152,14 @@ void Geant4UIManager::start() { context()->kernel().runManager().BeamOn(numEvent); } catch (DD4hep_End_Of_File& e) { printout(INFO,"Geant4UIManager","++ End of file reached, ending run..."); - context()->kernel().runManager().RunTermination(); } + // Execute the chained command statements + for(const auto& c : m_postRunCommands) { + printout(INFO,"Geant4UIManager","++ Executing Command statement:%s",c.c_str()); + mgr->ApplyCommand(c.c_str()); + executed_statements = true; + } + context()->kernel().runManager().RunTermination(); } /// Stop and release resources diff --git a/examples/CLICSiD/CMakeLists.txt b/examples/CLICSiD/CMakeLists.txt index 9967ff104fb2ab5bf609c82d635dde47ea546536..cc2f74aeb3c073ba2efc57ff47db1a3cbbe09d43 100644 --- a/examples/CLICSiD/CMakeLists.txt +++ b/examples/CLICSiD/CMakeLists.txt @@ -119,12 +119,12 @@ if (DD4HEP_USE_GEANT4) endforeach(script) # # Write GDML from Geant4 using UI - #dd4hep_add_test_reg( CLICSiD_DDG4_GDML_LONGTEST - # COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_CLICSiD.sh" - # EXEC_ARGS python ${CLICSiDEx_INSTALL}/scripts/CLIC_GDML.py - # REQUIRES DDG4 Geant4 - # REGEX_PASS "G4GDML. Writing 'CLICSiD.gdml' done !" - # REGEX_FAIL "Exception;EXCEPTION;ERROR" ) + dd4hep_add_test_reg( CLICSiD_DDG4_GDML_LONGTEST + COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_CLICSiD.sh" + EXEC_ARGS python ${CLICSiDEx_INSTALL}/scripts/CLIC_GDML.py + REQUIRES DDG4 Geant4 + REGEX_PASS "G4GDML. Writing 'CLICSiD.gdml' done !" + REGEX_FAIL "Exception;EXCEPTION;ERROR" ) # # Material scan dd4hep_add_test_reg( CLICSiD_DDG4_g4material_scan_LONGTEST diff --git a/examples/CLICSiD/scripts/CLIC_G4Gun.py b/examples/CLICSiD/scripts/CLIC_G4Gun.py index 2ac08ab5a591068e830d0b2bd7d654f8733b8362..e0f49f1be15429e9189da05bd2b94f908eed7089 100644 --- a/examples/CLICSiD/scripts/CLIC_G4Gun.py +++ b/examples/CLICSiD/scripts/CLIC_G4Gun.py @@ -51,6 +51,11 @@ def run(): user.TrackingVolume_Rmax = DDG4.EcalBarrel_rmin user.enableUI() part.adopt(user) + # + # Setup the GDML writer action + writer = DDG4.Action(kernel,'Geant4GDMLWriteAction/Writer') + writer.enableUI() + kernel.registerGlobalAction(writer) sid.setupDetectors() sid.setupPhysics('QGSP_BERT') diff --git a/examples/CLICSiD/scripts/CLIC_GDML.py b/examples/CLICSiD/scripts/CLIC_GDML.py index 69a007e8312fc7c0630196b99414d09670f2b99d..d6b8209acb6a95070002b2d6d993342760cdf121 100644 --- a/examples/CLICSiD/scripts/CLIC_GDML.py +++ b/examples/CLICSiD/scripts/CLIC_GDML.py @@ -7,6 +7,7 @@ """ def run(): + from g4units import * import logging, CLICSid, DDG4 from DDG4 import OutputLevel as Output @@ -22,15 +23,29 @@ def run(): writer = DDG4.Action(kernel,'Geant4GDMLWriteAction/Writer') writer.enableUI() kernel.registerGlobalAction(writer) + sid.setupPhysics('QGSP_BERT') # + gen = sid.geant4.setupGun('Gun','pi-',10*GeV,Standalone=True) # Now initialize. At the Geant4 command prompt we can write the geometry: # Idle> /ddg4/Writer/write - # or by configuring the UI: + # or by configuring the UI using ui.Commands + # + # Please note: The Geant4 physics list must be initialized BEFORE + # invoking the writer with options. Otherwise the particle definitions + # are missing! + # If you ONLY want to dump the geometry to GDML you must call + # /run/beamOn 0 + # before writing the GDML file! + # You also need to setup a minimal generation action like: + # sid.geant4.setupGun('Gun','pi-',10*GeV,Standalone=True) + # ui.Commands = [ + '/run/beamOn 0', '/ddg4/Writer/Output CLICSiD.gdml', '/ddg4/Writer/OverWrite 1', '/ddg4/Writer/write' ] + kernel.NumEvents = 0 kernel.configure() kernel.initialize() kernel.run()