source: trunk/documents/UserDoc/UsersGuides/ForApplicationDeveloper/html/GettingStarted/executeProgram.html @ 1358

Last change on this file since 1358 was 1208, checked in by garnier, 15 years ago

CVS update

File size: 10.9 KB
Line 
1<HTML>
2<TITLE>
3</TITLE>
4<!-- Changed by: Katsuya Amako, 30-Jul-1998 -->
5<!-- Changed by: Michel MAIRE,   3-Aug-1998 -->
6<!-- Changed by: Katsuya Amako, 16-Nov-1998 -->
7<!-- Changed by: Dennis Wright, 28-Nov-2001 -->
8<!-- Proof read by: Joe Chuma,  15-Jun-1999 -->
9<BODY>
10<TABLE WIDTH="100%"><TR>
11<TD>
12
13
14<A HREF="index.html">
15<IMG SRC="../../../../resources/html/IconsGIF/Contents.gif" ALT="Contents"></A>
16<A HREF="graphicalUserInterface.html">
17<IMG SRC="../../../../resources/html/IconsGIF/Previous.gif" ALT="Previous"></A>
18<A HREF="visualization.html">
19<IMG SRC="../../../../resources/html/IconsGIF/Next.gif" ALT="Next"></A>
20</TD>
21<TD ALIGN="Right">
22<FONT SIZE="-1" COLOR="#238E23">
23<B>Geant4 User's Guide</B>
24<BR>
25<B>For Application Developers</B>
26<BR>
27<B>Getting Started with Geant4</B>
28</FONT>
29</TD>
30</TR></TABLE>
31<BR><BR>
32
33<P ALIGN="Center">
34<FONT SIZE="+3" COLOR="#238E23">
35<B>2.9 How to Execute a Program</B>
36</FONT>
37<BR><BR>
38
39<HR ALIGN="Center" SIZE="7%">
40<p>
41
42<a name="2.9.1">
43<H2>2.9.1 Introduction</H2></a>
44
45 A Geant4 application can be run either in
46<UL>
47 <LI>`purely hard-coded` batch mode
48 <LI>batch mode, but reading a macro of commands
49 <LI>interactive mode, driven by command lines
50 <LI>interactive mode via a Graphical User Interface
51</UL>                             
52 The last mode will be covered in <a href="graphicalUserInterface.html">Section 2.8</a>.  The first three modes are explained here.
53<P>
54
55<hr>
56<a name="2.9.2">
57<H2>2.9.2 'Hard-coded' Batch Mode</H2></a>
58
59 Below is an example of the main program for an application which will run
60 in batch mode.
61<p>
62<center>
63<table border=2 cellpadding=10>
64<tr>
65<td>
66<PRE>                                     
67int main()
68{
69  // Construct the default run manager
70  G4RunManager* runManager = new G4RunManager;
71
72  // set mandatory initialization classes
73  runManager->SetUserInitialization(new ExN01DetectorConstruction);
74  runManager->SetUserInitialization(new ExN01PhysicsList);
75
76  // set mandatory user action class
77  runManager->SetUserAction(new ExN01PrimaryGeneratorAction);
78
79  // Initialize G4 kernel
80  runManager->Initialize();
81
82  // start a run
83  int numberOfEvent = 1000;
84  runManager->BeamOn(numberOfEvent);
85
86  // job termination
87  delete runManager;
88  return 0;
89}
90</PRE>
91</td>
92</tr>
93<tr>
94<td align=center>
95 Source listing 2.9.1<BR>
96 An example of the <tt>main()</tt> routine for an application
97 which will run in batch mode.
98</td>
99</tr>
100</table></center>
101<p>
102 Even the number of events in the run is `frozen`.  To change this number
103 you must at least recompile <tt>main()</tt>.
104<P>
105
106<HR> 
107<a name="2.9.3">
108<H2>2.9.3 Batch Mode with Macro File</H2></a>
109 
110Below is an example of the main program for an application which will run
111in batch mode, but reading a file of commands.
112<p>
113<center>
114<table border=2 cellpadding=10>
115<tr>
116<td>
117<PRE> 
118int main(int argc,char** argv) {
119
120   // Construct the default run manager
121  G4RunManager * runManager = new G4RunManager;
122 
123  // set mandatory initialization classes
124  runManager->SetUserInitialization(new MyDetectorConstruction);
125  runManager->SetUserInitialization(new MyPhysicsList);
126 
127  // set mandatory user action class
128  runManager->SetUserAction(new MyPrimaryGeneratorAction);
129 
130  // Initialize G4 kernel
131  runManager->Initialize();
132
133  //read a macro file of commands
134  G4UImanager * UI = G4UImanager::getUIpointer();
135  G4String command = "/control/execute ";
136  G4String fileName = argv[1];
137  UI->applyCommand(command+fileName);
138
139  delete runManager;
140  return 0;
141}
142</PRE>
143</td>
144</tr>
145<tr>
146<td align=center>
147 Source listing 2.9.2<BR>
148 An example of the <tt>main()</tt> routine for an application which will run
149 in batch mode, but reading a file of commands.
150</td>
151</tr>
152</table></center>
153<p>
154 This example will be executed with the command:
155<PRE>
156    > myProgram  run1.mac
157</PRE>
158<p>                 
159 where <tt>myProgram</tt> is the name of your executable and <TT>run1.mac</TT>
160 is a macro of commands located in the current directory, which could
161 look like:
162<p>
163<center>
164<table border=2 cellpadding=10>
165<tr>
166<td>
167<PRE> 
168#
169# Macro file for "myProgram.cc"
170#
171# set verbose level for this run
172#
173/run/verbose      2
174/event/verbose    0
175/tracking/verbose 1
176#
177# Set the initial kinematic and run 100 events
178# electron 1 GeV to the direction (1.,0.,0.)
179#
180/gun/particle e-
181/gun/energy 1 GeV
182/run/beamOn 100
183</PRE>
184</td>
185</tr>
186<tr>
187<td align=center>
188 Source listing 2.9.3<BR>
189 A typical command macro.
190</td>
191</tr>
192</table></center>
193<p>
194 Indeed, you can re-execute your program with different run conditions without
195 recompiling anything.
196<P> 
197<table>
198<tr>
199<td valign=top>Digression:
200<td>many G4 category of classes have a verbose flag which controls
201 the level of 'verbosity'.<br>
202 Usually <TT>verbose=0</TT> means silent. For instance
203<UL>
204<LI><t>/run/verbose</t> is for the <t>RunManager</t>
205<LI><t>/event/verbose</t> is for the <t>EventManager</t>
206<LI><t>/tracking/verbose</t> is for the <t>TrackingManager</t>
207<LI>...etc...
208</UL>                           
209</table>
210<P>
211
212<HR>                           
213<a name="2.9.4">
214<H2>2.9.4 Interactive Mode Driven by Command Lines</H2></a>
215
216 Below is an example of the main program for an application which will run
217 interactively, waiting for command lines entered from the keyboard.
218<p>
219<center>
220<table border=2 cellpadding=10>
221<tr>
222<td>
223<PRE>                           
224int main(int argc,char** argv) {
225
226  // Construct the default run manager
227  G4RunManager * runManager = new G4RunManager;
228
229  // set mandatory initialization classes
230  runManager->SetUserInitialization(new MyDetectorConstruction);
231  runManager->SetUserInitialization(new MyPhysicsList);
232 
233  // visualization manager
234  G4VisManager* visManager = new G4VisExecutive;
235  visManager->Initialize();
236   
237  // set user action classes
238  runManager->SetUserAction(new MyPrimaryGeneratorAction);
239  runManager->SetUserAction(new MyRunAction);
240  runManager->SetUserAction(new MyEventAction);
241  runManager->SetUserAction(new MySteppingAction);
242 
243  // Initialize G4 kernel
244  runManager->Initialize();
245 
246  // Define UI terminal for interactive mode   
247  G4UIsession * session = new G4UIterminal;   
248  session->SessionStart();
249  delete session;
250
251  // job termination
252  delete visManager;
253  delete runManager;
254
255  return 0;
256}
257</PRE>
258</td>
259</tr>
260<tr>
261<td align=center>
262 Source listing 2.9.4<BR>
263 An example of the <tt>main()</tt> routine for an application which will run
264 interactively, waiting for commands from the keyboard.
265</td>
266</tr>
267</table></center>
268<p>
269 This example will be executed with the command:
270<PRE>
271  > myProgram                 
272</PRE>
273 where <TT>myProgram</TT> is the name of your executable.
274<P> 
275 The G4 kernel will prompt: 
276<PRE>
277  Idle>
278</PRE>
279 and you can start your session. An example session could be:
280<p>
281 Create an empty scene ("world" is default):
282<PRE> 
283  Idle> /vis/scene/create
284</PRE>
285 Add a volume to the scene:
286<PRE> 
287  Idle> /vis/scene/add/volume
288</PRE>
289 Create a scene handler for a specific graphics system.
290 Change the next line to choose another graphic system:
291<PRE> 
292  Idle> /vis/sceneHandler/create OGLIX
293</PRE>
294 Create a viewer:
295<PRE> 
296  Idle> /vis/viewer/create
297</PRE>
298 Draw the scene, etc.:
299<PRE> 
300  Idle> /vis/scene/notifyHandlers
301  Idle> /run/verbose      0
302  Idle> /event/verbose    0
303  Idle> /tracking/verbose 1
304  Idle> /gun/particle mu+
305  Idle> /gun/energy 10 GeV
306  Idle> /run/beamOn 1
307  Idle> /gun/particle proton
308  Idle> /gun/energy 100 MeV
309  Idle> /run/beamOn 3           
310  Idle> exit                 
311</PRE>
312<p>
313 For the meaning of the machine state <TT>Idle</TT>, see
314 <a href="../Fundamentals/run.html#3.4.2">Section 3.4.2</a>.
315<P>
316This mode is useful for running a few events in debug mode and visualizing
317them.  Notice that the <i>VisManager</I> is created in the <tt>main()</tt>,
318and the visualization system is choosen via the command:
319<PRE>
320  /vis/sceneHandler/create OGLIX
321</PRE>
322<P>
323                 
324<HR>
325<a name="2.9.5">
326<H2>2.9.5 General Case</H2></a>
327
328 Most of the examples in the <TT>$G4INSTALL/examples/</TT> directory
329 have the following <TT>main()</TT>, which covers cases 2 and 3 above.
330 Thus, the application can be run either in batch or interactive mode.
331<p>
332<center>
333<table border=2 cellpadding=10>
334<tr>
335<td>
336<PRE>
337int main(int argc,char** argv) {
338
339  // Construct the default run manager
340  G4RunManager * runManager = new G4RunManager;
341
342  // set mandatory initialization classes
343  N03DetectorConstruction* detector = new N03DetectorConstruction;
344  runManager->SetUserInitialization(detector);
345  runManager->SetUserInitialization(new N03PhysicsList);
346 
347#ifdef G4VIS_USE
348  // visualization manager
349  G4VisManager* visManager = new G4VisExecutive;
350  visManager->Initialize();
351#endif
352   
353  // set user action classes
354  runManager->SetUserAction(new N03PrimaryGeneratorAction(detector));
355  runManager->SetUserAction(new N03RunAction);
356  runManager->SetUserAction(new N03EventAction);
357  runManager->SetUserAction(new N03SteppingAction);
358   
359  // get the pointer to the User Interface manager
360    G4UImanager* UI = G4UImanager::GetUIpointer(); 
361
362  if (argc==1)   // Define UI terminal for interactive mode 
363    {
364     G4UIsession * session = new G4UIterminal;
365     UI->ApplyCommand("/control/execute prerunN03.mac");   
366     session->SessionStart();
367     delete session;
368    }
369  else           // Batch mode
370    {
371     G4String command = "/control/execute ";
372     G4String fileName = argv[1];
373     UI->ApplyCommand(command+fileName);
374    }
375
376  // job termination
377#ifdef G4VIS_USE
378  delete visManager;
379#endif
380  delete runManager;
381
382  return 0;
383}
384</PRE>
385</td>
386</tr>
387<tr>
388<td align=center>
389 Source listing 2.9.5<BR>
390 The typical <tt>main()</tt> routine from the examples directory.
391</td>
392</tr>
393</table></center>
394<p>
395 Notice that the visualization system is under the control of the precompiler
396 variable <TT>G4VIS_USE</TT>.
397 Notice also that, in interactive mode, few intializations have been put in the
398 macro <TT>prerunN03.mac</TT> which is executed before the session start.
399<p>
400<center>
401<table border=2 cellpadding=10>
402<tr>
403<td>
404<PRE> 
405 # Macro file for the initialization phase of "exampleN03.cc"
406 #
407 # Sets some default verbose flags
408 # and initializes the graphics.
409 #
410 /control/verbose 2
411 /control/saveHistory
412 /run/verbose 2
413 #
414 /run/particle/dumpCutValues
415 #
416 # Create empty scene ("world" is default)
417 /vis/scene/create
418 #
419 # Add volume to scene
420 /vis/scene/add/volume
421 #
422 # Create a scene handler for a specific graphics system
423 # Edit the next line(s) to choose another graphic system
424 #
425 #/vis/sceneHandler/create DAWNFILE
426 /vis/sceneHandler/create OGLIX
427 #
428 # Create a viewer
429 /vis/viewer/create
430 #
431 # Draw scene
432 /vis/scene/notifyHandlers
433 #
434 # for drawing the tracks
435 # if too many tracks cause core dump => storeTrajectory 0
436 /tracking/storeTrajectory 1
437 #/vis/scene/include/trajectories
438</PRE>
439</td>
440</tr>
441<tr>
442<td align=center>
443 Source listing 2.9.6<BR>
444 The <tt>prerunN03.mac</tt> macro.
445</td>
446</tr>
447</table></center>
448<P>
449 Also, this example demonstrates that you can read and execute a macro interactively:
450<PRE> 
451  Idle> /control/execute  mySubMacro.mac
452</PRE>
453<p>
454
455<HR>
456<A HREF="../../../../Authors/html/subjectsToAuthors.html">
457<I>About the authors</I></A>
458
459</BODY>
460</HTML>
Note: See TracBrowser for help on using the repository browser.