source: trunk/environments/g4py/examples/education/lesson1/Lesson1withN03.py@ 1345

Last change on this file since 1345 was 1337, checked in by garnier, 15 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 10.8 KB
Line 
1#!/usr/bin/python
2# ==================================================================
3# python script for "measurement" of mass attenuation coefficient
4#
5#
6# - using site-module packages
7# ==================================================================
8from Geant4 import *
9import g4py.NISTmaterials
10import g4py.ezgeom
11from g4py.ezgeom import G4EzVolume
12import g4py.ExN03pl
13import g4py.ParticleGun
14from time import *
15import sys
16
17# ==================================================================
18# intialize
19# ==================================================================
20def 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.ExN03pl.Construct()
37
38 # ------------------------------------------------------------------
39 # setup for primary generator action
40 # ------------------------------------------------------------------
41 g4py.ParticleGun.Construct()
42 gControlExecute("gun.mac")
43
44# ==================================================================
45# constructing geometry
46# ==================================================================
47def ConstructGeom():
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# ------------------------------------------------------------------
77print "Random numbers..."
78rand_engine= Ranlux64Engine()
79HepRandom.setTheEngine(rand_engine)
80HepRandom.setTheSeed(20050830L)
81
82# setup...
83
84Configure()
85ConstructGeom()
86
87
88# ------------------------------------------------------------------
89# go...
90# ------------------------------------------------------------------
91gRunManager.Initialize()
92
93# visualization not here but after "Start a run" button
94gControlExecute("oglx.mac")
95#gControlExecute("vrml.mac")
96
97# creating widgets using grid layout
98
99from Tkinter import *
100
101class App(Frame):
102
103
104
105 def init(self):
106
107#title and header row=0, 1
108 title = Label(self, text="Geant4Py for Education @ H. Yoshida Naruto Univ. of Education")
109 title.grid(row=0, column=1, columnspan=5)
110 header = Label(self, text="Measurement of Mass Attenuation Coefficient")
111 header.grid(row=1, column=1, columnspan=5)
112
113#material selection row=2
114 materialLabel = Label(self, bg="green", text="Material")
115 materialLabel.grid(row=2, column=0, sticky=W)
116 self.materialVar = StringVar()
117 self.materialVar.set("water")
118 ra1 = { }
119 pos=1
120 for i in absorber.keys():
121 ra1[i] = Radiobutton(self, text=i, variable=self.materialVar, value=i)
122 ra1[i].grid(row=2, column=pos, sticky=W)
123 pos=pos+1
124
125#absorber thickness row=3
126 thickLabel = Label(self, bg="green", text="Thickness (mm)")
127 self.thickVar = DoubleVar()
128 self.thickVar.set(100.0)
129 thick = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., resolution=0.05, tickinterval=10.0, digits=4, variable=self.thickVar)
130 thickLabel.grid(row=3, column=0, sticky=W)
131 thick.grid(row=3, column=1, columnspan=5, sticky=W)
132
133#get logical volume and set its half length
134 self.solid = g4py.ezgeom.G4EzVolume.GetSold(water_phantom)
135
136#particle row=4
137 particleLabel = Label(self, bg="green", text="Particle")
138 particleLabel.grid(row=4, column=0, sticky=W)
139 self.particleVar = StringVar()
140 self.particleVar.set("gamma")
141 ra1 = { }
142 pos1=1
143 for i in ("gamma", "e-", "proton"):
144 ra1[i] = Radiobutton(self, text=i, variable=self.particleVar, value=i)
145 ra1[i].grid(row=4, column=pos1, sticky=W)
146 pos1=pos1+1
147
148#energy row=5
149 energyLabel = Label(self, bg="green", text="Energy (MeV)")
150
151 self.energyVar=StringVar()
152 self.energyVar.set(1)
153 energy = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., tickinterval=10.0, resolution=0.1, variable=self.energyVar, digits=4 )
154 energyLabel.grid(row=5, column=0, sticky=W)
155 energy.grid(row=5, column=1, columnspan=5, sticky=W)
156
157#number of event row=6
158 eventLabel = Label(self, bg="green", text="Events")
159 self.eventVar=IntVar()
160 event = Scale(self, orient=HORIZONTAL, length=400, from_=1, to=100, tickinterval=10, resolution=1, variable=self.eventVar )
161 eventLabel.grid(row=6, column=0, sticky=W)
162 event.grid(row=6, column=1, columnspan=5, sticky=W)
163
164#start a run button row=7
165 startBut = Button(self, bg="orange", text="Start a run", command=self.cmd_beamOn)
166 startBut.grid(row=0, column=0, sticky=W)
167
168
169#on off processes
170
171 processLabel = Label(self, text="Process On/Off", bg="green")
172 processLabel.grid(row=8, column=0, sticky=W)
173 procTab = {}
174 self.processList = ["phot", "compt", "msc", "conv", "eIoni", "eBrem", "hIoni"]
175 self.processVar = {}
176 pos = 1
177 for i in self.processList:
178 self.processVar[i] = IntVar()
179 procTab[i] = Checkbutton(self, text=i, variable=self.processVar[i], command=self.cmd_setProcess)
180 procTab[i].grid(row=8, column=pos, sticky=W)
181 pos = pos + 1
182 procTab[i].select()
183
184
185#Zoom in/out Pan X Y row=9
186 visLabel = Label(self, text="viewer", bg="orange")
187 expandBut = Button(self, text="Zoom in", command=self.cmd_expand)
188 shrinkBut = Button(self, text="Zoom out", command=self.cmd_shrink)
189 visLabel.grid(row=9, column=0, sticky=W)
190 expandBut.grid(row=9, column=1, sticky=W)
191 shrinkBut.grid(row=9, column=2, sticky=W)
192
193 upBut = Button(self, text="Up", command=self.cmd_up)
194 downBut = Button(self, text="Down", command=self.cmd_down)
195 upBut.grid(row=9, column=3, sticky=W)
196 downBut.grid(row=9, column=4, sticky=W)
197
198 leftBut = Button(self, text="Left", command=self.cmd_left)
199 rightBut = Button(self, text="Right", command=self.cmd_right)
200 leftBut.grid(row=9, column=5, sticky=W)
201 rightBut.grid(row=9, column=6, sticky=W)
202# later
203# resetBut = Button(self, text="Reset", command=self.cmd_reset)
204# resetBut.grid(row=8, column=7, sticky=W)
205
206
207# panLabel = Label(self, text="Pan X Y (mm)")
208# self.panXYVar = StringVar()
209# panXYEnt = Entry(self, textvariable=self.panXYVar)
210# panBut = Button(self, bg="orange", text="OK", command=self.cmd_pan)
211# panLabel.grid(row=8, column=3, sticky=W)
212# panXYEnt.grid(row=8, column=4)
213# panBut.grid(row=8, column=5)
214#Geant4 command entry row = 9
215# g4comLabel = Label(self, text="Geant4 command")
216# self.g4commandVar = StringVar()
217# commandEntry = Entry(self, textvariable=self.g4commandVar)
218# comBut = Button(self, bg="orange", text="Execute", command=self.cmd_g4command)
219# g4comLabel.grid(row=9, column=0, sticky=W)
220# commandEntry.grid(row=9, column=1, columnspan=4, sticky=E+W)
221# comBut.grid(row=9, column=5)
222
223#exit row = 10
224 exitBut = Button(self, bg="red", text="End all", command=sys.exit)
225 exitBut.grid(row=0, column=6, sticky=W)
226
227#on Run butto do...
228 def cmd_beamOn(self):
229 materialChosen = self.materialVar.get()
230 water_phantom.SetMaterial(absorber[materialChosen])
231
232 if materialChosen == "water":
233 water_phantom.SetColor(0., 0.9, 1.0)
234
235 if materialChosen == "air":
236 water_phantom.SetColor(0.9, 0.9, 1.0)
237
238 if materialChosen == "lead":
239 water_phantom.SetColor(0.2, 0.2, 0.2)
240
241 if materialChosen == "iron":
242 water_phantom.SetColor(0.7, 0.5, 0.7)
243
244 if materialChosen == "aluminum":
245 water_phantom.SetColor(.7, 0.9, 1.0)
246
247 if materialChosen == "gold":
248 water_phantom.SetColor(1., 0.9, .0)
249
250 self.solid.SetZHalfLength(self.thickVar.get() * mm/2.0)
251# gControlExecute("oglx.mac") #draw for each run
252 gApplyUICommand("/vis/viewer/flush")
253
254 self.cmd_particle(self.particleVar.get())
255 self.cmd_energy(self.energyVar.get())
256# TODO later to reflesh text
257 gApplyUICommand("/vis/scene/add/text 0 610 610 mm 20 0 0 " + " ")
258 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")
259
260 eventNum = self.eventVar.get()
261 for i in range(eventNum):
262 gunYZpos = str(i-eventNum/2) + ". -20. cm"
263 gApplyUICommand("/gun/position 0. " + gunYZpos)
264 gRunManager.BeamOn(1)
265 sleep(0.01)
266# self.cmd_expand() #Zoom in to the last diaplayed OGLSX
267# self.cmd_shrink()
268
269
270
271 def cmd_g4command(self):
272 gApplyUICommand(self.g4commandVar.get())
273
274 def cmd_particle(self, particle):
275 gApplyUICommand("/gun/particle " + particle)
276
277 def cmd_setProcess(self):
278 for i in self.processList:
279 if self.processVar[i].get() == 0:
280 gProcessTable.SetProcessActivation(i, 0)
281 else:
282 gProcessTable.SetProcessActivation(i, 1)
283
284 def cmd_energy(self, penergy):
285 gApplyUICommand("/gun/energy " + penergy + " MeV")
286
287
288 def cmd_expand(self):
289 gApplyUICommand("/vis/viewer/zoom 1.2")
290
291 def cmd_up(self):
292 gApplyUICommand("/vis/viewer/pan " + " 0. 10. mm")
293
294 def cmd_down(self):
295 gApplyUICommand("/vis/viewer/pan " + " 0. -10. mm")
296
297 def cmd_right(self):
298 gApplyUICommand("/vis/viewer/pan " + " -1. 0. mm")
299
300 def cmd_left(self):
301 gApplyUICommand("/vis/viewer/pan " + " 1. 0. mm")
302
303
304 def cmd_shrink(self):
305 gApplyUICommand("/vis/viewer/zoom 0.8")
306
307
308# def cmd_reset(self):
309# gApplyUICommand("/vis/viewer/pan " + " 0. 0. mm")
310
311
312 def __init__(self, master=None):
313 Frame.__init__(self, master)
314 self.init()
315 self.grid()
316
317
318app = App()
319app.mainloop()
Note: See TracBrowser for help on using the repository browser.