Newer
Older
def loadDDG4():
from ROOT import gSystem
result = gSystem.Load("libDD4hepG4")
if 0 != result:
raise Exception('DDG4.py: Failed to load the Geant4 library libDD4hepG4: '+gSystem.GetErrorStr())
from ROOT import DD4hep as module
return module
# We are nearly there ....
current = __import__(__name__)
def _import_class(ns,nam):
scope = getattr(current,ns)
setattr(current,nam,getattr(scope,nam))
#---------------------------------------------------------------------------
#
try:
DD4hep = loadDDG4()
except Exception,X:
print '+--%-100s--+'%(100*'-',)
print '| %-100s |'%('Failed to load DDG4 library:',)
print '| %-100s |'%(str(X),)
print '| %-100s |'%('Try to compile AClick on the fly.',)
print '+--%-100s--+'%(100*'-',)
DD4hep = compileAClick(dictionary='DDG4Dict.C',g4=True)
Sim = DD4hep.Simulation
Simulation = DD4hep.Simulation
Kernel = Sim.KernelHandle
Interface = Sim.Geant4ActionCreation
def _registerGlobalAction(self,action):
self.get().registerGlobalAction(Interface.toAction(action))
def _registerGlobalFilter(self,filter):
self.get().registerGlobalFilter(Interface.toAction(filter))
#---------------------------------------------------------------------------
def _getKernelProperty(self, name):
#print '_getKernelProperty:',str(type(self)),name
ret = Interface.getPropertyKernel(self.get(),name)
if ret.status > 0:
return ret.data
elif hasattr(self.get(),name):
return getattr(self.get(),name)
elif hasattr(self,name):
return getattr(self,name)
msg = 'Geant4Kernel::GetProperty [Unhandled]: Cannot access Kernel.'+name
raise exceptions.KeyError(msg)
#---------------------------------------------------------------------------
def _setKernelProperty(self, name, value):
#print '_setKernelProperty:',name,value
if Interface.setPropertyKernel(self.get(),name,str(value)):
return
msg = 'Geant4Kernel::SetProperty [Unhandled]: Cannot set Kernel.'+name+' = '+str(value)
raise exceptions.KeyError(msg)
Kernel.registerGlobalAction = _registerGlobalAction
Kernel.registerGlobalFilter = _registerGlobalFilter
Kernel.__getattr__ = _getKernelProperty
Kernel.__setattr__ = _setKernelProperty
ActionHandle = Sim.ActionHandle
#---------------------------------------------------------------------------
def SensitiveAction(kernel,nam,det): return Interface.createSensitive(kernel,nam,det)
#---------------------------------------------------------------------------
def Action(kernel,nam): return Interface.createAction(kernel,nam)
#---------------------------------------------------------------------------
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def Filter(kernel,nam): return Interface.createFilter(kernel,nam)
#---------------------------------------------------------------------------
def RunAction(kernel,nam): return Interface.createRunAction(kernel,nam)
#---------------------------------------------------------------------------
def EventAction(kernel,nam): return Interface.createEventAction(kernel,nam)
#---------------------------------------------------------------------------
def GeneratorAction(kernel,nam): return Interface.createGeneratorAction(kernel,nam)
#---------------------------------------------------------------------------
def TrackingAction(kernel,nam): return Interface.createTrackingAction(kernel,nam)
#---------------------------------------------------------------------------
def SteppingAction(kernel,nam): return Interface.createSteppingAction(kernel,nam)
#---------------------------------------------------------------------------
def StackingAction(kernel,nam): return Interface.createStackingAction(kernel,nam)
#---------------------------------------------------------------------------
def PhysicsList(kernel,nam): return Interface.createPhysicsList(kernel,nam)
#---------------------------------------------------------------------------
def SensitiveSequence(kernel, nam): return Interface.createSensDetSequence(kernel,nam)
#---------------------------------------------------------------------------
def _adopt(self,action): self.adopt(action.get())
def _setup(obj):
_import_class('Sim',obj)
setattr(getattr(current,obj),'add',_adopt)
#---------------------------------------------------------------------------
_setup('Geant4RunActionSequence')
_setup('Geant4EventActionSequence')
_setup('Geant4GeneratorActionSequence')
_setup('Geant4TrackingActionSequence')
_setup('Geant4SteppingActionSequence')
_setup('Geant4StackingActionSequence')
_setup('Geant4PhysicsListActionSequence')
_setup('Geant4SensDetActionSequence')
_setup('Geant4Sensitive')
_import_class('Sim','Geant4Filter')
_import_class('Sim','Geant4RunAction')
#---------------------------------------------------------------------------
def _get(self, name):
#print '_get:',str(type(self)),name
a = Interface.toAction(self)
ret = Interface.getProperty(a,name)
if ret.status > 0:
return ret.data
elif hasattr(self.action,name):
return getattr(self.action,name)
elif hasattr(self,name):
return getattr(self,name)
msg = 'Geant4Action::GetProperty [Unhandled]: Cannot access '+a.name()+'.'+name
raise exceptions.KeyError(msg)
def _set(self, name, value):
#print '_set:',name,value
a = Interface.toAction(self)
if Interface.setProperty(a,name,str(value)):
return
msg = 'Geant4Action::SetProperty [Unhandled]: Cannot set '+a.name()+'.'+name+' = '+str(value)
raise exceptions.KeyError(msg)
def _props(obj):
_import_class('Sim',obj)
cl = getattr(current,obj)
cl.__getattr__ = _get
cl.__setattr__ = _set
_props('FilterHandle')
_props('ActionHandle')
_props('RunActionHandle')
_props('EventActionHandle')
_props('GeneratorActionHandle')
_props('PhysicsListHandle')
_props('TrackingActionHandle')
_props('SteppingActionHandle')
_props('StackingActionHandle')
_props('SensitiveHandle')
_props('GeneratorActionSequenceHandle')
_props('RunActionSequenceHandle')
_props('EventActionSequenceHandle')
_props('TrackingActionSequenceHandle')
_props('SteppingActionSequenceHandle')
_props('StackingActionSequenceHandle')
_props('PhysicsListActionSequenceHandle')
_props('SensDetActionSequenceHandle')
_props('Geant4PhysicsListActionSequence')
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
Helper object to perform stuff, which occurs very often.
I am sick of typing the same over and over again.
@author M.Frank
@version 1.0
"""
class Simple:
def __init__(self, kernel,calo='Geant4SimpleCalorimeterAction',tracker='Geant4SimpleTrackerAction'):
kernel.UI = "UI"
kernel.printProperties()
self.kernel = kernel
self.calo = calo
self.tracker = tracker
def printDetectors(self):
lcdd = self.kernel.lcdd()
print '+++ List of sensitive detectors:'
for i in lcdd.detectors():
o = DetElement(i.second)
sd = lcdd.sensitiveDetector(o.name())
if sd.isValid():
print '+++ %-32s type:%s'%(o.name(), sd.type(), )
def setupDetector(self,name,sensitive_type):
seq = SensitiveSequence(self.kernel,'Geant4SensDetActionSequence/'+name)
act = SensitiveAction(self.kernel,sensitive_type+'/'+name+'Handler',name)
seq.add(act)
return (seq,act)
def setupCalorimeter(self,name,type=None):
if type is None: type = self.calo
return self.setupDetector(name,type)
def setupTracker(self,name,type=None):
if type is None: type = self.tracker
return self.setupDetector(name,type)
def setupPhysics(self,name):
phys = self.kernel.physicsList()
phys.extends = name
phys.decays = True
phys.enableUI()
phys.dump()
return phys
def setupGun(self, name, particle, energy, isotrop=True, multiplicity=1, position=(0.0,0.0,0.0)):
gun = GeneratorAction(self.kernel,"Geant4ParticleGun/"+name)
gun.energy = energy
gun.particle = particle
gun.multiplicity = multiplicity
gun.position = position
gun.isotrop = isotrop
gun.enableUI()
self.kernel.generatorAction().add(gun)
def setupCshUI(self):
# Configure UI
ui = Action(self.kernel,"Geant4UIManager/UI")
ui.HaveVIS = True
ui.HaveUI = True
ui.SessionType = 'csh'
self.kernel.registerGlobalAction(ui)
def setupROOTOutput(self,name,output):
evt_root = EventAction(self.kernel,'Geant4Output2ROOT/'+name)
evt_root.Control = True
evt_root.Output = output+'.root'
evt_root.enableUI()
self.kernel.eventAction().add(evt_root)
return evt_root
def setupLCIOOutput(self,name,output):
evt_lcio = EventAction(self.kernel,'Geant4Output2LCIO/'+name)
evt_lcio.Control = True
evt_lcio.Output = output
evt_lcio.enableUI()
self.kernel.eventAction().add(evt_lcio)
return evt_lcio