ATLAS Offline Software
Loading...
Searching...
No Matches
Herwig7Control Namespace Reference

Functions

 get_share_path ()
 run (gen_config)
 matchbox_run (gen_config, integration_jobs, cleanup_herwig_scratch)
 matchbox_run_gridpack (gen_config, integration_jobs, gridpack_name, cleanup_herwig_scratch, integrate)
 do_step (step, command, logfile_name=None)
 do_abort ()
 do_read (gen_config)
 do_read_existing_infile (gen_config)
 do_build (gen_config, integration_jobs)
 do_integrate (run_name, integration_job)
 do_mergegrids (run_name, integration_jobs)
 do_build_integrate_mergegrids (gen_config, integration_jobs)
 do_compress_gridpack (run_name, gridpack_name)
 do_uncompress_gridpack (gridpack_name)
 do_run (gen_config, cleanup_herwig_scratch=True)
 do_run_existing_runfile (gen_config)
 herwig_version ()
 thepeg_version ()
 start_banner ()
 get_software_versions ()
 get_infile_name (run_name="Herwig-Matchbox")
 get_setupfile_name (run_name="Herwig-Matchbox")
 get_runfile_name (run_name="Herwig-Matchbox")
 write_infile (gen_config, print_infile=True)
 write_setupfile (run_name, commands, print_setupfile=True)
 get_default_repository ()
 log (level='info', message='')
 exit_banner (gridpack, cross_section, cross_section_error)

Variables

 athMsgLog = Logging.logging.getLogger('Herwig7Control')
 herwig7_path = os.environ['HERWIG7_PATH']
 herwig7_bin_path = os.path.join(herwig7_path, 'bin')
 herwig7_share_path = get_share_path()
 herwig7_binary = os.path.join(herwig7_bin_path, 'Herwig')

Function Documentation

◆ do_abort()

Herwig7Control.do_abort ( )

Definition at line 141 of file Herwig7Control.py.

141def do_abort():
142 athMsgLog.info(hw7Utils.ansi_format_info("Doing abort"))
143 sys.exit(0)
144
145
146# Do the read step

◆ do_build()

Herwig7Control.do_build ( gen_config,
integration_jobs )

Definition at line 177 of file Herwig7Control.py.

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
186 get_default_repository()
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

◆ do_build_integrate_mergegrids()

Herwig7Control.do_build_integrate_mergegrids ( gen_config,
integration_jobs )

Definition at line 220 of file Herwig7Control.py.

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

◆ do_compress_gridpack()

Herwig7Control.do_compress_gridpack ( run_name,
gridpack_name )

Definition at line 247 of file Herwig7Control.py.

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

◆ do_integrate()

Herwig7Control.do_integrate ( run_name,
integration_job )

Definition at line 195 of file Herwig7Control.py.

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

◆ do_mergegrids()

Herwig7Control.do_mergegrids ( run_name,
integration_jobs )

Definition at line 206 of file Herwig7Control.py.

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

◆ do_read()

Herwig7Control.do_read ( gen_config)

Definition at line 147 of file Herwig7Control.py.

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
156 get_default_repository()
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

◆ do_read_existing_infile()

Herwig7Control.do_read_existing_infile ( gen_config)

Definition at line 163 of file Herwig7Control.py.

163def do_read_existing_infile(gen_config):
164
165 # print start banner including version numbers
166 log(message=start_banner())
167
168 # copy HerwigDefaults.rpo to the current working directory
169 get_default_repository()
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

◆ do_run()

Herwig7Control.do_run ( gen_config,
cleanup_herwig_scratch = True )

Definition at line 267 of file Herwig7Control.py.

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

◆ do_run_existing_runfile()

Herwig7Control.do_run_existing_runfile ( gen_config)

Definition at line 300 of file Herwig7Control.py.

300def do_run_existing_runfile(gen_config):
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

◆ do_step()

Herwig7Control.do_step ( step,
command,
logfile_name = None )

Definition at line 122 of file Herwig7Control.py.

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

◆ do_uncompress_gridpack()

Herwig7Control.do_uncompress_gridpack ( gridpack_name)

Definition at line 260 of file Herwig7Control.py.

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

◆ exit_banner()

