1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from __future__ import absolute_import
18 from __future__ import print_function
19 import logging
20 import os
21 import pydoc
22 import subprocess
23 import time
24 from six.moves import range
25 start=time.time()
26 import madgraph.iolibs.files as files
27 import madgraph.interface.madevent_interface as me_cmd
28 import madgraph.various.misc as misc
29 import madgraph.various.process_checks as process_checks
30 import madgraph.various.banner as banner_mod
31
32
33
34 pjoin = os.path.join
35
36
37 logger = logging.getLogger('cmdprint.ext_program')
38
39
40
42 """ Generic Class for executing external program """
43
44 program_dir = ''
45 executable = ''
46
47 force = False
48
49 - def __init__(self, cmd, running_dir, card_dir='', **options):
50 """ initialize an object """
51
52 self.running_dir = running_dir
53 self.card_dir = os.path.join(self.running_dir, card_dir)
54 self.cmd_int = cmd
55 if 'force' in options:
56 self.force = options['force']
57
58
59 for key,value in options.items():
60 setattr(self, key, value)
61
62 self.cards = []
63
81
82
84 """ aditional way to prepare the run"""
85 pass
86
88 """launch the main program"""
89 subprocess.call([self.executable], cwd=self.running_dir)
90
96
97
98
103
104 - def ask(self, question, default, choices=[], path_msg=None):
105 """nice handling of question"""
106
107 if not self.force:
108 return self.cmd_int.ask(question, default, choices=choices,
109 path_msg=path_msg, fct_timeout=self.timeout_fct)
110 else:
111 return str(default)
112
113
140
142 """ A class to launch a simple Standalone test """
143
144 - def __init__(self, cmd_int, running_dir, **options):
145 """ initialize the StandAlone Version """
146
147 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options)
148 self.cards = ['param_card.dat','MadLoopParams.dat']
149
151 """ Possible preparatory actions to take."""
152 pass
153
194
196 """launch the main program"""
197 evaluator = process_checks.LoopMatrixElementTimer
198 sub_path = os.path.join(self.running_dir, 'SubProcesses')
199 for path in os.listdir(sub_path):
200 if path.startswith('P') and \
201 os.path.isdir(os.path.join(sub_path, path)):
202 shell_name = path.split('_')[1]+' > '+path.split('_')[2]
203 curr_path = os.path.join(sub_path, path)
204 infos = {}
205 logger.info("Initializing process %s."%shell_name)
206 nps = me_cmd.MadLoopInitializer.run_initialization(
207 curr_path, sub_path, infos,
208 req_files = ['HelFilter.dat','LoopFilter.dat'])
209 if nps == None:
210 raise MadGraph5Error("MadLoop could not initialize the process %s"\
211 %shell_name)
212 logger.debug(("MadLoop initialization performed for %s"+\
213 " using %d PS points (%s)")\
214 %(shell_name,abs(nps),\
215 'double precision' if nps>0 else 'quadruple precision'))
216
217 self.treat_input_file('PS.input', default='n',
218 msg='Phase-space point for process %s.'%shell_name,\
219 dir_path=curr_path)
220
221
222 me_cmd.MadLoopInitializer.fix_PSPoint_in_check(sub_path,
223 read_ps = os.path.isfile(os.path.join(curr_path, 'PS.input')),
224 npoints = 1, mu_r=-1.0)
225
226 t1, t2, ram_usage = me_cmd.MadLoopInitializer.make_and_run(curr_path)
227 if t1==None or t2==None:
228 raise MadGraph5Error("Error while running process %s."\
229 %shell_name)
230 try:
231 rFile=open(os.path.join(curr_path,'result.dat'), 'r')
232 except IOError:
233 raise MadGraph5Error("Could not find result file %s."%\
234 str(os.path.join(curr_path,'result.dat')))
235
236 result = evaluator.parse_check_output(rFile,format='dict')
237 for line in self.format_res_string(result, shell_name):
238 if isinstance(line, str):
239 logger.info(line)
240 elif isinstance(line,tuple):
241 logger.info(line[0],line[1])
242
251
252 so_order_names = res['Split_Orders_Names']
253
254 def format_so_orders(so_orders):
255 return ' '.join(['%s=%d'%(so_order_names[i],so_orders[i]) for i in
256 range(len(so_orders))])
257
258 ASCII_bar = ('|'+''.join(['='*96]),main_color)
259
260 ret_code_h = res['return_code']//100
261 ret_code_t = (res['return_code']-100*ret_code_h)//10
262 ret_code_u = res['return_code']%10
263 StabilityOutput=[]
264 if ret_code_h==1:
265 if ret_code_t==3 or ret_code_t==4:
266 StabilityOutput.append('| Unknown numerical stability because '+\
267 'MadLoop is in the initialization stage.')
268 else:
269 StabilityOutput.append('| Unknown numerical stability, check '+\
270 'CTRunMode value in MadLoopParams.dat.')
271 elif ret_code_h==2:
272 StabilityOutput.append('| Stable kinematic configuration (SPS).')
273 elif ret_code_h==3:
274 StabilityOutput.append('| Unstable kinematic configuration (UPS).')
275 StabilityOutput.append('| Quadruple precision rescue successful.')
276 elif ret_code_h==4:
277 StabilityOutput.append('| Exceptional kinematic configuration (EPS).')
278 StabilityOutput.append('| Both double and quadruple precision'+\
279 ' computations are unstable.')
280
281 if ret_code_t==2 or ret_code_t==4:
282 StabilityOutput.append('| Quadruple precision was used for this'+\
283 'computation.')
284 if ret_code_h!=1:
285 if res['accuracy']>0.0:
286 StabilityOutput.append('| Estimated relative accuracy = %.1e'\
287 %res['accuracy'])
288 elif res['accuracy']==0.0:
289 StabilityOutput.append('| Estimated relative accuracy = %.1e'\
290 %res['accuracy']+' (i.e. beyond double precision)')
291 else:
292 StabilityOutput.append('| Estimated accuracy could not be '+\
293 'computed for an unknown reason.')
294
295 PS_point_spec = ['|| Phase-Space point specification (E,px,py,pz)','|']
296 PS_point_spec.append('\n'.join(['| '+' '.join(['%s'%\
297 special_float_format(pi) for pi in pmom]) for pmom in res['res_p']]))
298 PS_point_spec.append('|')
299
300 str_lines=[]
301
302 notZeroBorn=True
303 if res['export_format']!='LoopInduced' and len(so_order_names) and \
304 len([1 for k in res['Born_kept'] if k])==0:
305 notZeroBorn = False
306 str_lines.append(
307 ("| /!\\ There is no Born contribution for the squared orders specified in "+
308 "the process definition/!\\",'$MG:color:RED'))
309
310 if res['export_format']=='Default' and notZeroBorn:
311 str_lines.extend(['\n',ASCII_bar,
312 ('|| Results for process %s'%shell_name,main_color),
313 ASCII_bar]+PS_point_spec+StabilityOutput+[
314 '|',
315 ('|| Total(*) Born contribution (GeV^%d):'%res['gev_pow'],main_color),
316 ('| Born = %s'%special_float_format(res['born']),main_color),
317 ('|| Total(*) virtual contribution normalized with born*alpha_S/(2*pi):',main_color),
318 ('| Finite = %s'%special_float_format(res['finite']),main_color),
319 ('| Single pole = %s'%special_float_format(res['1eps']),main_color),
320 ('| Double pole = %s'%special_float_format(res['2eps']),main_color)])
321 elif res['export_format']=='LoopInduced' and notZeroBorn:
322 str_lines.extend(['\n',ASCII_bar,
323 ('|| Results for process %s (Loop-induced)'%shell_name,main_color),
324 ASCII_bar]+PS_point_spec+StabilityOutput+[
325 '|',
326 ('|| Loop amplitude squared, must be finite:',main_color),
327 ('| Finite = %s'%special_float_format(res['finite']),main_color),
328 '|(| Pole residues, indicated only for checking purposes: )',
329 '|( Single pole = %s )'%special_float_format(res['1eps']),
330 '|( Double pole = %s )'%special_float_format(res['2eps'])])
331
332 if (len(res['Born_SO_Results'])+len(res['Born_SO_Results']))>0:
333 if notZeroBorn:
334 str_lines.append(
335 ("| (*) The results above sum all starred contributions below",main_color))
336
337 str_lines.append('|')
338 if not notZeroBorn:
339 str_lines.append(
340 ("| The Born contributions below are computed but do not match these squared "+
341 "orders constraints",main_color))
342
343 if len(res['Born_SO_Results'])==1:
344 str_lines.append('|| All Born contributions are of split orders *(%s)'\
345 %format_so_orders(res['Born_SO_Results'][0][0]))
346 elif len(res['Born_SO_Results'])>1:
347 for i, bso_contrib in enumerate(res['Born_SO_Results']):
348 str_lines.append('|| Born contribution of split orders %s(%s) = %s'\
349 %('*' if res['Born_kept'][i] else ' ',
350 format_so_orders(bso_contrib[0]),
351 special_float_format(bso_contrib[1]['BORN'])))
352
353 if len(so_order_names):
354 str_lines.append('|')
355
356 if len(res['Loop_SO_Results'])==1:
357 str_lines.append('|| All virtual contributions are of split orders *(%s)'\
358 %format_so_orders(res['Loop_SO_Results'][0][0]))
359 elif len(res['Loop_SO_Results'])>1:
360 if not notZeroBorn:
361 str_lines.append(
362 ("| The coupling order combinations matching the squared order"+
363 " constraints are marked with a star",main_color))
364 for i, lso_contrib in enumerate(res['Loop_SO_Results']):
365 str_lines.append('|| Virtual contribution of split orders %s(%s):'\
366 %('*' if res['Loop_kept'][i] else ' ',
367 format_so_orders(lso_contrib[0])))
368 str_lines.append('| Accuracy = %.1e'%\
369 lso_contrib[1]['ACC']),
370 str_lines.append('| Finite = %s'%\
371 special_float_format(lso_contrib[1]['FIN'])),
372 if res['export_format']=='LoopInduced':
373 str_lines.append('|( Single pole = %s )'%\
374 special_float_format(lso_contrib[1]['1EPS']))
375 str_lines.append('|( Double pole = %s )'%\
376 special_float_format(lso_contrib[1]['2EPS']))
377 else:
378 str_lines.append('| Single pole = %s'%\
379 special_float_format(lso_contrib[1]['1EPS']))
380 str_lines.append('| Double pole = %s'%\
381 special_float_format(lso_contrib[1]['2EPS']))
382 str_lines.extend([ASCII_bar,'\n'])
383
384 return str_lines
385
387 """ A class to launch a simple Standalone test """
388
389 - def __init__(self, cmd_int, running_dir, **options):
390 """ initialize the StandAlone Version"""
391
392 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options)
393 self.cards = ['param_card.dat']
394
395
397 """launch the main program"""
398 sub_path = os.path.join(self.running_dir, 'SubProcesses')
399 for path in os.listdir(sub_path):
400 if path.startswith('P') and \
401 os.path.isdir(os.path.join(sub_path, path)):
402 cur_path = os.path.join(sub_path, path)
403
404 misc.compile(cwd=cur_path, mode='unknown')
405
406 subprocess.call(['./check'], cwd=cur_path)
407
409 """ A class to launch a simple Standalone test """
410
411
412 - def __init__(self, cmd_int, running_dir, **options):
413 """ initialize the StandAlone Version"""
414 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **options)
415 self.options = cmd_int.options
416
418 """launch the main program"""
419
420 import madgraph.interface.madweight_interface as MW
421
422 mode = str(self.cluster)
423 nb_node = 1
424 if mode == "2":
425 import multiprocessing
426 max_node = multiprocessing.cpu_count()
427 if max_node == 1:
428 logger.warning('Only one core is detected on your computer! Pass in single machine')
429 self.cluster = 0
430 self.launch_program()
431 return
432 elif max_node == 2:
433 nb_node = 2
434 elif not self.force:
435 nb_node = self.ask('How many core do you want to use?', max_node, list(range(2,max_node+1)))
436 else:
437 nb_node=max_node
438
439 import madgraph.interface.madevent_interface as ME
440
441 stdout_level = self.cmd_int.options['stdout_level']
442 if self.shell:
443 usecmd = MW.MadWeightCmdShell(me_dir=self.running_dir, options=self.options)
444 else:
445 usecmd = MW.MadWeightCmd(me_dir=self.running_dir, options=self.options)
446 usecmd.pass_in_web_mode()
447
448 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')]
449 for line in set_cmd:
450 try:
451 usecmd.do_set(line[3:], log=False)
452 except Exception:
453 pass
454
455 usecmd.do_set('stdout_level %s' % stdout_level,log=False)
456
457 launch = self.cmd_int.define_child_cmd_interface(
458 usecmd, interface=False)
459
460 command = 'launch'
461 if mode == "1":
462 command += " --cluster"
463 elif mode == "2":
464 command += " --nb_core=%s" % nb_node
465
466 if self.force:
467 command+= " -f"
468 if self.laststep:
469 command += ' --laststep=%s' % self.laststep
470
471 try:
472 os.remove('ME5_debug')
473 except:
474 pass
475 launch.run_cmd(command)
476 launch.run_cmd('quit')
477
478 if os.path.exists('ME5_debug'):
479 return True
480
481
482
484 """A class to launch MadEvent run"""
485
486 - def __init__(self, running_dir, cmd_int, run_mode='', unit='pb', **option):
487 """ initialize the StandAlone Version"""
488
489 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option)
490
491
492 self.options = option
493 assert hasattr(self, 'cluster')
494 assert hasattr(self, 'multicore')
495 assert hasattr(self, 'name')
496 assert hasattr(self, 'shell')
497
498 self.unit = unit
499 self.run_mode = run_mode
500
501 if self.cluster or option['cluster']:
502 self.cluster = 1
503 if self.multicore or option['multicore']:
504 self.cluster = 2
505
506 self.cards = []
507
508
509 if self.name == '':
510 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
511
513 """launch the main program"""
514
515
516 mode = str(self.cluster)
517 nb_node = 1
518 if mode == "2":
519 import multiprocessing
520 max_node = multiprocessing.cpu_count()
521 if max_node == 1:
522 logger.warning('Only one core is detected on your computer! Pass in single machine')
523 self.cluster = 0
524 self.launch_program()
525 return
526 elif max_node == 2:
527 nb_node = 2
528 elif not self.force:
529 nb_node = self.ask('How many cores do you want to use?', max_node, list(range(2,max_node+1)))
530 else:
531 nb_node=max_node
532
533 import madgraph.interface.amcatnlo_run_interface as run_int
534
535 if hasattr(self, 'shell') and self.shell:
536 usecmd = run_int.aMCatNLOCmdShell(me_dir=self.running_dir, options = self.cmd_int.options)
537 else:
538 usecmd = run_int.aMCatNLOCmd(me_dir=self.running_dir, options = self.cmd_int.options)
539
540
541 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')]
542 all_options = list(usecmd.options_configuration.keys()) + list(usecmd.options_madgraph.keys()) + list(usecmd.options_madevent.keys())
543 for line in set_cmd:
544 arg = line.split()
545 if arg[1] not in all_options:
546 continue
547 try:
548 usecmd.exec_cmd(line)
549 except Exception as error:
550 misc.sprint('Command %s fails with msg: %s'%(str(line), \
551 str(error)))
552 pass
553 launch = self.cmd_int.define_child_cmd_interface(
554 usecmd, interface=False)
555
556 option_line = ' '.join([' --%s' % opt for opt in self.options.keys() \
557 if self.options[opt] and not opt in ['cluster', 'multicore', 'name', 'appl_start_grid','shell']])
558 if self.options['name']:
559 option_line += ' --name %s' % self.options['name']
560 if 'appl_start_grid' in self.options and self.options['appl_start_grid']:
561 option_line += ' --appl_start_grid %s' % self.options['appl_start_grid']
562 command = 'launch ' + self.run_mode + ' ' + option_line
563
564 if mode == "1":
565 command += " -c"
566 elif mode == "2":
567 command += " -m"
568 usecmd.nb_core = int(nb_node)
569 try:
570 os.remove('ME5_debug')
571 except:
572 pass
573 launch.run_cmd(command)
574 launch.run_cmd('quit')
575
576
577
578
579
581 """A class to launch MadEvent run"""
582
583 - def __init__(self, running_dir, cmd_int , unit='pb', **option):
584 """ initialize the StandAlone Version"""
585
586 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option)
587
588 self.pythia = cmd_int.options['pythia-pgs_path']
589 self.delphes = cmd_int.options['delphes_path'],
590 self.options = cmd_int.options
591
592 assert hasattr(self, 'cluster')
593 assert hasattr(self, 'multicore')
594 assert hasattr(self, 'name')
595 assert hasattr(self, 'shell')
596
597 self.unit = unit
598
599 if self.cluster:
600 self.cluster = 1
601 if self.multicore:
602 self.cluster = 2
603
604 self.cards = []
605
606
607 if self.name == '':
608 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
609
611 """launch the main program"""
612
613
614 mode = str(self.cluster)
615 nb_node = 1
616 if mode == "2":
617 import multiprocessing
618 max_node = multiprocessing.cpu_count()
619 if max_node == 1:
620 logger.warning('Only one core is detected on your computer! Pass in single machine')
621 self.cluster = 0
622 self.launch_program()
623 return
624 elif max_node == 2:
625 nb_node = 2
626 elif not self.force:
627 nb_node = self.ask('How many cores do you want to use?', max_node, list(range(2,max_node+1)))
628 else:
629 nb_node=max_node
630
631 import madgraph.interface.madevent_interface as ME
632
633 stdout_level = self.cmd_int.options['stdout_level']
634
635 with ME.MadEventCmd.RunWebHandling(self.running_dir):
636 if self.shell:
637 usecmd = ME.MadEventCmdShell(me_dir=self.running_dir, options=self.options, force_run=True)
638 else:
639 usecmd = ME.MadEventCmd(me_dir=self.running_dir, options=self.options, force_run=True)
640 usecmd.pass_in_web_mode()
641
642 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')]
643 all_options = list(usecmd.options_configuration.keys()) + list(usecmd.options_madgraph.keys()) + list(usecmd.options_madevent.keys())
644 for line in set_cmd:
645 arg = line.split()
646 if arg[1] not in all_options:
647 continue
648 try:
649 usecmd.do_set(line[3:], log=False)
650 except usecmd.InvalidCmd:
651 pass
652 usecmd.do_set('stdout_level %s' % stdout_level,log=False)
653
654 launch = self.cmd_int.define_child_cmd_interface(
655 usecmd, interface=False)
656
657 if self.unit == 'pb':
658 command = 'generate_events %s' % self.name
659 else:
660 warning_text = '''\
661 Note that since 2.3. The launch for 1>N pass in event generation
662 For efficient width computation see arXiv:1402.1178.'''
663 logger.warning(warning_text)
664 command = 'generate_events %s' % self.name
665 if mode == "1":
666 command += " --cluster"
667 elif mode == "2":
668 command += " --nb_core=%s" % nb_node
669
670 if self.force:
671 command+= " -f"
672
673 if self.laststep:
674 command += ' --laststep=%s' % self.laststep
675 if self.reweight:
676 command += ' -R '
677 if self.madspin:
678 command += ' -M '
679
680
681 try:
682 os.remove('ME5_debug')
683 except:
684 pass
685
686 launch.run_cmd(command)
687 launch.run_cmd('quit')
688
689 if os.path.exists('ME5_debug'):
690 return True
691
692
693 path = os.path.join(self.running_dir, 'SubProcesses', 'results.dat')
694 if not os.path.exists(path):
695 logger.error('Generation failed (no results.dat file found)')
696 return
697 fsock = open(path)
698 line = fsock.readline()
699 cross, error = line.split()[0:2]
700
701 logger.info('more information in %s'
702 % os.path.join(self.running_dir, 'index.html'))
703
704
706 """A class to launch Pythia8 run"""
707
708 - def __init__(self, running_dir, cmd_int, **option):
709 """ initialize launching Pythia 8"""
710
711 running_dir = os.path.join(running_dir, 'examples')
712 ExtLauncher.__init__(self, cmd_int, running_dir, '.', **option)
713 self.cards = []
714
716 """ ask for pythia-pgs/delphes run """
717
718
719 date_file_list = []
720 for file in misc.glob('main_*_*.cc', self.running_dir):
721
722
723
724 stats = os.stat(file)
725
726
727
728 lastmod_date = time.localtime(stats[8])
729 date_file_list.append((lastmod_date, os.path.split(file)[-1]))
730
731 if not date_file_list:
732 raise MadGraph5Error('No Pythia output found')
733
734 date_file_list.sort()
735 date_file_list.reverse()
736 files = [d[1] for d in date_file_list]
737
738 answer = ''
739 answer = self.ask('Select a main file to run:', files[0], files)
740
741 self.cards.append(answer)
742
743 self.executable = self.cards[-1].replace(".cc","")
744
745
746 if self.name == '':
747 for i in range(1000):
748 path = os.path.join(self.running_dir, '',
749 '%s_%02i.log' % (self.executable, i))
750 if not os.path.exists(path):
751 self.name = '%s_%02i.log' % (self.executable, i)
752 break
753
754 if self.name == '':
755 raise MadGraph5Error('too many runs in this directory')
756
757
758 models = misc.glob("Processes_*", pjoin(self.running_dir,os.path.pardir))
759 models = [os.path.split(m)[-1].replace("Processes_","") for m in models]
760
761 models.sort(key=len)
762 models.reverse()
763 model_dir = ""
764 for model in models:
765 if self.executable.replace("main_", "").startswith(model):
766 model_dir = "Processes_%s" % model
767 break
768 if model_dir:
769 self.model = model
770 self.model_dir = os.path.realpath(os.path.join(self.running_dir,
771 os.path.pardir,
772 model_dir))
773 self.cards.append(os.path.join(self.model_dir,
774 "param_card_%s.dat" % model))
775
777 """launch the main program"""
778
779
780 print("Running make for pythia8 directory")
781 if self.model_dir:
782 print("Running make in %s" % self.model_dir)
783 misc.compile(cwd=self.model_dir, mode='cpp')
784
785 makefile = self.executable.replace("main_","Makefile_")
786 print("Running make with %s" % makefile)
787 misc.compile(arg=['-f', makefile], cwd=self.running_dir, mode='cpp')
788
789 print("Running " + self.executable)
790
791 output = open(os.path.join(self.running_dir, self.name), 'w')
792 if not self.executable.startswith('./'):
793 self.executable = os.path.join(".", self.executable)
794 subprocess.call([self.executable], stdout = output, stderr = output,
795 cwd=self.running_dir)
796
797
798 path = os.path.join(self.running_dir, self.name)
799 pydoc.pager(open(path).read())
800
801 print("Output of the run is found at " + \
802 os.path.realpath(os.path.join(self.running_dir, self.name)))
803
804
805 open_file = misc.open_file
806