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

Source Code for Module madgraph.interface.master_interface

  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  """A user friendly command line interface to access all MadGraph5_aMC@NLO features. 
 16     Uses the cmd package for command interpretation and tab completion. 
 17  """ 
 18   
 19   
 20  import atexit 
 21  import logging 
 22  import optparse 
 23  import os 
 24  import pydoc 
 25  import re 
 26  import subprocess 
 27  import sys 
 28  import traceback 
 29  import time 
 30   
 31  root_path = os.path.split(os.path.dirname(os.path.realpath( __file__ )))[0] 
 32  root_path = os.path.split(root_path)[0] 
 33  sys.path.insert(0, root_path) 
 34   
 35  #usefull shortcut 
 36  pjoin = os.path.join 
 37   
 38  import madgraph 
 39  import madgraph.core.diagram_generation as diagram_generation 
 40  import madgraph.core.helas_objects as helas_objects 
 41  import madgraph.loop.loop_base_objects as loop_base_objects 
 42  import madgraph.interface.extended_cmd as cmd 
 43  import madgraph.interface.madgraph_interface as MGcmd 
 44  import madgraph.interface.loop_interface as LoopCmd 
 45  import madgraph.interface.amcatnlo_interface as amcatnloCmd 
 46  import madgraph.fks.fks_base as fks_base 
 47  import madgraph.iolibs.files as files 
 48  import madgraph.various.misc as misc 
 49   
 50  from madgraph import MG4DIR, MG5DIR, MadGraph5Error 
 51   
 52  logger = logging.getLogger('cmdprint') # -> stdout 
