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

Source Code for Module madgraph.madweight.MW_info

  1  #!/usr/bin/env python 
  2  ########################################################################## 
  3  ##                                                                      ## 
  4  ##                               MadWeight                              ## 
  5  ##                               ---------                              ## 
  6  ########################################################################## 
  7  ##                                                                      ## 
  8  ##   author: Mattelaer Olivier (CP3)                                    ## 
  9  ##       email:  olivier.mattelaer@uclouvain.be                         ## 
 10  ##   author: Artoisenet Pierre (CP3)                                    ## 
 11  ##       email:  pierre.artoisenet@uclouvain.be                         ## 
 12  ##                                                                      ## 
 13  ########################################################################## 
 14  ##                                                                      ## 
 15  ##   license: GNU                                                       ## 
 16  ##   last-modif:10/06/08                                                ## 
 17  ##                                                                      ## 
 18  ########################################################################## 
 19  ##                                                                      ## 
 20  ##   Content                                                            ## 
 21  ##   -------                                                            ## 
 22  ##      +go_to_main_dir                                                 ## 
 23  ##      +read_card                                                      ## 
 24  ##      +check_for_help                                                 ## 
 25  ########################################################################## 
 26  ##      +Class P_info                                                   ## 
 27  ##      |     +  P_listdir                                              ## 
 28  ##      |     +  detectSubProcesses                                     ## 
 29  ########################################################################## 
 30  ##      +Class MW_info (dict,P_info)                                    ## 
 31  ##      |     +  init                                                   ## 
 32  ##      |     |    + init_run_opt                                       ## 
 33  ##      |     +  check_info                                             ## 
 34  ##      |     +  take_un_name                                           ## 
 35  ##      |     +  update_nb_card                                         ## 
 36  ##      |     +  number_of_P_run                                        ## 
 37  ##      |     +  load_events                                            ## 
 38  ##      |     +  give_block_param_info                                  ## 
 39  ##      |     |    +  CardNB_to_ParameterTag                            ## 
 40  ##      |     +  set_run_opt                                            ## 
 41  ##      |     |    + control_opt                                        ## 
 42  ##      |     +  def_actif_param                                        ## 
 43  ##      |     +  find_existing_events                                   ## 
 44  ########################################################################## 
 45  ##      +Class move                                                     ## 
 46  ##      |     +  init                                                   ## 
 47  ##      |     +  to_SubProcesses                                        ## 
 48  ##      |     +  to_init                                                ## 
 49  ##      |     +  store                                                  ## 
 50  ##      |     +  to_store                                               ## 
 51  ##      |     +  to_last_pos                                            ## 
 52  ##      |     +  to_main                                                ## 
 53  ##                                                                      ## 
 54  ########################################################################## 
 55  ## 
 56  ## BEGIN INCLUDE 
 57  ## 
 58  import sys 
 59  import os 
 60  import re 
 61  import stat 
 62  try:  
 63      import madgraph.madweight.Cards as Cards 
 64  except ImportError, error: 
 65      print error 
 66      import internal.madweight.Cards as Cards 
 67   
 68  ## 
 69  ## END INCLUDE 
 70  ## GLOBAL DEFINITION 
 71   
 72  num_to_tag={1:'param',2:'analyzer',3:'compilation',4:'event',5:'dir',6:'launch',7:'control',8:'collect',9:'plot',\ 
 73              -1:'relaunch',-2:'clean',-3:'refine',-4:'status'} 
 74  tag_to_num={'param':1,'analyzer':2,'compilation':3,'event':4,'dir':5,'launch':6,'control':7,'collect':8,'plot':9,\ 
 75              'relaunch':-1,'clean':-2,'refine':-3,'status':-4} 
 76  last_step=9 
 77  #1######################################################################### 
 78  ##                    START CODE 
 79  #1######################################################################### 
