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