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> |
---|
18 | How to Execute a Program |
---|
19 | </title> |
---|
20 | |
---|
21 | |
---|
22 | <!-- ******************* Section (Level#2) ****************** --> |
---|
23 | <sect2 id="sect.HowToExec.Intro"> |
---|
24 | <title> |
---|
25 | Introduction |
---|
26 | </title> |
---|
27 | |
---|
28 | <para> |
---|
29 | A 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 | |
---|
46 | The last mode will be covered in <xref linkend="sect.HowToSetUpInter" />. |
---|
47 | The 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> |
---|
59 | Below is an example of the main program for an application which |
---|
60 | will run in batch mode. |
---|
61 | |
---|
62 | <example id="programlist_HowToExec_1"> |
---|
63 | <title> |
---|
64 | An example of the <literal>main()</literal> routine |
---|
65 | for an application which will run in batch mode. |
---|
66 | </title> |
---|
67 | <programlisting> |
---|
68 | int main() |
---|
69 | { |
---|
70 | // Construct the default run manager |
---|
71 | G4RunManager* runManager = new G4RunManager; |
---|
72 | |
---|
73 | // set mandatory initialization classes |
---|
74 | runManager->SetUserInitialization(new ExN01DetectorConstruction); |
---|
75 | runManager->SetUserInitialization(new ExN01PhysicsList); |
---|
76 | |
---|
77 | // set mandatory user action class |
---|
78 | runManager->SetUserAction(new ExN01PrimaryGeneratorAction); |
---|
79 | |
---|
80 | // Initialize G4 kernel |
---|
81 | runManager->Initialize(); |
---|
82 | |
---|
83 | // start a run |
---|
84 | int numberOfEvent = 1000; |
---|
85 | runManager->BeamOn(numberOfEvent); |
---|
86 | |
---|
87 | // job termination |
---|
88 | delete runManager; |
---|
89 | return 0; |
---|
90 | } |
---|
91 | </programlisting> |
---|
92 | </example> |
---|
93 | </para> |
---|
94 | |
---|
95 | <para> |
---|
96 | Even the number of events in the run is `frozen`. To change this |
---|
97 | number 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> |
---|
106 | Batch Mode with Macro File |
---|
107 | </title> |
---|
108 | |
---|
109 | <para> |
---|
110 | Below is an example of the main program for an application which |
---|
111 | will run in batch mode, but reading a file of commands. |
---|
112 | |
---|
113 | <example id="programlist_HowToExec_2"> |
---|
114 | <title> |
---|
115 | An example of the <literal>main()</literal> routine |
---|
116 | for an application which will run in batch mode, but reading a file of commands. |
---|
117 | </title> |
---|
118 | <programlisting> |
---|
119 | int 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->SetUserInitialization(new MyDetectorConstruction); |
---|
126 | runManager->SetUserInitialization(new MyPhysicsList); |
---|
127 | |
---|
128 | // set mandatory user action class |
---|
129 | runManager->SetUserAction(new MyPrimaryGeneratorAction); |
---|
130 | |
---|
131 | // Initialize G4 kernel |
---|
132 | runManager->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->applyCommand(command+fileName); |
---|
139 | |
---|
140 | delete runManager; |
---|
141 | return 0; |
---|
142 | } |
---|
143 | </programlisting> |
---|
144 | </example> |
---|
145 | </para> |
---|
146 | |
---|
147 | <para> |
---|
148 | This example will be executed with the command: |
---|
149 | |
---|
150 | <informalexample> |
---|
151 | <programlisting> |
---|
152 | > myProgram run1.mac |
---|
153 | </programlisting> |
---|
154 | </informalexample> |
---|
155 | |
---|
156 | where <literal>myProgram</literal> is the name of your executable and |
---|
157 | <literal>run1.mac</literal> is a macro of commands located in the current |
---|
158 | directory, which could look like: |
---|
159 | |
---|
160 | <example id="programlist_HowToExec_3"> |
---|
161 | <title> |
---|
162 | A 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> |
---|
185 | Indeed, you can re-execute your program with different run |
---|
186 | conditions without recompiling anything. |
---|
187 | </para> |
---|
188 | |
---|
189 | <para> |
---|
190 | <emphasis>Digression:</emphasis> |
---|
191 | many G4 category of classes have a verbose flag which controls |
---|
192 | the level of 'verbosity'. |
---|
193 | </para> |
---|
194 | |
---|
195 | <para> |
---|
196 | Usually <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> |
---|
219 | Interactive Mode Driven by Command Lines |
---|
220 | </title> |
---|
221 | |
---|
222 | <para> |
---|
223 | Below is an example of the main program for an application which |
---|
224 | will run interactively, waiting for command lines entered from the |
---|
225 | keyboard. |
---|
226 | |
---|
227 | <example id="programlist_HowToExec_4"> |
---|
228 | <title> |
---|
229 | An example of the <literal>main()</literal> routine for |
---|
230 | an application which will run interactively, waiting for commands from the |
---|
231 | keyboard. |
---|
232 | </title> |
---|
233 | <programlisting> |
---|
234 | int 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->SetUserInitialization(new MyDetectorConstruction); |
---|
241 | runManager->SetUserInitialization(new MyPhysicsList); |
---|
242 | |
---|
243 | // visualization manager |
---|
244 | G4VisManager* visManager = new G4VisExecutive; |
---|
245 | visManager->Initialize(); |
---|
246 | |
---|
247 | // set user action classes |
---|
248 | runManager->SetUserAction(new MyPrimaryGeneratorAction); |
---|
249 | runManager->SetUserAction(new MyRunAction); |
---|
250 | runManager->SetUserAction(new MyEventAction); |
---|
251 | runManager->SetUserAction(new MySteppingAction); |
---|
252 | |
---|
253 | // Initialize G4 kernel |
---|
254 | runManager->Initialize(); |
---|
255 | |
---|
256 | // Define UI terminal for interactive mode |
---|
257 | G4UIsession * session = new G4UIterminal; |
---|
258 | session->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> |
---|
272 | This example will be executed with the command: |
---|
273 | |
---|
274 | <informalexample> |
---|
275 | <programlisting> |
---|
276 | > myProgram |
---|
277 | </programlisting> |
---|
278 | </informalexample> |
---|
279 | |
---|
280 | where <literal>myProgram</literal> is the name of your executable. |
---|
281 | </para> |
---|
282 | |
---|
283 | <para> |
---|
284 | The G4 kernel will prompt: |
---|
285 | |
---|
286 | <informalexample> |
---|
287 | <programlisting> |
---|
288 | Idle> |
---|
289 | </programlisting> |
---|
290 | </informalexample> |
---|
291 | |
---|
292 | and you can start your session. An example session could be: |
---|
293 | </para> |
---|
294 | |
---|
295 | <para> |
---|
296 | Create an empty scene ("world" is default): |
---|
297 | |
---|
298 | <informalexample> |
---|
299 | <programlisting> |
---|
300 | Idle> /vis/scene/create |
---|
301 | </programlisting> |
---|
302 | </informalexample> |
---|
303 | |
---|
304 | Add a volume to the scene: |
---|
305 | |
---|
306 | <informalexample> |
---|
307 | <programlisting> |
---|
308 | Idle> /vis/scene/add/volume |
---|
309 | </programlisting> |
---|
310 | </informalexample> |
---|
311 | </para> |
---|
312 | |
---|
313 | <para> |
---|
314 | Create a scene handler for a specific graphics system. Change the |
---|
315 | next line to choose another graphic system: |
---|
316 | |
---|
317 | <informalexample> |
---|
318 | <programlisting> |
---|
319 | Idle> /vis/sceneHandler/create OGLIX |
---|
320 | </programlisting> |
---|
321 | </informalexample> |
---|
322 | |
---|
323 | Create a viewer: |
---|
324 | |
---|
325 | <informalexample> |
---|
326 | <programlisting> |
---|
327 | Idle> /vis/viewer/create |
---|
328 | </programlisting> |
---|
329 | </informalexample> |
---|
330 | |
---|
331 | Draw the scene, etc.: |
---|
332 | |
---|
333 | <informalexample> |
---|
334 | <programlisting> |
---|
335 | Idle> /vis/scene/notifyHandlers |
---|
336 | Idle> /run/verbose 0 |
---|
337 | Idle> /event/verbose 0 |
---|
338 | Idle> /tracking/verbose 1 |
---|
339 | Idle> /gun/particle mu+ |
---|
340 | Idle> /gun/energy 10 GeV |
---|
341 | Idle> /run/beamOn 1 |
---|
342 | Idle> /gun/particle proton |
---|
343 | Idle> /gun/energy 100 MeV |
---|
344 | Idle> /run/beamOn 3 |
---|
345 | Idle> exit |
---|
346 | </programlisting> |
---|
347 | </informalexample> |
---|
348 | </para> |
---|
349 | |
---|
350 | <para> |
---|
351 | For the meaning of the machine state <literal>Idle</literal>, see |
---|
352 | <xref linkend="sect.Run.StateMac" />. |
---|
353 | </para> |
---|
354 | |
---|
355 | <para> |
---|
356 | This mode is useful for running a few events in debug mode and |
---|
357 | visualizing them. Notice that the <emphasis>VisManager</emphasis> is created in |
---|
358 | the <literal>main()</literal>, and the visualization system is choosen via |
---|
359 | the 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> |
---|
373 | General Case |
---|
374 | </title> |
---|
375 | |
---|
376 | <para> |
---|
377 | Most of the examples in the <literal>$G4INSTALL/examples/</literal> directory |
---|
378 | have the following <literal>main()</literal>. |
---|
379 | The application can be run either in batch or interactive mode. |
---|
380 | |
---|
381 | <example id="programlist_HowToExec_5"> |
---|
382 | <title> |
---|
383 | The typical <literal>main()</literal> routine from the examples directory. |
---|
384 | </title> |
---|
385 | <programlisting> |
---|
386 | int 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->SetUserInitialization(detector); |
---|
394 | runManager->SetUserInitialization(new N03PhysicsList); |
---|
395 | |
---|
396 | #ifdef G4VIS_USE |
---|
397 | // visualization manager |
---|
398 | G4VisManager* visManager = new G4VisExecutive; |
---|
399 | visManager->Initialize(); |
---|
400 | #endif |
---|
401 | |
---|
402 | // set user action classes |
---|
403 | runManager->SetUserAction(new N03PrimaryGeneratorAction(detector)); |
---|
404 | runManager->SetUserAction(new N03RunAction); |
---|
405 | runManager->SetUserAction(new N03EventAction); |
---|
406 | runManager->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->ApplyCommand("/control/execute prerunN03.mac"); |
---|
415 | session->SessionStart(); |
---|
416 | delete session; |
---|
417 | } |
---|
418 | else // Batch mode |
---|
419 | { |
---|
420 | G4String command = "/control/execute "; |
---|
421 | G4String fileName = argv[1]; |
---|
422 | UI->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> |
---|
438 | Notice that the visualization system is under the control of the |
---|
439 | precompiler variable <literal>G4VIS_USE</literal>. Notice also that, in |
---|
440 | interactive mode, few intializations have been put in the macro |
---|
441 | <literal>prerunN03.mac</literal> which is executed before the session |
---|
442 | start. |
---|
443 | |
---|
444 | <example id="programlist_HowToExec_6"> |
---|
445 | <title> |
---|
446 | The <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 => storeTrajectory 0 |
---|
480 | /tracking/storeTrajectory 1 |
---|
481 | #/vis/scene/include/trajectories |
---|
482 | </programlisting> |
---|
483 | </example> |
---|
484 | </para> |
---|
485 | |
---|
486 | <para> |
---|
487 | Also, this example demonstrates that you can read and execute a |
---|
488 | macro interactively: |
---|
489 | |
---|
490 | <informalexample> |
---|
491 | <programlisting> |
---|
492 | Idle> /control/execute mySubMacro.mac |
---|
493 | </programlisting> |
---|
494 | </informalexample> |
---|
495 | </para> |
---|
496 | |
---|
497 | |
---|
498 | </sect2> |
---|
499 | </sect1> |
---|