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