蒙特卡洛模拟GEANT4例子exampleB4a源码解读.doc
《蒙特卡洛模拟GEANT4例子exampleB4a源码解读.doc》由会员分享,可在线阅读,更多相关《蒙特卡洛模拟GEANT4例子exampleB4a源码解读.doc(29页珍藏版)》请在三一文库上搜索。
1、Geant4 ExampleB4a 源码解读今天继续简单讲一下exampleB4中的几个子例子,先是exampleB4a,仍然是采用Steppi ngAction 步一步抽取事件,这种方法的好处是能够很详细的考虑、输出每一步的信 息,但有时候我们只希望对感兴趣部分(位置)进行事件抽取,则用SD和Hit要明显方便一些,例如在exampleB4c中用到的就是 SD和Hit进行数据抽取。exampleB4a和exampleB4c 描述的是同一个几何、物理过程,只是数据抽取的方式不同,因此将这两个例子放在一起具 有比较意义。本次先对 exampleB4a进行讲解。exampleB4a计算了入射粒子(默
2、认电子,能 量50 MeV,轴向方向)在多层铅-液氩材料中(由10个吸收体和间隙复制组成)的能量沉 积及带电粒子(包括次级粒子)径迹长度。图1.几何可视化及粒子输运可视化exampleB4a.cc#in clude B4DetectorCo nstructio n.hh#in clude B4aActio nlni tializatio n.hh#ifdef G4MULTITHREADED#in clude G4MTRu nMan ager.hh#else#in clude G4Ru nMan ager.hh#en dif#in clude G4UIma nager.hh#in clude G
3、4UIcomma nd.hh#include FTFP_BERT.hh #include Randomize.hh#include G4VisExecutive.hh#include G4UIExecutive.hh/oooOO0OOooooooOO0OOooooooOO0OOooooooOO0OOooonamespace void PrintUsage() G4cerr Usage: G4endl;G4cerr exampleB4a -m macro -u UIsession -t nThreads G4endl;G4cerr note: -t option is available onl
4、y for multi-threaded mode. 7 ) PrintUsage(); return 1;G4String macro;G4String session;#ifdef G4MULTITHREADEDG4int nThreads = 0;#endiffor ( G4int i=1; i 0 ) runManager-SetNumberOfThreads(nThreads);#elseauto runManager = new G4RunManager;#endif/ Set mandatory initialization classes/auto detConstructio
5、n = new B4DetectorConstruction(); runManager-SetUserInitialization(detConstruction);auto physicsList = new FTFP_BERT; runManager-SetUserInitialization(physicsList);auto actionInitialization = new B4aActionInitialization(detConstruction); runManager-SetUserInitialization(actionInitialization);/ Initi
6、alize visualization/auto visManager = new G4VisExecutive;/ G4VisExecutive can take a verbosity argument - see /vis/verbose guidance./ G4VisManager* visManager = new G4VisExecutive(Quiet); visManager-Initialize();/ Get the pointer to the User Interface managerauto UImanager = G4UImanager:GetUIpointer
7、);/ Process macro or start UI session/if ( macro.size() ) / batch modeG4String command = /control/execute ;UImanager-ApplyCommand(command+macro);else / interactive mode : define UI sessionUImanager-ApplyCommand(/control/execute init_vis.mac);if (ui-IsGUI() UImanager-ApplyCommand(/control/execute gu
8、i.mac);ui-SessionStart();delete ui;/ Job termination/ Free the store: user actions, physics_list and detector_description are/ owned and deleted by the run manager, so they should not be deleted/ in the main() program !delete visManager;delete runManager;用户在终端输入字符串个数大于 7 则调用 PrintUsage 提示出错,并返回 1,跳出
9、 main()函数。通过for循环对输入字符串扫描识别,若程序名后字符串为-m”给macro赋值该字符串后一个字符串;若程序名后字符串为“ -u”给session赋值该字符串后一个字符串,否则调用PrintUsage提示出错,并返回 1跳出main()函数。定义一个 UI操作类对象,ui指针 初始化空指针,若用户没有调用 macro 控制文件,则 new 一个 ui 指针,分内存空间辟。初 始化探测器、物理过程以及用户行为,其中,用户行为与探测器构造有关。new 一个可视化管理器并对其初始化。定义一个 UI 管理类,通过判断条件 macro 是否为空,来决定执行交 互式界面模式还是批处理模式。
10、最后delete所有管理类,释放内存。B4DetectorConstruction.hh#ifndef B4DetectorConstruction_h #define B4DetectorConstruction_h 1#include G4VUserDetectorConstruction.hh#include globals.hhclass G4VPhysicalVolume;class G4GlobalMagFieldMessenger;class B4DetectorConstruction : public G4VUserDetectorConstructionpublic:B4D
11、etectorConstruction();virtual B4DetectorConstruction();public:virtual G4VPhysicalV olume* Construct();virtual void ConstructSDandField();/ get methods/const G4VPhysicalVolume* GetAbsorberPV() const; /const 修饰指针, 指针的内存空间 数据不能改变;函数重载const G4VPhysicalV olume* GetGapPV() const;private:/ methods/void Def
12、ineMaterials();G4VPhysicalV olume* DefineV olumes();/ data members/static G4ThreadLocal G4GlobalMagFieldMessenger* fMagFieldMessenger;/ magnetic field messengerG4VPhysicalV olume* fAbsorberPV; / the absorber physical volumeG4VPhysicalV olume* fGapPV; / the gap physical volumeG4bool fCheckOverlaps; /
13、 option to activate checking of volumes overlaps;/ inline functionsinline const G4VPhysicalV olume* B4DetectorConstruction:GetAbsorberPV() const return fAbsorberPV;inline const G4VPhysicalV olume* B4DetectorConstruction:GetGapPV() const return fGapPV;B4DetectorConstruction 继承于 G4VUserDetectorConstru
14、ction 基类, 声明构造函数、 析构 函数,Con struct。返回物理体。虚方法Con structSDa ndField()用于定义SD探测器以及空间内 的电磁场。 GetAbsorberPV() 和 GetGapPV() 为自定义的内联函数在类体外进行定义,分别返 回物理体 fAbsorberPV 和 fGapPV (私有数据成员) 。 G4GlobalMagFieldMessenger 类用于描 述电磁场的大小、方向等信息。B4DetectorConstruction.ccB4DetectorConstruction:B4DetectorConstruction(): G4VUs
15、erDetectorConstruction(),fAbsorberPV(nullptr),fGapPV(nullptr),fCheckOverlaps(true)/oooOO0OOooooooOO0OOooooooOO0OOooooooOO0OOoooB4DetectorConstruction:B4DetectorConstruction()/oooOO0OOooooooOO0OOooooooOO0OOooooooOO0OOoooG4VPhysicalVolume* B4DetectorConstruction:Construct() / Define materialsDefineMat
16、erials();/ Define volumesreturn DefineVolumes();/oooOO0OOooooooOO0OOooooooOO0OOooooooOO0OOooovoid B4DetectorConstruction:DefineMaterials()/ Lead material defined using NIST Managerauto nistManager = G4NistManager:Instance(); nistManager-FindOrBuildMaterial(G4_Pb);/ Liquid argon materialG4double a; /
17、 mass of a mole;G4double z; / z=mean number of protons;G4double density;new G4Material(liquidArgon, z=18., a= 39.95*g/mole, density= 1.390*g/cm3);/ The argon by NIST Manager is a gas with a different density/ Vacuumnew G4Material(Galactic, z=1., a=1.01*g/mole,density= universe_mean_density, kStateGa
18、s, 2.73*kelvin, 3.e-18*pascal);/ Print materialsG4cout *(G4Material:GetMaterialTable() G4endl;/oooOO0OOooooooOO0OOooooooOO0OOooooooOO0OOoooG4VPhysicalVolume* B4DetectorConstruction:DefineV olumes()/ Geometry parametersG4int nofLayers = 10;/ 定义有多少层 copynumberG4double absoThickness = 10.*mm;G4double g
19、apThickness = 5.*mm;G4double calorSizeXY = 10.*cm;/整体厚度auto layerThickness = absoThickness + gapThickness;/一层厚度auto calorThickness = nofLayers * layerThickness;auto worldSizeXY = 1.2 * calorSizeXY;auto worldSizeZ = 1.2 * calorThickness;/ Get materialsauto defaultMaterial = G4Material:GetMaterial(Gal
20、actic);auto absorberMaterial = G4Material:GetMaterial(G4_Pb);auto gapMaterial = G4Material:GetMaterial(liquidArgon);if ( ! defaultMaterial | ! absorberMaterial | ! gapMaterial ) G4ExceptionDescription msg;msg Cannot retrieve materials already defined.;G4Exception(B4DetectorConstruction:DefineV olume
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 蒙特卡洛 模拟 GEANT4 例子 exampleB4a 源码 解读
