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