ATLAS Offline Software
Loading...
Searching...
No Matches
python.MadGraphConfig Namespace Reference

Functions

 _get_nevents (flags, safety)
 _prepare_lhe_for_shower (produced_output, lhe_file)
 _symlink_first_existing (link_name, candidates, overwrite=False)
 MadGraphBaseCfg (flags, **kwargs)
 MadGraphCfg (flags, process_definition, *, safety=None, settings=None, pdf_setting=None, devices=None, catch_errors=None, lhe_version=None, saveProcDir=None, plugin=None, keepJpegs=None, usePMGSettings=None, prepare_lhe_for_shower=False, lhe_file="events.lhe")

Function Documentation

◆ _get_nevents()

python.MadGraphConfig._get_nevents ( flags,
safety )
protected
Helper function to determing number of events to be generated
in MadGraph, based on MaxEvents or nEventsPerJob and a user-provided 
safety factor (the latter defaults to 1.1 to allow for failures 
in showering stage).

Definition at line 12 of file MadGraphConfig.py.

12def _get_nevents(flags, safety):
13 """Helper function to determing number of events to be generated
14 in MadGraph, based on MaxEvents or nEventsPerJob and a user-provided
15 safety factor (the latter defaults to 1.1 to allow for failures
16 in showering stage)."""
17 try:
18 sf = float(safety)
19 except (TypeError, ValueError) as exc:
20 raise RuntimeError(f"safety must be numeric, got {safety}.") from exc
21 if sf <= 0:
22 raise RuntimeError(f"safety must be > 0, got {safety}.")
23
24 base_events = (
25 flags.Exec.MaxEvents
26 if flags.Exec.MaxEvents > 0
27 else flags.Generator.nEventsPerJob
28 )
29
30 return int(base_events * sf)
31
32

◆ _prepare_lhe_for_shower()

python.MadGraphConfig._prepare_lhe_for_shower ( produced_output,
lhe_file )
protected

Definition at line 33 of file MadGraphConfig.py.

33def _prepare_lhe_for_shower(produced_output, lhe_file):
34 primary_output = None
35 if produced_output:
36 if produced_output.endswith(".tar.gz"):
37 root = produced_output[:-7]
38 elif produced_output.endswith(".tgz"):
39 root = produced_output[:-4]
40 elif produced_output.endswith(".gz"):
41 root = produced_output[:-3]
42 else:
43 root, _ = os.path.splitext(produced_output)
44 primary_output = f"{root}.events"
45
46 # If the transform requested a specific TXT output name, symlink the
47 # produced output to the filename that the transform expects
48 candidates = [candidate
49 for candidate in (primary_output,
50 "tmp_LHE_events.events",
51 "events.events")
52 if candidate]
53 if _symlink_first_existing(lhe_file, candidates, overwrite=True):
54 return
55
56 raise RuntimeError(
57 "Could not prepare LHE file for showering. "
58 f"Expected one of: {', '.join(candidates)}"
59 )
60
61

◆ _symlink_first_existing()

python.MadGraphConfig._symlink_first_existing ( link_name,
candidates,
overwrite = False )
protected
Helper function to symlink the first existing file in candidates to link_name.

Definition at line 62 of file MadGraphConfig.py.

62def _symlink_first_existing(link_name, candidates, overwrite=False):
63 """
64 Helper function to symlink the first existing file in candidates to link_name.
65 """
66 if os.path.exists(link_name) and not overwrite:
67 return True
68
69 for candidate in candidates:
70 if not candidate or not os.path.exists(candidate):
71 continue
72 if os.path.abspath(candidate) == os.path.abspath(link_name):
73 return True
74 if os.path.lexists(link_name):
75 os.remove(link_name)
76 os.symlink(os.path.abspath(candidate), link_name)
77 return True
78
79 return False
80
81

◆ MadGraphBaseCfg()

python.MadGraphConfig.MadGraphBaseCfg ( flags,
** kwargs )
Base MadGraph CA fragment. It returns a CA object 
that contains the generator metadata and registers 
default values for steering the MGC object 
(to be created by the top-level config).

Definition at line 82 of file MadGraphConfig.py.

82def MadGraphBaseCfg(flags, **kwargs):
83 """Base MadGraph CA fragment. It returns a CA object
84 that contains the generator metadata and registers
85 default values for steering the MGC object
86 (to be created by the top-level config)."""
87 from MadGraphControl.MGC import (
88 MADGRAPH_CATCH_ERRORS,
89 MADGRAPH_DEVICES,
90 MADGRAPH_PDFSETTING,
91 )
92
93 # Default values for MGC. Use MGC defaults for now, but these
94 # can be declared here in the future.
95 defaults = {
96 "safety": 1.1,
97 "pdf_setting": MADGRAPH_PDFSETTING,
98 "devices": MADGRAPH_DEVICES,
99 "catch_errors": MADGRAPH_CATCH_ERRORS,
100 "lhe_version": 3,
101 "saveProcDir": False,
102 "keepJpegs": False,
103 "usePMGSettings": False,
104 }
105
106 # Create a dictionary with default settings.
107 # This can be used in top-level configs.
108 cfg = {**defaults, **{k: v for k, v in kwargs.items() if v is not None}}
109
110 # Create the CA object adding the generator metadata
111 ca = ComponentAccumulator(EvgenSequenceFactory(EvgenSequence.Generator))
112 ca.merge(
113 GeneratorInfoSvcCfg(flags, Generators=["MadGraph"]),
114 sequenceName=EvgenSequence.Generator.value,
115 )
116
117 return ca, cfg
118
119

