~maddevelopers/mg5amcnlo/3.4.0

« back to all changes in this revision

Viewing changes to tests/input_files/SMEFTatNLO_running/write_param_card.py

  • Committer: olivier-mattelaer
  • Date: 2022-04-06 19:53:03 UTC
  • mfrom: (962.3.26 3.2.0_eft_running)
  • Revision ID: olivier-mattelaer-20220406195303-ikpe2rvh6olzimyw
merge with eft_running branch

Show diffs side-by-side

added added

removed removed

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