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

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

ajout de la doc

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