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