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> |
---|
17 | How to Execute a Program |
---|
18 | </title> |
---|
19 | |
---|
20 | |
---|
21 | <!-- ******************* Section (Level#2) ****************** --> |
---|
22 | <sect2 id="sect.HowToExec.Intro"> |
---|
23 | <title> |
---|
24 | Introduction |
---|
25 | </title> |
---|
26 | |
---|
27 | <para> |
---|
28 | A 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 | |
---|
45 | The last mode will be covered in <xref linkend="sect.HowToSetUpInter" />. |
---|
46 | The 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> |
---|
58 | Below is an example of the main program for an application which |
---|
59 | will run in batch mode. |
---|
60 | |
---|
61 | <example id="programlist_HowToExec_1"> |
---|
62 | <title> |
---|
63 | An example of the <literal>main()</literal> routine |
---|
64 | for an application which will run in batch mode. |
---|
65 | </title> |
---|
66 | <programlisting> |
---|
67 | int 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 | </programlisting> |
---|
91 | </example> |
---|
92 | </para> |
---|
93 | |
---|
94 | <para> |
---|
95 | Even the number of events in the run is `frozen`. To change this |
---|
96 | number 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> |
---|
105 | Batch Mode with Macro File |
---|
106 | </title> |
---|
107 | |
---|
108 | <para> |
---|
109 | Below is an example of the main program for an application which |
---|
110 | will run in batch mode, but reading a file of commands. |
---|
111 | |
---|
112 | <example id="programlist_HowToExec_2"> |
---|
113 | <title> |
---|
114 | An example of the <literal>main()</literal> routine |
---|
115 | for an application which will run in batch mode, but reading a file of commands. |
---|
116 | </title> |
---|
117 | <programlisting> |
---|
118 | int 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 | </programlisting> |
---|
143 | </example> |
---|
144 | </para> |
---|
145 | |
---|
146 | <para> |
---|
147 | This example will be executed with the command: |
---|
148 | |
---|
149 | <informalexample> |
---|
150 | <programlisting> |
---|
151 | > myProgram run1.mac |
---|
152 | </programlisting> |
---|
153 | </informalexample> |
---|
154 | |
---|
155 | where <literal>myProgram</literal> is the name of your executable and |
---|
156 | <literal>run1.mac</literal> is a macro of commands located in the current |
---|
157 | directory, which could look like: |
---|
158 | |
---|
159 | <example id="programlist_HowToExec_3"> |
---|
160 | <title> |
---|
161 | A 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> |
---|
184 | Indeed, you can re-execute your program with different run |
---|
185 | conditions without recompiling anything. |
---|
186 | </para> |
---|
187 | |
---|
188 | <para> |
---|
189 | <emphasis>Digression:</emphasis> |
---|
190 | many G4 category of classes have a verbose flag which controls |
---|
191 | the level of 'verbosity'. |
---|
192 | </para> |
---|
193 | |
---|
194 | <para> |
---|
195 | Usually <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> |
---|
218 | Interactive Mode Driven by Command Lines |
---|
219 | </title> |
---|
220 | |
---|
221 | <para> |
---|
222 | Below is an example of the main program for an application which |
---|
223 | will run interactively, waiting for command lines entered from the |
---|
224 | keyboard. |
---|
225 | |
---|
226 | <example id="programlist_HowToExec_4"> |
---|
227 | <title> |
---|
228 | An example of the <literal>main()</literal> routine for |
---|
229 | an application which will run interactively, waiting for commands from the |
---|
230 | keyboard. |
---|
231 | </title> |
---|
232 | <programlisting> |
---|
233 | int 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->SetUserInitialization(new MyDetectorConstruction); |
---|
240 | runManager->SetUserInitialization(new MyPhysicsList); |
---|
241 | |
---|
242 | // visualization manager |
---|
243 | G4VisManager* visManager = new G4VisExecutive; |
---|
244 | visManager->Initialize(); |
---|
245 | |
---|
246 | // set user action classes |
---|
247 | runManager->SetUserAction(new MyPrimaryGeneratorAction); |
---|
248 | runManager->SetUserAction(new MyRunAction); |
---|
249 | runManager->SetUserAction(new MyEventAction); |
---|
250 | runManager->SetUserAction(new MySteppingAction); |
---|
251 | |
---|
252 | // Initialize G4 kernel |
---|
253 | runManager->Initialize(); |
---|
254 | |
---|
255 | // Define UI terminal for interactive mode |
---|
256 | G4UIsession * session = new G4UIterminal; |
---|
257 | session->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> |
---|
271 | This example will be executed with the command: |
---|
272 | |
---|
273 | <informalexample> |
---|
274 | <programlisting> |
---|
275 | > myProgram |
---|
276 | </programlisting> |
---|
277 | </informalexample> |
---|
278 | |
---|
279 | where <literal>myProgram</literal> is the name of your executable. |
---|
280 | </para> |
---|
281 | |
---|
282 | <para> |
---|
283 | The G4 kernel will prompt: |
---|
284 | |
---|
285 | <informalexample> |
---|
286 | <programlisting> |
---|
287 | Idle> |
---|
288 | </programlisting> |
---|
289 | </informalexample> |
---|
290 | |
---|
291 | and you can start your session. An example session could be: |
---|
292 | </para> |
---|
293 | |
---|
294 | <para> |
---|
295 | Create an empty scene ("world" is default): |
---|
296 | |
---|
297 | <informalexample> |
---|
298 | <programlisting> |
---|
299 | Idle> /vis/scene/create |
---|
300 | </programlisting> |
---|
301 | </informalexample> |
---|
302 | |
---|
303 | Add a volume to the scene: |
---|
304 | |
---|
305 | <informalexample> |
---|
306 | <programlisting> |
---|
307 | Idle> /vis/scene/add/volume |
---|
308 | </programlisting> |
---|
309 | </informalexample> |
---|
310 | </para> |
---|
311 | |
---|
312 | <para> |
---|
313 | Create a scene handler for a specific graphics system. Change the |
---|
314 | next line to choose another graphic system: |
---|
315 | |
---|
316 | <informalexample> |
---|
317 | <programlisting> |
---|
318 | Idle> /vis/sceneHandler/create OGLIX |
---|
319 | </programlisting> |
---|
320 | </informalexample> |
---|
321 | |
---|
322 | Create a viewer: |
---|
323 | |
---|
324 | <informalexample> |
---|
325 | <programlisting> |
---|
326 | Idle> /vis/viewer/create |
---|
327 | </programlisting> |
---|
328 | </informalexample> |
---|
329 | |
---|
330 | Draw the scene, etc.: |
---|
331 | |
---|
332 | <informalexample> |
---|
333 | <programlisting> |
---|
334 | Idle> /vis/scene/notifyHandlers |
---|
335 | Idle> /run/verbose 0 |
---|
336 | Idle> /event/verbose 0 |
---|
337 | Idle> /tracking/verbose 1 |
---|
338 | Idle> /gun/particle mu+ |
---|
339 | Idle> /gun/energy 10 GeV |
---|
340 | Idle> /run/beamOn 1 |
---|
341 | Idle> /gun/particle proton |
---|
342 | Idle> /gun/energy 100 MeV |
---|
343 | Idle> /run/beamOn 3 |
---|
344 | Idle> exit |
---|
345 | </programlisting> |
---|
346 | </informalexample> |
---|
347 | </para> |
---|
348 | |
---|
349 | <para> |
---|
350 | For the meaning of the machine state <literal>Idle</literal>, see |
---|
351 | <xref linkend="sect.Run.StateMac" />. |
---|
352 | </para> |
---|
353 | |
---|
354 | <para> |
---|
355 | This mode is useful for running a few events in debug mode and |
---|
356 | visualizing them. Notice that the <emphasis>VisManager</emphasis> is created in |
---|
357 | the <literal>main()</literal>, and the visualization system is choosen via |
---|
358 | the 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> |
---|
372 | General Case |
---|
373 | </title> |
---|
374 | |
---|
375 | <para> |
---|
376 | Most of the examples in the <literal>$G4INSTALL/examples/</literal> directory |
---|
377 | have the following <literal>main()</literal>, which covers cases 2 and 3 |
---|
378 | above. Thus, the application can be run either in batch or |
---|
379 | 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 | G4UIsession * session = new G4UIterminal; |
---|
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> |
---|