Skip to content

Commit 44e7a10

Browse files
committed
Improve hybrid configuration maker
1 parent 1370c5f commit 44e7a10

1 file changed

Lines changed: 104 additions & 67 deletions

File tree

‎MC/bin/o2_hybrid_gen.py‎

Lines changed: 104 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,101 @@
77
# with ini files. The --clone flag allows the user to create multiple instances of the generator list,
88
# which is a useful feature when running multi-threaded event pool. This can be enabled via setting
99
# the --mode flag to 'parallel', which is set "sequential" by default.
10+
# Each --cocktail flag adds one cocktail, which is one entry of the generators list and takes a single fraction.
11+
# A cocktail member given as a path ending in .ini is an external generator configured with that file.
12+
# Cocktails cannot be used in parallel mode, where the hybrid generator takes the event from a shared
13+
# queue and would merge the particles of no generator at all, producing empty events.
14+
# The fractions, which are 1 for each entry by default, can be set with --fractions: one integer per
15+
# entry of the generators list, cocktails included. The entries are ordered as --gen, then --iniFile,
16+
# then --cocktail, independently of the order the flags are given in, and are counted after cloning.
1017
# Since the script uses the ROOT dictionary to import the parameters names, O2 must be loaded, otherwise
1118
# the template generation will not work.
1219
# Example:
13-
# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8 boxgen external extkinO2 hepmc pythia8hf --clone 2 \
20+
# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8 boxgen external hepmc pythia8hf --clone 2 \
1421
# --output config.json --mode parallel --iniFile /path/to/file0.ini /path/to/file1.ini
22+
# $O2DPG_ROOT/MC/bin/o2_hybrid_gen.py --gen pythia8pp --cocktail pythia8hf boxgen \
23+
# --cocktail boxgen /path/to/external.ini \
24+
# --fractions 10 2 1 --output config.json
1525

1626
import argparse
1727
import json
1828
import ROOT
19-
import cppyy
20-
import numpy as np
2129

22-
# Get the TClass object for the struct
23-
tclass = ROOT.TClass.GetClass("o2::eventgen::Pythia8GenConfig")
24-
tclass1 = ROOT.TClass.GetClass("o2::eventgen::BoxGenConfig")
30+
# The hybrid generator builds the configuration of a generator by deserialising the JSON with
31+
# TBufferJSON::FromJSON, see GeneratorHybrid::confSetter. The template is therefore written with the
32+
# symmetric serialisation of a default constructed configuration class, so that the parameters and
33+
# their default values always match the loaded O2sim.
34+
ROOT.gInterpreter.Declare(r'''
35+
#include "TBufferJSON.h"
36+
#include <string>
37+
template <typename T>
38+
std::string o2dpgHybridDefaultJSON() { T conf{}; return std::string(TBufferJSON::ToJSON(&conf).Data()); }
39+
''')
2540

