Belle II Software  release-06-02-00
write_param_card.py
1 
2 __date__ = "02 Aug 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_outputgeneric_output = generic
24  if generic:
25  self.define_not_dep_paramdefine_not_dep_param(list_of_parameters)
26 
27  self.fsockfsock = open(filename, 'w')
28  self.fsockfsock.write(self.headerheader)
29 
30  self.write_cardwrite_card(list_of_parameters)
31  self.fsockfsock.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_massdep_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_widthdep_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 = sorted(all_lhablock)
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_blockwrite_block(lhablock)
82  need_writing = [param for param in all_ext_param if
83  param.lhablock == lhablock]
84  need_writing.sort(self.order_paramorder_param)
85  [self.write_paramwrite_param(param, lhablock) for param in need_writing]
86 
87  if self.generic_outputgeneric_output:
88  if lhablock in ['MASS', 'DECAY']:
89  self.write_dep_param_blockwrite_dep_param_block(lhablock)
90 
91  if self.generic_outputgeneric_output:
92  self.write_qnumberwrite_qnumber()
93 
94  def write_block(self, name):
95  """ write a comment for a block"""
96 
97  self.fsockfsock.writelines(
98  """\n###################################""" +
99  """\n## INFORMATION FOR %s""" % name.upper() +
100  """\n###################################\n"""
101  )
102  if name != 'DECAY':
103  self.fsockfsock.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.fsockfsock.write(text)
113 
114  def write_dep_param_block(self, lhablock):
115  import cmath
116  from parameters import all_parameters
117  from particles import all_particles
118  for parameter in all_parameters:
119  exec("%s = %s" % (parameter.name, parameter.value))
120  text = "## Not dependent paramater.\n"
121  text += "## Those values should be edited following analytical the \n"
122  text += "## analytical expression. Some generator could simply ignore \n"
123  text += "## those values and use the analytical expression\n"
124 
125  if lhablock == 'MASS':
126  data = self.dep_massdep_mass
127  prefix = " "
128  else:
129  data = self.dep_widthdep_width
130  prefix = "DECAY "
131 
132  for part, param in data:
133  if isinstance(param.value, str):
134  value = complex(eval(param.value)).real
135  else:
136  value = param.value
137 
138  text += """%s %s %f # %s : %s \n""" % (prefix, part.pdg_code,
139  value, part.name, param.value)
140  # If more than a particles has the same mass/width we need to write it here
141  # as well
142  if lhablock == 'MASS':
143  arg = 'mass'
144  done = [part for (part, param) in self.dep_massdep_mass]
145  else:
146  arg = 'width'
147  done = [part for (part, param) in self.dep_widthdep_width]
148  for particle in all_particles:
149  if particle.pdg_code < 0:
150  continue
151  is_define = True
152  if particle not in done:
153  if getattr(particle, arg).lhacode[0] != particle.pdg_code:
154  is_define = False
155  if not is_define:
156  value = float(particle.get(arg).value)
157  name = particle.get(arg).name
158  text += """%s %s %f # %s : %s \n""" % (prefix, particle.pdg_code,
159  value, particle.name, name)
160 
161  self.fsockfsock.write(text)
162 
163  sm_pdg = [1, 2, 3, 4, 5, 6, 11, 12, 13, 13, 14, 15, 16, 21, 22, 23, 24, 25]
164  data = """Block QNUMBERS %(pdg)d # %(name)s
165  1 %(charge)d # 3 times electric charge
166  2 %(spin)d # number of spin states (2S+1)
167  3 %(color)d # colour rep (1: singlet, 3: triplet, 8: octet)
168  4 %(antipart)d # Particle/Antiparticle distinction (0=own anti)\n"""
169 
170  def write_qnumber(self):
171  """ write qnumber """
172  from particles import all_particles
173  import particles
174  print particles.__file__
175  text = """#===========================================================\n"""
176  text += """# QUANTUM NUMBERS OF NEW STATE(S) (NON SM PDG CODE)\n"""
177  text += """#===========================================================\n\n"""
178 
179  for part in all_particles:
180  if part.pdg_code in self.sm_pdgsm_pdg or part.pdg_code < 0:
181  continue
182  text += self.datadata % {'pdg': part.pdg_code,
183  'name': part.name,
184  'charge': 3 * part.charge,
185  'spin': part.spin,
186  'color': part.color,
187  'antipart': part.name != part.antiname and 1 or 0}
188 
189  self.fsockfsock.write(text)
190 
191 
192 if '__main__' == __name__:
193  ParamCardWriter('./param_card.dat', generic=True)
194  print 'write ./param_card.dat'
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