~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to models/loop_sm_FR/write_param_card.py

merge with 2.0.0beta4 revision 216

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import division
 
2
__date__ = "10 Dec 2012"
 
3
__author__ = 'olivier.mattelaer@uclouvain.be'
 
4
 
 
5
from function_library import *
 
6
 
 
7
 
 
8
class ParamCardWriter(object):
 
9
    
 
10
    header = \
 
11
    """######################################################################\n""" + \
 
12
    """## PARAM_CARD AUTOMATICALY GENERATED BY THE UFO  #####################\n""" + \
 
13
    """######################################################################\n"""   
 
14
    
 
15
    def __init__(self, filename, list_of_parameters=None, generic=False):
 
16
        """write a valid param_card.dat"""
 
17
        
 
18
        if not list_of_parameters:
 
19
            from parameters import all_parameters
 
20
            list_of_parameters = [param for param in all_parameters if \
 
21
                                                       param.nature=='external']
 
22
        
 
23
        self.generic_output = generic
 
24
        if generic:
 
25
            self.define_not_dep_param(list_of_parameters)
 
26
 
 
27
        
 
28
        self.fsock = open(filename, 'w')
 
29
        self.fsock.write(self.header)
 
30
        
 
31
        self.write_card(list_of_parameters)
 
32
        self.fsock.close()
 
33
    
 
34
    def define_not_dep_param(self, list_of_parameters):
 
35
        """define self.dep_mass and self.dep_width in case that they are 
 
36
        requested in the param_card.dat"""
 
37
        from particles import all_particles
 
38
        
 
39
        self.dep_mass = [(part, part.mass) for part in all_particles \
 
40
                            if part.pdg_code > 0 and \
 
41
                                            part.mass not in list_of_parameters]
 
42
        self.dep_width = [(part, part.width) for part in all_particles\
 
43
                             if part.pdg_code > 0 and \
 
44
                                part.width not in list_of_parameters]        
 
45
    
 
46
    @staticmethod
 
47
    def order_param(obj1, obj2):
 
48
        """ order parameter of a given block """
 
49
        
 
50
        maxlen = min([len(obj1.lhacode), len(obj2.lhacode)])
 
51
    
 
52
        for i in range(maxlen):
 
53
            if obj1.lhacode[i] < obj2.lhacode[i]:
 
54
                return -1
 
55
            elif obj1.lhacode[i] == obj2.lhacode[i]:
 
56
                return 0
 
57
            else:
 
58
                return 1
 
59
        #identical up to the first finish
 
60
        if len(obj1.lhacode) > len(obj2.lhacode):
 
61
            return 1
 
62
        elif  len(obj1.lhacode) == len(obj2.lhacode):
 
63
            return 0
 
64
        else:
 
65
            return -1
 
66
        
 
67
    def write_card(self, all_ext_param):
 
68
        """ """
 
69
        
 
70
        # list all lhablock
 
71
        all_lhablock = set([param.lhablock for param in all_ext_param])
 
72
        
 
73
        # ordonate lhablock alphabeticaly
 
74
        all_lhablock = list(all_lhablock)
 
75
        all_lhablock.sort()
 
76
        # put at the beginning SMINPUT + MASS + DECAY
 
77
        for name in ['DECAY', 'MASS','SMINPUTS']:
 
78
            if name in all_lhablock:
 
79
                all_lhablock.remove(name)
 
80
                all_lhablock.insert(0, name)
 
81
        
 
82
        for lhablock in all_lhablock:
 
83
            self.write_block(lhablock)
 
84
            need_writing = [ param for param in all_ext_param if \
 
85
                                                     param.lhablock == lhablock]
 
86
            need_writing.sort(self.order_param)
 
87
            [self.write_param(param, lhablock) for param in need_writing]
 
88
            
 
89
            if self.generic_output:
 
90
                if lhablock in ['MASS', 'DECAY']:
 
91
                    self.write_dep_param_block(lhablock)
 
