source: trunk/documents/UserDoc/DocBookUsersGuides/ForApplicationDeveloper/xml/GettingStarted/executeProgram.xml @ 904

Last change on this file since 904 was 904, checked in by garnier, 16 years ago

ajout de la doc

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