ATLAS Offline Software
Loading...
Searching...
No Matches
Herwig7Control.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2# \file Herwig7Control.py
3# \brief Main python interface for %Herwig7 for preparing the event generation
4# \author Daniel Rauch (daniel.rauch@desy.de)
5# \author Lukas Kretschmann (lukas.kretschmann@cern.ch)
6#
7# This part of the interface provides functionality for running all the tasks
8# necessary to initialize and prepare the event generation.
9# More concretely, it handles the read or alternatively the build/integrate/
10# mergegrids steps in order to produce the Herwig runfile and all other
11# ingredients for a run, possibly also creating a gridpack.
12# The event generation itself starting from reading the runfile is handled
13# in Herwig7_i/Herwig7.h and src/Herwig7.cxx.
14
15import os, shutil, subprocess, sys
16
17from . import Herwig7Utils as hw7Utils
18from . import Herwig7JOChecker as JOChecker
19from . import Herwig7ConfigDecoder as ConfigDecoder
20
21from AthenaCommon import Logging
22athMsgLog = Logging.logging.getLogger('Herwig7Control')
23
24
25# \brief Get path to the `share/Herwig` folder
26#
27# Try to get it from the `InstallArea` first.
28# If this fails fall back to `$HERWIG7_PATH/share/Herwig`
29#
31
32 cmt_paths = os.environ.get("CMAKE_PREFIX_PATH")
33 cmt_config = os.environ.get("BINARY_TAG")
34
35 # trying to get it from the `InstallArea`
36 for path in cmt_paths.split(':'):
37 path = os.path.join(path, "InstallArea", cmt_config, "share")
38 try:
39 filelist = os.listdir(path)
40 except Exception:
41 filelist = []
42 if "HerwigDefaults.rpo" in filelist: return(path)
43
44 # falling back to `$HERWIG7_PATH`
45 path = os.path.join(os.environ['HERWIG7_PATH'], 'share/Herwig')
46 if os.path.isfile(os.path.join(path, 'HerwigDefaults.rpo')):
47 return(path)
48
49 # raise exception if none of the two methods work out
50 raise RuntimeError(hw7Utils.ansi_format_error('Could not find a valid share/Herwig folder'))
51
52
53# proper handling with path set in External/Herwig7/cmt/requirements
54herwig7_path = os.environ['HERWIG7_PATH']
55herwig7_bin_path = os.path.join(herwig7_path, 'bin')
56herwig7_share_path = get_share_path()
57
58herwig7_binary = os.path.join(herwig7_bin_path, 'Herwig')
59
60
61# Do the read/run sequence.
62#
63# This function should provide the read and run step in one go
64def run(gen_config):
65
66 # perform the read step
67 do_read(gen_config)
68
69 # start the event generation
70 do_run(gen_config, cleanup_herwig_scratch=False)
71
72
73# Do the build, integrate, mergegrids and run step in one go
74# without creating a gridpack
75#
76# \param[in] cleanup_herwig_scratch Remove `Herwig-scratch` or 'Herwig-cache' folder after event generation to save disk space
77def matchbox_run(gen_config, integration_jobs, cleanup_herwig_scratch):
78
79 # perform build/integrate/mergegrids sequence
80 do_build_integrate_mergegrids(gen_config, integration_jobs)
81
82 # start the event generation
83 do_run(gen_config, cleanup_herwig_scratch)
84
85
86# Either do the build, integrate and mergegrids steps and create a gridpack
87# or extract it and generate events from it
88#
89# \param[in] cleanup_herwig_scratch Remove `Herwig-scratch` or 'Herwig-cache' folder after event generation to save disk space
90def matchbox_run_gridpack(gen_config, integration_jobs, gridpack_name, cleanup_herwig_scratch, integrate):
91
92 # print start banner including version numbers
93 log(message=start_banner())
94
95 if not gridpack_name or integrate:
96
97 # create infile from jobOption commands
98 write_infile(gen_config)
99
100 # do build/integrate/mergegrids sequence
101 xsec, err = do_build_integrate_mergegrids(gen_config, integration_jobs)
102
103 # compress infile, runfile and process folder to gridpack tarball
104 do_compress_gridpack(gen_config.run_name, gridpack_name)
105
106 # display banner and exit
107 log(message=exit_banner(gridpack_name, xsec, err))
108 sys.exit(0)
109
110 else:
111
112 # unpack the gridpack
113 DSIS_dir = gen_config.runArgs.jobConfig[0]+"/"
114 do_uncompress_gridpack(DSIS_dir+gridpack_name)
115 athMsgLog.info("Finished unpacking the gridpack")
116
117 # start the event generation
118 do_run(gen_config, cleanup_herwig_scratch)
119
120
121
122def do_step(step, command, logfile_name=None):
123
124 athMsgLog.info(hw7Utils.ansi_format_info("Starting Herwig7 '{}' step with command '{}'".format(step, ' '.join(command))))
125
126 logfile = open(logfile_name, 'w') if logfile_name else None
127 do = subprocess.Popen(command, stdout=logfile, stderr=logfile)
128 do.wait()
129 if not do.returncode == 0:
130 raise RuntimeError(hw7Utils.ansi_format_error("Some error occured during the '{}' step.".format(step)))
131
132 if logfile:
133 athMsgLog.info("Content of %s log file '%s':", step, logfile_name)
134 athMsgLog.info("")
135 with open(logfile_name, 'r') as logfile:
136 for line in logfile:
137 athMsgLog.info(' %s', line.rstrip('\n'))
138 athMsgLog.info("")
139
140
142 athMsgLog.info(hw7Utils.ansi_format_info("Doing abort"))
143 sys.exit(0)
144
145
146# Do the read step
147def do_read(gen_config):
148
149 # print start banner including version numbers
150 log(message=start_banner())
151
152 # create infile from JobOption object
153 write_infile(gen_config)
154
155 # copy HerwigDefaults.rpo to the current working directory
157
158 # call Herwig7 binary to do the read step
159 share_path = get_share_path()
160 do_step('read', [herwig7_binary, 'read', get_infile_name(gen_config.run_name), '-I', share_path])
161
162# Do the read step and re-use an already existing infile
164
165 # print start banner including version numbers
166 log(message=start_banner())
167
168 # copy HerwigDefaults.rpo to the current working directory
170
171 # call Herwig7 binary to do the read step
172 share_path = get_share_path()
173 do_step('read', [herwig7_binary, 'read', gen_config.infile_name, '-I', share_path])
174
175
176# Do the build step
177def do_build(gen_config, integration_jobs):
178
179 # print start banner including version numbers
180 log(message=start_banner())
181
182 # create infile from JobOption object
183 write_infile(gen_config)
184
185 # copy HerwigDefaults.rpo to the current working directory
187
188 # call the Herwig7 binary to do the build step
189 share_path = get_share_path()
190 do_step('build', [herwig7_binary, 'build', get_infile_name(gen_config.run_name), '-I', share_path, '-y '+str(integration_jobs)])
191
192
193# Do the integrate step for one specific integration job
194# \todo provide info about the range
195def do_integrate(run_name, integration_job):
196
197 runfile_name = get_runfile_name(run_name)
198
199 integrate_log = run_name+'.integrate'+str(integration_job)+'.log'
200 integrate_command = [herwig7_binary,'integrate',runfile_name,'--jobid='+str(integration_job)]
201
202 do_step('integrate', integrate_command, integrate_log)
203
204
205# This function provides the mergegrids step
206def do_mergegrids(run_name, integration_jobs):
207
208 runfile_name = get_runfile_name(run_name)
209 mergegrids_command = [herwig7_binary, 'mergegrids', runfile_name]
210
211 do_step('mergegrids', mergegrids_command)
212
213 # calculate the cross section from the integration logfiles and possibly warn about low accuracy
214 xsec, err = hw7Utils.get_cross_section(run_name, integration_jobs)
215
216 return(xsec, err)
217
218
219# Subsequent build, integrate and mergegrid steps
220def do_build_integrate_mergegrids(gen_config, integration_jobs):
221
222 # run build step
223 do_build(gen_config, integration_jobs)
224
225 # run integration jobs in parallel subprocesses
226 runfile_name = get_runfile_name(gen_config.run_name)
227 athMsgLog.info(hw7Utils.ansi_format_info('Starting integration with {} jobs'.format(integration_jobs)))
228
229 integration_procs = []
230 for integration_job in range(integration_jobs):
231 integrate_log = gen_config.run_name+'.integrate'+str(integration_job)+'.log'
232 integrate_command = [herwig7_binary,'integrate',runfile_name,'--jobid='+str(integration_job)]
233 integration_procs.append(hw7Utils.Process(integration_job, integrate_command, integrate_log))
234
235 integration_handler = hw7Utils.ProcessHandler(integration_procs, athMsgLog)
236 if not integration_handler.success():
237 raise RuntimeError(hw7Utils.ansi_format_error('Not all of the integration jobs finished successfully'))
238
239 athMsgLog.info(hw7Utils.ansi_format_ok('All integration jobs finished successfully'))
240
241 # combine the different integration grids
242 xsec, err = do_mergegrids(gen_config.run_name, integration_jobs)
243
244 return(xsec, err)
245
246
247def do_compress_gridpack(run_name, gridpack_name):
248
249 if not (gridpack_name.endswith('.tar.gz') or gridpack_name.endswith('.tgz')): gridpack_name += '.tar.gz'
250 infile_name = get_infile_name(run_name)
251 runfile_name = get_runfile_name(run_name)
252 version = herwig_version()
253 athMsgLog.debug("Scratch area, this is Herwig version '{}'".format(version))
254 if "7.1" in version or "7.0" in version:
255 do_step('compress', ['tar', 'czf', gridpack_name, infile_name, runfile_name, 'Herwig-scratch'])
256 else:
257 do_step('compress', ['tar', 'czf', gridpack_name, infile_name, runfile_name, 'Herwig-cache'])
258
259
260def do_uncompress_gridpack(gridpack_name):
261
262 athMsgLog.info("unpacking gridpack '%s'", gridpack_name)
263 do_step('uncompress', ['tar', 'xzf', gridpack_name])
264
265
266# \param[in] cleanup_herwig_scratch Remove `Herwig-scratch` folder after event generation to save disk space
267def do_run(gen_config, cleanup_herwig_scratch=True):
268
269 # this is necessary to make Herwig aware of the name of the run file
270 gen_config.genSeq.Herwig7.RunFile = get_runfile_name(gen_config.run_name)
271
272 # check the options in the .in file
273 JOChecker.check_file()
274
275 # decode the run file to get list of all parameters
276 ConfigDecoder.DecodeRunCard(input_file = gen_config.genSeq.Herwig7.RunFile)
277
278 # overwrite athena's seed for the random number generator
279 if gen_config.runArgs.randomSeed is None:
280 gen_config.genSeq.Herwig7.UseRandomSeedFromGeneratetf = False
281 else:
282 gen_config.genSeq.Herwig7.UseRandomSeedFromGeneratetf = True
283 gen_config.genSeq.Herwig7.RandomSeedFromGeneratetf = gen_config.runArgs.randomSeed
284
285 # set matrix element PDF name in the Herwig7 C++ class
286 gen_config.genSeq.Herwig7.PDFNameME = gen_config.me_pdf_name
287
288 # set underlying event PDF name in the Herwig7 C++ class
289 gen_config.genSeq.Herwig7.PDFNameMPI = gen_config.mpi_pdf_name
290
291 # possibly delete Herwig-scratch folder after finishing the event generation
292 gen_config.genSeq.Herwig7.CleanupHerwigScratch = cleanup_herwig_scratch
293
294 # don't break out here so that the job options can be finished and the C++
295 # part of the interface can take over and generate the events
296 athMsgLog.info(hw7Utils.ansi_format_info("Returning to the job options and starting the event generation afterwards"))
297
298
299# Do the run step and re-use an already existing runfile
301
302 # this is necessary to make Herwig aware of the name of the run file
303 gen_config.genSeq.Herwig7.RunFile = gen_config.runfile_name
304
305 # overwrite athena's seed for the random number generator
306 if gen_config.runArgs.randomSeed is None:
307 gen_config.genSeq.Herwig7.UseRandomSeedFromGeneratetf = False
308 else:
309 gen_config.genSeq.Herwig7.UseRandomSeedFromGeneratetf = True
310 gen_config.genSeq.Herwig7.RandomSeedFromGeneratetf = gen_config.runArgs.randomSeed
311
312 # don't break out here so that the job options can be finished and the C++
313 # part of the interface can take over and generate the events
314 athMsgLog.info(hw7Utils.ansi_format_info("Returning to the job options and starting the event generation afterwards"))
315
316
317# utility functions -----------------------------------------------------------
318
319
321
322 versions = get_software_versions()
323 return(' '.join(versions[0].split()[1:]))
324
326
327 versions = get_software_versions()
328 return(' '.join(versions[1].split()[1:]))
329
331
332 herwig_version_number = herwig_version()
333 thepeg_version_number = thepeg_version()
334 herwig_version_space = ' '.join(['' for i in range(14-len(herwig_version_number))])
335 thepeg_version_space = ' '.join(['' for i in range(14-len(thepeg_version_number))])
336
337 banner = ''
338 banner += "#####################################\n"
339 banner += "## {} ##\n".format(hw7Utils.ansi_format_ok("---------------------------"))
340 banner += "## {} ##\n".format(hw7Utils.ansi_format_ok("Starting HERWIG 7 in ATHENA"))
341 banner += "## {} ##\n".format(hw7Utils.ansi_format_ok("---------------------------"))
342 banner += "## ##\n"
343 banner += "## with software versions: ##\n"
344 banner += "## - Herwig7: {}{} ##\n".format(herwig_version_number, herwig_version_space)
345 banner += "## - ThePEG: {}{} ##\n".format(thepeg_version_number, thepeg_version_space)
346 banner += "## ##\n"
347 banner += "#####################################\n"
348 return(banner)
349
350
352
353 return(subprocess.check_output([herwig7_binary,'--version'], text=True).splitlines())
354
355
356def get_infile_name(run_name="Herwig-Matchbox"):
357
358 return('{}.in'.format(run_name))
359
360
361def get_setupfile_name(run_name="Herwig-Matchbox"):
362
363 return('{}.setupfile.in'.format(run_name))
364
365
366def get_runfile_name(run_name="Herwig-Matchbox"):
367
368 return('{}.run'.format(run_name) if not run_name.endswith('.run') else run_name)
369
370
371def write_infile(gen_config, print_infile=True):
372
373 # lock settings to prevent modification from within the job options after infile was written to disk
374 gen_config.default_commands.lock()
375 gen_config.commands.lock()
376
377 infile_name = get_infile_name(gen_config.run_name)
378 if print_infile: athMsgLog.info("")
379 athMsgLog.info(hw7Utils.ansi_format_info("Writing infile '{}'".format(infile_name)))
380 commands = \
381 gen_config.global_pre_commands().splitlines() \
382 + gen_config.local_pre_commands().splitlines() \
383 + ["",
384 "## ================",
385 "## Default Commands",
386 "## ================"] \
387 + str(gen_config.default_commands.commands).splitlines() \
388 + ["",
389 "## ========================",
390 "## Commands from jobOptions",
391 "## ========================"] \
392 + str(gen_config.commands.commands).splitlines() \
393 + gen_config.local_post_commands().splitlines()
394 try:
395 with open(infile_name, 'w') as infile:
396 for command in commands:
397 infile.write(command+'\n')
398 except Exception:
399 raise RuntimeError('Could not write Herwig/Matchbox infile')
400
401 if print_infile:
402 athMsgLog.info("")
403 for command in commands:
404 athMsgLog.info(' %s', command)
405 athMsgLog.info("")
406
407
408def write_setupfile(run_name, commands, print_setupfile=True):
409
410 setupfile_name = get_setupfile_name(run_name)
411
412 if len(commands) > 0:
413 if print_setupfile: athMsgLog.info("")
414 athMsgLog.info("Writing setupfile '%s'", setupfile_name)
415 try:
416 with open(setupfile_name, 'w') as setupfile:
417 for command in commands: setupfile.write(command+'\n')
418 except Exception:
419 raise RuntimeError('Could not write Herwig/Matchbox setupfile')
420
421 if print_setupfile:
422 athMsgLog.info("")
423 for command in commands: athMsgLog.info(' %s', command)
424 athMsgLog.info("")
425
426 else:
427 athMsgLog.info("No setupfile commands given.")
428
429
430# \brief Copy default repository `HerwigDefaults.rpo` to current working directory
431#
433
434 shutil.copy(os.path.join(get_share_path(), 'HerwigDefaults.rpo'), 'HerwigDefaults.rpo')
435
436
437def log(level='info', message=''):
438
439 if level in ['info', 'warn', 'error']:
440 logger = getattr(athMsgLog, level)
441 for line in message.splitlines(): logger(line)
442 else:
443 raise ValueError("Unknown logging level'{}' specified. Possible values are 'info', 'warn' or 'error'".format(level))
444
445
446def exit_banner(gridpack, cross_section, cross_section_error):
447
448 size = hw7Utils.humanize_bytes(hw7Utils.get_size(gridpack))
449 space_size = hw7Utils.get_repeated_pattern(' ', 31-len(size))
450
451 xsec = '{:f}'.format(cross_section)
452 err = '{:f}'.format(cross_section_error)
453 rel_err = '{:.2f}'.format(cross_section_error / cross_section * 100.0)
454 space_xsec = hw7Utils.get_repeated_pattern(' ', 37-len(xsec)-len(err)-len(rel_err))
455
456 banner = ''
457 space = ' '.join(['' for i in range(70+4+1-len(gridpack))])
458 banner += "##########################################################################################\n"
459 banner += "## ------------------------------------------------------------------------------- ##\n"
460 banner += "## {} (size: {}){} ##\n".format(hw7Utils.ansi_format_ok("HERWIG 7 successfully created the gridpack"), size, space_size)
461 banner += "## ##\n"
462 banner += "## {}{} ##\n".format(hw7Utils.ansi_format_info(gridpack), space)
463 banner += "## ##\n"
464 banner += "## cross section from integration: {} +/- {} ({}%) nb {} ##\n".format(xsec, err, rel_err, space_xsec)
465
466 if cross_section_error / cross_section > hw7Utils.integration_grids_precision_threshold:
467 threshold = '{}%'.format(hw7Utils.integration_grids_precision_threshold*100.0)
468 space_threshold = hw7Utils.get_repeated_pattern(' ', 6-len(threshold))
469 banner += "## ##\n"
470 banner += "## {} ##\n".format(hw7Utils.ansi_format_warning("! WARNING: The integration grids only have a low precision (worse than {}){}!".format(threshold, space_threshold)))
471
472 banner += "## ##\n"
473 banner += "## ------------------------------------------------------------------------------- ##\n"
474 banner += "## ##\n"
475 banner += "## Please ignore the error ##\n"
476 banner += "## ##\n"
477 banner += "## No such file or directory: 'evgen.root' raised while stating file evgen.root ##\n"
478 banner += "## ##\n"
479 banner += "##########################################################################################\n"
480 return(banner)
481
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
static Root::TMsgLogger logger("iLumiCalc")
do_read(gen_config)
matchbox_run(gen_config, integration_jobs, cleanup_herwig_scratch)
do_step(step, command, logfile_name=None)
get_setupfile_name(run_name="Herwig-Matchbox")
do_run_existing_runfile(gen_config)
exit_banner(gridpack, cross_section, cross_section_error)
do_integrate(run_name, integration_job)
do_uncompress_gridpack(gridpack_name)
get_infile_name(run_name="Herwig-Matchbox")
write_infile(gen_config, print_infile=True)
get_runfile_name(run_name="Herwig-Matchbox")
matchbox_run_gridpack(gen_config, integration_jobs, gridpack_name, cleanup_herwig_scratch, integrate)
do_read_existing_infile(gen_config)
do_compress_gridpack(run_name, gridpack_name)
write_setupfile(run_name, commands, print_setupfile=True)
do_mergegrids(run_name, integration_jobs)
do_build_integrate_mergegrids(gen_config, integration_jobs)
do_build(gen_config, integration_jobs)
log(level='info', message='')
do_run(gen_config, cleanup_herwig_scratch=True)
Definition run.py:1