2641
gens_params = {"pythia8": "o2::eventgen::Pythia8GenConfig", "external": "o2::eventgen::ExternalGenConfig",
27-
"evtpool": "o2::eventgen::EventPoolGenConfig", "extkinO2": "o2::eventgen::O2KineGenConfig",
42+
"evtpool": "o2::eventgen::EventPoolGenConfig",
2843
"hepmc": "o2::eventgen::HepMCGenConfig", "boxgen": "o2::eventgen::BoxGenConfig"}
2944
cmd_params = "o2::eventgen::FileOrCmdGenConfig"
30-
gens_instances = {"pythia8": ROOT.o2.eventgen.Pythia8GenConfig(), "external": ROOT.o2.eventgen.ExternalGenConfig(),
31-
"evtpool": ROOT.o2.eventgen.EventPoolGenConfig(), "extkinO2": ROOT.o2.eventgen.O2KineGenConfig(),
32-
"hepmc": ROOT.o2.eventgen.HepMCGenConfig(), "boxgen": ROOT.o2.eventgen.BoxGenConfig()}
33-
cmd_instance = ROOT.o2.eventgen.FileOrCmdGenConfig()
34-
35-
def get_params(instance, class_name):
36-
tclass = ROOT.TClass.GetClass(class_name)
37-
members = tclass.GetListOfDataMembers()
38-
params = {}
39-
for member in members:
40-
if isinstance(member, ROOT.TDataMember):
41-
member_value = getattr(instance, member.GetName())
42-
params[member.GetName()] = member_value
43-
# replace C++ strings and arrays
44-
for key, value in params.items():
45-
if isinstance(value, cppyy.gbl.std.string):
46-
# convert to a JSON serialisable python string
47-
params[key] = str(value)
48-
elif hasattr(value, '__len__') and hasattr(value, '__getitem__'):
49-
# convert C++ numerical array to python array, no string arrays are declared as parameters, so far
50-
params[key] = np.array(value).tolist()
45+
46+
# generators for which the hybrid generator uses a fixed configuration file
47+
noConfGen = ["pythia8pp", "pythia8hf", "pythia8hi", "pythia8powheg"]
48+
49+
def get_params(class_name):
50+
params = json.loads(str(ROOT.o2dpgHybridDefaultJSON[class_name]()))
51+
params.pop("_typename", None) # ROOT bookkeeping, not a parameter
5152
return params
5253

54+
def make_gen(gen, add_trigger):
55+
# one entry of the generators list, with the parameters of the loaded O2sim at their default
56+
if gen.endswith(".ini"):
57+
# an external generator configured with the given ini file
58+
configs = get_params(gens_params["external"])
59+
configs["iniFile"] = gen
60+
entry = {
61+
'name': 'external',
62+
'config': configs
63+
}
64+
elif gen in gens_params:
65+
if gen == "hepmc":
66+
entry = {
67+
'name': gen,
68+
'config': {
69+
"configcmd": get_params(cmd_params),
70+
"confighepmc": get_params(gens_params[gen])
71+
}
72+
}
73+
else:
74+
entry = {
75+
'name': gen,
76+
'config': get_params(gens_params[gen])
77+
}
78+
elif gen in noConfGen:
79+
entry = {
80+
"name": gen,
81+
"config": ""
82+
}
83+
else:
84+
print(f"Generator {gen} not found in the list of available generators")
85+
if gen == "extkinO2":
86+
print("The hybrid generator has no branch for 'extkinO2', use 'evtpool' to read O2 kinematics files")
87+
exit(1)
88+
add_trigger(entry)
89+
return entry
90+
5391
def main():
5492
parser = argparse.ArgumentParser(description='Create a JSON file from command line flags.')
5593
parser.add_argument('--gen', type=str, nargs='+', help='List of generators to be used')
5694
parser.add_argument('--iniFile', type=str, nargs='+', help='List of external generators configured with ini files')
95+
parser.add_argument('--cocktail', type=str, nargs='+', action='append', metavar='GEN',
96+
help='Generators of one cocktail, whose particles are merged into a single event; '
97+
'a member ending in .ini is an external generator configured with that file. '
98+
'Not available in parallel mode')
5799
parser.add_argument('--mode', type=str, help='Run generator in sequential or parallel mode for quicker event generation (multi-threading)')
58100
parser.add_argument('--output', type=str, required=True, help='Output JSON file path')
59101
parser.add_argument('--clone', type=int, help='Number of clones to make of the generator list')
102+
parser.add_argument('--fractions', type=int, nargs='+',
103+
help='Fraction of each entry of the generators list, a cocktail counting as one entry; '
104+
'the entries are ordered as --gen, --iniFile, --cocktail and counted after cloning')
60105
parser.add_argument('--trigger', action='store_true', help='Add triggers to the template JSON file')
61106

62107
args = parser.parse_args()
@@ -76,55 +121,37 @@ def main():
76121
add_trigger = lambda d: d.update({"triggers": {"mode": "off", "specs": [{"macro": "", "function": ""}]}}) if args.trigger else None
77122

