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