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