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