~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to models/loop_sm_UNITARY_ONLY/write_param_card.py

merge with 2.0.0beta4 revision 216

Show diffs side-by-side

added added

removed removed

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