Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 99721 2016-10-03 08:11:44Z 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 : fParticleGun(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 
71  fParticleGun->SetParticleMomentumDirection(dir);
72 
73  //set energy from a tabulated distribution
74  //
75  //G4double energy = RejectAccept();
77  fParticleGun->SetParticleEnergy(energy);
78 
79  //create vertex
80  //
81  fParticleGun->GeneratePrimaryVertex(anEvent);
82 }
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
85 void PrimaryGeneratorAction2::InitFunction()
86 {
87  // tabulated function
88  // Y is assumed positive, linear per segment, continuous
89  //
90  fNPoints = 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 yy[] =
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  fX.resize(fNPoints); fY.resize(fNPoints);
102  fYmax = 0.;
103  for (G4int j=0; j<fNPoints; j++) {
104  fX[j] = xx[j]; fY[j] = yy[j];
105  if (fYmax < fY[j]) fYmax = fY[j];
106  };
107 
108  //compute slopes
109  //
110  fSlp.resize(fNPoints);
111  for (G4int j=0; j<fNPoints-1; j++) {
112  fSlp[j] = (fY[j+1] - fY[j])/(fX[j+1] - fX[j]);
113  };
114 
115  //compute cumulative function
116  //
117  fYC.resize(fNPoints);
118  fYC[0] = 0.;
119  for (G4int j=1; j<fNPoints; j++) {
120  fYC[j] = fYC[j-1] + 0.5*(fY[j] + fY[j-1])*(fX[j] - fX[j-1]);
121  };
122 }
123 
124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
125 
127 {
128  // tabulated function
129  // Y is assumed positive, linear per segment, continuous
130  //
131  G4double Xrndm = 0., Yrndm = 0., Yinter = -1.;
132 
133  while (Yrndm > Yinter) {
134  //choose a point randomly
135  Xrndm = fX[0] + G4UniformRand()*(fX[fNPoints-1] - fX[0]);
136  Yrndm = G4UniformRand()*fYmax;
137  //find bin
138  G4int j = fNPoints-2;
139  while ((fX[j] > Xrndm) && (j > 0)) j--;
140  //compute Y(x_rndm) by linear interpolation
141  Yinter = fY[j] + fSlp[j]*(Xrndm - fX[j]);
142  };
143  return Xrndm;
144 }
145 
146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
147 
149 {
150  // tabulated function
151  // Y is assumed positive, linear per segment, continuous
152  // --> cumulative function is second order polynomial
153 
154  //choose y randomly
155  G4double Yrndm = G4UniformRand()*fYC[fNPoints-1];
156  //find bin
157  G4int j = fNPoints-2;
158  while ((fYC[j] > Yrndm) && (j > 0)) j--;
159  //y_rndm --> x_rndm : fYC(x) is second order polynomial
160  G4double Xrndm = fX[j];
161  G4double a = fSlp[j];
162  if (a != 0.) {
163  G4double b = fY[j]/a, c = 2*(Yrndm - fYC[j])/a;
164  G4double delta = b*b + c;
165  G4int sign = 1; if (a < 0.) sign = -1;
166  Xrndm += sign*std::sqrt(delta) - b;
167  } else if (fY[j] > 0.) {
168  Xrndm += (Yrndm - fYC[j])/fY[j];
169  };
170  return Xrndm;
171 }
172 
173 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Definition of the PrimaryGeneratorAction2 class.
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
static constexpr double twopi
Definition: G4SIunits.hh:76
tuple b
Definition: test.py:12
#define G4UniformRand()
Definition: Randomize.hh:97
PrimaryGeneratorAction2(G4ParticleGun *)
void SetParticleEnergy(G4double aKineticEnergy)
G4double energy(const ThreeVector &p, const G4double m)
double G4double
Definition: G4Types.hh:76
tuple c
Definition: test.py:13
static constexpr double keV
Definition: G4SIunits.hh:216
G4int sign(const T t)