1 | #!/usr/bin/python |
---|
2 | # ================================================================== |
---|
3 | # python script for Geant4Py test |
---|
4 | # |
---|
5 | # gtest01 |
---|
6 | # - check basic control flow |
---|
7 | # ================================================================== |
---|
8 | from Geant4 import * |
---|
9 | import gtest01 |
---|
10 | import ROOT |
---|
11 | |
---|
12 | # ================================================================== |
---|
13 | # ROOT PART # |
---|
14 | # ================================================================== |
---|
15 | |
---|
16 | # ------------------------------------------------------------------ |
---|
17 | def init_root(): |
---|
18 | # ------------------------------------------------------------------ |
---|
19 | ROOT.gROOT.Reset() |
---|
20 | |
---|
21 | # plot style |
---|
22 | ROOT.gStyle.SetTextFont(82) |
---|
23 | ROOT.gStyle.SetTitleFont(82, "X") |
---|
24 | ROOT.gStyle.SetLabelFont(82, "X") |
---|
25 | ROOT.gStyle.SetTitleFont(82, "Y") |
---|
26 | ROOT.gStyle.SetLabelFont(82, "Y") |
---|
27 | |
---|
28 | #ROOT.gStyle.SetOptTitle(0) |
---|
29 | ROOT.gStyle.SetErrorX(0) |
---|
30 | |
---|
31 | canvas= ROOT.TCanvas("g4py_plots", |
---|
32 | "Geant4Py Sample Plots", |
---|
33 | 620, 30, 600, 400) |
---|
34 | |
---|
35 | #canvas.Divide(2,2); |
---|
36 | #canvas.SetFillColor(29) |
---|
37 | |
---|
38 | canvas.SetGrid() |
---|
39 | |
---|
40 | return canvas |
---|
41 | |
---|
42 | # ------------------------------------------------------------------ |
---|
43 | def hini(): |
---|
44 | # ------------------------------------------------------------------ |
---|
45 | global hist1 |
---|
46 | hist1= ROOT.TH1D("dE/dx/step", "dE/dx", 100, 0., 2000.) |
---|
47 | hist1.SetXTitle("(keV)") |
---|
48 | |
---|
49 | |
---|
50 | # ------------------------------------------------------------------ |
---|
51 | def hshow(): |
---|
52 | # ------------------------------------------------------------------ |
---|
53 | hist1.Draw() |
---|
54 | |
---|
55 | # ================================================================== |
---|
56 | # Geant4 PART # |
---|
57 | # ================================================================== |
---|
58 | |
---|
59 | # ================================================================== |
---|
60 | # user actions in python |
---|
61 | # ================================================================== |
---|
62 | class MyPrimaryGeneratorAction(G4VUserPrimaryGeneratorAction): |
---|
63 | "My Primary Generator Action" |
---|
64 | |
---|
65 | def __init__(self): |
---|
66 | G4VUserPrimaryGeneratorAction.__init__(self) |
---|
67 | self.particleGun= G4ParticleGun(1) |
---|
68 | |
---|
69 | def GeneratePrimaries(self, event): |
---|
70 | self.particleGun.GeneratePrimaryVertex(event) |
---|
71 | |
---|
72 | # ------------------------------------------------------------------ |
---|
73 | class MyRunAction(G4UserRunAction): |
---|
74 | "My Run Action" |
---|
75 | |
---|
76 | def BeginOfRunAction(self, run): |
---|
77 | print "*** #event to be processed (BRA)=", |
---|
78 | run.numberOfEventToBeProcessed |
---|
79 | |
---|
80 | def EndOfRunAction(self, run): |
---|
81 | print "*** run end run(ERA)=", run.runID |
---|
82 | |
---|
83 | # ------------------------------------------------------------------ |
---|
84 | class MyEventAction(G4UserEventAction): |
---|
85 | "My Event Action" |
---|
86 | |
---|
87 | def BeginOfEventAction(self, event): |
---|
88 | print "*** current event (BEA)=", event.eventID |
---|
89 | |
---|
90 | def EndOfEventAction(self, event): |
---|
91 | print "*** current event (EEA)=", event.eventID |
---|
92 | |
---|
93 | # ------------------------------------------------------------------ |
---|
94 | class MySteppingAction(G4UserSteppingAction): |
---|
95 | "My Stepping Action" |
---|
96 | |
---|
97 | def UserSteppingAction(self, step): |
---|
98 | #print "*** dE/dx in current step=", step.GetTotalEnergyDeposit() |
---|
99 | dedx= step.GetTotalEnergyDeposit() |
---|
100 | if(dedx>0): |
---|
101 | hist1.Fill(dedx/HEPUnit.keV) |
---|
102 | |
---|
103 | # ================================================================== |
---|
104 | # main |
---|
105 | # ================================================================== |
---|
106 | g4pyCanvas= init_root() |
---|
107 | hini() |
---|
108 | |
---|
109 | app= gtest01.MyApplication() |
---|
110 | app.Configure() |
---|
111 | |
---|
112 | # set user actions... |
---|
113 | myPGA= MyPrimaryGeneratorAction() |
---|
114 | gRunManager.SetUserAction(myPGA) |
---|
115 | |
---|
116 | myRA= MyRunAction() |
---|
117 | gRunManager.SetUserAction(myRA) |
---|
118 | |
---|
119 | #myEA= MyEventAction() |
---|
120 | #gRunManager.SetUserAction(myEA) |
---|
121 | |
---|
122 | mySA= MySteppingAction() |
---|
123 | gRunManager.SetUserAction(mySA) |
---|
124 | |
---|
125 | |
---|
126 | # set particle gun |
---|
127 | #ApplyUICommand("/control/execute gun.mac") |
---|
128 | pg= myPGA.particleGun |
---|
129 | pg.SetParticleByName("e-") |
---|
130 | pg.SetParticleEnergy(200.*HEPUnit.MeV) |
---|
131 | pg.SetParticleMomentumDirection(G4ThreeVector(0.2, 0., 1.)) |
---|
132 | pg.SetParticlePosition(G4ThreeVector(0.,0.,-14.9)*HEPUnit.cm) |
---|
133 | |
---|
134 | # visualization |
---|
135 | ApplyUICommand("/control/execute vis.mac") |
---|
136 | |
---|
137 | # beamOn |
---|
138 | gRunManager.BeamOn(1000) |
---|
139 | |
---|
140 | # |
---|
141 | hshow() |
---|
142 | |
---|
143 | |
---|