ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExN03.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ExN03.py
1 #!/usr/bin/python3
2 # ==================================================================
3 # python script for Geant4Py
4 #
5 # ExN03 : geant4/examples/novice/N03
6 # using site-module packages
7 # ==================================================================
8 from Geant4 import *
9 import g4py.Qmaterials, g4py.NISTmaterials
10 import g4py.ExN03geom
11 import g4py.ExN03pl
12 import g4py.ParticleGun, g4py.MedicalBeam
13 import sys
14 from time import *
15 from subprocess import *
16 import os
17 
18 # ==================================================================
19 # main
20 # ==================================================================
21 # ------------------------------------------------------------------
22 # randum number
23 # ------------------------------------------------------------------
24 rand_engine= Ranlux64Engine()
25 HepRandom.setTheEngine(rand_engine)
26 HepRandom.setTheSeed(20050830)
27 
28 # ------------------------------------------------------------------
29 # setup for materials
30 # ------------------------------------------------------------------
31 
32 # NIST materials
33 #g4py.NISTmaterials.Construct()
34 
35 # ------------------------------------------------------------------
36 # setup for geometry
37 # ------------------------------------------------------------------
38 # normal way for constructing user geometry
39 
40 
41 exN03geom= g4py.ExN03geom.ExN03DetectorConstruction()
42 gRunManager.SetUserInitialization(exN03geom)
43 
44 # 2nd way, short-cut way
45 
46 #g4py.ExN01geom.Construct()
47 #g4py.ExN03geom.Construct()
48 
49 # magnetic field
50 #exN03geom.SetMagField(0.1 * tesla)
51 
52 # ------------------------------------------------------------------
53 # setup for physics list
54 # ------------------------------------------------------------------
55 # normal way for constructing user physics list
56 exN03PL= g4py.EMSTDpl.PhysicsListEMstd()
57 gRunManager.SetUserInitialization(exN03PL)
58 
59 # 2nd way, short-cut way
60 #g4py.ExN01pl.Construct()
61 #g4py.EMSTDpl.Construct()
62 
63 # ------------------------------------------------------------------
64 # setup for primary generator action
65 # ------------------------------------------------------------------
66 # normal way for constructing user physics list
67 #pgPGA= g4py.ParticleGun.ParticleGunAction()
68 #gRunManager.SetUserAction(pgPGA)
69 #pg= pgPGA.GetParticleGun()
70 
71 # 2nd way, short-cut way
72 pg= g4py.ParticleGun.Construct()
73 
74 # set parameters of particle gun
75 pg.SetParticleByName("e-")
76 pg.SetParticleEnergy(50.*MeV)
77 pg.SetParticlePosition(G4ThreeVector(-40.,0.,0.)*cm)
78 pg.SetParticleMomentumDirection(G4ThreeVector(1.,0.,0.))
79 
80 # medical beam
81 #beam= MedicalBeam.Construct()
82 
83 # ------------------------------------------------------------------
84 # go...
85 # ------------------------------------------------------------------
86 gRunManager.Initialize()
87 
88 # beamOn
89 #gRunManager.BeamOn(3)
90 
91 
92 #TEST
93 #gProcessTable.SetProcessActivation("msc", 0)
94 #gProcessTable.SetProcessActivation("conv", 0)
95 #gProcessTable.SetProcessActivation("eBrem", 0)
96 #gProcessTable.SetProcessActivation("eIoni", 0)
97 #gProcessTable.SetProcessActivation("annihil", 0)
98 
99 
100 # visualization
101 # OGLSX, VRML and HEPREP sceneHandlers are all created with names
102 gApplyUICommand("/vis/sceneHandler/create OGLSX OGLSX")
103 gApplyUICommand("/vis/sceneHandler/create VRML2FILE VRML")
104 gApplyUICommand("/vis/sceneHandler/create HepRepFile HEPREP")
105 
106 # OGLSX is the default so, viewer is created and volume is drawn
107 gApplyUICommand("/vis/viewer/create OGLSX oglsxviewer")
108 gApplyUICommand("/vis/drawVolume")
109 gApplyUICommand("/vis/scene/add/trajectories")
110 
111 gApplyUICommand("/tracking/storeTrajectory 1")
112 gApplyUICommand("/vis/scene/endOfEventAction accumulate")
113 gApplyUICommand("/vis/scene/endOfRunAction accumulate")
114 gApplyUICommand("/vis/viewer/select oglsxviewer")
115 
116 # viewers VRML and Wired are tested by their envs vars
117 # if their envs var are set, then viewers are created and drawVolume
118 
119 global heprepViewer, heprepDir, heprepName
120 heprepViewer = os.environ.get("G4HEPREPFILE_VIEWER")
121 heprepDir = os.environ.get("G4HEPREPFILE_DIR")
122 heprepName = os.environ.get("G4HEPREPFILE_NAME")
123 if heprepViewer is not None:
124  gApplyUICommand("/vis/viewer/create HEPREP wired")
125  gApplyUICommand("/vis/drawVolume")
126 
127 # VRML viewers name is user defined
128 vrmlDir = os.environ.get("G4VRML_DEST_DIR")
129 vrmlViewer = os.environ.get("G4VRMLFILE_VIEWER")
130 
131 if vrmlViewer is not None:
132  gApplyUICommand("/vis/viewer/create VRML vrmlviewer")
133  gApplyUICommand("/vis/drawVolume")
134 
135 
136 
137 # creating widgets using grid layout
138 
139 from tkinter import *
140 
141 class App(Frame):
142 
143  g4pipe = 0
144 
145  def init(self):
146 
147 #title and header row=0, 1
148  title = Label(self, text="exampleN03")
149  title.grid(row=0, column=1, columnspan=3)
150  header = Label(self, text="empowered by \n Geant4Py")
151  header.grid(row=1, column=1, columnspan=3)
152 # number of layers
153  layerLabel = Label(self, bg="green", text="No of layers")
154  self.layerVar=IntVar()
155  self.layerVar.set(10)
156  layer = Scale(self, orient=HORIZONTAL, length=400, from_=0, to=10, tickinterval=1, resolution=1, variable=self.layerVar )
157  layerLabel.grid(row=2, column=0, sticky=W)
158  layer.grid(row=2, column=1, columnspan=5, sticky=W)
159 
160 #absorber material selection row=3
161  absorbermaterialLabel = Label(self, bg="green", text="Absorber Material")
162  absorbermaterialLabel.grid(row=3, column=0, sticky=W)
163  self.absorbermaterialVar = StringVar()
164  self.absorbermaterialVar.set("Lead")
165  ra1 = { }
166  pos=1
167  for i in ("Aluminium", "Lead"):
168  ra1[i] = Radiobutton(self, text=i, variable=self.absorbermaterialVar, value=i)
169  ra1[i].grid(row=3, column=pos, sticky=W)
170  pos=pos+1
171 
172 #absorber thickness row=4
173  absorberthickLabel = Label(self, bg="green", text="Thickness (mm)")
174  self.absorberthickVar = DoubleVar()
175  self.absorberthickVar.set(10.0)
176  absorberthick = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., resolution=0.05, tickinterval=10.0, digits=4, variable=self.absorberthickVar)
177  absorberthickLabel.grid(row=4, column=0, sticky=W)
178  absorberthick.grid(row=4, column=1, columnspan=5, sticky=W)
179 
180 
181 #gap material selection row=5
182  gapmaterialLabel = Label(self, bg="green", text="Gap Material")
183  gapmaterialLabel.grid(row=5, column=0, sticky=W)
184  self.gapmaterialVar = StringVar()
185  self.gapmaterialVar.set("liquidArgon")
186  ra2 = { }
187  pos=1
188  for i in ("liquidArgon","Scintillator", "Air", "Aerogel", "Galactic" ):
189  ra2[i] = Radiobutton(self, text=i, variable=self.gapmaterialVar, value=i)
190  ra2[i].grid(row=5, column=pos, sticky=W)
191  pos=pos+1
192 
193 #gap thickness row=6
194  gapthickLabel = Label(self, bg="green", text="Thickness (mm)")
195  self.gapthickVar = DoubleVar()
196  self.gapthickVar.set(5.0)
197  gapthick = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=100., resolution=0.05, tickinterval=10.0, digits=4, variable=self.gapthickVar)
198  gapthickLabel.grid(row=6, column=0, sticky=W)
199  gapthick.grid(row=6, column=1, columnspan=5, sticky=W)
200 
201 #calorSizeYZ row=7
202  calorsizeYZLabel = Label(self, bg="green", text="SizeYZ (mm)")
203  self.calorsizeYZVar = DoubleVar()
204  self.calorsizeYZVar.set(100.0)
205  calorsizeYZ = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=200., resolution=0.05, tickinterval=20.0, digits=4, variable=self.calorsizeYZVar)
206  calorsizeYZLabel.grid(row=7, column=0, sticky=W)
207  calorsizeYZ.grid(row=7, column=1, columnspan=5, sticky=W)
208 
209 
210 #particle row=8
211  particleLabel = Label(self, bg="green", text="Particle")
212  particleLabel.grid(row=8, column=0, sticky=W)
213  self.particleVar = StringVar()
214  self.particleVar.set("e-")
215  ra1 = { }
216  pos1=1
217  for i in ("proton", "gamma", "e-", "e+", "mu-", "mu+"):
218  ra1[i] = Radiobutton(self, text=i, variable=self.particleVar, value=i)
219  ra1[i].grid(row=8, column=pos1, sticky=W)
220  pos1=pos1+1
221 
222 #energy row=9
223  energyLabel = Label(self, bg="green", text="Energy (MeV)")
224 
225  self.energyVar=StringVar()
226  self.energyVar.set(50)
227  energy = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=1000., tickinterval=100.0, resolution=0.1, variable=self.energyVar, digits=5 )
228  energyLabel.grid(row=9, column=0, sticky=W)
229  energy.grid(row=9, column=1, columnspan=5, sticky=W)
230 
231 #number of event row=10
232  eventLabel = Label(self, bg="green", text="Events")
233  self.eventVar=IntVar()
234  self.eventVar.set(3)
235  event = Scale(self, orient=HORIZONTAL, length=400, from_=0, to=100, tickinterval=10, resolution=1, variable=self.eventVar )
236  eventLabel.grid(row=10, column=0, sticky=W)
237  event.grid(row=10, column=1, columnspan=5, sticky=W)
238 
239 #start a run button row=0
240  startBut = Button(self, bg="orange", text="Start a run", command=self.cmd_beamOn)
241  startBut.grid(row=0, column=0, sticky=W)
242 
243 #Zoom in/out Pan X Y row=13
244 # visLabel = Label(self, text="viewer", bg="orange")
245 # expandBut = Button(self, text="Zoom in", command=self.cmd_expand)
246 # shrinkBut = Button(self, text="Zoom out", command=self.cmd_shrink)
247 # visLabel.grid(row=13, column=0, sticky=W)
248 # expandBut.grid(row=13, column=1, sticky=W)
249 # shrinkBut.grid(row=13, column=2, sticky=W)
250 # panLabel = Label(self, text="Pan X Y(mm)")
251 # self.panXYVar = StringVar()
252 # panXYEnt = Entry(self, textvariable=self.panXYVar, width=6)
253 # panBut = Button(self, bg="orange", text="OK", command=self.cmd_pan)
254 # panLabel.grid(row=13, column=3, sticky=W)
255 # panXYEnt.grid(row=13, column=4)
256 # panBut.grid(row=13, column=5)
257 
258 # process activate row 11 - 13
259  processLabel=Label(self, text="Process on/off", bg="green")
260  processLabel.grid(row=11, column=0, sticky=W)
261  procTab = {}
262 
263  self.processList = ["phot", "compt", "conv", "msc", "eIoni", "eBrem", "annihil","muIoni", "muBrems", "hIoni"]
264  pos=1
265  self.processVar = {}
266  for i in self.processList:
267  self.processVar[i] = IntVar()
268  procTab[i] = Checkbutton(self, text=i, variable=self.processVar[i], command=self.cmd_setProcess)
269  if pos <= 3:
270  procTab[i].grid(row=11, column=pos, sticky=W)
271  if 4<= pos <= 7:
272  procTab[i].grid(row=12, column=pos-3, sticky=W)
273  if pos >= 8:
274  procTab[i].grid(row=13, column=pos-7, sticky=W)
275  pos=pos+1
276  procTab[i].select()
277 # set cuts row 14
278  cutLabel = Label(self, bg="green", text="Cut (mm)")
279 
280  self.cutVar=DoubleVar()
281  self.cutVar.set(1.)
282  cut = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=10., tickinterval=1., resolution=0.005, variable=self.cutVar, digits=5 )
283  cutLabel.grid(row=14, column=0, sticky=W)
284  cut.grid(row=14, column=1, columnspan=5, sticky=W)
285 
286 # set mag field row 15
287  magLabel = Label(self, bg="green", text="Magnetic (T)")
288 
289  self.magVar=DoubleVar()
290  self.magVar.set(0.)
291  mag = Scale(self, orient=HORIZONTAL, length=400, from_=0., to=5., tickinterval=1., resolution=0.1, variable=self.magVar, digits=3 )
292  magLabel.grid(row=15, column=0, sticky=W)
293  mag.grid(row=15, column=1, columnspan=5, sticky=W)
294 
295 
296 # viewer selection row=16
297  viewerLabel = Label(self, bg="green", text="Viewer")
298  viewerLabel.grid(row=16, column=0, sticky=W)
299  self.viewerVar = StringVar()
300  self.viewerVar.set("")
301  stateOfViewer = {"OpenGL":"normal", "VRML":"normal", "Wired":"normal"}
302  if vrmlViewer is None: stateOfViewer["VRML"] = "disabled"
303  if heprepViewer is None: stateOfViewer["Wired"] = "disabled"
304  viewers = { }
305  pos=1
306  for i in ("OpenGL", "VRML", "Wired"):
307  viewers[i] = Radiobutton(self, text=i, variable=self.viewerVar, value=i, command=self.cmd_viewer, state=stateOfViewer[i])
308  viewers[i].grid(row=16, column=pos, sticky=W)
309  pos=pos+1
310 
311 
312 #Geant4 command entry row = 17
313  g4comLabel = Label(self, text="Geant4 command", bg="orange")
314  self.g4commandVar = StringVar()
315  commandEntry = Entry(self, textvariable=self.g4commandVar, width=15)
316  self.g4commandVar.set("/vis/viewer/zoom 1.2")
317  comBut = Button(self, bg="orange", text="Execute", command=self.cmd_g4command)
318  g4comLabel.grid(row=17, column=0, sticky=W)
319  commandEntry.grid(row=17, column=1, columnspan=3, sticky=E+W)
320  comBut.grid(row=17, column=5)
321 
322 #exit row = 0
323  exitBut = Button(self, bg="red", text="End all", command=sys.exit)
324  exitBut.grid(row=0, column=5, sticky=W)
325 
326 #on Run butto do...
327  def cmd_beamOn(self):
328  exN03geom.SetNbOfLayers(self.layerVar.get())
329  exN03geom.SetAbsorberMaterial(self.absorbermaterialVar.get())
330  exN03geom.SetAbsorberThickness(self.absorberthickVar.get() * mm/2.0)
331  exN03geom.SetGapMaterial(self.gapmaterialVar.get())
332  exN03geom.SetGapThickness(self.gapthickVar.get() * mm/2.0)
333  exN03geom.SetCalorSizeYZ(self.calorsizeYZVar.get() * mm)
334  position = -self.layerVar.get()*(self.absorberthickVar.get() + self.gapthickVar.get())*1.2
335 
336  exN03geom.UpdateGeometry()
337  exN03PL.SetDefaultCutValue(self.cutVar.get() * mm)
338  exN03PL.SetCutsWithDefault()
339  exN03geom.SetMagField(self.magVar.get() * tesla)
340 
341  print("Now geometry updated")
342 
343 
344  self.cmd_particle(self.particleVar.get())
345  self.cmd_energy(self.energyVar.get())
346 
347  print(position)
348 
349  eventNum = self.eventVar.get()
350  for i in range(eventNum):
351 
352  pg.SetParticlePosition(G4ThreeVector(position*mm, (i-eventNum/2)*5.*mm, 0.*cm))
353  gRunManager.BeamOn(1)
354  sleep(0.01)
355  gApplyUICommand("/vis/viewer/update")
356 
357  def cmd_setProcess(self):
358  for i in self.processList:
359  if self.processVar[i].get() == 0:
360  gProcessTable.SetProcessActivation(i, 0)
361  print("Process " + i + " inactivated")
362  else:
363  gProcessTable.SetProcessActivation(i, 1)
364  print("Process " + i + " activated")
365 
366  def cmd_g4command(self):
367  gApplyUICommand(self.g4commandVar.get())
368 
369  def cmd_particle(self, particle):
370  gApplyUICommand("/gun/particle " + particle)
371 
372 
373  def cmd_energy(self, penergy):
374  gApplyUICommand("/gun/energy " + penergy + " MeV")
375 
376 
377  def cmd_viewer(self):
378  if self.viewerVar.get() == "OpenGL":
379  gApplyUICommand("/vis/viewer/select oglsxviewer")
380  gApplyUICommand("/vis/scene/add/trajectories")
381 
382  gApplyUICommand("/tracking/storeTrajectory 1")
383  gApplyUICommand("/vis/scene/endOfEventAction accumulate")
384  gApplyUICommand("/vis/scene/endOfRunAction accumulate")
385 
386  if self.viewerVar.get() == "VRML":
387  gApplyUICommand("/vis/viewer/select vrmlviewer")
388  gApplyUICommand("/vis/scene/add/trajectories")
389 
390  gApplyUICommand("/tracking/storeTrajectory 1")
391  gApplyUICommand("/vis/scene/endOfEventAction accumulate")
392  gApplyUICommand("/vis/scene/endOfRunAction accumulate")
393 
394  if self.viewerVar.get() == "Wired":
395  gApplyUICommand("/vis/viewer/select wired")
396  gApplyUICommand("/vis/scene/add/trajectories")
397 
398  gApplyUICommand("/tracking/storeTrajectory 1")
399  gApplyUICommand("/vis/scene/endOfEventAction accumulate")
400  gApplyUICommand("/vis/scene/endOfRunAction accumulate")
401 
402  if self.g4pipe == 0:
403  Popen(heprepViewer + " -file " + heprepDir + "/" + heprepName +".heprep", shell=True)
404  self.g4pipe = 1
405 
406 
407  def cmd_expand(self):
408  gApplyUICommand("/vis/viewer/zoom 1.2")
409 
410  def cmd_pan(self):
411  gApplyUICommand("/vis/viewer/pan " + self.panXYVar.get() + " " + " mm")
412 
413 
414  def cmd_shrink(self):
415  gApplyUICommand("/vis/viewer/zoom 0.8")
416 
417 
418 
419  def __init__(self, master=None):
420  Frame.__init__(self, master)
421  self.init()
422  self.grid()
423 
424 
425 app = App()
426 app.mainloop()