Package madgraph :: Package interface :: Module launch_ext_program
[hide private]
[frames] | no frames]

Source Code for Module madgraph.interface.launch_ext_program

  1  ################################################################################ 
  2  # 
  3  # Copyright (c) 2009 The MadGraph5_aMC@NLO Development team and Contributors 
  4  # 
  5  # This file is a part of the MadGraph5_aMC@NLO project, an application which  
  6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
  7  # high-energy processes in the Standard Model and beyond. 
  8  # 
  9  # It is subject to the MadGraph5_aMC@NLO license which should accompany this  
 10  # distribution. 
 11  # 
 12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
 13  # 
 14  ################################################################################ 
 15   
 16  import glob 
 17  import logging 
 18  import os 
 19  import pydoc 
 20  import re 
 21  import sys 
 22  import subprocess 
 23  import thread 
 24  import time 
 25   
 26  import madgraph.iolibs.files as files 
 27  import madgraph.interface.extended_cmd as cmd 
 28  import madgraph.interface.madevent_interface as me_cmd 
 29  import madgraph.various.misc as misc 
 30  import madgraph.various.process_checks as process_checks 
 31  import madgraph.various.banner as banner_mod 
 32   
 33  from madgraph import MG4DIR, MG5DIR, MadGraph5Error 
 34  from madgraph.iolibs.files import cp 
 35   
 36   
 37   
 38  logger = logging.getLogger('cmdprint.ext_program') 
 39   
