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