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  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  ## END INCLUDE 
 55  ## 
 56   
 57  #1######################################################################### 
 58  ##                    START CODE 
 59  #1######################################################################### 
60 -class Param_card:
61 62 #2#########################################################################
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 #2#########################################################################
77 - def charge_card(self,name):
78 """ charge the card """ 79 self.source=name 80 self.info=MW_param.read_card(name)
81 82 83 84 #2#########################################################################
85 - def mod_content(self,line_content):
86 """ modify an entry in info """ 87 88 # line_content_old=list(line_content) 89 name_blok=line_content[1] 90 tag=line_content[0] 91 line_content=line_content[2:] 92 # treat special case 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 #line_content=line_content[1:] 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 #obj.update(prov) 111 112 #3 #########################################################################
113 - def mod_with_diff(self,name_blok,line_content):
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 #-1 because of the value 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 #3 #########################################################################
129 - def mod_with_fct(self,name_blok,line_content,level):
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]]' #select the blok name 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)+'\']' # the function start with ' 147 if int(param_nb)!=level: exec('param'+param_nb+'=float('+string+')') # find the current value of the parameter 148 else: exec('param'+param_nb+'=float('+line_content[-1]+')') 149 try: 150 exec('new_value='+fct[1:-1]) #supress the ' on the fct and find the correct value 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 #2#########################################################################
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 #create list of dictionary 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 #put in final data 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): #multiple definition of the same input 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 #2#########################################################################
185 - def write_card(self,name):
186 """write the param_card with name $name """ 187 188 if 1: 189 190 #try: 191 num=int(name[11:-4]) 192 self.create_info_line(num) 193 #except: 194 # pass 195 196 MW_param.go_to_main_dir() 197 ff=open('./Cards/'+name,'w') 198 if self.source: 199 #charge the comment from the original card 200 gg=open('./Cards/'+self.source,'r') 201 while 1: 202 line=gg.readline() 203 if line=='': 204 break 205 if line[0]=="#": 206 ff.writelines(line) 207 else: 208 break 209 gg.close() 210 211 decay=0 212 for key in self.info.keys(): 213 if key in ['decay','br']: 214 decay=1 215 continue 216 if key=="comment": 217 continue 218 text=self.create_blok_text(key) 219 ff.writelines(text) 220 221 if decay: 222 text=self.create_decay_text() 223 ff.writelines(text) 224 225 ff.close() 226 self.creating_card+=1
227 228 #3#########################################################################
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 #3#########################################################################
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 #3#########################################################################
262 - def create_decay_text(self):
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 #3#########################################################################
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 #3#########################################################################
299 - def create_br_line(self,obj,begin):
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 #2#########################################################################
318 - def create_set_card(self,name):
319 """ create all the card from schedular in file name """ 320 self.creating_card=1 #tag to know card under creation 321 self.wrong_generation=[] #can happen if fct is wrongly defined -> automatic desactivation 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 #3#########################################################################
358 - def update_event_dir(self):
359 360 #update event directory 361 self.file_mapping.close() 362 self.MWparam.def_actif_param() 363 import create_run as Create 364 create_obj=Create.create_dir(self.MWparam) 365 create_obj.update_card_status()
366 367 368 #3#########################################################################
369 - def create_info_line(self,nb_card):
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' #ActifTag 393 394 self.file_ParamInfo.writelines(line)
395 396 #3#########################################################################
397 - def define_mapping_file(self):
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 #3#########################################################################
426 - def define_mapping_file_for0gen(self,gap=0):
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 #3#########################################################################
436 - def define_mapping_file_for1gen(self,gap=0):
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 #3#########################################################################
465 - def define_mapping_file_for2gen(self,gap=0):
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 #3#########################################################################
492 - def create_change_tag(self,info):
493 """ create list of possible modification """ 494 495 output=[] 496 num=1 497 while info['mw_parameter'].has_key(str(10*num+1)): 498 content=[] 499 tag=str(10*num+1) 500 data=[num] 501 data.append(info['mw_parameter'][tag].lower()) 502 tag=str(10*num+2) 503 if type(info['mw_parameter'][tag])==list: 504 data+=info['mw_parameter'][tag] 505 else: 506 data.append(info['mw_parameter'][tag]) 507 tag=str(10*num+3) 508 if type(info['mw_parameter'][tag])==list: 509 for i in range(0,len(info['mw_parameter'][tag])): 510 content.append(data+[info['mw_parameter'][tag][i]]) 511 else: 512 content.append(data+[info['mw_parameter'][tag]]) 513 output.append(content) 514 num+=1 515 516 return output
517 518 519 #3#########################################################################
520 - def generated_uncorolated_card(self,param_list,num=1):
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 #3#########################################################################
545 - def generated_corolated_card(self,param_list):
546 """ create the card in a coralated way """ 547 548 # 1) check if all parameter have the same number of data: 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 # 2) pass in all case 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 #3#########################################################################
569 - def del_old_param(self):
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 #3#########################################################################
578 - def check_exist(self):
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