1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """A File for splitting"""
16
17 import sys
18 import re
19 import os
20 import logging
21 try:
22 import madgraph
23 except ImportError:
24 import internal.misc as misc
25 import internal.banner as banner
26 from internal import InvalidCmd
27 else:
28 import madgraph.various.misc as misc
29 import madgraph.various.banner as banner
30 from madgraph import InvalidCmd
31
32 logger = logging.getLogger('madgraph.shower_card')
33
34 pjoin = os.path.join
35
38
40 """ """
41 true = ['.true.', 't', 'true', '1']
42 false = ['.false.', 'f', 'false', '0']
43 logical_vars = ['ue_enabled', 'hadronize', 'b_stable', 'pi_stable', 'wp_stable',
44 'wm_stable', 'z_stable', 'h_stable', 'tap_stable', 'tam_stable',
45 'mup_stable', 'mum_stable', 'is_4lep', 'is_bbar', 'combine_td']
46 string_vars = ['extralibs', 'extrapaths', 'includepaths', 'analyse']
47 for i in range(1,100):
48 string_vars.append('dm_'+str(i))
49 int_vars = ['nsplit_jobs', 'maxprint', 'nevents', 'pdfcode', 'rnd_seed', 'rnd_seed2', 'njmax']
50 float_vars = ['maxerrs', 'lambda_5', 'b_mass', 'qcut']
51
52
53
54
55
56
57 names_dict = {\
58 'ue_enabled' : {'HERWIG6':'lhsoft', 'PYTHIA6': 'mstp_81', 'HERWIGPP': 'ue_hwpp', 'PYTHIA8': 'ue_py8'},
59 'pdfcode' : {'HERWIG6':'pdfcode', 'PYTHIA6': 'pdfcode', 'HERWIGPP': 'pdfcode', 'PYTHIA8': 'pdfcode'},
60 'nevents' : {'HERWIG6':'nevents', 'PYTHIA6': 'nevents', 'HERWIGPP': 'nevents', 'PYTHIA8': 'nevents'},
61 'hadronize' : {'PYTHIA6': 'mstp_111', 'HERWIGPP': 'hadronize_hwpp', 'PYTHIA8': 'hadronize_py8'},
62 'b_stable' : {'HERWIG6':'b_stable_hw', 'PYTHIA6': 'b_stable_py', 'HERWIGPP': 'b_stable_hwpp', 'PYTHIA8': 'b_stable_py8'},
63 'pi_stable' : {'HERWIG6':'pi_stable_hw', 'PYTHIA6': 'pi_stable_py', 'HERWIGPP': 'pi_stable_hwpp', 'PYTHIA8': 'pi_stable_py8'},
64 'wp_stable' : {'HERWIG6':'wp_stable_hw', 'PYTHIA6': 'wp_stable_py', 'HERWIGPP': 'wp_stable_hwpp', 'PYTHIA8': 'wp_stable_py8'},
65 'wm_stable' : {'HERWIG6':'wm_stable_hw', 'PYTHIA6': 'wm_stable_py', 'HERWIGPP': 'wm_stable_hwpp', 'PYTHIA8': 'wm_stable_py8'},
66 'z_stable' : {'HERWIG6':'z_stable_hw', 'PYTHIA6': 'z_stable_py', 'HERWIGPP': 'z_stable_hwpp', 'PYTHIA8': 'z_stable_py8'},
67 'h_stable' : {'HERWIG6':'h_stable_hw', 'PYTHIA6': 'h_stable_py', 'HERWIGPP': 'h_stable_hwpp', 'PYTHIA8': 'h_stable_py8'},
68 'tap_stable' : {'HERWIG6':'taup_stable_hw', 'PYTHIA6': 'taup_stable_py', 'HERWIGPP': 'taup_stable_hwpp', 'PYTHIA8': 'taup_stable_py8'},
69 'tam_stable' : {'HERWIG6':'taum_stable_hw', 'PYTHIA6': 'taum_stable_py', 'HERWIGPP': 'taum_stable_hwpp', 'PYTHIA8': 'taum_stable_py8'},
70 'mup_stable' : {'HERWIG6':'mup_stable_hw', 'PYTHIA6': 'mup_stable_py', 'HERWIGPP': 'mup_stable_hwpp', 'PYTHIA8': 'mup_stable_py8'},
71 'mum_stable' : {'HERWIG6':'mum_stable_hw', 'PYTHIA6': 'mum_stable_py', 'HERWIGPP': 'mum_stable_hwpp', 'PYTHIA8': 'mum_stable_py8'},
72 'is_4lep' : {'PYTHIA6':'is_4l_py'},
73 'is_bbar' : {'HERWIG6':'is_bb_hw'},
74 'maxprint' : {'HERWIG6':'maxpr_hw', 'PYTHIA6': 'maxpr_py', 'HERWIGPP': 'maxpr_hwpp', 'PYTHIA8': 'maxpr_py8'},
75 'rnd_seed' : {'HERWIG6':'rndevseed1_hw', 'PYTHIA6': 'rndevseed_py', 'HERWIGPP': 'rndevseed_hwpp', 'PYTHIA8': 'rndevseed_py8'},
76 'rnd_seed2' : {'HERWIG6':'rndevseed2_hw'},
77 'maxerrs' : {'HERWIG6':'err_fr_hw', 'PYTHIA6': 'err_fr_py', 'HERWIGPP': 'err_fr_hwpp', 'PYTHIA8': 'err_fr_py8'},
78 'lambda_5' : {'HERWIG6':'lambdaherw', 'PYTHIA6': 'lambdapyth', 'HERWIGPP': 'lambdaherw', 'PYTHIA8': 'lambdapyth'},
79 'b_mass' : {'HERWIG6':'b_mass', 'PYTHIA6': 'b_mass', 'HERWIGPP': 'b_mass', 'PYTHIA8': 'b_mass'},
80 'analyse' : {'HERWIG6':'hwuti', 'PYTHIA6':'pyuti', 'HERWIGPP':'hwpputi', 'PYTHIA8':'py8uti'},
81 'qcut' : {'PYTHIA8':'qcut'},
82 'njmax' : {'PYTHIA8':'njmax'}}
83
84 stdhep_dict = {'HERWIG6':'mcatnlo_hwan_stdhep.o', 'PYTHIA6':'mcatnlo_pyan_stdhep.o'}
85
86 - def __init__(self, card=None, testing=False):
87 """ if testing, card is the content"""
88 self.testing = testing
89 dict.__init__(self)
90 self.keylist = self.keys()
91
92 if card:
93 self.read_card(card)
94
95
97 """read the shower_card, if testing card_path is the content"""
98 if not self.testing:
99 content = open(card_path).read()
100 else:
101 content = card_path
102 lines = content.split('\n')
103 list_dm = []
104 for l in lines:
105 if '#' in l:
106 l = l.split('#',1)[0]
107 if '=' not in l:
108 continue
109 args = l.split('=',1)
110 key = args[0].strip().lower()
111 value = args[1].strip()
112 self.set_param(key, value)
113 if str(key).upper().startswith('DM'):
114 list_dm.append(int(key.split('_',1)[1]))
115
116 for i in range(1,100):
117 if not i in list_dm:
118 self['dm_'+str(i)] = ''
119
120 self.text=content
121
122
123 - def set_param(self, key, value, write_to = ''):
124 """set the param key to value.
125 if write_to is passed then write the new shower_card:
126 if not testing write_to is an input path, if testing the text is
127 returned by the function
128 """
129
130 if key in self.logical_vars:
131 try:
132 self[key] = banner.ConfigFile.format_variable(value, bool, key)
133 except InvalidCmd, error:
134 raise ShowerCardError(str(error))
135 elif key in self.string_vars:
136 if value.lower() == 'none':
137 self[key] = ''
138 else:
139 self[key] = value
140 elif key in self.int_vars:
141 try:
142 self[key] = banner.ConfigFile.format_variable(value, int, key)
143 except InvalidCmd, error:
144 raise ShowerCardError(str(error))
145 elif key in self.float_vars:
146 try:
147 self[key] = banner.ConfigFile.format_variable(value, float, key)
148 except InvalidCmd, error:
149 raise ShowerCardError(str(error))
150 else:
151 raise ShowerCardError('Unknown entry: %s = %s' % (key, value))
152 self.keylist.append(key)
153
154
155 if write_to:
156 logger.info('modify parameter %s of the shower_card.dat to %s' % (key, value))
157 key_re = re.compile('^(\s*)%s\s*=\s*(.+)\s*$' % key , re.IGNORECASE)
158 newlines = []
159 for line in self.text.split('\n'):
160 key_match = key_re.match(line)
161 if key_match and not ( str(key).upper().startswith('DM') ):
162 try:
163 comment = line.split('#')[1]
164 except:
165 comment = ''
166 if key not in self.logical_vars:
167 newlines.append('%s = %s #%s' % (key, value, comment))
168 else:
169
170 if self[key]:
171 newlines.append('%s = %s #%s' % (key, 'T', comment))
172 else:
173 newlines.append('%s = %s #%s' % (key, 'F', comment))
174 elif key_match and ( str(key).upper().startswith('DM') ):
175 pass
176 else:
177 newlines.append(line)
178
179 if str(key).upper().startswith('DM') and not value.lower() in ['','none','default']:
180 newlines.append('%s = %s' % (str(key).upper(), value[0:len(value)]))
181 logger.info('please specify a decay through set DM_1 decay; see shower_card.dat for details')
182
183 self.text = '\n'.join(newlines) + '\n'
184
185 if self.testing:
186 return self.text
187 else:
188 open(write_to, 'w').write(self.text)
189 return ''
190 else:
191 return ''
192
193
194
196 """write the shower_card for shower in card_path.
197 if self.testing, card_path takes the value of the string"""
198
199 shower = shower.upper()
200 if shower.startswith('PYTHIA6'):
201 self.shower = 'PYTHIA6'
202 else:
203 self.shower = shower
204 lines = []
205 bool_dict = {True: '.true.', False: '.false.'}
206 bool_dict_num = {True: '1', False: '0'}
207
208 for key in self.keylist:
209 value = self[key]
210 if key in self.logical_vars:
211
212 if key in ['ue_enabled', 'hadronize'] and self.shower == 'PYTHIA6':
213 value = bool_dict_num[value]
214 else:
215 value = bool_dict[value]
216 elif key in self.string_vars:
217
218 if key == 'analyse':
219 if value is None or not value:
220 try:
221 value = self.stdhep_dict[self.shower]
222 except KeyError:
223 pass
224 try:
225 line = '%s="%s"' % (self.names_dict[key][self.shower].upper(), value)
226 lines.append(line)
227 continue
228 except KeyError:
229 continue
230 if value is None or not value:
231 value = ''
232 else:
233 value = '"%s"' % value
234
235 line = '%s=%s' % (key.upper(), value)
236 lines.append(line)
237 continue
238 elif key in self.int_vars:
239 value = '%d' % value
240 elif key in self.float_vars:
241 value = '%4.3f' % value
242 else:
243 raise ShowerCardError('Unknown key: %s = %s' % (key, value))
244 try:
245 line = '%s=%s' % (self.names_dict[key][self.shower].upper(), value.upper())
246 lines.append(line)
247 except KeyError:
248 pass
249
250 if self.testing:
251 return ('\n'.join(lines) + '\n')
252 else:
253 open(card_path, 'w').write(('\n'.join(lines) + '\n'))
254