diff --git a/MC/bin/o2_hybrid_gen.py b/MC/bin/o2_hybrid_gen.py index b3f0e9048..e0ea12d4c 100755 --- a/MC/bin/o2_hybrid_gen.py +++ b/MC/bin/o2_hybrid_gen.py @@ -7,56 +7,101 @@ # with ini files. The --clone flag allows the user to create multiple instances of the generator list, # which is a useful feature when running multi-threaded event pool. This can be enabled via setting # the --mode flag to 'parallel', which is set "sequential" by default. +# Each --cocktail flag adds one cocktail, which is one entry of the generators list and takes a single fraction. +# A cocktail member given as a path ending in .ini is an external generator configured with that file. +# Cocktails cannot be used in parallel mode, where the hybrid generator takes the event from a shared +# queue and would merge the particles of no generator at all, producing empty events. +# The fractions, which are 1 for each entry by default, can be set with --fractions: one integer per +# entry of the generators list, cocktails included. The entries are ordered as --gen, then --iniFile, +# then --cocktail, independently of the order the flags are given in, and are counted after cloning. # Since the script uses the ROOT dictionary to import the parameters names, O2 must be loaded, otherwise # the template generation will not work. # Example: -# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8 boxgen external extkinO2 hepmc pythia8hf --clone 2 \ +# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8 boxgen external hepmc pythia8hf --clone 2 \ # --output config.json --mode parallel --iniFile /path/to/file0.ini /path/to/file1.ini +# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8pp --cocktail pythia8hf boxgen \ +# --cocktail boxgen /path/to/external.ini \ +# --fractions 10 2 1 --output config.json import argparse import json import ROOT -import cppyy -import numpy as np -# Get the TClass object for the struct -tclass = ROOT.TClass.GetClass("o2::eventgen::Pythia8GenConfig") -tclass1 = ROOT.TClass.GetClass("o2::eventgen::BoxGenConfig") +# The hybrid generator builds the configuration of a generator by deserialising the JSON with +# TBufferJSON::FromJSON, see GeneratorHybrid::confSetter. The template is therefore written with the +# symmetric serialisation of a default constructed configuration class, so that the parameters and +# their default values always match the loaded O2sim. +ROOT.gInterpreter.Declare(r''' +#include "TBufferJSON.h" +#include +template +std::string o2dpgHybridDefaultJSON() { T conf{}; return std::string(TBufferJSON::ToJSON(&conf).Data()); } +''') gens_params = {"pythia8": "o2::eventgen::Pythia8GenConfig", "external": "o2::eventgen::ExternalGenConfig", - "evtpool": "o2::eventgen::EventPoolGenConfig", "extkinO2": "o2::eventgen::O2KineGenConfig", + "evtpool": "o2::eventgen::EventPoolGenConfig", "hepmc": "o2::eventgen::HepMCGenConfig", "boxgen": "o2::eventgen::BoxGenConfig"} cmd_params = "o2::eventgen::FileOrCmdGenConfig" -gens_instances = {"pythia8": ROOT.o2.eventgen.Pythia8GenConfig(), "external": ROOT.o2.eventgen.ExternalGenConfig(), - "evtpool": ROOT.o2.eventgen.EventPoolGenConfig(), "extkinO2": ROOT.o2.eventgen.O2KineGenConfig(), - "hepmc": ROOT.o2.eventgen.HepMCGenConfig(), "boxgen": ROOT.o2.eventgen.BoxGenConfig()} -cmd_instance = ROOT.o2.eventgen.FileOrCmdGenConfig() - -def get_params(instance, class_name): - tclass = ROOT.TClass.GetClass(class_name) - members = tclass.GetListOfDataMembers() - params = {} - for member in members: - if isinstance(member, ROOT.TDataMember): - member_value = getattr(instance, member.GetName()) - params[member.GetName()] = member_value - # replace C++ strings and arrays - for key, value in params.items(): - if isinstance(value, cppyy.gbl.std.string): - # convert to a JSON serialisable python string - params[key] = str(value) - elif hasattr(value, '__len__') and hasattr(value, '__getitem__'): - # convert C++ numerical array to python array, no string arrays are declared as parameters, so far - params[key] = np.array(value).tolist() + +# generators for which the hybrid generator uses a fixed configuration file +noConfGen = ["pythia8pp", "pythia8hf", "pythia8hi", "pythia8powheg"] + +def get_params(class_name): + params = json.loads(str(ROOT.o2dpgHybridDefaultJSON[class_name]())) + params.pop("_typename", None) # ROOT bookkeeping, not a parameter return params +def make_gen(gen, add_trigger): + # one entry of the generators list, with the parameters of the loaded O2sim at their default + if gen.endswith(".ini"): + # an external generator configured with the given ini file + configs = get_params(gens_params["external"]) + configs["iniFile"] = gen + entry = { + 'name': 'external', + 'config': configs + } + elif gen in gens_params: + if gen == "hepmc": + entry = { + 'name': gen, + 'config': { + "configcmd": get_params(cmd_params), + "confighepmc": get_params(gens_params[gen]) + } + } + else: + entry = { + 'name': gen, + 'config': get_params(gens_params[gen]) + } + elif gen in noConfGen: + entry = { + "name": gen, + "config": "" + } + else: + print(f"Generator {gen} not found in the list of available generators") + if gen == "extkinO2": + print("The hybrid generator has no branch for 'extkinO2', use 'evtpool' to read O2 kinematics files") + exit(1) + add_trigger(entry) + return entry + def main(): parser = argparse.ArgumentParser(description='Create a JSON file from command line flags.') parser.add_argument('--gen', type=str, nargs='+', help='List of generators to be used') parser.add_argument('--iniFile', type=str, nargs='+', help='List of external generators configured with ini files') + parser.add_argument('--cocktail', type=str, nargs='+', action='append', metavar='GEN', + help='Generators of one cocktail, whose particles are merged into a single event; ' + 'a member ending in .ini is an external generator configured with that file. ' + 'Not available in parallel mode') parser.add_argument('--mode', type=str, help='Run generator in sequential or parallel mode for quicker event generation (multi-threading)') parser.add_argument('--output', type=str, required=True, help='Output JSON file path') parser.add_argument('--clone', type=int, help='Number of clones to make of the generator list') + parser.add_argument('--fractions', type=int, nargs='+', + help='Fraction of each entry of the generators list, a cocktail counting as one entry; ' + 'the entries are ordered as --gen, --iniFile, --cocktail and counted after cloning') parser.add_argument('--trigger', action='store_true', help='Add triggers to the template JSON file') args = parser.parse_args() @@ -76,55 +121,37 @@ def main(): add_trigger = lambda d: d.update({"triggers": {"mode": "off", "specs": [{"macro": "", "function": ""}]}}) if args.trigger else None # put in a list all the elementes in the gen flag - noConfGen = ["pythia8pp", "pythia8hf", "pythia8hi", "pythia8powheg"] gens = [] - if args.gen is None and args.iniFile is None: + if args.gen is None and args.iniFile is None and args.cocktail is None: print("No generators specified") exit(1) if args.gen: print(f"Generators to be used: {args.gen}") for gen in args.gen: - if gen in gens_params: - if gen == "hepmc": - configs = [get_params(cmd_instance, cmd_params), get_params(gens_instances[gen], gens_params[gen])] - gens.append({ - 'name': gen, - 'config': { - "configcmd": configs[0], - "confighepmc": configs[1] - } - }) - add_trigger(gens[-1]) - else: - configs = get_params(gens_instances[gen],gens_params[gen]) - gens.append({ - 'name': gen, - 'config': configs - }) - add_trigger(gens[-1]) - elif gen in noConfGen: - gens.append({ - "name": gen, - "config": "" - }) - add_trigger(gens[-1]) - else: - print(f"Generator {gen} not found in the list of available generators") - exit(1) + gens.append(make_gen(gen, add_trigger)) if args.iniFile: print(f"External generators to be used: {args.iniFile}") for ini in args.iniFile: - if ".ini" != ini[-4:]: + if not ini.endswith(".ini"): print(f"File {ini} is not an ini file") exit(1) - configs = get_params(gens_instances["external"],gens_params["external"]) - configs["iniFile"] = ini + gens.append(make_gen(ini, add_trigger)) + + # each cocktail is a single entry of the generators list, grouping generators whose + # particles are merged into the same event + if args.cocktail: + if mode == "parallel": + # in parallel mode the hybrid generator picks the event from a shared queue and never + # collects the members of a cocktail, so every event would come out empty + print("Cocktails cannot be used in parallel mode, where they would produce empty events") + print("Please use the sequential mode for a configuration containing cocktails") + exit(1) + for cocktail in args.cocktail: + print(f"Cocktail to be used: {cocktail}") gens.append({ - 'name': 'external', - 'config': configs + 'cocktail': [make_gen(gen, add_trigger) for gen in cocktail] }) - add_trigger(gens[-1]) if args.clone: if args.clone < 2: @@ -135,16 +162,26 @@ def main(): gens = gens * args.clone - # fill fractions with 1 for each generator - fractions = [1] * len(gens) - + # fill fractions with 1 for each generator, unless they are provided + if args.fractions: + if len(args.fractions) != len(gens): + print(f"{len(args.fractions)} fractions provided for {len(gens)} entries of the generators list") + print("One fraction per entry is needed, a cocktail counting as one entry") + exit(1) + if any(fraction < 0 for fraction in args.fractions): + print("The fractions cannot be negative") + exit(1) + fractions = args.fractions + else: + fractions = [1] * len(gens) + # Put gens and fractions in the data dictionary data = { "mode": mode, "generators": gens, "fractions": fractions } - + # Write the data dictionary to a JSON file with open(args.output, 'w') as f: json.dump(data, f, indent=2)