~maddevelopers/mg5amcnlo/WWW5_caching

« back to all changes in this revision

Viewing changes to users/mardelcourt/PROC_141512/PROC_141512/bin/internal/ufomodel/write_param_card.py

  • Committer: John Doe
  • Date: 2013-03-25 20:27:02 UTC
  • Revision ID: john.doe@gmail.com-20130325202702-5sk3t1r8h33ca4p4
first clean version

Show diffs side-by-side

added added

removed removed

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