Package madgraph :: Package madweight :: Module create_param
[hide private]
[frames] | no frames]

Source Code for Module madgraph.madweight.create_param

  1  #!/usr/bin/env python 
  2  ########################################################################## 
  3  ##                                                                      ## 
  4  ##                               MadWeight                              ## 
  5  ##                               ---------                              ## 
  6  ########################################################################## 
  7  ##                                                                      ## 
  8  ##   author: Mattelaer Olivier (CP3)                                    ## 
  9  ##   email:  olivier.mattelaer@uclouvain.be                             ## 
 10  ##                                                                      ## 
 11  ########################################################################## 
 12  ##                                                                      ## 
 13  ##   license: GNU                                                       ## 
 14  ##   last-modif:07/01/09                                                ## 
 15  ##                                                                      ## 
 16  ########################################################################## 
 17  ##                                                                      ## 
 18  ##   Content                                                            ## 
 19  ##   -------                                                            ## 
 20  ##      +Class Param_card                                               ## 
 21  ##      |     +  init                                                   ## 
 22  ##      |     +  charge_card                                            ## 
 23  ##      |     +  create_map_file                                        ## 
 24  ##      |     +  mod_content                                            ## 
 25  ##      |     |    -  mod_with_diff                                     ## 
 26  ##      |     |    -  mod_with_fct                                      ##   
 27  ##      |     +  add_content                                            ## 
 28  ##      |     +  write_card                                             ## 
 29  ##      |     |    -  create_blok_text                                  ## 
 30  ##      |     |    -  create_line_text                                  ## 
 31  ##      |     |    -  create_decay_text                                 ## 
 32  ##      |     |    -  create_br_text                                    ## 
 33  ##      |     |_   -  create_br_line                                    ## 
 34  ##      |     +  create_set_card                                        ## 
 35  ##      |     |    -  create_change_tag                                 ## 
 36  ##      |     |    -  generated_uncorolated_card                        ## 
 37  ##      |     |    -  generated_corolated_card                          ## 
 38  ##      |     |    -  del_old_param                                     ## 
 39  ##      |_    |_   -  check_exist                                       ## 
 40  ##                                                                      ## 
 41  ########################################################################## 
 42  ## 
 43  ## BEGIN INCLUDE 
 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  ## END INCLUDE 
 58  ## 
 59   
 60  #1######################################################################### 
 61  ##                    START CODE 
 62  #1######################################################################### 
63 -class Param_card:
64 65 #2#########################################################################
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 #2#########################################################################
80 - def charge_card(self,name):
81 """ charge the card """ 82 self.source=name 83 self.info=MW_param.read_card(name)
84 85 86 87 #2#########################################################################
88 - def mod_content(self,line_content):
89 """ modify an entry in info """ 90 91 # line_content_old=list(line_content) 92 name_blok=line_content[1] 93 tag=line_content[0] 94 line_content=line_content[2:] 95 # treat special case 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 #line_content=line_content[1:] 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 #obj.update(prov) 114 115 #3 #########################################################################
116 - def mod_with_diff(self,name_blok,line_content):
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 #-1 because of the value 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 #3 #########################################################################
132 - def mod_with_fct(self,name_blok,line_content,level):
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]]' #select the blok name 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)+'\']' # the function start with ' 150 if int(param_nb)!=level: exec('param'+param_nb+'=float('+string+')') # find the current value of the parameter 151 else: exec('param'+param_nb+'=float('+line_content[-1]+')') 152 try: 153 exec('new_value='+fct[1:-1]) #supress the ' on the fct and find the correct value 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 #2#########################################################################
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 #create list of dictionary 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 #put in final data 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): #multiple definition of the same input 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 #2#########################################################################
188 - def write_card(self,name):
189 """write the param_card with name $name """ 190 191 if 1: 192 193 #try: 194 num=int(name[11:-4]) 195 self.create_info_line(num) 196 #except: 197 # pass 198 199 MW_param.go_to_main_dir() 200 ff=open('./Cards/'+name,'w') 201 if self.source: 202 #charge the comment from the original card 203 gg=open('./Cards/'+self.source,'r') 204 while 1: 205 line=gg.readline() 206 if line=='': 207 break 208 if line[0]=="#": 209 ff.writelines(line) 210 else: 211 break 212 gg.close() 213 214 decay=0 215 for key in self.info.keys(): 216 if key in ['decay','br']: 217 decay=1 218 continue 219 if key=="comment": 220 continue 221 text=self.create_blok_text(key) 222 ff.writelines(text) 223 224 if decay: 225 text=self.create_decay_text() 226 ff.writelines(text) 227 228 ff.close() 229 self.creating_card+=1
230 231 #3#########################################################################
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 #3#########################################################################
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 #3#########################################################################
265 - def create_decay_text(self):
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 #3#########################################################################
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 #3#########################################################################
302 - def create_br_line(self,obj,begin):
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 #2#########################################################################
321 - def create_set_card(self,name):
322 """ create all the card from schedular in file name """ 323 self.creating_card=1 #tag to know card under creation 324 self.wrong_generation=[] #can happen if fct is wrongly defined -> automatic desactivation 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 #3#########################################################################
361 - def update_event_dir(self):
362 363 #update event directory 364 self.file_mapping.close() 365 self.MWparam.def_actif_param() 366 from . import create_run as Create 367 create_obj=Create.create_dir(self.MWparam) 368 create_obj.update_card_status()
369 370 371 #3#########################################################################
372 - def create_info_line(self,nb_card):
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' #ActifTag 396 397 self.file_ParamInfo.writelines(line)
398 399 #3#########################################################################
400 - def define_mapping_file(self):
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 #3#########################################################################
429 - def define_mapping_file_for0gen(self,gap=0):
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 #3#########################################################################
439 - def define_mapping_file_for1gen(self,gap=0):
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 #3#########################################################################
468 - def define_mapping_file_for2gen(self,gap=0):
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 #3#########################################################################
495 - def create_change_tag(self,info):
496 """ create list of possible modification """ 497 498 output=[] 499 num=1 500 while str(10*num+1) in info['mw_parameter']: 501 content=[] 502 tag=str(10*num+1) 503 data=[num] 504 data.append(info['mw_parameter'][tag].lower()) 505 tag=str(10*num+2) 506 if type(info['mw_parameter'][tag])==list: 507 data+=info['mw_parameter'][tag] 508 else: 509 data.append(info['mw_parameter'][tag]) 510 tag=str(10*num+3) 511 if type(info['mw_parameter'][tag])==list: 512 for i in range(0,len(info['mw_parameter'][tag])): 513 content.append(data+[info['mw_parameter'][tag][i]]) 514 else: 515 content.append(data+[info['mw_parameter'][tag]]) 516 output.append(content) 517 num+=1 518 519 return output
520 521 522 #3#########################################################################
523 - def generated_uncorolated_card(self,param_list,num=1):
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 #3#########################################################################
548 - def generated_corolated_card(self,param_list):
549 """ create the card in a coralated way """ 550 551 # 1) check if all parameter have the same number of data: 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 # 2) pass in all case 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 #3#########################################################################
572 - def del_old_param(self):
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 #3#########################################################################
581 - def check_exist(self):
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