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

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

CVS update

File size: 14.6 KB
Line 
1<!-- ******************************************************** -->
2<!--                                                          -->
3<!--  [History]                                               -->
4<!--    Changed by: Katsuya Amako,  4-Aug-1998                -->
5<!--    Changed by: Dennis Wright, 29-Nov-2001                -->
6<!--    Proof read by: Joe Chuma,  14-Jun-1999                -->
7<!--    Converted to DocBook: Katsuya Amako, Aug-2006         -->
8<!--    Updated by Koichi Murakami, Dec-2009                  -->
9<!--                                                          -->
10<!-- ******************************************************** -->
11
12
13<!-- ******************* Section (Level#1) ****************** -->
14<sect1 id="sect.HowToDefMain">
15<title>
16How to Define the main() Program
17</title>
18
19<!-- ******************* Section (Level#2) ****************** -->
20<sect2 id="sect.HowToDefMain.SimpleMainMethod">
21<title>
22A Sample <literal>main()</literal> Method
23</title>
24
25<para>
26The contents of <literal>main()</literal> will vary according to the
27needs of a given simulation application and therefore must be supplied
28by the user. The Geant4 toolkit does not provide a <literal>main()</literal>
29method, but a sample is provided here as a guide to the beginning
30user. <xref linkend="programlist_HowToDefMain_1" /> is the  simplest example of
31<literal>main()</literal> required to build a simulation program.
32</para>
33
34<example id="programlist_HowToDefMain_1">
35<title>
36Simplest example of <literal>main()</literal> 
37</title>
38
39<programlisting>
40 #include "G4RunManager.hh"
41 #include "G4UImanager.hh"
42
43 #include "ExN01DetectorConstruction.hh"
44 #include "ExN01PhysicsList.hh"
45 #include "ExN01PrimaryGeneratorAction.hh"
46
47 int main()
48 {
49   // construct the default run manager
50   G4RunManager* runManager = new G4RunManager;
51
52   // set mandatory initialization classes
53   runManager-&gt;SetUserInitialization(new ExN01DetectorConstruction);
54   runManager-&gt;SetUserInitialization(new ExN01PhysicsList);
55
56   // set mandatory user action class
57   runManager-&gt;SetUserAction(new ExN01PrimaryGeneratorAction);
58
59   // initialize G4 kernel
60   runManager-&gt;Initialize();
61
62   // get the pointer to the UI manager and set verbosities
63   G4UImanager* UI = G4UImanager::GetUIpointer();
64   UI-&gt;ApplyCommand("/run/verbose 1");
65   UI-&gt;ApplyCommand("/event/verbose 1");
66   UI-&gt;ApplyCommand("/tracking/verbose 1");
67
68   // start a run
69   int numberOfEvent = 3;
70   runManager-&gt;BeamOn(numberOfEvent);
71
72   // job termination
73   delete runManager;
74   return 0;
75 }
76</programlisting>
77</example>
78
79<para>
80The <literal>main()</literal> method is implemented by two toolkit
81classes, <emphasis>G4RunManager</emphasis> and <emphasis>G4UImanager</emphasis>,
82and three classes, <emphasis>ExN01DetectorConstruction</emphasis>,
83<emphasis>ExN01PhysicsList</emphasis> and
84<emphasis>ExN01PrimaryGeneratorAction</emphasis>, which are derived from
85toolkit classes. Each of these are explained in the following sections.
86</para>
87
88</sect2>
89
90<!-- ******************* Section (Level#2) ****************** -->
91<sect2 id="sect.HowToDefMain.G4RunManager">
92<title>
93<emphasis>G4RunManager</emphasis>
94</title>
95
96<para>
97The first thing <literal>main()</literal> must do is create an instance of
98the <emphasis>G4RunManager</emphasis> class. This is the only manager class in
99the Geant4 kernel which should be explicitly constructed in the
100user's <literal>main()</literal>. It controls the flow of the program and
101manages the event loop(s) within a run. When <emphasis>G4RunManager</emphasis> is
102created, the other major manager classes are also created. They are
103deleted automatically when <emphasis>G4RunManager</emphasis> is deleted. The run
104manager is also responsible for managing initialization procedures,
105including methods in the user initialization classes. Through these
106the run manager must be given all the information necessary to
107build and run the simulation, including
108</para>
109
110<orderedlist spacing="compact">
111  <listitem><para>
112  how the detector should be constructed,
113  </para></listitem>
114  <listitem><para>
115  all the particles and all the physics processes to be
116  simulated,
117  </para></listitem>
118  <listitem><para>
119  how the primary particle(s) in an event should be produced
120  and
121  </para></listitem>
122  <listitem><para>
123  any additional requirements of the simulation.</para></listitem>
124</orderedlist>
125
126<para>
127In the sample <literal>main()</literal> the lines
128
129<informalexample>
130<programlisting>
131  runManager-&gt;SetUserInitialization(new ExN01DetectorConstruction);
132  runManager-&gt;SetUserInitialization(new ExN01PhysicsList);
133</programlisting>
134</informalexample>
135
136create objects which specify the detector geometry and physics
137processes, respectively, and pass their pointers to the run
138manager. <emphasis>ExN01DetectorConstruction</emphasis> is an example of a user
139initialization class which is derived from
140<emphasis>G4VUserDetectorConstruction</emphasis>. This is where the user
141describes the entire detector setup, including
142</para>
143
144<itemizedlist spacing="compact">
145  <listitem><para>
146  its geometry,
147  </para></listitem>
148  <listitem><para>
149  the materials used in its construction,
150  </para></listitem>
151  <listitem><para>
152  a definition of its sensitive regions and
153  </para></listitem>
154  <listitem><para>
155  the readout schemes of the sensitive regions.
156  </para></listitem>
157</itemizedlist>
158
159<para>
160Similarly <emphasis>ExN01PhysicsList</emphasis> is derived from
161<emphasis>G4VUserPhysicsList</emphasis> and requires the user to define
162</para>
163
164<itemizedlist spacing="compact">
165  <listitem><para>
166  the particles to be used in the simulation,
167  </para></listitem>
168  <listitem><para>
169  the range cuts for these particles and
170  </para></listitem>
171  <listitem><para>
172  all the physics processes to be simulated.
173  </para></listitem>
174</itemizedlist>
175
176<para>
177The next instruction in <literal>main()</literal>
178
179<informalexample>
180<programlisting>
181  runManager-&gt;SetUserAction(new ExN01PrimaryGeneratorAction);
182</programlisting>
183</informalexample>
184
185creates an instance of a particle generator and passes its pointer
186to the run manager. <emphasis>ExN01PrimaryGeneratorAction</emphasis> is an
187example of a user action class which is derived from
188<emphasis>G4VUserPrimaryGeneratorAction</emphasis>. In this class the user must
189describe the initial state of the primary event. This class has a
190public virtual method named <literal>generatePrimaries()</literal> which will
191be invoked at the beginning of each event. Details will be given in
192<xref linkend="sect.HowToGenEvent" />.
193Note that Geant4 does not provide any default behavior for generating a primary event.
194</para>
195
196<para>
197The next instruction
198
199<informalexample>
200<programlisting>
201  runManager-&gt;Initialize();
202</programlisting>
203</informalexample>
204
205performs the detector construction, creates the physics processes,
206calculates cross sections and otherwise sets up the run. The final
207run manager method in <literal>main()</literal>
208
209<informalexample>
210<programlisting>
211  int numberOfEvent = 3;
212  runManager-&gt;beamOn(numberOfEvent);
213</programlisting>
214</informalexample>
215
216begins a run of three sequentially processed events. The
217<literal>beamOn()</literal> method may be invoked any number of times within
218<literal>main()</literal> with each invocation representing a separate run.
219Once a run has begun neither the detector setup nor the physics
220processes may be changed. They may be changed between runs,
221however, as described in <xref linkend="sect.Run.Custom" />.
222More information on <emphasis>G4RunManager</emphasis> in general is found in
223<xref linkend="sect.Run" />.
224</para>
225
226<para>
227As mentioned above, other manager classes are created when the
228run manager is created. One of these is the user interface manager,
229<emphasis>G4UImanager</emphasis>. In <literal>main()</literal> a pointer to
230the interface manager must be obtained
231
232<informalexample>
233<programlisting>
234  G4UImanager* UI = G4UImanager::getUIpointer();
235</programlisting>
236</informalexample> 
237
238in order for the user to issue commands to the program. In the
239present example the <literal>applyCommand()</literal> method is called three
240times to direct the program to print out information at the run,
241event and tracking levels of simulation. A wide range of commands
242is available which allows the user detailed control of the
243simulation. A list of these commands can be found in
244<xref linkend="sect.BuiltinCom" />.
245</para>
246
247</sect2>
248
249
250<!-- ******************* Section (Level#2) ****************** -->
251<sect2 id="sect.HowToDefMain.UserInitAction">
252<title>
253User Initialization and Action Classes
254</title>
255
256<!-- ******************* Section (Level#3) ****************** -->
257<sect3 id="sect.HowToDefMain.UserInitAction.MandatoryUserClasses">
258<title>
259Mandatory User Classes
260</title>
261
262<para>
263There are three classes which must be defined by the user. Two
264of them are user initialization classes, and the other is a user
265action class. They must be derived from the abstract base classes
266provided by Geant4: <emphasis>G4VUserDetectorConstruction</emphasis>,
267<emphasis>G4VuserPhysicsList</emphasis> and
268<emphasis>G4VuserPrimaryGeneratorAction</emphasis>.
269Geant4 does not provide default behavior for these classes.
270<emphasis>G4RunManager</emphasis> checks for the existence of these mandatory
271classes when the <literal>Initialize()</literal> and <literal>BeamOn()</literal>
272methods are invoked.
273</para>
274
275<para>
276As mentioned in the previous section,
277<emphasis>G4VUserDetectorConstruction</emphasis> requires the user to define the
278detector and <emphasis>G4VUserPhysicsList</emphasis> requires the user to define
279the physics. Detector definition will be discussed in Sections
280</para>
281
282<para>
283<xref linkend="sect.HowToDefDetectorGeom" /> and
284<xref linkend="sect.HowToSpecMate" />.
285Physics definition will be discussed in Sections
286<xref linkend="sect.HowToSpecParti" /> 
287and
288<xref linkend="sect.HowToSpecPhysProc" />.
289The user action  <emphasis>G4VuserPrimaryGeneratorAction</emphasis> 
290requires that the initial event state be defined. Primary event generation will
291be discussed in
292<xref linkend="sect.HowToMakeExec" />.
293</para>
294
295</sect3>
296
297<!-- ******************* Section (Level#3) ****************** -->
298<sect3 id="sect.HowToDefMain.UserInitAction.OptionalUserAction">
299<title>
300Optional User Action Classes
301</title>
302
303<para>
304Geant4 provides five user hook classes:
305</para>
306
307<itemizedlist spacing="compact">
308  <listitem><para>
309  <emphasis>G4UserRunAction</emphasis>
310  </para></listitem>
311  <listitem><para>
312  <emphasis>G4UserEventAction</emphasis>
313  </para></listitem>
314  <listitem><para>
315  <emphasis>G4UserStackingAction</emphasis>
316  </para></listitem>
317  <listitem><para>
318  <emphasis>G4UserTrackingAction</emphasis>
319  </para></listitem>
320  <listitem><para>
321  <emphasis>G4UserSteppingAction</emphasis>
322  </para></listitem>
323</itemizedlist>
324
325<para>
326There are several virtual methods in each of these classes which
327allow the specification of additional procedures at all levels of
328the simulation application. Details of the user initialization and
329action classes are provided in
330<xref linkend="chap.UserActions" />.
331</para>
332
333</sect3>
334</sect2>
335
336<!-- ******************* Section (Level#2) ****************** -->
337<sect2 id="sect.HowToDefMain.G4UImanagerUICommand">
338<title>
339<emphasis>G4UImanager</emphasis> and UI CommandSubmission
340</title>
341
342<para>
343Geant4 provides a category named <emphasis role="bold">intercoms</emphasis>.
344<emphasis>G4UImanager</emphasis> is the manager class of this category. Using the
345functionalities of this category, you can invoke
346<emphasis role="bold">set</emphasis> methods
347of class objects of which you do not know the pointer.
348In <xref linkend="programlist_HowToDefMain_2" />,
349the verbosities of various Geant4 manager classes
350are set. Detailed mechanism description and usage of
351<emphasis role="bold">intercoms</emphasis> will be given in the next chapter,
352with a list of available commands. Command submission can be done all through the
353application.
354</para>
355
356<example id="programlist_HowToDefMain_2">
357<title>
358An example of <literal>main()</literal> using interactive
359terminal and visualization. Code modified from the previous
360example are shown in  <emphasis role="color_blue">blue</emphasis>.
361</title>
362<programlisting>
363 #include "G4RunManager.hh"
364 #include "G4UImanager.hh"
365 <emphasis role="color_blue">#include "G4UIExecutive.hh"</emphasis>
366 #include "G4VisExecutive.hh"
367
368 #include "N02DetectorConstruction.hh"
369 #include "N02PhysicsList.hh"
370 #include "N02PrimaryGeneratorAction.hh"
371 #include "N02RunAction.hh"
372 #include "N02EventAction.hh"
373 #include "N02SteppingAction.hh"
374
375 #include "g4templates.hh"
376
377 int main(int argc,char** argv)
378 {
379   // construct the default run manager
380   G4RunManager * runManager = new G4RunManager;
381
382   // set mandatory initialization classes
383   N02DetectorConstruction* detector = new N02DetectorConstruction;
384   runManager-&gt;SetUserInitialization(detector);
385   runManager-&gt;SetUserInitialization(new N02PhysicsList);
386 
387   // visualization manager
388   G4VisManager* visManager = new G4VisExecutive;
389   visManager-&gt;Initialize();
390   
391   // set user action classes
392   runManager-&gt;SetUserAction(new N02PrimaryGeneratorAction(detector));
393   runManager-&gt;SetUserAction(new N02RunAction);
394   runManager-&gt;SetUserAction(new N02EventAction);
395   runManager-&gt;SetUserAction(new N02SteppingAction);
396   
397   // get the pointer to the User Interface manager
398   G4UImanager* UImanager = G4UImanager::GetUIpointer();
399
400 <emphasis role="color_blue">
401   if(argc==1)
402   // Define (G)UI terminal for interactive mode
403   {
404     G4UIExecutive * ui = new G4UIExecutive(argc,argv);
405     UImanager-&gt;ApplyCommand("/control/execute prerun.g4mac");
406     ui-&gt;sessionStart();
407     delete ui;
408   }
409   else
410   // Batch mode
411   {
412     G4String command = "/control/execute ";
413     G4String fileName = argv[1];
414     UImanager-&gt;ApplyCommand(command+fileName);
415   }
416</emphasis>
417
418   // job termination
419   delete visManager;
420   delete runManager;
421
422   return 0;
423 }
424</programlisting>
425</example>
426
427
428</sect2>
429
430
431<!-- ******************* Section (Level#2) ****************** -->
432<sect2 id="sect.HowToDefMain.G4coutG4cerr">
433<title>
434<emphasis>G4cout</emphasis> and <emphasis>G4cerr</emphasis>
435</title>
436
437<para>
438Although not yet included in the above examples, output streams
439will be needed. <emphasis>G4cout</emphasis> and <emphasis>G4cerr</emphasis> 
440are <emphasis role="bold">iostream</emphasis>
441objects defined by Geant4. The usage of these objects is exactly
442the same as the ordinary <emphasis>cout</emphasis> and
443<emphasis>cerr</emphasis>,
444except that the output streams will be handled by
445<emphasis>G4UImanager</emphasis>.
446Thus, output strings may be displayed on another window or stored in a
447file. Manipulation of these output streams will be described in
448<xref linkend="sect.UIDefNew.HowCont" />.
449These objects should be used instead of the ordinary
450<emphasis>cout</emphasis> and <emphasis>cerr</emphasis>.
451</para>
452
453
454</sect2>
455</sect1>
Note: See TracBrowser for help on using the repository browser.