1 """! @package generators
2 Event generation tools."""
14 """! Event generator base class."""
16 def __init__(self, name, command=None, **kwargs):
17 Component.__init__(self, name, command=command, description=
'', **kwargs)
23 if os.getenv(
"HPSMC_DIR")
is None:
24 raise Exception(
"HPSMC_DIR is not set!")
25 return "{}/share/generators".format(os.getenv(
"HPSMC_DIR",
None))
30 Run the EGS5 event generator to produce a StdHep file.
32 Required parameters are **seed**, **target_thickness**, **beam_energy**, **num_electrons** \n
33 Optional parameters are: **bunches**
47 EventGenerator.__init__(self, name,
"egs5_" + name, **kwargs)
50 """! Get installation directory."""
51 return EventGenerator.get_install_dir(self) +
"/egs5"
54 """! Setup of egs5 event generator."""
55 EventGenerator.setup(self)
59 self.
loggerlogger.debug(
"Using EGS5 from install dir: " + self.
egs5_diregs5_dir)
69 if os.path.exists(
"data"):
73 if os.path.exists(
"pgs5job.pegs5inp"):
74 os.unlink(
"pgs5job.pegs5inp")
75 os.symlink(self.
egs5_config_diregs5_config_dir +
"/src/esa.inp",
"pgs5job.pegs5inp")
80 self.
loggerlogger.debug(
"Target thickness set from job param: {}".format(self.
target_dztarget_dz))
82 raise Exception(
"Target thickness not set!")
88 seed_data =
" ".join(str(item)
for item
in [self.
seedseed, self.
target_dztarget_dz, ebeam, electrons])
89 self.
loggerlogger.debug(
"Seed data (seed, target_dz, ebeam, electrons): {}".format(seed_data))
90 seed_file = open(
"seed.dat",
'w')
91 seed_file.write(seed_data)
95 """! Generate output file name.
96 @return moller.stdhep if name of generator contains moller; beam.stdhep else"""
98 if 'moller' in self.
namename:
99 return [
'moller.stdhep']
101 return [
'beam.stdhep']
104 """! Execute event generator.
105 @param log_out name of log file for output
106 @param log_err name of log file for error
108 EventGenerator.execute(self, log_out, log_err)
109 if 'moller' not in self.
namename:
110 src = os.path.join(self.rundir,
'brems.stdhep')
112 self.
loggerlogger.debug(
"Copying '%s' to '%s'" % (src, dest))
113 shutil.copy(src, dest)
117 Return required parameters.
119 Required parameters are **seed**, **target_thickness**, **beam_energy**, **num_electrons**
121 return [
'seed',
'target_thickness',
'beam_energy',
'num_electrons']
125 Return optional parameters.
127 Optional parameters are: **bunches**
136 """! Convert LHE files to StdHep using EGS5."""
139 EGS5.__init__(self, name, **kwargs)
141 if self.
namename
not in [
"lhe_v1",
"lhe_rad",
"lhe_prompt",
"lhe_uniform",
"lhe_exponential"]:
142 raise Exception(
"The name '%s' is not valid for StdHepConverter tools." % self.
namename)
145 EGS5.config(self, parser)
148 """! Setup egs5 generator.
149 Throws exception if input LHE file is missing."""
151 if not len(self.
inputsinputs):
152 raise Exception(
"Missing required input LHE file.")
155 """! Execute converter.
156 Calls egs5 generator.
158 @param log_out name of log file for output
159 @param log_err name of log file for error
160 @return egs5 error code
162 input_file = self.
inputsinputs[0]
163 base, ext = os.path.splitext(input_file)
165 os.symlink(input_file,
"egs5job.inp")
167 with open(
"egs5job.inp",
'wb')
as outfile:
168 with gzip.open(self.
inputsinputs[0],
'rb')
as infile:
169 outfile.write(infile.read())
171 raise Exception(
'Input file has an unknown extension: %s' % input_file)
172 return EGS5.execute(self, log_out, log_err)
175 """! Converts *.lhe.gz and *.lhe to *.stdhep files."""
176 return [self.
input_filesinput_files()[0].replace(
".lhe.gz",
".stdhep").replace(
".lhe",
".stdhep")]
181 Abstract class for MadGraph generators.
183 Required parameters are: **nevents**, **run_params** \n
184 Optional parameters are: **seed**, **param_card**, **apmass**, **map**, **mpid**, **mrhod**
214 if 'event_types' in kwargs:
218 self.
event_typesevent_types = [
'unweighted',
'weighted']
220 EventGenerator.__init__(self, name, **kwargs)
223 """! Generate output file name."""
226 o.append(self.
namenamename +
"_unweighted_events.lhe.gz")
228 o.append(self.
namenamename +
"_events.lhe.gz")
232 """! Set parameters."""
233 Component.set_parameters(self, params)
234 self.
run_cardrun_card =
"run_card_" + self.run_params +
".dat"
235 self.
loggerlogger.debug(
"Set run card to '%s'" % self.
run_cardrun_card)
239 Return required parameters.
241 Required parameters are: **nevents**, **run_params**
243 return [
'nevents',
'run_params']
247 Return optional parameters.
249 Optional parameters are: **seed**, **param_card**, **apmass**, **map**, **mpid**, **mrhod**, **Map**, **mchi**, **dmchi**
251 return [
'seed',
'param_card',
'apmass',
'map',
'mpid',
'mrhod',
'Map',
'mchi',
'dmchi']
257 """! Make run card."""
259 self.
loggerlogger.info(
"Making run card '%s' with nevents=%d, seed=%d" % (run_card, self.
neventsnevents, self.
seedseed))
261 with open(run_card,
'r')
as cardin:
262 data = cardin.readlines()
264 for i
in range(0, len(data)):
265 if "= nevents" in data[i]:
266 self.
loggerlogger.debug(
"Setting nevents=%d in run card" % self.
neventsnevents)
267 data[i] =
" " + str(self.
neventsnevents) +
" = nevents ! Number of unweighted events requested" +
'\n'
268 if "= iseed" in data[i]:
269 self.
loggerlogger.debug(
"Setting seed=%d in run card" % self.
seedseed)
270 data[i] =
" " + str(self.
seedseed) +
" = iseed ! rnd seed (0=assigned automatically=default))" +
'\n'
272 with open(run_card,
'w')
as cardout:
273 cardout.writelines(data)
276 """! Make parameter card."""
278 self.
loggerlogger.debug(
"Making param card '%s'" % param_card)
280 with open(param_card,
'r')
as paramin:
281 data = paramin.readlines()
283 for i
in range(0, len(data)):
284 if "APMASS" in data[i]
and self.
apmassapmass
is not None:
285 data[i] =
" 622 %.7fe-03 # APMASS" % (self.
apmassapmass) +
'\n'
286 self.
loggerlogger.debug(
"APMASS in param card set to %d" % self.
apmassapmass)
287 if "map" in data[i]
and self.
mapmap
is not None:
288 data[i] =
" 622 %.7fe-03 # map" % (self.
mapmap) +
'\n'
289 if "mpid" in data[i]
and self.
mpidmpid
is not None:
290 data[i] =
" 624 %.7fe-03 # mpid" % (self.
mpidmpid) +
'\n'
291 if "mrhod" in data[i]
and self.
mrhodmrhod
is not None:
292 data[i] =
" 625 %.7fe-03 # mrhod" % (self.
mrhodmrhod) +
'\n'
293 if "Map" in data[i]
and "ap :" not in data[i]
and self.
MapMap
is not None:
294 data[i] =
" 1 %.7e # Map\n" % (self.
MapMap/1000)
295 if "Mchi" in data[i]
and "dMchi" not in data[i]
and self.
mchimchi
is not None:
296 data[i] =
" 1 %.7e # Mchi\n" % (self.
mchimchi/1000)
297 if "dMchi" in data[i]
and "Mchi/2" not in data[i]
and self.
dmchidmchi
is not None:
298 data[i] =
" 2 %.7e # dMchi" % (self.
dmchidmchi/1000)
300 with open(param_card,
'w')
as paramout:
301 paramout.writelines(data)
304 """! Return command arguments."""
308 """! Execute MadGraph generator.
309 @param log_out name of log file for output
310 @param log_err name of log file for error
313 os.chdir(os.path.dirname(self.
commandcommand))
314 self.
loggerlogger.debug(
"Executing '%s' from '%s'" % (self.
namenamename, os.getcwd()))
315 return Component.execute(self, log_out, log_err)
318 """! Setup event generator."""
319 EventGenerator.setup(self)
323 self.
loggerlogger.debug(
"Using Madgraph from install dir: " + self.
madgraph_dirmadgraph_dir)
326 raise Exception(
"Missing apmass param for AP generation.")
330 """! Run the MadGraph 4 event generator."""
333 dir_map = {
"BH":
"BH/MG_mini_BH/apBH",
334 "RAD":
"RAD/MG_mini_Rad/apRad",
335 "TM":
"TM/MG_mini/ap",
336 "ap":
"ap/MG_mini/ap",
337 "trigg":
"trigg/MG_mini_Trigg/apTri",
338 "tritrig":
"tritrig/MG_mini_Tri_W/apTri",
339 "wab":
"wab/MG_mini_WAB/AP_6W_XSec2_HallB"}
343 MG.__init__(self, name, **kwargs)
345 if self.
namenamename
not in MG4.dir_map:
346 raise Exception(
"The name '%s' is not valid for MG4." % self.
namenamename)
349 """! Get installation directory of MadGraph4."""
350 return EventGenerator.get_install_dir(self) +
"/madgraph4/src"
353 """! Setup MadGraph4 generator."""
356 proc_dirs = MG4.dir_map[self.
namenamename].split(os.sep)
357 src = os.path.join(self.
madgraph_dirmadgraph_dir, proc_dirs[0], proc_dirs[1])
359 self.
loggerlogger.debug(
"Copying '%s' to '%s'" % (src, dest))
360 shutil.copytree(src, dest, symlinks=
True)
362 self.
event_direvent_dir = os.path.join(self.rundir, proc_dirs[1], proc_dirs[2],
"Events")
363 if not os.path.isdir(self.
event_direvent_dir):
366 self.
commandcommandcommand = os.path.join(os.getcwd(), proc_dirs[1], proc_dirs[2],
"bin",
"generate_events")
370 run_card_dest = os.path.join(self.rundir, proc_dirs[1], proc_dirs[2],
"Cards",
"run_card.dat")
371 self.
loggerlogger.debug(
"Copying run card from '%s' to '%s'" % (run_card_src, run_card_dest))
372 shutil.copyfile(run_card_src, run_card_dest)
377 param_card_dest = os.path.join(self.rundir, proc_dirs[1], proc_dirs[2],
"Cards",
"param_card.dat")
378 self.
loggerlogger.debug(
"Copying param card from '%s' to '%s'" % (param_card_src, param_card_dest))
379 shutil.copyfile(param_card_src, param_card_dest)
384 """! Execute MadGraph4 generator.
385 @param log_out name of log file for output
386 @param log_err name of log file for error
389 returncode = MG.execute(self, log_out, log_err)
390 lhe_files = glob.glob(os.path.join(self.
event_direvent_dir,
"*.lhe.gz"))
392 dest = os.path.join(self.rundir, os.path.basename(f))
393 self.
loggerlogger.debug(
"Copying '%s' to '%s'" % (f, dest))
395 os.chdir(self.rundir)
400 """! Run the MadGraph 5 event generator."""
403 dir_map = {
"BH":
"BH",
405 "tritrig":
"tritrig",
407 "simp-3body":
"simp-3body",
413 MG.__init__(self, name, **kwargs)
415 if self.
namenamename
not in MG5.dir_map:
416 raise Exception(
"The name '%s' is not valid for MG5." % self.
namenamename)
419 """! Get installation directory of MadGraph5."""
420 return EventGenerator.get_install_dir(self) +
"/madgraph5/src"
423 """! Setup MadGraph5 generator."""
430 dest = os.path.join(self.rundir, self.
proc_dirproc_dir)
431 self.
loggerlogger.debug(
"Copying '%s' to '%s'" % (src, dest))
432 shutil.copytree(src, dest, symlinks=
True)
436 input_dir = os.path.join(self.madgraph_dir, "input")
437 dest_input_dir = os.path.join(self.rundir, self.proc_dir, "input")
438 self.logger.info("Copying '%s' to '%s'" % (input_dir, dest_input_dir))
439 shutil.copytree(input_dir, dest_input_dir)
445 run_card_src = os.path.join(src,
"Cards", self.
run_cardrun_card)
446 run_card_dest = os.path.join(dest,
"Cards",
"run_card.dat")
447 self.
loggerlogger.debug(
"Copying run card from '%s' to '%s'" % (run_card_src, run_card_dest))
448 shutil.copyfile(run_card_src, run_card_dest)
452 param_card_src = os.path.join(src,
"Cards", self.
param_cardparam_card)
453 param_card_dest = os.path.join(dest,
"Cards",
"param_card.dat")
454 self.
loggerlogger.debug(
"Copying param card from '%s' to '%s'" % (param_card_src, param_card_dest))
455 shutil.copyfile(param_card_src, param_card_dest)
460 """! Execute MadGraph5 generator.
461 @param log_out name of log file for output
462 @param log_err name of log file for error
465 returncode = MG.execute(self, log_out, log_err)
466 lhe_files = glob.glob(os.path.join(self.
event_direvent_dir,
"*.lhe.gz"))
468 dest = os.path.join(self.rundir,
'%s_%s' % (self.
namenamename, os.path.basename(f)))
469 self.
loggerlogger.debug(
"Copying '%s' to '%s'" % (f, dest))
471 os.chdir(self.rundir)
Base class for components in a job.
def output_files(self)
Return a list of output files created by this component.
def input_files(self)
Get a list of input files for this component.
Run the EGS5 event generator to produce a StdHep file.
egs5_dir
egs5 installation directory
egs5_data_dir
data directory
beam_energy
beam energy in MeV
target_thickness
target thickness in $\mu$m,
def execute(self, log_out, log_err)
Execute event generator.
def required_parameters(self)
Return required parameters.
egs5_config_dir
config directory
def output_files(self)
Generate output file name.
def optional_parameters(self)
Return optional parameters.
num_electrons
number of electrons
def __init__(self, name='', **kwargs)
def get_install_dir(self)
Get installation directory.
def setup(self)
Setup of egs5 event generator.
Event generator base class.
def __init__(self, name, command=None, **kwargs)
def required_parameters(self)
Return a list of required parameters.
def get_install_dir(self)
Run the MadGraph 4 event generator.
def __init__(self, name='ap', **kwargs)
def execute(self, log_out, log_err)
Execute MadGraph4 generator.
def get_install_dir(self)
Get installation directory of MadGraph4.
def setup(self)
Setup MadGraph4 generator.
Run the MadGraph 5 event generator.
def execute(self, log_out, log_err)
Execute MadGraph5 generator.
def __init__(self, name='tritrig', **kwargs)
def get_install_dir(self)
Get installation directory of MadGraph5.
def setup(self)
Setup MadGraph5 generator.
Abstract class for MadGraph generators.
event_types
event types: weighted or unweighted
mchi
average dark fermion mass when running idm
def __init__(self, name, **kwargs)
def execute(self, log_out, log_err)
Execute MadGraph generator.
def required_parameters(self)
Return required parameters.
def output_files(self)
Generate output file name.
param_card
Default name of param card.
def optional_parameters(self)
Return optional parameters.
def make_param_card(self, param_card)
Make parameter card.
def make_run_card(self, run_card)
Make run card.
madgraph_dir
Install dir or user config will be used for this.
dmchi
difference between dark fermion masses when running idm
def setup(self)
Setup event generator.
def set_parameters(self, params)
Set parameters.
def cmd_args(self)
Return command arguments.
Convert LHE files to StdHep using EGS5.
def config(self, parser)
Automatic configuration.
def execute(self, log_out, log_err)
Execute converter.
def output_files(self)
Converts *.lhe.gz and *.lhe to *.stdhep files.
def setup(self)
Setup egs5 generator.
def __init__(self, name="lhe_v1", **kwargs)