1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 try:
46 import madgraph.madweight.MW_info as MW_param
47 except ImportError:
48 import internal.madweight.MW_info as MW_param
49 import sys
50 import os
51 import re
52 import math
53
54
55
56
57
58
59
61
62
63 - def __init__(self,param_name=0,run_name=0):
64 if type(param_name)==str:
65 self.charge_card(param_name)
66 elif type(param_name)==dict:
67 self.source=1
68 self.info=param_name
69 else:
70 self.source=0
71
72 if run_name:
73 print 'starting the creation of the param_card'
74 self.create_set_card(run_name)
75
76
81
82
83
84
85 - def mod_content(self,line_content):
86 """ modify an entry in info """
87
88
89 name_blok=line_content[1]
90 tag=line_content[0]
91 line_content=line_content[2:]
92
93 if '_' in name_blok:
94 special_tag,name_blok=name_blok.split('_')
95 if special_tag=='diff':
96 line_content=self.mod_with_diff(name_blok,line_content)
97 elif special_tag=='fct':
98 line_content=self.mod_with_fct(name_blok,line_content,tag)
99
100
101 obj=self.info[name_blok]
102 for i in range(0,len(line_content)-1):
103 if line_content[i] not in obj.keys():
104 self.add_content([name_blok]+line_content)
105 return
106 if i!=len(line_content)-2:
107 obj=obj[line_content[i]]
108 else:
109 obj[line_content[i]]=line_content[i+1]
110
111
112
114 """ modify the line content to fix the parameter in fixing the mass differences
115 entry line_content doesn't content the name blok information
116
117 You should prefer to use mod_with_fct (more general)
118 """
119 diff=line_content[-1]
120 number_of_tag=(len(line_content)-1)//2
121 start_value=self.info[name_blok]
122 for i in range(0,number_of_tag):
123 start_value=prec_value[line_content[number_of_tag+i]]
124 new_value=str(float(start_value)+float(diff))
125
126 return [line_content[i] for i in range(0,number_of_tag)]+[new_value]
127
128
130 """ modify the line content to fix the parameter in fixing the mass differences
131 entry line_content doesn't content the name blok information
132 """
133
134 pat=re.compile(r'''\$(\d+)3''')
135 fct=line_content[-2]
136 values=pat.findall(fct)
137
138 for param_nb in values:
139 fct=fct.replace('$'+param_nb+'3','param'+param_nb)
140 string0='self.info[self.MWparam[\'mw_parameter\'][\''+str(10*int(param_nb)+1)+'\'].split(\'_\')[-1]]'
141 string=string0
142 if type(self.MWparam['mw_parameter'][str(10*int(param_nb)+2)])==str:
143 string+='[\''+str(self.MWparam['mw_parameter'][str(10*int(param_nb)+2)])+'\']'
144 else:
145 for id in self.MWparam['mw_parameter'][str(10*int(param_nb)+2)]:
146 if id[0] not in['\'','\"']: string+='[\''+str(id)+'\']'
147 if int(param_nb)!=level: exec('param'+param_nb+'=float('+string+')')
148 else: exec('param'+param_nb+'=float('+line_content[-1]+')')
149 try:
150 exec('new_value='+fct[1:-1])
151 except:
152 print 'WARNING: fct undefined for card ',self.creating_card,'. This card will be desactivated'
153 self.wrong_generation.append(self.creating_card)
154 new_value=-1
155
156 return [line_content[i] for i in range(0,len(line_content)-2)]+[str(new_value)]
157
158
159 - def add_content(self,line_content):
160 """ add new content in info """
161
162 name_block=line_content[0]
163 line_content=line_content[1:]
164
165 obj=line_content[-1]
166 for i in range(-2,-len(line_content)-1,-1):
167 obj={line_content[i]:obj}
168
169
170
171 dico=self.info[name_block]
172 for i in range(0,len(line_content)-1):
173 if line_content[i] not in dico.keys():
174 dico[line_content[i]]=obj[line_content[i]]
175 break
176 elif i!=len(line_content)-2:
177 dico=dico[line_content[i]]
178 obj=obj[line_content[i]]
179 elif(type(dico[line_content[i]])==list):
180 dico[line_content[i]].append(obj[line_content[i]])
181 else:
182 dico[line_content[i]]=[dico[line_content[i]],line_content[i+1]]
183
184
227
228
229 - def create_blok_text(self,blok_name):
230 """write the param_card with name $name """
231
232 text='Block '+blok_name.upper()+' '+self.info['comment'][blok_name]+'\n'
233 prop_text=self.create_line_text(self.info[blok_name])
234 if prop_text.count('$$'):
235 print 'multiple inputs are not supported yet'
236 print 'you must create your Cards by hand'
237 sys.exit()
238
239 return text+prop_text
240
241
242 - def create_line_text(self,obj,key_tag=""):
243
244 text=''
245 if type(obj)==dict:
246 for key in obj.keys():
247 text+=self.create_line_text(obj[key],key_tag+' '+key)
248
249 elif type(obj)==str:
250 text=key_tag+' '+obj+'\n'
251 elif type(obj)==list:
252 text=' $$ '
253 for data in obj:
254 text+=data+' $ '
255 text+='$\n'
256
257 return text
258
259
260
261
263 """write the param_card with name $name """
264
265 decay=self.info['decay']
266 try:
267 br=self.info['br']
268 except:
269 br=0
270 text=''
271 for key in decay.keys():
272 text+='DECAY '+key+' '
273 text+=self.create_line_text(decay[key])
274 if br:
275 if key in br.keys():
276 text+=self.create_br_text(br[key])
277
278
279 return text
280
281
282 - def create_br_text(self,obj):
283 """write the param_card with name $name """
284
285 text=''
286 space=' '
287 list_data=[]
288 for key in obj.keys():
289 list_data+=self.create_br_line(obj[key],[key])
290
291 for data in list_data:
292 text+=space+str(data[-1])+space+str(len(data)-1)
293 for i in range(0,len(data)-1):
294 text+=space+data[i]
295 text+='\n'
296 return text
297
298
300 """write the param_card with name $name """
301
302
303 content=[]
304 if type(obj)==dict:
305 for key in obj.keys():
306 content_i=[key]
307 content_i=self.create_br_line(obj[key],begin+[key])
308 if type(content_i[0])==str:
309 content.append(content_i)
310 else:
311 content+=content_i
312 elif type(obj)==str:
313 return begin+[obj]
314
315 return content
316
317
319 """ create all the card from schedular in file name """
320 self.creating_card=1
321 self.wrong_generation=[]
322
323 if type(name)==str:
324 self.MWparam=MW_param.read_card(name)
325 else:
326 self.MWparam=name
327
328 if self.MWparam['mw_parameter']['2']:
329 self.file_ParamInfo=open('./Cards/info_card.dat','a')
330 else:
331 print 'define new mapping file'
332 self.file_ParamInfo=open('./Cards/info_card.dat','w')
333
334 param_list=self.create_change_tag(self.MWparam)
335
336 if not self.source:
337 self.charge_card('param_card.dat')
338 if self.MWparam['mw_parameter']['1'] == 0:
339 self.check_exist()
340 self.define_mapping_file()
341 return
342 elif self.MWparam['mw_parameter']['1'] == 1:
343 self.del_old_param()
344 num=self.generated_uncorolated_card(param_list)
345 elif self.MWparam['mw_parameter']['1'] == 2:
346 self.del_old_param()
347 num=self.generated_corolated_card(param_list)
348
349 self.define_mapping_file()
350 print 'we have created ',num-1,' param_card\'s'
351 if self.wrong_generation:
352 print 'but ',len(self.wrong_generation),' are desactivated'
353
354 if self.MWparam['mw_parameter']['2']:
355 self.update_event_dir()
356
357
366
367
368
370 """ create the file containing the mapping between the card number and the parameter
371 syntax:
372 card_nb param1 param2 ... paramX valid
373 """
374
375 nb_param=1
376 line=str(nb_card)+'\t'
377 while self.MWparam['mw_parameter'].has_key(str(nb_param*10+1)):
378 tag1=self.MWparam['mw_parameter'][str(nb_param*10+1)]
379 tag2=self.MWparam['mw_parameter'][str(nb_param*10+2)]
380 if 'fct_' in tag1:
381 tag1=tag1[4:]
382 tag2=tag2[:-1]
383
384 value=self.info[tag1.lower()]
385 if type(tag2)==str:
386 value=value[tag2.lower()]
387 else:
388 for param in tag2:
389 value=value[param.lower()]
390 line+=value+'\t'
391 nb_param+=1
392 line+=' \n'
393
394 self.file_ParamInfo.writelines(line)
395
396
398 """ create the file containing the mapping between the card number and the parameter
399 syntax:
400 card_nb param1 param2 ... paramX valid
401 """
402
403 if self.MWparam['mw_parameter']['2']:
404 print 'add card in mapping file'
405 gap=self.MWparam.nb_card
406 self.file_mapping=open('./Cards/mapping_card.dat','a')
407 self.file_ParamInfo=open('./Cards/info_card.dat','a')
408 else:
409 print 'define new mapping file'
410 gap=0
411 self.file_mapping=open('./Cards/mapping_card.dat','w')
412 self.file_ParamInfo=open('./Cards/info_card.dat','w')
413
414
415 if self.MWparam['mw_parameter']['1']==0:
416 self.define_mapping_file_for0gen(gap)
417 elif self.MWparam['mw_parameter']['1']==1:
418 self.define_mapping_file_for1gen(gap)
419 elif self.MWparam['mw_parameter']['1']==2:
420 self.define_mapping_file_for2gen(gap)
421
422
423
424
425
427 """ create the file containing the mapping between the card number and the parameter
428 syntax:
429 card_nb param1 param2 ... paramX valid
430 """
431
432 for i in range(1,self.MWparam.nb_card+1):
433 self.file_mapping.writelines(str(i)+'\t1 \n')
434
435
437 """ create the file containing the mapping between the card number and the parameter
438 syntax:
439 card_nb param1 param2 ... paramX valid
440 """
441
442 start=1+gap
443 nb_new_card=1
444 nb_param=1
445 while self.MWparam.info['mw_parameter'].has_key(str(nb_param*10+1)):
446 nb_new_card*=len(self.MWparam['mw_parameter'][str(nb_param*10+3)])
447 nb_param+=1
448
449 for card in range(start,start+nb_new_card):
450 line=str(card)+'\t'
451 param_pos=self.MWparam.CardNb_to_ParameterTag(card)
452 for param in range(1,nb_param):
453 if type(self.MWparam['mw_parameter'][str(param*10+3)])==list:
454 line+=self.MWparam.info['mw_parameter'][str(param*10+3)][param_pos[param-1]]+'\t'
455 else:
456 line+=self.MWparam.info['mw_parameter'][str(param*10+3)]+'\t'
457 if card in self.wrong_generation:
458 line+='0 \n'
459 else:
460 line+='1 \n'
461
462 self.file_mapping.writelines(line)
463
464
466 """ create the file containing the mapping between the card number and the parameter
467 syntax:
468 card_nb param1 param2 ... paramX valid
469 """
470
471 start=1+gap
472 nb_new_card=1
473 nb_param=1
474
475 nb_block,nb_data_by_block=self.MWparam.give_block_param_info()
476 for card in range(start,start+nb_data_by_block[0]):
477 line=str(card)+'\t'
478 for param in range(1,nb_block+1):
479 if type(self.MWparam['mw_parameter'][str(param*10+3)])==list:
480 line+=self.MWparam.info['mw_parameter'][str(param*10+3)][card-start]+'\t'
481 else:
482 line+=self.MWparam.info['mw_parameter'][str(param*10+3)]
483
484 if card in self.wrong_generation:
485 line+='0 \n'
486 else:
487 line+='1 \n'
488
489 self.file_mapping.writelines(line)
490
491
517
518
519
521 """ create the card in a uncoralated way """
522
523 if self.MWparam['mw_parameter']['2']:
524 gap=self.MWparam.nb_card
525 self.creating_card+=gap
526 else:
527 gap=0
528
529
530 new_list=param_list[1:]
531 for data in param_list[0]:
532 self.mod_content(data)
533 if new_list:
534 num=self.generated_uncorolated_card(new_list,num)
535 else:
536 self.write_card('param_card_'+str(num+gap)+'.dat')
537 num=num+1
538 return num
539
540
541
542
543
544
546 """ create the card in a coralated way """
547
548
549 for i in range(0,len(param_list)-1):
550 if len(param_list[i])!=len(param_list[i+1]):
551 print """ERROR: all parameters don't have the same number of entries"""
552 sys.exit()
553
554
555 if self.MWparam['mw_parameter']['2']:
556 gap=1+self.MWparam.nb_card
557 self.creating_card+=gap
558 else:
559 gap=1
560
561 for i in range(0,len(param_list[0])):
562 for j in range(0,len(param_list)):
563 self.mod_content(param_list[j][i])
564 self.write_card('param_card_'+str(i+gap)+'.dat')
565
566 return len(param_list[0])+1
567
568
570 """ supress all the all param_card """
571
572 if not self.MWparam['mw_parameter']['2']:
573 os.system('rm ./Cards/param_card_?.dat &>/dev/null')
574 os.system('rm ./Cards/param_card_??.dat &>/dev/null')
575 os.system('rm ./Cards/param_card_???.dat &>/dev/null')
576
577
579 """ check if param_card_1 exist and copy param_card if not """
580
581
582 try:
583 ff=open('Cards/param_card_1.dat','r')
584 ff.close()
585 os.system('ln -s param_card_1.dat Cards/param_card.dat')
586 except:
587 os.system('cp ./Cards/param_card.dat ./Cards/param_card_1.dat')
588 os.system('ln -s param_card_1.dat Cards/param_card.dat')
589