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