53 54 55 -class Switcher(object):
56 """ Helping class containing all the switching routine """ 57
58 - def __init__(self, main='MadGraph', *args, **opt):
59 60 # define the interface 61 self.change_principal_cmd(main) 62 self.cmd.__init__(self, *args, **opt)
63 64 interface_names= {'MadGraph':('MG5_aMC',MGcmd.MadGraphCmd), 65 'MadLoop':('MG5_aMC',LoopCmd.LoopInterface), 66 'aMC@NLO':('MG5_aMC',amcatnloCmd.aMCatNLOInterface)} 67 68 _switch_opts = interface_names.keys() 69 current_interface = None 70 71 # Helper functions 72
73 - def setup(self, *args, **opts):
74 """ Function to initialize the interface when switched to it. It is not 75 the same as __init__ as this latter functions would call its mother 76 from madgraph_interface and this is only desirable for the first 77 initialization when launching MG5 """ 78 return self.cmd.setup(self, *args, **opts)
79 155 156 157 158 @staticmethod
159 - def extract_process_type(line):
160 """Extract from a string what is the type of the computation. This 161 returns a tuple (mode, option, pert_orders) where mode can be either 'NLO' or 'tree' 162 and option 'all', 'real' or 'virt'.""" 163 164 # Perform sanity modifications on the lines: 165 # Add a space before and after any > , $ / | [ ] 166 space_before = re.compile(r"(?P<carac>\S)(?P<tag>[\\[\\]/\,\\$\\>|])(?P<carac2>\S)") 167 line2 = space_before.sub(r'\g<carac> \g<tag> \g<carac2>', line) 168 169 # Use regular expressions to extract the loop mode (if present) and its 170 # option, specified in the line with format [ option = loop_orders ] or 171 # [ loop_orders ] which implicitly select the 'all' option. 172 loopRE = re.compile(r"^(.*)(?P<loop>\[(\s*(?P<option>\w+)\s*=)?(?P<orders>.+)?\])(.*)$") 173 res=loopRE.search(line) 174 if res: 175 orders=res.group('orders').split() if res.group('orders') else [] 176 if res.group('option') and len(res.group('option').split())==1: 177 if res.group('option').split()[0]=='tree': 178 return ('tree',res.group('option').split()[0],orders) 179 else: 180 return ('NLO',res.group('option').split()[0],orders) 181 else: 182 # If not option is set the convention is that the mode is 'all' 183 # unless no perturbation orders is defined. 184 if len(orders)>0: 185 return ('NLO','all',orders) 186 else: 187 return ('tree',None,[]) 188 else: 189 return ('tree',None,[])
190 191 # Wrapping functions possibly switching to new interfaces 192
193 - def do_add(self, line, *args, **opts):
194 195 argss = cmd.Cmd.split_arg(line) 196 if len(argss)>=1 and argss[0] in ['process','timing','profile']: 197 proc_line = ' '.join(argss[1:]) 198 (type,nlo_mode,orders)=self.extract_process_type(proc_line) 199 if type=='NLO': 200 if not nlo_mode in self._valid_nlo_modes: raise self.InvalidCMD( \ 201 'The NLO mode %s is not valid. Please choose one among: %s' \ 202 % (nlo_mode, ' '.join(self._valid_nlo_modes))) 203 elif nlo_mode == 'all': 204 self.change_principal_cmd('aMC@NLO') 205 elif nlo_mode == 'real': 206 self.change_principal_cmd('aMC@NLO') 207 elif nlo_mode == 'virt' or nlo_mode == 'sqrvirt': 208 self.change_principal_cmd('MadLoop') 209 210 return self.cmd.do_add(self, line, *args, **opts)
211
212 - def do_check(self, line, *args, **opts):
213 214 argss = self.split_arg(line) 215 proc_line = " ".join(argss[1:]) 216 (type,nlo_mode,orders)=self.extract_process_type(proc_line) 217 if type=='NLO': 218 if not nlo_mode in self._valid_nlo_modes: raise self.InvalidCMD(\ 219 'The NLO mode %s is not valid. Please chose one among: %s' \ 220 % (nlo_mode, ' '.join(self._valid_nlo_modes))) 221 elif nlo_mode == 'all': 222 self.change_principal_cmd('aMC@NLO') 223 elif nlo_mode == 'real': 224 self.change_principal_cmd('aMC@NLO') 225 elif nlo_mode == 'virt' or nlo_mode == 'sqrvirt': 226 self.change_principal_cmd('MadLoop') 227 else: 228 self.change_principal_cmd('MadGraph') 229 230 return self.cmd.do_check(self, line, *args, **opts)
231
232 - def do_generate(self, line, *args, **opts):
233 234 argss = cmd.Cmd.split_arg(line) 235 # Make sure to switch to the right interface. 236 if len(argss)>=1: 237 proc_line = ' '.join(argss[1:]) 238 (type,nlo_mode,orders)=self.extract_process_type(proc_line) 239 if type=='NLO': 240 if not nlo_mode in self._valid_nlo_modes: raise self.InvalidCmd( \ 241 'The NLO mode %s is not valid. Please chose one among: %s' \ 242 % (nlo_mode, ' '.join(self._valid_nlo_modes))) 243 elif nlo_mode == 'all' or nlo_mode == 'real': 244 self._fks_multi_proc = fks_base.FKSMultiProcess() 245 self.change_principal_cmd('aMC@NLO') 246 elif nlo_mode == 'virt' or nlo_mode == 'virtsqr': 247 self.change_principal_cmd('MadLoop') 248 else: 249 self.change_principal_cmd('MadGraph') 250 return self.cmd.do_generate(self, line, *args, **opts)
251
252 - def do_import(self, *args, **opts):
253 self.cmd.do_import(self, *args, **opts) 254 if self._curr_model: 255 if isinstance(self._curr_model, loop_base_objects.LoopModel) and \ 256 self._curr_model['perturbation_couplings']!=[] and \ 257 self.current_interface not in ['aMC@NLO','MadLoop']: 258 self.change_principal_cmd('aMC@NLO') 259 if (not isinstance(self._curr_model, loop_base_objects.LoopModel) or \ 260 self._curr_model['perturbation_couplings']==[]) and \ 261 self.current_interface in ['MadLoop']: 262 self.change_principal_cmd('MadGraph') 263 import madgraph.various.misc as misc 264 return
265
266 - def do_output(self, line, *args, **opts):
267 """ treat output aloha in order to use always the one in MG5 """ 268 if line.strip().startswith('aloha'): 269 MGcmd.MadGraphCmd.do_output(self, line, *args, **opts) 270 else: 271 self.cmd.do_output(self, line, *args, **opts)
272
273 - def check_output(self, arg, *args, **opts):
274 if arg and arg[0] == 'aloha': 275 MGcmd.MadGraphCmd.check_output(self, arg, *args, **opts) 276 else: 277 self.cmd.check_output(self, arg, *args, **opts)
278 279 280 281 282 # Dummy functions, not triggering any switch of interfaces 283
284 - def export(self, *args, **opts):
285 return self.cmd.export(self, *args, **opts)
286
287 - def check_add(self, *args, **opts):
288 return self.cmd.check_add(self, *args, **opts)
289
290 - def check_answer_in_input_file(self, *args, **opts):
291 return self.cmd.check_answer_in_input_file(self, *args, **opts)
292
293 - def check_check(self, *args, **opts):
294 return self.cmd.check_check(self, *args, **opts)
295
296 - def check_define(self, *args, **opts):
297 return self.cmd.check_define(self, *args, **opts)
298
299 - def check_decay_diagram(self, *args, **opts):
300 return self.cmd.check_decay_diagram(self, *args, **opts)
301
302 - def complete_decay_diagram(self, *args, **opts):
303 return self.cmd.complete_decay_diagram(self, *args, **opts)
304
305 - def do_decay_diagram(self, *args, **opts):
306 return self.cmd.do_decay_diagram(self, *args, **opts)
307
308 - def help_decay_diagram(self, *args, **opts):
309 return self.cmd.help_decay_diagram(self, *args, **opts)
310
311 - def check_compute_widths(self, *args, **opts):
312 return self.cmd.check_compute_widths(self, *args, **opts)
313
314 - def complete_compute_widths(self, *args, **opts):
315 return self.cmd.complete_compute_widths(self, *args, **opts)
316
317 - def do_compute_widths(self, *args, **opts):
318 return self.cmd.do_compute_widths(self, *args, **opts)
319
320 - def help_compute_widths(self, *args, **opts):
321 return self.cmd.help_compute_widths(self, *args, **opts)
322
323 - def check_display(self, *args, **opts):
324 return self.cmd.check_display(self, *args, **opts)
325
326 - def check_draw(self, *args, **opts):
327 return self.cmd.check_draw(self, *args, **opts)
328
329 - def check_for_export_dir(self, *args, **opts):
330 return self.cmd.check_for_export_dir(self, *args, **opts)
331
332 - def check_generate(self, *args, **opts):
333 return self.cmd.check_generate(self, *args, **opts)
334
335 - def check_tutorial(self, *args, **opts):
336 return self.cmd.check_tutorial(self, *args, **opts)
337
338 - def check_history(self, *args, **opts):
339 return self.cmd.check_history(self, *args, **opts)
340
341 - def check_import(self, *args, **opts):
342 return self.cmd.check_import(self, *args, **opts)
343
344 - def check_install(self, *args, **opts):
345 return self.cmd.check_install(self, *args, **opts)
346
347 - def check_launch(self, *args, **opts):
348 return self.cmd.check_launch(self, *args, **opts)
349
350 - def check_load(self, *args, **opts):
351 return self.cmd.check_load(self, *args, **opts)
352
353 - def check_open(self, *args, **opts):
354 return self.cmd.check_open(self, *args, **opts)
355
356 - def check_process_format(self, *args, **opts):
357 return self.cmd.check_process_format(self, *args, **opts)
358
359 - def check_save(self, *args, **opts):
360 return self.cmd.check_save(self, *args, **opts)
361
362 - def check_set(self, *args, **opts):
363 return self.cmd.check_set(self, *args, **opts)
364
365 - def get_stored_line(self, *args, **opts):
366 return self.cmd.get_stored_line(self, *args, **opts)
367
368 - def complete_add(self, *args, **opts):
369 return self.cmd.complete_add(self, *args, **opts)
370
371 - def complete_switch(self, *args, **opts):
372 return self.cmd.complete_switch(self, *args, **opts)
373
374 - def complete_check(self, *args, **opts):
375 return self.cmd.complete_check(self, *args, **opts)
376
377 - def complete_define(self, *args, **opts):
378 return self.cmd.complete_define(self, *args, **opts)
379
380 - def complete_display(self, *args, **opts):
381 return self.cmd.complete_display(self, *args, **opts)
382
383 - def complete_draw(self, *args, **opts):
384 return self.cmd.complete_draw(self, *args, **opts)
385
386 - def complete_generate(self, *args, **opts):
387 return self.cmd.complete_generate(self, *args, **opts)
388
389 - def complete_help(self, *args, **opts):
390 return self.cmd.complete_help(self, *args, **opts)
391
392 - def complete_history(self, *args, **opts):
393 return self.cmd.complete_history(self, *args, **opts)
394
395 - def complete_import(self, *args, **opts):
396 return self.cmd.complete_import(self, *args, **opts)
397
398 - def complete_install(self, *args, **opts):
399 return self.cmd.complete_install(self, *args, **opts)
400
401 - def complete_launch(self, *args, **opts):
402 return self.cmd.complete_launch(self, *args, **opts)
403
404 - def complete_load(self, *args, **opts):
405 return self.cmd.complete_load(self, *args, **opts)
406
407 - def complete_open(self, *args, **opts):
408 return self.cmd.complete_open(self, *args, **opts)
409
410 - def complete_output(self, *args, **opts):
411 return self.cmd.complete_output(self, *args, **opts)
412
413 - def complete_save(self, *args, **opts):
414 return self.cmd.complete_save(self, *args, **opts)
415
416 - def complete_set(self, *args, **opts):
417 return self.cmd.complete_set(self, *args, **opts)
418
419 - def complete_tutorial(self, *args, **opts):
420 return self.cmd.complete_tutorial(self, *args, **opts)
421
422 - def do_switch(self, *args, **opts):
423 """Not in help """ 424 return self.cmd.do_switch(self, *args, **opts)
425
426 - def do_EOF(self, *args, **opts):
427 return self.cmd.do_EOF(self, *args, **opts)
428
429 - def do_define(self, *args, **opts):
430 return self.cmd.do_define(self, *args, **opts)
431
432 - def do_display(self, *args, **opts):
433 return self.cmd.do_display(self, *args, **opts)
434
435 - def do_exit(self, *args, **opts):
436 return self.cmd.do_exit(self, *args, **opts)
437
438 - def do_help(self, *args, **opts):
439 return self.cmd.do_help(self, *args, **opts)
440
441 - def do_history(self, *args, **opts):
442 return self.cmd.do_history(self, *args, **opts)
443
444 - def do_install(self, *args, **opts):
445 self.cmd.do_install(self, *args, **opts)
446
447 - def do_launch(self, line, *argss, **opts):
448 args = cmd.Cmd.split_arg(line) 449 # check if a path is given 450 if len(args) >=1: 451 if os.path.isdir(args[0]): 452 path = os.path.realpath(args[0]) 453 elif os.path.isdir(pjoin(MG5DIR,args[0])): 454 path = pjoin(MG5DIR,args[0]) 455 elif MG4DIR and os.path.isdir(pjoin(MG4DIR,args[0])): 456 path = pjoin(MG4DIR,args[0]) 457 else: 458 path=None 459 # if there is a path, find what output has been done 460 if path: 461 type = self.cmd.find_output_type(self, path) 462 if type in ['standalone', 'standalone_cpp', 'pythia8', 'madevent']: 463 self.change_principal_cmd('MadGraph') 464 elif type == 'aMC@NLO': 465 self.change_principal_cmd('aMC@NLO') 466 elif type == 'MadLoop': 467 self.change_principal_cmd('MadLoop') 468 469 return self.cmd.do_launch(self, line, *argss, **opts)
470
471 - def do_load(self, *args, **opts):
472 return self.cmd.do_load(self, *args, **opts)
473
474 - def do_open(self, *args, **opts):
475 return self.cmd.do_open(self, *args, **opts)
476
477 - def do_quit(self, *args, **opts):
478 return self.cmd.do_quit(self, *args, **opts)
479
480 - def do_save(self, *args, **opts):
481 return self.cmd.do_save(self, *args, **opts)
482
483 - def do_set(self, *args, **opts):
484 return self.cmd.do_set(self, *args, **opts)
485
486 - def do_tutorial(self, *args, **opts):
487 return self.cmd.do_tutorial(self, *args, **opts)
488
489 - def help_EOF(self, *args, **opts):
490 return self.cmd.help_EOF(self, *args, **opts)
491
492 - def help_add(self, *args, **opts):
493 return self.cmd.help_add(self, *args, **opts)
494
495 - def help_check(self, *args, **opts):
496 return self.cmd.help_check(self, *args, **opts)
497
498 - def help_define(self, *args, **opts):
499 return self.cmd.help_define(self, *args, **opts)
500
501 - def help_display(self, *args, **opts):
502 return self.cmd.help_display(self, *args, **opts)
503
504 - def help_generate(self, *args, **opts):
505 return self.cmd.help_generate(self, *args, **opts)
506
507 - def help_help(self, *args, **opts):
508 return self.cmd.help_help(self, *args, **opts)
509
510 - def help_history(self, *args, **opts):
511 return self.cmd.help_history(self, *args, **opts)
512
513 - def help_import(self, *args, **opts):
514 return self.cmd.help_import(self, *args, **opts)
515
516 - def help_install(self, *args, **opts):
517 return self.cmd.help_install(self, *args, **opts)
518
519 - def help_launch(self, *args, **opts):
520 return self.cmd.help_launch(self, *args, **opts)
521
522 - def help_load(self, *args, **opts):
523 return self.cmd.help_load(self, *args, **opts)
524
525 - def help_open(self, *args, **opts):
526 return self.cmd.help_open(self, *args, **opts)
527
528 - def help_output(self, *args, **opts):
529 return self.cmd.help_output(self, *args, **opts)
530
531 - def help_quit(self, *args, **opts):
532 return self.cmd.help_quit(self, *args, **opts)
533
534 - def help_save(self, *args, **opts):
535 return self.cmd.help_save(self, *args, **opts)
536
537 - def help_set(self, *args, **opts):
538 return self.cmd.help_set(self, *args, **opts)
539
540 - def help_tutorial(self, *args, **opts):
541 return self.cmd.help_tutorial(self, *args, **opts)
542
543 - def test_interface(self, *args, **opts):
544 return self.cmd.test_interface(self, *args, **opts)
545
546 - def set_configuration(self, *args, **opts):
547 return self.cmd.set_configuration(self, *args, **opts)
548
549 - def check_customize_model(self, *args, **opts):
550 return self.cmd.check_customize_model(self, *args, **opts)
551
552 - def complete_customize_model(self, *args, **opts):
553 return self.cmd.complete_customize_model(self, *args, **opts)
554
555 - def do_customize_model(self, *args, **opts):
556 return self.cmd.do_customize_model(self, *args, **opts)
557
558 - def help_customize_model(self, *args, **opts):
559 return self.cmd.help_customize_model(self, *args, **opts)
560
561 -class MasterCmd(Switcher, LoopCmd.LoopInterface, amcatnloCmd.aMCatNLOInterface, cmd.CmdShell):
562
563 - def __init__(self, main='MadGraph', *args, **opt):
564 565 # define the interface 566 if main in self.interface_names.keys(): 567 self.prompt= self.interface_names[main][0]+'>' 568 self.cmd= self.interface_names[main][1] 569 self.current_interface=main 570 else: 571 raise MadGraph5Error, 'Type of interface not valid: %s' % main 572 self.cmd.__init__(self, *args, **opt) 573 self.current_interface = main
574
575 - def complete_switch(self, text, line, begidx, endidx):
576 """Complete the switch command""" 577 return self.list_completion(text,self._switch_opts)
578
579 - def do_switch(self, line):
580 """Not in help: Allow to switch to any given interface from command line """ 581 582 args = cmd.Cmd.split_arg(line) 583 if len(args)==1 and args[0] in self.interface_names.keys(): 584 self.change_principal_cmd(args[0]) 585 else: 586 raise self.InvalidCmd("Invalid switch command or non existing interface %s."\ 587 %args[0]+" Valid interfaces are %s"\ 588 %','.join(interface_quick_name.keys()))
589
590 - def change_principal_cmd(self, name):
591 old_cmd=self.current_interface 592 if name in self.interface_names.keys(): 593 self.prompt= self.interface_names[name][0]+'>' 594 self.cmd= self.interface_names[name][1] 595 self.current_interface=name 596 else: 597 raise MadGraph5Error, 'Type of interface not valid: %s' % name 598 599 if self.interface_names[old_cmd][0]!=self.interface_names[name][0]: 600 logger.info("Switching from interface %s to %s"\ 601 %(self.interface_names[old_cmd][0],\ 602 self.interface_names[name][0])) 603 # Setup the interface 604 self.cmd.setup(self) 605 606 if __debug__: 607 self.debug_link_to_command()
608
609 610 -class MasterCmdWeb(MGcmd.MadGraphCmdWeb, Switcher, LoopCmd.LoopInterfaceWeb):
611
612 - def __init__(self, *arg, **opt):
613 614 if os.environ.has_key('_CONDOR_SCRATCH_DIR'): 615 self.writing_dir = pjoin(os.environ['_CONDOR_SCRATCH_DIR'], \ 616 os.path.pardir) 617 else: 618 self.writing_dir = pjoin(os.environ['MADGRAPH_DATA'], 619 os.environ['REMOTE_USER']) 620 621 622 #standard initialization 623 Switcher.__init__(self, mgme_dir = '', *arg, **opt) 624 625 self.options['timeout'] = 1 # time authorize to answer question [0 is no time limit]
626
627 - def change_principal_cmd(self, name):
628 if name == 'MadGraph': 629 self.cmd = MGcmd.MadGraphCmdWeb 630 elif name == 'Loop': 631 self.cmd = LoopCmd.LoopInterfaceWeb 632 else: 633 raise MadGraph5Error, 'Type of interface not valid' 634 635 if __debug__: 636 self.debug_link_to_command()
637
638 - def do_shell(self, *args):
639 raise Exception
640
641 - def finalize(self, nojpeg):
642 """Finalize web generation""" 643 644 self.cmd.finalize(self, nojpeg, online = True)
645 646 # Generate a new amplitude
647 - def do_generate(self, line):
648 """Generate an amplitude for a given process""" 649 650 try: 651 Switcher.do_generate(self, line) 652 except: 653 # put the stop logo on the web 654 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg') 655 raise
656 657 # Add a process to the existing multiprocess definition
658 - def do_add(self, line):
659 """Generate an amplitude for a given process and add to 660 existing amplitudes 661 syntax: 662 """ 663 try: 664 Switcher.do_add(self, line) 665 except: 666 # put the stop logo on the web 667 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg') 668 raise
669 670 # Use the cluster file for the configuration
671 - def set_configuration(self, config_path=None, final=False):
672 673 """Force to use the web configuration file only""" 674 config_path = pjoin(os.environ['MADGRAPH_BASE'], 'mg5_configuration.txt') 675 return Switcher.set_configuration(self, config_path=config_path, final=final)
676
677 - def do_save(self, line, check=True, **opt):
678 """Save information to file""" 679 680 if check: 681 self.check_save([]) 682 raise #useless but full security 683 684 args = self.split_arg(line) 685 if args[0] != 'options': 686 Switcher.do_save(self, line,check, opt) 687 else: 688 # put default options since 689 # in the web the local file is not used 690 # in download the default file is more usefull 691 files.cp(pjoin(MG5DIR,'input','mg5_configuration.txt'), args[1])
692
693 - def do_install(self, line):
694 """block all install""" 695 return
696