source: trunk/Documentation/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s09.html@ 901

Last change on this file since 901 was 901, checked in by garnier, 17 years ago

Add Geant4 Documentation at 8.12.2008

File size: 14.7 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>2.9.  How to Execute a Program</title><link rel="stylesheet" href="../xml/XSLCustomizationLayer/G4HTMLStylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="Geant4 User's Guide for Application Developers"><link rel="up" href="ch02.html" title="Chapter 2.  Getting Started with Geant4 - Running a Simple Example"><link rel="prev" href="ch02s08.html" title="2.8.  How to Set Up an Interactive Session"><link rel="next" href="ch02s10.html" title="2.10.  How to Visualize the Detector and Events"><script language="JavaScript">
2function remote_win(fName)
3{
4 var url = "AllResources/Detector/geometry.src/" + fName;
5 RemoteWin=window.open(url,"","resizable=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,width=520,height=520")
6 RemoteWin.creator=self
7}
8</script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.9. 
9How to Execute a Program
10</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s08.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><th width="60%" align="center">Chapter 2. 
11Getting Started with Geant4 - Running a Simple Example
12</th><td width="20%" align="right"> <a accesskey="n" href="ch02s10.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sect.HowToExec"></a>2.9. 
13How to Execute a Program
14</h2></div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToExec.Intro"></a>2.9.1. 
15Introduction
16</h3></div></div></div><p>
17A Geant4 application can be run either in
18
19</p><div class="itemizedlist"><ul type="disc" compact><li><p>
20 `purely hard-coded` batch mode
21 </p></li><li><p>
22 batch mode, but reading a macro of commands
23 </p></li><li><p>
24 interactive mode, driven by command lines
25 </p></li><li><p>
26 interactive mode via a Graphical User Interface
27 </p></li></ul></div><p>
28
29The last mode will be covered in <a href="ch02s08.html" title="2.8. 
30How to Set Up an Interactive Session
31">Section 2.8</a>.
32The first three modes are explained here.
33</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToExec.HardCodedBatch"></a>2.9.2. 
34'Hard-coded' Batch Mode
35</h3></div></div></div><p>
36Below is an example of the main program for an application which
37will run in batch mode.
38
39</p><div class="example"><a name="programlist_HowToExec_1"></a><p class="title"><b>Example 2.20. 
40An example of the <code class="literal">main()</code> routine
41for an application which will run in batch mode.
42</b></p><div class="example-contents"><pre class="programlisting">
43int main()
44{
45 // Construct the default run manager
46 G4RunManager* runManager = new G4RunManager;
47
48 // set mandatory initialization classes
49 runManager-&gt;SetUserInitialization(new ExN01DetectorConstruction);
50 runManager-&gt;SetUserInitialization(new ExN01PhysicsList);
51
52 // set mandatory user action class
53 runManager-&gt;SetUserAction(new ExN01PrimaryGeneratorAction);
54
55 // Initialize G4 kernel
56 runManager-&gt;Initialize();
57
58 // start a run
59 int numberOfEvent = 1000;
60 runManager-&gt;BeamOn(numberOfEvent);
61
62 // job termination
63 delete runManager;
64 return 0;
65}
66</pre></div></div><p><br class="example-break">
67</p><p>
68Even the number of events in the run is `frozen`. To change this
69number you must at least recompile <code class="literal">main()</code>.
70</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToExec.BatchMacro"></a>2.9.3. 
71Batch Mode with Macro File
72</h3></div></div></div><p>
73Below is an example of the main program for an application which
74will run in batch mode, but reading a file of commands.
75
76</p><div class="example"><a name="programlist_HowToExec_2"></a><p class="title"><b>Example 2.21. 
77An example of the <code class="literal">main()</code> routine
78for an application which will run in batch mode, but reading a file of commands.
79</b></p><div class="example-contents"><pre class="programlisting">
80int main(int argc,char** argv) {
81
82 // Construct the default run manager
83 G4RunManager * runManager = new G4RunManager;
84
85 // set mandatory initialization classes
86 runManager-&gt;SetUserInitialization(new MyDetectorConstruction);
87 runManager-&gt;SetUserInitialization(new MyPhysicsList);
88
89 // set mandatory user action class
90 runManager-&gt;SetUserAction(new MyPrimaryGeneratorAction);
91
92 // Initialize G4 kernel
93 runManager-&gt;Initialize();
94
95 //read a macro file of commands
96 G4UImanager * UI = G4UImanager::getUIpointer();
97 G4String command = "/control/execute ";
98 G4String fileName = argv[1];
99 UI-&gt;applyCommand(command+fileName);
100
101 delete runManager;
102 return 0;
103}
104</pre></div></div><p><br class="example-break">
105</p><p>
106This example will be executed with the command:
107
108</p><div class="informalexample"><pre class="programlisting">
109 &gt; myProgram run1.mac
110</pre></div><p>
111
112where <code class="literal">myProgram</code> is the name of your executable and
113<code class="literal">run1.mac</code> is a macro of commands located in the current
114directory, which could look like:
115
116</p><div class="example"><a name="programlist_HowToExec_3"></a><p class="title"><b>Example 2.22. 
117A typical command macro.
118</b></p><div class="example-contents"><pre class="programlisting">
119#
120# Macro file for "myProgram.cc"
121#
122# set verbose level for this run
123#
124/run/verbose 2
125/event/verbose 0
126/tracking/verbose 1
127#
128# Set the initial kinematic and run 100 events
129# electron 1 GeV to the direction (1.,0.,0.)
130#
131/gun/particle e-
132/gun/energy 1 GeV
133/run/beamOn 100
134</pre></div></div><p><br class="example-break">
135</p><p>
136Indeed, you can re-execute your program with different run
137conditions without recompiling anything.
138</p><p>
139<span class="emphasis"><em>Digression:</em></span>
140many G4 category of classes have a verbose flag which controls
141the level of 'verbosity'.
142</p><p>
143Usually <code class="literal">verbose=0</code> means silent. For instance
144
145</p><div class="itemizedlist"><ul type="disc" compact><li><p>
146 <code class="literal">/run/verbose</code> is for the <code class="literal">RunManager</code>
147 </p></li><li><p>
148 <code class="literal">/event/verbose</code> is for the <code class="literal">EventManager</code>
149 </p></li><li><p>
150 <code class="literal">/tracking/verbose</code> is for the <code class="literal">TrackingManager</code>
151 </p></li><li><p>
152 ...etc...
153 </p></li></ul></div><p>
154</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToExec.InteractiveMode"></a>2.9.4. 
155Interactive Mode Driven by Command Lines
156</h3></div></div></div><p>
157Below is an example of the main program for an application which
158will run interactively, waiting for command lines entered from the
159keyboard.
160
161</p><div class="example"><a name="programlist_HowToExec_4"></a><p class="title"><b>Example 2.23. 
162An example of the <code class="literal">main()</code> routine for
163an application which will run interactively, waiting for commands from the
164keyboard.
165</b></p><div class="example-contents"><pre class="programlisting">
166int main(int argc,char** argv) {
167
168 // Construct the default run manager
169 G4RunManager * runManager = new G4RunManager;
170
171 // set mandatory initialization classes
172 runManager-&gt;SetUserInitialization(new MyDetectorConstruction);
173 runManager-&gt;SetUserInitialization(new MyPhysicsList);
174
175 // visualization manager
176 G4VisManager* visManager = new G4VisExecutive;
177 visManager-&gt;Initialize();
178
179 // set user action classes
180 runManager-&gt;SetUserAction(new MyPrimaryGeneratorAction);
181 runManager-&gt;SetUserAction(new MyRunAction);
182 runManager-&gt;SetUserAction(new MyEventAction);
183 runManager-&gt;SetUserAction(new MySteppingAction);
184
185 // Initialize G4 kernel
186 runManager-&gt;Initialize();
187
188 // Define UI terminal for interactive mode
189 G4UIsession * session = new G4UIterminal;
190 session-&gt;SessionStart();
191 delete session;
192
193 // job termination
194 delete visManager;
195 delete runManager;
196
197 return 0;
198}
199</pre></div></div><p><br class="example-break">
200</p><p>
201This example will be executed with the command:
202
203</p><div class="informalexample"><pre class="programlisting">
204 &gt; myProgram
205</pre></div><p>
206
207where <code class="literal">myProgram</code> is the name of your executable.
208</p><p>
209The G4 kernel will prompt:
210
211</p><div class="informalexample"><pre class="programlisting">
212 Idle&gt;
213</pre></div><p>
214
215and you can start your session. An example session could be:
216</p><p>
217Create an empty scene ("world" is default):
218
219</p><div class="informalexample"><pre class="programlisting">
220 Idle&gt; /vis/scene/create
221</pre></div><p>
222
223Add a volume to the scene:
224
225</p><div class="informalexample"><pre class="programlisting">
226 Idle&gt; /vis/scene/add/volume
227</pre></div><p>
228</p><p>
229Create a scene handler for a specific graphics system. Change the
230next line to choose another graphic system:
231
232</p><div class="informalexample"><pre class="programlisting">
233 Idle&gt; /vis/sceneHandler/create OGLIX
234</pre></div><p>
235
236Create a viewer:
237
238</p><div class="informalexample"><pre class="programlisting">
239 Idle&gt; /vis/viewer/create
240</pre></div><p>
241
242Draw the scene, etc.:
243
244</p><div class="informalexample"><pre class="programlisting">
245 Idle&gt; /vis/scene/notifyHandlers
246 Idle&gt; /run/verbose 0
247 Idle&gt; /event/verbose 0
248 Idle&gt; /tracking/verbose 1
249 Idle&gt; /gun/particle mu+
250 Idle&gt; /gun/energy 10 GeV
251 Idle&gt; /run/beamOn 1
252 Idle&gt; /gun/particle proton
253 Idle&gt; /gun/energy 100 MeV
254 Idle&gt; /run/beamOn 3
255 Idle&gt; exit
256</pre></div><p>
257</p><p>
258For the meaning of the machine state <code class="literal">Idle</code>, see
259<a href="ch03s04.html#sect.Run.StateMac" title="3.4.2. 
260Geant4 as a state machine
261">Section 3.4.2</a>.
262</p><p>
263This mode is useful for running a few events in debug mode and
264visualizing them. Notice that the <span class="emphasis"><em>VisManager</em></span> is created in
265the <code class="literal">main()</code>, and the visualization system is choosen via
266the command:
267
268</p><div class="informalexample"><pre class="programlisting">
269 /vis/sceneHandler/create OGLIX
270</pre></div><p>
271</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToExec.GeneralCase"></a>2.9.5. 
272General Case
273</h3></div></div></div><p>
274Most of the examples in the <code class="literal">$G4INSTALL/examples/</code> directory
275have the following <code class="literal">main()</code>, which covers cases 2 and 3
276above. Thus, the application can be run either in batch or
277interactive mode.
278
279</p><div class="example"><a name="programlist_HowToExec_5"></a><p class="title"><b>Example 2.24. 
280The typical <code class="literal">main()</code> routine from the examples directory.
281</b></p><div class="example-contents"><pre class="programlisting">
282int main(int argc,char** argv) {
283
284 // Construct the default run manager
285 G4RunManager * runManager = new G4RunManager;
286
287 // set mandatory initialization classes
288 N03DetectorConstruction* detector = new N03DetectorConstruction;
289 runManager-&gt;SetUserInitialization(detector);
290 runManager-&gt;SetUserInitialization(new N03PhysicsList);
291
292#ifdef G4VIS_USE
293 // visualization manager
294 G4VisManager* visManager = new G4VisExecutive;
295 visManager-&gt;Initialize();
296#endif
297
298 // set user action classes
299 runManager-&gt;SetUserAction(new N03PrimaryGeneratorAction(detector));
300 runManager-&gt;SetUserAction(new N03RunAction);
301 runManager-&gt;SetUserAction(new N03EventAction);
302 runManager-&gt;SetUserAction(new N03SteppingAction);
303
304 // get the pointer to the User Interface manager
305 G4UImanager* UI = G4UImanager::GetUIpointer();
306
307 if (argc==1) // Define UI terminal for interactive mode
308 {
309 G4UIsession * session = new G4UIterminal;
310 UI-&gt;ApplyCommand("/control/execute prerunN03.mac");
311 session-&gt;SessionStart();
312 delete session;
313 }
314 else // Batch mode
315 {
316 G4String command = "/control/execute ";
317 G4String fileName = argv[1];
318 UI-&gt;ApplyCommand(command+fileName);
319 }
320
321 // job termination
322#ifdef G4VIS_USE
323 delete visManager;
324#endif
325 delete runManager;
326
327 return 0;
328}
329</pre></div></div><p><br class="example-break">
330</p><p>
331Notice that the visualization system is under the control of the
332precompiler variable <code class="literal">G4VIS_USE</code>. Notice also that, in
333interactive mode, few intializations have been put in the macro
334<code class="literal">prerunN03.mac</code> which is executed before the session
335start.
336
337</p><div class="example"><a name="programlist_HowToExec_6"></a><p class="title"><b>Example 2.25. 
338The <code class="literal">prerunN03.mac</code> macro.
339</b></p><div class="example-contents"><pre class="programlisting">
340 # Macro file for the initialization phase of "exampleN03.cc"
341 #
342 # Sets some default verbose flags
343 # and initializes the graphics.
344 #
345 /control/verbose 2
346 /control/saveHistory
347 /run/verbose 2
348 #
349 /run/particle/dumpCutValues
350 #
351 # Create empty scene ("world" is default)
352 /vis/scene/create
353 #
354 # Add volume to scene
355 /vis/scene/add/volume
356 #
357 # Create a scene handler for a specific graphics system
358 # Edit the next line(s) to choose another graphic system
359 #
360 #/vis/sceneHandler/create DAWNFILE
361 /vis/sceneHandler/create OGLIX
362 #
363 # Create a viewer
364 /vis/viewer/create
365 #
366 # Draw scene
367 /vis/scene/notifyHandlers
368 #
369 # for drawing the tracks
370 # if too many tracks cause core dump =&gt; storeTrajectory 0
371 /tracking/storeTrajectory 1
372 #/vis/scene/include/trajectories
373</pre></div></div><p><br class="example-break">
374</p><p>
375Also, this example demonstrates that you can read and execute a
376macro interactively:
377
378</p><div class="informalexample"><pre class="programlisting">
379 Idle&gt; /control/execute mySubMacro.mac
380</pre></div><p>
381</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s08.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="ch02.html"><img src="AllResources/IconsGIF/up.gif" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s10.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">2.8. 
382How to Set Up an Interactive Session
383 </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="AllResources/IconsGIF/home.gif" alt="Home"></a></td><td width="40%" align="right" valign="top"> 2.10. 
384How to Visualize the Detector and Events
385</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.