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"> |
---|
2 | function 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. |
---|
9 | How 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. |
---|
11 | Getting 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. |
---|
13 | How 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. |
---|
15 | Generating Primary Events |
---|
16 | </h3></div></div></div><p> |
---|
17 | <span class="emphasis"><em>G4VuserPrimaryGeneratorAction</em></span> is one of the mandatory |
---|
18 | classes available for deriving your own concrete class. In your |
---|
19 | concrete class, you have to specify how a primary event should be |
---|
20 | generated. Actual generation of primary particles will be done by |
---|
21 | concrete classes of <span class="emphasis"><em>G4VPrimaryGenerator</em></span>, explained in the |
---|
22 | following sub-section. Your <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> |
---|
23 | concrete 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. |
---|
26 | An example of a <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> |
---|
27 | concrete class using <span class="emphasis"><em>G4ParticleGun</em></span>. |
---|
28 | For the usage of <span class="emphasis"><em>G4ParticleGun</em></span> refer to the |
---|
29 | next 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 | |
---|
36 | class G4ParticleGun; |
---|
37 | class G4Event; |
---|
38 | |
---|
39 | class 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 | |
---|
61 | ExN01PrimaryGeneratorAction::ExN01PrimaryGeneratorAction() |
---|
62 | { |
---|
63 | G4int n_particle = 1; |
---|
64 | particleGun = new G4ParticleGun(n_particle); |
---|
65 | |
---|
66 | particleGun->SetParticleDefinition(G4Geantino::GeantinoDefinition()); |
---|
67 | particleGun->SetParticleEnergy(1.0*GeV); |
---|
68 | particleGun->SetParticlePosition(G4ThreeVector(-2.0*m,0.0*m,0.0*m)); |
---|
69 | } |
---|
70 | |
---|
71 | ExN01PrimaryGeneratorAction::~ExN01PrimaryGeneratorAction() |
---|
72 | { |
---|
73 | delete particleGun; |
---|
74 | } |
---|
75 | |
---|
76 | void ExN01PrimaryGeneratorAction::generatePrimaries(G4Event* anEvent) |
---|
77 | { |
---|
78 | G4int i = anEvent->get_eventID() % 3; |
---|
79 | switch(i) |
---|
80 | { |
---|
81 | case 0: |
---|
82 | particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.0)); |
---|
83 | break; |
---|
84 | case 1: |
---|
85 | particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.1,0.0)); |
---|
86 | break; |
---|
87 | case 2: |
---|
88 | particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.1)); |
---|
89 | break; |
---|
90 | } |
---|
91 | |
---|
92 | particleGun->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. |
---|
97 | Selection of the generator |
---|
98 | </h4></div></div></div><p> |
---|
99 | In the constructor of your <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span>, |
---|
100 | you should instantiate the primary generator(s). If necessary, you |
---|
101 | need to set some initial conditions for the generator(s). |
---|
102 | </p><p> |
---|
103 | In <a href="ch02s06.html#programlist_HowToGenEvent_1" title="Example 2.19. |
---|
104 | An example of a G4VUserPrimaryGeneratorAction |
---|
105 | concrete class using G4ParticleGun. |
---|
106 | For the usage of G4ParticleGun refer to the |
---|
107 | next subsection. |
---|
108 | ">Example 2.19</a>, |
---|
109 | <span class="emphasis"><em>G4ParticleGun</em></span> is constructed to |
---|
110 | use as the actual primary particle generator. Methods of |
---|
111 | <span class="emphasis"><em>G4ParticleGun</em></span> are described in the following section. Please |
---|
112 | note that the primary generator object(s) you construct in your |
---|
113 | <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> concrete class must be deleted |
---|
114 | in 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. |
---|
116 | Generation of an event |
---|
117 | </h4></div></div></div><p> |
---|
118 | <span class="emphasis"><em>G4VUserPrimaryGeneratorAction</em></span> has a pure virtual method |
---|
119 | named <code class="literal">generatePrimaries()</code>. This method is invoked at the |
---|
120 | beginning 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> |
---|
124 | You can invoke more than one generator and/or invoke one |
---|
125 | generator more than once. Mixing up several generators can produce |
---|
126 | a 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. |
---|
128 | G4VPrimaryGenerator |
---|
129 | </h3></div></div></div><p> |
---|
130 | Geant4 provides two <span class="emphasis"><em>G4VPrimaryGenerator</em></span> concrete classes. |
---|
131 | One is <span class="emphasis"><em>G4ParticleGun</em></span>, which will be discussed here, and the |
---|
132 | other is <span class="emphasis"><em>G4HEPEvtInterface</em></span>, which will be discussed in |
---|
133 | <a href="ch03s06.html" title="3.6. |
---|
134 | Event 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. |
---|
137 | G4ParticleGun |
---|
138 | </h4></div></div></div><p> |
---|
139 | <span class="emphasis"><em>G4ParticleGun</em></span> is a generator provided by Geant4. This class |
---|
140 | generates primary particle(s) with a given momentum and position. |
---|
141 | It 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 |
---|
143 | of one or more primaries of exactly same kinematics. It is a rather |
---|
144 | frequent user requirement to generate a primary with randomized |
---|
145 | energy, momentum, and/or position. Such randomization can be |
---|
146 | achieved by invoking various set methods provided by |
---|
147 | <span class="emphasis"><em>G4ParticleGun</em></span>. The invocation of these methods should be |
---|
148 | implemented in the <code class="literal">generatePrimaries()</code> method of your |
---|
149 | concrete <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>. |
---|
152 | Geant4 provides various random number generation methods with various |
---|
153 | distributions (see <a href="ch03s02.html" title="3.2. |
---|
154 | Global 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. |
---|
157 | Public methods of <span class="emphasis"><em>G4ParticleGun</em></span> |
---|
158 | </h4></div></div></div><p> |
---|
159 | The following methods are provided by <span class="emphasis"><em>G4ParticleGun</em></span>, and all |
---|
160 | of them can be invoked from the <code class="literal">generatePrimaries()</code> method |
---|
161 | in 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. |
---|
181 | How 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. |
---|
183 | How to Make an Executable Program |
---|
184 | </td></tr></table></div></body></html> |
---|