Geant4-11
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
emcalc_gui.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3
4# ==================================================================
5# EM Calculator (GTK)
6#
7# Koichi Murakami (KEK/CRC)
8# ==================================================================
9from Geant4 import *
10import g4py.EMSTDpl
11import g4py.emcalculator
12import EmPlot
13import ROOT
14import sys
15from cStringIO import StringIO
16
17# ==================================================================
18# Geant4
19# ==================================================================
22 g4py.EMSTDpl.Construct()
23
24# -------------------------------------------------------------------
25# plot for chaged particles
26# -------------------------------------------------------------------
27def plot_charged(material, pname) :
28 EmPlot.SetMaterial(material)
29
30 # initialize G4 kernel
31 gRunManager.Initialize()
32 gRunManagerKernel.RunInitialization()
33
34 # energy
35 elist= []
36 for n in range(-3, 3):
37 for i in range(10,99):
38 elist.append(i/10.*10.**n *MeV)
39
40 # calculate stopping power
41 global mycout
42 mycout.close()
43 sys.stdout = mycout = StringIO()
44 dedx_list= g4py.emcalculator.CalculateDEDX(pname, material, elist, 1)
45 xlist_tot=[]
46 xlist_ioni=[]
47 xlist_brems=[]
48
49 for x in dedx_list:
50 xlist_tot.append((x[0], x[1]["tot"]/(MeV*cm2/g)))
51 xlist_ioni.append((x[0], x[1]["ioni"]/(MeV*cm2/g)))
52 xlist_brems.append((x[0], x[1]["brems"]/(MeV*cm2/g)))
53
54 # make plot
55 global myCanvas, aplot, bplot, cplot
56 myCanvas = EmPlot.init_root()
57 aplot = EmPlot.make_plot(xlist_tot, pname+" Stopping Power ("+material+")",
58 "dE/dX (MeV cm^{2}/g)")
59 bplot = EmPlot.make_plot(xlist_ioni, "Stopping Power ("+material+")",
60 "dE/dX (MeV cm^{2}/g)", 1)
61 cplot = EmPlot.make_plot(xlist_brems, "Stopping Power ("+material+")",
62 "dE/dX (MeV cm^{2}/g)", 3)
63 myCanvas.SaveAs("/tmp/sp.png")
64
65
66# -------------------------------------------------------------------
67# plot for gamma
68# -------------------------------------------------------------------
69def plot_gamma(material) :
70 EmPlot.SetMaterial(material)
71
72 # initialize G4 kernel
73 gRunManager.Initialize()
74 gRunManagerKernel.RunInitialization()
75
76 # energy
77 elist= []
78 for n in range(-3, 4):
79 for i in range(10,99):
80 elist.append(i/10.*10.**n *MeV)
81
82 # calculate cross sections
83 global mycout
84 mycout.close()
85 sys.stdout = mycout = StringIO()
86 xsection_list= g4py.emcalculator.CalculatePhotonCrossSection(material,
87 elist, 1)
88 xlist_tot=[]
89 xlist_comp=[]
90 xlist_pe=[]
91 xlist_conv=[]
92 for x in xsection_list:
93 xlist_tot.append((x[0]/MeV, x[1]["tot"]/(cm2/g)))
94 xlist_comp.append((x[0]/MeV, x[1]["compt"]/(cm2/g)))
95 xlist_pe.append((x[0]/MeV, x[1]["phot"]/(cm2/g)))
96 xlist_conv.append((x[0]/MeV, x[1]["conv"]/(cm2/g)))
97
98 # make plots
99 global myCanvas, aplot, bplot, cplot, dplot
100 myCanvas = EmPlot.init_root()
101 aplot = EmPlot.make_plot(xlist_tot, "Photon Cross Section ("+material+")",
102 "Cross Section (cm^{2}/g)")
103 bplo = EmPlot.make_plot(xlist_comp, "Photon Cross Section ("+material+")",
104 "Cross Section (cm^{2}/g)", 1)
105 cplot = EmPlot.make_plot(xlist_pe, "Photon Cross Section ("+material+")",
106 "Cross Section (cm^{2}/g)", 7)
107 dplot = EmPlot.make_plot(xlist_conv, "Photon Cross Section ("+material+")",
108 "Cross Section (cm^{2}/g)", 3)
109 myCanvas.SaveAs("/tmp/cs.png")
110
111
112# ==================================================================
113# GUI
114# ==================================================================
115import pygtk
116pygtk.require20()
117import gtk
118
119# -------------------------------------------------------------------
120# main window
121# -------------------------------------------------------------------
123 def __init__(self) :
124 self.__margin = 8
125
126 # main window
127 self.mainwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
128 self.mainwindow.set_title('Em Calculator')
129 self.mainwindow.set_position(gtk.WIN_POS_MOUSE)
130 self.mainwindow.set_default_size(500, 300)
131 self.mainwindow.connect('delete-event', lambda w,d: gtk.main_quit())
132 self.mainwindow.connect('destroy-event',lambda w,d: gtk.main_quit())
133
134 # components
135 header = self.create_header()
136
137 particle_frame = self.create_particle_frame()
138 particle_frame.set_border_width(self.__margin)
139
140 material_frame = self.create_material_frame()
141 material_frame.set_border_width(self.__margin)
142
143 separator = gtk.HSeparator()
144
145 action_box = self.create_action_box()
146 action_box.set_border_width(self.__margin)
147
148 # layout
149 vbox = gtk.VBox()
150 vbox.pack_start(header)
151 vbox.pack_start(particle_frame)
152 vbox.pack_start(material_frame)
153 vbox.pack_start(separator)
154 vbox.pack_start(action_box)
155
156 self.mainwindow.add(vbox)
157 self.mainwindow.show_all()
158
159 # text view
161
162 # error dialog
163 self.error_dialog = gtk.MessageDialog(parent=self.mainwindow,
164 buttons=gtk.BUTTONS_CLOSE, type=gtk.MESSAGE_ERROR,
165 message_format="Material is not defined in G4Nist materials")
166 self.error_dialog.connect("response", self.cb_close_dialog)
167
168 def create_header(self) :
169 hbox = gtk.HBox()
170
171 label = gtk.Label()
172 label.set_markup("<big><b>EM Calculator</b></big>")
173 hbox.pack_start(label)
174
175 label = gtk.Label()
176 text = """
177
178Shows
179 - stopping power for e/mu/proton
180 - cross sections for gamma
181"""
182 label.set_markup(text)
183 hbox.pack_start(label)
184
185 return hbox
186
188 frame = gtk.Frame("Particle")
189
190 hbox = gtk.HBox()
191 frame.add(hbox)
192 hbox.set_border_width(self.__margin)
193
194 button = gtk.RadioButton(None, "electron")
195 button.connect("toggled", self.cb_select_particle, "e-")
196 hbox.pack_start(button, True, True, 0)
197 button.show()
198 self.particle = "e-"
199
200 button = gtk.RadioButton(button, "positron")
201 button.connect("toggled", self.cb_select_particle, "e+")
202 hbox.pack_start(button, True, True, 0)
203 button.show()
204
205 button = gtk.RadioButton(button, "mu-")
206 button.connect("toggled", self.cb_select_particle, "mu-")
207 hbox.pack_start(button, True, True, 0)
208 button.show()
209
210 button = gtk.RadioButton(button, "mu+")
211 button.connect("toggled", self.cb_select_particle, "mu+")
212 hbox.pack_start(button, True, True, 0)
213 button.show()
214
215 button = gtk.RadioButton(button, "proton")
216 button.connect("toggled", self.cb_select_particle, "proton")
217 hbox.pack_start(button, True, True, 0)
218 button.show()
219
220 button = gtk.RadioButton(button, "gamma")
221 button.connect("toggled", self.cb_select_particle, "gamma")
222 hbox.pack_start(button, True, True, 0)
223 button.show()
224
225 return frame
226
228 frame = gtk.Frame("Material (G4Nist)")
229
230 hbox = gtk.HBox()
231 frame.add(hbox)
232 hbox.set_border_width(self.__margin)
233
234 self.material_list = [ "G4_Al", "G4_Si", "G4_Ar", "G4_Cu", "G4_Fe",
235 "G4_Ge", "G4_Ag", "G4_W", "G4_Au", "G4_Pb",
236 "G4_AIR", "G4_Galactic", "G4_WATER", "G4_CESIUM_IODIDE",
237 "G4_SODIUM_IODIDE", "G4_PLASTIC_SC_VINYLTOLUENE",
238 "G4_MYLAR" ]
239
240 self.material_combo = gtk.combo_box_entry_new_text()
241 hbox.pack_start(self.material_combo)
242 for name in self.material_list :
243 self.material_combo.append_text(name)
244 self.material_combo.set_active(0)
245 self.material = self.material_list[0]
246 self.material_combo.connect("changed", self.cb_select_material)
247
248 return frame
249
251 box = gtk.HButtonBox()
252 box.set_layout(gtk.BUTTONBOX_END)
253 box.set_spacing(self.__margin)
254
255 exec_button = gtk.Button(stock = gtk.STOCK_EXECUTE)
256 text_button = gtk.Button("Text View")
257 quit_button = gtk.Button(stock = gtk.STOCK_QUIT)
258 box.add(exec_button)
259 box.add(text_button)
260 box.add(quit_button)
261
262 exec_button.connect("clicked", self.cb_show_plot)
263 text_button.connect("clicked", self.cb_show_textview)
264 quit_button.connect("clicked", lambda w: gtk.main_quit())
265
266 return box
267
268 # callbacks
269 def cb_show_textview(self, widget, data=None) :
270 window = self.textview.get_window()
271 window.show_all()
272
273 def cb_select_particle(self, widget, data=None) :
274 self.particle = data
275
276 def cb_select_material(self, widget, data=None) :
277 entry = widget.get_child()
278 self.material = entry.get_text()
279
280 def cb_show_plot(self, widget, data=None) :
281 g4mate = gNistManager.FindOrBuildMaterial(self.material)
282 if (g4mate == None) :
283 self.error_dialog.show_all()
284 return
285
286 if (self.material_list.count(self.material) == 0) :
287 self.material_combo.append_text(self.material)
288
289 if (self.particle == "gamma" ) :
290 plot_gamma(self.material)
291 else :
292 plot_charged(self.material, self.particle)
293 self.textview.textbuffer.set_text(mycout.getvalue())
294
295 def cb_close_dialog(self, widget, data=None) :
296 widget.hide_all()
297
298
299# -------------------------------------------------------------------
300# text view
301# -------------------------------------------------------------------
302class TextView :
303 def __init__(self) :
304 self.__margin = 8
305 self.text_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
306 self.text_window.set_title('Value with Text')
307 self.text_window.set_position(gtk.WIN_POS_MOUSE)
308 self.text_window.set_default_size(500, 300)
309
310 vbox = gtk.VBox()
311 self.text_window.add(vbox)
312
313 sw = gtk.ScrolledWindow()
314 sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
315 sw.set_border_width(self.__margin)
316 vbox.pack_start(sw)
317
318 textview = gtk.TextView()
319 self.textbuffer = textview.get_buffer()
320 sw.add(textview)
321
322 hbox = gtk.HButtonBox()
323 hbox.set_layout(gtk.BUTTONBOX_END)
324 hbox.set_border_width(self.__margin)
325 vbox.pack_start(hbox, expand=False)
326
327 close_button = gtk.Button(stock = gtk.STOCK_CLOSE)
328 close_button.connect("clicked", self.cb_hide_window)
329 hbox.add(close_button)
330
331 def get_window(self) :
332 return self.text_window
333
334 def cb_hide_window(self, widget, data=None) :
335 self.text_window.hide_all()
336 return False
337
338
339# ==================================================================
340# main
341# ==================================================================
342def main() :
344
345 default_stdout = sys.stdout
346 global mycout
347 sys.stdout = mycout = StringIO()
348
349 # G4 setup
351
352 # start GUI
353 application = MainWindow()
354 gtk.main()
355
356 gTerminate()
357 sys.stdout = default_stdout
358
359
360if __name__ == "__main__":
361 try :
362 main()
363 except KeyboardInterrupt :
364 pass
def create_action_box(self)
Definition: emcalc_gui.py:250
def cb_show_textview(self, widget, data=None)
Definition: emcalc_gui.py:269
def create_material_frame(self)
Definition: emcalc_gui.py:227
def cb_show_plot(self, widget, data=None)
Definition: emcalc_gui.py:280
def create_header(self)
Definition: emcalc_gui.py:168
def create_particle_frame(self)
Definition: emcalc_gui.py:187
def cb_close_dialog(self, widget, data=None)
Definition: emcalc_gui.py:295
def cb_select_particle(self, widget, data=None)
Definition: emcalc_gui.py:273
def cb_select_material(self, widget, data=None)
Definition: emcalc_gui.py:276
def get_window(self)
Definition: emcalc_gui.py:331
def __init__(self)
Definition: emcalc_gui.py:303
def cb_hide_window(self, widget, data=None)
Definition: emcalc_gui.py:334
def Configure()
Definition: EmPlot.py:18
def init_root()
Definition: EmPlot.py:58
def make_plot(xlist, user_title, axis_titile, q_super_impose=0)
Definition: EmPlot.py:84
def SetMaterial(material_name)
Definition: EmPlot.py:25
def plot_gamma(material)
Definition: emcalc_gui.py:69
def main()
Definition: emcalc_gui.py:342
def plot_charged(material, pname)
Definition: emcalc_gui.py:27
def g4_configure()
Definition: emcalc_gui.py:20
def connect(endpoint="tcp://127.0.0.1:5555")
Definition: g4zmq.py:15
void SetG4PyCoutDestination()
Definition: pyglobals.cc:49
def gTerminate()
Definition: __init__.py:214