7 import math, os, subprocess
9 from AthenaCommon
import Logging
10 athMsgLog = Logging.logging.getLogger(
'Herwig7Utils')
12 integration_grids_precision_threshold = 0.0005
32 def add(self, commands):
36 raise RuntimeError(
"The commands associated to this configuration object can't be modified anymore because the Herwig7 infile has already been written to disk. Therefore, any subsequent additional modifications can't be adopted in the Herwig7 run.")
56 for ID, process
in enumerate(self.
processes):
57 returncode = process.poll()
58 if returncode
is not None:
65 athMsgLog.error(
ansi_format_error(
"Process #{} finished with error code {} (please check logfile '{}'), {}/{} still running".
format(process.ID, returncode, process.logfile_title, len(self.
processes), self.
n_initial)))
68 athMsgLog.error(
ansi_format_error(
"Process #{} was terminated by signal {} (please check logfile '{}'), {}/{} still running".
format(process.ID, -returncode, process.logfile_title, len(self.
processes), self.
n_initial)))
75 athMsgLog.info(
"Content of integration log file '%s':", process.logfile_title)
77 with open(process.logfile_title,
'r')
as logfile:
79 athMsgLog.info(
' %s', line.rstrip(
'\n'))
94 def __init__(self, ID, command, logfile_title):
96 athMsgLog.info(
ansi_format_info(
"Starting subprocess #{} with command '{}' and logfile '{}'".
format(ID,
' '.
join(command), logfile_title)))
109 athMsgLog.info(
"Calculating cross section after integration")
110 logfiles = [run_name+
'.integrate'+
str(integration_job)+
'.log' for integration_job
in range(integration_jobs)]
115 for logfile
in logfiles:
117 athMsgLog.info(
"- %s", logfile)
120 with open(logfile,
'r')
as log: data = log.read().strip()
122 with open(logfile,
'r')
as log:
126 if 'Integrate ' in line:
127 n_subprocs =
int(line.split(
'of')[1].
replace(
':',
''))
128 athMsgLog.info(
" found %s subprocesses", n_subprocs)
131 data = data.split(
"Integrate ")[1:]
133 for s, subproc
in enumerate(data, start=1):
136 for line
in subproc.split(
"\n"):
137 if 'integrated ( ' in line:
138 _xsec =
float(line.split()[2])
139 _err =
float(line.split()[4])
140 athMsgLog.info(
" - subprocess %s: xsec = %s +/- %s nb", s, _xsec, _err)
146 if err / xsec > integration_grids_precision_threshold:
147 threshold =
'{}%'.
format(integration_grids_precision_threshold*100.0)
148 if run_name ==
"Herwig_DEBUG":
149 athMsgLog.warn(
ansi_format_warning(
'! The integration grids only have a low precision (worse than {}): xsec = {} +/- {} nb (accuracy: {:.3f}%)'.
format(threshold, xsec, err, err/xsec*100.0)))
151 athMsgLog.error(
ansi_format_error(
'! The integration grids only have a low precision (worse than {}): xsec = {} +/- {} nb (accuracy: {:.3f}%)'.
format(threshold, xsec, err, err/xsec*100.0)))
152 athMsgLog.warn(
ansi_format_warning(
'! In order to speed up the event generation you should consider improving the statistics of the integration / phase space sampling stage (see the sampler_commands() function).'))
154 athMsgLog.info(
ansi_format_info(
'After integration the estimated cross section was found to be: xsec = {} +/- {} nb (accuracy: {:.3f}%)'.
format(xsec, err, err/xsec*100.0)))
162 return(pattern.join([
'' for i
in range(repetitions+1)]))
170 if os.path.isfile(path):
171 return(os.path.getsize(path))
173 elif os.path.isdir(path):
174 for dirpath, dirs, files
in os.walk(path):
176 file_path = os.path.join(dirpath, file)
178 if os.path.isfile(file_path):
179 size += os.path.getsize(file_path)
199 for factor, suffix
in abbrevs:
202 return '%.*f %s' % (precision, bytes / factor, suffix)
207 bold=False, underline=False, italics=False, marked=False, strikethrough=False):
209 format_none =
'\033[0m'
210 format_bold =
'\033[1m'
211 format_underline =
'\033[4m'
212 format_italics =
'\033[3m'
213 format_marked =
'\033[7m'
214 format_strikethrough =
'\033[9m'
220 'Red':
'\033[91m',
'DarkRed':
'\033[31m',
221 'Green':
'\033[92m',
'DarkGreen':
'\033[32m',
222 'Yellow':
'\033[93m',
'Orange':
'\033[33m',
223 'Blue':
'\033[94m',
'DarkBlue':
'\033[34m',
224 'Pink':
'\033[95m',
'DarkPink':
'\033[35m',
225 'Cyan':
'\033[96m',
'DarkCyan':
'\033[36m',
226 'White':
'\033[97m',
'LightGrey':
'\033[37m',
229 background_colours = {
234 'Orange':
'\033[43m',
241 if colour
in colours:
242 if background_colour
in background_colours:
245 + background_colours[background_colour]
246 + (format_bold
if bold
else '')
247 + (format_underline
if underline
else '')
248 + (format_italics
if italics
else '')
249 + (format_marked
if marked
else '')
250 + (format_strikethrough
if strikethrough
else '')
251 + text + format_none)
253 raise Exception(
"Could not find background colour '{}'.".
format(background_colour))
255 raise Exception(
"Could not find colour '{}'.".
format(colour))