]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TGeant4/TG4RunManager.cxx
renamed local constants
[u/mrichter/AliRoot.git] / TGeant4 / TG4RunManager.cxx
1 // $Id$
2 // Category: run
3 //
4 // See the class description in the header file.
5
6 #include "TG4RunManager.h"
7 #include "TG4RunMessenger.h"
8 #include "TG4VRunConfiguration.h"
9 #include "TGeant4.h"
10 #include "TG4Globals.h"
11 #include "TG4GeometryManager.h"
12 #include "TG4PhysicsManager.h"
13 //#include "TG4StepManager.h"
14
15 #include <G4RunManager.hh>
16 #include <G4StateManager.hh>
17 #include <G4UIsession.hh>
18 #include <G4UImanager.hh>
19 #include <G4UIterminal.hh>
20 #include <G4UIXm.hh>
21 #ifdef G4UI_USE_WO
22 #include <G4UIWo.hh>
23 #endif
24 #ifdef G4UI_USE_GAG
25 #include <G4UIGAG.hh>
26 #endif
27 #include <globals.hh>
28
29 #include <TRint.h>
30 #include <TROOT.h> 
31 #include <TCint.h> 
32
33 TG4RunManager* TG4RunManager::fgInstance = 0;
34
35 TG4RunManager::TG4RunManager(TG4VRunConfiguration* runConfiguration, 
36                              int argc, char** argv)               
37   : fRunConfiguration(runConfiguration),
38     fGeantUISession(0),
39     fRootUISession(0),
40     fRootUIOwner(false),
41     fARGC(argc),
42     fARGV(argv)
43   
44 {
45 // 
46   if (fgInstance) {
47     TG4Globals::Exception(
48       "TG4RunManager: attempt to create two instances of singleton.");
49   };
50       
51   if (!fRunConfiguration) {
52     TG4Globals::Exception(
53       "TG4RunManager: attempt to create instance without runConfiguration.");
54   };
55       
56   fgInstance = this;
57   
58   // create and configure geant4 run manager
59   fRunManager =  new G4RunManager();
60   fRunConfiguration->ConfigureRunManager(fRunManager);
61   // add verbose level
62   //G4cout << "G4RunManager has been created." << endl;
63
64   // create geant4 UI
65   CreateGeantUI();
66       // must be created before TG4VisManager::Initialize()
67       // (that is invoked in TGeant4 constructor)
68
69   // create root UI
70   CreateRootUI();
71
72   // messenger
73   fMessenger = new TG4RunMessenger(this);
74 }
75
76 TG4RunManager::TG4RunManager(TG4VRunConfiguration* runConfiguration)
77   : fRunConfiguration(runConfiguration),
78     fGeantUISession(0),
79     fRootUISession(0),
80     fRootUIOwner(false),
81     fARGC(0),
82     fARGV(0)
83   
84 {
85 //
86   if (fgInstance) {
87     TG4Globals::Exception(
88       "TG4RunManager: attempt to create two instances of singleton.");
89   };
90       
91   if (!fRunConfiguration) {
92     TG4Globals::Exception(
93       "TG4RunManager: attempt to create instance without runConfiguration.");
94   };
95       
96   fgInstance = this;
97   
98   // set primary UI
99   fRootUISession = gROOT->GetApplication();
100   if (fRootUISession) {
101     fARGC = fRootUISession->Argc();
102     fARGV = fRootUISession->Argv();
103   }
104
105   // create and configure geant4 run manager
106   fRunManager =  new G4RunManager();
107   fRunConfiguration->ConfigureRunManager(fRunManager);
108   // add verbose level
109   //G4cout << "G4RunManager has been created." << endl;
110
111   // create geant4 UI
112   CreateGeantUI();
113       // must be created before TG4VisManager::Initialize()
114       // (that is invoked in TGeant4 constructor)
115
116   // create root UI
117   CreateRootUI();
118
119   // messenger
120   fMessenger = new TG4RunMessenger(this);
121 }
122
123 TG4RunManager::TG4RunManager() {
124 //
125 }
126
127 TG4RunManager::TG4RunManager(const TG4RunManager& right) {
128 // 
129   TG4Globals::Exception(
130     "Attempt to copy TG4RunManager singleton.");
131 }
132
133 TG4RunManager::~TG4RunManager() {
134 //  
135   delete fRunConfiguration;
136   delete fRunManager;
137   delete fGeantUISession;
138   if (fRootUIOwner) delete fRootUISession;
139   delete fMessenger;
140 }
141
142 // operators
143
144 TG4RunManager& TG4RunManager::operator=(const TG4RunManager& right)
145 {
146   // check assignement to self
147   if (this == &right) return *this;
148
149   TG4Globals::Exception(
150     "Attempt to assign TG4RunManager singleton.");
151     
152   return *this;  
153 }    
154
155 // private methods
156
157 void TG4RunManager::CreateGeantUI()
158 {
159 // Creates interactive Geant4.
160 // ---
161
162   if (!fGeantUISession)
163   {
164     // create geant4 UI
165     G4UImanager* pUI = G4UImanager::GetUIpointer();  
166     if (fARGC == 1) {
167 #ifdef G4UI_USE_GAG
168       fGeantUISession = new G4UIGAG();
169 #else      
170       fGeantUISession = new G4UIterminal(); 
171 #endif      
172     }  
173     else if (strcmp (fARGV[1], "dumb") == 0) {
174       fGeantUISession = new G4UIterminal(); 
175     }
176 #ifdef G4UI_USE_WO
177     else if (strcmp (fARGV[1], "Wo") == 0) {
178       fGeantUISession = new G4UIWo(fARGC, fARGV); 
179     }
180 #endif
181 #ifdef G4UI_USE_XM
182     else if (strcmp (fARGV[1], "Xm") == 0) {
183       fGeantUISession = new G4UIXm(fARGC, fARGV); 
184     }
185 #endif
186 #ifdef G4UI_USE_XAW
187     else if (strcmp (fARGV[1], "Xaw") == 0) {
188       fGeantUISession = new G4UIXaw(fARGC, fARGV); 
189     }
190 #endif 
191 #ifdef G4UI_USE_GAG
192     else if (strcmp (fARGV[1], "GAG") == 0) {
193       fGeantUISession = new G4UIGAG (); 
194     }
195 #endif 
196     if (fGeantUISession) {   
197       pUI->SetSession(fGeantUISession); 
198     }
199   }
200 }
201
202 void TG4RunManager::CreateRootUI()
203 {
204 // Creates interactive Root.
205 // ---
206
207   if (!fRootUISession) 
208   {
209     // create session if it does not exist  
210     fRootUISession = new TRint("aliroot", 0, 0, 0, 0);
211
212     // set ownership of Root UI
213     fRootUIOwner = true;
214   }
215 }
216
217 // public methods
218
219 void TG4RunManager::Initialize()
220 {
221 // Initializes G4.
222 // ---
223
224   // initialize Geant4 
225   fRunManager->Initialize();
226   
227   // activate/inactivate physics processes
228   // (this operation is not allowed in "Init" phase)
229   TG4PhysicsManager* physicsManager = TG4PhysicsManager::Instance();
230   physicsManager->SetProcessActivation();
231 }
232
233 void TG4RunManager::ProcessEvent()
234 {
235 // Not yet implemented.
236 // ---
237
238   TG4Globals::Warning("TG4RunManager::ProcessEvent(): is not yet implemented.");
239 }
240     
241 void TG4RunManager::ProcessRun(G4int nofEvents)
242 {
243 // Processes Geant4 run.
244 // ---
245
246   fRunManager->BeamOn(nofEvents); 
247 }
248     
249 void TG4RunManager::StartGeantUI()
250
251 // Starts interactive/batch Geant4.
252 // ---
253
254   if (!fGeantUISession) CreateGeantUI();
255   if (fGeantUISession) {  
256     // interactive session
257     G4cout << "Welcome back in Geant4" << endl;
258     fGeantUISession->SessionStart();
259     G4cout << "Welcome back in Root" << endl;  
260   }
261   else {
262     // execute Geant4 macro if file is specified as an argument 
263     G4String fileName = fARGV[1];
264     ProcessGeantMacro(fileName);
265   }
266 }
267
268 void TG4RunManager::StartRootUI()
269 {
270 // Starts interactive Root.
271 // ---
272
273   if (!fRootUISession) CreateRootUI();
274   if (fRootUISession) { 
275     G4cout << "Welcome back in Root" << endl;
276     fRootUISession->Run(kTRUE);
277     G4cout << "Welcome back in Geant4" << endl;  
278   }
279 }
280  
281 void TG4RunManager::ProcessGeantMacro(G4String macroName)
282 {
283 // Processes Geant4 macro.
284 // ---
285
286   G4String command = "/control/execute " + macroName;
287   ProcessGeantCommand(command);
288 }
289  
290 void TG4RunManager::ProcessRootMacro(G4String macroName)
291 {
292 // Processes Root macro.
293 // ---
294
295   // load macro file
296   G4String macroFile = macroName;
297   macroFile.append(".C");
298   gROOT->LoadMacro(macroFile);
299
300   // execute macro function
301   G4String macroFunction = macroName;
302   macroFunction.append("()");
303   gInterpreter->ProcessLine(macroFunction);
304 }
305  
306 void TG4RunManager::ProcessGeantCommand(G4String command)
307 {
308 // Processes Geant4 command.
309 // ---
310
311   G4UImanager* pUI = G4UImanager::GetUIpointer();  
312   pUI->ApplyCommand(command);
313 }
314
315 void TG4RunManager::ProcessRootCommand(G4String command)
316 {
317 // Processes Root command.
318 // ---
319
320   gInterpreter->ProcessLine(command);
321 }
322
323 void TG4RunManager::UseG3Defaults() 
324 {
325 // Controls G3 defaults usage.
326 // ---
327
328   TG4GeometryManager::Instance()->UseG3TrackingMediaLimits();
329   TG4PhysicsManager::Instance()->SetG3DefaultCuts();
330   TG4PhysicsManager::Instance()->SetG3DefaultProcesses();
331 }
332
333 Int_t TG4RunManager::CurrentEvent() const
334 {
335 // Returns the number of the current event.
336 // ---
337
338   G4int eventID = fRunManager->GetCurrentEvent()->GetEventID();
339   return eventID;
340 }