Belle II Software  release-05-01-25
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_output = generic
31  if generic:
32  self.define_not_dep_param(list_of_parameters)
33 
34  self.fsock = open(filename, 'w')
35  self.fsock.write(self.header)
36 
37  self.write_card(list_of_parameters)
38  self.fsock.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_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_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 = list(all_lhablock)
82  all_lhablock.sort()
83  # put at the beginning SMINPUT + MASS + DECAY
84  for name in ['DECAY', 'MASS', 'SMINPUTS']:
85  if name in all_lhablock:
86  all_lhablock.remove(name)
87  all_lhablock.insert(0, name)
88 
89  for lhablock in all_lhablock:
90  self.write_block(lhablock)
91  need_writing = [param for param in all_ext_param if param.lhablock
92  == lhablock]
93  need_writing.sort(self.order_param)
94  [self.write_param(param, lhablock) for param in need_writing]
95 
96  if self.generic_output:
97  if lhablock in ['MASS', 'DECAY']:
98  self.write_dep_param_block(lhablock)
99 
100  if self.generic_output:
101  self.write_qnumber()
102 
103  def write_block(self, name):
104  """ write a comment for a block"""
105 
106  self.fsock.writelines("""\n###################################"""
107  + """\n## INFORMATION FOR %s""" % name.upper()
108  + """
109 ###################################
110 """)
111  if name != 'DECAY':
112  self.fsock.write("""Block %s \n""" % name)
113 
114  def write_param(self, param, lhablock):
115 
116  lhacode = """ """.join(['%3s' % key for key in param.lhacode])
117  if lhablock != 'DECAY':
118  text = """ %s %e # %s \n""" % (lhacode,
119  complex(param.value).real, param.name)
120  else:
121  text = '''DECAY %s %e \n''' % (lhacode, complex(param.value).real)
122  self.fsock.write(text)
123 
124  def write_dep_param_block(self, lhablock):
125  import cmath
126  from parameters import all_parameters
127  from particles import all_particles
128  for parameter in all_parameters:
129  exec '%s = %s' % (parameter.name, parameter.value)
130  text = '## Not dependent paramater.\n'
131  text += '## Those values should be edited following analytical the \n'
132  text += \
133  '## analytical expression. Some generator could simply ignore \n'
134  text += '## those values and use the analytical expression\n'
135 
136  if lhablock == 'MASS':
137  data = self.dep_mass
138  prefix = """ """
139  else:
140  data = self.dep_width
141  prefix = 'DECAY '
142 
143  for (part, param) in data:
144  if isinstance(param.value, str):
145  value = complex(eval(param.value)).real
146  else:
147  value = param.value
148 
149  text += """%s %s %f # %s : %s \n""" % (prefix, part.pdg_code,
150  value, part.name, param.value)
151  # If more than a particles has the same mass/width we need to write it here
152  # as well
153  if lhablock == 'MASS':
154  arg = 'mass'
155  done = [part for (part, param) in self.dep_mass]
156  else:
157  arg = 'width'
158  done = [part for (part, param) in self.dep_width]
159  for particle in all_particles:
160  if particle.pdg_code < 0:
161  continue
162  is_define = True
163  if particle not in done:
164  if getattr(particle, arg).lhacode[0] != particle.pdg_code:
165  is_define = False
166  if not is_define:
167  value = float(particle.get(arg).value)
168  name = particle.get(arg).name
169  text += """%s %s %f # %s : %s \n""" % (prefix,
170  particle.pdg_code, value, particle.name, name)
171 
172  self.fsock.write(text)
173 
174  sm_pdg = [
175  1,
176  2,
177  3,
178  4,
179  5,
180  6,
181  11,
182  12,
183  13,
184  13,
185  14,
186  15,
187  16,
188  21,
189  22,
190  23,
191  24,
192  25,
193  ]
194  data = \
195  """Block QNUMBERS %(pdg)d # %(name)s
196  1 %(charge)d # 3 times electric charge
197  2 %(spin)d # number of spin states (2S+1)
198  3 %(color)d # colour rep (1: singlet, 3: triplet, 8: octet)
199  4 %(antipart)d # Particle/Antiparticle distinction (0=own anti)
200 """
201 
202  def write_qnumber(self):
203  """ write qnumber """
204 
205  from particles import all_particles
206  import particles
207  print particles.__file__
208  text = \
209  """#===========================================================\n"""
210  text += """# QUANTUM NUMBERS OF NEW STATE(S) (NON SM PDG CODE)\n"""
211  text += \
212  """#===========================================================
213 
214 """
215 
216  for part in all_particles:
217  if part.pdg_code in self.sm_pdg or part.pdg_code < 0:
218  continue
219  text += self.data % {
220  'pdg': part.pdg_code,
221  'name': part.name,
222  'charge': 3 * part.charge,
223  'spin': part.spin,
224  'color': part.color,
225  'antipart': part.name != part.antiname and 1 or 0,
226  }
227 
228  self.fsock.write(text)
229 
230 
231 if '__main__' == __name__:
232  ParamCardWriter('./param_card.dat', generic=True)
233  print 'write ./param_card.dat'
234 
darkphoton.write_param_card.ParamCardWriter.sm_pdg
list sm_pdg
Definition: write_param_card.py:174
darkphoton.write_param_card.ParamCardWriter.write_param
def write_param(self, param, lhablock)
Definition: write_param_card.py:114
darkphoton.write_param_card.ParamCardWriter
Definition: write_param_card.py:10
darkphoton.write_param_card.ParamCardWriter.write_dep_param_block
def write_dep_param_block(self, lhablock)
Definition: write_param_card.py:124
darkphoton.write_param_card.ParamCardWriter.write_block
def write_block(self, name)
Definition: write_param_card.py:103
darkphoton.write_param_card.ParamCardWriter.dep_mass
dep_mass
Definition: write_param_card.py:46
darkphoton.write_param_card.ParamCardWriter.data
data
Definition: write_param_card.py:194
darkphoton.write_param_card.ParamCardWriter.header
header
Definition: write_param_card.py:12
darkphoton.write_param_card.ParamCardWriter.dep_width
dep_width
Definition: write_param_card.py:49
Belle2::eval
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:118
darkphoton.write_param_card.ParamCardWriter.define_not_dep_param
def define_not_dep_param(self, list_of_parameters)
Definition: write_param_card.py:40
darkphoton.write_param_card.ParamCardWriter.fsock
fsock
Definition: write_param_card.py:29
darkphoton.write_param_card.ParamCardWriter.write_card
def write_card(self, all_ext_param)
Definition: write_param_card.py:74
darkphoton.write_param_card.ParamCardWriter.order_param
def order_param(obj1, obj2)
Definition: write_param_card.py:54
darkphoton.write_param_card.ParamCardWriter.generic_output
generic_output
Definition: write_param_card.py:25
darkphoton.write_param_card.ParamCardWriter.write_qnumber
def write_qnumber(self)
Definition: write_param_card.py:202
darkphoton.write_param_card.ParamCardWriter.__init__
def __init__(self, filename, list_of_parameters=None, generic=False)
Definition: write_param_card.py:17