文|梁佐佐

一個通用的粒子發(fā)生函數(shù)源文件,即MYPrimaryGeneratorAction.cc大致就是本文的全部了。同現(xiàn)實(shí)場景相符,你需要知道每個發(fā)射的模擬粒子的特點(diǎn),即出射點(diǎn)在哪、發(fā)射方向、粒子類型、能量大小等參數(shù)。
給大家上個G4論壇帖子,我們G4模擬中的很多問題,該論壇幾乎都會有人提問回答的。
http://hypernews.slac.stanford.edu/HyperNews/geant4/get/eventtrackmanage/13.html?inline=-1
#include"MYPrimaryGeneratorAction.hh"
#include"Randomize.hh"
#include"G4Event.hh"
#include"G4ParticleGun.hh"
#include"G4GeneralParticleSource.hh"
#include"G4ParticleTable.hh"
#include"G4ParticleDefinition.hh"
#include"G4SystemOfUnits.hh"
#include"G4RunManager.hh"
#include"G4Run.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......?
MYPrimaryGeneratorAction::MYPrimaryGeneratorAction()
{G4int?n_particle?=1;
//particleGun?=?new?G4GeneralParticleSource();
particleGun?=newG4ParticleGun(n_particle);
G4ParticleTable*?particleTable?=?G4ParticleTable::GetParticleTable();
G4ParticleDefinition*?particle?=?particleTable->FindParticle("opticalphoton");
//G4ParticleDefinition*?particle?=?particleTable->FindParticle("gamma");
//我們找G4中現(xiàn)成的粒子源particleGun->SetParticleDefinition(particle);
particleGun->SetParticleDefinition(particle);
particleGun->SetParticleTime(0.0*ns);
//particleGun->SetParticlePosition(G4ThreeVector(-1*mm,1*mm,-1*mm));
// particleGun->SetParticleMomentumDirection(G4ThreeVector(1.,0.,0.));
particleGun->SetParticleEnergy(3.*eV);
//particleGun->SetParticlePolarization(G4ThreeVector(1,0,0));
//particleGun->SetParticleEnergy(60*keV);
//我們可以在初始化這個源文件時,就設(shè)置好particleGun的默認(rèn)值。
}
MYPrimaryGeneratorAction::~MYPrimaryGeneratorAction()
{
deleteparticleGun;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
voidMYPrimaryGeneratorAction::GeneratePrimaries(G4Event*?anEvent)
{
constG4Run?*nowrun=G4RunManager::GetRunManager()->GetCurrentRun();
G4int runid=nowrun->GetRunID();
//?在程序的任意cc源文件中,均可調(diào)用G4RunManager,在這個例子中,我們想得到當(dāng)前RUN的RunID,即如果我在mac執(zhí)行文件中,/run/beamOn?100?重復(fù)寫了3次,表示有3個RUN,RunID分別是0,1,2。
G4int posx,posy,posz;
posx=int(floor(runid/64));
posy=int(floor((runid%64)/8));
posz=int(floor((runid%64)%8));
particleGun->SetParticlePosition(G4ThreeVector((posx-3.5)*3.2*mm,(posy-3.5)*3.2*mm,(posz-3.5)*3.2*mm));
//我們想依據(jù)不同的RUN依次設(shè)置放射源的不同位置。這樣子處理,我就不必每跑一次RUN改一次放射源位置了,比較靈活。這其中很多的Set***成員函數(shù),GPS是沒有的(或許有但是還沒找到),ParticleGun更靈活,而GPS在mac文件中更機(jī)智,看個人選擇。
G4double rndmCosTheta,rndmTheta,rndmPhi,rndmLambda,px,py,pz,pol_x,pol_y,pol_z;
rndmCosTheta = G4UniformRand()*2-1;
rndmTheta =acos(rndmCosTheta);
rndmPhi = G4UniformRand()*(2*M_PI);
rndmLambda = G4UniformRand()*(2*M_PI);
// Momentum direction (random)
px=sin(rndmTheta)*cos(rndmPhi);
py=sin(rndmTheta)*sin(rndmPhi);
pz=cos(rndmTheta); particleGun->SetParticleMomentumDirection(G4ThreeVector(px,py,pz));
//我們定義一個動量方向矢量,隨機(jī)為一個球面源,在此特別感謝唐光毅博后的數(shù)學(xué)指導(dǎo)。
// Polarization direction (random)
pol_x=cos(rndmLambda)*cos(rndmTheta)*cos(rndmPhi)-sin(rndmLambda)*sin(rndmPhi);
pol_y=cos(rndmLambda)*cos(rndmTheta)*sin(rndmPhi)+sin(rndmLambda)*cos(rndmPhi);
pol_z= -cos(rndmLambda)*sin(rndmTheta);?
?particleGun->SetParticlePolarization(G4ThreeVector(pol_x,pol_y,pol_z));
//定義光學(xué)光子的極化角,這個我現(xiàn)在還不懂,不知道到底有啥用,可能是考慮光子的偏振吧。
particleGun->GeneratePrimaryVertex(anEvent);
//?GeneratePrimaryVertex(anEvent)?這函數(shù)運(yùn)行的瞬間,就會產(chǎn)生一個模擬粒子,寫兩遍就可以發(fā)射兩次,你也可以借此機(jī)智的設(shè)置為一個雙源或者多源。
//G4cout<<"run ID is "<<runid<<G4endl;
//用好這個簡單的G4cout,你的G4程序代入感和自主學(xué)習(xí)能力將會增強(qiáng)很多很多倍。哪里不懂就試試萬能的G4cout吧。
}