BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSVisManager.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSColours.hh"
19#include "BDSVisManager.hh"
20
21#include "G4UIterminal.hh"
22#ifdef G4UI_USE_TCSH
23#include "G4UItcsh.hh"
24#endif
25
26#ifdef G4VIS_USE
27#include "G4VisExecutive.hh"
28#endif
29
30#ifdef G4UI_USE
31#ifdef G4VIS_USE
32#include "G4UImanager.hh" // G4 session managers
33#endif
34#include "G4UIExecutive.hh"
35#endif
36
37#include "G4String.hh"
38#include "G4Types.hh"
39
40#include "G4TrajectoryDrawByCharge.hh"
41#include "G4Version.hh"
42
43#include "BDSDebug.hh"
44#include "BDSDetectorConstruction.hh"
45#include "BDSMessenger.hh"
46#include "BDSUtilities.hh"
47#include "BDSVisCommandSceneAddQueryMagneticField.hh"
48
49#include <string>
50
51
52BDSVisManager::BDSVisManager(const G4String& visMacroFileNameIn,
53 const G4String& geant4MacroFileNameIn,
54 const BDSDetectorConstruction* realWorldIn,
55 G4int visVerbosity):
56 uiSession(nullptr),
57 visManager(nullptr),
58 bdsMessenger(nullptr),
59 visMacroFileName(visMacroFileNameIn),
60 geant4MacroFileName(geant4MacroFileNameIn)
61{
62#ifdef G4UI_USE_TCSH
63 uiSession = new G4UIterminal(new G4UItcsh);
64#else
65 uiSession = new G4UIterminal();
66#endif
67 visManager = new G4VisExecutive(std::to_string(visVerbosity));
68 bdsMessenger = new BDSMessenger();
69 if (realWorldIn)
70 {visManager->RegisterMessenger(new BDSVisCommandSceneAddQueryMagneticField(realWorldIn));}
71}
72
73BDSVisManager::~BDSVisManager()
74{
75 delete uiSession;
76 delete visManager;
77 delete bdsMessenger;
78}
79
80void BDSVisManager::StartSession(int argc, char** argv)
81{
82#ifdef G4VIS_USE
83 visManager->Initialize();
84
85 // setup trajectory colouring
86 G4TrajectoryDrawByCharge* trajModel1 = new G4TrajectoryDrawByCharge("bdsim_traj_by_charge");
87 const auto colours = BDSColours::Instance();
88 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Neutral, *(colours->GetColour("traj_neutral")));
89 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Positive, *(colours->GetColour("traj_positive")));
90 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Negative, *(colours->GetColour("traj_negative")));
91 visManager->RegisterModel(trajModel1);
92 visManager->SelectTrajectoryModel(trajModel1->Name());
93#endif
94
95#ifdef G4UI_USE
96 G4UIExecutive* uiExecutive = new G4UIExecutive(argc, argv);
97#ifdef G4VIS_USE
98
99 G4UImanager* UIManager = G4UImanager::GetUIpointer();
100 // setup paths to look for macros for the installation then the build directory
101 G4String bdsimExecPath = G4String(BDS::GetBDSIMExecPath());
102 G4String macroPaths = bdsimExecPath + "../share/bdsim/vis:@CMAKE_BINARY_DIR@/vis:./";
103 G4cout << __METHOD_NAME__ << "Setting macro path to: " << macroPaths << G4endl;
104 UIManager->ApplyCommand("/control/macroPath "+macroPaths);
105
106 G4String visMacName = visMacroFileName;
107 G4String visMacPath = visMacName; // by default just copy it
108 if (visMacName.empty()) // none specified - use default in BDSIM
109 {
110#if G4VERSION_NUMBER > 1119
111 visMacName = "bdsim_default_vis_11p2.mac";
112#else
113#ifdef G4VIS_USE_OPENGLQT
114 visMacName = "bdsim_default_vis.mac";
115#else
116 visMacName = "bdsim_default_dawnfile.mac";
117#endif
118#endif
119 // check if we find the file to at least let the user know what's being executed
120 visMacPath = UIManager->FindMacroPath(visMacName);
121 G4cout << __METHOD_NAME__ << "Visualisation macro name: " << visMacName << G4endl;
122 G4cout << __METHOD_NAME__ << "Visualisation macro path: " << visMacPath << G4endl;
123 if (visMacPath == visMacName) // this happens when the geant4 ui manager doesn't find the file in any directory
124 {// behaviour found from geant4 source code inspection...
125 G4cout << __METHOD_NAME__ << "bdsim_default_vis.mac missing from BDSIM installation directory." << G4endl;
126 return;
127 }
128 }
129 else
130 {// user specified visualisation macro - check if it exists
131 if (!BDS::FileExists(visMacPath))
132 {
133 std::cout << __METHOD_NAME__ << "ERROR: visualisation file " << visMacPath << " not present!" << G4endl;
134 return;
135 }
136 G4cout << __METHOD_NAME__ << "Using visualisation macro: " << visMacName << G4endl;
137 }
138 // execute the macro
139 UIManager->ApplyCommand("/control/execute " + visMacPath);
140
141 // apply optional macro if file name not empty
142 if (!geant4MacroFileName.empty())
143 {
144 G4cout << __METHOD_NAME__ << "Applying geant4MacroFileName: " << geant4MacroFileName << G4endl;
145 UIManager->ApplyCommand("/control/execute " + geant4MacroFileName);
146 }
147
148#if G4VERSION_NUMBER < 1030
149 if (uiExecutive->IsGUI())
150 {// these were added by default in Geant4.10.3 onwards
151 UIManager->ApplyCommand("/control/execute bdsim_default_icons.mac"); // add icons
152 UIManager->ApplyCommand("/gui/addIcon \"Run beam on\" user_icon \"/run/beamOn 1\" run.png"); // add run icon
153 UIManager->ApplyCommand("/control/execute bdsim_default_gui.mac"); // add menus
154 }
155#endif
156#endif
157 // run gui
158 uiExecutive->SessionStart();
159 delete uiExecutive;
160#endif
161}
static BDSColours * Instance()
singleton pattern
Definition BDSColours.cc:32
Class that constructs a Geant4 model of an accelerator.
User interface commands.
Scene describer for adding magnetic field lines according to a query.
G4String visMacroFileName
Full macro file path to run for visualisation.
G4String geant4MacroFileName
Full macro file path for optional macro to run afterwards.
void StartSession(int argc, char **argv)
Start interactive mode.
G4bool FileExists(const G4String &filename)
Checks if filename exists.
std::string GetBDSIMExecPath()