80 -def go_to_main_dir():
81 """ move to main position """ 82 pos=os.getcwd() 83 last=pos.split(os.sep)[-1] 84 if last=='bin': 85 os.chdir(os.pardir) 86 return 87 if last=='Python': 88 os.chdir(os.pardir+os.sep+os.pardir+os.sep+os.pardir) 89 return 90 91 list_dir=os.listdir('./') 92 if 'bin' in list_dir: 93 return 94 else: 95 sys.exit('Error: script must be executed from the main, bin or Python directory')
96 97 #1#########################################################################
98 -def read_card(name_card):
99 """put all card information in a dictionary""" 100 101 card=Cards.Card(name_card) 102 return card.info
103 104 #1#########################################################################
105 -def check_for_help(opt):
106 """ check if the user use the -h or -help option or simply invalid option """ 107 108 opt_pat=re.compile(r'''-?(?P<opt>\w*)[+-]?''',re.I) 109 help=0 110 authorized_opt=tag_to_num.keys()+['version'] 111 for i in range(0,len(num_to_tag)): 112 authorized_opt+=[str(i)] 113 for i in range(1,len(opt)): 114 if opt_pat.search(opt[i]): 115 if opt_pat.search(opt[i]).group('opt').lower() not in authorized_opt: 116 try: 117 int(opt_pat.search(opt[i]).group('opt').lower()) 118 except: 119 os.system('cat ./Source/MadWeight/Readme.txt') 120 sys.exit() 121 if opt_pat.search(opt[i]).group('opt').lower()=='version': 122 print 'MadWeight Version' 123 os.system('cat ./Source/MadWeight/MW_TemplateVersion.txt') 124 sys.exit()
125 126 #1#########################################################################
127 -class P_info:
128 """ class containing option/routine for standard MG/ME run """ 129 130 #2#########################################################################
131 - def P_lisdir(self):
132 try: 133 return self.Plistdir 134 except: 135 self.Plistdir,nothing=self.detect_SubProcess() 136 return self.Plistdir
137 138 #2#########################################################################
139 - def detect_SubProcess(self,P_mode=0):
140 141 142 P_SubProcess_list,MW_SubProcess_list=detect_SubProcess(P_mode) 143 return P_SubProcess_list,MW_SubProcess_list
144 145 146 147 #1#########################################################################
148 -class MW_info(dict,P_info):
149 """ class containing all the option/information from the run """ 150 151 #2#########################################################################
152 - def __init__(self,card_name):
153 """ init all the param for the run """ 154 155 self.mw_card=Cards.Card(card_name) 156 self.info=self.mw_card.info 157 for key,value in self.info.items(): 158 self[key]=value 159 160 dict.__init__(self.info) 161 self.check_info() 162 #assign special value 163 self.nb_event= self.info['mw_run']['nb_exp_events'] 164 self.nb_card=self.number_of_P_run() 165 try: 166 self.name = self.info['mw_run']['name'] 167 except: 168 self.name = self.take_run_name() 169 self.P_listdir,self.MW_listdir=self.detect_SubProcess() 170 self.nb_event_MW = {} 171 for MW in self.MW_listdir: 172 self.nb_event_MW[MW] = self.nb_event 173 self.init_run_opt() 174 self.def_actif_param() 175 self.Pinupdate=[] 176 self.Minpudate=[] 177 self.startevent=0
178 179 180 #3#########################################################################
181 - def init_run_opt(self,value=1):
182 """ init all the run scheduling paramater to value """ 183 self.run_opt={} 184 self.run_opt['param']=value 185 self.run_opt['analyzer']=value 186 self.run_opt['compilation']=value 187 self.run_opt['event']=value 188 self.run_opt['dir']=value 189 self.run_opt['launch']=value 190 self.run_opt['control']=value 191 self.run_opt['collect']=value 192 self.run_opt['plot']=value 193 self.run_opt['madweight_main']=value 194 self.run_opt['relaunch']=0 #only for bugging case... -> desactivate 195 self.run_opt['refine']=0 #only for bugging case... -> desactivate 196 self.run_opt['clean']=0 #dangerous... -> desactivate 197 self.run_opt['status']=0 # not in the mean stream 198 self.control_opt()
199 200 #2#########################################################################
201 - def check_info(self):
202 """ assign default value if not defined already and check the input type 203 those default value and the type are defined in MW_param_default.inc 204 structure of this file: 205 block tag type value #comment 206 additionaly check if the mw_parameter['?3'] is a list 207 """ 208 209 #define convertissor 210 def pass_in_integer(value): 211 return int(value)
212 213 def pass_in_logical(value): 214 if value in ['1','t','T','.true.',1,True]: 215 return 1 216 else: 217 return 0
218 219 def pass_in_float(value): 220 return float(value) 221 222 for line in open('./Source/MadWeight/Python/MW_param_default.inc'): 223 line=line.split('#')[0] #remove comment 224 splitline=line.split() #split the data 225 if len(splitline)!=4: 226 continue 227 #assign element 228 block=splitline[0].lower() 229 tag=splitline[1].lower() 230 type=splitline[2].lower() 231 value=splitline[3] 232 #check if exist -> default 233 try: 234 self[block][tag] 235 except: 236 if value.count('@@'): 237 value=value.split('@@') 238 value_2=self[value[0]][value[1]] 239 value=value_2 240 try: 241 self[block][tag]=value 242 except: 243 self[block]={tag:value} 244 #change type 245 if type in ['integer','logical','float']: 246 self[block][tag]=eval('pass_in_'+type+'(self[block][tag])') 247 self.check_mw_parameter_list_type() 248 249 #2#########################################################################
250 - def check_mw_parameter_list_type(self):
251 """ check that self['mw_parameter']['?3'] is a list """ 252 nb_param=1 253 254 while self['mw_parameter'].has_key(str(nb_param*10+3)): 255 if not isinstance(self['mw_parameter'][str(nb_param*10+3)], list): 256 self['mw_parameter'][str(nb_param*10+3)]=[self['mw_parameter'][str(nb_param*10+3)]] 257 nb_param+=1
258 259 260 #2#########################################################################
261 - def take_run_name(self):
262 """take the run name in run_card""" 263 name="run" 264 Pattern=re.compile(r'''\'(\S*)\'\s*=\s*run_tag''',re.I) 265 card=open("./Cards/run_card.dat") 266 267 while 1: 268 line=card.readline() 269 if line=='': 270 break 271 272 if Pattern.search(line): 273 name=Pattern.search(line).groups()[0] 274 break 275 return name
276 277 #2##########################################################################
278 - def update_nb_card(self):
279 "take the info from MW_runcard.dat" 280 self.nb_card=self.number_of_P_run() 281 self.def_actif_param()
282 283 #2##########################################################################
284 - def number_of_P_run(self):
285 "take the info from the existing card in Cards" 286 287 #check if we use different param_card.dat 288 # if self.info["mw_parameter"]["1"]=="1": 289 j=1 290 while 1: 291 if os.path.isfile('Cards/param_card_'+str(j)+'.dat'): j+=1 292 elif(j==1): return j 293 else: return j-1
294 295 296 297 #2##########################################################################
298 - def load_events(self):
299 "detect the number of events for P and MW run" 300 301 self.P_nevents=self.info['mw_run']['5'] 302 self.MW_nevents=self.info['mw_run']['6']
303 304 305 #2##########################################################################
306 - def give_block_param_info(self):
307 """ return the number of modified parameter and the number of different value for each""" 308 309 nb_block=0 310 nb_values=[] 311 k=0 312 while 1: 313 k+=1 314 try: 315 self.info['mw_parameter'][str(10*k+1)] 316 except: 317 break 318 nb_block+=1 319 if type(self.info['mw_parameter'][str(10*k+3)])==list: 320 nb_values.append(len(self.info['mw_parameter'][str(10*k+3)])) 321 else: 322 nb_values.append(1) 323 324 return nb_block,nb_values
325 326 #3########################################################################
327 - def CardNb_to_ParameterTag(self,num_card):
328 """ find from th card number, to which value for each block this card belong 329 num_card is the number of the card in the last generation. 330 card in previous generation are not accessible by this routine 331 (and are not related to this MadWeight card anyway) 332 """ 333 334 nb_block,nb_data_by_block=self.give_block_param_info() 335 336 if self['mw_parameter']['1']==2: 337 return [num_card-1]*len(nb_data_by_block) 338 339 tag=[] 340 for k in range(-1,-nb_block-1,-1): 341 tag.append((num_card-1)%nb_data_by_block[k]) 342 num_card=1+(num_card-(num_card-1)%nb_data_by_block[k])//nb_data_by_block[k] 343 tag.reverse() 344 return tag
345 346 #2##########################################################################
347 - def set_run_opt(self,option):
348 """analyze option for the run""" 349 350 if len(option)>1: 351 self.init_run_opt(0)#put everything to false 352 else: 353 return 354 for i in range(1,len(option)): 355 if option[i][0]=='-' and option[i][-1]=='+': 356 num=int(option[i][1:-1]) 357 for j in range(num,last_step+1): 358 self.run_opt[num_to_tag[j]]=1 359 elif option[i][0]=='-' and option[i][-1]=='-': 360 num=int(option[i][1:-1])+1 361 for j in range(1,num): 362 self.run_opt[num_to_tag[j]]=1 363 elif option[i][0]=='-': 364 num=int(option[i][1:]) 365 for i in option[i][1:]: 366 self.run_opt[num_to_tag[int(i)]]=1 367 elif option[i][-1]=='+': 368 num=tag_to_num[option[i][:-1]] 369 for j in range(num,last_step+1): 370 self.run_opt[num_to_tag[j]]=1 371 elif option[i][-1]=='-': 372 num=tag_to_num[option[i][:-1]]+1 373 for j in range(1,num): 374 self.run_opt[num_to_tag[j]]=1 375 elif '=' in option[i]: 376 obj=option[i].split('=') 377 tag=obj[0] 378 value=obj[1] 379 self.run_opt[tag]=value 380 else: 381 self.run_opt[option[i]]=1 382 383 self.control_opt()
384 385 #3##########################################################################
386 - def control_opt(self):
387 """analyze option for the run to have coherent input""" 388 389 390 if self.run_opt['refine']: 391 self.run_opt['relaunch']=1 392 393 #check value for 'madweight_main' 394 for i in range(3,9)+[-1,-3,-4]: 395 if self.run_opt[num_to_tag[i]]==1: 396 self.run_opt['madweight_main']=1 397 break 398 399 if self.run_opt['relaunch']==1: 400 self.run_opt['control']=1
401 402 #3##########################################################################
403 - def def_actif_param(self):
404 """ find which card are still actif """ 405 406 self.param_is_actif={} 407 try: 408 ff=open('Cards/mapping_card.dat') 409 except: 410 print 'no mapping card found' 411 for i in range(1,self.nb_card+1): 412 self.param_is_actif[i]=1 #if no file defined all card are supose to be used 413 self.actif_param=range(1,self.nb_card+1) 414 return 415 416 self.actif_param=[] 417 for line in ff: 418 split=line.split() 419 if len(split)<2: 420 continue 421 nb=int(split[0]) 422 actif=int(split[-1]) 423 self.param_is_actif[nb]=actif 424 if actif: 425 self.actif_param.append(nb) 426 427 if self.nb_card!=len(self.actif_param): 428 print 'wrong mapping file or some card are desactivated'
429 430 431 #3##########################################################################
432 - def find_existing_events(self,directory='',card_nb=''):
433 """ find how much event are already defined """ 434 435 if directory=='': 436 directory='./SubProcesses/'+self.MW_listdir[0]+'/'+self.name+'/' 437 if card_nb=='': 438 card_nb=self.actif_param[0] 439 if os.path.isdir(directory+'/card_'+str(card_nb)+'/'): 440 number=[int(eventdir.split('_')[-1]) for eventdir in os.listdir(directory+'/card_'+str(card_nb)+'/')] 441 else: 442 return 0 443 444 if len(number): 445 return max(number)+1 446 else: 447 return 0
448 449 #1 #################################################################################
450 -class move:
451 - def __init__(self):
452 self.initpos=os.getcwd() 453 self.old=self.initpos
454
455 - def to_SubProcesses(self):
456 457 old_pos=os.getcwd()+'/' 458 self.old=old_pos 459 if old_pos.count('/SubProcesses/')>1: 460 new_pos=pos.split('/SubProcesses/')[0]+'/SubProcesses/' 461 elif old_pos.count('/SubProcesses/')==0: 462 new_pos=old_pos+'/SubProcesses/' 463 else: 464 new_pos='/SubProcesses/'.join(old_pos.split('/SubProcesses/')[:-1])+'/SubProcesses/' 465 466 if new_pos!=old_pos: 467 self.old=old_pos 468 os.chdir(new_pos)
469
470 - def to_init(self):
471 self.old=os.getcwd() 472 os.chdir(self.initpos)
473
474 - def store(self):
475 self.store=os.getcwd()
476
477 - def to_store(self):
478 self.old=os.getcwd() 479 os.chdir(self.store)
480
481 - def to_last_pos(self):
482 pos,self.old=self.old,os.getcwd() 483 os.chdir(pos)
484
485 - def to_main(self):
486 old=os.getcwd() 487 try: 488 go_to_main_dir() 489 except: 490 self.to_SubProcesses() 491 os.chdir('..') 492 self.old=old
493 494
495 -def detect_SubProcess(P_mode=0):
496 MW_SubProcess_list=[] 497 if P_mode == 1: 498 print 'WARNING: automatic renormalization is not supported anymore' 499 500 list_dir=os.listdir("./SubProcesses/") 501 for name in list_dir: 502 try: 503 st = os.lstat(os.path.join("./SubProcesses/", name)) 504 except os.error: 505 continue 506 if stat.S_ISDIR(st.st_mode): 507 if name[0]=='P': 508 MW_SubProcess_list.append(name) 509 510 # Pierre: set MW_SubProcess_list to SubProcess_list, set SubProcess_list to [] 511 return [],MW_SubProcess_list
512