40 -class ExtLauncher(object):
41 """ Generic Class for executing external program """ 42 43 program_dir = '' 44 executable = '' # path from program_dir 45 46 force = False 47
48 - def __init__(self, cmd, running_dir, card_dir='', **options):
49 """ initialize an object """ 50 51 self.running_dir = running_dir 52 self.card_dir = os.path.join(self.running_dir, card_dir) 53 self.cmd_int = cmd 54 #include/overwrite options 55 for key,value in options.items(): 56 setattr(self, key, value) 57 58 self.cards = [] # files can be modified (path from self.card_dir)
59
60 - def run(self):
61 """ execute the main code """ 62 63 self.prepare_run() 64 for card in self.cards: 65 self.treat_input_file(card, default = 'n') 66 67 self.launch_program()
68 69
70 - def prepare_run(self):
71 """ aditional way to prepare the run""" 72 pass
73
74 - def launch_program(self):
75 """launch the main program""" 76 subprocess.call([self.executable], cwd=self.running_dir)
77
78 - def edit_file(self, path):
79 """edit a file""" 80 81 path = os.path.realpath(path) 82 open_file(path)
83 84 85 # Treat Nicely the timeout
86 - def timeout_fct(self,timeout):
87 if timeout: 88 # avoid to always wait a given time for the next answer 89 self.force = True
90
91 - def ask(self, question, default, choices=[], path_msg=None):
92 """nice handling of question""" 93 94 if not self.force: 95 return self.cmd_int.ask(question, default, choices=choices, 96 path_msg=path_msg, fct_timeout=self.timeout_fct) 97 else: 98 return str(default)
99 100
101 - def treat_input_file(self, filename, default=None, msg=''):
102 """ask to edit a file""" 103 104 if msg == '' and filename == 'param_card.dat': 105 msg = \ 106 "WARNING: If you edit this file don\'t forget to consistently "+\ 107 "modify the different parameters,\n especially the width of all "+\ 108 "particles." 109 110 if not self.force: 111 if msg: print msg 112 question = 'Do you want to edit file: %(card)s?' % {'card':filename} 113 choices = ['y', 'n'] 114 path_info = 'path of the new %(card)s' % {'card':os.path.basename(filename)} 115 ans = self.ask(question, default, choices, path_info) 116 else: 117 ans = default 118 119 if ans == 'y': 120 path = os.path.join(self.card_dir, filename) 121 self.edit_file(path) 122 elif ans == 'n': 123 return 124 else: 125 path = os.path.join(self.card_dir, filename) 126 files.cp(ans, path)
127
128 -class MadLoopLauncher(ExtLauncher):
129 """ A class to launch a simple Standalone test """ 130
131 - def __init__(self, cmd_int, running_dir, **options):
132 """ initialize the StandAlone Version """ 133 134 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options) 135 self.cards = ['param_card.dat','MadLoopParams.dat']
136
137 - def prepare_run(self):
138 """ Possible preparatory actions to take.""" 139 pass
140
141 - def treat_input_file(self, filename, default=None, msg='', dir_path=None):
142 """ask to edit a file""" 143 144 if filename == 'PS.input': 145 if not self.force: 146 if msg!='': print msg 147 question = 'Do you want to specify the Phase-Space point: %(card)s?' % {'card':filename} 148 choices = ['y', 'n'] 149 path_info = 'path of the PS.input file' 150 ans = self.ask(question, default, choices, path_info) 151 else: 152 ans = default 153 if ans == 'y': 154 if not os.path.isfile(os.path.join(dir_path,'PS.input')): 155 PSfile = open(os.path.join(dir_path,'PS.input'), 'w') 156 if not os.path.isfile(os.path.join(dir_path,'result.dat')): 157 PSfile.write('\n'.join([' '.join(['%.16E'%0.0 for \ 158 pi in range(4)]) for pmom in range(1)])) 159 else: 160 default_ps = process_checks.LoopMatrixElementEvaluator.\ 161 parse_check_output(file(os.path.join(dir_path,\ 162 'result.dat')),format='dict')['res_p'] 163 PSfile.write('\n'.join([' '.join(['%.16E'%pi for pi \ 164 in pmom]) for pmom in default_ps])) 165 PSfile.write("\n\nEach line number 'i' like the above one sets"+\ 166 " the momentum of particle number i, \nordered like in"+\ 167 " the process definition. The format is (E,px,py,pz).") 168 PSfile.close() 169 self.edit_file(os.path.join(dir_path,'PS.input')) 170 else: 171 super(MadLoopLauncher,self).treat_input_file(filename,default,msg) 172 if filename == 'MadLoopParams.dat': 173 # Make sure to update the changes 174 MadLoopparam = banner_mod.MadLoopParam( 175 os.path.join(self.card_dir, 'MadLoopParams.dat')) 176 # Unless user asked for it, don't doublecheck the helicity filter. 177 MadLoopparam.set('DoubleCheckHelicityFilter', False, 178 ifnotdefault=False) 179 MadLoopparam.write(os.path.join(self.card_dir,os.path.pardir, 180 'SubProcesses', 'MadLoopParams.dat'))
181
182 - def launch_program(self):
183 """launch the main program""" 184 evaluator = process_checks.LoopMatrixElementTimer 185 sub_path = os.path.join(self.running_dir, 'SubProcesses') 186 for path in os.listdir(sub_path): 187 if path.startswith('P') and \ 188 os.path.isdir(os.path.join(sub_path, path)): 189 shell_name = path.split('_')[1]+' > '+path.split('_')[2] 190 curr_path = os.path.join(sub_path, path) 191 infos = {} 192 logger.info("Initializing process %s."%shell_name) 193 nps = me_cmd.MadLoopInitializer.run_initialization( 194 curr_path, sub_path, infos, 195 req_files = ['HelFilter.dat','LoopFilter.dat']) 196 if nps == None: 197 raise MadGraph5Error,"MadLoop could not initialize the process %s"\ 198 %shell_name 199 logger.debug(("MadLoop initialization performed for %s"+\ 200 " using %d PS points (%s)")\ 201 %(shell_name,abs(nps),\ 202 'double precision' if nps>0 else 'quadruple precision')) 203 # Ask if the user wants to edit the PS point. 204 self.treat_input_file('PS.input', default='n', 205 msg='Phase-space point for process %s.'%shell_name,\ 206 dir_path=curr_path) 207 # We use mu_r=-1.0 to use the one defined by the user in the 208 # param_car.dat 209 me_cmd.MadLoopInitializer.fix_PSPoint_in_check(sub_path, 210 read_ps = os.path.isfile(os.path.join(curr_path, 'PS.input')), 211 npoints = 1, mu_r=-1.0) 212 # check 213 t1, t2, ram_usage = me_cmd.MadLoopInitializer.make_and_run(curr_path) 214 if t1==None or t2==None: 215 raise MadGraph5Error,"Error while running process %s."\ 216 %shell_name 217 try: 218 rFile=open(os.path.join(curr_path,'result.dat'), 'r') 219 except IOError: 220 rFile.close() 221 raise MadGraph5Error,"Could not find result file %s."%\ 222 str(os.path.join(curr_path,'result.dat')) 223 # The result are returned as a dictionary. 224 result = evaluator.parse_check_output(rFile,format='dict') 225 for line in self.format_res_string(result, shell_name): 226 if isinstance(line, str): 227 logger.info(line) 228 elif isinstance(line,tuple): 229 logger.info(line[0],line[1])
230
231 - def format_res_string(self, res, shell_name):
232 """ Returns a good-looking string presenting the results. 233 The argument the tuple ((fin,born,spole,dpole,me_pow), p_out).""" 234 235 main_color='$MG:color:BLUE' 236 237 def special_float_format(float): 238 return '%s%.16e'%('' if float<0.0 else ' ',float)
239 240 so_order_names = res['Split_Orders_Names'] 241 242 def format_so_orders(so_orders): 243 return ' '.join(['%s=%d'%(so_order_names[i],so_orders[i]) for i in 244 range(len(so_orders))])
245 246 ASCII_bar = ('|'+''.join(['='*96]),main_color) 247 248 ret_code_h = res['return_code']//100 249 ret_code_t = (res['return_code']-100*ret_code_h)//10 250 ret_code_u = res['return_code']%10 251 StabilityOutput=[] 252 if ret_code_h==1: 253 if ret_code_t==3 or ret_code_t==4: 254 StabilityOutput.append('| Unknown numerical stability because '+\ 255 'MadLoop is in the initialization stage.') 256 else: 257 StabilityOutput.append('| Unknown numerical stability, check '+\ 258 'CTRunMode value in MadLoopParams.dat.') 259 elif ret_code_h==2: 260 StabilityOutput.append('| Stable kinematic configuration (SPS).') 261 elif ret_code_h==3: 262 StabilityOutput.append('| Unstable kinematic configuration (UPS).') 263 StabilityOutput.append('| Quadruple precision rescue successful.') 264 elif ret_code_h==4: 265 StabilityOutput.append('| Exceptional kinematic configuration (EPS).') 266 StabilityOutput.append('| Both double and quadruple precision'+\ 267 ' computations are unstable.') 268 269 if ret_code_t==2 or ret_code_t==4: 270 StabilityOutput.append('| Quadruple precision was used for this'+\ 271 'computation.') 272 if ret_code_h!=1: 273 if res['accuracy']>0.0: 274 StabilityOutput.append('| Estimated relative accuracy = %.1e'\ 275 %res['accuracy']) 276 elif res['accuracy']==0.0: 277 StabilityOutput.append('| Estimated relative accuracy = %.1e'\ 278 %res['accuracy']+' (i.e. beyond double precision)') 279 else: 280 StabilityOutput.append('| Estimated accuracy could not be '+\ 281 'computed for an unknown reason.') 282 283 PS_point_spec = ['|| Phase-Space point specification (E,px,py,pz)','|'] 284 PS_point_spec.append('\n'.join(['| '+' '.join(['%s'%\ 285 special_float_format(pi) for pi in pmom]) for pmom in res['res_p']])) 286 PS_point_spec.append('|') 287 288 str_lines=[] 289 290 notZeroBorn=True 291 if res['export_format']!='LoopInduced' and len(so_order_names) and \ 292 len([1 for k in res['Born_kept'] if k])==0: 293 notZeroBorn = False 294 str_lines.append( 295 ("| /!\\ There is no Born contribution for the squared orders specified in "+ 296 "the process definition/!\\",'$MG:color:RED')) 297 298 if res['export_format']=='Default' and notZeroBorn: 299 str_lines.extend(['\n',ASCII_bar, 300 ('|| Results for process %s'%shell_name,main_color), 301 ASCII_bar]+PS_point_spec+StabilityOutput+[ 302 '|', 303 ('|| Total(*) Born contribution (GeV^%d):'%res['gev_pow'],main_color), 304 ('| Born = %s'%special_float_format(res['born']),main_color), 305 ('|| Total(*) virtual contribution normalized with born*alpha_S/(2*pi):',main_color), 306 ('| Finite = %s'%special_float_format(res['finite']),main_color), 307 ('| Single pole = %s'%special_float_format(res['1eps']),main_color), 308 ('| Double pole = %s'%special_float_format(res['2eps']),main_color)]) 309 elif res['export_format']=='LoopInduced' and notZeroBorn: 310 str_lines.extend(['\n',ASCII_bar, 311 ('|| Results for process %s (Loop-induced)'%shell_name,main_color), 312 ASCII_bar]+PS_point_spec+StabilityOutput+[ 313 '|', 314 ('|| Loop amplitude squared, must be finite:',main_color), 315 ('| Finite = %s'%special_float_format(res['finite']),main_color), 316 '|(| Pole residues, indicated only for checking purposes: )', 317 '|( Single pole = %s )'%special_float_format(res['1eps']), 318 '|( Double pole = %s )'%special_float_format(res['2eps'])]) 319 320 if (len(res['Born_SO_Results'])+len(res['Born_SO_Results']))>0: 321 if notZeroBorn: 322 str_lines.append( 323 ("| (*) The results above sum all starred contributions below",main_color)) 324 325 str_lines.append('|') 326 if not notZeroBorn: 327 str_lines.append( 328 ("| The Born contributions below are computed but do not match these squared "+ 329 "orders constraints",main_color)) 330 331 if len(res['Born_SO_Results'])==1: 332 str_lines.append('|| All Born contributions are of split orders *(%s)'\ 333 %format_so_orders(res['Born_SO_Results'][0][0])) 334 elif len(res['Born_SO_Results'])>1: 335 for i, bso_contrib in enumerate(res['Born_SO_Results']): 336 str_lines.append('|| Born contribution of split orders %s(%s) = %s'\ 337 %('*' if res['Born_kept'][i] else ' ', 338 format_so_orders(bso_contrib[0]), 339 special_float_format(bso_contrib[1]['BORN']))) 340 341 if len(so_order_names): 342 str_lines.append('|') 343 344 if len(res['Loop_SO_Results'])==1: 345 str_lines.append('|| All virtual contributions are of split orders *(%s)'\ 346 %format_so_orders(res['Loop_SO_Results'][0][0])) 347 elif len(res['Loop_SO_Results'])>1: 348 if not notZeroBorn: 349 str_lines.append( 350 ("| The coupling order combinations matching the squared order"+ 351 " constraints are marked with a star",main_color)) 352 for i, lso_contrib in enumerate(res['Loop_SO_Results']): 353 str_lines.append('|| Virtual contribution of split orders %s(%s):'\ 354 %('*' if res['Loop_kept'][i] else ' ', 355 format_so_orders(lso_contrib[0]))) 356 str_lines.append('| Accuracy = %.1e'%\ 357 lso_contrib[1]['ACC']), 358 str_lines.append('| Finite = %s'%\ 359 special_float_format(lso_contrib[1]['FIN'])), 360 if res['export_format']=='LoopInduced': 361 str_lines.append('|( Single pole = %s )'%\ 362 special_float_format(lso_contrib[1]['1EPS'])) 363 str_lines.append('|( Double pole = %s )'%\ 364 special_float_format(lso_contrib[1]['2EPS'])) 365 else: 366 str_lines.append('| Single pole = %s'%\ 367 special_float_format(lso_contrib[1]['1EPS'])) 368 str_lines.append('| Double pole = %s'%\ 369 special_float_format(lso_contrib[1]['2EPS'])) 370 str_lines.extend([ASCII_bar,'\n']) 371 372 return str_lines 373
374 -class SALauncher(ExtLauncher):
375 """ A class to launch a simple Standalone test """ 376
377 - def __init__(self, cmd_int, running_dir, **options):
378 """ initialize the StandAlone Version""" 379 380 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options) 381 self.cards = ['param_card.dat']
382 383
384 - def launch_program(self):
385 """launch the main program""" 386 sub_path = os.path.join(self.running_dir, 'SubProcesses') 387 for path in os.listdir(sub_path): 388 if path.startswith('P') and \ 389 os.path.isdir(os.path.join(sub_path, path)): 390 cur_path = os.path.join(sub_path, path) 391 # make 392 misc.compile(cwd=cur_path, mode='unknown') 393 # check 394 subprocess.call(['./check'], cwd=cur_path)
395
396 -class MWLauncher(ExtLauncher):
397 """ A class to launch a simple Standalone test """ 398 399
400 - def __init__(self, cmd_int, running_dir, **options):
401 """ initialize the StandAlone Version""" 402 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options) 403 self.options = cmd_int.options
404
405 - def launch_program(self):
406 """launch the main program""" 407 408 import madgraph.interface.madweight_interface as MW 409 # Check for number of cores if multicore mode 410 mode = str(self.cluster) 411 nb_node = 1 412 if mode == "2": 413 import multiprocessing 414 max_node = multiprocessing.cpu_count() 415 if max_node == 1: 416 logger.warning('Only one core is detected on your computer! Pass in single machine') 417 self.cluster = 0 418 self.launch_program() 419 return 420 elif max_node == 2: 421 nb_node = 2 422 elif not self.force: 423 nb_node = self.ask('How many core do you want to use?', max_node, range(2,max_node+1)) 424 else: 425 nb_node=max_node 426 427 import madgraph.interface.madevent_interface as ME 428 429 stdout_level = self.cmd_int.options['stdout_level'] 430 if self.shell: 431 usecmd = MW.MadWeightCmdShell(me_dir=self.running_dir, options=self.options) 432 else: 433 usecmd = MW.MadWeightCmd(me_dir=self.running_dir, options=self.options) 434 usecmd.pass_in_web_mode() 435 #Check if some configuration were overwritten by a command. If so use it 436 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')] 437 for line in set_cmd: 438 try: 439 usecmd.do_set(line[3:], log=False) 440 except Exception: 441 pass 442 443 usecmd.do_set('stdout_level %s' % stdout_level,log=False) 444 #ensure that the logger level 445 launch = self.cmd_int.define_child_cmd_interface( 446 usecmd, interface=False) 447 448 command = 'launch' 449 if mode == "1": 450 command += " --cluster" 451 elif mode == "2": 452 command += " --nb_core=%s" % nb_node 453 454 if self.force: 455 command+= " -f" 456 if self.laststep: 457 command += ' --laststep=%s' % self.laststep 458 459 try: 460 os.remove('ME5_debug') 461 except: 462 pass 463 launch.run_cmd(command) 464 launch.run_cmd('quit') 465 466 if os.path.exists('ME5_debug'): 467 return True
468 469 470
471 -class aMCatNLOLauncher(ExtLauncher):
472 """A class to launch MadEvent run""" 473
474 - def __init__(self, running_dir, cmd_int, run_mode='', unit='pb', **option):
475 """ initialize the StandAlone Version""" 476 477 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option) 478 #self.executable = os.path.join('.', 'bin','generate_events') 479 480 self.options = option 481 assert hasattr(self, 'cluster') 482 assert hasattr(self, 'multicore') 483 assert hasattr(self, 'name') 484 assert hasattr(self, 'shell') 485 486 self.unit = unit 487 self.run_mode = run_mode 488 489 if self.cluster or option['cluster']: 490 self.cluster = 1 491 if self.multicore or option['multicore']: 492 self.cluster = 2 493 494 self.cards = [] 495 496 # Assign a valid run name if not put in options 497 if self.name == '': 498 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
499
500 - def launch_program(self):
501 """launch the main program""" 502 503 # Check for number of cores if multicore mode 504 mode = str(self.cluster) 505 nb_node = 1 506 if mode == "2": 507 import multiprocessing 508 max_node = multiprocessing.cpu_count() 509 if max_node == 1: 510 logger.warning('Only one core is detected on your computer! Pass in single machine') 511 self.cluster = 0 512 self.launch_program() 513 return 514 elif max_node == 2: 515 nb_node = 2 516 elif not self.force: 517 nb_node = self.ask('How many cores do you want to use?', max_node, range(2,max_node+1)) 518 else: 519 nb_node=max_node 520 521 import madgraph.interface.amcatnlo_run_interface as run_int 522 523 if hasattr(self, 'shell') and self.shell: 524 usecmd = run_int.aMCatNLOCmdShell(me_dir=self.running_dir, options = self.cmd_int.options) 525 else: 526 usecmd = run_int.aMCatNLOCmd(me_dir=self.running_dir, options = self.cmd_int.options) 527 528 #Check if some configuration were overwritten by a command. If so use it 529 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')] 530 all_options = usecmd.options_configuration.keys() + usecmd.options_madgraph.keys() + usecmd.options_madevent.keys() 531 for line in set_cmd: 532 arg = line.split() 533 if arg[1] not in all_options: 534 continue 535 misc.sprint(line) 536 try: 537 usecmd.exec_cmd(line) 538 except Exception, error: 539 misc.sprint('Command %s fails with msg: %s'%(str(line), \ 540 str(error))) 541 pass 542 launch = self.cmd_int.define_child_cmd_interface( 543 usecmd, interface=False) 544 #launch.me_dir = self.running_dir 545 option_line = ' '.join([' --%s' % opt for opt in self.options.keys() \ 546 if self.options[opt] and not opt in ['cluster', 'multicore', 'name', 'appl_start_grid','shell']]) 547 if self.options['name']: 548 option_line += ' --name %s' % self.options['name'] 549 if 'appl_start_grid' in self.options and self.options['appl_start_grid']: 550 option_line += ' --appl_start_grid %s' % self.options['appl_start_grid'] 551 command = 'launch ' + self.run_mode + ' ' + option_line 552 553 if mode == "1": 554 command += " -c" 555 elif mode == "2": 556 command += " -m" 557 usecmd.nb_core = int(nb_node) 558 try: 559 os.remove('ME5_debug') 560 except: 561 pass 562 launch.run_cmd(command) 563 launch.run_cmd('quit')
564 565 566 567 568
569 -class MELauncher(ExtLauncher):
570 """A class to launch MadEvent run""" 571
572 - def __init__(self, running_dir, cmd_int , unit='pb', **option):
573 """ initialize the StandAlone Version""" 574 575 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option) 576 #self.executable = os.path.join('.', 'bin','generate_events') 577 self.pythia = cmd_int.options['pythia-pgs_path'] 578 self.delphes = cmd_int.options['delphes_path'], 579 self.options = cmd_int.options 580 581 assert hasattr(self, 'cluster') 582 assert hasattr(self, 'multicore') 583 assert hasattr(self, 'name') 584 assert hasattr(self, 'shell') 585 586 self.unit = unit 587 588 if self.cluster: 589 self.cluster = 1 590 if self.multicore: 591 self.cluster = 2 592 593 self.cards = [] 594 595 # Assign a valid run name if not put in options 596 if self.name == '': 597 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
598
599 - def launch_program(self):
600 """launch the main program""" 601 602 # Check for number of cores if multicore mode 603 mode = str(self.cluster) 604 nb_node = 1 605 if mode == "2": 606 import multiprocessing 607 max_node = multiprocessing.cpu_count() 608 if max_node == 1: 609 logger.warning('Only one core is detected on your computer! Pass in single machine') 610 self.cluster = 0 611 self.launch_program() 612 return 613 elif max_node == 2: 614 nb_node = 2 615 elif not self.force: 616 nb_node = self.ask('How many cores do you want to use?', max_node, range(2,max_node+1)) 617 else: 618 nb_node=max_node 619 620 import madgraph.interface.madevent_interface as ME 621 622 stdout_level = self.cmd_int.options['stdout_level'] 623 if self.shell: 624 usecmd = ME.MadEventCmdShell(me_dir=self.running_dir, options=self.options) 625 else: 626 usecmd = ME.MadEventCmd(me_dir=self.running_dir, options=self.options) 627 usecmd.pass_in_web_mode() 628 #Check if some configuration were overwritten by a command. If so use it 629 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')] 630 all_options = usecmd.options_configuration.keys() + usecmd.options_madgraph.keys() + usecmd.options_madevent.keys() 631 for line in set_cmd: 632 arg = line.split() 633 if arg[1] not in all_options: 634 continue 635 try: 636 usecmd.do_set(line[3:], log=False) 637 except usecmd.InvalidCmd: 638 pass 639 usecmd.do_set('stdout_level %s' % stdout_level,log=False) 640 #ensure that the logger level 641 launch = self.cmd_int.define_child_cmd_interface( 642 usecmd, interface=False) 643 #launch.me_dir = self.running_dir 644 if self.unit == 'pb': 645 command = 'generate_events %s' % self.name 646 else: 647 warning_text = '''\ 648 This command will create a new param_card with the computed width. 649 This param_card makes sense only if you include all processes for 650 the computation of the width. For more efficient width computation: 651 see arXiv:1402.1178.''' 652 logger.warning(warning_text) 653 654 command = 'generate_events %s' % self.name 655 if mode == "1": 656 command += " --cluster" 657 elif mode == "2": 658 command += " --nb_core=%s" % nb_node 659 660 if self.force: 661 command+= " -f" 662 663 if self.laststep: 664 command += ' --laststep=%s' % self.laststep 665 if self.reweight: 666 command += ' -R ' 667 if self.madspin: 668 command += ' -M ' 669 670 671 try: 672 os.remove('ME5_debug') 673 except: 674 pass 675 676 launch.run_cmd(command) 677 launch.run_cmd('quit') 678 679 if os.path.exists('ME5_debug'): 680 return True 681 682 # Display the cross-section to the screen 683 path = os.path.join(self.running_dir, 'SubProcesses', 'results.dat') 684 if not os.path.exists(path): 685 logger.error('Generation failed (no results.dat file found)') 686 return 687 fsock = open(path) 688 line = fsock.readline() 689 cross, error = line.split()[0:2] 690 691 logger.info('more information in %s' 692 % os.path.join(self.running_dir, 'index.html'))
693 694
695 -class Pythia8Launcher(ExtLauncher):
696 """A class to launch Pythia8 run""" 697
698 - def __init__(self, running_dir, cmd_int, **option):
699 """ initialize launching Pythia 8""" 700 701 running_dir = os.path.join(running_dir, 'examples') 702 ExtLauncher.__init__(self, cmd_int, running_dir, '.', **option) 703 self.cards = []
704
705 - def prepare_run(self):
706 """ ask for pythia-pgs/delphes run """ 707 708 # Find all main_model_process.cc files 709 date_file_list = [] 710 for file in glob.glob(os.path.join(self.running_dir,'main_*_*.cc')): 711 # retrieves the stats for the current file as a tuple 712 # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) 713 # the tuple element mtime at index 8 is the last-modified-date 714 stats = os.stat(file) 715 # create tuple (year yyyy, month(1-12), day(1-31), hour(0-23), minute(0-59), second(0-59), 716 # weekday(0-6, 0 is monday), Julian day(1-366), daylight flag(-1,0 or 1)) from seconds since epoch 717 # note: this tuple can be sorted properly by date and time 718 lastmod_date = time.localtime(stats[8]) 719 date_file_list.append((lastmod_date, os.path.split(file)[-1])) 720 721 if not date_file_list: 722 raise MadGraph5Error, 'No Pythia output found' 723 # Sort files according to date with newest first 724 date_file_list.sort() 725 date_file_list.reverse() 726 files = [d[1] for d in date_file_list] 727 728 answer = '' 729 answer = self.ask('Select a main file to run:', files[0], files) 730 731 self.cards.append(answer) 732 733 self.executable = self.cards[-1].replace(".cc","") 734 735 # Assign a valid run name if not put in options 736 if self.name == '': 737 for i in range(1000): 738 path = os.path.join(self.running_dir, '', 739 '%s_%02i.log' % (self.executable, i)) 740 if not os.path.exists(path): 741 self.name = '%s_%02i.log' % (self.executable, i) 742 break 743 744 if self.name == '': 745 raise MadGraph5Error, 'too many runs in this directory' 746 747 # Find all exported models 748 models = glob.glob(os.path.join(self.running_dir,os.path.pardir, 749 "Processes_*")) 750 models = [os.path.split(m)[-1].replace("Processes_","") for m in models] 751 # Extract model name from executable 752 models.sort(key=len) 753 models.reverse() 754 model_dir = "" 755 for model in models: 756 if self.executable.replace("main_", "").startswith(model): 757 model_dir = "Processes_%s" % model 758 break 759 if model_dir: 760 self.model = model 761 self.model_dir = os.path.realpath(os.path.join(self.running_dir, 762 os.path.pardir, 763 model_dir)) 764 self.cards.append(os.path.join(self.model_dir, 765 "param_card_%s.dat" % model))
766
767 - def launch_program(self):
768 """launch the main program""" 769 770 # Make pythia8 771 print "Running make for pythia8 directory" 772 misc.compile(cwd=os.path.join(self.running_dir, os.path.pardir), mode='cpp') 773 if self.model_dir: 774 print "Running make in %s" % self.model_dir 775 misc.compile(cwd=self.model_dir, mode='cpp') 776 # Finally run make for executable 777 makefile = self.executable.replace("main_","Makefile_") 778 print "Running make with %s" % makefile 779 misc.compile(arg=['-f', makefile], cwd=self.running_dir, mode='cpp') 780 781 print "Running " + self.executable 782 783 output = open(os.path.join(self.running_dir, self.name), 'w') 784 if not self.executable.startswith('./'): 785 self.executable = os.path.join(".", self.executable) 786 subprocess.call([self.executable], stdout = output, stderr = output, 787 cwd=self.running_dir) 788 789 # Display the cross-section to the screen 790 path = os.path.join(self.running_dir, self.name) 791 pydoc.pager(open(path).read()) 792 793 print "Output of the run is found at " + \ 794 os.path.realpath(os.path.join(self.running_dir, self.name))
795 796 # old compatibility shortcut 797 open_file = misc.open_file 798