ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
__init__.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file __init__.py
1 """
2 # ==================================================================
3 # [Geant4] module package
4 #
5 # Welcome to Geant4Py.
6 #
7 # This package contains a set of Python interface with Geant4.
8 # ==================================================================
9 """
10 __version__ ='10.6'
11 __date__ = 'December/2019'
12 __author__ = 'K.Murakami (Koichi.Murakami@kek.jp)'
13 
14 # import submodules
15 from .G4interfaces import *
16 from .G4intercoms import *
17 from .G4global import *
18 from .G4run import *
19 from .G4event import *
20 from .G4tracking import *
21 from .G4track import *
22 from .G4particles import *
23 from .G4processes import *
24 from .G4geometry import *
25 from .G4materials import *
26 from .G4physicslists import *
27 from .G4digits_hits import *
28 from .G4visualization import *
29 from .G4graphics_reps import *
30 from .hepunit import *
31 from .colortable import *
32 
34  print("""=============================================================
35  Welcome to Geant4Py (A Geant4-Python Bridge)
36 
37  Version : %s
38  Date : %s
39 =============================================================
40 """ % ( __version__, __date__) )
41 
42 # ==================================================================
43 # initialize
44 # ==================================================================
45 # set G4cout/G4cerr to Python stdout
47 
48 # ==================================================================
49 # globals, which start with "g"
50 # ==================================================================
51 # gRunManager
52 if G4RunManager.GetRunManager() == None:
53  gRunManager = G4RunManager()
54 else:
55  gRunManager = G4RunManager.GetRunManager()
57 
58 # gUImanager
60 
61 # gEventManager
63 
64 # gStackManager
65 gStackManager = gEventManager.GetStackManager()
66 
67 # gTrackingManager
68 gTrackingManager = gEventManager.GetTrackingManager()
69 
70 # gStateManager
72 gExceptionHandler = G4ExceptionHandler() # automatically registered
73 
74 # gGeometryManager
75 gGeometryManager = G4GeometryManager.GetInstance()
76 
77 # gTransportationManager
79 
80 # gParticleTable
82 gParticleIterator = PyG4ParticleList()
83 
84 # gProcessTable
86 
87 # gProductionCutsTable
89 
90 # gEmCalculator
91 gEmCalculator = G4EmCalculator()
92 
93 # gMaterial/ElementTable
94 gMaterialTable = G4Material.GetMaterialTable()
95 gElementTable = G4Element.GetElementTable()
96 
97 # gNistManager (since 7.1)
98 _material_class_list = dir(G4materials)
99 _qfind = _material_class_list.count("G4NistManager") > 0
100 if _qfind:
101  gNistManager = G4NistManager.Instance()
102 
103 # gVisManager
104 _visdriver_list = dir(G4visualization)
105 _q_opengl_ix = "G4OpenGLImmediateX" in _visdriver_list
106 _q_opengl_sx = "G4OpenGLStoredX" in _visdriver_list
107 _q_opengl_ixm = "G4OpenGLImmediateXm" in _visdriver_list
108 _q_opengl_sxm = "G4OpenGLStoredXm" in _visdriver_list
109 _q_raytracer_x = "G4RayTracerX" in _visdriver_list
110 
112  gVisManager = G4VisManager()
113  if _q_opengl_ix:
114  _opengl_ix = G4OpenGLImmediateX()
115  if _q_opengl_sx:
116  _opengl_sx = G4OpenGLStoredX()
117  if _q_opengl_ixm:
118  _opengl_ixm = G4OpenGLImmediateXm()
119  if _q_opengl_sxm:
120  _opengl_sxm = G4OpenGLStoredXm()
121  if _q_raytracer_x:
122  _raytracer_x = G4RayTracerX()
123 
124  _vrml1 = G4VRML1File()
125  _vrml2 = G4VRML2File()
126  _dawn = G4DAWNFILE()
127  _heprep_xml = G4HepRep()
128  _heprep_file = G4HepRepFile()
129  _atree = G4ASCIITree()
130  _raytracer = G4RayTracer()
131 
132  if _q_opengl_ix:
133  gVisManager.RegisterGraphicsSystem(_opengl_ix)
134  if _q_opengl_sx:
135  gVisManager.RegisterGraphicsSystem(_opengl_sx)
136  if _q_opengl_ixm:
137  gVisManager.RegisterGraphicsSystem(_opengl_ixm)
138  if _q_opengl_sxm:
139  gVisManager.RegisterGraphicsSystem(_opengl_sxm)
140  if _q_raytracer_x:
141  gVisManager.RegisterGraphicsSystem(_raytracer_x)
142 
143  gVisManager.RegisterGraphicsSystem(_vrml1)
144  gVisManager.RegisterGraphicsSystem(_vrml2)
145  gVisManager.RegisterGraphicsSystem(_dawn)
146  gVisManager.RegisterGraphicsSystem(_heprep_xml)
147  gVisManager.RegisterGraphicsSystem(_heprep_file)
148  gVisManager.RegisterGraphicsSystem(_atree)
149  gVisManager.RegisterGraphicsSystem(_raytracer)
150 
151  gVisManager.Initialize()
152 
153 # version information
154 gG4Version = G4Version
155 gG4Date = G4Date
156 gG4VERSION_NUMBER = G4VERSION_NUMBER
157 
158 # ------------------------------------------------------------------
159 # functions
160 # ------------------------------------------------------------------
161 gControlExecute = gUImanager.ExecuteMacroFile
162 gApplyUICommand = G4intercoms.ApplyUICommand
163 gGetCurrentValues = gUImanager.GetCurrentValues
164 gStartUISession = G4interfaces.StartUISession
165 
166 
167 # ==================================================================
168 # extentions
169 # ==================================================================
170 
171 # ------------------------------------------------------------------
172 # generate one event
173 # ------------------------------------------------------------------
174 def _one_event(self):
175  "generate one event."
176  self.BeamOn(1)
177 
178 G4RunManager.OneEvent = _one_event
179 
180 # ------------------------------------------------------------------
181 # list material information
182 # ------------------------------------------------------------------
183 def _list_material(self):
184  "list materials."
185  n_materials = len(gMaterialTable)
186  print(" +------------------------------------------------------------------")
187  print(" | Table of G4Material-s (%d materails defined)" % (n_materials))
188  for i in range(0, n_materials) :
189  material = gMaterialTable[i]
190  print(" |--------------------------------------------------------"\
191  "----------")
192  print(" | %s: %s" % (material.GetName(),
193  G4BestUnit(material.GetDensity(),"Volumic Mass")))
194 
195  elementVec = material.GetElementVector()
196  fractionVec = material.GetFractionVector()
197  abundanceVec = material.GetVecNbOfAtomsPerVolume()
198  totNAtoms = material.GetTotNbOfAtomsPerVolume()
199 
200  n_elements = len(elementVec)
201  for j in range(0, n_elements):
202  print(" | + (%1d) %s(%s): A=%4.1f, N=%5.1f, " \
203  "Frac.=(%4.1f%%m,%4.1f%%a)" % \
204  (j+1, elementVec[j].GetName(), elementVec[j].GetSymbol(),
205  elementVec[j].GetZ(),
206  elementVec[j].GetN(),
207  fractionVec[j]/hepunit.perCent,
208  abundanceVec[j]/totNAtoms/hepunit.perCent))
209 
210  print(" +------------------------------------------------------------------")
211 
212 G4MaterialTable.ListMaterial = _list_material
213 
214 
215 # ------------------------------------------------------------------
216 # termination
217 # ------------------------------------------------------------------
219  gGeometryManager.OpenGeometry()
220 
221 
222 # ------------------------------------------------------------------
223 # signal handler
224 # ------------------------------------------------------------------
225 import signal
226 import threading
227 
228 def _run_abort(signum, frame):
229  state = gStateManager.GetCurrentState()
230 
231  if(state == G4ApplicationState.G4State_GeomClosed or
232  state == G4ApplicationState.G4State_EventProc):
233  print("aborting Run ...")
234  gRunManager.AbortRun(True)
235  else:
236  raise KeyboardInterrupt
237 
238 if (threading.activeCount() == 1):
239  signal.signal(signal.SIGINT, _run_abort)