Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Lesson1.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # ==================================================================
3 # python script for "measurement" of mass attenuation coefficient
4 #
5 #
6 # - using site-module packages
7 # ==================================================================
8 from Geant4 import *
9 import g4py.NISTmaterials
10 import g4py.ezgeom
11 from g4py.ezgeom import G4EzVolume
12 import g4py.EMSTDpl
13 import g4py.ParticleGun
14 from time import *
15 import sys
16 
17 # ==================================================================
18 # intialize
19 # ==================================================================
20 def Configure():
21  # ------------------------------------------------------------------
22  # setup for materials
23  # ------------------------------------------------------------------
24  # simple materials for Qgeom
25  g4py.NISTmaterials.Construct()
26 
27  # ------------------------------------------------------------------
28  # setup for geometry
29  # ------------------------------------------------------------------
30  #g4py.Qgeom.Construct()
31  g4py.ezgeom.Construct() # initialize
32 
33  # ------------------------------------------------------------------
34  # setup for physics list
35  # ------------------------------------------------------------------
36  g4py.EMSTDpl.Construct()
37 
38  # ------------------------------------------------------------------
39  # setup for primary generator action
40  # ------------------------------------------------------------------
41  g4py.ParticleGun.Construct()
42  gControlExecute("gun.mac")
43 
44 # ==================================================================
45 # constructing geometry
46 # ==================================================================
48  print "* Constructing geometry..."
49  # reset world material
50  global absorber
51  air= G4Material.GetMaterial("G4_AIR", 1)
52  galactic = G4Material.GetMaterial("G4_Galactic", 1)
53  absorber = {} # material's dictionary to be used by a radiobutton
54  aluminum = G4Material.GetMaterial("G4_Al", 1)
55  iron = G4Material.GetMaterial("G4_Fe", 1)
56  silver = G4Material.GetMaterial("G4_Ag", 1)
57  gold = G4Material.GetMaterial("G4_Au", 1)
58  lead = G4Material.GetMaterial("G4_Pb", 1)
59  water = G4Material.GetMaterial("G4_WATER", 1)
60  absorber = {"air":air, "aluminum":aluminum, "iron":iron, "lead":lead, "water":water, "gold":gold}
61  g4py.ezgeom.SetWorldMaterial(galactic)
62  g4py.ezgeom.ResizeWorld(120.*cm, 120.*cm, 100.*cm)
63  # water phantom
64  global water_phantom, water_phantom_pv
65 
66  water_phantom= G4EzVolume("WaterPhantom")
67  water_phantom.CreateBoxVolume(water, 110.*cm, 110.*cm, 10.*cm)
68 
69  water_phantom_pv = water_phantom.PlaceIt(G4ThreeVector(0.,0.,0.*cm))
70 
71 # ==================================================================
72 # main
73 # ==================================================================
74 # ------------------------------------------------------------------
75 # randum number
76 # ------------------------------------------------------------------
77 print "Random numbers..."
78 rand_engine= Ranlux64Engine()
79 HepRandom.setTheEngine(rand_engine)
80 HepRandom.setTheSeed(20050830L)
81 
82 # setup...
83 
84 Configure()
86 
87 # ------------------------------------------------------------------
88 # go...
89 # ------------------------------------------------------------------
90 gRunManager.Initialize()
91 
92 # visualization not here but after "Start a run" button
93 gControlExecute("oglx.mac")
94 #gControlExecute("vrml.mac")
95 
96 # creating widgets using grid layout
97 
98 from Tkinter import *
99 
100 class App(Frame):
101 
102 
103  def init(self):
104 
105 #title and header row=0, 1
106  title = Label(self, text="Geant4Py for Education @ H. Yoshida Naruto Univ. of Education")
107  title.grid(row=0, column=1, columnspan=5)
108  header = Label(self, text="Measurement of Mass Attenuation Coefficient")
109  header.grid(row=1, column=1, columnspan=5)
110 
111 #material selection row=2
112  materialLabel = Label(self, bg="green", text="Material")
113  materialLabel.grid(row=2, column=0, sticky=W)
114  self.materialVar = StringVar()
115  self.materialVar.set("water")
116  ra1 = { }
117  pos=1
118  for i in absorber.keys():
119  ra1[i] = Radiobutton(self, text=i, variable=self.materialVar, value=i)
120  ra1[i].grid(row=2, column=pos, sticky=W)
121  pos=pos+1
122 
123 #absorber thickness row=3
124  thickLabel = Label(self, bg="green", text="Thickness (mm)")
125  self.thickVar = DoubleVar()
126  self.thickVar.set(100.0)
127  thick = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., resolution=0.05, tickinterval=10.0, digits=4, variable=self.thickVar)
128  thickLabel.grid(row=3, column=0, sticky=W)
129  thick.grid(row=3, column=1, columnspan=5, sticky=W)
130 
131 #get logical volume and set its half length
132  self.solid = g4py.ezgeom.G4EzVolume.GetSold(water_phantom)
133 
134 #particle row=4
135  particleLabel = Label(self, bg="green", text="Particle")
136  particleLabel.grid(row=4, column=0, sticky=W)
137  self.particleVar = StringVar()
138  self.particleVar.set("gamma")
139  ra1 = { }
140  pos1=1
141  for i in ("gamma", "e-"):
142  ra1[i] = Radiobutton(self, text=i, variable=self.particleVar, value=i)
143  ra1[i].grid(row=4, column=pos1, sticky=W)
144  pos1=pos1+1
145 
146 #energy row=5
147  energyLabel = Label(self, bg="green", text="Energy (MeV)")
148 
149  self.energyVar=StringVar()
150  self.energyVar.set(1)
151  energy = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., tickinterval=10.0, resolution=0.1, variable=self.energyVar, digits=4 )
152  energyLabel.grid(row=5, column=0, sticky=W)
153  energy.grid(row=5, column=1, columnspan=5, sticky=W)
154 
155 #number of event row=6
156  eventLabel = Label(self, bg="green", text="Events")
157  self.eventVar=IntVar()
158  event = Scale(self, orient=HORIZONTAL, length=400, from_=1, to=100, tickinterval=10, resolution=1, variable=self.eventVar )
159  eventLabel.grid(row=6, column=0, sticky=W)
160  event.grid(row=6, column=1, columnspan=5, sticky=W)
161 
162 #start a run button row=7
163  startBut = Button(self, bg="orange", text="Start a run", command=self.cmd_beamOn)
164  startBut.grid(row=0, column=0, sticky=W)
165 
166 #Zoom in/out Pan X Y row=8
167  visLabel = Label(self, text="viewer", bg="orange")
168  expandBut = Button(self, text="Zoom in", command=self.cmd_expand)
169  shrinkBut = Button(self, text="Zoom out", command=self.cmd_shrink)
170  visLabel.grid(row=8, column=0, sticky=W)
171  expandBut.grid(row=8, column=1, sticky=W)
172  shrinkBut.grid(row=8, column=2, sticky=W)
173 
174  upBut = Button(self, text="Up", command=self.cmd_up)
175  downBut = Button(self, text="Down", command=self.cmd_down)
176  upBut.grid(row=8, column=3, sticky=W)
177  downBut.grid(row=8, column=4, sticky=W)
178 
179  leftBut = Button(self, text="Left", command=self.cmd_left)
180  rightBut = Button(self, text="Right", command=self.cmd_right)
181  leftBut.grid(row=8, column=5, sticky=W)
182  rightBut.grid(row=8, column=6, sticky=W)
183 # later
184 # resetBut = Button(self, text="Reset", command=self.cmd_reset)
185 # resetBut.grid(row=8, column=7, sticky=W)
186 
187 
188 # panLabel = Label(self, text="Pan X Y (mm)")
189 # self.panXYVar = StringVar()
190 # panXYEnt = Entry(self, textvariable=self.panXYVar)
191 # panBut = Button(self, bg="orange", text="OK", command=self.cmd_pan)
192 # panLabel.grid(row=8, column=3, sticky=W)
193 # panXYEnt.grid(row=8, column=4)
194 # panBut.grid(row=8, column=5)
195 #Geant4 command entry row = 9
196 # g4comLabel = Label(self, text="Geant4 command")
197 # self.g4commandVar = StringVar()
198 # commandEntry = Entry(self, textvariable=self.g4commandVar)
199 # comBut = Button(self, bg="orange", text="Execute", command=self.cmd_g4command)
200 # g4comLabel.grid(row=9, column=0, sticky=W)
201 # commandEntry.grid(row=9, column=1, columnspan=4, sticky=E+W)
202 # comBut.grid(row=9, column=5)
203 
204 #exit row = 10
205  exitBut = Button(self, bg="red", text="End all", command=sys.exit)
206  exitBut.grid(row=0, column=6, sticky=W)
207 
208 #on Run butto do...
209  def cmd_beamOn(self):
210  materialChosen = self.materialVar.get()
211  water_phantom.SetMaterial(absorber[materialChosen])
212 
213  if materialChosen == "water":
214  water_phantom.SetColor(0., 0.9, 1.0)
215 
216  if materialChosen == "air":
217  water_phantom.SetColor(0.9, 0.9, 1.0)
218 
219  if materialChosen == "lead":
220  water_phantom.SetColor(0.2, 0.2, 0.2)
221 
222  if materialChosen == "iron":
223  water_phantom.SetColor(0.7, 0.5, 0.7)
224 
225  if materialChosen == "aluminum":
226  water_phantom.SetColor(.7, 0.9, 1.0)
227 
228  if materialChosen == "gold":
229  water_phantom.SetColor(1., 0.9, .0)
230 
231  self.solid.SetZHalfLength(self.thickVar.get() * mm/2.0)
232 # gControlExecute("oglx.mac") #draw for each run
233  gApplyUICommand("/vis/viewer/flush")
234 
235  self.cmd_particle(self.particleVar.get())
236  self.cmd_energy(self.energyVar.get())
237 # TODO later to reflesh text
238  gApplyUICommand("/vis/scene/add/text 0 610 610 mm 20 0 0 " + " ")
239  gApplyUICommand("/vis/scene/add/text 0 610 610 mm 20 0 0 " + self.materialVar.get() + " = " + str(self.thickVar.get()) + "mm " + self.particleVar.get() + " = "+self.energyVar.get() + "MeV")
240 
241  eventNum = self.eventVar.get()
242  for i in range(eventNum):
243  gunYZpos = str(i-eventNum/2) + ". -20. cm"
244  gApplyUICommand("/gun/position 0. " + gunYZpos)
245  gRunManager.BeamOn(1)
246  sleep(0.01)
247 # self.cmd_expand() #Zoom in to the last diaplayed OGLSX
248 # self.cmd_shrink()
249 
250 
251 
252  def cmd_g4command(self):
253  gApplyUICommand(self.g4commandVar.get())
254 
255  def cmd_particle(self, particle):
256  gApplyUICommand("/gun/particle " + particle)
257 
258 
259  def cmd_energy(self, penergy):
260  gApplyUICommand("/gun/energy " + penergy + " MeV")
261 
262 
263  def cmd_expand(self):
264  gApplyUICommand("/vis/viewer/zoom 1.2")
265 
266  def cmd_up(self):
267  gApplyUICommand("/vis/viewer/pan " + " 0. 10. mm")
268 
269  def cmd_down(self):
270  gApplyUICommand("/vis/viewer/pan " + " 0. -10. mm")
271 
272  def cmd_right(self):
273  gApplyUICommand("/vis/viewer/pan " + " -1. 0. mm")
274 
275  def cmd_left(self):
276  gApplyUICommand("/vis/viewer/pan " + " 1. 0. mm")
277 
278 
279  def cmd_shrink(self):
280  gApplyUICommand("/vis/viewer/zoom 0.8")
281 
282 
283 # def cmd_reset(self):
284 # gApplyUICommand("/vis/viewer/pan " + " 0. 0. mm")
285 
286 
287  def __init__(self, master=None):
288  Frame.__init__(self, master)
289  self.init()
290  self.grid()
291 
292 
293 app = App()
294 app.mainloop()
def __init__
Definition: Lesson1.py:287
def cmd_right
Definition: Lesson1.py:272
static G4Material * GetMaterial(const G4String &name, G4bool warning=true)
Definition: G4Material.cc:578
def cmd_expand
Definition: Lesson1.py:263
def cmd_left
Definition: Lesson1.py:275
def cmd_g4command
Definition: Lesson1.py:252
def cmd_down
Definition: Lesson1.py:269
def Configure
Definition: Lesson1.py:20
def init
Definition: Lesson1.py:103
def cmd_energy
Definition: Lesson1.py:259
def ConstructGeom
Definition: Lesson1.py:47
def cmd_particle
Definition: Lesson1.py:255
def cmd_beamOn
Definition: Lesson1.py:209
def cmd_shrink
Definition: Lesson1.py:279
def cmd_up
Definition: Lesson1.py:266