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, python2_override=False, ): """ 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. python2_override : bool, optional If True, MadMiner explicitly calls "python2" instead of relying on the system Python version to be Python 2.6 or Python 2.7. If you use systematics, make sure that the python interface of LHAPDF was compiled with the Python version you are using. Default: False. 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" 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, explicit_python_call=python2_override, ) # 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 mg_scripts = [] 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) logger.info("Run %s", i) logger.info(" Sampling from benchmark: %s", sample_benchmark) logger.info(" Original run card: %s", run_card_file) logger.info(" Original Pythia8 card: %s", pythia8_card_file) logger.info(" Copied run card: %s", new_run_card_file) logger.info(" Copied Pythia8 card: %s", new_pythia8_card_file) logger.info(" Param card: %s", param_card_file) logger.info(" Reweight card: %s", reweight_card_file) logger.info(" Log file: %s", log_file_run) # Check input if run_card_file is None and self.systematics is not None: logger.warning( "Warning: No run card given, but systematics set up. The correct systematics" " settings are not set automatically. Make sure to set them correctly!" ) # Create 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, ) # Create run card if run_card_file is not None: export_run_card( template_filename=run_card_file, run_card_filename=mg_process_directory + "/" + new_run_card_file, systematics=self.systematics, ) # Copy Pythia card 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: mg_script = setup_mg_with_scripts( 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=None if new_pythia8_card_file is None else 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, explicit_python_call=python2_override, ) mg_scripts.append(mg_script) else: run_mg( 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, None if new_pythia8_card_file is None else mg_process_directory + "/" + new_pythia8_card_file, is_background=is_background, initial_command=initial_command, log_file=log_directory + "/" + log_file_run, explicit_python_call=python2_override, ) i += 1 n_runs_total = i # Master shell script if only_prepare_script: master_script_filename = "{}/madminer/run.sh".format( mg_process_directory) create_master_script(log_directory, master_script_filename, mg_directory, mg_process_directory, mg_scripts) logger.info( "To generate events, please run:\n\n %s [MG_directory] [MG_process_directory] [log_dir]\n\n", master_script_filename, ) else: expected_event_files = [ mg_process_directory + "/Events/run_{:02d}".format(i + 1) for i in range(n_runs_total) ] expected_event_files = "\n".join(expected_event_files) logger.info( "Finished running MadGraph! Please check that events were succesfully generated in the following " "folders:\n\n%s\n\n", expected_event_files, )
def run_multiple( self, mg_directory, proc_card_file, param_card_template_file, run_card_files, mg_process_directory=None, pythia8_card_file=None, configuration_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, systematics=None, order="LO", python_executable=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. configuration_file : str, optional Path to the MadGraph me5_configuration 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. 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). If not specified and `python2_override` is True, it adds the user-installed Python2 binaries to the PATH. Default value: None. systematics : None or list of str, optional If list of str, defines which systematics are used for these runs. order : 'LO' or 'NLO', optional Differentiates between LO and NLO order runs. Minor changes to writing, reading and naming cards. Default value: 'LO' python_executable : None or str, optional Provides a path to the Python executable that should be used to call MadMiner. Default: 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.keys() ] # This snippet is useful when using virtual envs. # (Derives from a Python2 - Python3 issue). # Ref: https://github.com/madminer-tool/madminer/issues/422 if python_executable and initial_command is None: logger.info(f"Adding {python_executable} bin folder to PATH") binary_path = os.popen( f"command -v {python_executable}").read().strip() binary_folder = Path(binary_path).parent initial_command = f"export PATH={binary_folder}:$PATH" logger.info(f"Using Python executable {binary_path}") # Generate process folder log_file_generate = f"{log_directory}/generate.log" 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, python_executable=python_executable, ) # Make MadMiner folders Path(mg_process_directory, "madminer", "cards").mkdir(parents=True, exist_ok=True) Path(mg_process_directory, "madminer", "scripts").mkdir(parents=True, exist_ok=True) # Systematics if systematics is None: systematics_used = self.systematics else: systematics_used = OrderedDict() for key in systematics: systematics_used[key] = self.systematics[key] # Loop over settings i = 0 mg_scripts = [] for run_card_file in run_card_files: for sample_benchmark in sample_benchmarks: # Files script_file = f"madminer/scripts/run_{i}.sh" log_file_run = f"run_{i}.log" mg_commands_filename = f"madminer/cards/mg_commands_{i}.dat" param_card_file = f"madminer/cards/param_card_{i}.dat" reweight_card_file = f"madminer/cards/reweight_card_{i}.dat" new_pythia8_card_file = None if pythia8_card_file is not None: new_pythia8_card_file = f"madminer/cards/pythia8_card_{i}.dat" new_run_card_file = None if run_card_file is not None: new_run_card_file = f"madminer/cards/run_card_{i}.dat" new_configuration_file = None if configuration_file is not None: new_configuration_file = f"madminer/cards/me5_configuration_{i}.txt" logger.info("Run %s", i) logger.info(" Sampling from benchmark: %s", sample_benchmark) logger.info(" Original run card: %s", run_card_file) logger.info(" Original Pythia8 card: %s", pythia8_card_file) logger.info(" Original config card: %s", configuration_file) logger.info(" Copied run card: %s", new_run_card_file) logger.info(" Copied Pythia8 card: %s", new_pythia8_card_file) logger.info(" Copied config card: %s", new_configuration_file) logger.info(" Param card: %s", param_card_file) logger.info(" Reweight card: %s", reweight_card_file) logger.info(" Log file: %s", log_file_run) # Check input if run_card_file is None and any( syst.type in {SystematicType.PDF, SystematicType.SCALE} for syst in systematics_used.values()): logger.warning( "Warning: No run card given, but PDF or scale variation set up. The correct systematics" " settings are not set automatically. Make sure to set them correctly!" ) # Create param and reweight cards self._export_cards( param_card_template_file, mg_process_directory, sample_benchmark=sample_benchmark, param_card_filename= f"{mg_process_directory}/{param_card_file}", reweight_card_filename= f"{mg_process_directory}/{reweight_card_file}", ) # Create run card if run_card_file is not None: export_run_card( template_filename=run_card_file, run_card_filename= f"{mg_process_directory}/{new_run_card_file}", systematics=systematics_used, order=order, ) # Copy Pythia card if pythia8_card_file is not None: copy_file( pythia8_card_file, f"{mg_process_directory}/{new_pythia8_card_file}") # Copy Configuration card if configuration_file is not None: copy_file( configuration_file, f"{mg_process_directory}/{new_configuration_file}") # Run MG and Pythia if only_prepare_script: mg_script = setup_mg_with_scripts( 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, configuration_file_from_mgprocdir= new_configuration_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, python_executable=python_executable, order=order, ) mg_scripts.append(mg_script) else: run_mg( mg_directory, mg_process_directory, f"{mg_process_directory}/{mg_commands_filename}", f"{mg_process_directory}/{new_run_card_file}", f"{mg_process_directory}/{param_card_file}", f"{mg_process_directory}/{reweight_card_file}", None if new_pythia8_card_file is None else f"{mg_process_directory}/{new_pythia8_card_file}", None if new_configuration_file is None else f"{mg_process_directory}/{new_configuration_file}", is_background=is_background, initial_command=initial_command, log_file=f"{log_directory}/{log_file_run}", python_executable=python_executable, order=order, ) i += 1 n_runs_total = i # Master shell script if only_prepare_script: master_script_filename = f"{mg_process_directory}/madminer/run.sh" create_master_script( log_directory, master_script_filename, mg_directory, mg_process_directory, mg_scripts, ) logger.info( "To generate events, please run:\n\n %s [MG_directory] [MG_process_directory] [log_dir]\n\n", master_script_filename, ) else: expected_event_files = [ f"{mg_process_directory}/Events/run_{(i+1):02d}" for i in range(n_runs_total) ] expected_event_files = "\n".join(expected_event_files) logger.info( "Finished running MadGraph! Please check that events were successfully generated in the following " "folders:\n\n%s\n\n", expected_event_files, )