GEANT4学习 输出粒子信息小计

GEANT4学习 输出粒子信息小计

写了一个简单的模型,并在step里面输出粒子的信息,通过这些验证G4在粒子输出信息上的规则。

我原本想的是输出
eventID、trackID、stepID、particle name、prepoint process name、postpoint process name、creator name、prepoint Kinetic Energy、postpoint Kinetic Energy、prepoint volume name、postpoint volume name以及energy deposit。

第一次尝试的时候出现了问题,个别的信息输出时会遇到“段错误”,网上解释段错误是栈溢出,但是我感觉好多都是信息的逻辑提取出现了问题,这个我之后讲。

第一次输出信息的代码如下所示:

  G4int eventID = (G4EventManager::GetEventManager())->GetConstCurrentEvent()->GetEventID();
  // 因为在step里面,我使用EventManager这个类去获取EventID
  G4int trackID = step->GetTrack()->GetTrackID();
 
  G4String particleName =  step->GetTrack()->GetDefinition()->GetParticleName();
  
  G4String postProcessName = step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();

  G4String preName = step->GetPreStepPoint()->GetTouchableHandle()->GetVolume()->GetName();

  G4double postKE = step->GetPostStepPoint()->GetKineticEnergy();
  G4double preKE = step->GetPreStepPoint()->GetKineticEnergy();

  G4double edepStep = step->GetTotalEnergyDeposit();

输出语句如下所示:

 outfileInfo<<std::setw(10)<<std::setfill(' ') <<std::left <<particleName                           
                      <<std::setw(4)<<std::setfill(' ') <<std::left <<eventID
                      <<std::setw(4)<<std::setfill(' ') <<std::left <<trackID
                      
                     <<std::setw(8)<<std::setfill(' ') <<std::left <<preName
                    <<std::setw(20)<<std::setfill(' ') <<std::left <<preKE

                   <<std::setw(20)<<std::setfill(' ') <<std::left <<postProcessName
          
                  <<std::setw(20)<<std::setfill(' ') <<std::left <<postKE

                  <<std::setw(20)<<std::setfill(' ') <<std::left <<edepStep
                  <<G4endl;

输出信息的文件如下所示:

particleN eID tID preN    preKE               postProName         postKE              edepStep
neutron   0   1   World   14                  Transportation      14                  0                   
neutron   0   1   A1      14                  Transportation      14                  0                   
neutron   0   1   A2      14                  hadElastic          13.5434             0                   
neutron   0   1   A2      13.5434             hadElastic          9.21031             0                   
neutron   0   1   A2      9.21031             hadElastic          5.6599              0                   
neutron   0   1   A2      5.6599              hadElastic          3.56607             0                   
neutron   0   1   A2      3.56607             hadElastic          2.93008             0                   
neutron   0   1   A2      2.93008             Transportation      2.93008             0                   
neutron   0   1   World   2.93008             Transportation      2.93008             0                   
O16       0   6   A2      0.63616             ionIoni             0                   0.63616             
O16       0   6   A2      0                   RadioactiveDecay    0                   0                   
proton    0   5   A2      2.09497             hIoni               1.38174             0.713232            
proton    0   5   A2      1.38174             hIoni               0.580998            0.800742            
proton    0   5   A2      0.580998            hIoni               0.0178738           0.563124            
proton    0   5   A2      0.0178738           hIoni               0.0114283           0.00644545          
proton    0   5   A2      0.0114283           hIoni               0.00879221          0.00263613          
proton    0   5   A2      0.00879221          hIoni               0                   0.00879221          
proton    0   4   A2      3.55233             hIoni               2.98914             0.563191            
proton    0   4   A2      2.98914             hIoni               2.34926             0.639877            
proton    0   4   A2      2.34926             hIoni               1.67271             0.676547            
proton    0   4   A2      1.67271             hIoni               0.877513            0.7952              
proton    0   4   A2      0.877513            hIoni               0.0726702           0.804843            
proton    0   4   A2      0.0726702           hIoni               0                   0.0726702           
proton    0   3   A2      4.33546             hIoni               3.72805             0.607402            
proton    0   3   A2      3.72805             hIoni               3.15269             0.575363            
proton    0   3   A2      3.15269             hIoni               2.55276             0.599933            
proton    0   3   A2      2.55276             hIoni               1.88505             0.667708            
proton    0   3   A2      1.88505             hIoni               1.15731             0.727738            
proton    0   3   A2      1.15731             hIoni               0.0440143           1.1133              
proton    0   3   A2      0.0440143           hIoni               0                   0.0440143           
proton    0   2   A2      0.456834            hIoni               0.0206327           0.436202            
proton    0   2   A2      0.0206327           hIoni               0.0198335           0.00079924          
proton    0   2   A2      0.0198335           hIoni               0.0121395           0.007694            
proton    0   2   A2      0.0121395           hIoni               0.00741999          0.00471949          
proton    0   2   A2      0.00741999          hIoni               0                   0.00741999          

我想输出的信息有很多还没办法实现,接下来来一个一个讲:
stepID:同学说没有stepID这个讲法,官网上好像确实没有相关的语句;

preProcessName:

G4String preProcessName = step->GetPreStepPoint()->GetProcessDefinedStep()->GetProcessName();

这段一放上去就会出现段错误,我判断是初始粒子的preProcessName是没有的,因为是初始粒子,由初始粒子生成器生成的。

