文章目录
一、生成初级event
G4VuserPrimaryGeneratorAction是获得用户自己的实体类的强制类
- 实体类中必须指定如何生成初级event
- 初级event实际将由G4VPrimaryGenerator的实体类完成
- 用户的G4VUserPrimaryGeneratorAction实体类只是安排了初级粒子的生成方式
//
// ExG4PrimaryGeneratorAction01.hh
//
#ifndef ExG4PrimaryGeneratorAction01_h
#define ExG4PrimaryGeneratorAction01_h 1 //G4VUserPrimaryGeneratorAction实体类中构造的主生成器对象必须在析构函数中被删
#include "G4VUserPrimaryGeneratorAction.hh"
#include "G4ThreeVector.hh"
#include "globals.hh"
class G4ParticleGun;
class G4Event;
class ExG4PrimaryGeneratorAction01 : public G4VUserPrimaryGeneratorAction
{
public:
ExG4PrimaryGeneratorAction01(
const G4String& particleName = "geantino", //字符&,粒子名称
G4double energy = 1.*MeV, //粒子能量
G4ThreeVector position= G4ThreeVector(0,0,0), //初始位置
G4ThreeVector momentumDirection = G4ThreeVector(0,0,1)); //动量方向
~ExG4PrimaryGeneratorAction01();
// methods
virtual void GeneratePrimaries(G4Event*);
private:
// data members
G4ParticleGun* fParticleGun; //pointer a to G4 service class
};
#endif
//
// ExG4PrimaryGeneratorAction01.cc
//
#include "ExG4PrimaryGeneratorAction01.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
ExG4PrimaryGeneratorAction01::ExG4PrimaryGeneratorAction01(
const G4String& particleName,
G4double energy,
G4ThreeVector position,
G4ThreeVector momentumDirection)
: G4VUserPrimaryGeneratorAction(),
fParticleGun(0)
{
G4int nofParticles = 1;
fParticleGun = new G4ParticleGun(nofParticles);
// default particle kinematic
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4ParticleDefinition* particle
= particleTable->FindParticle(particleName);
fParticleGun->SetParticleDefinition(particle);
fParticleGun->SetParticleEnergy(energy);
fParticleGun->SetParticlePosition(position);
fParticleGun->SetParticleMomentumDirection(momentumDirection);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
ExG4PrimaryGeneratorAction01::~ExG4PrimaryGeneratorAction01()
{
delete fParticleGun;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void ExG4PrimaryGeneratorAction01::GeneratePrimaries(G4Event* anEvent)
{
// this function is called at the beginning of event
fParticleGun->GeneratePrimaryVertex(anEvent);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
1. generator的选择
在G4VUserPrimaryGeneratorAction的构造函数中,要实例化主generator。必要的话,还要为generator设置初始条件
class ExG4PrimaryGeneratorAction01 : public G4VUserPrimaryGeneratorAction
#ifndef ExG4PrimaryGeneratorAction01_h
#define ExG4PrimaryGeneratorAction01_h 1
2. 生成一个event
generatePrimaries()
- 是G4VUserPrimaryGeneratorAction的纯虚方法
- 在每个event开始时被调用
- 必须调用通过generatePrimaryVertex()方法实例化的G4VPrimaryGenerator实体类
二、G4VPrimaryGenerator G4初级发生器
三个G4VPrimaryGenerator实体类:
- G4ParticleGun
- G4GeneralParticleSource
- G4HEPEvtInterface(见3.6 Event Generator Interface)
1. G4ParticleGun
-
G4ParticleGun类产生具有给定动量和位置的初级粒子,不提供随机化
-
G4ParticleGun的构造函数取整数,产生一个或多个运动学完全相同的初级粒子
-
如需随机化能量、动量或位置,可通过G4ParticleGun提供的各种set方法实现(如,可定义X,Y,Z为random变量),在实例化的G4VUserPrimaryGeneratorAction类的generatePrimaries()方法中实现这些调用,随后再调用G4ParticleGun的generatePrimaryVertex()
G4提供的各种分布的随机数生成方法见*3.2 Global Usage Classes
2. Public methods of G4ParticleGun
G4ParticleGun 提供以下方法,可从用户实体化的G4VUserPrimaryGeneratorAction 类中的generatePrimaries()方法调用:
- void SetParticleDefinition(G4ParticleDefinition*)
- void SetParticleMomentum(G4ParticleMomentum)
- void SetParticleMomentumDirection(G4ThreeVector)
- void SetParticleEnergy(G4double)
- void SetParticleTime(G4double)
- void SetParticlePosition(G4ThreeVector)
- void SetParticlePolarization(G4ThreeVector)
- void SetNumberOfParticles(G4int)
3. G4GeneralParticleSource
若想以更复杂的方式生成初级粒子,可使用G4GeneralParticleSource,称作GEANT4 General Particle Source module (GPS
),见2.7 GEANT4 General Particle Source