[904] | 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> |
---|
| 15 | How to Define the main() Program |
---|
| 16 | </title> |
---|
| 17 | |
---|
| 18 | <!-- ******************* Section (Level#2) ****************** --> |
---|
| 19 | <sect2 id="sect.HowToDefMain.SimpleMainMethod"> |
---|
| 20 | <title> |
---|
| 21 | A Sample <literal>main()</literal> Method |
---|
| 22 | </title> |
---|
| 23 | |
---|
| 24 | <para> |
---|
| 25 | The contents of <literal>main()</literal> will vary according to the |
---|
| 26 | needs of a given simulation application and therefore must be supplied |
---|
| 27 | by the user. The Geant4 toolkit does not provide a <literal>main()</literal> |
---|
| 28 | method, but a sample is provided here as a guide to the beginning |
---|
| 29 | user. <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> |
---|
| 35 | Simplest 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->SetUserInitialization(new ExN01DetectorConstruction); |
---|
| 53 | runManager->SetUserInitialization(new ExN01PhysicsList); |
---|
| 54 | |
---|
| 55 | // set mandatory user action class |
---|
| 56 | runManager->SetUserAction(new ExN01PrimaryGeneratorAction); |
---|
| 57 | |
---|
| 58 | // initialize G4 kernel |
---|
| 59 | runManager->Initialize(); |
---|
| 60 | |
---|
| 61 | // get the pointer to the UI manager and set verbosities |
---|
| 62 | G4UImanager* UI = G4UImanager::GetUIpointer(); |
---|
| 63 | UI->ApplyCommand("/run/verbose 1"); |
---|
| 64 | UI->ApplyCommand("/event/verbose 1"); |
---|
| 65 | UI->ApplyCommand("/tracking/verbose 1"); |
---|
| 66 | |
---|
| 67 | // start a run |
---|
| 68 | int numberOfEvent = 3; |
---|
| 69 | runManager->BeamOn(numberOfEvent); |
---|
| 70 | |
---|
| 71 | // job termination |
---|
| 72 | delete runManager; |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
| 75 | </programlisting> |
---|
| 76 | </example> |
---|
| 77 | |
---|
| 78 | <para> |
---|
| 79 | The <literal>main()</literal> method is implemented by two toolkit |
---|
| 80 | classes, <emphasis>G4RunManager</emphasis> and <emphasis>G4UImanager</emphasis>, |
---|
| 81 | and three classes, <emphasis>ExN01DetectorConstruction</emphasis>, |
---|
| 82 | <emphasis>ExN01PhysicsList</emphasis> and |
---|
| 83 | <emphasis>ExN01PrimaryGeneratorAction</emphasis>, which are derived from |
---|
| 84 | toolkit 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> |
---|
| 96 | The first thing <literal>main()</literal> must do is create an instance of |
---|
| 97 | the <emphasis>G4RunManager</emphasis> class. This is the only manager class in |
---|
| 98 | the Geant4 kernel which should be explicitly constructed in the |
---|
| 99 | user's <literal>main()</literal>. It controls the flow of the program and |
---|
| 100 | manages the event loop(s) within a run. When <emphasis>G4RunManager</emphasis> is |
---|
| 101 | created, the other major manager classes are also created. They are |
---|
| 102 | deleted automatically when <emphasis>G4RunManager</emphasis> is deleted. The run |
---|
| 103 | manager is also responsible for managing initialization procedures, |
---|
| 104 | including methods in the user initialization classes. Through these |
---|
| 105 | the run manager must be given all the information necessary to |
---|
| 106 | build 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> |
---|
| 126 | In the sample <literal>main()</literal> the lines |
---|
| 127 | |
---|
| 128 | <informalexample> |
---|
| 129 | <programlisting> |
---|
| 130 | runManager->SetUserInitialization(new ExN01DetectorConstruction); |
---|
| 131 | runManager->SetUserInitialization(new ExN01PhysicsList); |
---|
| 132 | </programlisting> |
---|
| 133 | </informalexample> |
---|
| 134 | |
---|
| 135 | create objects which specify the detector geometry and physics |
---|
| 136 | processes, respectively, and pass their pointers to the run |
---|
| 137 | manager. <emphasis>ExN01DetectorConstruction</emphasis> is an example of a user |
---|
| 138 | initialization class which is derived from |
---|
| 139 | <emphasis>G4VUserDetectorConstruction</emphasis>. This is where the user |
---|
| 140 | describes 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> |
---|
| 159 | Similarly <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> |
---|
| 176 | The next instruction in <literal>main()</literal> |
---|
| 177 | |
---|
| 178 | <informalexample> |
---|
| 179 | <programlisting> |
---|
| 180 | runManager->SetUserAction(new ExN01PrimaryGeneratorAction); |
---|
| 181 | </programlisting> |
---|
| 182 | </informalexample> |
---|
| 183 | |
---|
| 184 | creates an instance of a particle generator and passes its pointer |
---|
| 185 | to the run manager. <emphasis>ExN01PrimaryGeneratorAction</emphasis> is an |
---|
| 186 | example of a user action class which is derived from |
---|
| 187 | <emphasis>G4VUserPrimaryGeneratorAction</emphasis>. In this class the user must |
---|
| 188 | describe the initial state of the primary event. This class has a |
---|
| 189 | public virtual method named <literal>generatePrimaries()</literal> which will |
---|
| 190 | be invoked at the beginning of each event. Details will be given in |
---|
| 191 | <xref linkend="sect.HowToGenEvent" />. |
---|
| 192 | Note that Geant4 does not provide any default behavior for generating a primary event. |
---|
| 193 | </para> |
---|
| 194 | |
---|
| 195 | <para> |
---|
| 196 | The next instruction |
---|
| 197 | |
---|
| 198 | <informalexample> |
---|
| 199 | <programlisting> |
---|
| 200 | runManager->Initialize(); |
---|
| 201 | </programlisting> |
---|
| 202 | </informalexample> |
---|
| 203 | |
---|
| 204 | performs the detector construction, creates the physics processes, |
---|
| 205 | calculates cross sections and otherwise sets up the run. The final |
---|
| 206 | run manager method in <literal>main()</literal> |
---|
| 207 | |
---|
| 208 | <informalexample> |
---|
| 209 | <programlisting> |
---|
| 210 | int numberOfEvent = 3; |
---|
| 211 | runManager->beamOn(numberOfEvent); |
---|
| 212 | </programlisting> |
---|
| 213 | </informalexample> |
---|
| 214 | |
---|
| 215 | begins 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. |
---|
| 218 | Once a run has begun neither the detector setup nor the physics |
---|
| 219 | processes may be changed. They may be changed between runs, |
---|
| 220 | however, as described in <xref linkend="sect.Run.Custom" />. |
---|
| 221 | More information on <emphasis>G4RunManager</emphasis> in general is found in |
---|
| 222 | <xref linkend="sect.Run" />. |
---|
| 223 | </para> |
---|
| 224 | |
---|
| 225 | <para> |
---|
| 226 | As mentioned above, other manager classes are created when the |
---|
| 227 | run manager is created. One of these is the user interface manager, |
---|
| 228 | <emphasis>G4UImanager</emphasis>. In <literal>main()</literal> a pointer to |
---|
| 229 | the interface manager must be obtained |
---|
| 230 | |
---|
| 231 | <informalexample> |
---|
| 232 | <programlisting> |
---|
| 233 | G4UImanager* UI = G4UImanager::getUIpointer(); |
---|
| 234 | </programlisting> |
---|
| 235 | </informalexample> |
---|
| 236 | |
---|
| 237 | in order for the user to issue commands to the program. In the |
---|
| 238 | present example the <literal>applyCommand()</literal> method is called three |
---|
| 239 | times to direct the program to print out information at the run, |
---|
| 240 | event and tracking levels of simulation. A wide range of commands |
---|
| 241 | is available which allows the user detailed control of the |
---|
| 242 | simulation. 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> |
---|
| 252 | User Initialization and Action Classes |
---|
| 253 | </title> |
---|
| 254 | |
---|
| 255 | <!-- ******************* Section (Level#3) ****************** --> |
---|
| 256 | <sect3 id="sect.HowToDefMain.UserInitAction.MandatoryUserClasses"> |
---|
| 257 | <title> |
---|
| 258 | Mandatory User Classes |
---|
| 259 | </title> |
---|
| 260 | |
---|
| 261 | <para> |
---|
| 262 | There are three classes which must be defined by the user. Two |
---|
| 263 | of them are user initialization classes, and the other is a user |
---|
| 264 | action class. They must be derived from the abstract base classes |
---|
| 265 | provided by Geant4: <emphasis>G4VUserDetectorConstruction</emphasis>, |
---|
| 266 | <emphasis>G4VuserPhysicsList</emphasis> and |
---|
| 267 | <emphasis>G4VuserPrimaryGeneratorAction</emphasis>. |
---|
| 268 | Geant4 does not provide default behavior for these classes. |
---|
| 269 | <emphasis>G4RunManager</emphasis> checks for the existence of these mandatory |
---|
| 270 | classes when the <literal>Initialize()</literal> and <literal>BeamOn()</literal> |
---|
| 271 | methods are invoked. |
---|
| 272 | </para> |
---|
| 273 | |
---|
| 274 | <para> |
---|
| 275 | As mentioned in the previous section, |
---|
| 276 | <emphasis>G4VUserDetectorConstruction</emphasis> requires the user to define the |
---|
| 277 | detector and <emphasis>G4VUserPhysicsList</emphasis> requires the user to define |
---|
| 278 | the physics. Detector definition will be discussed in Sections |
---|
| 279 | </para> |
---|
| 280 | |
---|
| 281 | <para> |
---|
| 282 | <xref linkend="sect.HowToDefDetectorGeom" /> and |
---|
| 283 | <xref linkend="sect.HowToSpecMate" />. |
---|
| 284 | Physics definition will be discussed in Sections |
---|
| 285 | <xref linkend="sect.HowToSpecParti" /> |
---|
| 286 | and |
---|
| 287 | <xref linkend="sect.HowToSpecPhysProc" />. |
---|
| 288 | The user action <emphasis>G4VuserPrimaryGeneratorAction</emphasis> |
---|
| 289 | requires that the initial event state be defined. Primary event generation will |
---|
| 290 | be 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> |
---|
| 299 | Optional User Action Classes |
---|
| 300 | </title> |
---|
| 301 | |
---|
| 302 | <para> |
---|
| 303 | Geant4 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> |
---|
| 325 | There are several virtual methods in each of these classes which |
---|
| 326 | allow the specification of additional procedures at all levels of |
---|
| 327 | the simulation application. Details of the user initialization and |
---|
| 328 | action 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> |
---|
| 342 | Geant4 provides a category named <emphasis role="bold">intercoms</emphasis>. |
---|
| 343 | <emphasis>G4UImanager</emphasis> is the manager class of this category. Using the |
---|
| 344 | functionalities of this category, you can invoke |
---|
| 345 | <emphasis role="bold">set</emphasis> methods |
---|
| 346 | of class objects of which you do not know the pointer. |
---|
| 347 | In <xref linkend="programlist_HowToDefMain_2" />, |
---|
| 348 | the verbosities of various Geant4 manager classes |
---|
| 349 | are set. Detailed mechanism description and usage of |
---|
| 350 | <emphasis role="bold">intercoms</emphasis> will be given in the next chapter, |
---|
| 351 | with a list of available commands. Command submission can be done all through the |
---|
| 352 | application. |
---|
| 353 | </para> |
---|
| 354 | |
---|
| 355 | <example id="programlist_HowToDefMain_2"> |
---|
| 356 | <title> |
---|
| 357 | An example of <literal>main()</literal> using interactive |
---|
| 358 | terminal and visualization. Code modified from the previous |
---|
| 359 | example 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->SetUserInitialization(detector); |
---|
| 384 | runManager->SetUserInitialization(new N02PhysicsList); |
---|
| 385 | |
---|
| 386 | // visualization manager |
---|
| 387 | G4VisManager* visManager = new G4VisExecutive; |
---|
| 388 | visManager->Initialize(); |
---|
| 389 | |
---|
| 390 | // set user action classes |
---|
| 391 | runManager->SetUserAction(new N02PrimaryGeneratorAction(detector)); |
---|
| 392 | runManager->SetUserAction(new N02RunAction); |
---|
| 393 | runManager->SetUserAction(new N02EventAction); |
---|
| 394 | runManager->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->ApplyCommand("/control/execute prerun.g4mac"); |
---|
| 405 | session->sessionStart(); |
---|
| 406 | delete session; |
---|
| 407 | } |
---|
| 408 | else |
---|
| 409 | // Batch mode |
---|
| 410 | { |
---|
| 411 | G4String command = "/control/execute "; |
---|
| 412 | G4String fileName = argv[1]; |
---|
| 413 | UI->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> |
---|
| 437 | Although not yet included in the above examples, output streams |
---|
| 438 | will be needed. <emphasis>G4cout</emphasis> and <emphasis>G4cerr</emphasis> |
---|
| 439 | are <emphasis role="bold">iostream</emphasis> |
---|
| 440 | objects defined by Geant4. The usage of these objects is exactly |
---|
| 441 | the same as the ordinary <emphasis>cout</emphasis> and |
---|
| 442 | <emphasis>cerr</emphasis>, |
---|
| 443 | except that the output streams will be handled by |
---|
| 444 | <emphasis>G4UImanager</emphasis>. |
---|
| 445 | Thus, output strings may be displayed on another window or stored in a |
---|
| 446 | file. Manipulation of these output streams will be described in |
---|
| 447 | <xref linkend="sect.UIDefNew.HowCont" />. |
---|
| 448 | These objects should be used instead of the ordinary |
---|
| 449 | <emphasis>cout</emphasis> and <emphasis>cerr</emphasis>. |
---|
| 450 | </para> |
---|
| 451 | |
---|
| 452 | |
---|
| 453 | </sect2> |
---|
| 454 | </sect1> |
---|