Geant4_10
test.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 # ==================================================================
3 # python script for Geant4Py test
4 #
5 # gtest05
6 # - test for CSG geometry construction in Python
7 # ==================================================================
8 from Geant4 import *
9 import g4py.ExN01pl, g4py.ParticleGun
10 import os, math
11 
12 # ==================================================================
13 # user actions in python
14 # ==================================================================
16  "My Detector Construction"
17 
18  def __init__(self):
19  G4VUserDetectorConstruction.__init__(self)
20  self.air= gNistManager.FindOrBuildMaterial("G4_AIR")
21  self.lv_object= None
22  self.world= self.ConstructWorld()
23 
24  self.va_red= G4VisAttributes(G4Color(1.,0.,0.))
25  self.va_cyan= G4VisAttributes(G4Color(0.,1.,1.))
26  self.va_green= G4VisAttributes(G4Color(0.,1.,0.))
27  self.va_blue= G4VisAttributes(G4Color(0.,0.,1.))
29 
30  # -----------------------------------------------------------------
31  def ConstructWorld(self):
32  # Python has automatic garbage collection system.
33  # Geometry objects must be defined as GLOBAL not to be deleted.
34  global sld_world, lv_world, pv_world, va_world
35 
36  sld_world= G4Box("world", 1.*m, 1.*m, 1.*m)
37  lv_world= G4LogicalVolume(sld_world, self.air, "world")
38  pv_world= G4PVPlacement(G4Transform3D(), lv_world, "world",
39  None, False, 0)
40 
41  va_world= G4VisAttributes()
42  va_world.SetVisibility(False)
43  lv_world.SetVisAttributes(va_world)
44 
45  # solid object (dummy)
46  global sld_sld, lv_sld, pv_sld
47  sld_sld= G4Box("dummy", 10.*cm, 10.*cm, 10.*cm)
48  self.lv_object= lv_sld= G4LogicalVolume(sld_sld, self.air, "dummy")
49  pv_sld= G4PVPlacement(None, G4ThreeVector(), "dummy", lv_sld,
50  pv_world, False, 0)
51 
52  return pv_world
53 
54  # -----------------------------------------------------------------
55  def ConstructBox(self):
56  global sld_box
57  sld_box= G4Box("box", 30.*cm, 40.*cm, 60.*cm)
58  self.lv_object.SetSolid(sld_box)
59  self.lv_object.SetVisAttributes(self.va_red)
60  gRunManager.GeometryHasBeenModified()
61 
62  # -----------------------------------------------------------------
63  def ConstructTubs(self):
64  global sld_tubs
65  sld_tubs= G4Tubs("tubs", 10.*cm, 15.*cm, 20.*cm, 0., pi)
66  self.lv_object.SetSolid(sld_tubs)
67  self.lv_object.SetVisAttributes(self.va_cyan)
68  gRunManager.GeometryHasBeenModified()
69 
70  # -----------------------------------------------------------------
71  def ConstructCons(self):
72  global sld_cons
73  sld_cons= G4Cons("cons", 5.*cm, 10.*cm, 20.*cm, 25.*cm,
74  40.*cm, 0., 4./3.*pi)
75  self.lv_object.SetSolid(sld_cons)
76  self.lv_object.SetVisAttributes(self.va_green)
77  gRunManager.GeometryHasBeenModified()
78 
79  # -----------------------------------------------------------------
80  def ConstructPara(self):
81  global sld_para
82  sld_para= G4Para("para", 30.*cm, 40.*cm, 60.*cm, pi/4., pi/8., 0.)
83  self.lv_object.SetSolid(sld_para)
84  self.lv_object.SetVisAttributes(self.va_blue)
85  gRunManager.GeometryHasBeenModified()
86 
87  # -----------------------------------------------------------------
88  def ConstructTrd(self):
89  global sld_trd
90  sld_trd= G4Trd("trd", 30.*cm, 10.*cm, 40.*cm, 15.*cm, 60.*cm)
91  self.lv_object.SetSolid(sld_trd)
92  self.lv_object.SetVisAttributes(self.va_blue)
93  gRunManager.GeometryHasBeenModified()
94 
95  # -----------------------------------------------------------------
96  def ConstructTrap(self):
97  global sld_trap
98  sld_trap= G4Trap("trap", 60.*cm, 20.*degree, 5.*degree,
99  40.*cm, 30.*cm, 40.*cm, 10.*degree,
100  16.*cm, 10*cm, 14.*cm, 10.*deg)
101  self.lv_object.SetSolid(sld_trap)
102  self.lv_object.SetVisAttributes(self.va_green)
103  gRunManager.GeometryHasBeenModified()
104 
105  # -----------------------------------------------------------------
106  def ConstructSphere(self):
107  global sld_sphere
108  sld_sphere= G4Sphere("sphere", 100.*cm, 120.*cm, 0., 180.*deg,
109  0., 180.*deg)
110  self.lv_object.SetSolid(sld_sphere)
111  self.lv_object.SetVisAttributes(self.va_cyan)
112  gRunManager.GeometryHasBeenModified()
113 
114  # -----------------------------------------------------------------
115  def ConstructOrb(self):
116  global sld_orb
117  sld_orb= G4Orb("orb", 100.*cm)
118  self.lv_object.SetSolid(sld_orb)
119  self.lv_object.SetVisAttributes(self.va_red)
120  gRunManager.GeometryHasBeenModified()
121 
122  # -----------------------------------------------------------------
123  def ConstructTorus(self):
124  global sld_torus
125  sld_torus= G4Torus("torus", 40.*cm, 60.*cm, 200.*cm, 0., 90.*deg)
126  self.lv_object.SetSolid(sld_torus)
127  self.lv_object.SetVisAttributes(self.va_magenta)
128  gRunManager.GeometryHasBeenModified()
129 
130  # -----------------------------------------------------------------
131  def ConstructPolycone(self):
132  zvec= G4doubleVector()
133  rinvec= G4doubleVector()
134  routvec= G4doubleVector()
135 
136  zvec[:]= [ 5.*cm, 7.*cm, 9.*cm, 11.*cm, 25.*cm, 27.*cm, 29.*cm,
137  31.*cm, 35.*cm ]
138  rinvec[:]= [0.,0.,0.,0.,0.,0.,0.,0.,0.]
139  routvec[:]= [ 0., 10.*cm, 10.*cm, 5.*cm, 5.*cm, 10.*cm,
140  10.*cm, 2.*cm, 2.*cm ]
141 
142  global sld_pcon
143  sld_pcon= CreatePolycone("pcon", 0., twopi, 9, zvec, rinvec,routvec)
144  self.lv_object.SetSolid(sld_pcon)
145  self.lv_object.SetVisAttributes(self.va_cyan)
146  gRunManager.GeometryHasBeenModified()
147 
148  # -----------------------------------------------------------------
150  zvec= G4doubleVector()
151  rinvec= G4doubleVector()
152  routvec= G4doubleVector()
153 
154  zvec[:]= [ 0., 5.*cm, 8.*cm, 13.*cm, 30.*cm, 32.*cm, 35.*cm ]
155  rinvec[:]= [0.,0.,0.,0.,0.,0.,0. ]
156  routvec[:]= [ 0., 15.*cm, 15.*cm, 4.*cm, 4.*cm, 10.*cm, 10.*cm ]
157 
158  global sld_pgon
159  sld_pgon= CreatePolyhedra("pgon", 0., twopi, 5, 7, zvec, rinvec,routvec)
160  self.lv_object.SetSolid(sld_pgon)
161  self.lv_object.SetVisAttributes(self.va_green)
162  gRunManager.GeometryHasBeenModified()
163 
164  # -----------------------------------------------------------------
166  global sld_et
167  sld_et= G4EllipticalTube("ellipticaltube", 5.*cm, 10.*cm, 20.*cm)
168  self.lv_object.SetSolid(sld_et)
169  self.lv_object.SetVisAttributes(self.va_cyan)
170  gRunManager.GeometryHasBeenModified()
171 
172  # -----------------------------------------------------------------
174  global sld_es
175  sld_es= G4Ellipsoid("ellipsoid", 10.*cm, 20.*cm, 50.*cm,
176  -10.*cm, 40.*cm)
177  self.lv_object.SetSolid(sld_es)
178  self.lv_object.SetVisAttributes(self.va_red)
179  gRunManager.GeometryHasBeenModified()
180 
181  # -----------------------------------------------------------------
183  global sld_ec
184  sld_ec= G4EllipticalCone("ellipticalcone", 30.*cm, 60.*cm,
185  50.*cm, 25.*cm)
186  self.lv_object.SetSolid(sld_ec)
187  self.lv_object.SetVisAttributes(self.va_magenta)
188  gRunManager.GeometryHasBeenModified()
189 
190  # -----------------------------------------------------------------
191  def ConstructHype(self):
192  global sld_hype
193  sld_hype= G4Hype("hype", 20.*cm, 30.*cm, 0.7, 0.7, 50.*cm)
194  self.lv_object.SetSolid(sld_hype)
195  self.lv_object.SetVisAttributes(self.va_blue)
196  gRunManager.GeometryHasBeenModified()
197 
198  # -----------------------------------------------------------------
199  def ConstructTet(self):
200  global sld_tet
201  p1= G4ThreeVector(0., 0., math.sqrt(3.)*cm)
202  p2= G4ThreeVector(0., 2*math.sqrt(2./3.)*cm, -1./math.sqrt(3)*cm)
203  p3= G4ThreeVector(-math.sqrt(2.)*cm, -math.sqrt(2./3.)*cm,
204  -1./math.sqrt(3)*cm)
205  p4= G4ThreeVector(math.sqrt(2)*cm, -math.sqrt(2./3.)*cm,
206  -1./math.sqrt(3)*cm)
207 
208  sld_tet= G4Tet("tet", 20.*p1, 20.*p2, 20.*p3, 20.*p4)
209  self.lv_object.SetSolid(sld_tet)
210  self.lv_object.SetVisAttributes(self.va_green)
211  gRunManager.GeometryHasBeenModified()
212 
213  # -----------------------------------------------------------------
215  global sld_twb
216  sld_twb= G4TwistedBox("twistedbox", 30.*deg, 30.*cm, 40.*cm, 60.*cm)
217  self.lv_object.SetSolid(sld_twb)
218  self.lv_object.SetVisAttributes(self.va_cyan)
219  gRunManager.GeometryHasBeenModified()
220 
221  # -----------------------------------------------------------------
223  global sld_twtrp
224  sld_twtrp= G4TwistedTrap("twistedtrap", 30.*deg,
225  60.*cm, 20.*deg, 5.*deg,
226  40.*cm, 30.*cm, 40.*cm,
227  16.*cm, 10.*cm, 14.*cm, 10.*deg)
228  self.lv_object.SetSolid(sld_twtrp)
229  self.lv_object.SetVisAttributes(self.va_blue)
230  gRunManager.GeometryHasBeenModified()
231 
232  # -----------------------------------------------------------------
234  global sld_twtrd
235  sld_twtrd= G4TwistedTrd("twistedtrd", 30.*cm, 10.*cm,
236  40.*cm, 15.*cm, 60.*cm, 30.*deg)
237  self.lv_object.SetSolid(sld_twtrd)
238  self.lv_object.SetVisAttributes(self.va_green)
239  gRunManager.GeometryHasBeenModified()
240 
241  # -----------------------------------------------------------------
243  global sld_twt
244  sld_twt= G4TwistedTubs("twistedtube", 60.*deg,
245  10.*cm, 15.*cm, 20.*cm, 90.*deg)
246  self.lv_object.SetSolid(sld_twt)
247  self.lv_object.SetVisAttributes(self.va_magenta)
248  gRunManager.GeometryHasBeenModified()
249 
250  # -----------------------------------------------------------------
251  def Construct(self): # return the world volume
252  return self.world
253 
254 # ==================================================================
255 # main
256 # ==================================================================
257 os.environ["G4VRML_DEST_DIR"]= "."
258 os.environ["G4VRMLFILE_MAX_FILE_NUM"]= "1"
259 os.environ["G4VRMLFILE_VIEWER"]= "echo"
260 
261 # set geometry
263 gRunManager.SetUserInitialization(myDC)
264 
265 # minimal physics list
266 g4py.ExN01pl.Construct()
267 
268 # set primary generator action
269 g4py.ParticleGun.Construct()
270 
271 # initialize
272 gRunManager.Initialize()
273 
274 # visualization
275 gApplyUICommand("/vis/open VRML2FILE")
276 gApplyUICommand("/vis/scene/create")
277 gApplyUICommand("/vis/scene/add/volume")
278 gApplyUICommand("/vis/sceneHandler/attach")
279 gApplyUICommand("/vis/scene/add/axes 0. 0. 0. 10. cm")
280 
281 # create a vrml file for each solid type
282 f_list= (
283  ("g4box", myDC.ConstructBox),
284  ("g4tubs", myDC.ConstructTubs),
285  ("g4cons", myDC.ConstructCons),
286  ("g4para", myDC.ConstructPara),
287  ("g4trd", myDC.ConstructTrd),
288  ("g4trap", myDC.ConstructTrap),
289  ("g4sphere", myDC.ConstructSphere),
290  ("g4orb", myDC.ConstructOrb),
291  ("g4torus", myDC.ConstructTorus),
292  ("g4polycone", myDC.ConstructPolycone),
293  ("g4polyhedra", myDC.ConstructPolyhedra),
294  ("g4ellipticaltube", myDC.ConstructEllipticalTube),
295  ("g4ellipsoid", myDC.ConstructEllipsoid),
296  ("g4ellipticalcone", myDC.ConstructEllipticalCone),
297  ("g4hype", myDC.ConstructHype),
298  ("g4tet", myDC.ConstructTet),
299  ("g4twistedbox", myDC.ConstructTwistedBox),
300  ("g4twistedtrap", myDC.ConstructTwistedTrap),
301  ("g4twistedtrd", myDC.ConstructTwistedTrd),
302  ("g4twistedtubs", myDC.ConstructTwistedTubs)
303  )
304 
305 for s,f in f_list:
306  f.__call__()
307  gRunManager.BeamOn(1)
308  fname= "%s.wrl" % (s)
309  os.rename("g4_00.wrl", fname)
310 
virtual G4VPhysicalVolume * Construct()=0
Definition: G4Para.hh:76
Definition: G4Box.hh:63
Definition: G4Tubs.hh:84
Definition: G4Trd.hh:71
Definition: G4Tet.hh:65
Definition: G4Hype.hh:66
Definition: G4Cons.hh:82
gApplyUICommand
Definition: __init__.py:168
Definition: G4Orb.hh:60
std::vector< G4double > G4doubleVector
Definition: pyglobals.cc:65