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

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

CVS update

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<!--    Updated by : Koichi Murakami, Dec-2009                -->
11<!--                                                          -->
12<!-- ******************************************************** -->
13
14
15<!-- ******************* Section (Level#1) ****************** -->
16<sect1 id="sect.HowToExec">
17<title>
18How to Execute a Program
19</title>
20
21
22<!-- ******************* Section (Level#2) ****************** -->
23<sect2 id="sect.HowToExec.Intro">
24<title>
25Introduction
26</title>
27
28<para>
29A Geant4 application can be run either in
30
31<itemizedlist spacing="compact">
32  <listitem><para>
33  `purely hard-coded` batch mode
34  </para></listitem>
35  <listitem><para>
36  batch mode, but reading a macro of commands
37  </para></listitem>
38  <listitem><para>
39  interactive mode, driven by command lines
40  </para></listitem>
41  <listitem><para>
42  interactive mode via a Graphical User Interface
43  </para></listitem>
44</itemizedlist>
45
46The last mode will be covered in <xref linkend="sect.HowToSetUpInter" />.
47The first three modes are explained here.
48</para>
49
50</sect2>
51
52<!-- ******************* Section (Level#2) ****************** -->
53<sect2 id="sect.HowToExec.HardCodedBatch">
54<title>
55'Hard-coded' Batch Mode
56</title>
57
58<para>
59Below is an example of the main program for an application which
60will run in batch mode.
61
62<example id="programlist_HowToExec_1">
63<title>
64An example of the <literal>main()</literal> routine
65for an application which will run in batch mode.
66</title>
67<programlisting>
68int main()
69{
70  // Construct the default run manager
71  G4RunManager* runManager = new G4RunManager;
72
73  // set mandatory initialization classes
74  runManager-&gt;SetUserInitialization(new ExN01DetectorConstruction);
75  runManager-&gt;SetUserInitialization(new ExN01PhysicsList);
76
77  // set mandatory user action class
78  runManager-&gt;SetUserAction(new ExN01PrimaryGeneratorAction);
79
80  // Initialize G4 kernel
81  runManager-&gt;Initialize();
82
83  // start a run
84  int numberOfEvent = 1000;
85  runManager-&gt;BeamOn(numberOfEvent);
86
87  // job termination
88  delete runManager;
89  return 0;
90}
91</programlisting>
92</example>
93</para>
94
95<para>
96Even the number of events in the run is `frozen`. To change this
97number you must at least recompile <literal>main()</literal>.
98</para>
99
100</sect2>
101
102
103<!-- ******************* Section (Level#2) ****************** -->
104<sect2 id="sect.HowToExec.BatchMacro">
105<title>
106Batch Mode with Macro File
107</title>
108
109<para>
110Below is an example of the main program for an application which
111will run in batch mode, but reading a file of commands.
112
113<example id="programlist_HowToExec_2">
114<title>
115An example of the <literal>main()</literal> routine
116for an application which will run in batch mode, but reading a file of commands.
117</title>
118<programlisting>
119int main(int argc,char** argv) {
120
121   // Construct the default run manager
122  G4RunManager * runManager = new G4RunManager;
123 
124  // set mandatory initialization classes
125  runManager-&gt;SetUserInitialization(new MyDetectorConstruction);
126  runManager-&gt;SetUserInitialization(new MyPhysicsList);
127 
128  // set mandatory user action class
129  runManager-&gt;SetUserAction(new MyPrimaryGeneratorAction);
130 
131  // Initialize G4 kernel
132  runManager-&gt;Initialize();
133
134  //read a macro file of commands
135  G4UImanager * UI = G4UImanager::getUIpointer();
136  G4String command = "/control/execute ";
137  G4String fileName = argv[1];
138  UI-&gt;applyCommand(command+fileName);
139
140  delete runManager;
141  return 0;
142}
143</programlisting>
144</example>
145</para>
146
147<para>
148This example will be executed with the command:
149
150<informalexample>
151<programlisting>
152    &gt; myProgram  run1.mac
153</programlisting>
154</informalexample>
155
156where <literal>myProgram</literal> is the name of your executable and
157<literal>run1.mac</literal> is a macro of commands located in the current
158directory, which could look like:
159
160<example id="programlist_HowToExec_3">
161<title>
162A typical command macro.
163</title>
164<programlisting>
165#
166# Macro file for "myProgram.cc"
167#
168# set verbose level for this run
169#
170/run/verbose      2
171/event/verbose    0
172/tracking/verbose 1
173#
174# Set the initial kinematic and run 100 events
175# electron 1 GeV to the direction (1.,0.,0.)
176#
177/gun/particle e-
178/gun/energy 1 GeV
179/run/beamOn 100
180</programlisting>
181</example>
182</para>
183
184<para>
185Indeed, you can re-execute your program with different run
186conditions without recompiling anything.
187</para>
188
189<para>
190<emphasis>Digression:</emphasis> 
191many G4 category of classes have a verbose flag which controls
192the level of 'verbosity'.
193</para>
194
195<para>
196Usually <literal>verbose=0</literal> means silent. For instance
197
198<itemizedlist spacing="compact">
199  <listitem><para>
200  <literal>/run/verbose</literal> is for the <literal>RunManager</literal>
201  </para></listitem>
202  <listitem><para>
203  <literal>/event/verbose</literal> is for the <literal>EventManager</literal>
204  </para></listitem>
205  <listitem><para>
206  <literal>/tracking/verbose</literal> is for the <literal>TrackingManager</literal>
207  </para></listitem>
208  <listitem><para>
209  ...etc...
210  </para></listitem>
211</itemizedlist>
212</para>
213
214</sect2>
215
216<!-- ******************* Section (Level#2) ****************** -->
217<sect2 id="sect.HowToExec.InteractiveMode">
218<title>
219Interactive Mode Driven by Command Lines
220</title>
221
222<para>
223Below is an example of the main program for an application which
224will run interactively, waiting for command lines entered from the
225keyboard.
226
227<example id="programlist_HowToExec_4">
228<title>
229An example of the <literal>main()</literal> routine for
230an application which will run interactively, waiting for commands from the
231keyboard.
232</title>
233<programlisting>
234int main(int argc,char** argv) {
235
236  // Construct the default run manager
237  G4RunManager * runManager = new G4RunManager;
238
239  // set mandatory initialization classes
240  runManager-&gt;SetUserInitialization(new MyDetectorConstruction);
241  runManager-&gt;SetUserInitialization(new MyPhysicsList);
242 
243  // visualization manager
244  G4VisManager* visManager = new G4VisExecutive;
245  visManager-&gt;Initialize();
246   
247  // set user action classes
248  runManager-&gt;SetUserAction(new MyPrimaryGeneratorAction);
249  runManager-&gt;SetUserAction(new MyRunAction);
250  runManager-&gt;SetUserAction(new MyEventAction);
251  runManager-&gt;SetUserAction(new MySteppingAction);
252 
253  // Initialize G4 kernel
254  runManager-&gt;Initialize();
255 
256  // Define UI terminal for interactive mode   
257  G4UIsession * session = new G4UIterminal;   
258  session-&gt;SessionStart();
259  delete session;
260
261  // job termination
262  delete visManager;
263  delete runManager;
264
265  return 0;
266}
267</programlisting>
268</example>
269</para>                           
270
271<para>
272This example will be executed with the command:
273
274<informalexample>
275<programlisting>
276  &gt; myProgram                 
277</programlisting>
278</informalexample>
279
280where <literal>myProgram</literal> is the name of your executable.
281</para>
282
283<para>
284The G4 kernel will prompt:
285
286<informalexample>
287<programlisting>
288  Idle&gt;
289</programlisting>
290</informalexample>
291
292and you can start your session. An example session could be:
293</para>
294
295<para>
296Create an empty scene ("world" is default):
297
298<informalexample>
299<programlisting>
300  Idle&gt; /vis/scene/create
301</programlisting>
302</informalexample>
303
304Add a volume to the scene:
305
306<informalexample>
307<programlisting>
308  Idle&gt; /vis/scene/add/volume
309</programlisting>
310</informalexample>
311</para>
312
313<para>
314Create a scene handler for a specific graphics system. Change the
315next line to choose another graphic system:
316
317<informalexample>
318<programlisting>
319  Idle&gt; /vis/sceneHandler/create OGLIX
320</programlisting>
321</informalexample>
322
323Create a viewer:
324
325<informalexample>
326<programlisting>
327  Idle&gt; /vis/viewer/create
328</programlisting>
329</informalexample>
330
331Draw the scene, etc.:
332
333<informalexample>
334<programlisting>
335  Idle&gt; /vis/scene/notifyHandlers
336  Idle&gt; /run/verbose      0
337  Idle&gt; /event/verbose    0
338  Idle&gt; /tracking/verbose 1
339  Idle&gt; /gun/particle mu+
340  Idle&gt; /gun/energy 10 GeV
341  Idle&gt; /run/beamOn 1
342  Idle&gt; /gun/particle proton
343  Idle&gt; /gun/energy 100 MeV
344  Idle&gt; /run/beamOn 3           
345  Idle&gt; exit                 
346</programlisting>
347</informalexample>
348</para>
349
350<para>
351For the meaning of the machine state <literal>Idle</literal>, see
352<xref linkend="sect.Run.StateMac" />.
353</para>
354
355<para>
356This mode is useful for running a few events in debug mode and
357visualizing them. Notice that the <emphasis>VisManager</emphasis> is created in
358the <literal>main()</literal>, and the visualization system is choosen via
359the command:
360
361<informalexample>
362<programlisting>
363  /vis/sceneHandler/create OGLIX
364</programlisting>
365</informalexample>
366</para>
367
368</sect2>
369
370<!-- ******************* Section (Level#2) ****************** -->
371<sect2 id="sect.HowToExec.GeneralCase">
372<title>
373General Case
374</title>
375
376<para>
377Most of the examples in the <literal>$G4INSTALL/examples/</literal> directory
378have the following <literal>main()</literal>.
379The application can be run either in batch or interactive 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     G4UIExecutive* session = new G4UIExecutive(argc, argv);
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.