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