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
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 if hasattr(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 misc.sprint(line)
535 try:
536 usecmd.exec_cmd(line)
537 except Exception, error:
538 misc.sprint('Command %s fails with msg: %s'%(str(line), \
539 str(error)))
540 pass
541 launch = self.cmd_int.define_child_cmd_interface(
542 usecmd, interface=False)
543
544 option_line = ' '.join([' --%s' % opt for opt in self.options.keys() \
545 if self.options[opt] and not opt in ['cluster', 'multicore', 'name', 'appl_start_grid']])
546 if self.options['name']:
547 option_line += ' --name %s' % self.options['name']
548 if 'appl_start_grid' in self.options and self.options['appl_start_grid']:
549 option_line += ' --appl_start_grid %s' % self.options['appl_start_grid']
550 command = 'launch ' + self.run_mode + ' ' + option_line
551
552 if mode == "1":
553 command += " -c"
554 elif mode == "2":
555 command += " -m"
556 usecmd.nb_core = int(nb_node)
557 try:
558 os.remove('ME5_debug')
559 except:
560 pass
561 launch.run_cmd(command)
562 launch.run_cmd('quit')
563
564
565
566
567
569 """A class to launch MadEvent run"""
570
571 - def __init__(self, running_dir, cmd_int , unit='pb', **option):
572 """ initialize the StandAlone Version"""
573
574 ExtLauncher.__init__(self, cmd_int, running_dir, './Cards', **option)
575
576 self.pythia = cmd_int.options['pythia-pgs_path']
577 self.delphes = cmd_int.options['delphes_path'],
578 self.options = cmd_int.options
579
580 assert hasattr(self, 'cluster')
581 assert hasattr(self, 'multicore')
582 assert hasattr(self, 'name')
583 assert hasattr(self, 'shell')
584
585 self.unit = unit
586
587 if self.cluster:
588 self.cluster = 1
589 if self.multicore:
590 self.cluster = 2
591
592 self.cards = []
593
594
595 if self.name == '':
596 self.name = me_cmd.MadEventCmd.find_available_run_name(self.running_dir)
597
599 """launch the main program"""
600
601
602 mode = str(self.cluster)
603 nb_node = 1
604 if mode == "2":
605 import multiprocessing
606 max_node = multiprocessing.cpu_count()
607 if max_node == 1:
608 logger.warning('Only one core is detected on your computer! Pass in single machine')
609 self.cluster = 0
610 self.launch_program()
611 return
612 elif max_node == 2:
613 nb_node = 2
614 elif not self.force:
615 nb_node = self.ask('How many cores do you want to use?', max_node, range(2,max_node+1))
616 else:
617 nb_node=max_node
618
619 import madgraph.interface.madevent_interface as ME
620
621 stdout_level = self.cmd_int.options['stdout_level']
622 if self.shell:
623 usecmd = ME.MadEventCmdShell(me_dir=self.running_dir, options=self.options)
624 else:
625 usecmd = ME.MadEventCmd(me_dir=self.running_dir, options=self.options)
626 usecmd.pass_in_web_mode()
627
628 set_cmd = [l for l in self.cmd_int.history if l.strip().startswith('set')]
629 all_options = usecmd.options_configuration.keys() + usecmd.options_madgraph.keys() + usecmd.options_madevent.keys()
630 for line in set_cmd:
631 arg = line.split()
632 if arg[1] not in all_options:
633 continue
634 try:
635 usecmd.do_set(line[3:], log=False)
636 except usecmd.InvalidCmd:
637 pass
638 usecmd.do_set('stdout_level %s' % stdout_level,log=False)
639
640 launch = self.cmd_int.define_child_cmd_interface(
641 usecmd, interface=False)
642
643 if self.unit == 'pb':
644 command = 'generate_events %s' % self.name
645 else:
646 warning_text = '''\
647 This command will create a new param_card with the computed width.
648 This param_card makes sense only if you include all processes for
649 the computation of the width. For more efficient width computation:
650 see arXiv:1402.1178.'''
651 logger.warning(warning_text)
652
653 command = 'generate_events %s' % self.name
654 if mode == "1":
655 command += " --cluster"
656 elif mode == "2":
657 command += " --nb_core=%s" % nb_node
658
659 if self.force:
660 command+= " -f"
661
662 if self.laststep:
663 command += ' --laststep=%s' % self.laststep
664 if self.reweight:
665 command += ' -R '
666 if self.madspin:
667 command += ' -M '
668
669
670 try:
671 os.remove('ME5_debug')
672 except:
673 pass
674
675 launch.run_cmd(command)
676 launch.run_cmd('quit')
677
678 if os.path.exists('ME5_debug'):
679 return True
680
681
682 path = os.path.join(self.running_dir, 'SubProcesses', 'results.dat')
683 if not os.path.exists(path):
684 logger.error('Generation failed (no results.dat file found)')
685 return
686 fsock = open(path)
687 line = fsock.readline()
688 cross, error = line.split()[0:2]
689
690 logger.info('more information in %s'
691 % os.path.join(self.running_dir, 'index.html'))
692
693
695 """A class to launch Pythia8 run"""
696
697 - def __init__(self, running_dir, cmd_int, **option):
698 """ initialize launching Pythia 8"""
699
700 running_dir = os.path.join(running_dir, 'examples')
701 ExtLauncher.__init__(self, cmd_int, running_dir, '.', **option)
702 self.cards = []
703
705 """ ask for pythia-pgs/delphes run """
706
707
708 date_file_list = []
709 for file in glob.glob(os.path.join(self.running_dir,'main_*_*.cc')):
710
711
712
713 stats = os.stat(file)
714
715
716
717 lastmod_date = time.localtime(stats[8])
718 date_file_list.append((lastmod_date, os.path.split(file)[-1]))
719
720 if not date_file_list:
721 raise MadGraph5Error, 'No Pythia output found'
722
723 date_file_list.sort()
724 date_file_list.reverse()
725 files = [d[1] for d in date_file_list]
726
727 answer = ''
728 answer = self.ask('Select a main file to run:', files[0], files)
729
730 self.cards.append(answer)
731
732 self.executable = self.cards[-1].replace(".cc","")
733
734
735 if self.name == '':
736 for i in range(1000):
737 path = os.path.join(self.running_dir, '',
738 '%s_%02i.log' % (self.executable, i))
739 if not os.path.exists(path):
740 self.name = '%s_%02i.log' % (self.executable, i)
741 break
742
743 if self.name == '':
744 raise MadGraph5Error, 'too many runs in this directory'
745
746
747 models = glob.glob(os.path.join(self.running_dir,os.path.pardir,
748 "Processes_*"))
749 models = [os.path.split(m)[-1].replace("Processes_","") for m in models]
750
751 models.sort(key=len)
752 models.reverse()
753 model_dir = ""
754 for model in models:
755 if self.executable.replace("main_", "").startswith(model):
756 model_dir = "Processes_%s" % model
757 break
758 if model_dir:
759 self.model = model
760 self.model_dir = os.path.realpath(os.path.join(self.running_dir,
761 os.path.pardir,
762 model_dir))
763 self.cards.append(os.path.join(self.model_dir,
764 "param_card_%s.dat" % model))
765
767 """launch the main program"""
768
769
770 print "Running make for pythia8 directory"
771 misc.compile(cwd=os.path.join(self.running_dir, os.path.pardir), mode='cpp')
772 if self.model_dir:
773 print "Running make in %s" % self.model_dir
774 misc.compile(cwd=self.model_dir, mode='cpp')
775
776 makefile = self.executable.replace("main_","Makefile_")
777 print "Running make with %s" % makefile
778 misc.compile(arg=['-f', makefile], cwd=self.running_dir, mode='cpp')
779
780 print "Running " + self.executable
781
782 output = open(os.path.join(self.running_dir, self.name), 'w')
783 if not self.executable.startswith('./'):
784 self.executable = os.path.join(".", self.executable)
785 subprocess.call([self.executable], stdout = output, stderr = output,
786 cwd=self.running_dir)
787
788
789 path = os.path.join(self.running_dir, self.name)
790 pydoc.pager(open(path).read())
791
792 print "Output of the run is found at " + \
793 os.path.realpath(os.path.join(self.running_dir, self.name))
794
795
796 open_file = misc.open_file
797