Herwig7Control.exit_banner ( gridpack,
cross_section,
cross_section_error )

Definition at line 446 of file Herwig7Control.py.

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

◆ get_default_repository()

Herwig7Control.get_default_repository ( )

Definition at line 432 of file Herwig7Control.py.

432def get_default_repository():
433
434 shutil.copy(os.path.join(get_share_path(), 'HerwigDefaults.rpo'), 'HerwigDefaults.rpo')
435
436

◆ get_infile_name()

Herwig7Control.get_infile_name ( run_name = "Herwig-Matchbox")

Definition at line 356 of file Herwig7Control.py.

356def get_infile_name(run_name="Herwig-Matchbox"):
357
358 return('{}.in'.format(run_name))
359
360

◆ get_runfile_name()

Herwig7Control.get_runfile_name ( run_name = "Herwig-Matchbox")

Definition at line 366 of file Herwig7Control.py.

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

◆ get_setupfile_name()

Herwig7Control.get_setupfile_name ( run_name = "Herwig-Matchbox")

Definition at line 361 of file Herwig7Control.py.

361def get_setupfile_name(run_name="Herwig-Matchbox"):
362
363 return('{}.setupfile.in'.format(run_name))
364
365

◆ get_share_path()

Herwig7Control.get_share_path ( )

Definition at line 30 of file Herwig7Control.py.

30def get_share_path():
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

◆ get_software_versions()

Herwig7Control.get_software_versions ( )

Definition at line 351 of file Herwig7Control.py.

351def get_software_versions():
352
353 return(subprocess.check_output([herwig7_binary,'--version'], text=True).splitlines())
354
355

◆ herwig_version()

Herwig7Control.herwig_version ( )

Definition at line 320 of file Herwig7Control.py.

320def herwig_version():
321
322 versions = get_software_versions()
323 return(' '.join(versions[0].split()[1:]))
324
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177

◆ log()

Herwig7Control.log ( level = 'info',
message = '' )

Definition at line 437 of file Herwig7Control.py.

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
static Root::TMsgLogger logger("iLumiCalc")

◆ matchbox_run()

Herwig7Control.matchbox_run ( gen_config,
integration_jobs,
cleanup_herwig_scratch )

Definition at line 77 of file Herwig7Control.py.

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

◆ matchbox_run_gridpack()

Herwig7Control.matchbox_run_gridpack ( gen_config,
integration_jobs,
gridpack_name,
cleanup_herwig_scratch,
integrate )

Definition at line 90 of file Herwig7Control.py.

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

◆ run()

Herwig7Control.run ( gen_config)

Definition at line 64 of file Herwig7Control.py.

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
Definition run.py:1

◆ start_banner()

Herwig7Control.start_banner ( )

Definition at line 330 of file Herwig7Control.py.

330def start_banner():
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

◆ thepeg_version()

Herwig7Control.thepeg_version ( )

Definition at line 325 of file Herwig7Control.py.

325def thepeg_version():
326
327 versions = get_software_versions()
328 return(' '.join(versions[1].split()[1:]))
329

◆ write_infile()

Herwig7Control.write_infile ( gen_config,
print_infile = True )

Definition at line 371 of file Herwig7Control.py.

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

◆ write_setupfile()

Herwig7Control.write_setupfile ( run_name,
commands,
print_setupfile = True )

Definition at line 408 of file Herwig7Control.py.

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#

Variable Documentation

◆ athMsgLog

Herwig7Control.athMsgLog = Logging.logging.getLogger('Herwig7Control')

Definition at line 22 of file Herwig7Control.py.

◆ herwig7_bin_path

Herwig7Control.herwig7_bin_path = os.path.join(herwig7_path, 'bin')

Definition at line 55 of file Herwig7Control.py.

◆ herwig7_binary

Herwig7Control.herwig7_binary = os.path.join(herwig7_bin_path, 'Herwig')

Definition at line 58 of file Herwig7Control.py.

◆ herwig7_path

Herwig7Control.herwig7_path = os.environ['HERWIG7_PATH']

Definition at line 54 of file Herwig7Control.py.

◆ herwig7_share_path

Herwig7Control.herwig7_share_path = get_share_path()

Definition at line 56 of file Herwig7Control.py.