File indexing completed on 2025-01-18 09:14:22
0001 """Class for input plugin configuration"""
0002
0003 from __future__ import absolute_import, unicode_literals
0004 from DDSim.Helper.ConfigHelper import ConfigHelper
0005
0006
0007 class InputConfig(ConfigHelper):
0008 """Configuration for Input Files."""
0009
0010 def __init__(self):
0011 super(InputConfig, self).__init__()
0012 self._userPlugin = []
0013 self._closeProperties()
0014
0015 @property
0016 def userInputPlugin(self):
0017 """Set one or more functions to configure input steps.
0018
0019 The functions must take a ``DD4hepSimulation`` object as their only argument and return the created generatorAction
0020 ``gen`` (for example).
0021
0022 For example one can add this to the ddsim steering file:
0023
0024 def exampleUserPlugin(dd4hepSimulation):
0025 '''Example code for user created plugin.
0026
0027 :param DD4hepSimulation dd4hepSimulation: The DD4hepSimulation instance, so all parameters can be accessed
0028 :return: GeneratorAction
0029 '''
0030 from DDG4 import GeneratorAction, Kernel
0031 # Geant4InputAction is the type of plugin, Cry1 just an identifier
0032 gen = GeneratorAction(Kernel(), 'Geant4InputAction/Cry1' , True)
0033 # CRYEventReader is the actual plugin, steeringFile its constructor parameter
0034 gen.Input = 'CRYEventReader|' + 'steeringFile'
0035 # we can give a dictionary of Parameters that has to be interpreted by the setParameters function of the plugin
0036 gen.Parameters = {'DataFilePath': '/path/to/files/data'}
0037 gen.enableUI()
0038 return gen
0039
0040 SIM.inputConfig.userInputPlugin = exampleUserPlugin
0041
0042 Repeat function definition and assignment to add multiple input steps
0043
0044 """
0045 return self._userPlugin
0046
0047 @userInputPlugin.setter
0048 def userInputPlugin(self, userInputPluginConfig):
0049 if not userInputPluginConfig:
0050 return
0051
0052 if isinstance(userInputPluginConfig, list):
0053 if not all(callable(func) for func in userInputPluginConfig):
0054 raise RuntimeError("Some provided userPlugins are not a callable function")
0055 self._userPlugin = userInputPluginConfig
0056 return
0057
0058 if not callable(userInputPluginConfig):
0059 raise RuntimeError("The provided userPlugin is not a callable function: %s, %s" % (type(userInputPluginConfig),
0060 repr(userInputPluginConfig)))
0061 self._userPlugin.append(userInputPluginConfig)