source: trunk/Documentation/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch02s06.html @ 901

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

Add Geant4 Documentation at 8.12.2008

File size: 10.2 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>2.6.  How to Generate a Primary Event</title><link rel="stylesheet" href="../xml/XSLCustomizationLayer/G4HTMLStylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="Geant4 User's Guide for Application Developers"><link rel="up" href="ch02.html" title="Chapter 2.  Getting Started with Geant4 - Running a Simple Example"><link rel="prev" href="ch02s05.html" title="2.5.  How to Specify Physics Processes"><link rel="next" href="ch02s07.html" title="2.7.  How to Make an Executable Program"><script language="JavaScript">
2function remote_win(fName)
3{
4   var url = "AllResources/Detector/geometry.src/" + fName;
5   RemoteWin=window.open(url,"","resizable=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,width=520,height=520")
6   RemoteWin.creator=self
7}
8</script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.6. 
9How to Generate a Primary Event
10</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s05.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><th width="60%" align="center">Chapter 2. 
11Getting Started with Geant4 - Running a Simple Example
12</th><td width="20%" align="right"> <a accesskey="n" href="ch02s07.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sect.HowToGenEvent"></a>2.6. 
13How to Generate a Primary Event
14</h2></div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToGenEvent.GenPrimary"></a>2.6.1. 
15Generating Primary Events
16</h3></div></div></div><p>
17<span class="emphasis"><em>G4VuserPrimaryGeneratorAction</em></span> is one of the mandatory
18classes available for deriving your own concrete class. In your
19concrete class, you have to specify how a primary event should be
20generated. Actual generation of primary particles will be done by
21concrete classes of <span class="emphasis"><em>G4VPrimaryGenerator</em></span>, explained in the
22following sub-section. Your <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span>
23concrete class just arranges the way primary particles are generated.
24
25</p><div class="example"><a name="programlist_HowToGenEvent_1"></a><p class="title"><b>Example 2.19. 
26An example of a <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> 
27concrete class using <span class="emphasis"><em>G4ParticleGun</em></span>.
28For the usage of <span class="emphasis"><em>G4ParticleGun</em></span> refer to the
29next subsection.
30</b></p><div class="example-contents"><pre class="programlisting">
31#ifndef ExN01PrimaryGeneratorAction_h
32#define ExN01PrimaryGeneratorAction_h 1
33
34#include "G4VUserPrimaryGeneratorAction.hh"
35
36class G4ParticleGun;
37class G4Event;
38
39class ExN01PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
40{
41  public:
42    ExN01PrimaryGeneratorAction();
43    ~ExN01PrimaryGeneratorAction();
44
45  public:
46    void generatePrimaries(G4Event* anEvent);
47
48  private:
49    G4ParticleGun* particleGun;
50};
51
52#endif
53
54#include "ExN01PrimaryGeneratorAction.hh"
55#include "G4Event.hh"
56#include "G4ParticleGun.hh"
57#include "G4ThreeVector.hh"
58#include "G4Geantino.hh"
59#include "globals.hh"
60
61ExN01PrimaryGeneratorAction::ExN01PrimaryGeneratorAction()
62{
63  G4int n_particle = 1;
64  particleGun = new G4ParticleGun(n_particle);
65
66  particleGun-&gt;SetParticleDefinition(G4Geantino::GeantinoDefinition());
67  particleGun-&gt;SetParticleEnergy(1.0*GeV);
68  particleGun-&gt;SetParticlePosition(G4ThreeVector(-2.0*m,0.0*m,0.0*m));
69}
70
71ExN01PrimaryGeneratorAction::~ExN01PrimaryGeneratorAction()
72{
73  delete particleGun;
74}
75
76void ExN01PrimaryGeneratorAction::generatePrimaries(G4Event* anEvent)
77{
78  G4int i = anEvent-&gt;get_eventID() % 3;
79  switch(i)
80  {
81    case 0:
82      particleGun-&gt;SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.0));
83      break;
84    case 1:
85      particleGun-&gt;SetParticleMomentumDirection(G4ThreeVector(1.0,0.1,0.0));
86      break;
87    case 2:
88      particleGun-&gt;SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.1));
89      break;
90  }
91
92  particleGun-&gt;generatePrimaryVertex(anEvent);
93}
94</pre></div></div><p><br class="example-break">
95
96</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="sect.HowToGenEvent.GenPrimary.SelectGenerator"></a>2.6.1.1. 
97Selection of the generator
98</h4></div></div></div><p>
99In the constructor of your <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span>,
100you should instantiate the primary generator(s). If necessary, you
101need to set some initial conditions for the generator(s).
102</p><p>
103In <a href="ch02s06.html#programlist_HowToGenEvent_1" title="Example 2.19. 
104An example of a G4VUserPrimaryGeneratorAction
105concrete class using G4ParticleGun.
106For the usage of G4ParticleGun refer to the
107next subsection.
108">Example 2.19</a>,
109<span class="emphasis"><em>G4ParticleGun</em></span> is constructed to
110use as the actual primary particle generator. Methods of
111<span class="emphasis"><em>G4ParticleGun</em></span> are described in the following section. Please
112note that the primary generator object(s) you construct in your
113<span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> concrete class must be deleted
114in your destructor.
115</p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="sect.HowToGenEvent.GenPrimary.GenEvent"></a>2.6.1.2. 
116Generation of an event
117</h4></div></div></div><p>
118<span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> has a pure virtual method
119named <code class="literal">generatePrimaries()</code>. This method is invoked at the
120beginning of each event. In this method, you have to invoke the
121<span class="emphasis"><em>G4VPrimaryGenerator</em></span> concrete class you instantiated via the
122<code class="literal">generatePrimaryVertex()</code> method.
123</p><p>
124You can invoke more than one generator and/or invoke one
125generator more than once. Mixing up several generators can produce
126a more complicated primary event.
127</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.HowToGenEvent.G4VPrimGen"></a>2.6.2. 
128G4VPrimaryGenerator
129</h3></div></div></div><p>
130Geant4 provides two <span class="emphasis"><em>G4VPrimaryGenerator</em></span> concrete classes.
131One is <span class="emphasis"><em>G4ParticleGun</em></span>, which will be discussed here, and the
132other is <span class="emphasis"><em>G4HEPEvtInterface</em></span>, which will be discussed in
133<a href="ch03s06.html" title="3.6. 
134Event Generator Interface
135">Section 3.6</a>.
136</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="sect.HowToGenEvent.G4VPrimGen.G4PartGun"></a>2.6.2.1. 
137G4ParticleGun
138</h4></div></div></div><p>
139<span class="emphasis"><em>G4ParticleGun</em></span> is a generator provided by Geant4. This class
140generates primary particle(s) with a given momentum and position.
141It does not provide any sort of randomizing. The constructor of
142<span class="emphasis"><em>G4ParticleGun</em></span> takes an integer which causes the generation
143of one or more primaries of exactly same kinematics. It is a rather
144frequent user requirement to generate a primary with randomized
145energy, momentum, and/or position. Such randomization can be
146achieved by invoking various set methods provided by
147<span class="emphasis"><em>G4ParticleGun</em></span>. The invocation of these methods should be
148implemented in the <code class="literal">generatePrimaries()</code> method of your
149concrete <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> class before invoking
150<code class="literal">generatePrimaryVertex()</code> of
151<span class="emphasis"><em>G4ParticleGun</em></span>.
152Geant4 provides various random number generation methods with various
153distributions (see <a href="ch03s02.html" title="3.2. 
154Global Usage Classes
155">Section 3.2</a>).
156</p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="sect.HowToGenEvent.G4VPrimGen.PublicMethG4PartGun"></a>2.6.2.2. 
157Public methods of <span class="emphasis"><em>G4ParticleGun</em></span>
158</h4></div></div></div><p>
159The following methods are provided by <span class="emphasis"><em>G4ParticleGun</em></span>, and all
160of them can be invoked from the <code class="literal">generatePrimaries()</code> method
161in your concrete <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> class.
162
163</p><div class="itemizedlist"><ul type="disc" compact><li><p>
164  <code class="literal">void SetParticleDefinition(G4ParticleDefinition*)</code>
165  </p></li><li><p>
166  <code class="literal">void SetParticleMomentum(G4ParticleMomentum)</code>
167  </p></li><li><p>
168  <code class="literal">void SetParticleMomentumDirection(G4ThreeVector)</code>
169  </p></li><li><p>
170  <code class="literal">void SetParticleEnergy(G4double)</code>
171  </p></li><li><p>
172  <code class="literal">void SetParticleTime(G4double)</code>
173  </p></li><li><p>
174  <code class="literal">void SetParticlePosition(G4ThreeVector)</code>
175  </p></li><li><p>
176  <code class="literal">void SetParticlePolarization(G4ThreeVector)</code>
177  </p></li><li><p>
178  <code class="literal">void SetNumberOfParticles(G4int)</code>
179  </p></li></ul></div><p>
180</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s05.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="ch02.html"><img src="AllResources/IconsGIF/up.gif" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s07.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">2.5. 
181How to Specify Physics Processes
182 </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="AllResources/IconsGIF/home.gif" alt="Home"></a></td><td width="40%" align="right" valign="top"> 2.7. 
183How to Make an Executable Program
184</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.