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) Royal Holloway,
3University of London 2001 - 2024.
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
50#include <string>
51
52
53BDSVisManager::BDSVisManager(const G4String& visMacroFileNameIn,
54 const G4String& geant4MacroFileNameIn,
55 const BDSDetectorConstruction* realWorldIn,
56 G4int visVerbosity):
57 uiSession(nullptr),
58 visManager(nullptr),
59 bdsMessenger(nullptr),
60 visMacroFileName(visMacroFileNameIn),
61 geant4MacroFileName(geant4MacroFileNameIn)
62{
63#ifdef G4UI_USE_TCSH
64 uiSession = new G4UIterminal(new G4UItcsh);
65#else
66 uiSession = new G4UIterminal();
67#endif
68 visManager = new G4VisExecutive(std::to_string(visVerbosity));
69 bdsMessenger = new BDSMessenger();
70 if (realWorldIn)
71 {visManager->RegisterMessenger(new BDSVisCommandSceneAddQueryMagneticField(realWorldIn));}
72}
73
74BDSVisManager::~BDSVisManager()
75{
76 delete uiSession;
77 delete visManager;
78 delete bdsMessenger;
79}
80
81void BDSVisManager::StartSession(int argc, char** argv)
82{
83#ifdef G4VIS_USE
84 visManager->Initialize();
85
86 // setup trajectory colouring
87 G4TrajectoryDrawByCharge* trajModel1 = new G4TrajectoryDrawByCharge("bdsim_traj_by_charge");
88 const auto colours = BDSColours::Instance();
89 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Neutral, *(colours->GetColour("traj_neutral")));
90 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Positive, *(colours->GetColour("traj_positive")));
91 trajModel1->Set(G4TrajectoryDrawByCharge::Charge::Negative, *(colours->GetColour("traj_negative")));
92 visManager->RegisterModel(trajModel1);
93 visManager->SelectTrajectoryModel(trajModel1->Name());
94#endif
95
96#ifdef G4UI_USE
97 G4UIExecutive* uiExecutive = new G4UIExecutive(argc, argv);
98#ifdef G4VIS_USE
99
100 G4UImanager* UIManager = G4UImanager::GetUIpointer();
101 // setup paths to look for macros for the installation then the build directory
102 G4String bdsimExecPath = G4String(BDS::GetBDSIMExecPath());
103 G4String macroPaths = bdsimExecPath + "../share/bdsim/vis:@CMAKE_BINARY_DIR@/vis:./";
104 G4cout << __METHOD_NAME__ << "Setting macro path to: " << macroPaths << G4endl;
105 UIManager->ApplyCommand("/control/macroPath "+macroPaths);
106
107 G4String visMacName = visMacroFileName;
108 G4String visMacPath = visMacName; // by default just copy it
109 if (visMacName.empty()) // none specified - use default in BDSIM
110 {
111#if G4VERSION_NUMBER > 1119
112 visMacName = "bdsim_default_vis_11p2.mac";
113#else
114#ifdef G4VIS_USE_OPENGLQT
115 visMacName = "bdsim_default_vis.mac";
116#else
117 visMacName = "bdsim_default_dawnfile.mac";
118#endif
119#endif
120 // check if we find the file to at least let the user know what's being executed
121 visMacPath = UIManager->FindMacroPath(visMacName);
122 G4cout << __METHOD_NAME__ << "Visualisation macro name: " << visMacName << G4endl;
123 G4cout << __METHOD_NAME__ << "Visualisation macro path: " << visMacPath << G4endl;
124 if (visMacPath == visMacName) // this happens when the geant4 ui manager doesn't find the file in any directory
125 {// behaviour found from geant4 source code inspection...
126 G4cout << __METHOD_NAME__ << "bdsim_default_vis.mac missing from BDSIM installation directory." << G4endl;
127 return;
128 }
129 }
130 else
131 {// user specified visualisation macro - check if it exists
132 if (!BDS::FileExists(visMacPath))
133 {
134 std::cout << __METHOD_NAME__ << "ERROR: visualisation file " << visMacPath << " not present!" << G4endl;
135 return;
136 }
137 G4cout << __METHOD_NAME__ << "Using visualisation macro: " << visMacName << G4endl;
138 }
139 // execute the macro
140 UIManager->ApplyCommand("/control/execute " + visMacPath);
141
142 // apply optional macro if file name not empty
143 if (!geant4MacroFileName.empty())
144 {
145 G4cout << __METHOD_NAME__ << "Applying geant4MacroFileName: " << geant4MacroFileName << G4endl;
146 UIManager->ApplyCommand("/control/execute " + geant4MacroFileName);
147 }
148
149#if G4VERSION_NUMBER < 1030
150 if (uiExecutive->IsGUI())
151 {// these were added by default in Geant4.10.3 onwards
152 UIManager->ApplyCommand("/control/execute bdsim_default_icons.mac"); // add icons
153 UIManager->ApplyCommand("/gui/addIcon \"Run beam on\" user_icon \"/run/beamOn 1\" run.png"); // add run icon
154 UIManager->ApplyCommand("/control/execute bdsim_default_gui.mac"); // add menus
155 }
156#endif
157#endif
158 // run gui
159 uiExecutive->SessionStart();
160 delete uiExecutive;
161#endif
162}
static BDSColours * Instance()
singleton pattern
Definition BDSColours.cc:33
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()