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