92
 
 
93
        if self.generic_output:
 
94
            self.write_qnumber()
 
95
                               
 
96
    def write_block(self, name):
 
97
        """ write a comment for a block"""
 
98
        
 
99
        self.fsock.writelines(
 
100
        """\n###################################""" + \
 
101
        """\n## INFORMATION FOR %s""" % name.upper() +\
 
102
        """\n###################################\n"""
 
103
         )
 
104
        if name!='DECAY':
 
105
            self.fsock.write("""Block %s \n""" % name)
 
106
 
 
107
    def write_param(self, param, lhablock):
 
108
        
 
109
        lhacode=' '.join(['%3s' % key for key in param.lhacode])
 
110
        if lhablock != 'DECAY':
 
111
            text = """  %s %e # %s \n""" % (lhacode, complex(param.value).real, param.name ) 
 
112
        else:
 
113
            text = '''DECAY %s %e \n''' % (lhacode, complex(param.value).real)
 
114
        self.fsock.write(text) 
 
115
                    
 
116
 
 
117
 
 
118
    
 
119
    def write_dep_param_block(self, lhablock):
 
120
        import cmath
 
121
        from parameters import all_parameters
 
122
        for parameter in all_parameters:
 
123
            exec("%s = %s" % (parameter.name, parameter.value))
 
124
        text = "##  Not dependent paramater.\n"
 
125
        text += "## Those values should be edited following analytical the \n"
 
126
        text += "## analytical expression. Some generator could simply ignore \n"
 
127
        text += "## those values and use the analytical expression\n"
 
128
        
 
129
        if lhablock == 'MASS':
 
130
            data = self.dep_mass
 
131
            prefix = " "
 
132
        else:
 
133
            data = self.dep_width
 
134
            prefix = "DECAY "
 
135
        for part, param in data:
 
136
            if isinstance(param.value, str):
 
137
                value = complex(eval(param.value)).real
 
138
            else:
 
139
                value = param.value
 
140
            
 
141
            text += """%s %s %f # %s : %s \n""" %(prefix, part.pdg_code, 
 
142
                        value, part.name, param.value)
 
143
        self.fsock.write(text)    
 
144
    
 
145
    sm_pdg = [1,2,3,4,5,6,11,12,13,13,14,15,16,21,22,23,24,25]
 
146
    data="""Block QNUMBERS %(pdg)d  # %(name)s 
 
147
        1 %(charge)d  # 3 times electric charge
 
148
        2 %(spin)d  # number of spin states (2S+1)
 
149
        3 %(color)d  # colour rep (1: singlet, 3: triplet, 8: octet)
 
150
        4 %(antipart)d  # Particle/Antiparticle distinction (0=own anti)\n"""
 
151
    
 
152
    def write_qnumber(self):
 
153
        """ write qnumber """
 
154
        from particles import all_particles
 
155
        import particles
 
156
        print particles.__file__
 
157
        text="""#===========================================================\n"""
 
158
        text += """# QUANTUM NUMBERS OF NEW STATE(S) (NON SM PDG CODE)\n"""
 
159
        text += """#===========================================================\n\n"""
 
160
        
 
161
        for part in all_particles:
 
162
            if part.pdg_code in self.sm_pdg or part.pdg_code < 0:
 
163
                continue
 
164
            text += self.data % {'pdg': part.pdg_code,
 
165
                                 'name': part.name,
 
166
                                 'charge': 3 * part.charge,
 
167
                                 'spin': part.spin,
 
168
                                 'color': part.color,
 
169
                                 'antipart': part.name != part.antiname and 1 or 0}
 
170
        
 
171
        self.fsock.write(text)
 
172
        
 
173
            
 
174
            
 
175
            
 
176
            
 
177
        
 
178
            
 
179
if '__main__' == __name__:
 
180
    ParamCardWriter('./param_card.dat', generic=True)
 
181
    print 'write ./param_card.dat'
 
182