Belle II Software  release-06-02-00
write_param_card.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 __date__ = '02 Aug 2012'
5 __author__ = 'olivier.mattelaer@uclouvain.be'
6 
7 from function_library import *
8 
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__(
18  self,
19  filename,
20  list_of_parameters=None,
21  generic=False,
22  ):
23  """write a valid param_card.dat"""
24 
25  if not list_of_parameters:
26  from parameters import all_parameters
27  list_of_parameters = [param for param in all_parameters
28  if param.nature == 'external']
29 
30  self.generic_outputgeneric_output = generic
31  if generic:
32  self.define_not_dep_paramdefine_not_dep_param(list_of_parameters)
33 
34  self.fsockfsock = open(filename, 'w')
35  self.fsockfsock.write(self.headerheader)
36 
37  self.write_cardwrite_card(list_of_parameters)
38  self.fsockfsock.close()
39 
40  def define_not_dep_param(self, list_of_parameters):
41  """define self.dep_mass and self.dep_width in case that they are
42  requested in the param_card.dat"""
43 
44  from particles import all_particles
45 
46  self.dep_massdep_mass = [(part, part.mass) for part in all_particles
47  if part.pdg_code > 0 and part.mass
48  not in list_of_parameters]
49  self.dep_widthdep_width = [(part, part.width) for part in all_particles
50  if part.pdg_code > 0 and part.width
51  not in list_of_parameters]
52 
53  @staticmethod
54  def order_param(obj1, obj2):
55  """ order parameter of a given block """
56 
57  maxlen = min([len(obj1.lhacode), len(obj2.lhacode)])
58 
59  for i in range(maxlen):
60  if obj1.lhacode[i] < obj2.lhacode[i]:
61  return -1
62  elif obj1.lhacode[i] == obj2.lhacode[i]:
63  return 0
64  else:
65  return 1
66  # identical up to the first finish
67  if len(obj1.lhacode) > len(obj2.lhacode):
68  return 1
69  elif len(obj1.lhacode) == len(obj2.lhacode):
70  return 0
71  else:
72  return -1
73 
74  def write_card(self, all_ext_param):
75  """ """
76 
77  # list all lhablock
78  all_lhablock = set([param.lhablock for param in all_ext_param])
79 
80  # ordonate lhablock alphabeticaly
81  all_lhablock = sorted(all_lhablock)
82  # put at the beginning SMINPUT + MASS + DECAY
83  for name in ['DECAY', 'MASS', 'SMINPUTS']:
84  if name in all_lhablock:
85  all_lhablock.remove(name)
86  all_lhablock.insert(0, name)
87 
88  for lhablock in all_lhablock:
89  self.write_blockwrite_block(lhablock)
90  need_writing = [param for param in all_ext_param if param.lhablock
91  == lhablock]
92  need_writing.sort(self.order_paramorder_param)
93  [self.write_paramwrite_param(param, lhablock) for param in need_writing]
94 
95  if self.generic_outputgeneric_output:
96  if lhablock in ['MASS', 'DECAY']:
97  self.write_dep_param_blockwrite_dep_param_block(lhablock)
98 
99  if self.generic_outputgeneric_output:
100  self.write_qnumberwrite_qnumber()
101 
102  def write_block(self, name):
103  """ write a comment for a block"""
104 
105  self.fsockfsock.writelines("""\n###################################"""
106  + """\n## INFORMATION FOR %s""" % name.upper()
107  + """
108 ###################################
109 """)
110  if name != 'DECAY':
111  self.fsockfsock.write("""Block %s \n""" % name)
112 
113  def write_param(self, param, lhablock):
114 
115  lhacode = """ """.join(['%3s' % key for key in param.lhacode])
116  if lhablock != 'DECAY':
117  text = """ %s %e # %s \n""" % (lhacode,
118  complex(param.value).real, param.name)
119  else:
120  text = '''DECAY %s %e \n''' % (lhacode, complex(param.value).real)
121  self.fsockfsock.write(text)
122 
123  def write_dep_param_block(self, lhablock):
124  import cmath
125  from parameters import all_parameters
126  from particles import all_particles
127  for parameter in all_parameters:
128  exec '%s = %s' % (parameter.name, parameter.value)
129  text = '## Not dependent paramater.\n'
130  text += '## Those values should be edited following analytical the \n'
131  text += \
132  '## analytical expression. Some generator could simply ignore \n'
133  text += '## those values and use the analytical expression\n'
134 
135  if lhablock == 'MASS':
136  data = self.dep_massdep_mass
137  prefix = """ """
138  else:
139  data = self.dep_widthdep_width
140  prefix = 'DECAY '
141 
142  for (part, param) in data:
143  if isinstance(param.value, str):
144  value = complex(eval(param.value)).real
145  else:
146  value = param.value
147 
148  text += """%s %s %f # %s : %s \n""" % (prefix, part.pdg_code,
149  value, part.name, param.value)
150  # If more than a particles has the same mass/width we need to write it here
151  # as well
152  if lhablock == 'MASS':
153  arg = 'mass'
154  done = [part for (part, param) in self.dep_massdep_mass]
155  else:
156  arg = 'width'
157  done = [part for (part, param) in self.dep_widthdep_width]
158  for particle in all_particles:
159  if particle.pdg_code < 0:
160  continue
161  is_define = True
162  if particle not in done:
163  if getattr(particle, arg).lhacode[0] != particle.pdg_code:
164  is_define = False
165  if not is_define:
166  value = float(particle.get(arg).value)
167  name = particle.get(arg).name
168  text += """%s %s %f # %s : %s \n""" % (prefix,
169  particle.pdg_code, value, particle.name, name)
170 
171  self.fsockfsock.write(text)
172 
173  sm_pdg = [
174  1,
175  2,
176  3,
177  4,
178  5,
179  6,
180  11,
181  12,
182  13,
183  13,
184  14,
185  15,
186  16,
187  21,
188  22,
189  23,
190  24,
191  25,
192  ]
193  data = \
194  """Block QNUMBERS %(pdg)d # %(name)s
195  1 %(charge)d # 3 times electric charge
196  2 %(spin)d # number of spin states (2S+1)
197  3 %(color)d # colour rep (1: singlet, 3: triplet, 8: octet)
198  4 %(antipart)d # Particle/Antiparticle distinction (0=own anti)
199 """
200 
201  def write_qnumber(self):
202  """ write qnumber """
203 
204  from particles import all_particles
205  import particles
206  print particles.__file__
207  text = \
208  """#===========================================================\n"""
209  text += """# QUANTUM NUMBERS OF NEW STATE(S) (NON SM PDG CODE)\n"""
210  text += \
211  """#===========================================================
212 
213 """
214 
215  for part in all_particles:
216  if part.pdg_code in self.sm_pdgsm_pdg or part.pdg_code < 0:
217  continue
218  text += self.datadata % {
219  'pdg': part.pdg_code,
220  'name': part.name,
221  'charge': 3 * part.charge,
222  'spin': part.spin,
223  'color': part.color,
224  'antipart': part.name != part.antiname and 1 or 0,
225  }
226 
227  self.fsockfsock.write(text)
228 
229 
230 if '__main__' == __name__:
231  ParamCardWriter('./param_card.dat', generic=True)
232  print 'write ./param_card.dat'
def define_not_dep_param(self, list_of_parameters)
def __init__(self, filename, list_of_parameters=None, generic=False)
double eval(const std::vector< double > &spl, const std::vector< double > &vals, double x)
Evaluate spline (zero order or first order) in point x.
Definition: tools.h:115