1
2
3
4
5
6
7
8
9
10
11
12
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
41 """ Generic Class for executing external program """
42
43 program_dir = ''
44 executable = ''
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
55 for key,value in options.items():
56 setattr(self, key, value)
57
58 self.cards = []
59
68
69
71 """ aditional way to prepare the run"""
72 pass
73
75 """launch the main program"""
76 subprocess.call([self.executable], cwd=self.running_dir)
77
79 """edit a file"""
80
81 path = os.path.realpath(path)
82 open_file(path)
83
84
85
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
127
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
138 """ Possible preparatory actions to take."""
139 pass
140
181
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
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
208
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
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
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
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
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
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
392 misc.compile(cwd=cur_path, mode='unknown')
393
394 subprocess.call(['./check'], cwd=cur_path)
395
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
406 """launch the main program"""
407
408 import madgraph.interface.madweight_interface as MW
409
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
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
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
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
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
497 if self.name == '':
498 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
499
501 """launch the main program"""
502
503
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
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
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
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
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
596 if self.name == '':
597 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
598
600 """launch the main program"""
601
602
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
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
641 launch = self.cmd_int.define_child_cmd_interface(
642 usecmd, interface=False)
643
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
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
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
706 """ ask for pythia-pgs/delphes run """
707
708
709 date_file_list = []
710 for file in glob.glob(os.path.join(self.running_dir,'main_*_*.cc')):
711
712
713
714 stats = os.stat(file)
715
716
717
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
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
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
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
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
768 """launch the main program"""
769
770
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
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
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
797 open_file = misc.open_file
798