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