ChinaNet
标题:
在RunAction中如何取得PrimaryGeneratorAction中发射的粒子信息?
[打印本页]
作者:
鸿飞冥冥
时间:
2015-4-8 09:57
标题:
在RunAction中如何取得PrimaryGeneratorAction中发射的粒子信息?
我的做法是这样的:
SteppingAction里面判断一下step号(track->GetCurrentStepNumber),挑step号为1的那个,就有初始粒子信息了;
然后EventAction和RunAction里面就能利用SteppingAction里得到的初始粒子信息了(可能中间需要设几个变量倒一倒)。
应该还有别的做法。
转自
www.52mc.net
作者:
鸿飞冥冥
时间:
2015-4-8 09:57
本文仅为在蒙卡学术论坛(52mc.net)答复相关问题使用,任何人未经作者本人许可不得转载至其他论坛,作者保留追究转载者相关责任的权利!
[sell=10]这些调用都可以直接通过类的调用来实现,比如你把PGAction作为RunAction的一个参数
但是,我觉得你这个问题有些奇怪,光只是在runAction里面调用PGAction的信息没有意义
RunAction是每次beamOn的开始和结束的时候各调用一次的,而PGAction是每个event调用一次的
如果你在RunAction里面调用PGAction的信息的话,只能得到最后一个event对应的PGAction信息
除非你再在其他类,比如SteppingAction里面调用RunAction
如果你是想要看每一个event对应的PGAction信息的话,我建议你在RunAction里面设定一个public的数组
然后在PGAction里面访问RunAction里面的这个public数组,把每次的PG信息写入这个数组
在Run结束的时候由EndOfRunAction把这个数组输出
举例如下:
首先需要在main.cc里面定义PGAction是RunAction的函数
testRunAction* testRun = new testRunAction;
runManager->SetUserAction(testRun);
runManager->SetUserAction(new testPrimaryGeneratorAction(testRun)); //这就是把一个RunAction类型的指针作为PGAction的变量了
然后需要在PGAction.hh文件里同样定义PGAction的构造器
class testPrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
{
public:
testPrimaryGeneratorAction(testRunAction*);
~testPrimaryGeneratorAction();
public:
testRunAction* theRun; //这是为testRunAction类生成一个实例,作为PGAction的变量
private:
……
};
然后你需要在PGAction.cc里面为theRun赋值
testPrimaryGeneratorAction::testPrimaryGeneratorAction(testRunAction* tRun):theRun(tRun)
{
……
}
然后你就可以直接在PGAction.cc里面访问RunAction里面设定为public的函数和变量了
void testPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
……
particleGun->SetParticlePosition();
……
i=theRun->EventNumber; //EventNumber是在RunAction.hh里面定义为public的整型变量,是用来统计event数的
theRun->theParticleEnergy
=particleGun->GetParticleEnergy; //theParticleEnergy是在RunAction.hh里面定义为public的G4double型数组变量,是用来记录每次发射粒子能量的
……
theRun->EventNumber++; //将event计数+1
……
particleGun->GeneratePrimaryVertex(anEvent);
}
然后你就可以在RunAction的EndOfRunAction里面输出每次发射的粒子信息了
void testRunAction::EndOfRunAction(const G4Run* )
{
……
fstream datafile;
datafile.open("
articleEnergy.dat",ios:
ut|ios::app);
datafile<<"下面输出本次模拟中每次发射的粒子能量信息"<<endl;
datafile<<"Event计数"<<"\t"<<"粒子能量"<<endl;
for (i=0;i++;i<EventNumber)
{
datafile<<i+1<<"\t"<<
articleEnergy
<<endl;
}
datafile.close;
……
}
这样运行结束后你只要打开ParticleEnergy.dat文件就可以看到每次发射的粒子能量了
同样,你可以多定义几个数组记录粒子的其他信息 [/sell]
欢迎光临 ChinaNet (http://www.nuclear.net.cn/)
Powered by Discuz! X3.1