因此我加了一句来判断是否是初始粒子:

G4String preProcessName;
 if (trackID==1)  preProcessName="primary";
 else preProcessName = step->GetPreStepPoint()->GetProcessDefinedStep()->GetProcessName();

但是还是会出现“段错误”,有可能这个prePoint就没有processname。
所以在输出的时候我又换成下面的语句。对于一个step 前点使用creatorProcessName,后点使用postPointProcessName。

G4String creatorName;
if (trackID==1)  creatorName="primary";
else creatorName = step->GetTrack()->GetCreatorProcess()->GetProcessName();

postPointVolumName
后点volume的名字出现问题,我觉得是当粒子输运接触world边界时,无法获取后点的volume,因此我也加一个判断:

G4String postName;
  if (step->GetPostStepPoint()->GetStepStatus() == fWorldBoundary) postName="out";
  else postName = step->GetPostStepPoint()->GetTouchableHandle()->GetVolume()->GetName();

这样就可以把想要的信息输出了,摘录部分输出结果:

neutron   6   1   primary             World   14                  Transportation      A1     14                  0                   
neutron   6   1   primary             A1      14                  Transportation      A2     14                  0                   
neutron   6   1   primary             A2      14                  neutronInelastic    A2     0                   0                   
gamma     6   7   neutronInelastic    A2      0.0210016           phot                A2     0                   2.834e-05           
e-        6   9   phot                A2      0.00050894          msc                 A2     0.00050894          0                   
e-        6   9   phot                A2      0.00050894          eIoni               A2     0                   0.00050894          
e-        6   8   phot                A2      0.0204643           msc                 A2     0.000334888         0.0201294           
e-        6   8   phot                A2      0.000334888         eIoni               A2     0                   0.000334888         
gamma     6   6   neutronInelastic    A2      0.0210016           phot                A2     0                   4.338e-05           
e-        6   11  phot                A2      0.0004939           msc                 A2     0.000458443         3.54571e-05         
e-        6   11  phot                A2      0.000458443         eIoni               A2     0                   0.000458443         
e-        6   10  phot                A2      0.0204643           msc                 A2     0.0190617           0.00140257          
e-        6   10  phot                A2      0.0190617           eIoni               A2     0                   0.0190617           
gamma     6   5   neutronInelastic    A2      0.00267715          phot                A2     0                   5.846e-05           
e-        6   13  phot                A2      0.00047882          msc                 A2     0.00047882          0                   
e-        6   13  phot                A2      0.00047882          eIoni               A2     0                   0.00047882          
e-        6   12  phot                A2      0.00213987          msc                 A2     0.00186088          0.000278987         
e-        6   12  phot                A2      0.00186088          eIoni               A2     0                   0.00186088          
gamma     6   4   neutronInelastic    A2      3.85317             Transportation      World  3.85317             0                   
gamma     6   4   neutronInelastic    World   3.85317             Transportation      out    3.85317             0                   
C13       6   3   neutronInelastic    A2      1.38632             ionIoni             A2     0.272673            1.11365             
C13       6   3   neutronInelastic    A2      0.272673            ionIoni             A2     0                   0.272673            
C13       6   3   neutronInelastic    A2      0                   RadioactiveDecay    A2     0                   0                   
alpha     6   2   neutronInelastic    A2      6.5013              ionIoni             A2     3.15038             3.35092             
alpha     6   2   neutronInelastic    A2      3.15038             ionIoni             A2     0                   3.15038           
  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
Geant4 中,可以通过实现 G4SteppingVerbose 类来获取二次粒子信息。这个类是 Geant4 中用于输出每一步模拟信息的基类。你可以继承 G4SteppingVerbose 类并覆盖其成员函数,以输出你需要的信息。 以下是一个简单的例子,演示了如何获取二次粒子信息: ```cpp #include "G4SteppingVerbose.hh" #include "G4Track.hh" class MySteppingVerbose: public G4SteppingVerbose { public: void TrackingStarted() override { G4SteppingVerbose::TrackingStarted(); G4Track* track = fpSteppingManager->GetTrack(); G4cout << "Primary particle: " << track->GetDefinition()->GetParticleName() << G4endl; } void StepInfo() override { G4SteppingVerbose::StepInfo(); const G4Step* step = fpSteppingManager->GetStep(); G4Track* track = fpSteppingManager->GetTrack(); if (track->GetCurrentStepNumber() == 1) { G4cout << "Secondary particle: " << step->GetSecondary()->at(0)->GetDefinition()->GetParticleName() << G4endl; } } }; int main() { // ... MySteppingVerbose* mySteppingVerbose = new MySteppingVerbose(); runManager->SetUserVerbose(mySteppingVerbose); // ... return 0; } ``` 在上面的例子中,我们继承了 G4SteppingVerbose 类,并覆盖了其中的 TrackingStarted() 和 StepInfo() 函数。TrackingStarted() 函数在开始跟踪一个粒子时调用,我们在这里输出了主粒子信息。StepInfo() 函数在每一步结束后调用,我们在这里判断当前步骤是否为第一步,如果是,则输出第一个二次粒子信息。 注意,为了使用自定义的 G4SteppingVerbose 类,我们需要在创建 G4RunManager 对象之后,调用 SetUserVerbose() 函数,将其设置为用户定义的详细模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于谦烫头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值