def create_master_script(log_directory, master_script_filename, mg_directory, mg_process_directory, results): placeholder_definition = r"mgdir=${1:-" + mg_directory + r"}" + "\n" placeholder_definition += r"mgprocdir=${2:-" + mg_process_directory + r"}" + "\n" placeholder_definition += r"mmlogdir=${3:-" + log_directory + r"}" commands = "\n".join(results) script = ( "#!/bin/bash\n\n# Master script to generate events for MadMiner\n\n" + "# Usage: run.sh [MG_directory] [MG_process_directory] [log_directory]\n\n" + "{}\n\n{}").format(placeholder_definition, commands) with open(master_script_filename, "w") as file: file.write(script) make_file_executable(master_script_filename)
def setup_mg_reweighting_with_scripts( mg_process_directory, run_name, reweight_card_file_from_mgprocdir=None, script_file_from_mgprocdir=None, initial_command=None, log_dir=None, log_file_from_logdir=None, ): """ Prepares a bash script that will start the event generation. Parameters ---------- mg_process_directory : str Path to the MG process directory. reweight_card_file_from_mgprocdir : str or None, optional Path to the MadGraph reweight card, relative from mg_process_directory. If None, the card present in the process folder is used. Default value: None. script_file_from_mgprocdir : str or None, optional This sets where the shell script to run the reweighting is generated, relative from mg_process_directory. If None, a default filename in `mg_process_directory/madminer` is used. Default value: None. initial_command : str or None, optional Initial shell commands that have to be executed before MG is run (e.g. to load a virtual environment). Default value: None. log_dir : str or None, optional Log directory. Default value: None. log_file_from_logdir : str or None, optional Path to a log file in which the MadGraph output is saved, relative from the default log directory. Default value: None. Returns ------- bash_script_call : str How to call this script. """ # Preparations if log_dir is not None: create_missing_folders([log_dir]) # Prepare run... logger.info("Preparing script to reweight an existing sample in %s", mg_process_directory) # Bash script can optionally provide MG path or process directory mg_process_directory_placeholder = "$mgprocdir" log_dir_placeholder = "$mmlogdir" placeholder_definition = r"mgprocdir=${1:-" + mg_process_directory + r"}" + "\n" placeholder_definition += r"mmlogdir=${2:-" + log_dir + r"}" if script_file_from_mgprocdir is None: script_file = mg_process_directory + "/madminer/scripts/madminer_reweight_{}.sh".format( run_name) else: script_file = mg_process_directory + "/" + script_file_from_mgprocdir script_filename = os.path.basename(script_file) if log_file_from_logdir is None: log_file_from_logdir = "/log.log" # Initial commands if initial_command is None: initial_command = "" # Card copying commands if reweight_card_file_from_mgprocdir is not None: copy_commands = "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, reweight_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/reweight_card.dat", ) else: copy_commands = "" # Put together script script = ( "#!/bin/bash\n\n# Script generated by MadMiner\n\n# Usage: {} MG_process_directory log_dir\n\n" + "{}\n\n{}\n\n{}\n\n{}/bin/madevent reweight {} -f > {}/{}\n").format( script_filename, initial_command, placeholder_definition, copy_commands, mg_process_directory_placeholder, run_name, log_dir_placeholder, log_file_from_logdir, ) with open(script_file, "w") as file: file.write(script) make_file_executable(script_file) # How to call it from master script call_instruction = "{}/{} [MG_process_directory] [log_directory]".format( mg_process_directory, script_file_from_mgprocdir) return call_instruction
def run_multiple( self, mg_directory, proc_card_file, param_card_template_file, run_card_files, mg_process_directory=None, pythia8_card_file=None, sample_benchmarks=None, is_background=False, only_prepare_script=False, ufo_model_directory=None, log_directory=None, temp_directory=None, initial_command=None, ): """ High-level function that creates the the MadGraph process, all required cards, and prepares or runs the event generation for multiple combinations of run_cards or importance samplings (`sample_benchmarks`). If `only_prepare_scripts=True`, the event generation is not run directly, but a bash script is created in `<process_folder>/madminer/run.sh` that will start the event generation with the correct settings. Parameters ---------- mg_directory : str Path to the MadGraph 5 base directory. proc_card_file : str Path to the process card that tells MadGraph how to generate the process. param_card_template_file : str Path to a param card that will be used as template to create the appropriate param cards for these runs. run_card_files : list of str Paths to the MadGraph run card. mg_process_directory : str or None, optional Path to the MG process directory. If None, MadMiner uses ./MG_process. Default value: None. pythia8_card_file : str, optional Path to the MadGraph Pythia8 card. If None, the card present in the process folder is used. Default value: None. sample_benchmarks : list of str or None, optional Lists the names of benchmarks that should be used to sample events. A different sampling does not change the expected differential cross sections, but will change which regions of phase space have many events (small variance) or few events (high variance). If None, a run is started for each of the benchmarks, which should map out all regions of phase space well. Default value: None. is_background : bool, optional Should be True for background processes, i.e. process in which the differential cross section does not depend on the parameters (i.e. is the same for all benchmarks). In this case, no reweighting is run, which can substantially speed up the event generation. Default value: False. only_prepare_script : bool, optional If True, the event generation is not started, but instead a run.sh script is created in the process directory. Default value: False. only_prepare_script : bool, optional If True, MadGraph is not executed, but instead a run.sh script is created in the process directory. Default value: False. ufo_model_directory : str or None, optional Path to an UFO model directory that should be used, but is not yet installed in mg_directory/models. The model will be copied to the MadGraph model directory before the process directory is generated. (Default value = None) log_directory : str or None, optional Directory for log files with the MadGraph output. If None, ./logs is used. Default value: None. temp_directory : str or None, optional Path to a temporary directory. If None, a system default is used. Default value: None. initial_command : str or None, optional Initial shell commands that have to be executed before MG is run (e.g. to load a virtual environment). Default value: None. Returns ------- None """ # Defaults if mg_process_directory is None: mg_process_directory = "./MG_process" if temp_directory is None: temp_directory = tempfile.gettempdir() if log_directory is None: log_directory = "./logs" if sample_benchmarks is None: sample_benchmarks = [benchmark for benchmark in self.benchmarks] # Generate process folder log_file_generate = log_directory + "/generate.log" self._generate_mg_process( mg_directory, temp_directory, proc_card_file, mg_process_directory, ufo_model_directory=ufo_model_directory, initial_command=initial_command, log_file=log_file_generate, ) # Make MadMiner folders create_missing_folders([ mg_process_directory + "/madminer", mg_process_directory + "/madminer/cards", mg_process_directory + "/madminer/scripts", ]) # Loop over settings i = 0 results = [] for run_card_file in run_card_files: for sample_benchmark in sample_benchmarks: # Files script_file = "madminer/scripts/run_{}.sh".format(i) log_file_run = "run_{}.log".format(i) mg_commands_filename = "/madminer/cards/mg_commands_{}.dat".format( i) param_card_file = "/madminer/cards/param_card_{}.dat".format(i) reweight_card_file = "/madminer/cards/reweight_card_{}.dat".format( i) new_pythia8_card_file = None if pythia8_card_file is not None: new_pythia8_card_file = "/madminer/cards/pythia8_card_{}.dat".format( i) new_run_card_file = None if run_card_file is not None: new_run_card_file = "/madminer/cards/run_card_{}.dat".format( i) logging.info("Run %s", i) logging.info(" Sampling from benchmark: %s", sample_benchmark) logging.info(" Original run card: %s", run_card_file) logging.info(" Original Pythia8 card: %s", pythia8_card_file) logging.info(" Copied run card: %s", new_run_card_file) logging.info(" Copied Pythia8 card: %s", new_pythia8_card_file) logging.info(" Param card: %s", param_card_file) logging.info(" Reweight card: %s", reweight_card_file) logging.info(" Log file: %s", log_file_run) # Creat param and reweight cards self._export_cards( param_card_template_file, mg_process_directory, sample_benchmark=sample_benchmark, param_card_filename=mg_process_directory + "/" + param_card_file, reweight_card_filename=mg_process_directory + "/" + reweight_card_file, ) # Copy run and Pythia cards if run_card_file is not None: copy_file(run_card_file, mg_process_directory + "/" + new_run_card_file) if pythia8_card_file is not None: copy_file( pythia8_card_file, mg_process_directory + "/" + new_pythia8_card_file) # Run MG and Pythia if only_prepare_script: result = self._prepare_mg_and_pythia( mg_process_directory, proc_card_filename_from_mgprocdir=mg_commands_filename, run_card_file_from_mgprocdir=new_run_card_file, param_card_file_from_mgprocdir=param_card_file, reweight_card_file_from_mgprocdir=reweight_card_file, pythia8_card_file_from_mgprocdir=new_pythia8_card_file, is_background=is_background, script_file_from_mgprocdir=script_file, initial_command=initial_command, log_dir=log_directory, log_file_from_logdir=log_file_run, ) results.append(result) else: self._run_mg_and_pythia( mg_directory, mg_process_directory, mg_process_directory + "/" + mg_commands_filename, mg_process_directory + "/" + new_run_card_file, mg_process_directory + "/" + param_card_file, mg_process_directory + "/" + reweight_card_file, # mg_process_directory + "/" + new_pythia8_card_file, is_background=is_background, initial_command=initial_command, log_file=log_directory + "/" + log_file_run, ) i += 1 # Master shell script if only_prepare_script: master_script_filename = "{}/madminer/run.sh".format( mg_process_directory) placeholder_definition = r"mgdir=${1:-" + mg_directory + r"}" + "\n" placeholder_definition += r"mgprocdir=${2:-" + mg_process_directory + r"}" + "\n" placeholder_definition += r"mmlogdir=${3:-" + log_directory + r"}" commands = "\n".join(results) script = ( "#!/bin/bash\n\n# Master script to generate events for MadMiner\n\n" + "# Usage: run.sh [MG_directory] [MG_process_directory] [log_directory]\n\n" + "{}\n\n{}").format(placeholder_definition, commands) with open(master_script_filename, "w") as file: file.write(script) make_file_executable(master_script_filename) logging.info( "To generate events, please run:\n\n %s [MG_directory] [MG_process_directory] [log_dir]\n\n", master_script_filename, )
def setup_mg_with_scripts( mg_process_directory, proc_card_filename_from_mgprocdir=None, run_card_file_from_mgprocdir=None, param_card_file_from_mgprocdir=None, reweight_card_file_from_mgprocdir=None, pythia8_card_file_from_mgprocdir=None, configuration_file_from_mgprocdir=None, is_background=False, script_file_from_mgprocdir=None, initial_command=None, log_dir=None, log_file_from_logdir=None, explicit_python_call=False, order="LO", python_executable=None, ): """ Prepares a bash script that will start the event generation. Parameters ---------- mg_process_directory : str Path to the MG process directory. proc_card_filename_from_mgprocdir : str or None, optional Filename for the MG command card that will be generated, relative from mg_process_directory. If None, a default filename in the MG process directory will be chosen. run_card_file_from_mgprocdir : str or None, optional Path to the MadGraph run card, relative from mg_process_directory. If None, the card present in the process folder is used. Default value: None. param_card_file_from_mgprocdir : str or None, optional Path to the MadGraph run card, relative from mg_process_directory. If None, the card present in the process folder is used. Default value: None. reweight_card_file_from_mgprocdir : str or None, optional Path to the MadGraph reweight card, relative from mg_process_directory. If None, the card present in the process folder is used. Default value: None. pythia8_card_file_from_mgprocdir : str or None, optional Path to the MadGraph Pythia8 card, relative from mg_process_directory. If None, Pythia is not run. Default value: None. configuration_file_from_mgprocdir : str or None, optional Path to the MadGraph me5_configuration card, relative from mg_process_directory. If None, the card present in the process folder is used. Default value: None. is_background : bool, optional Should be True for background processes, i.e. process in which the differential cross section does not depend on the parameters (and would be the same for all benchmarks). In this case, no reweighting is run, which can substantially speed up the event generation. Default value: False. script_file_from_mgprocdir : str or None, optional This sets where the shell script to run MG and Pythia is generated, relative from mg_process_directory. If None, a default filename in `mg_process_directory/madminer` is used. Default value: None. initial_command : str or None, optional Initial shell commands that have to be executed before MG is run (e.g. to load a virtual environment). Default value: None. log_dir : str or None, optional Log directory. Default value: None. log_file_from_logdir : str or None, optional Path to a log file in which the MadGraph output is saved, relative from the default log directory. Default value: None. explicit_python_call : bool, optional Calls `python2.7` instead of `python`. python_executable : None or str, optional Overwrites the default Python executable Returns ------- bash_script_call : str How to call this script. """ # Preparations create_missing_folders([mg_process_directory]) if log_dir is not None: create_missing_folders([log_dir]) if proc_card_filename_from_mgprocdir is not None: create_missing_folders([ os.path.dirname(mg_process_directory + "/" + proc_card_filename_from_mgprocdir) ]) # Prepare run... logger.info("Preparing script to run MadGraph and Pythia in %s", mg_process_directory) # Bash script can optionally provide MG path or process directory mg_directory_placeholder = "$mgdir" mg_process_directory_placeholder = "$mgprocdir" log_dir_placeholder = "$mmlogdir" placeholder_definition = "mgdir=$1\nmgprocdir=$2\nmmlogdir=$3" # Find filenames for process card and script if proc_card_filename_from_mgprocdir is None: for i in range(1000): proc_card_filename_from_mgprocdir = "/Cards/start_event_generation_{}.mg5".format( i) if not os.path.isfile(mg_process_directory + "/" + proc_card_filename_from_mgprocdir): break else: proc_card_filename = mg_process_directory + "/" + proc_card_filename_from_mgprocdir if script_file_from_mgprocdir is None: for i in range(1000): script_file = mg_process_directory + "/madminer/scripts/madminer_run_{}.sh".format( i) if not os.path.isfile(script_file): break else: script_file = mg_process_directory + "/" + script_file_from_mgprocdir script_filename = os.path.basename(script_file) if log_file_from_logdir is None: log_file_from_logdir = "/log.log" # MG commands shower_option = "OFF" if pythia8_card_file_from_mgprocdir is None else "Pythia8" reweight_option = "OFF" if is_background else "ON" mg_commands = """ launch {} shower={} detector=OFF analysis=OFF madspin=OFF reweight={} done """.format(mg_process_directory_placeholder, shower_option, reweight_option) with open(proc_card_filename, "w") as file: file.write(mg_commands) # Initial commands if initial_command is None: initial_command = "" # Card copying commands copy_commands = "" if run_card_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, run_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/run_card.dat", ) if param_card_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, param_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/param_card.dat", ) if reweight_card_file_from_mgprocdir is not None and not is_background: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, reweight_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/reweight_card.dat", ) if pythia8_card_file_from_mgprocdir is not None and order == "LO": copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, pythia8_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/pythia8_card.dat", ) elif pythia8_card_file_from_mgprocdir is not None and order == "NLO": copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, pythia8_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/shower_card.dat", ) if configuration_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, configuration_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/me5_configuration.txt", ) # Replace environment variable in proc card replacement_command = """sed -e 's@\$mgprocdir@'"$mgprocdir"'@' {}/{} > {}/{}""".format( mg_process_directory_placeholder, proc_card_filename_from_mgprocdir, mg_process_directory_placeholder, "Cards/mg_commands.mg5", ) # Explicitly call Python 2 if necessary if explicit_python_call: python_call = python_executable + " " if python_executable is not None else "python2.7 " else: python_call = "" # Put together script script = ( "#!/bin/bash\n\n# Script generated by MadMiner\n\n# Usage: {} MG_directory MG_process_directory log_dir\n\n" + "{}\n\n{}\n\n{}\n{}\n\n{} {}/bin/mg5_aMC {}/{} > {}/{}\n").format( script_filename, initial_command, placeholder_definition, copy_commands, replacement_command, python_call, mg_directory_placeholder, mg_process_directory_placeholder, "Cards/mg_commands.mg5", log_dir_placeholder, log_file_from_logdir, ) with open(script_file, "w") as file: file.write(script) make_file_executable(script_file) # How to call it from master script call_placeholder = "{}/{} {} {} {}".format( mg_process_directory_placeholder, script_file_from_mgprocdir, mg_directory_placeholder, mg_process_directory_placeholder, log_dir_placeholder, ) return call_placeholder
def prepare_run_mg_pythia( mg_process_directory, proc_card_filename_from_mgprocdir=None, run_card_file_from_mgprocdir=None, param_card_file_from_mgprocdir=None, reweight_card_file_from_mgprocdir=None, pythia8_card_file_from_mgprocdir=None, is_background=False, script_file_from_mgprocdir=None, initial_command=None, log_file_from_logdir=None, ): # Bash script can optionally provide MG path or process directory mg_directory_placeholder = "$mgdir" mg_process_directory_placeholder = "$mgprocdir" log_dir_placeholder = "$mmlogdir" placeholder_definition = "mgdir=$1\nmgprocdir=$2\nmmlogdir=$3" # Find filenames for process card and script if proc_card_filename_from_mgprocdir is None: for i in range(1000): proc_card_filename_from_mgprocdir = "/Cards/start_event_generation_{}.mg5".format( i) if not os.path.isfile(mg_process_directory + "/" + proc_card_filename_from_mgprocdir): break else: proc_card_filename = mg_process_directory + "/" + proc_card_filename_from_mgprocdir proc_card_filename_placeholder = mg_process_directory_placeholder + proc_card_filename_from_mgprocdir if script_file_from_mgprocdir is None: for i in range(1000): script_file = mg_process_directory + "/madminer/scripts/madminer_run_{}.sh".format( i) if not os.path.isfile(script_file): break else: script_file = mg_process_directory + "/" + script_file_from_mgprocdir script_filename = os.path.basename(script_file) if log_file_from_logdir is None: log_file_from_logdir = "/log.log" # MG commands shower_option = "OFF" if pythia8_card_file_from_mgprocdir is None else "Pythia8" reweight_option = "OFF" if is_background else "ON" mg_commands = """ launch {} shower={} detector=OFF analysis=OFF madspin=OFF reweight={} done """.format(mg_process_directory_placeholder, shower_option, reweight_option) with open(proc_card_filename, "w") as file: file.write(mg_commands) # Initial commands if initial_command is None: initial_command = "" # Card copying commands copy_commands = "" if run_card_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, run_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/run_card.dat", ) if param_card_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, param_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/param_card.dat", ) if reweight_card_file_from_mgprocdir is not None and not is_background: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, reweight_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/reweight_card.dat", ) if pythia8_card_file_from_mgprocdir is not None: copy_commands += "cp {}/{} {}{}\n".format( mg_process_directory_placeholder, pythia8_card_file_from_mgprocdir, mg_process_directory_placeholder, "/Cards/pythia8_card.dat", ) # Replace environment variable in proc card replacement_command = """sed -e 's@\$mgprocdir@'"$mgprocdir"'@' {}/{} > {}/{}""".format( mg_process_directory_placeholder, proc_card_filename_from_mgprocdir, mg_process_directory_placeholder, "Cards/mg_commands.mg5", ) # Put together script script = ( "#!/bin/bash\n\n# Script generated by MadMiner\n\n# Usage: {} MG_directory MG_process_directory log_dir\n\n" + "{}\n\n{}\n\n{}\n{}\n\n{}/bin/mg5_aMC {}/{} > {}/{}\n").format( script_filename, initial_command, placeholder_definition, copy_commands, replacement_command, mg_directory_placeholder, mg_process_directory_placeholder, "Cards/mg_commands.mg5", log_dir_placeholder, log_file_from_logdir, ) with open(script_file, "w") as file: file.write(script) make_file_executable(script_file) # How to call it from master script call_placeholder = "{}/{} {} {} {}".format( mg_process_directory_placeholder, script_file_from_mgprocdir, mg_directory_placeholder, mg_process_directory_placeholder, log_dir_placeholder, ) return call_placeholder