diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py index 35dc29804c801ee335ec7ea1f78e374180defbb7..df80f7113bf98d49a0888c18fd46963f16f04733 100644 --- a/DDG4/python/DDSim/DD4hepSimulation.py +++ b/DDG4/python/DDSim/DD4hepSimulation.py @@ -19,6 +19,7 @@ from DDSim.Helper.Filter import Filter from DDSim.Helper.Random import Random from DDSim.Helper.Action import Action from DDSim.Helper.OutputConfig import OutputConfig +from DDSim.Helper.InputConfig import InputConfig from DDSim.Helper.ConfigHelper import ConfigHelper from DDSim.Helper.MagneticField import MagneticField from DDSim.Helper.ParticleHandler import ParticleHandler @@ -95,6 +96,7 @@ class DD4hepSimulation(object): self.field = MagneticField() self.action = Action() self.outputConfig = OutputConfig() + self.inputConfig = InputConfig() self.guineapig = GuineaPig() self.lcio = LCIO() self.hepmc3 = HepMC3() @@ -391,6 +393,10 @@ class DD4hepSimulation(object): logger.info("++++ Adding Geant4 General Particle Source ++++") actionList.append(self._g4gps) + if self.inputConfig.userInputPlugin: + gen = self.inputConfig.userInputPlugin(self) + actionList.append(gen) + for index, inputFile in enumerate(self.inputFiles, start=4): if inputFile.endswith(".slcio"): gen = DDG4.GeneratorAction(kernel, "LCIOInputAction/LCIO%d" % index) diff --git a/DDG4/python/DDSim/Helper/InputConfig.py b/DDG4/python/DDSim/Helper/InputConfig.py new file mode 100644 index 0000000000000000000000000000000000000000..200ca79b740ef17d464b76393c3635502761bb9b --- /dev/null +++ b/DDG4/python/DDSim/Helper/InputConfig.py @@ -0,0 +1,49 @@ +"""Class for input plugin configuration""" + +from __future__ import absolute_import, unicode_literals +from DDSim.Helper.ConfigHelper import ConfigHelper + + +class InputConfig(ConfigHelper): + """Configuration for Input Files.""" + + def __init__(self): + super(InputConfig, self).__init__() + self._userPlugin = None + + @property + def userInputPlugin(self): + """Set a function to configure an inputStep. + + The function must take a ``DD4hepSimulation`` object as its only argument and return the created generatorAction + ``gen`` (for example). + + For example one can add this to the ddsim steering file: + + def exampleUserPlugin(dd4hepSimulation): + '''Example code for user created plugin. + + :param DD4hepSimulation dd4hepSimulation: The DD4hepSimulation instance, so all parameters can be accessed + :return: GeneratorAction + ''' + from DDG4 import GeneratorAction, Kernel + # Geant4InputAction is the type of plugin, Cry1 just an identifier + gen = GeneratorAction(Kernel(), 'Geant4InputAction/Cry1' , True) + # CRYEventReader is the actual plugin, steeringFile its constructor parameter + gen.Input = 'CRYEventReader|' + 'steeringFile' + # we can give a dictionary of Parameters that has to be interpreted by the setParameters function of the plugin + gen.Parameters = {'DataFilePath': '/path/to/files/data'} + gen.enableUI() + return gen + + SIM.inputConfig.userInputPlugin = exampleUserPlugin + """ + return self._userPlugin + + @userInputPlugin.setter + def userInputPlugin(self, userInputPluginConfig): + if userInputPluginConfig is None: + return + if not callable(userInputPluginConfig): + raise RuntimeError("The provided userPlugin is not a callable function.") + self._userPlugin = userInputPluginConfig