LDMX Software
g4_vis.cxx
1#include "Framework/Configure/Parameters.h"
3#include "G4GDMLParser.hh"
4#include "G4GeometryManager.hh"
5#include "G4PhysListFactory.hh"
6#include "G4RunManager.hh"
7#include "G4UIExecutive.hh"
8#include "G4UImanager.hh"
9#include "G4VisExecutive.hh"
10#include "SimCore/DetectorConstruction.h"
11#include "SimCore/Geo/Parser.h"
12
13static void printUsage() {
14 std::cout << "usage: g4-vis {detector.gdml} (macro.mac)" << std::endl;
15 std::cout << " {detector.gdml} is the geometry description "
16 "that you wish to visualize."
17 << std::endl
18 << " (macro.mac) is an optional argument to execute"
19 " a macro file immediately after initialization."
20 << std::endl
21 << " The macro file must be in the current directory"
22 " or in a subdirectory of the current directory."
23 << std::endl
24 << std::endl;
25}
26
27int main(int argc, char* argv[]) {
28 if (argc != 2 && argc != 3) {
29 printUsage();
30 std::cerr << "** Need to be given a single detector description and "
31 "(optionally) a single macro to execute. **"
32 << std::endl;
33 return 1;
34 }
35
36 std::string the_arg{argv[1]};
37 std::string the_macro;
38 if (argc == 3) the_macro = std::string(argv[2]);
39 if (the_arg == "-h" or the_arg == "--help") {
40 // ask for help, let's give it to them.
41 printUsage();
42 return 0;
43 }
44
45 framework::EventProcessor* null_processor{nullptr};
46 simcore::ConditionsInterface empty_interface(null_processor);
47 framework::config::Parameters parser_parameters;
48 parser_parameters.addParameter("validate_detector", true);
49 parser_parameters.addParameter<std::string>("detector", the_arg);
50
51 // RunManager
52 G4RunManager* run_manager = new G4RunManager;
53
54 // Detector components
55 auto parser{simcore::geo::Parser::Factory::get().make(
56 "gdml", parser_parameters, empty_interface)};
57 if (not parser) {
58 std::cerr << "Unable to create a 'gdml' parser to read the geometry."
59 << std::endl;
60 return 1;
61 }
62 auto parser_ptr{parser.value()};
63 run_manager->SetUserInitialization(new simcore::DetectorConstruction(
64 parser_ptr, parser_parameters, empty_interface));
65 G4GeometryManager::GetInstance()->OpenGeometry();
66 parser_ptr->read();
67 run_manager->DefineWorldVolume(parser_ptr->getWorldVolume());
68
69 // required to define a physics list to complete initialization
70 G4PhysListFactory lists;
71 run_manager->SetUserInitialization(lists.GetReferencePhysList("FTFP_BERT"));
72
73 run_manager->Initialize();
74
75 // Define (G)UI
76 G4UIExecutive* ui = new G4UIExecutive(argc, argv);
77 G4VisManager* vis_manager = new G4VisExecutive;
78 vis_manager->Initialize();
79 if (argc == 3) {
80 if (!std::fopen(argv[2], "r")) {
81 std::cerr << "Macro file doesn't exist, or the path is incorrect! "
82 "Try using an absolute path or checking your directory."
83 << std::endl
84 << std::endl;
85 return 1;
86 }
87 auto* uimanager = G4UImanager::GetUIpointer();
88 G4String command = "/control/execute " + the_macro;
89 uimanager->ApplyCommand(command);
90 }
91
92 ui->SessionStart();
93
94 delete ui;
95 delete run_manager;
96 delete vis_manager;
97
98 return 0;
99}
Base classes for all user event processing components to extend.
Base class for all event processing components.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
Handle to the conditions system, provided at construction to classes which require it.
Implements the Geant4 detector construction.