diff --git a/Models/Feynrules/python/slha2herwig b/Models/Feynrules/python/slha2herwig --- a/Models/Feynrules/python/slha2herwig +++ b/Models/Feynrules/python/slha2herwig @@ -1,111 +1,112 @@ #! /usr/bin/env python from __future__ import division +from __future__ import print_function import os, sys, argparse, re, string comment_pat = re.compile('\s*#.*$') block_pat = re.compile('^\s*block\s+(\w+)',flags=re.I) decay_pat = re.compile('^\s*decay\s+(\w+)\s+([-+\.\w]+)',flags=re.I) data_pat = re.compile('^\s*((\d+\s+)+)(-?\d\S*)\s*$') whitespace = re.compile('\s+') PARAMS = {} # set up the option parser for command line input parser = argparse.ArgumentParser( description='Modify a ThePEG model file with parameters from a matching SLHA file.' ) parser.add_argument( 'modelfile', metavar='ThePEG_model', help='ThePEG model file to use as template. Must have "# SLHA #"" annotations.' ) parser.add_argument( 'slhafile', metavar='SLHA_file', help='SLHA spectrum file.' ) parser.add_argument( '-o','--output', default="FRModel_slha.model", help="Name for the output file" ) args = parser.parse_args() with open(args.slhafile) as f: currentblock = None for line in f: line = comment_pat.sub('',line.rstrip()) if not line: continue m = block_pat.match(line) d = decay_pat.match(line) if m: currentblock = m.group(1).upper() elif d: currentblock = None label = 'DECAY_%s' % d.group(1) data = float(d.group(2)) PARAMS[label] = data elif currentblock is not None: d = data_pat.match(line) if d: index = whitespace.sub('_',d.group(1).rstrip()) try: data = float(d.group(3)) except ValueError: continue label = '%s_%s' % (currentblock, index) #if label in PARAMS: PARAMS[label] = data template = open(args.modelfile) output = open(args.output,'w') line = template.readline() while line: split = line.split("${") if (len(split) == 1 ) : output.write(line) else : outputLine = split[0] for i in range(1,len(split)) : split2 = split[i].split("}") key = split2[0] if key in PARAMS : outputLine += str(PARAMS[key]) + split2[1] else : name, suffix = key.rsplit('_',1) if suffix == 'ABS': mass = PARAMS[name] try: mass = mass.real except: pass outputLine += str(abs(mass)) + split2[1] elif suffix == 'CTAU': hbarc = 197.3269631e-15 if(name in PARAMS) : width = PARAMS[name] ctau = (hbarc / width) if width != 0 else 0 else : ctau = 0 outputLine += str(ctau) + split2[1] elif suffix == 'WCUT': if(name in PARAMS) : width = PARAMS[name] else : width = 0. wcut = 10.0 * width outputLine += str(wcut) + split2[1] elif name == 'DECAY': outputLine += str(0.) + split2[1] else : - print 'Parameter ',key,'not set in SLHA file, keeping default' + print ('Parameter ',key,'not set in SLHA file, keeping default') outputLine = '#' + outputLine + "${" + split2[0] + "}" + split2[1] output.write(outputLine) line = template.readline() output.write("\n") -print "Output written as : ",args.output +print ("Output written as : ",args.output) diff --git a/Models/Feynrules/python/ufo2peg/helpers.py b/Models/Feynrules/python/ufo2peg/helpers.py --- a/Models/Feynrules/python/ufo2peg/helpers.py +++ b/Models/Feynrules/python/ufo2peg/helpers.py @@ -1,312 +1,316 @@ from __future__ import print_function from string import Template from os import path import sys,cmath,glob import re """ Helper functions for the Herwig Feynrules converter """ class CheckUnique: """Uniqueness checker. An object of this class remembers the value it was called with first. Any subsequent call to it will only succeed if the same value is passed again. For example, >>> f = CheckUnique() >>> f(5) >>> f(5) >>> f(4) Traceback (most recent call last): ... AssertionError """ def __init__(self): self.val = None def __call__(self,val): """Store value on first call, then compare.""" if self.val is None: self.val = val else: assert( val == self.val ) def is_number(s): """Check if a value is a number.""" try: float(s) except ValueError: return False return True def getTemplate(name): """Create a python string template from a file.""" templatename = '{name}.template'.format(name=name) # assumes the template files sit next to this script moduledir = path.dirname(path.abspath(__file__)) templatepath = path.join(moduledir,templatename) with open(templatepath, 'r') as f: templateText = f.read() return Template( templateText ) def writeFile(filename, text): """Write text to a filename.""" with open(filename,'w') as f: f.write(text) def coupling_orders(vertex, coupling, defns): # if more than one take QCD over QED and then lowest order in QED if type(coupling) is list: print('not sure this happens') quit() qed = 0 qcd = 0 for coup in coupling : qed1 = coup.order.get('QED',0) qcd1 = coup.order.get('QCD',0) if qcd1 != 0: if qcd == 0 or (qcd1 != 0 and qcd1 < qcd): qcd=qcd1 qed=qed1 else: if qed == 0 or (qed1 != 0 and qed1 < qed): qed=qed1 else: output={} for ctype in defns : output[ctype]=coupling.order.get(ctype,0) return output def def_from_model(FR,s): """Return a C++ line that defines parameter s as coming from the model file.""" if("hw_kine" in s) :return "" stype = typemap(getattr(FR.parameters,s).type) return '{t} {s} = model_->{s}();'.format(t=stype,s=s) _typemap = {'complex':'Complex', 'real':'double'} def typemap(s): return _typemap[s] def add_brackets(expr, syms): result = expr for s in syms: pattern = r'({symb})(\W|$)'.format(symb=s) result = re.sub(pattern, r'\1()\2', result) return result def banner(): return """\ =============================================================================================================== ______ ______ _ __ _ _ _ | ___| | ___ \ | | / /| | | | (_) _ _ | |_ ___ _ _ _ __ | |_/ /_ _ | | ___ ___ / / | |_| | ___ _ __ __ __ _ __ _ _| |_ _| |_ | _|/ _ \| | | || \_ \ | /| | | || | / _ \/ __| / / | _ | / _ \| \__|\ \ /\ / /| | / _` ||_ _||_ _| | | | __/| |_| || | | || |\ \| |_| || || __/\__ \ / / | | | || __/| | \ V V / | || (_| | |_| |_| \_| \___| \__, ||_| |_|\_| \_|\__,_||_| \___||___//_/ \_| |_/ \___||_| \_/\_/ |_| \__, | __/ | __/ | |___/ |___/ =============================================================================================================== generating model/vertex/.model/.in files please be patient! =============================================================================================================== """ #################### ??? ####################### # function that replaces alphaS (aS)-dependent variables # with their explicit form which also contains strongCoupling def aStoStrongCoup(stringin, paramstoreplace, paramstoreplace_expressions): #print stringin for xx in range(0,len(paramstoreplace)): #print paramstoreplace[xx], paramstoreplace_expressions[xx] stringout = stringin.replace(paramstoreplace[xx], '(' + PyMathToThePEGMath(paramstoreplace_expressions[xx],allparams) + ')') stringout = stringout.replace('aS', '(sqr(strongCoupling(q2))/(4.0*Constants::pi))') #print 'resulting string', stringout return stringout # function that replaces alphaEW (aEW)-dependent variables # with their explicit form which also contains weakCoupling def aEWtoWeakCoup(stringin, paramstoreplace, paramstoreplace_expressions): #print stringin for xx in range(0,len(paramstoreplace)): #print paramstoreplace[xx], paramstoreplace_expressions[xx] stringout = stringin.replace(paramstoreplace[xx], '(' + PyMathToThePEGMath(paramstoreplace_expressions[xx],allparams) + ')') stringout = stringout.replace('aEWM1', '(1/(sqr(electroMagneticCoupling(q2))/(4.0*Constants::pi)))') #print 'resulting string', stringout return stringout if __name__ == "__main__": import doctest doctest.testmod() if False: # Check if the Vertex is self-conjugate or not pdgcode = [0,0,0,0] notsmvertex = False vhasw = 0 vhasz = 0 vhasf = 0 vhasg = 0 vhash = 0 vhasp = 0 # print 'printing particles in vertex' for i in range(len(v.particles)): # print v.particles[i].pdg_code pdgcode[i] = v.particles[i].pdg_code if pdgcode[i] == 23: vhasz += 1 if pdgcode[i] == 22: vhasp += 1 if pdgcode[i] == 25: vhash += 1 if pdgcode[i] == 21: vhasg += 1 if pdgcode[i] == 24: vhasw += 1 if abs(pdgcode[i]) < 7 or (abs(pdgcode[i]) > 10 and abs(pdgcode[i]) < 17): vhasf += 1 if pdgcode[i] not in SMPARTICLES: notsmvertex = True # treat replacement of SM vertices with BSM vertices? if notsmvertex == False: if( (vhasf == 2 and vhasz == 1) or (vhasf == 2 and vhasw == 1) or (vhasf == 2 and vhash == 1) or (vhasf == 2 and vhasg == 1) or (vhasf == 2 and vhasp == 0) or (vhasg == 3) or (vhasg == 4) or (vhasw == 2 and vhash == 1) or (vhasw == 3) or (vhasw == 4) or (vhash == 1 and vhasg == 2) or (vhash == 1 and vhasp == 2)): #print 'VERTEX INCLUDED IN STANDARD MODEL!' v.include = 0 notincluded += 1 #continue selfconjugate = 0 for j in range(len(pdgcode)): for k in range(len(pdgcode)): if j != k and j != 0 and abs(pdgcode[j]) == abs(pdgcode[k]): selfconjugate = 1 #print 'self-conjugate vertex' # print pdgcode[j] # if the Vertex is not self-conjugate, then add the conjugate vertex # automatically scfac = [1,1,1,1] if selfconjugate == 0: #first find the self-conjugate particles for u in range(len(v.particles)): if v.particles[u].selfconjugate == 0: scfac[u] = -1 # print 'particle ', v.particles[u].pdg_code, ' found not to be self-conjugate' if selfconjugate == 0: plistarray[1] += str(scfac[1] * v.particles[1].pdg_code) + ',' + str(scfac[0] * v.particles[0].pdg_code) + ',' + str(scfac[2] * v.particles[2].pdg_code) - if len(v.particles) is 4: + if len(v.particles) == 4: plistarray[1] += ',' + str(scfac[3] * v.particles[3].pdg_code) #print 'Conjugate vertex:', plistarray[1] class SkipThisVertex(Exception): pass def extractAntiSymmetricIndices(instring,funct) : terms = instring.strip(funct).strip(")").split(",") sign=1. for iy in range(0,len(terms)) : for ix in range(-1,-len(terms)+iy,-1) : swap = False if(len(terms[ix])==1 and len(terms[ix-1])==1) : swap = int(terms[ix]) items line=line.replace("iteritems","items") # fix imports if("import" in line) : for val in names : if("import %s" %val in line) : - line=line.replace("import %s" %val, - "from . import %s" %val) + if("from ." not in line) : + line=line.replace("import %s" %val, + "from . import %s" %val) if(val in tNames) : tNames.remove(val) if("from %s" %val in line) : line=line.replace("from %s" %val, "from .%s" %val) + # and raise + elif "raise" in line and "," in line : + line = line.replace(",","(").rstrip() + ")\n" # add brackets to print statements if("print" in line) : if line.strip()[0:5] == "print" : line=line.strip("\n").replace("print","print(")+")\n" output += line line=inFile.readline() inFile.close() if(isInit) : if "__init__" in tNames : tNames.remove("__init__") temp="" for val in tNames : temp+= "from . import %s\n" % val output = temp+output with open(fName,'w') as dest: dest.write(output)