diff --git a/python/Generators/Babayaga.py b/python/Generators/Babayaga.py index 52c02e9..bedbc72 100644 --- a/python/Generators/Babayaga.py +++ b/python/Generators/Babayaga.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import BabayagaProcDB +from .BabayagaProcDB import BabayagaProcDB class Babayaga(GeneratorBase): @@ -13,7 +13,7 @@ def __init__(self, procinfo, settings): self.process = "" self.cuts = "" - self.procDB = BabayagaProcDB.BabayagaProcDB(self.procinfo) + self.procDB = BabayagaProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo() diff --git a/python/Generators/Generators.py b/python/Generators/Generators.py index c757270..6f42542 100644 --- a/python/Generators/Generators.py +++ b/python/Generators/Generators.py @@ -1,10 +1,4 @@ -from . import Sherpa -from . import Whizard -from . import Madgraph -from . import Babayaga -from . import KKMC -from . import Pythia - +import importlib class Generators: """Generator class""" @@ -12,59 +6,31 @@ class Generators: def __init__(self, settings): self.settings = settings self.generator_list = settings.gens() - self.sherpa = None - self.whizard = None - self.madgraph = None - self.babayaga = None - self.proc_info = None def set_process_info(self, proc_info): self.proc_info = proc_info def initialize_generators(self): - if "Sherpa" in self.generator_list: - if Sherpa is not None: - self.sherpa = Sherpa.Sherpa(self.proc_info, self.settings) - self.sherpa.write_file() - self.sherpa.write_key4hepfile() - else: - print("Sherpa module not found. Unable to initialize Sherpa.") - if "Whizard" in self.generator_list: - if Whizard is not None: - self.whizard = Whizard.Whizard(self.proc_info, self.settings) - self.whizard.write_file() - self.whizard.write_key4hepfile() - else: - print("Whizard module not found. Unable to initialize Whizard.") - - if "Madgraph" in self.generator_list: - if Madgraph is not None: - self.madgraph = Madgraph.Madgraph(self.proc_info, self.settings) - self.madgraph.write_file() - self.madgraph.write_key4hepfile() - else: - print("Madgraph module not found. Unable to initialize Madgraph.") - - if "Babayaga" in self.generator_list: - if Babayaga is not None: - self.babayaga = Babayaga.Babayaga(self.proc_info, self.settings) - self.babayaga.write_file() - self.babayaga.write_key4hepfile() - else: - print("Babayaga module not found. Unable to initialize Babayaga.") - - if "KKMC" in self.generator_list: - if KKMC is not None: - self.kkmc = KKMC.KKMC(self.proc_info, self.settings) - self.kkmc.write_file() - self.kkmc.write_key4hepfile() - else: - print("KKMC module not found. Unable to initialize KKMC.") - if "Pythia" in self.generator_list: - if Pythia is not None: - self.pythia = Pythia.Pythia(self.proc_info, self.settings) - self.pythia.write_file() - self.pythia.write_key4hepfile() - else: - print("Pythia module not found. Unable to initialize Pythia.") + for generatorName in self.generator_list: + # get the module + try: + generator = importlib.import_module(f"Generators.{generatorName}") + # get the ClassObject + generatorClass = getattr(generator,generatorName) + # execute the object + generatorObj = generatorClass(self.proc_info, self.settings) + #writing file + generatorObj.write_file() + #writing key4hep file + generatorObj.write_key4hepfile() + + except ModuleNotFoundError: + print(f"{generatorName} python module not found for {self.proc_info.get('_proclabel')}") + except AttributeError: + print(f"{generatorName} class could not be loaded with getattr for {self.proc_info.get('_proclabel')}") + except: + # all that remains is an excption from the execution of the modules + print(f"Execution of {generatorName} for {self.proc_info.get('_proclabel')} resulted in an exception, check the module for problems with loading doownstream modules like the corresponding ProcDB etc") + print("Datacard files and execution scripts not written for this generator") + raise diff --git a/python/Generators/KKMC.py b/python/Generators/KKMC.py index be347e2..2fa5d9d 100644 --- a/python/Generators/KKMC.py +++ b/python/Generators/KKMC.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import KKMCProcDB +from .KKMCProcDB import KKMCProcDB import os, sys @@ -14,7 +14,7 @@ def __init__(self, procinfo, settings): self.process = "" self.cuts = "" - self.procDB = KKMCProcDB.KKMCProcDB(self.procinfo) + self.procDB = KKMCProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo() diff --git a/python/Generators/Madgraph.py b/python/Generators/Madgraph.py index 0d97c45..c3972e5 100644 --- a/python/Generators/Madgraph.py +++ b/python/Generators/Madgraph.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import MadgraphProcDB +from .MadgraphProcDB import MadgraphProcDB from Particles import Particle as part @@ -14,7 +14,7 @@ def __init__(self, procinfo, settings): self.add_header() self.executable = "mg5_aMC" - self.procDB = MadgraphProcDB.MadgraphProcDB(self.procinfo) + self.procDB = MadgraphProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo() diff --git a/python/Generators/Pythia.py b/python/Generators/Pythia.py index 8ebd1bc..1c987d4 100644 --- a/python/Generators/Pythia.py +++ b/python/Generators/Pythia.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import PythiaProcDB +from .PythiaProcDB import PythiaProcDB class Pythia(GeneratorBase): @@ -13,7 +13,7 @@ def __init__(self, procinfo, settings): self.cuts = "" self.PythiaSelectorFileExtension = "selectors" - self.procDB = PythiaProcDB.PythiaProcDB(self.procinfo) + self.procDB = PythiaProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo() diff --git a/python/Generators/Sherpa.py b/python/Generators/Sherpa.py index 0644250..d050e1d 100644 --- a/python/Generators/Sherpa.py +++ b/python/Generators/Sherpa.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import SherpaProcDB +from .SherpaProcDB import SherpaProcDB class Sherpa(GeneratorBase): @@ -12,7 +12,7 @@ def __init__(self, procinfo, settings): self.file = "" self.cuts = "" - self.procDB = SherpaProcDB.SherpaProcDB(self.procinfo) + self.procDB = SherpaProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo() diff --git a/python/Generators/Whizard.py b/python/Generators/Whizard.py index 6a34e3c..cdbad85 100644 --- a/python/Generators/Whizard.py +++ b/python/Generators/Whizard.py @@ -1,5 +1,5 @@ from .GeneratorBase import GeneratorBase -from . import WhizardProcDB +from .WhizardProcDB import WhizardProcDB class Whizard(GeneratorBase): @@ -13,7 +13,7 @@ def __init__(self, procinfo, settings): self.cuts = "" self.integrate = "" - self.procDB = WhizardProcDB.WhizardProcDB(self.procinfo) + self.procDB = WhizardProcDB(self.procinfo) if settings.get("usedefaults", True): self.procDB.write_DBInfo()