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