source: trunk/source/processes/electromagnetic/lowenergy/test/hTest/src/hTestPrimaryGeneratorAction.cc @ 1350

Last change on this file since 1350 was 1350, checked in by garnier, 13 years ago

update to last version 4.9.4

File size: 8.0 KB
Line 
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//
26#define hTestPrimaryGeneratorAction_CPP
27
28//---------------------------------------------------------------------------
29//
30// ClassName:   hTestPrimaryGeneratorAction
31// 
32// Description: Generate primary beam
33//
34// Authors:    0.6.04.01 V.Ivanchenko
35//
36// Modified:
37//
38//----------------------------------------------------------------------------
39//
40
41#include "hTestPrimaryGeneratorAction.hh"
42#include "hTestPrimaryGeneratorMessenger.hh"
43#include "Randomize.hh"
44#include "G4ParticleGun.hh"
45#include "G4ParticleTable.hh"
46#include "G4ParticleDefinition.hh"
47#include "hTestHisto.hh"
48
49//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
50//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
51
52hTestPrimaryGeneratorAction::hTestPrimaryGeneratorAction(
53                             hTestDetectorConstruction* det):
54  theDet(det)
55{
56  InitializeMe();
57}
58
59//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
60
61void hTestPrimaryGeneratorAction::InitializeMe()
62{
63  verbose = theDet->GetVerbose();
64  theMessenger = new hTestPrimaryGeneratorMessenger(this);
65  particleGun = new G4ParticleGun();
66  counter = 0;
67  x0 = 0.0; 
68  y0 = 0.0;
69  z0 = 0.0;
70  sigmaX = 0.0;
71  sigmaY = 0.0;
72  sigmaZ = 0.0;
73  sigmaE = 0.0;
74  minCosTheta = 1.0;
75  SetBeamEnergy(10.0*MeV);
76  position  = G4ThreeVector(x0,y0,z0);
77  direction = G4ThreeVector(0.0,0.0,1.0);
78  m_gauss = true;
79  if(energy < (hTestHisto::GetPointer())->GetMaxEnergy())
80              (hTestHisto::GetPointer())->SetMaxEnergy(energy);
81}
82
83//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
84
85hTestPrimaryGeneratorAction::~hTestPrimaryGeneratorAction()
86{
87  delete particleGun;
88  delete theMessenger;
89}
90
91//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
92
93void hTestPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
94{
95  counter++ ;
96  verbose = theDet->GetVerbose();
97
98  // Simulation of beam position
99  G4double x = x0;
100  G4double y = y0;
101  G4double z = z0;
102  if(0.0 < sigmaX) x += G4RandGauss::shoot(0.0,sigmaX);
103  if(0.0 < sigmaY) y += G4RandGauss::shoot(0.0,sigmaY);
104  if(0.0 < sigmaZ) z += G4RandGauss::shoot(0.0,sigmaZ);
105  position  = G4ThreeVector(x,y,z);
106  particleGun->SetParticlePosition(position);
107
108  // Simulation of beam direction
109  G4double ux = direction.x();
110  G4double uy = direction.y();
111  G4double uz = direction.z();
112
113  // Beam particles are uniformly distributed over phi, cosTheta
114  if(1.0 > minCosTheta) {
115    uz = minCosTheta + (1.0 - minCosTheta)*G4UniformRand() ;
116    ux = std::sqrt(1.0 - uz*uz) ;
117    uy = ux ;
118    G4double phi = 360.0*deg*G4UniformRand() ;
119    ux *= std::cos(phi) ;
120    uy *= std::sin(phi) ;
121    direction = G4ThreeVector(ux,uy,uz) ;
122  }
123
124  direction = direction.unit();
125  particleGun->SetParticleMomentumDirection(direction);
126  G4ParticleDefinition* particle = particleGun->GetParticleDefinition();
127  G4double mass = particle->GetPDGMass();
128
129  // Simulation of beam kinetic energy
130  G4double kinEnergy = energy;
131
132  if(m_gauss == "flatE") kinEnergy  = minE + (maxE-minE)*G4UniformRand();
133
134  else if(m_gauss == "flatBeta") {
135         G4double beta = minBeta + (maxBeta-minBeta)*G4UniformRand();
136         kinEnergy = mass*(1./std::sqrt(1. - beta*beta) - 1.);
137  }
138  else if(0.0 < sigmaE) kinEnergy += G4RandGauss::shoot(0.0,sigmaE);
139   
140
141  if(0.0 > kinEnergy) kinEnergy = 0.0;
142  particleGun->SetParticleEnergy(kinEnergy);
143
144  G4String particleName = particle->GetParticleName() ;
145
146  if(verbose > 0) {
147    G4cout << "Event#  " << counter
148           << "  Beam particle is generated by hTestPrimaryGeneratorAction " 
149           << G4endl;
150    G4cout << "ParticleName= " << particleName
151           << "  PDGcode= " << particle->GetPDGEncoding()
152           << std::setprecision(5) 
153           << "   KinEnergy(GeV)= "
154           << energy/GeV
155           << "   x(mm)= "
156           << x/mm
157           << " y(mm)= "
158           << y/mm
159           << " z(mm)= "
160           << z/mm
161           << "   ux= " 
162           << ux
163           << " uy= "
164           << uy
165           << " uz= "
166           << uz
167           << endl;
168    }
169
170  particleGun->GeneratePrimaryVertex(anEvent);
171  if(verbose > 1) G4cout << "hTestPrimaryGeneratorAction: BeamOn" << G4endl;
172}
173
174//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
175
176void hTestPrimaryGeneratorAction::SetBeamBeta(G4double val)
177{
178  G4ParticleDefinition* particle = particleGun->GetParticleDefinition();
179  G4double mass = particle->GetPDGMass();
180  if(val > 0. && val < 1.) energy = mass*(1./std::sqrt(1.-val*val) - 1.);
181  G4cout << "hTestPrimaryGeneratorAction: KinEnergy(MeV)= " 
182         << energy/MeV << G4endl;
183  minE = energy;
184  maxE = energy;
185  minBeta = val;
186  maxBeta = val;
187}
188
189//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
190
191void hTestPrimaryGeneratorAction::SetSigmaBeta(G4double val)
192{
193  G4ParticleDefinition* particle = particleGun->GetParticleDefinition();
194  G4double mass = particle->GetPDGMass();
195  if(val > 0. && val < 1.) {
196    sigmaE = mass*(1./std::sqrt(1.-val*val) - 1.);
197    G4double gamma = energy/mass + 1.;
198    G4double beta0 = std::sqrt(1. - 1./(gamma*gamma));
199    G4double beta  = beta0 + val;
200    if (beta >= 1.) beta = 0.9999;
201    maxBeta = beta;
202    maxE = mass*(1./std::sqrt(1.-beta*beta) - 1.);
203    beta  = beta0 - val;
204    if (beta <= 0.) beta = 0.0001;
205    minBeta = beta;
206    minE = mass*(1./std::sqrt(1.-beta*beta) - 1.);
207  }
208}
209
210//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
211
212void hTestPrimaryGeneratorAction::SetBeamSigmaE(G4double val) 
213{
214  G4ParticleDefinition* particle = particleGun->GetParticleDefinition();
215  G4double mass = particle->GetPDGMass();
216  sigmaE = val; 
217  minE = energy - sigmaE;
218  G4double gamma = minE/mass + 1.;
219  minBeta = std::sqrt(1. - 1./(gamma*gamma));
220  maxE = energy + sigmaE;
221  gamma = maxE/mass + 1.;
222  maxBeta = std::sqrt(1. - 1./(gamma*gamma));
223}
224//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
225
226void hTestPrimaryGeneratorAction::SetBeamEnergy(G4double val) 
227{
228  G4ParticleDefinition* particle = particleGun->GetParticleDefinition();
229  G4double mass = particle->GetPDGMass();
230  energy = val;
231  minE = energy - sigmaE;
232  G4double gamma = minE/mass + 1.;
233  minBeta = std::sqrt(1. - 1./(gamma*gamma));
234  maxE = energy + sigmaE;
235  gamma = maxE/mass + 1.;
236  maxBeta = std::sqrt(1. - 1./(gamma*gamma));
237  if(energy < (hTestHisto::GetPointer())->GetMaxEnergy())
238              (hTestHisto::GetPointer())->SetMaxEnergy(energy);
239}
240
241//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
242
243
244
245
246
247
248
249
Note: See TracBrowser for help on using the repository browser.