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