Geant 4中Run、Event、Step等数据提取(初学笔记)

Geant 4运行中涉及到Run、Event、Step、Track等过程,其ID、位置、时间、能量、几何体、材料、粒子类型、反应类型等各类数据均可被提取。数据提取方式较多,如SensitiveDetector,Hit和HitsCollection,以及RunAction, EventAction和SteppingAction等等。

相关知识的介绍可以参见官方手册(http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/ForApplicationDeveloper/html/index.html)或网络文章,如https://blog.csdn.net/Jack_chf/article/details/98631042和http://littlepascal.lofter.com/post/322764_ccda14b等。

Run、Event、Step、Track等过程的数据提取可参考官方网站FAQ(http://geant4-userdoc.web.cern.ch/geant4-userdoc/UsersGuides/FAQ/html/tracksAndSteps.html)。

Geant 4中各个类或例子的程序源码可参见:https://geant4.kek.jp/Reference/10.05/classG4BestUnit.html

 

本人在exampleB1例子的基础上,尝试在B1SteppingAction中提取并保存每个Step的部分数据,部分程序及输出结果如下所示:(output data 部分是本人添加的)

#include "G4RunManager.hh"

void B1SteppingAction::UserSteppingAction(const G4Step* step)
{
  if (!fScoringVolume) { 
    const B1DetectorConstruction* detectorConstruction
      = static_cast
        (G4RunManager::GetRunManager()->GetUserDetectorConstruction());
    fScoringVolume = detectorConstruction->GetScoringVolume();   
  }

  // get volume of the current step
  G4LogicalVolume* volume 
    = step->GetPreStepPoint()->GetTouchableHandle()
      ->GetVolume()->GetLogicalVolume();
      
  // check if we are in scoring volume
  if (volume != fScoringVolume) return;

  // collect energy deposited in this step
  G4double edepStep = step->GetTotalEnergyDeposit();
  fEventAction->AddEdep(edepStep);  

  // output data
  //
  G4int eventID = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
  G4Track* track = step->GetTrack();
  G4int trackID = track->GetTrackID();
  G4int parentID = track->GetParentID();
  G4int stepID = track->GetCurrentStepNumber();
  G4double globalTime = track->GetGlobalTime();
  G4String particalName = track->GetDefinition()->GetParticleName();// equal to GetDynamicParticle()
  // G4String particalName2 = track->GetDynamicParticle()->GetDefinition()->GetParticleName();
  G4ThreeVector xyzTrack = track->GetPosition();// equal to PostStepPoint()
  G4ThreeVector xyzPost = step->GetPostStepPoint()->GetPosition();// string type
  //
  std::ofstream myData;
  myData.open("MyData.data", std::ios::app);
  if (energy.is_open()){
    energy<

注:

1. xyzTrack输出的是string类型数据,xyzTrack[0/1/2]可以直接输出三维坐标。

2. EventID的提取还有一种复杂一些的方式,见文末。

3. 在RunAction.cc 中添加如下代码,确保每次程序运行前先清空前一次运行结果(不推荐)。

  std::ofstream myData;
  myData.open("MyData.data",std::ios::out);
  myData.close();

输出数据如下:

0	1	0	4	(-22.1688,22.4189,100)	(-22.1688,22.4189,100)	-22.1688	22.4189	100	gamma	0.83391	
4	1	0	2	(-16.1019,68.5515,100)	(-16.1019,68.5515,100)	-16.1019	68.5515	100	gamma	0.83391	
5	1	0	2	(45.9618,-22.952,100)	(45.9618,-22.952,100)	45.9618	-22.952	100	gamma	0.83391	
6	1	0	2	(32.5909,-42.5795,100)	(32.5909,-42.5795,100)	32.5909	-42.5795	100	gamma	0.83391	
7	1	0	2	(41.5544,-13.6419,66.3294)	(41.5544,-13.6419,66.3294)	41.5544	-13.6419	66.3294	gamma	0.721597	
7	1	0	3	(4.92261,-1.80317,70.8464)	(4.92261,-1.80317,70.8464)	4.92261	-1.80317	70.8464	gamma	0.850891	
7	1	0	4	(-20.7132,6.45138,64.4711)	(-20.7132,6.45138,64.4711)	-20.7132	6.45138	64.4711	gamma	0.943209	
7	1	0	5	(-32.3885,-0.428879,53.6671)	(-32.3885,-0.428879,53.6671)	-32.3885	-0.428879	53.6671	gamma	1.00102	
7	1	0	6	(-60,-61.2693,93.6792)	(-60,-61.2693,93.6792)	-60	-61.2693	93.6792	gamma	1.26079	
7	1	0	9	(-49.8705,-83.0484,86.0968)	(-49.8705,-83.0484,86.0968)	-49.8705	-83.0484	86.0968	gamma	1.35398	
7	5	1	1	(-32.4141,-0.422902,53.6124)	(-32.4141,-0.422902,53.6124)	-32.4141	-0.422902	53.6124	e-	1.00135	
7	4	1	1	(-20.744,6.49458,64.4881)	(-20.744,6.49458,64.4881)	-20.744	6.49458	64.4881	e-	0.943521	
7	3	1	1	(4.92159,-1.80282,70.8511)	(4.92159,-1.80282,70.8511)	4.92159	-1.80282	70.8511	e-	0.850939	
7	2	1	1	(42.9266,-12.9366,70.672)	(42.9266,-12.9366,70.672)	42.9266	-12.9366	70.672	e-	0.7364	
7	2	1	2	(45.6817,-11.3767,72.7819)	(45.6817,-11.3767,72.7819)	45.6817	-11.3767	72.7819	e-	0.749168	
7	2	1	3	(46.622,-10.0944,75.2173)	(46.622,-10.0944,75.2173)	46.622	-10.0944	75.2173	e-	0.75897	
7	2	1	4	(47.8083,-8.90427,76.5569)	(47.8083,-8.90427,76.5569)	47.8083	-8.90427	76.5569	e-	0.766295	
7	2	1	5	(47.4191,-9.00081,77.7972)	(47.4191,-9.00081,77.7972)	47.4191	-9.00081	77.7972	e-	0.770881	
7	2	1	6	(47.1056,-8.69988,77.9746)	(47.1056,-8.69988,77.9746)	47.1056	-8.69988	77.9746	e-	0.772706	
7	7	2	1	(43.0266,-12.8545,70.7928)	(43.0266,-12.8545,70.7928)	43.0266	-12.8545	70.7928	gamma	0.73699	
7	7	2	2	(42.917,-13.2873,70.172)	(42.917,-13.2873,70.172)	42.917	-13.2873	70.172	gamma	0.739541	
7	9	7	1	(42.9156,-13.2871,70.1727)	(42.9156,-13.2871,70.1727)	42.9156	-13.2871	70.1727	e-	0.739562	
9	1	0	4	(5.71433,4.95276,100)	(5.71433,4.95276,100)	5.71433	4.95276	100	gamma	0.83391	
7	8	7	1	(43.0266,-12.8545,70.7928)	(43.0266,-12.8545,70.7928)	43.0266	-12.8545	70.7928	e-	0.736991

附:EventID提取,参考程序(http://hurel.hanyang.ac.kr/Geant4/Doxygen/9.6.p02/html/_f04_event_action_8cc_source.html)

    // G4UserEventAction.hh
    //
      protected:
         G4EventManager* fpEventManager;

   // F04EventAction.cc
   //
   #include "G4EventManager.hh"
   G4int F04EventAction::GetEventNo()
   {
     G4int evno = fpEventManager->GetConstCurrentEvent()->GetEventID();
     return evno ;
   }

   // SteppingAction.cc
   //
   G4int eventID = fEventAction->GetEventNo();

 

你可能感兴趣的:(Geant 4中Run、Event、Step等数据提取(初学笔记))