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