File indexing completed on 2025-01-30 09:17:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 from __future__ import absolute_import
0012 import dd4hep
0013 import dddigi
0014
0015
0016 class Digitize(dd4hep.Logger, dd4hep.CommandLine):
0017 """
0018 Helper object to perform stuff, which occurs very often.
0019 I am sick of typing the same over and over again.
0020 Hence, I grouped often used python fragments to this small
0021 class to re-usage.
0022
0023 Long live laziness!
0024
0025
0026 \author M.Frank
0027 \version 1.0
0028
0029 #import pdb
0030 #pdb.set_trace()
0031
0032 """
0033 def __init__(self, kernel=None):
0034 dd4hep.Logger.__init__(self, 'dddigi')
0035 dd4hep.CommandLine.__init__(self)
0036 self._kernel = kernel
0037 self._main_processor = None
0038 self._input_processor = None
0039 self._event_processor = None
0040 self._parallel = True
0041 self._dddigi = dddigi
0042 self.description = self._kernel.detectorDescription()
0043 if self.output_level:
0044 lvl = int(self.output_level)
0045 self.setPrintLevel(lvl)
0046 self._kernel.OutputLevel = lvl
0047
0048 """
0049 Access the worker kernel object.
0050
0051 \author M.Frank
0052 """
0053
0054 def kernel(self):
0055 return self._kernel
0056
0057 def create_action(self, name, **options):
0058 action = dddigi.Action(self._kernel, name)
0059 for option in options.items():
0060 setattr(action, option[0], option[1])
0061 return action
0062
0063 def main_sequencer(self):
0064 """
0065 Create main digitization sequencer
0066 """
0067 if not self._main_processor:
0068 self._main_processor = self.create_action('DigiSynchronize/MainDigitizer', parallel=self._parallel)
0069 self._main_processor.parallel = self._parallel
0070 return self._main_processor
0071
0072 def input_action(self, name=None, **options):
0073 """
0074 Append a new action to the kernel's main input action sequence
0075 """
0076 if not self._input_processor:
0077 self._input_processor = self._kernel.inputAction()
0078 self._input_processor.parallel = self._parallel
0079
0080 if not name:
0081 return self._input_processor
0082
0083 act = self.create_action(name, **options)
0084 self._input_processor.adopt(act)
0085 return act
0086
0087 def event_action(self, name=None, register=True, **options):
0088 """
0089 Append a new action to the kernel's main event action sequence
0090 """
0091 if not self._event_processor:
0092 self._event_processor = self._kernel.eventAction()
0093 self._event_processor.parallel = self._parallel
0094
0095 if not name:
0096 return self._event_processor
0097
0098 action = self.create_action(name, **options)
0099 if register:
0100 self._event_processor.adopt(action)
0101 return action
0102
0103 def output_action(self, name=None, **options):
0104 """
0105 Append a new action to the kernel's main output action sequence
0106 """
0107 if not self._output_processor:
0108 self._output_processor = self._kernel.outputAction()
0109 self._output_processor.parallel = self._parallel
0110
0111 if not name:
0112 return self._output_processor
0113
0114 act = self.create_action(name, **options)
0115 self._output_processor.adopt(act)
0116 return act
0117
0118 def events_done(self):
0119 """
0120 Access the number of events which finished processing
0121 """
0122 return self.kernel().events_done()
0123
0124 def events_submitted(self):
0125 """
0126 Access the number of submitted events
0127 """
0128 return self.kernel().events_submitted()
0129
0130 def events_processing(self):
0131 """
0132 Access the currently number of processing events (events in-flight)
0133 """
0134 return self.kernel().events_processing()
0135
0136 """
0137 Execute the Geant 4 program with all steps.
0138
0139 \author M.Frank
0140 """
0141 def execute(self):
0142 self.kernel().configure()
0143 self.kernel().initialize()
0144 self.kernel().run()
0145 done = self.kernel().events_done()
0146 self.kernel().terminate()
0147 return done
0148
0149 def activeDetectors(self):
0150 detectors = []
0151 for i in self.description.detectors():
0152 o = dd4hep.DetElement(i.second.ptr())
0153 sd = self.description.sensitiveDetector(o.name())
0154 if sd.isValid():
0155 d = {'name': o.name(), 'type': sd.type(), 'detector': o, 'sensitive': sd}
0156 detectors.append(d)
0157 return detectors
0158
0159 def printDetectors(self):
0160 self.info('+++ List of sensitive detectors:')
0161 dets = self.activeDetectors()
0162 for d in dets:
0163 self.always('+++ %-32s ---> type:%-12s' % (d['name'], d['type'],))
0164
0165 """
0166 Configure ROOT output for the event digitization
0167
0168 \author M.Frank
0169 """
0170 def setupROOTOutput(self, name, output, mc_truth=True):
0171 evt_root = dddigi.EventAction(self.kernel(), 'DigiOutput2ROOT/' + name, True)
0172 evt_root.HandleMCTruth = mc_truth
0173 evt_root.Control = True
0174 if not output.endswith('.root'):
0175 output = output + '.root'
0176 evt_root.Output = output
0177 evt_root.enableUI()
0178 self.kernel().eventAction().add(evt_root)
0179 return evt_root
0180
0181 """
0182 Execute the main Digi action
0183 \author M.Frank
0184 """
0185 def run(self, num_events, num_threads, parallel):
0186 krnl = self.kernel()
0187 krnl.numEvents = num_events
0188 krnl.numThreads = num_threads
0189 krnl.maxEventsParallel = parallel
0190 krnl.configure()
0191 krnl.initialize()
0192 krnl.run()
0193 return krnl.events_done()