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 if hasattr(self, 'shell'): 523 usecmd = run_int.aMCatNLOCmdShell(me_dir=self.running_dir, options = self.cmd_int.options) 524 else: 525 usecmd = run_int.aMCatNLOCmd(me_dir=self.running_dir, options = self.cmd_int.options) 526 527 #Check if some configuration were overwritten by a command. If so use it 528 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')] 529 all_options = usecmd.options_configuration.keys() + usecmd.options_madgraph.keys() + usecmd.options_madevent.keys() 530 for line in set_cmd: 531 arg = line.split() 532 if arg[1] not in all_options: 533 continue 534 misc.sprint(line) 535 try: 536 usecmd.exec_cmd(line) 537 except Exception, error: 538 misc.sprint('Command %s fails with msg: %s'%(str(line), \ 539 str(error))) 540 pass 541 launch = self.cmd_int.define_child_cmd_interface( 542 usecmd, interface=False) 543 #launch.me_dir = self.running_dir 544 option_line = ' '.join([' --%s' % opt for opt in self.options.keys() \ 545 if self.options[opt] and not opt in ['cluster', 'multicore', 'name', 'appl_start_grid']]) 546 if self.options['name']: 547 option_line += ' --name %s' % self.options['name'] 548 if 'appl_start_grid' in self.options and self.options['appl_start_grid']: 549 option_line += ' --appl_start_grid %s' % self.options['appl_start_grid'] 550 command = 'launch ' + self.run_mode + ' ' + option_line 551 552 if mode == "1": 553 command += " -c" 554 elif mode == "2": 555 command += " -m" 556 usecmd.nb_core = int(nb_node) 557 try: 558 os.remove('ME5_debug') 559 except: 560 pass 561 launch.run_cmd(command) 562 launch.run_cmd('quit')
563 564 565 566 567
568 -class MELauncher(ExtLauncher):
569 """A class to launch MadEvent run""" 570
571 - def __init__(self, running_dir, cmd_int , unit='pb', **option):
572 """ initialize the StandAlone Version""" 573 574 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option) 575 #self.executable = os.path.join('.', 'bin','generate_events') 576 self.pythia = cmd_int.options['pythia-pgs_path'] 577 self.delphes = cmd_int.options['delphes_path'], 578 self.options = cmd_int.options 579 580 assert hasattr(self, 'cluster') 581 assert hasattr(self, 'multicore') 582 assert hasattr(self, 'name') 583 assert hasattr(self, 'shell') 584 585 self.unit = unit 586 587 if self.cluster: 588 self.cluster = 1 589 if self.multicore: 590 self.cluster = 2 591 592 self.cards = [] 593 594 # Assign a valid run name if not put in options 595 if self.name == '': 596 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
597
598 - def launch_program(self):
599 """launch the main program""" 600 601 # Check for number of cores if multicore mode 602 mode = str(self.cluster) 603 nb_node = 1 604 if mode == "2": 605 import multiprocessing 606 max_node = multiprocessing.cpu_count() 607 if max_node == 1: 608 logger.warning('Only one core is detected on your computer! Pass in single machine') 609 self.cluster = 0 610 self.launch_program() 611 return 612 elif max_node == 2: 613 nb_node = 2 614 elif not self.force: 615 nb_node = self.ask('How many cores do you want to use?', max_node, range(2,max_node+1)) 616 else: 617 nb_node=max_node 618 619 import madgraph.interface.madevent_interface as ME 620 621 stdout_level = self.cmd_int.options['stdout_level'] 622 if self.shell: 623 usecmd = ME.MadEventCmdShell(me_dir=self.running_dir, options=self.options) 624 else: 625 usecmd = ME.MadEventCmd(me_dir=self.running_dir, options=self.options) 626 usecmd.pass_in_web_mode() 627 #Check if some configuration were overwritten by a command. If so use it 628 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')] 629 all_options = usecmd.options_configuration.keys() + usecmd.options_madgraph.keys() + usecmd.options_madevent.keys() 630 for line in set_cmd: 631 arg = line.split() 632 if arg[1] not in all_options: 633 continue 634 try: 635 usecmd.do_set(line[3:], log=False) 636 except usecmd.InvalidCmd: 637 pass 638 usecmd.do_set('stdout_level %s' % stdout_level,log=False) 639 #ensure that the logger level 640 launch = self.cmd_int.define_child_cmd_interface( 641 usecmd, interface=False) 642 #launch.me_dir = self.running_dir 643 if self.unit == 'pb': 644 command = 'generate_events %s' % self.name 645 else: 646 warning_text = '''\ 647 This command will create a new param_card with the computed width. 648 This param_card makes sense only if you include all processes for 649 the computation of the width. For more efficient width computation: 650 see arXiv:1402.1178.''' 651 logger.warning(warning_text) 652 653 command = 'generate_events %s' % self.name 654 if mode == "1": 655 command += " --cluster" 656 elif mode == "2": 657 command += " --nb_core=%s" % nb_node 658 659 if self.force: 660 command+= " -f" 661 662 if self.laststep: 663 command += ' --laststep=%s' % self.laststep 664 if self.reweight: 665 command += ' -R ' 666 if self.madspin: 667 command += ' -M ' 668 669 670 try: 671 os.remove('ME5_debug') 672 except: 673 pass 674 675 launch.run_cmd(command) 676 launch.run_cmd('quit') 677 678 if os.path.exists('ME5_debug'): 679 return True 680 681 # Display the cross-section to the screen 682 path = os.path.join(self.running_dir, 'SubProcesses', 'results.dat') 683 if not os.path.exists(path): 684 logger.error('Generation failed (no results.dat file found)') 685 return 686 fsock = open(path) 687 line = fsock.readline() 688 cross, error = line.split()[0:2] 689 690 logger.info('more information in %s' 691 % os.path.join(self.running_dir, 'index.html'))
692 693
694 -class Pythia8Launcher(ExtLauncher):
695 """A class to launch Pythia8 run""" 696
697 - def __init__(self, running_dir, cmd_int, **option):
698 """ initialize launching Pythia 8""" 699 700 running_dir = os.path.join(running_dir, 'examples') 701 ExtLauncher.__init__(self, cmd_int, running_dir, '.', **option) 702 self.cards = []
703
704 - def prepare_run(self):
705 """ ask for pythia-pgs/delphes run """ 706 707 # Find all main_model_process.cc files 708 date_file_list = [] 709 for file in glob.glob(os.path.join(self.running_dir,'main_*_*.cc')): 710 # retrieves the stats for the current file as a tuple 711 # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) 712 # the tuple element mtime at index 8 is the last-modified-date 713 stats = os.stat(file) 714 # create tuple (year yyyy, month(1-12), day(1-31), hour(0-23), minute(0-59), second(0-59), 715 # weekday(0-6, 0 is monday), Julian day(1-366), daylight flag(-1,0 or 1)) from seconds since epoch 716 # note: this tuple can be sorted properly by date and time 717 lastmod_date = time.localtime(stats[8]) 718 date_file_list.append((lastmod_date, os.path.split(file)[-1])) 719 720 if not date_file_list: 721 raise MadGraph5Error, 'No Pythia output found' 722 # Sort files according to date with newest first 723 date_file_list.sort() 724 date_file_list.reverse() 725 files = [d[1] for d in date_file_list] 726 727 answer = '' 728 answer = self.ask('Select a main file to run:', files[0], files) 729 730 self.cards.append(answer) 731 732 self.executable = self.cards[-1].replace(".cc","") 733 734 # Assign a valid run name if not put in options 735 if self.name == '': 736 for i in range(1000): 737 path = os.path.join(self.running_dir, '', 738 '%s_%02i.log' % (self.executable, i)) 739 if not os.path.exists(path): 740 self.name = '%s_%02i.log' % (self.executable, i) 741 break 742 743 if self.name == '': 744 raise MadGraph5Error, 'too many runs in this directory' 745 746 # Find all exported models 747 models = glob.glob(os.path.join(self.running_dir,os.path.pardir, 748 "Processes_*")) 749 models = [os.path.split(m)[-1].replace("Processes_","") for m in models] 750 # Extract model name from executable 751 models.sort(key=len) 752 models.reverse() 753 model_dir = "" 754 for model in models: 755 if self.executable.replace("main_", "").startswith(model): 756 model_dir = "Processes_%s" % model 757 break 758 if model_dir: 759 self.model = model 760 self.model_dir = os.path.realpath(os.path.join(self.running_dir, 761 os.path.pardir, 762 model_dir)) 763 self.cards.append(os.path.join(self.model_dir, 764 "param_card_%s.dat" % model))
765
766 - def launch_program(self):
767 """launch the main program""" 768 769 # Make pythia8 770 print "Running make for pythia8 directory" 771 misc.compile(cwd=os.path.join(self.running_dir, os.path.pardir), mode='cpp') 772 if self.model_dir: 773 print "Running make in %s" % self.model_dir 774 misc.compile(cwd=self.model_dir, mode='cpp') 775 # Finally run make for executable 776 makefile = self.executable.replace("main_","Makefile_") 777 print "Running make with %s" % makefile 778 misc.compile(arg=['-f', makefile], cwd=self.running_dir, mode='cpp') 779 780 print "Running " + self.executable 781 782 output = open(os.path.join(self.running_dir, self.name), 'w') 783 if not self.executable.startswith('./'): 784 self.executable = os.path.join(".", self.executable) 785 subprocess.call([self.executable], stdout = output, stderr = output, 786 cwd=self.running_dir) 787 788 # Display the cross-section to the screen 789 path = os.path.join(self.running_dir, self.name) 790 pydoc.pager(open(path).read()) 791 792 print "Output of the run is found at " + \ 793 os.path.realpath(os.path.join(self.running_dir, self.name))
794 795 # old compatibility shortcut 796 open_file = misc.open_file 797