Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
demo.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # ==================================================================
3 # python script for testem0 python version
4 #
5 # ==================================================================
6 import Geant4 as g4
7 import testem0
8 
9 # ==================================================================
10 # user actions in python
11 # ==================================================================
12 
13 
14 # ==================================================================
15 # main
16 # ==================================================================
17 
18 myDC= testem0.DetectorConstruction()
19 g4.gRunManager.SetUserInitialization(myDC)
20 
21 myPL= testem0.PhysicsList()
22 g4.gRunManager.SetUserInitialization(myPL)
23 
24 # set user actions...
25 myPGA= testem0.PrimaryGeneratorAction(myDC)
26 g4.gRunManager.SetUserAction(myPGA)
27 
28 myRA= testem0.RunAction(myDC,myPGA)
29 
30 # set user action classes
31 g4.gRunManager.SetUserAction(myRA)
32 
33 
34 
35 g4.gRunManager.Initialize()
36 
37 pg = g4.G4ParticleGun()
38 
39 materialList = testem0.getMaterialTable();
40 
41 particleList = testem0.getParticleTable()
42 
43 enrgyList = ["eV","keV","MeV","GeV","TeV","PeV"]
44 
45 cutsList = ["um", "mm" , "cm", "m", "km"]
46 
47 
48 
49 
50 # GUI
51 
52 from Tkinter import *
53 class App(Frame):
54 
55 
56  def init(self):
57 
58 # title and header
59  title = Label(self, text="testem0 empowered by Geant4Py\n\n\n")
60  title.grid(row=0, column=1, columnspan = 4)
61 
62 # particle list box
63  particle_title = Label(self, text="Particle")
64  particle_title.grid(row=2, column=0)
65 
66  particleFrame = Frame(self)
67  scrollbar2 = Scrollbar(particleFrame)
68  scrollbar2.pack(side = RIGHT, fill = Y)
69  self.particleListBox = Listbox(particleFrame, yscrollcommand=scrollbar2.set, exportselection=FALSE,height = 6)
70  self.particleListBox.pack(side = LEFT)
71  for item in particleList:
72  self.particleListBox.insert(END, item)
73  scrollbar2.config(command=self.particleListBox.yview)
74  particleFrame.grid(row=3, column=0)
75  self.particleListBox.select_set(0)
76 
77 # separator frame
78  fblank = Frame(self,width = 40)
79  fblank.grid(row=3,column=1)
80 
81 # material list box
82  detmaterial_title = Label(self, text="Material")
83  detmaterial_title.grid(row=2, column=2)
84 
85  materialFrame = Frame(self)
86  scrollbar = Scrollbar(materialFrame)
87  scrollbar.pack(side = RIGHT, fill = Y)
88  self.materialListBox = Listbox(materialFrame, yscrollcommand=scrollbar.set, exportselection=FALSE, height = 6)
89  self.materialListBox.pack(side = LEFT, fill = Y)
90  for item in materialList:
91  self.materialListBox.insert(END, item)
92  scrollbar.config(command=self.materialListBox.yview)
93  materialFrame.grid(row=3, column=2)
94  self.materialListBox.select_set(0)
95 
96 # separator frame
97  fblank = Frame(self,width = 40)
98  fblank.grid(row=3,column=3)
99 
100 # energy
101  fEnergy = Frame(self)
102  energyLabel = Label(self, text="Energy")
103  energyLabel.grid(row = 2, column = 4)
104 
105  scrollbarEnergy = Scrollbar(fEnergy)
106  scrollbarEnergy.pack(side = RIGHT, fill = Y)
107  self.energyEntry = Entry(fEnergy, width= 8 );
108  self.energyEntry.pack(side = TOP)
109  self.energyEntry.insert(0, "1.0")
110 
111  self.energyListBox = Listbox(fEnergy, yscrollcommand=scrollbarEnergy.set,exportselection=FALSE,width=8,height = 5)
112  self.energyListBox.pack(side = BOTTOM )
113  for item in enrgyList:
114  self.energyListBox.insert(END, item)
115  scrollbarEnergy.config(command=self.energyListBox.yview)
116  fEnergy.grid(row = 3, column = 4 )
117  self.energyListBox.select_set(0)
118 
119 # separator frame
120  fblank = Frame(self,width = 40)
121  fblank.grid(row=3,column=5)
122 
123 # cuts
124  fCuts = Frame(self)
125  cutsLabel = Label(self, text="Cuts", width= 8)
126  cutsLabel.grid(row = 2, column = 6)
127 
128  scrollbarCuts = Scrollbar(fCuts)
129  scrollbarCuts.pack(side = RIGHT, fill = Y)
130  self.cutsEntry = Entry(fCuts, width= 8);
131  self.cutsEntry.pack(side = TOP)
132  self.cutsEntry.insert(0, "1.0")
133 
134  self.cutsListBox = Listbox(fCuts, width= 8 ,yscrollcommand=scrollbarCuts.set,exportselection=FALSE,height = 5)
135  self.cutsListBox.pack(side = BOTTOM )
136  for item in cutsList:
137  self.cutsListBox.insert(END, item)
138  scrollbarCuts.config(command=self.cutsListBox.yview)
139  fCuts.grid(row = 3, column = 6 )
140  self.cutsListBox.select_set(0)
141 
142 # separator frame
143  fblank = Frame(self,height = 40)
144  fblank.grid(row=4,column=0)
145 
146 # start a run button
147  startBut = Button(self, bg="green", text="Start a run", command=self.cmd_beamOn)
148  startBut.grid(row=5, column=2, sticky=W)
149 
150 # exit button
151  exitBut = Button(self, bg="grey", text="Exit", command=self.quit)
152  exitBut.grid(row=5, column=6, sticky=E)
153 
154  def __init__(self, master=None):
155  Frame.__init__(self, master)
156  self.init()
157  self.grid()
158 
159  def cmd_beamOn(self):
160 
161  # get and set particle
162  if self.particleListBox.curselection():
163  index =int(self.particleListBox.curselection()[0])
164  g4.gApplyUICommand("/gun/particle " + particleList[index])
165 
166  # get and set detector Material
167  if self.materialListBox.curselection():
168  index =int(self.materialListBox.curselection()[0])
169  g4.gApplyUICommand("/testem/det/setMat " + materialList[index])
170 
171  # get and set energy
172  energy = self.energyEntry.get()
173  if self.energyListBox.curselection():
174  index = int(self.energyListBox.curselection()[0])
175  unity = enrgyList[index]
176  g4.gApplyUICommand("/gun/energy " + energy + " " + unity)
177 
178  # get and set cuts
179  cuts = self.cutsEntry.get()
180  if self.cutsListBox.curselection():
181  index = int(self.cutsListBox.curselection()[0])
182  unity = cutsList[index]
183  g4.gApplyUICommand("/testem/phys/setCuts " + cuts + " " + unity)
184 
185  # run beamOn
186  g4.gRunManager.BeamOn(1)
187 
188 
189 app = App()
190 app.mainloop()
191 
energyListBox
Definition: demo.py:111
cutsEntry
Definition: demo.py:130
energyEntry
Definition: demo.py:107
cutsListBox
Definition: demo.py:134
materialListBox
Definition: demo.py:88
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
def __init__
Definition: demo.py:154
def init
Definition: demo.py:56
def cmd_beamOn
Definition: demo.py:159
Definition: demo.py:53
particleListBox
Definition: demo.py:69