Geant4  10.02
PrimaryGeneratorAction2.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 //
30 // $Id: PrimaryGeneratorAction2.cc 68024 2013-03-13 13:42:01Z gcosmo $
31 //
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
34 
36 #include "PrimaryGeneratorAction.hh"
37 
38 #include "G4Event.hh"
39 #include "G4ParticleGun.hh"
40 #include "G4ParticleTable.hh"
41 #include "G4ParticleDefinition.hh"
42 #include "G4PhysicalConstants.hh"
43 #include "G4SystemOfUnits.hh"
44 #include "Randomize.hh"
45 
46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
47 
49 : particleGun(gun)
50 {
51  // energy distribution
52  //
53  InitFunction();
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 { }
60 
61 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
62 
64 {
65  //cosAlpha uniform in [cos(0), cos(pi)]
66  G4double cosAlpha = 1. - 2*G4UniformRand();
67  G4double sinAlpha = std::sqrt(1. - cosAlpha*cosAlpha);
68  G4double psi = twopi*G4UniformRand(); //psi uniform in [0, 2*pi]
69  G4ThreeVector dir(sinAlpha*std::cos(psi),sinAlpha*std::sin(psi),cosAlpha);
70 
72 
73  //set energy from a tabulated distribution
74  //
75  //G4double energy = RejectAccept();
78 
79  //create vertex
80  //
82 }
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
86 {
87  // tabulated function
88  // f is assumed positive, linear per segment, continuous
89  //
90  nPoints = 16;
91  const G4double xx[] =
92  { 37*keV, 39*keV, 45*keV, 51*keV, 57*keV, 69*keV, 71*keV, 75*keV,
93  83*keV, 91*keV, 97*keV, 107*keV, 125*keV, 145*keV, 159*keV, 160*keV };
94 
95  const G4double ff[] =
96  { 0.000, 0.077, 0.380, 2.044, 5.535, 15.077, 12.443, 14.766,
97  17.644, 18.518, 17.772, 14.776, 8.372, 3.217, 0.194, 0.000 };
98 
99  //copy arrays in std::vector and compute fMax
100  //
101  x.resize(nPoints); f.resize(nPoints);
102  fMax = 0.;
103  for (G4int j=0; j<nPoints; j++) {
104  x[j] = xx[j]; f[j] = ff[j];
105  if (fMax < f[j]) fMax = f[j];
106  };
107 
108  //compute slopes
109  //
110  a.resize(nPoints);
111  for (G4int j=0; j<nPoints-1; j++) {
112  a[j] = (f[j+1] - f[j])/(x[j+1] - x[j]);
113  };
114 
115  //compute cumulative function
116  //
117  Fc.resize(nPoints);
118  Fc[0] = 0.;
119  for (G4int j=1; j<nPoints; j++) {
120  Fc[j] = Fc[j-1] + 0.5*(f[j] + f[j-1])*(x[j] - x[j-1]);
121  };
122 }
123 
124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
125 
127 {
128  // tabulated function
129  // f is assumed positive, linear per segment, continuous
130  //
131  G4double x_rndm = 0., y_rndm = 0., f_inter = -1.;
132 
133  while (y_rndm > f_inter) {
134  //choose a point randomly
135  x_rndm = x[0] + G4UniformRand()*(x[nPoints-1] - x[0]);
136  y_rndm = G4UniformRand()*fMax;
137  //find bin
138  G4int j = nPoints-2;
139  while ((x[j] > x_rndm) && (j > 0)) j--;
140  //compute f(x_rndm) by linear interpolation
141  f_inter = f[j] + a[j]*(x_rndm - x[j]);
142  };
143  return x_rndm;
144 }
145 
146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
147 
149 {
150  // tabulated function
151  // f is assumed positive, linear per segment, continuous
152  // --> cumulative function is second order polynomial
153 
154  //choose y randomly
155  G4double y_rndm = G4UniformRand()*Fc[nPoints-1];
156  //find bin
157  G4int j = nPoints-2;
158  while ((Fc[j] > y_rndm) && (j > 0)) j--;
159  //y_rndm --> x_rndm : Fc(x) is second order polynomial
160  G4double x_rndm = x[j];
161  G4double aa = a[j];
162  if (aa != 0.) {
163  G4double b = f[j]/aa, c = 2*(y_rndm - Fc[j])/aa;
164  G4double delta = b*b + c;
165  G4int sign = 1; if (aa < 0.) sign = -1;
166  x_rndm += sign*std::sqrt(delta) - b;
167  } else if (f[j] > 0.) {
168  x_rndm += (y_rndm - Fc[j])/f[j];
169  };
170  return x_rndm;
171 }
172 
173 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Definition of the PrimaryGeneratorAction2 class.
CLHEP::Hep3Vector G4ThreeVector
std::vector< G4double > a
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
#define G4UniformRand()
Definition: Randomize.hh:97
static const double twopi
Definition: G4SIunits.hh:75
PrimaryGeneratorAction2(G4ParticleGun *)
void SetParticleEnergy(G4double aKineticEnergy)
std::vector< G4double > x
G4double energy(const ThreeVector &p, const G4double m)
std::vector< G4double > Fc
std::vector< G4double > f
static const double keV
Definition: G4SIunits.hh:213
double G4double
Definition: G4Types.hh:76
G4int sign(const T t)
A simple sign function that allows us to port fortran code to c++ more easily.