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

Functions

 _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, run_card_settings=None, param_card_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

◆ _prepare_lhe_for_shower()

_prepare_lhe_for_shower ( produced_output,
lhe_file )
protected

Definition at line 13 of file MadGraphConfig.py.

13def _prepare_lhe_for_shower(produced_output, lhe_file):
14 # The supported lhe file formats are .lhe, .lhe.gz, .tar.gz, and .tgz.
15 # .tar.gz and .tgz are tarballs that contain a single .lhe file
16 # .gz files can be read directly by Pythia so we don't want to unzip them.
17 if (produced_output and produced_output.endswith(".gz")
18 and not produced_output.endswith((".tar.gz", ".tgz"))):
19 compressed_lhe_file = (
20 lhe_file if lhe_file.endswith(".gz") else f"{lhe_file}.gz"
21 )
22 if _symlink_first_existing(
23 compressed_lhe_file, [produced_output], overwrite=True):
24 return
25 raise RuntimeError(
26 "Could not prepare compressed LHE file for showering. "
27 f"Expected: {produced_output}"
28 )
29
30 primary_output = None
31 if produced_output:
32 if produced_output.endswith(".tar.gz"):
33 root = produced_output[:-7]
34 elif produced_output.endswith(".tgz"):
35 root = produced_output[:-4]
36 else:
37 root, _ = os.path.splitext(produced_output)
38 primary_output = f"{root}.events"
39
40 # If the transform requested a specific TXT output name, symlink the
41 # produced output to the filename that the transform expects
42 candidates = [candidate
43 for candidate in (primary_output,
44 "tmp_LHE_events.events",
45 "events.events")
46 if candidate]
47 if _symlink_first_existing(lhe_file, candidates, overwrite=True):
48 return
49
50 raise RuntimeError(
51 "Could not prepare LHE file for showering. "
52 f"Expected one of: {', '.join(candidates)}"
53 )
54
55

◆ _symlink_first_existing()

_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 56 of file MadGraphConfig.py.

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

◆ MadGraphBaseCfg()

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 76 of file MadGraphConfig.py.

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

◆ MadGraphCfg()

MadGraphCfg ( flags,
process_definition,
* ,
safety = None,
run_card_settings = None,
param_card_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.

run_card_settings maps run_card.dat settings to their requested values.
param_card_settings maps param_card.dat settings to dictionaries of
parameter indices and values.

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 114 of file MadGraphConfig.py.

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