1
2
3
4
5
6
7
8
9
10
11
12
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
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')
56 """ Helping class containing all the switching routine """
57
58 - def __init__(self, main='MadGraph', *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
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
81 """redefine all the command to call directly the appropriate child"""
82
83 correct = True
84
85
86 overwritable = []
87
88 self.to_preserve = [key for key,method in Switcher.__dict__.items() if
89 hasattr(method, '__call__') ]
90 self.to_preserve += ['do_shell', 'help_shell', 'complete_shell']
91
92 ff = open(pjoin(os.getcwd(), 'additional_command'), 'w')
93
94 for key in dir(self):
95
96 if key in self.to_preserve:
97 continue
98 if not (key.startswith('do_') or key.startswith('complete_') or \
99 key.startswith('help_') or key.startswith('check_') or \
100 key in overwritable):
101 continue
102 text = """\
103 def %(key)s(self, *args, **opts):
104 return self.cmd.%(key)s(self, *args, **opts)
105
106 """ % {'key': key}
107 logger.warning("""Command %s not define in the Master.
108 The line to add to the master_interface.py are written in 'additional_command' file""" % key)
109 ff.write(text)
110 correct = False
111
112
113
114
115 define = {}
116 for mother in MasterCmd.__mro__:
117 if mother.__name__ in ['Cmd', 'BasicCmd', 'ExtendedCmd']:
118 continue
119
120
121 for data in mother.__dict__:
122
123 if data in Switcher.__dict__ or data.startswith('__'):
124 continue
125 if data in MasterCmd.__dict__:
126
127 continue
128 if data not in define:
129 define[data] = mother.__name__
130 else:
131 logger.warning('%s define in %s and in %s but not in Switcher.' % (data, define[data], mother.__name__))
132 correct = False
133
134
135 define = {}
136 for mother in MasterCmdWeb.__mro__:
137 if mother.__name__ in ['Cmd', 'BasicCmd', 'ExtendedCmd']:
138 continue
139
140 for data in mother.__dict__:
141
142 if data in Switcher.__dict__ or data.startswith('__'):
143 continue
144 if data in MasterCmdWeb.__dict__:
145
146 continue
147 if data not in define:
148 define[data] = mother.__name__
149 else:
150 logger.warning('%s define in %s and in %s but not in Switcher.' % (data, define[data], mother.__name__))
151 correct = False
152
153 if not correct:
154 raise Exception, 'The Cmd interface has dangerous features. Please see previous warnings and correct those.'
155
156
157
158 @staticmethod
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
165
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
170
171
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
183
184 if len(orders)>0:
185 return ('NLO','all',orders)
186 else:
187 return ('tree',None,[])
188 else:
189 return ('tree',None,[])
190
191
192
193 - def do_add(self, line, *args, **opts):
211
212 - def do_check(self, line, *args, **opts):
231
251
265
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
278
279
280
281
282
283
284 - def export(self, *args, **opts):
286
289
292
295
298
301
304
307
310
313
316
319
322
325
328
331
334
337
338 - def check_history(self, *args, **opts):
339 return self.cmd.check_history(self, *args, **opts)
340
343
346
349
352
355
358
361
364
367
370
373
376
379
382
385
388
391
392 - def complete_history(self, *args, **opts):
393 return self.cmd.complete_history(self, *args, **opts)
394
397
400
403
406
409
412
415
418
421
423 """Not in help """
424 return self.cmd.do_switch(self, *args, **opts)
425
426 - def do_EOF(self, *args, **opts):
428
431
434
437
440
441 - def do_history(self, *args, **opts):
442 return self.cmd.do_history(self, *args, **opts)
443
446
448 args = cmd.Cmd.split_arg(line)
449
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
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
473
476
479
482
483 - def do_set(self, *args, **opts):
485
488
491
494
497
500
503
506
509
510 - def help_history(self, *args, **opts):
511 return self.cmd.help_history(self, *args, **opts)
512
515
518
521
524
527
530
533
536
539
542
545
548
551
554
557
560
561 -class MasterCmd(Switcher, LoopCmd.LoopInterface, amcatnloCmd.aMCatNLOInterface, cmd.CmdShell):
562
563 - def __init__(self, main='MadGraph', *args, **opt):
574
578
589
608
609
610 -class MasterCmdWeb(MGcmd.MadGraphCmdWeb, Switcher, LoopCmd.LoopInterfaceWeb):
611
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
623 Switcher.__init__(self, mgme_dir = '', *arg, **opt)
624
625 self.options['timeout'] = 1
626
637
640
642 """Finalize web generation"""
643
644 self.cmd.finalize(self, nojpeg, online = True)
645
646
648 """Generate an amplitude for a given process"""
649
650 try:
651 Switcher.do_generate(self, line)
652 except:
653
654 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg')
655 raise
656
657
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
667 files.cp(self._export_dir+'/HTML/stop.jpg',self._export_dir+'/HTML/card.jpg')
668 raise
669
670
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):
692
694 """block all install"""
695 return
696