◆ MadGraphCfg()

python.MadGraphConfig.MadGraphCfg ( flags,
process_definition,
* ,
safety = None,
settings = None,
pdf_setting = None,
devices = None,
catch_errors = None,
lhe_version = None,
saveProcDir = None,
plugin = None,
keepJpegs = None,
usePMGSettings = None,
prepare_lhe_for_shower = False,
lhe_file = "events.lhe" )
Fragment for configuring a LHE generation step.

This starts from MadGraphBaseCfg and creates a MGC instance
that is later used to call the MadGraphUtils functions that steer
the event generation.

All arguments after * are keyword-only to avoid confusion 
between MadGraphControl settings and CA configuration options.
Set prepare_lhe_for_shower=True when the same job should feed the
produced LHE file into a shower generator.

process_definition is required, the rest is optional.

If prepare_lhe_for_shower is True, the produced LHE file will be 
symlinked to lhe_file (default: events.lhe) 
for later use in the showering step.

Definition at line 120 of file MadGraphConfig.py.

136):
137 """
138 Fragment for configuring a LHE generation step.
139
140 This starts from MadGraphBaseCfg and creates a MGC instance
141 that is later used to call the MadGraphUtils functions that steer
142 the event generation.
143
144 All arguments after * are keyword-only to avoid confusion
145 between MadGraphControl settings and CA configuration options.
146 Set prepare_lhe_for_shower=True when the same job should feed the
147 produced LHE file into a shower generator.
148
149 process_definition is required, the rest is optional.
150
151 If prepare_lhe_for_shower is True, the produced LHE file will be
152 symlinked to lhe_file (default: events.lhe)
153 for later use in the showering step.
154 """
155
156 from MadGraphControl.MGC import MGControl
157 import MadGraphControl.MadGraphUtils as MadGraphUtils
158
159 if isinstance(pdf_setting, MadGraphPDFSets):
160 pdf_setting = get_pdf_set(pdf_setting)
161
162 # TODO: implement deduplication of settings as done in Pythia8Config
163 ca, cfg = MadGraphBaseCfg(
164 flags,
165 safety=safety,
166 pdf_setting=pdf_setting,
167 devices=devices,
168 catch_errors=catch_errors,
169 lhe_version=lhe_version,
170 saveProcDir=saveProcDir,
171 keepJpegs=keepJpegs,
172 usePMGSettings=usePMGSettings,
173 )
174
175 run_card_settings = {} if settings is None else dict(settings)
176
177 # Get nEvents
178 run_card_settings["nevents"] = _get_nevents(flags, cfg["safety"])
179
180 # Create the MGC instance
181 mgc = MGControl(
182 process=process_definition,
183 plugin=plugin,
184 keepJpegs=cfg["keepJpegs"],
185 usePMGSettings=cfg["usePMGSettings"],
186 pdf_setting=cfg["pdf_setting"],
187 devices=cfg["devices"],
188 catch_errors=cfg["catch_errors"],
189 )
190
191 # Bind the MGC instance to the MadGraphUtils module,
192 # so that it can be used in the calls to the MadGraphUtils functions
193 # This is not following the CA logic completely, but it avoids
194 # having to pass the MGC instance through multiple function calls.
195 MadGraphUtils.my_MGC_instance = mgc
196
197 # Create the process directory
198 process_dir = mgc.process_dir
199 MadGraphUtils.modify_run_card(
200 process_dir=process_dir,
201 flags=flags,
202 settings=run_card_settings,
203 pdf_setting=cfg["pdf_setting"],
204 )
205
206 # Generate events
207 MadGraphUtils.generate(process_dir=process_dir, flags=flags, pdf_setting=cfg["pdf_setting"])
208 produced_output = MadGraphUtils.arrange_output(
209 process_dir=process_dir,
210 flags=flags,
211 lhe_version=cfg["lhe_version"],
212 saveProcDir=cfg["saveProcDir"],
213 pdf_setting=cfg["pdf_setting"],
214 )
215
216 # If requested, prepare the produced LHE file for showering
217 # by symlinking it to the filename that pythia expects,
218 # by default "events.lhe".
219 if prepare_lhe_for_shower:
220 _prepare_lhe_for_shower(produced_output, lhe_file)
221
222 # If the transform requested a specific TXT output name, symlink the
223 # produced output to the filename that the transform expects
224 # (only if the file does not exist).
225 requested_output = flags.Output.TXTFileName
226 if requested_output and not os.path.exists(requested_output):
227 root, _ = os.path.splitext(requested_output)
228 candidates = [candidate for candidate in (produced_output, f"{root}.events", "events.events") if candidate]
229 _symlink_first_existing(requested_output, candidates, overwrite=True)
230
231 return ca