def make_stdHep(): print "Compiling StdHEP in %s."%str(os.path.join(MG5DIR, 'vendor', 'StdHEP')) # this is for 64-bit systems if sys.maxsize > 2**32: path = os.path.join(MG5DIR, 'vendor', 'StdHEP', 'src', 'make_opts') text = open(path).read() text = text.replace('MBITS=32','MBITS=64') open(path, 'w').writelines(text) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' elif misc.which('g77'): compiler = 'FC=g77' else: raise self.InvalidCmd('Require g77 or Gfortran compiler') else: compiler = '#FC=gfortran' base_compiler= ['FC=g77','FC=gfortran','#FC=g77','#FC=gfortran'] path = None path = os.path.join(MG5DIR, 'vendor', 'StdHEP', 'src', 'make_opts') text = open(path).read() for base in base_compiler: text = text.replace(base,compiler) open(path, 'w').writelines(text) misc.compile(cwd = os.path.join(MG5DIR, 'vendor', 'StdHEP'))
def make_stdHep(): print "Compiling StdHEP in %s." % str( os.path.join(MG5DIR, 'vendor', 'StdHEP')) # this is for 64-bit systems if sys.maxsize > 2**32: path = os.path.join(MG5DIR, 'vendor', 'StdHEP', 'src', 'make_opts') text = open(path).read() text = text.replace('MBITS=32', 'MBITS=64') open(path, 'w').writelines(text) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' elif misc.which('g77'): compiler = 'FC=g77' else: raise self.InvalidCmd('Require g77 or Gfortran compiler') else: compiler = '#FC=gfortran' base_compiler = ['FC=g77', 'FC=gfortran', '#FC=g77', '#FC=gfortran'] path = None path = os.path.join(MG5DIR, 'vendor', 'StdHEP', 'src', 'make_opts') text = open(path).read() for base in base_compiler: text = text.replace(base, compiler) open(path, 'w').writelines(text) misc.compile(cwd=os.path.join(MG5DIR, 'vendor', 'StdHEP'))
def generate(self, process, model): """Create a process""" try: shutil.rmtree(self.run_dir) except Exception as error: pass interface = MGCmd.MasterCmd() interface.no_notification() interface.run_cmd('import model %s' % model) if isinstance(process, str): interface.run_cmd('generate %s' % process) else: for p in process: interface.run_cmd('add process %s' % p) if logging.getLogger('madgraph').level <= 20: stdout = None stderr = None else: devnull = open(os.devnull, 'w') stdout = devnull stderr = devnull if not os.path.exists(pjoin(MG5DIR, 'pythia-pgs')): print("install pythia-pgs") p = subprocess.Popen([pjoin(MG5DIR, 'bin', 'mg5')], stdin=subprocess.PIPE, stdout=stdout, stderr=stderr) out = p.communicate('install pythia-pgs'.encode()) misc.compile(cwd=pjoin(MG5DIR, 'pythia-pgs')) if not os.path.exists(pjoin(MG5DIR, 'MadAnalysis')): print("install MadAnalysis") p = subprocess.Popen([pjoin(MG5DIR, 'bin', 'mg5')], stdin=subprocess.PIPE, stdout=stdout, stderr=stderr) out = p.communicate('install MadAnalysis4'.encode()) misc.compile(cwd=pjoin(MG5DIR, 'MadAnalysis')) if not misc.which('root'): raise Exception('root is require for this test') interface.exec_cmd('set pythia-pgs_path %s --no_save' % pjoin(MG5DIR, 'pythia-pgs')) interface.exec_cmd('set madanalysis_path %s --no_save' % pjoin(MG5DIR, 'MadAnalysis')) interface.onecmd('output madevent %s -f' % self.run_dir) if os.path.exists(pjoin(interface.options['syscalc_path'], 'sys_calc')): shutil.rmtree(interface.options['syscalc_path']) #print "install SysCalc" #interface.onecmd('install SysCalc') self.cmd_line = MECmd.MadEventCmdShell(me_dir=self.run_dir) self.cmd_line.no_notification() self.cmd_line.options['syscalc_path'] = pjoin(MG5DIR, 'SysCalc')
def make_CutTools(): print "Compiling CutTools in %s."%str(os.path.join(MG5DIR, 'vendor', 'CutTools')) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' else: raise self.InvalidCmd('Require gfortran compiler') else: compiler = '#FC=gfortran' base_compiler= ['FC=gfortran','#FC=gfortran'] path = None path = os.path.join(MG5DIR, 'vendor', 'CutTools', 'makefile') text = open(path).read() for base in base_compiler: text = text.replace(base,compiler) open(path, 'w').writelines(text) misc.compile(cwd = os.path.join(MG5DIR, 'vendor', 'CutTools'))
def make_CutTools(): print("Compiling CutTools in %s."%str(os.path.join(MG5DIR, 'vendor', 'CutTools'))) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' else: raise self.InvalidCmd('Require gfortran compiler') else: compiler = '#FC=gfortran' base_compiler= ['FC=gfortran','#FC=gfortran'] path = None path = os.path.join(MG5DIR, 'vendor', 'CutTools', 'makefile') text = open(path).read() for base in base_compiler: text = text.replace(base,compiler) open(path, 'w').writelines(text) misc.compile(cwd = os.path.join(MG5DIR, 'vendor', 'CutTools'))
def output_is_valid(self, position, pdf_check=True): """Test if the output files exist. Additionally if pdf_check is on True check if we can convert the output file in pdf. Finally delete files.""" # Check if exist self.assertTrue(os.path.isfile(position)) # Check if the file is valid if pdf_check and misc.which('convert'): filename, ext = os.path.splitext('position') os.system('convert ' + position + ' ' + filename + '.pdf') # Try is use to ensure that no file are left on disk try: self.assertTrue(os.path.isfile(filename + '.pdf')) except: os.remove(position) raise os.remove(filename + '.pdf') os.remove(position) return
class TestMECmdShell(unittest.TestCase): """this treats all the command not related to MG_ME""" def setUp(self): self.debugging = False if self.debugging: self.path = pjoin(MG5DIR, "tmp_test") if os.path.exists(self.path): shutil.rmtree(self.path) os.mkdir(pjoin(MG5DIR, "tmp_test")) else: self.path = tempfile.mkdtemp(prefix='acc_test_mg5') self.run_dir = pjoin(self.path, 'MGPROC') def tearDown(self): if self.path != pjoin(MG5DIR, "tmp_test"): shutil.rmtree(self.path) def generate(self, process, model): """Create a process""" try: shutil.rmtree(self.run_dir) except Exception, error: pass interface = MGCmd.MasterCmd() interface.no_notification() interface.run_cmd('import model %s' % model) if isinstance(process, str): interface.run_cmd('generate %s' % process) else: for p in process: interface.run_cmd('add process %s' % p) if logging.getLogger('madgraph').level <= 20: stdout = None stderr = None else: devnull = open(os.devnull, 'w') stdout = devnull stderr = devnull if not os.path.exists(pjoin(MG5DIR, 'pythia-pgs')): print "install pythia-pgs" p = subprocess.Popen([pjoin(MG5DIR, 'bin', 'mg5')], stdin=subprocess.PIPE, stdout=stdout, stderr=stderr) out = p.communicate('install pythia-pgs') misc.compile(cwd=pjoin(MG5DIR, 'pythia-pgs')) if not os.path.exists(pjoin(MG5DIR, 'MadAnalysis')): print "install MadAnalysis" p = subprocess.Popen([pjoin(MG5DIR, 'bin', 'mg5')], stdin=subprocess.PIPE, stdout=stdout, stderr=stderr) out = p.communicate('install MadAnalysis4') misc.compile(cwd=pjoin(MG5DIR, 'MadAnalysis')) if not misc.which('root'): raise Exception, 'root is require for this test' interface.exec_cmd('set pythia-pgs_path %s --no_save' % pjoin(MG5DIR, 'pythia-pgs')) interface.exec_cmd('set madanalysis_path %s --no_save' % pjoin(MG5DIR, 'MadAnalysis')) interface.onecmd('output madevent %s -f' % self.run_dir) if os.path.exists(pjoin(interface.options['syscalc_path'], 'sys_calc')): shutil.rmtree(interface.options['syscalc_path']) #print "install SysCalc" #interface.onecmd('install SysCalc') self.cmd_line = MECmd.MadEventCmdShell(me_dir=self.run_dir) self.cmd_line.no_notification() self.cmd_line.options['syscalc_path'] = pjoin(MG5DIR, 'SysCalc')
class MG4Runner(MERunner): """Runner object for the MG4 Matrix Element generator.""" mg4_path = "" type = 'v4' name = 'MadGraph v4' compilator = 'f77' if misc.which('gfortran'): compilator = 'gfortran' model = '' orders = {} energy = "" def setup(self, mg4_path, temp_dir=None): """Setup routine: create a temporary copy of Template and execute the proper script to use the standalone mode. the temp_dir variable can be given to specify the name of the process directory, otherwise a temporary one is created.""" self.proc_list = [] self.res_list = [] self.setup_flag = False self.check_path(mg4_path) # Create a copy of Template if not os.path.isdir(os.path.join(mg4_path, "Template")) or \ not os.path.isdir(os.path.join(mg4_path, "HELAS")): raise IOError, "Path %s is not a valid MG4 path" % str(mg4_path) self.mg4_path = os.path.abspath(mg4_path) if not temp_dir: i = 0 while os.path.exists( os.path.join(mg4_path, "ptest_%s_%s" % (self.type, i))): i += 1 temp_dir = "ptest_%s_%s" % (self.type, i) if os.path.exists(os.path.join(mg4_path, temp_dir)): raise IOError, "Path %s for test already exist" % \ str(os.path.join(mg4_path, temp_dir)) shutil.copytree(os.path.join(mg4_path, 'Template'), os.path.join(mg4_path, temp_dir)) self.temp_dir_name = temp_dir # Execute the standalone script in it subprocess.call(os.path.join('bin', 'standalone'), cwd=os.path.join(mg4_path, temp_dir), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT) # Set the setup flag to true to tell other routines everything is OK self.setup_flag = True # print some info logging.info("Temporary standalone directory %s successfully created" % \ temp_dir) def check_path(self, mg4_path): """Check the path for necessary directories""" if not os.path.isdir(os.path.join(mg4_path, "Template")) or \ not os.path.isdir(os.path.join(mg4_path, "HELAS")): raise IOError, "Path %s is not a valid MG4 path" % str(mg4_path) def cleanup(self): """Clean up temporary directories""" #if not self.setup_flag: # raise self.MERunnerException, \ # "MERunner setup should be called first" try: if os.path.isdir(os.path.join(self.mg4_path, self.temp_dir_name)): shutil.rmtree(os.path.join(self.mg4_path, self.temp_dir_name)) logging.info("Temporary standalone directory %s successfully removed" % \ self.temp_dir_name) except: pass def run(self, proc_list, model, orders={}, energy=1000): """Execute MG4 on the list of processes mentioned in proc_list, using the specified model, the specified maximal coupling orders and a certain energy for incoming particles (for decay, incoming particle is at rest). """ # Due to the limitation for the number of proc defined in proc_card, # work with bunches of fixed number of proc. bunch_size = 1000 curr_index = 0 self.proc_list = proc_list self.model = model self.orders = orders self.energy = energy dir_name = os.path.join(self.mg4_path, self.temp_dir_name) self.fix_energy_in_check(dir_name, energy) while (curr_index < len(proc_list)): temp_proc_list = proc_list[curr_index:min(curr_index + bunch_size, len(proc_list ))] # Create a proc_card.dat in the v4 format proc_card_file = open( os.path.join(dir_name, 'Cards', 'proc_card.dat'), 'w') proc_card_file.write( self.format_mg4_proc_card(temp_proc_list, model, orders)) proc_card_file.close() logging.info("proc_card.dat file for %i processes successfully created in %s" % \ (len(temp_proc_list), os.path.join(dir_name, 'Cards'))) # Run the newprocess script devnull = open(os.devnull, 'w') logging.info("Running newprocess script") subprocess.call(os.path.join('.', 'bin', 'newprocess'), cwd=dir_name, stdout=devnull, stderr=devnull) # Get the ME value for i, proc in enumerate(temp_proc_list): self.res_list.append(self.get_me_value(proc, i)) curr_index += bunch_size return self.res_list def format_mg4_proc_card(self, proc_list, model, orders): """Create a proc_card.dat string following v4 conventions. Does not support v5 decay chain format for the moment not squared order constraints.""" # TODO: fix the decay chain notation proc_card_template = template_files.mg4_proc_card.mg4_template process_template = template_files.mg4_proc_card.process_template proc_string = "" couplings = '\n'.join(["%s=%i" % (k, v) for k, v in orders.items()]) for i, proc in enumerate(proc_list): proc_string += process_template.substitute({ 'process': proc + ' @%i' % i, 'coupling': couplings }) return proc_card_template.substitute({ 'process': proc_string, 'model': model, 'multiparticle': '' }) def get_me_value(self, proc, proc_id): """Compile and run ./check, then parse the output and return the result for process with id = proc_id.""" sys.stdout.write('.') sys.stdout.flush() working_dir = os.path.join(self.mg4_path, self.temp_dir_name) shell_name = None directories = glob.glob( os.path.join(working_dir, 'SubProcesses', 'P%i_*' % proc_id)) if directories and os.path.isdir(directories[0]): shell_name = os.path.basename(directories[0]) # If directory doesn't exist, skip and return 0 if not shell_name: logging.info("Directory hasn't been created for process %s" % (proc)) return ((0.0, 0), []) logging.info("Working on process %s in dir %s" % (proc, shell_name)) dir_name = os.path.join(working_dir, 'SubProcesses', shell_name) # Run make devnull = open(os.devnull, 'w') retcode = subprocess.call('make', cwd=dir_name, stdout=devnull, stderr=devnull) if retcode != 0: logging.info("Error while executing make in %s" % shell_name) return ((0.0, 0), []) # Run ./check try: output = subprocess.Popen('./check', cwd=dir_name, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout return self.parse_check_output(output.read()) output.close() except IOError: logging.warning("Error while executing ./check in %s" % shell_name) return ((0.0, 0), []) def parse_check_output(self, output): """Parse the output string and return a pair where first value is the ME value and GeV exponent and the second value is a list of 4 momenta for all particles involved.""" res_p = [] value = 0.0 gev_pow = 0 momentum_pattern = re.compile( r"""\s*\d+\s+(?P<p0>-?\d*\.\d*E[+-]?\d*)\s+ (?P<p1>-?\d*\.\d*E[+-]?\d*)\s+ (?P<p2>-?\d*\.\d*E[+-]?\d*)\s+ (?P<p3>-?\d*\.\d*E[+-]?\d*)""", re.IGNORECASE | re.VERBOSE) me_value_pattern = re.compile( r"\sMatrix\selement\s=\s*(?P<value>-?\d*\.\d*(E[+-]?\d*)?)\s*GeV\^\s*(?P<pow>-?\d+)", re.IGNORECASE | re.VERBOSE) for line in output.split('\n'): match_momentum = momentum_pattern.match(line) if match_momentum: res_p.append([float(s) for s in match_momentum.groups()]) match_value = me_value_pattern.match(line) if match_value: value = float(match_value.group('value')) gev_pow = int(match_value.group('pow')) return ((value, gev_pow), res_p) def fix_energy_in_check(self, dir_name, energy): """Replace the hard coded collision energy in check_sa.f by the given energy, assuming a working dir dir_name""" for check_sa_path in glob.glob(\ os.path.join(dir_name, 'SubProcesses','*','check_sa.f')): file = open(check_sa_path, 'r') check_sa = file.read() file.close() file = open(check_sa_path, 'w') file.write( re.sub("SQRTS=1000d0", "SQRTS=%id0" % int(energy), check_sa)) file.close()
def make_IREGI(): print "Compiling IREGI in %s." % str( os.path.join(MG5DIR, 'vendor', 'IREGI', 'src')) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' else: raise self.InvalidCmd('Require gfortran compiler') else: compiler = '#FC=gfortran' iregi_path = os.path.join(MG5DIR, 'vendor', 'IREGI') # replace the current compiler to be gfortran if compiler == 'FC=gfortran': # in case there is #FC -> FC #search file in src file_to_change = misc.find_makefile_in_dir( os.path.join(iregi_path, 'src')) comp_re = re.compile('^(\s*)#FC\s*=\s*(.+)\s*$') var = 'FC' mod = False for name in file_to_change: lines = open(name, 'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = result.group( 1) + var + "=" + result.group(2) if mod: open(name, 'w').write('\n'.join(lines)) mod = False # search file in src/oneloop file_to_change = misc.find_makefile_in_dir( os.path.join(iregi_path, 'src', 'oneloop')) mod = False for name in file_to_change: lines = open(name, 'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = result.group( 1) + var + "=" + result.group(2) if mod: open(name, 'w').write('\n'.join(lines)) mod = False # change FC = current to FC = new current = misc.detect_current_compiler( os.path.join(iregi_path, 'src', 'makefile_ML5_lib')) new = 'gfortran' if current != new: misc.mod_compilator(os.path.join(iregi_path, 'src'), new, current) misc.mod_compilator(os.path.join(iregi_path, 'src', 'oneloop'), new, current) else: #search file in src file_to_change = misc.find_makefile_in_dir( os.path.join(iregi_path, 'src')) comp_re = re.compile('^(\s*)FC\s*=\s*(.+)\s*$') var = 'FC' mod = False for name in file_to_change: lines = open(name, 'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = '#' + result.group( 1) + var + "=" + result.group(2) if mod: open(name, 'w').write('\n'.join(lines)) mod = False # search file in src/oneloop file_to_change = misc.find_makefile_in_dir( os.path.join(iregi_path, 'src', 'oneloop')) mod = False for name in file_to_change: lines = open(name, 'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = '#' + result.group( 1) + var + "=" + result.group(2) if mod: open(name, 'w').write('\n'.join(lines)) mod = False misc.compile(cwd=os.path.join(iregi_path, 'src'))
def make_IREGI(): print "Compiling IREGI in %s."%str(os.path.join(MG5DIR, 'vendor', 'IREGI','src')) # Set the correct fortran compiler if 'FC' not in os.environ or not os.environ['FC']: if misc.which('gfortran'): compiler = 'FC=gfortran' else: raise self.InvalidCmd('Require gfortran compiler') else: compiler = '#FC=gfortran' iregi_path = os.path.join(MG5DIR, 'vendor', 'IREGI') # replace the current compiler to be gfortran if compiler == 'FC=gfortran': # in case there is #FC -> FC #search file in src file_to_change=misc.find_makefile_in_dir(os.path.join(iregi_path,'src')) comp_re = re.compile('^(\s*)#FC\s*=\s*(.+)\s*$') var = 'FC' mod = False for name in file_to_change: lines = open(name,'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = result.group(1) + var + "=" + result.group(2) if mod: open(name,'w').write('\n'.join(lines)) mod = False # search file in src/oneloop file_to_change=misc.find_makefile_in_dir(os.path.join(iregi_path,'src','oneloop')) mod = False for name in file_to_change: lines = open(name,'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = result.group(1) + var + "=" + result.group(2) if mod: open(name,'w').write('\n'.join(lines)) mod = False # change FC = current to FC = new current = misc.detect_current_compiler( os.path.join(iregi_path,'src','makefile_ML5_lib')) new = 'gfortran' if current != new: misc.mod_compilator(os.path.join(iregi_path,'src'), new,current) misc.mod_compilator(os.path.join(iregi_path,'src','oneloop'), new, current) else: #search file in src file_to_change=misc.find_makefile_in_dir(os.path.join(iregi_path,'src')) comp_re = re.compile('^(\s*)FC\s*=\s*(.+)\s*$') var = 'FC' mod = False for name in file_to_change: lines = open(name,'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = '#'+result.group(1) + var + "=" + result.group(2) if mod: open(name,'w').write('\n'.join(lines)) mod = False # search file in src/oneloop file_to_change=misc.find_makefile_in_dir(os.path.join(iregi_path,'src','oneloop')) mod = False for name in file_to_change: lines = open(name,'r').read().split('\n') for iline, line in enumerate(lines): result = comp_re.match(line) if result: mod = True lines[iline] = '#'+result.group(1) + var + "=" + result.group(2) if mod: open(name,'w').write('\n'.join(lines)) mod = False misc.compile(cwd = os.path.join(iregi_path,'src'))