第一范文网 - 专业文章范例文档资料分享平台

Geant4 程序编写中的常用代码

来源:用户分享 时间:2025/6/7 2:36:59 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Geant4 程序编写中的常用代码 2010-06-01 20:51:44| 分类: 学习与工作 | 标签:geant4 |字号 订阅 系个人原创,未经同意请勿转载。cnscott的博客 1. 各种常用源粒子的产生 cnscott的博客 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable(); ? 光子:

G4ParticleDefinition* particle

= particleTable->FindParticle(\? 中子:

G4ParticleDefinition* particle

= particleTable->FindParticle(\? 电子:

G4ParticleDefinition* particle

= particleTable->FindParticle(\? Alpha:

G4ParticleDefinition* particle

= particleTable->FindParticle(\? 质子:

G4ParticleDefinition* particle

= particleTable->FindParticle(\? 离子:

G4ParticleDefinition* particle

= particleTable->GetIon(6, 12, 0.0); //原子序数,质量数和激发能量(基态12C)

源粒子的能量、位置、方向、电荷等相关信息要在ParticleGun中设置。

2. 获取日期,记录程序开始运行的时刻 cnscott的博客 #include \ #include \ struct tm *ptr; time_t lt; lt =time(NULL); ptr=gmtime(<);

printf(asctime(ptr)); //格林威治时间 printf(ctime(<)); //本地时间

3. 记录程序运行所用时间 cnscott的博客 #include \ G4Timer myTimer; myTimer.Start(); myTimer.Stop();

G4cout << myTimer << G4endl;

4. 保存文件 cnscott的博客 #include

fstream dataFile(FileName, ios::out); //文件输出流 G4String mydata=”Save my data.”;

dataFile << mydata << G4endl; //数据可以是整数、浮点数或是字符串等。

5. 检查几何体是否重叠 cnscott的博客

G4PVPlacement和G4PVParameterised提供了成员函数: G4bool CheckOverlaps(G4int res=1000, G4double tol=0., G4bool verbose=true)

//当返回值为true时,表明几何存在重叠。

6. 获取相关类的指针 cnscott的博客

G4RunManager* runManager= G4RunManager::GetRunManager(); (MyRunAction *)runManager->GetUserRunAction(); (MyEventAction *)runManager->GetUserEventAction(); (MyStackingAction *)runManager->GetUserStackingAction(); (MySteppingAction *)runManager->GetUserSteppingAction(); (MyTrackingAction *)runManager->GetUserTrackingAction(); G4Run* run = G4RunManager::GetRunManager()->GetCurrentRun(); G4Event* event = G4RunManager::GetRunManager()->GetCurrentEvent(); //版本号

G4String version = G4RunManager::GetRunManager()->GetVersionString()

7. 随机数操作 cnscott的博客

Geant4的随机数产生主要是使用CLHEP函数库。

http://proj-clhep.web.cern.ch/proj-clhep/manual/UserGuide/Random/Random.html

CLHEP::HepRandom::setTheEngine(HepRandomEngine *theNewEngine); //设置随机数引擎,默认引擎为CLHEP::HepJamesRandom。其他可选的引擎包括: CLHEP::DRand48Engine, CLHEP::DualRand, CLHEP::Hurd160Engine, CLHEP::Hurd288Engine, CLHEP::MTwistEngine,

CLHEP::NonRandomEngine, CLHEP::RandEngine, CLHEP::RanecuEngine,

CLHEP::Ranlux64Engine, CLHEP::RanluxEngine, CLHEP::RanshiEngine, and CLHEP::TripleRand.

CLHEP::HepRandom::setTheSeed(long seed, int lux=3); //为默认引擎设置随机数种子,其他引擎可能使用不同函数

G4UniformRand();

//产生均匀分布随机数,等同于CLHEP::HepRandom::getTheEngine()->flat();

G4RandGauss::shoot(double mean, double stdDev);

//产生高斯分布随机数,等同于CLHEP::RandGaussQ::shoot(double mean, double stdDev);

8. 从G4Step中读取有用的信息 cnscott的博客

void MySteppingAction::UserSteppingAction(const G4Step* stp) {

G4Track* theTrack = stp->GetTrack(); //获取G4Track指针

theTrack->GetVolume()->GetName(); //获取当前几何体(PhysicalVolume)名称

G4StepPoint* preStepPoint = stp->GetPreStepPoint();

G4StepPoint* postStepPoint = stp->GetPostStepPoint(); //获取两个StepPoint的指针

if (preStepPoint->GetStepStatus() == fGeomBoundary) //判断是否位于几何体边界

std::cout << \

if (postStepPoint->GetStepStatus() == fGeomBoundary) //判断是否位于几何体边界

std::cout << \

if (theTrack->GetTrackID() == 1) //判断是否是初级粒子(初级粒子TrackID=1) std::cout << \;

搜索更多关于: Geant4 程序编写中的常用代码 的文档
Geant4 程序编写中的常用代码.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c7x1ez2igvg0mq5f7e8f4_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top