19#include <FTFP_BERT.hh>
20#include <G4RunManagerFactory.hh>
21#include <G4UIExecutive.hh>
22#include <G4UIcommand.hh>
23#include <G4UImanager.hh>
24#include <G4VisExecutive.hh>
35#include <mach-o/dyld.h>
43std::filesystem::path ExeDir() {
46 auto p = std::filesystem::read_symlink(
"/proc/self/exe", ec);
48 return p.parent_path();
49#elif defined(__APPLE__)
51 uint32_t size =
sizeof(buf);
52 if (_NSGetExecutablePath(buf, &size) == 0) {
54 auto p = std::filesystem::canonical(buf, ec);
56 return p.parent_path();
73G4String BuildMacroSearchPath() {
74 std::vector<std::string> dirs;
77 if (
const char* env = std::getenv(
"G4OCCT_MACRO_PATH"))
79 dirs.emplace_back(env);
82 auto exeDir = ExeDir();
83 if (!exeDir.empty()) {
84 auto runtimeDir = exeDir.parent_path() / G4OCCT_INSTALL_DATADIR /
"g4occt" /
"macros";
85 dirs.push_back(runtimeDir.string());
89 dirs.emplace_back(G4OCCT_MACRO_DIR_BUILD);
92 for (
const auto& d : dirs) {
96 if (!std::filesystem::exists(d, ec) || ec)
105void PrintUsage(
const char* prog) {
106 G4cerr <<
"Usage: " << prog
107 <<
" [-m macro] [-u UIsession] [-t nThreads]"
108 " [file.step|file.stp ...] [file.xml ...]"
110 G4cerr <<
" -m macro Execute Geant4 macro file (batch mode)." << G4endl;
111 G4cerr <<
" -u UIsession Select UI session (Qt, Xm, tcsh, …)." << G4endl;
112 G4cerr <<
" -t nThreads Number of worker threads (multi-threaded builds only)." << G4endl;
113 G4cerr <<
" file.step Load STEP file (solid or assembly, auto-detected)." << G4endl;
114 G4cerr <<
" file.xml Load G4OCCT material-map XML file." << G4endl;
118std::string FileExtension(
const std::string& path) {
119 auto dot = path.rfind(
'.');
120 if (dot == std::string::npos)
122 std::string ext = path.substr(dot);
123 std::transform(ext.begin(), ext.end(), ext.begin(),
124 [](
unsigned char c) { return std::tolower(c); });
130int main(
int argc,
char** argv) {
133#ifdef G4MULTITHREADED
136 std::vector<std::string> stepFiles;
137 std::vector<std::string> materialFiles;
140 for (G4int i = 1; i < argc; ++i) {
141 const std::string arg(argv[i]);
149 }
else if (arg ==
"-u") {
155 }
else if (arg ==
"-t") {
162 const std::string nextArg(argv[i + 1]);
163 if (nextArg[0] ==
'-') {
168#ifdef G4MULTITHREADED
169 nThreads = G4UIcommand::ConvertToInt(argv[i]);
171 G4cerr <<
"Warning: -t option (" << argv[i]
172 <<
") ignored (Geant4 built without multi-threading)." << G4endl;
174 }
else if (arg[0] ==
'-') {
175 G4cerr <<
"Unknown option: " << arg << G4endl;
180 const auto ext = FileExtension(arg);
181 if (ext ==
".step" || ext ==
".stp") {
182 stepFiles.push_back(arg);
183 }
else if (ext ==
".xml") {
184 materialFiles.push_back(arg);
186 G4cerr <<
"Unrecognised file type (expected .step, .stp, or .xml): " << arg << G4endl;
194 G4UIExecutive* ui =
nullptr;
196 ui =
new G4UIExecutive(argc, argv, session);
200 auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
201#ifdef G4MULTITHREADED
203 runManager->SetNumberOfThreads(nThreads);
211 for (
const auto& f : materialFiles) {
215 for (
const auto& f : stepFiles) {
216 detConstruction->AddSTEP(f);
219 runManager->SetUserInitialization(detConstruction);
220 runManager->SetUserInitialization(
new FTFP_BERT(0));
226 G4VisExecutive* visManager =
nullptr;
228 visManager =
new G4VisExecutive(
"Quiet");
229 visManager->Initialize();
237 auto* UImanager = G4UImanager::GetUIpointer();
238 UImanager->SetMacroSearchPath(BuildMacroSearchPath());
243 if (!macro.empty()) {
244 exitCode = UImanager->ApplyCommand(
"/control/execute " + macro);
246 UImanager->ApplyCommand(
"/control/execute init_vis.mac");
248 UImanager->ApplyCommand(
"/control/execute gui.mac");
256 return exitCode != 0 ? 1 : 0;
Minimal user action initialisation for the g4occt executable.
Dynamic detector construction for the g4occt interactive tool.
Action initialisation for the g4occt interactive tool.
Dynamically constructs a Geant4 world from STEP solids and assemblies.
void LoadMaterial(G4String xmlFile)
Queue a material-map XML file to be loaded at construction time.
int main(int argc, char **argv)