78123
# put in a list all the elementes in the gen flag
79-
noConfGen = ["pythia8pp", "pythia8hf", "pythia8hi", "pythia8powheg"]
80124
gens = []
81-
if args.gen is None and args.iniFile is None:
125+
if args.gen is None and args.iniFile is None and args.cocktail is None:
82126
print("No generators specified")
83127
exit(1)
84128
if args.gen:
85129
print(f"Generators to be used: {args.gen}")
86130
for gen in args.gen:
87-
if gen in gens_params:
88-
if gen == "hepmc":
89-
configs = [get_params(cmd_instance, cmd_params), get_params(gens_instances[gen], gens_params[gen])]
90-
gens.append({
91-
'name': gen,
92-
'config': {
93-
"configcmd": configs[0],
94-
"confighepmc": configs[1]
95-
}
96-
})
97-
add_trigger(gens[-1])
98-
else:
99-
configs = get_params(gens_instances[gen],gens_params[gen])
100-
gens.append({
101-
'name': gen,
102-
'config': configs
103-
})
104-
add_trigger(gens[-1])
105-
elif gen in noConfGen:
106-
gens.append({
107-
"name": gen,
108-
"config": ""
109-
})
110-
add_trigger(gens[-1])
111-
else:
112-
print(f"Generator {gen} not found in the list of available generators")
113-
exit(1)
131+
gens.append(make_gen(gen, add_trigger))
114132

115133
if args.iniFile:
116134
print(f"External generators to be used: {args.iniFile}")
117135
for ini in args.iniFile:
118-
if ".ini" != ini[-4:]:
136+
if not ini.endswith(".ini"):
119137
print(f"File {ini} is not an ini file")
120138
exit(1)
121-
configs = get_params(gens_instances["external"],gens_params["external"])
122-
configs["iniFile"] = ini
139+
gens.append(make_gen(ini, add_trigger))
140+
141+
# each cocktail is a single entry of the generators list, grouping generators whose
142+
# particles are merged into the same event
143+
if args.cocktail:
144+
if mode == "parallel":
145+
# in parallel mode the hybrid generator picks the event from a shared queue and never
146+
# collects the members of a cocktail, so every event would come out empty
147+
print("Cocktails cannot be used in parallel mode, where they would produce empty events")
148+
print("Please use the sequential mode for a configuration containing cocktails")
149+
exit(1)
150+
for cocktail in args.cocktail:
151+
print(f"Cocktail to be used: {cocktail}")
123152
gens.append({
124-
'name': 'external',
125-
'config': configs
153+
'cocktail': [make_gen(gen, add_trigger) for gen in cocktail]
126154
})
127-
add_trigger(gens[-1])
128155

129156
if args.clone:
130157
if args.clone < 2:
@@ -135,16 +162,26 @@ def main():
135162
gens = gens * args.clone
136163

137164

138-
# fill fractions with 1 for each generator
139-
fractions = [1] * len(gens)
140-
165+
# fill fractions with 1 for each generator, unless they are provided
166+
if args.fractions:
167+
if len(args.fractions) != len(gens):
168+
print(f"{len(args.fractions)} fractions provided for {len(gens)} entries of the generators list")
169+
print("One fraction per entry is needed, a cocktail counting as one entry")
170+
exit(1)
171+
if any(fraction < 0 for fraction in args.fractions):
172+
print("The fractions cannot be negative")
173+
exit(1)
174+
fractions = args.fractions
175+
else:
176+
fractions = [1] * len(gens)
177+
141178
# Put gens and fractions in the data dictionary
142179
data = {
143180
"mode": mode,
144181
"generators": gens,
145182
"fractions": fractions
146183
}
147-
184+
148185
# Write the data dictionary to a JSON file
149186
with open(args.output, 'w') as f:
150187
json.dump(data, f, indent=2)

0 commit comments

Comments
 (0)