Package madgraph :: Package various :: Module banner
[hide private]
[frames] | no frames]

Source Code for Module madgraph.various.banner

   1  ################################################################################ 
   2  # 
   3  # Copyright (c) 2011 The MadGraph5_aMC@NLO Development team and Contributors 
   4  # 
   5  # This file is a part of the MadGraph5_aMC@NLO project, an application which  
   6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
   7  # high-energy processes in the Standard Model and beyond. 
   8  # 
   9  # It is subject to the MadGraph5_aMC@NLO license which should accompany this  
  10  # distribution. 
  11  # 
  12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
  13  # 
  14  ################################################################################ 
  15  """A File for splitting""" 
  16   
  17  from __future__ import division 
  18  import copy 
  19  import logging 
  20  import numbers 
  21  import os 
  22  import sys 
  23  import re 
  24  import math 
  25   
  26  pjoin = os.path.join 
  27   
  28  try: 
  29      import madgraph 
  30  except ImportError: 
  31      MADEVENT = True 
  32      from internal import MadGraph5Error, InvalidCmd 
  33      import internal.file_writers as file_writers 
  34      import internal.files as files 
  35      import internal.check_param_card as param_card_reader 
  36      import internal.misc as misc 
  37      MEDIR = os.path.split(os.path.dirname(os.path.realpath( __file__ )))[0] 
  38      MEDIR = os.path.split(MEDIR)[0] 
  39  else: 
  40      MADEVENT = False 
  41      import madgraph.various.misc as misc 
  42      import madgraph.iolibs.file_writers as file_writers 
  43      import madgraph.iolibs.files as files  
  44      import models.check_param_card as param_card_reader 
  45      from madgraph import MG5DIR, MadGraph5Error, InvalidCmd 
  46   
  47   
  48  logger = logging.getLogger('madevent.cards') 
 151       
152 - def change_lhe_version(self, version):
153 """change the lhe version associate to the banner""" 154 155 version = float(version) 156 if version < 3: 157 version = 1 158 elif version > 3: 159 raise Exception, "Not Supported version" 160 self.lhe_version = version
161
162 - def get_cross(self):
163 """return the cross-section of the file""" 164 165 if "init" not in self: 166 misc.sprint(self.keys()) 167 raise Exception 168 169 text = self["init"].split('\n') 170 cross = 0 171 for line in text: 172 s = line.split() 173 if len(s)==4: 174 cross += float(s[0]) 175 return cross
176 177 178
179 - def modify_init_cross(self, cross):
180 """modify the init information with the associate cross-section""" 181 182 assert isinstance(cross, dict) 183 # assert "all" in cross 184 assert "init" in self 185 186 all_lines = self["init"].split('\n') 187 new_data = [] 188 new_data.append(all_lines[0]) 189 for i in range(1, len(all_lines)): 190 line = all_lines[i] 191 split = line.split() 192 if len(split) == 4: 193 xsec, xerr, xmax, pid = split 194 else: 195 new_data += all_lines[i:] 196 break 197 if int(pid) not in cross: 198 raise Exception 199 pid = int(pid) 200 ratio = cross[pid]/float(xsec) 201 line = " %+13.7e %+13.7e %+13.7e %i" % \ 202 (float(cross[pid]), ratio* float(xerr), ratio*float(xmax), pid) 203 new_data.append(line) 204 self['init'] = '\n'.join(new_data)
205
206 - def scale_init_cross(self, ratio):
207 """modify the init information with the associate scale""" 208 209 assert "init" in self 210 211 all_lines = self["init"].split('\n') 212 new_data = [] 213 new_data.append(all_lines[0]) 214 for i in range(1, len(all_lines)): 215 line = all_lines[i] 216 split = line.split() 217 if len(split) == 4: 218 xsec, xerr, xmax, pid = split 219 else: 220 new_data += all_lines[i:] 221 break 222 pid = int(pid) 223 224 line = " %+13.7e %+13.7e %+13.7e %i" % \ 225 (ratio*float(xsec), ratio* float(xerr), ratio*float(xmax), pid) 226 new_data.append(line) 227 self['init'] = '\n'.join(new_data)
228
229 - def load_basic(self, medir):
230 """ Load the proc_card /param_card and run_card """ 231 232 self.add(pjoin(medir,'Cards', 'param_card.dat')) 233 self.add(pjoin(medir,'Cards', 'run_card.dat')) 234 if os.path.exists(pjoin(medir, 'SubProcesses', 'procdef_mg5.dat')): 235 self.add(pjoin(medir,'SubProcesses', 'procdef_mg5.dat')) 236 self.add(pjoin(medir,'Cards', 'proc_card_mg5.dat')) 237 else: 238 self.add(pjoin(medir,'Cards', 'proc_card.dat'))
239 240
241 - def change_seed(self, seed):
242 """Change the seed value in the banner""" 243 # 0 = iseed 244 p = re.compile(r'''^\s*\d+\s*=\s*iseed''', re.M) 245 new_seed_str = " %s = iseed" % seed 246 self['mgruncard'] = p.sub(new_seed_str, self['mgruncard'])
247
248 - def add_generation_info(self, cross, nb_event):
249 """add info on MGGeneration""" 250 251 text = """ 252 # Number of Events : %s 253 # Integrated weight (pb) : %s 254 """ % (nb_event, cross) 255 self['MGGenerationInfo'] = text
256 257 ############################################################################ 258 # SPLIT BANNER 259 ############################################################################
260 - def split(self, me_dir, proc_card=True):
261 """write the banner in the Cards directory. 262 proc_card argument is present to avoid the overwrite of proc_card 263 information""" 264 265 for tag, text in self.items(): 266 if tag == 'mgversion': 267 continue 268 if not proc_card and tag in ['mg5proccard','mgproccard']: 269 continue 270 if not self.tag_to_file[tag]: 271 continue 272 ff = open(pjoin(me_dir, 'Cards', self.tag_to_file[tag]), 'w') 273 ff.write(text) 274 ff.close()
275 276 277 ############################################################################ 278 # WRITE BANNER 279 ############################################################################
280 - def check_pid(self, pid2label):
281 """special routine removing width/mass of particles not present in the model 282 This is usefull in case of loop model card, when we want to use the non 283 loop model.""" 284 285 if not hasattr(self, 'param_card'): 286 self.charge_card('slha') 287 288 for tag in ['mass', 'decay']: 289 block = self.param_card.get(tag) 290 for data in block: 291 pid = data.lhacode[0] 292 if pid not in pid2label.keys(): 293 block.remove((pid,))
294 295 296 ############################################################################ 297 # WRITE BANNER 298 ############################################################################
299 - def write(self, output_path, close_tag=True, exclude=[]):
300 """write the banner""" 301 302 if isinstance(output_path, str): 303 ff = open(output_path, 'w') 304 else: 305 ff = output_path 306 307 if MADEVENT: 308 header = open(pjoin(MEDIR, 'Source', 'banner_header.txt')).read() 309 else: 310 header = open(pjoin(MG5DIR,'Template', 'LO', 'Source', 'banner_header.txt')).read() 311 312 if not self.lhe_version: 313 self.lhe_version = self.get('run_card', 'lhe_version', default=1.0) 314 if float(self.lhe_version) < 3: 315 self.lhe_version = 1.0 316 317 ff.write(header % { 'version':float(self.lhe_version)}) 318 319 320 for tag in [t for t in self.ordered_items if t in self.keys()]: 321 if tag in exclude: 322 continue 323 capitalized_tag = self.capitalized_items[tag] if tag in self.capitalized_items else tag 324 ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \ 325 {'tag':capitalized_tag, 'text':self[tag].strip()}) 326 for tag in [t for t in self.keys() if t not in self.ordered_items]: 327 if tag in ['init'] or tag in exclude: 328 continue 329 capitalized_tag = self.capitalized_items[tag] if tag in self.capitalized_items else tag 330 ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \ 331 {'tag':capitalized_tag, 'text':self[tag].strip()}) 332 333 if not '/header' in exclude: 334 ff.write('</header>\n') 335 336 if 'init' in self and not 'init' in exclude: 337 text = self['init'] 338 ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \ 339 {'tag':'init', 'text':text.strip()}) 340 if close_tag: 341 ff.write('</LesHouchesEvents>\n') 342 return ff
343 344 345 ############################################################################ 346 # BANNER 347 ############################################################################
348 - def add(self, path, tag=None):
349 """Add the content of the file to the banner""" 350 351 if not tag: 352 card_name = os.path.basename(path) 353 if 'param_card' in card_name: 354 tag = 'slha' 355 elif 'run_card' in card_name: 356 tag = 'MGRunCard' 357 elif 'pythia_card' in card_name: 358 tag = 'MGPythiaCard' 359 elif 'pgs_card' in card_name: 360 tag = 'MGPGSCard' 361 elif 'delphes_card' in card_name: 362 tag = 'MGDelphesCard' 363 elif 'delphes_trigger' in card_name: 364 tag = 'MGDelphesTrigger' 365 elif 'proc_card_mg5' in card_name: 366 tag = 'MG5ProcCard' 367 elif 'proc_card' in card_name: 368 tag = 'MGProcCard' 369 elif 'procdef_mg5' in card_name: 370 tag = 'MGProcCard' 371 elif 'shower_card' in card_name: 372 tag = 'MGShowerCard' 373 elif 'madspin_card' in card_name: 374 tag = 'madspin' 375 elif 'FO_analyse_card' in card_name: 376 tag = 'foanalyse' 377 else: 378 raise Exception, 'Impossible to know the type of the card' 379 380 self.add_text(tag.lower(), open(path).read())
381
382 - def add_text(self, tag, text):
383 """Add the content of the file to the banner""" 384 385 if tag == 'param_card': 386 tag = 'slha' 387 elif tag == 'run_card': 388 tag = 'mgruncard' 389 elif tag == 'proc_card': 390 tag = 'mg5proccard' 391 elif tag == 'shower_card': 392 tag = 'mgshowercard' 393 elif tag == 'FO_analyse_card': 394 tag = 'foanalyse' 395 396 self[tag.lower()] = text
397 398
399 - def charge_card(self, tag):
400 """Build the python object associated to the card""" 401 402 if tag == 'param_card': 403 tag = 'slha' 404 elif tag == 'run_card': 405 tag = 'mgruncard' 406 elif tag == 'proc_card': 407 tag = 'mg5proccard' 408 elif tag == 'shower_card': 409 tag = 'mgshowercard' 410 elif tag == 'FO_analyse_card': 411 tag = 'foanalyse' 412 413 assert tag in ['slha', 'mgruncard', 'mg5proccard', 'mgshowercard', 'foanalyse'], 'invalid card %s' % tag 414 415 if tag == 'slha': 416 param_card = self[tag].split('\n') 417 self.param_card = param_card_reader.ParamCard(param_card) 418 return self.param_card 419 elif tag == 'mgruncard': 420 self.run_card = RunCard(self[tag]) 421 return self.run_card 422 elif tag == 'mg5proccard': 423 proc_card = self[tag].split('\n') 424 self.proc_card = ProcCard(proc_card) 425 return self.proc_card 426 elif tag =='mgshowercard': 427 shower_content = self[tag] 428 if MADEVENT: 429 import internal.shower_card as shower_card 430 else: 431 import madgraph.various.shower_card as shower_card 432 self.shower_card = shower_card.ShowerCard(shower_content, True) 433 # set testing to false (testing = true allow to init using 434 # the card content instead of the card path" 435 self.shower_card.testing = False 436 return self.shower_card 437 elif tag =='foanalyse': 438 analyse_content = self[tag] 439 if MADEVENT: 440 import internal.FO_analyse_card as FO_analyse_card 441 else: 442 import madgraph.various.FO_analyse_card as FO_analyse_card 443 # set testing to false (testing = true allow to init using 444 # the card content instead of the card path" 445 self.FOanalyse_card = FO_analyse_card.FOAnalyseCard(analyse_content, True) 446 self.FOanalyse_card.testing = False 447 return self.FOanalyse_card
448 449
450 - def get_detail(self, tag, *arg, **opt):
451 """return a specific """ 452 453 if tag in ['param_card', 'param']: 454 tag = 'slha' 455 attr_tag = 'param_card' 456 elif tag in ['run_card', 'run']: 457 tag = 'mgruncard' 458 attr_tag = 'run_card' 459 elif tag == 'proc_card': 460 tag = 'mg5proccard' 461 attr_tag = 'proc_card' 462 elif tag == 'model': 463 tag = 'mg5proccard' 464 attr_tag = 'proc_card' 465 arg = ('model',) 466 elif tag == 'generate': 467 tag = 'mg5proccard' 468 attr_tag = 'proc_card' 469 arg = ('generate',) 470 elif tag == 'shower_card': 471 tag = 'mgshowercard' 472 attr_tag = 'shower_card' 473 assert tag in ['slha', 'mgruncard', 'mg5proccard', 'shower_card'], '%s not recognized' % tag 474 475 if not hasattr(self, attr_tag): 476 self.charge_card(attr_tag) 477 478 card = getattr(self, attr_tag) 479 if len(arg) == 1: 480 if tag == 'mg5proccard': 481 try: 482 return card.info[arg[0]] 483 except KeyError, error: 484 if 'default' in opt: 485 return opt['default'] 486 else: 487 raise 488 try: 489 return card[arg[0]] 490 except KeyError: 491 if 'default' in opt: 492 return opt['default'] 493 else: 494 raise 495 elif len(arg) == 2 and tag == 'slha': 496 try: 497 return card[arg[0]].get(arg[1:]) 498 except KeyError: 499 if 'default' in opt: 500 return opt['default'] 501 else: 502 raise 503 elif len(arg) == 0: 504 return card 505 else: 506 raise Exception, "Unknow command"
507 508 #convenient alias 509 get = get_detail 510
511 - def set(self, card, *args):
512 """modify one of the cards""" 513 514 if tag == 'param_card': 515 tag = 'slha' 516 attr_tag = 'param_card' 517 elif tag == 'run_card': 518 tag = 'mgruncard' 519 attr_tag = 'run_card' 520 elif tag == 'proc_card': 521 tag = 'mg5proccard' 522 attr_tag = 'proc_card' 523 elif tag == 'model': 524 tag = 'mg5proccard' 525 attr_tag = 'proc_card' 526 arg = ('model',) 527 elif tag == 'generate': 528 tag = 'mg5proccard' 529 attr_tag = 'proc_card' 530 arg = ('generate',) 531 elif tag == 'shower_card': 532 tag = 'mgshowercard' 533 attr_tag = 'shower_card' 534 assert tag in ['slha', 'mgruncard', 'mg5proccard', 'shower_card'], 'not recognized' 535 536 if not hasattr(self, attr_tag): 537 self.charge_card(attr_tag) 538 539 card = getattr(self, attr_tag) 540 if len(args) ==2: 541 if tag == 'mg5proccard': 542 card.info[args[0]] = args[-1] 543 else: 544 card[args[0]] = args[1] 545 else: 546 card[args[:-1]] = args[-1]
547 548 549 @misc.multiple_try()
550 - def add_to_file(self, path, seed=None, out=None):
551 """Add the banner to a file and change the associate seed in the banner""" 552 553 if seed is not None: 554 self.set("run_card", "iseed", seed) 555 556 if not out: 557 path_out = "%s.tmp" % path 558 else: 559 path_out = out 560 561 ff = self.write(path_out, close_tag=False, 562 exclude=['MGGenerationInfo', '/header', 'init']) 563 ff.write("## END BANNER##\n") 564 if self.lhe_version >= 3: 565 #add the original content 566 [ff.write(line) if not line.startswith("<generator name='MadGraph5_aMC@NLO'") 567 else ff.write("<generator name='MadGraph5_aMC@NLO' version='%s'>" % self['mgversion'][:-1]) 568 for line in open(path)] 569 else: 570 [ff.write(line) for line in open(path)] 571 ff.write("</LesHouchesEvents>\n") 572 ff.close() 573 if out: 574 os.remove(path) 575 else: 576 files.mv(path_out, path)
577
578 579 580 -def split_banner(banner_path, me_dir, proc_card=True):
581 """a simple way to split a banner""" 582 583 banner = Banner(banner_path) 584 banner.split(me_dir, proc_card)
585
586 -def recover_banner(results_object, level, run=None, tag=None):
587 """as input we receive a gen_crossxhtml.AllResults object. 588 This define the current banner and load it 589 """ 590 591 if not run: 592 try: 593 _run = results_object.current['run_name'] 594 _tag = results_object.current['tag'] 595 except Exception: 596 return Banner() 597 else: 598 _run = run 599 if not tag: 600 try: 601 _tag = results_object[run].tags[-1] 602 except Exception,error: 603 return Banner() 604 else: 605 _tag = tag 606 607 path = results_object.path 608 banner_path = pjoin(path,'Events',run,'%s_%s_banner.txt' % (run, tag)) 609 610 if not os.path.exists(banner_path): 611 if level != "parton" and tag != _tag: 612 return recover_banner(results_object, level, _run, results_object[_run].tags[0]) 613 # security if the banner was remove (or program canceled before created it) 614 return Banner() 615 banner = Banner(banner_path) 616 617 618 619 if level == 'pythia': 620 if 'mgpythiacard' in banner: 621 del banner['mgpythiacard'] 622 if level in ['pythia','pgs','delphes']: 623 for tag in ['mgpgscard', 'mgdelphescard', 'mgdelphestrigger']: 624 if tag in banner: 625 del banner[tag] 626 return banner
627
628 -class InvalidRunCard(InvalidCmd):
629 pass
630
631 -class ProcCard(list):
632 """Basic Proccard object""" 633 634 history_header = \ 635 '#************************************************************\n' + \ 636 '#* MadGraph5_aMC@NLO *\n' + \ 637 '#* *\n' + \ 638 "#* * * *\n" + \ 639 "#* * * * * *\n" + \ 640 "#* * * * * 5 * * * * *\n" + \ 641 "#* * * * * *\n" + \ 642 "#* * * *\n" + \ 643 "#* *\n" + \ 644 "#* *\n" + \ 645 "%(info_line)s" +\ 646 "#* *\n" + \ 647 "#* The MadGraph5_aMC@NLO Development Team - Find us at *\n" + \ 648 "#* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \ 649 '#* *\n' + \ 650 '#************************************************************\n' + \ 651 '#* *\n' + \ 652 '#* Command File for MadGraph5_aMC@NLO *\n' + \ 653 '#* *\n' + \ 654 '#* run as ./bin/mg5_aMC filename *\n' + \ 655 '#* *\n' + \ 656 '#************************************************************\n' 657 658 659 660
661 - def __init__(self, init=None):
662 """ initialize a basic proc_card""" 663 self.info = {'model': 'sm', 'generate':None, 664 'full_model_line':'import model sm'} 665 list.__init__(self) 666 if init: 667 self.read(init)
668 669
670 - def read(self, init):
671 """read the proc_card and save the information""" 672 673 if isinstance(init, str): #path to file 674 init = file(init, 'r') 675 676 store_line = '' 677 for line in init: 678 line = line.strip() 679 if line.endswith('\\'): 680 store_line += line[:-1] 681 else: 682 self.append(store_line + line) 683 store_line = "" 684 if store_line: 685 raise Exception, "WRONG CARD FORMAT"
686 - def move_to_last(self, cmd):
687 """move an element to the last history.""" 688 for line in self[:]: 689 if line.startswith(cmd): 690 self.remove(line) 691 list.append(self, line)
692
693 - def append(self, line):
694 """"add a line in the proc_card perform automatically cleaning""" 695 696 line = line.strip() 697 cmds = line.split() 698 if len(cmds) == 0: 699 return 700 701 list.append(self, line) 702 703 # command type: 704 cmd = cmds[0] 705 706 if cmd == 'output': 707 # Remove previous outputs from history 708 self.clean(allow_for_removal = ['output'], keep_switch=True, 709 remove_bef_last='output') 710 elif cmd == 'generate': 711 # Remove previous generations from history 712 self.clean(remove_bef_last='generate', keep_switch=True, 713 allow_for_removal= ['generate', 'add process', 'output']) 714 self.info['generate'] = ' '.join(cmds[1:]) 715 elif cmd == 'add' and cmds[1] == 'process' and not self.info['generate']: 716 self.info['generate'] = ' '.join(cmds[2:]) 717 elif cmd == 'import': 718 if len(cmds) < 2: 719 return 720 if cmds[1].startswith('model'): 721 self.info['full_model_line'] = line 722 self.clean(remove_bef_last='import', keep_switch=True, 723 allow_for_removal=['generate', 'add process', 'add model', 'output']) 724 if cmds[1] == 'model': 725 self.info['model'] = cmds[2] 726 else: 727 self.info['model'] = None # not UFO model 728 elif cmds[1] == 'proc_v4': 729 #full cleaning 730 self[:] = []
731 732
733 - def clean(self, to_keep=['set','add','load'], 734 remove_bef_last=None, 735 to_remove=['open','display','launch', 'check','history'], 736 allow_for_removal=None, 737 keep_switch=False):
738 """Remove command in arguments from history. 739 All command before the last occurrence of 'remove_bef_last' 740 (including it) will be removed (but if another options tells the opposite). 741 'to_keep' is a set of line to always keep. 742 'to_remove' is a set of line to always remove (don't care about remove_bef_ 743 status but keep_switch acts.). 744 if 'allow_for_removal' is define only the command in that list can be 745 remove of the history for older command that remove_bef_lb1. all parameter 746 present in to_remove are always remove even if they are not part of this 747 list. 748 keep_switch force to keep the statement remove_bef_??? which changes starts 749 the removal mode. 750 """ 751 752 #check consistency 753 if __debug__ and allow_for_removal: 754 for arg in to_keep: 755 assert arg not in allow_for_removal 756 757 758 nline = -1 759 removal = False 760 #looping backward 761 while nline > -len(self): 762 switch = False # set in True when removal pass in True 763 764 #check if we need to pass in removal mode 765 if not removal and remove_bef_last: 766 if self[nline].startswith(remove_bef_last): 767 removal = True 768 switch = True 769 770 # if this is the switch and is protected pass to the next element 771 if switch and keep_switch: 772 nline -= 1 773 continue 774 775 # remove command in to_remove (whatever the status of removal) 776 if any([self[nline].startswith(arg) for arg in to_remove]): 777 self.pop(nline) 778 continue 779 780 # Only if removal mode is active! 781 if removal: 782 if allow_for_removal: 783 # Only a subset of command can be removed 784 if any([self[nline].startswith(arg) 785 for arg in allow_for_removal]): 786 self.pop(nline) 787 continue 788 elif not any([self[nline].startswith(arg) for arg in to_keep]): 789 # All command have to be remove but protected 790 self.pop(nline) 791 continue 792 793 # update the counter to pass to the next element 794 nline -= 1
795
796 - def __getattr__(self, tag, default=None):
797 if isinstance(tag, int): 798 list.__getattr__(self, tag) 799 elif tag == 'info' or tag == "__setstate__": 800 return default #for pickle 801 else: 802 return self.info[tag]
803
804 - def write(self, path):
805 """write the proc_card to a given path""" 806 807 fsock = open(path, 'w') 808 fsock.write(self.history_header) 809 for line in self: 810 while len(line) > 70: 811 sub, line = line[:70]+"\\" , line[70:] 812 fsock.write(sub+"\n") 813 else: 814 fsock.write(line+"\n")
815
816 817 -class ConfigFile(dict):
818 """ a class for storing/dealing with input file. 819 """ 820
821 - def __init__(self, finput=None):
822 """initialize a new instance. input can be an instance of MadLoopParam, 823 a file, a path to a file, or simply Nothing""" 824 825 if isinstance(finput, self.__class__): 826 dict.__init__(self, finput) 827 assert finput.__dict__.keys() 828 for key in finput.__dict__: 829 setattr(self, key, copy.copy(getattr(finput, key)) ) 830 return 831 else: 832 dict.__init__(self) 833 834 # Initialize it with all the default value 835 self.user_set = set() 836 self.lower_to_case = {} 837 self.default_setup() 838 839 840 841 # if input is define read that input 842 if isinstance(finput, (file, str)): 843 self.read(finput)
844 845
846 - def default_setup(self):
847 pass
848
849 - def __copy__(self):
850 return self.__class__(self)
851
852 - def __add__(self, other):
853 """define the sum""" 854 assert isinstance(other, dict) 855 base = self.__class__(self) 856 #base = copy.copy(self) 857 base.update((key.lower(),value) for key, value in other.items()) 858 return base
859
860 - def __radd__(self, other):
861 """define the sum""" 862 new = copy.copy(other) 863 new.update((key, value) for key, value in self.items()) 864 return new
865
866 - def __contains__(self, key):
867 return dict.__contains__(self, key.lower())
868
869 - def __iter__(self):
870 iter = super(ConfigFile, self).__iter__() 871 return (self.lower_to_case[name] for name in iter)
872
873 - def keys(self):
874 return [name for name in self]
875
876 - def items(self):
877 return [(self.lower_to_case[name], value) for name,value in \ 878 super(ConfigFile, self).items()]
879
880 - def __setitem__(self, name, value, change_userdefine=False):
881 """set the attribute and set correctly the type if the value is a string""" 882 if not len(self): 883 #Should never happen but when deepcopy/pickle 884 self.__init__() 885 886 name = name.strip() 887 # 1. Find the type of the attribute that we want 888 if name in self: 889 lower_name = name.lower() 890 targettype = type(self[name]) 891 else: 892 lower_name = name.lower() 893 logger.debug('Trying to add argument %s in %s. ' % (name, self.__class__.__name__) +\ 894 'This argument is not defined by default. Please consider to add it.') 895 logger.debug("Did you mean %s", [k for k in self.keys() if k.startswith(name[0].lower())]) 896 self.add_param(lower_name, self.format_variable(str(value), str, name)) 897 self.lower_to_case[lower_name] = name 898 if change_userdefine: 899 self.user_set.add(lower_name) 900 return 901 902 value = self.format_variable(value, targettype, name=name) 903 dict.__setitem__(self, lower_name, value) 904 if change_userdefine: 905 self.user_set.add(lower_name)
906
907 - def add_param(self, name, value):
908 """add a default parameter to the class""" 909 910 lower_name = name.lower() 911 if __debug__: 912 if lower_name in self: 913 raise Exception("Duplicate case for %s in %s" % (name,self.__class__)) 914 915 dict.__setitem__(self, lower_name, value) 916 self.lower_to_case[lower_name] = name
917 918 @staticmethod
919 - def format_variable(value, targettype, name="unknown"):
920 """assign the value to the attribute for the given format""" 921 922 if not isinstance(value, str): 923 # just have to check that we have the correct format 924 if isinstance(value, targettype): 925 pass # assignement at the end 926 elif isinstance(value, numbers.Number) and issubclass(targettype, numbers.Number): 927 try: 928 new_value = targettype(value) 929 except TypeError: 930 if value.imag/value.real<1e-12: 931 new_value = targettype(value.real) 932 else: 933 raise 934 if new_value == value: 935 value = new_value 936 else: 937 raise Exception, "Wrong input type for %s found %s and expecting %s for value %s" %\ 938 (name, type(value), targettype, value) 939 else: 940 raise Exception, "Wrong input type for %s found %s and expecting %s for value %s" %\ 941 (name, type(value), targettype, value) 942 else: 943 # We have a string we have to format the attribute from the string 944 if targettype == bool: 945 value = value.strip() 946 if value.lower() in ['0', '.false.', 'f', 'false']: 947 value = False 948 elif value.lower() in ['1', '.true.', 't', 'true']: 949 value = True 950 else: 951 raise Exception, "%s can not be mapped to True/False for %s" % (repr(value),name) 952 elif targettype == str: 953 value = value.strip() 954 if value.startswith('\'') and value.endswith('\''): 955 value = value[1:-1] 956 elif value.startswith('"') and value.endswith('"'): 957 value = value[1:-1] 958 elif targettype == int: 959 if value.isdigit(): 960 value = int(value) 961 elif value[1:].isdigit() and value[0] == '-': 962 value = int(value) 963 else: 964 try: 965 value = float(value.replace('d','e')) 966 except ValueError: 967 raise Exception, "%s can not be mapped to an integer" % value 968 try: 969 new_value = int(value) 970 except ValueError: 971 raise Exception, "%s can not be mapped to an integer" % value 972 else: 973 if value == new_value: 974 value = new_value 975 else: 976 raise Exception, "incorect input: %s need an integer for %s" % (value,name) 977 elif targettype == float: 978 value = value.replace('d','e') # pass from Fortran formatting 979 try: 980 value = float(value) 981 except ValueError: 982 raise Exception, "%s can not be mapped to a float" % value 983 else: 984 raise Exception, "type %s is not handle by MadLoopParam" % targettype 985 986 return value
987 988 989
990 - def __getitem__(self, name):
991 992 if __debug__: 993 if name.lower() not in self: 994 if name.lower() in [key.lower() for key in self] : 995 raise Exception, "Some key are not lower case %s. Invalid use of the class!"\ 996 % [key for key in self if key.lower() != key] 997 998 return dict.__getitem__(self, name.lower())
999 1000
1001 - def set(self, name, value, ifnotdefault=True, user=False):
1002 """convenient way to change attribute. 1003 ifnotdefault=False means that the value is NOT change is the value is not on default. 1004 user=True, means that the value will be marked as modified by the user 1005 (potentially preventing future change to the value) 1006 """ 1007 1008 # ifnotdefault=False -> we need to check if the user force a value. 1009 if not ifnotdefault: 1010 if name.lower() in self.user_set: 1011 #value modified by the user -> do nothing 1012 return 1013 1014 self.__setitem__(name, value, change_userdefine=user)
1015
1016 1017 1018 -class ProcCharacteristic(ConfigFile):
1019 """A class to handle information which are passed from MadGraph to the madevent 1020 interface.""" 1021
1022 - def default_setup(self):
1023 """initialize the directory to the default value""" 1024 1025 self.add_param('loop_induced', False) 1026 self.add_param('has_isr', False) 1027 self.add_param('has_fsr', False) 1028 self.add_param('nb_channel', 0) 1029 self.add_param('nexternal', 0) 1030 self.add_param('ninitial', 0) 1031 self.add_param('grouped_matrix', True) 1032 self.add_param('has_loops', False)
1033
1034 - def read(self, finput):
1035 """Read the input file, this can be a path to a file, 1036 a file object, a str with the content of the file.""" 1037 1038 if isinstance(finput, str): 1039 if "\n" in finput: 1040 finput = finput.split('\n') 1041 elif os.path.isfile(finput): 1042 finput = open(finput) 1043 else: 1044 raise Exception, "No such file %s" % finput 1045 1046 for line in finput: 1047 if '#' in line: 1048 line = line.split('#',1)[0] 1049 if not line: 1050 continue 1051 1052 if '=' in line: 1053 key, value = line.split('=',1) 1054 self[key.strip()] = value
1055
1056 - def write(self, outputpath):
1057 """write the file""" 1058 1059 template ="# Information about the process #\n" 1060 template +="#########################################\n" 1061 1062 fsock = open(outputpath, 'w') 1063 fsock.write(template) 1064 1065 for key, value in self.items(): 1066 fsock.write(" %s = %s \n" % (key, value)) 1067 1068 fsock.close()
1069
1070 1071 1072 1073 -class GridpackCard(ConfigFile):
1074 """an object for the GridpackCard""" 1075
1076 - def default_setup(self):
1077 """default value for the GridpackCard""" 1078 1079 self.add_param("GridRun", True) 1080 self.add_param("gevents", 2500) 1081 self.add_param("gseed", 1) 1082 self.add_param("ngran", -1)
1083
1084 - def read(self, finput):
1085 """Read the input file, this can be a path to a file, 1086 a file object, a str with the content of the file.""" 1087 1088 if isinstance(finput, str): 1089 if "\n" in finput: 1090 finput = finput.split('\n') 1091 elif os.path.isfile(finput): 1092 finput = open(finput) 1093 else: 1094 raise Exception, "No such file %s" % finput 1095 1096 for line in finput: 1097 line = line.split('#')[0] 1098 line = line.split('!')[0] 1099 line = line.split('=',1) 1100 if len(line) != 2: 1101 continue 1102 self[line[1].strip()] = line[0].replace('\'','').strip()
1103
1104 - def write(self, output_file, template=None):
1105 """Write the run_card in output_file according to template 1106 (a path to a valid run_card)""" 1107 1108 if not template: 1109 if not MADEVENT: 1110 template = pjoin(MG5DIR, 'Template', 'LO', 'Cards', 1111 'grid_card_default.dat') 1112 else: 1113 template = pjoin(MEDIR, 'Cards', 'grid_card_default.dat') 1114 1115 1116 text = "" 1117 for line in file(template,'r'): 1118 nline = line.split('#')[0] 1119 nline = nline.split('!')[0] 1120 comment = line[len(nline):] 1121 nline = nline.split('=') 1122 if len(nline) != 2: 1123 text += line 1124 elif nline[1].strip() in self: 1125 text += ' %s\t= %s %s' % (self[nline[1].strip()],nline[1], comment) 1126 else: 1127 logger.info('Adding missing parameter %s to current run_card (with default value)' % nline[1].strip()) 1128 text += line 1129 1130 fsock = open(output_file,'w') 1131 fsock.write(text) 1132 fsock.close()
1133
1134 -class RunCard(ConfigFile):
1135
1136 - def __new__(cls, finput=None):
1137 if cls is RunCard: 1138 if not finput: 1139 target_class = RunCardLO 1140 elif isinstance(finput, cls): 1141 target_class = finput.__class__ 1142 elif isinstance(finput, str): 1143 if '\n' not in finput: 1144 finput = open(finput).read() 1145 if 'fixed_QES_scale' in finput: 1146 target_class = RunCardNLO 1147 else: 1148 target_class = RunCardLO 1149 else: 1150 return None 1151 return super(RunCard, cls).__new__(target_class, finput) 1152 else: 1153 return super(RunCard, cls).__new__(cls, finput)
1154
1155 - def __init__(self, *args, **opts):
1156 1157 # The following parameter are updated in the defaultsetup stage. 1158 1159 #parameter for which no warning should be raised if not define 1160 self.hidden_param = [] 1161 # parameter which should not be hardcoded in the config file 1162 self.not_in_include = [] 1163 #some parameter have different name in fortran code 1164 self.fortran_name = {} 1165 #parameter which are not supported anymore. (no action on the code) 1166 self.legacy_parameter = {} 1167 #a list with all the cuts variable 1168 self.cuts_parameter = [] 1169 1170 1171 super(RunCard, self).__init__(*args, **opts)
1172
1173 - def add_param(self, name, value, fortran_name=None, include=True, 1174 hidden=False, legacy=False, cut=False):
1175 """ add a parameter to the card. value is the default value and 1176 defines the type (int/float/bool/str) of the input. 1177 fortran_name defines what is the associate name in the f77 code 1178 include defines if we have to put the value in the include file 1179 hidden defines if the parameter is expected to be define by the user. 1180 legacy:Parameter which is not used anymore (raise a warning if not default) 1181 cut: defines the list of cut parameter to allow to set them all to off. 1182 """ 1183 1184 super(RunCard, self).add_param(name, value) 1185 name = name.lower() 1186 if fortran_name: 1187 self.fortran_name[name] = fortran_name 1188 if not include: 1189 self.not_in_include.append(name) 1190 if hidden: 1191 self.hidden_param.append(name) 1192 if legacy: 1193 self.legacy_parameter[name] = value 1194 if include: 1195 self.not_in_include.append(name) 1196 if cut: 1197 self.cuts_parameter.append(name)
1198 1199 1200
1201 - def read(self, finput):
1202 """Read the input file, this can be a path to a file, 1203 a file object, a str with the content of the file.""" 1204 1205 if isinstance(finput, str): 1206 if "\n" in finput: 1207 finput = finput.split('\n') 1208 elif os.path.isfile(finput): 1209 finput = open(finput) 1210 else: 1211 raise Exception, "No such file %s" % finput 1212 1213 for line in finput: 1214 line = line.split('#')[0] 1215 line = line.split('!')[0] 1216 line = line.split('=',1) 1217 if len(line) != 2: 1218 continue 1219 value, name = line 1220 self.set( name, value, user=True)
1221
1222 - def write(self, output_file, template=None, python_template=False):
1223 """Write the run_card in output_file according to template 1224 (a path to a valid run_card)""" 1225 1226 to_write = set(self.user_set) 1227 if not template: 1228 raise Exception 1229 1230 if python_template and not to_write: 1231 text = file(template,'r').read() % self 1232 else: 1233 text = "" 1234 for line in file(template,'r'): 1235 nline = line.split('#')[0] 1236 nline = nline.split('!')[0] 1237 comment = line[len(nline):] 1238 nline = nline.split('=') 1239 if len(nline) != 2: 1240 text += line 1241 elif nline[1].strip() in self: 1242 if python_template: 1243 text += line % {nline[1].strip().lower(): self[nline[1].strip()]} 1244 else: 1245 text += ' %s\t= %s %s' % (self[nline[1].strip()],nline[1], comment) 1246 if nline[1].strip().lower() in to_write: 1247 to_write.remove(nline[1].strip().lower()) 1248 else: 1249 logger.info('Adding missing parameter %s to current run_card (with default value)' % nline[1].strip()) 1250 text += line 1251 1252 if to_write: 1253 text+="""#********************************************************************* 1254 # Additional parameter 1255 #********************************************************************* 1256 """ 1257 1258 for key in to_write: 1259 text += ' %s\t= %s # %s\n' % (self[key], key, 'hidden parameter') 1260 1261 if isinstance(output_file, str): 1262 fsock = open(output_file,'w') 1263 fsock.write(text) 1264 fsock.close() 1265 else: 1266 output_file.write(text)
1267 1268
1269 - def get_default(self, name, default=None, log_level=None):
1270 """return self[name] if exist otherwise default. log control if we 1271 put a warning or not if we use the default value""" 1272 1273 if name not in self.user_set: 1274 if log_level is None: 1275 if name.lower() in self.hidden_param: 1276 log_level = 10 1277 else: 1278 log_level = 20 1279 if not default: 1280 default = self[name] 1281 logger.log(log_level, 'run_card missed argument %s. Takes default: %s' 1282 % (name, default)) 1283 self[name] = default 1284 return default 1285 else: 1286 return self[name]
1287 1288 @staticmethod
1289 - def format(formatv, value):
1290 """for retro compatibility""" 1291 1292 logger.debug("please use f77_formatting instead of format") 1293 return self.f77_formatting(value, formatv=formatv)
1294 1295 @staticmethod
1296 - def f77_formatting(value, formatv=None):
1297 """format the variable into fortran. The type is detected by default""" 1298 1299 if not formatv: 1300 if isinstance(value, bool): 1301 formatv = 'bool' 1302 elif isinstance(value, int): 1303 formatv = 'int' 1304 elif isinstance(value, float): 1305 formatv = 'float' 1306 elif isinstance(value, str): 1307 formatv = 'str' 1308 else: 1309 logger.debug("unknow format for f77_formatting: %s" , value) 1310 formatv = 'str' 1311 else: 1312 assert formatv 1313 1314 if formatv == 'bool': 1315 if str(value) in ['1','T','.true.','True']: 1316 return '.true.' 1317 else: 1318 return '.false.' 1319 1320 elif formatv == 'int': 1321 try: 1322 return str(int(value)) 1323 except ValueError: 1324 fl = float(value) 1325 if int(fl) == fl: 1326 return str(int(fl)) 1327 else: 1328 raise 1329 1330 elif formatv == 'float': 1331 if isinstance(value, str): 1332 value = value.replace('d','e') 1333 return ('%.10e' % float(value)).replace('e','d') 1334 1335 elif formatv == 'str': 1336 return "'%s'" % value
1337 1338 1339
1340 - def write_include_file(self, output_file):
1341 """ """ 1342 1343 # ensure that all parameter are coherent and fix those if needed 1344 self.check_validity() 1345 1346 fsock = file_writers.FortranWriter(output_file) 1347 for key in self: 1348 if key in self.not_in_include: 1349 continue 1350 1351 #define the fortran name 1352 if key in self.fortran_name: 1353 fortran_name = self.fortran_name[key] 1354 else: 1355 fortran_name = key 1356 1357 #get the value with warning if the user didn't set it 1358 value = self.get_default(key) 1359 1360 line = '%s = %s \n' % (fortran_name, self.f77_formatting(value)) 1361 fsock.writelines(line) 1362 fsock.close()
1363
1365 """return a dictionary with the information needed to write 1366 the first line of the <init> block of the lhe file.""" 1367 1368 output = {} 1369 1370 def get_idbmup(lpp): 1371 """return the particle colliding pdg code""" 1372 if lpp in (1,2, -1,-2): 1373 return math.copysign(2212, lpp) 1374 elif lpp in (3,-3): 1375 return math.copysign(11, lpp) 1376 elif lpp == 0: 1377 logger.critical("Fail to write correct idbmup in the lhe file. Please correct those by hand") 1378 return 0 1379 else: 1380 return lpp
1381 1382 def get_pdf_id(pdf): 1383 if pdf == "lhapdf": 1384 return self["lhaid"] 1385 else: 1386 return {'none': 0, 'mrs02nl':20250, 'mrs02nn':20270, 'cteq4_m': 19150, 1387 'cteq4_l':19170, 'cteq4_d':19160, 'cteq5_m':19050, 1388 'cteq5_d':19060,'cteq5_l':19070,'cteq5m1':19051, 1389 'cteq6_m':10000,'cteq6_l':10041,'cteq6l1':10042, 1390 'nn23lo':246800,'nn23lo1':247000,'nn23nlo':244600 1391 }[pdf]
1392 1393 output["idbmup1"] = get_idbmup(self['lpp1']) 1394 output["idbmup2"] = get_idbmup(self['lpp2']) 1395 output["ebmup1"] = self["ebeam1"] 1396 output["ebmup2"] = self["ebeam2"] 1397 output["pdfgup1"] = 0 1398 output["pdfgup2"] = 0 1399 output["pdfsup1"] = get_pdf_id(self["pdlabel"]) 1400 output["pdfsup2"] = get_pdf_id(self["pdlabel"]) 1401 return output 1402
1403 1404 -class RunCardLO(RunCard):
1405 """an object to handle in a nice way the run_card infomration""" 1406
1407 - def default_setup(self):
1408 """default value for the run_card.dat""" 1409 1410 self.add_param("run_tag", "tag_1", include=False) 1411 self.add_param("gridpack", False) 1412 self.add_param("time_of_flight", -1.0, include=False) 1413 self.add_param("nevents", 10000) 1414 self.add_param("iseed", 0) 1415 self.add_param("lpp1", 1, fortran_name="lpp(1)") 1416 self.add_param("lpp2", 1, fortran_name="lpp(2)") 1417 self.add_param("ebeam1", 6500.0, fortran_name="ebeam(1)") 1418 self.add_param("ebeam2", 6500.0, fortran_name="ebeam(2)") 1419 self.add_param("polbeam1", 0.0, fortran_name="pb1") 1420 self.add_param("polbeam2", 0.0, fortran_name="pb2") 1421 self.add_param("pdlabel", "nn23lo1") 1422 self.add_param("lhaid", 230000, hidden=True) 1423 self.add_param("fixed_ren_scale", False) 1424 self.add_param("fixed_fac_scale", False) 1425 self.add_param("scale", 91.1880) 1426 self.add_param("dsqrt_q2fact1", 91.1880, fortran_name="sf1") 1427 self.add_param("dsqrt_q2fact2", 91.1880, fortran_name="sf2") 1428 self.add_param("dynamical_scale_choice", -1) 1429 1430 #matching 1431 self.add_param("scalefact", 1.0) 1432 self.add_param("ickkw", 0) 1433 self.add_param("highestmult", 1, fortran_name="nhmult") 1434 self.add_param("ktscheme", 1) 1435 self.add_param("alpsfact", 1) 1436 self.add_param("chcluster", False) 1437 self.add_param("pdfwgt", True) 1438 self.add_param("asrwgtflavor", 5) 1439 self.add_param("clusinfo", True) 1440 self.add_param("lhe_version", 3.0) 1441 #cut 1442 self.add_param("auto_ptj_mjj", True) 1443 self.add_param("bwcutoff", 15.0) 1444 self.add_param("cut_decays", False) 1445 self.add_param("nhel", 0, include=False) 1446 #pt cut 1447 self.add_param("ptj", 20.0, cut=True) 1448 self.add_param("ptb", 0.0, cut=True) 1449 self.add_param("pta", 10.0, cut=True) 1450 self.add_param("ptl", 10.0, cut=True) 1451 self.add_param("misset", 0.0, cut=True) 1452 self.add_param("ptheavy", 0.0, cut=True) 1453 self.add_param("ptonium", 1.0, legacy=True) 1454 self.add_param("ptjmax", -1.0, cut=True) 1455 self.add_param("ptbmax", -1.0, cut=True) 1456 self.add_param("ptamax", -1.0, cut=True) 1457 self.add_param("ptlmax", -1.0, cut=True) 1458 self.add_param("missetmax", -1.0, cut=True) 1459 # E cut 1460 self.add_param("ej", 0.0, cut=True) 1461 self.add_param("eb", 0.0, cut=True) 1462 self.add_param("ea", 0.0, cut=True) 1463 self.add_param("el", 0.0, cut=True) 1464 self.add_param("ejmax", -1.0, cut=True) 1465 self.add_param("ebmax", -1.0, cut=True) 1466 self.add_param("eamax", -1.0, cut=True) 1467 self.add_param("elmax", -1.0, cut=True) 1468 # Eta cut 1469 self.add_param("etaj", 5.0, cut=True) 1470 self.add_param("etab", -1.0, cut=True) 1471 self.add_param("etaa", 2.5, cut=True) 1472 self.add_param("etal", 2.5, cut=True) 1473 self.add_param("etaonium", 0.6, legacy=True) 1474 self.add_param("etajmin", 0.0, cut=True) 1475 self.add_param("etabmin", 0.0, cut=True) 1476 self.add_param("etaamin", 0.0, cut=True) 1477 self.add_param("etalmin", 0.0, cut=True) 1478 # DRJJ 1479 self.add_param("drjj", 0.4, cut=True) 1480 self.add_param("drbb", 0.0, cut=True) 1481 self.add_param("drll", 0.4, cut=True) 1482 self.add_param("draa", 0.4, cut=True) 1483 self.add_param("drbj", 0.0, cut=True) 1484 self.add_param("draj", 0.4, cut=True) 1485 self.add_param("drjl", 0.4, cut=True) 1486 self.add_param("drab", 0.0, cut=True) 1487 self.add_param("drbl", 0.0, cut=True) 1488 self.add_param("dral", 0.4, cut=True) 1489 self.add_param("drjjmax", -1.0, cut=True) 1490 self.add_param("drbbmax", -1.0, cut=True) 1491 self.add_param("drllmax", -1.0, cut=True) 1492 self.add_param("draamax", -1.0, cut=True) 1493 self.add_param("drbjmax", -1.0, cut=True) 1494 self.add_param("drajmax", -1.0, cut=True) 1495 self.add_param("drjlmax", -1.0, cut=True) 1496 self.add_param("drabmax", -1.0, cut=True) 1497 self.add_param("drblmax", -1.0, cut=True) 1498 self.add_param("dralmax", -1.0, cut=True) 1499 # invariant mass 1500 self.add_param("mmjj", 0.0, cut=True) 1501 self.add_param("mmbb", 0.0, cut=True) 1502 self.add_param("mmaa", 0.0, cut=True) 1503 self.add_param("mmll", 0.0, cut=True) 1504 self.add_param("mmjjmax", -1.0, cut=True) 1505 self.add_param("mmbbmax", -1.0, cut=True) 1506 self.add_param("mmaamax", -1.0, cut=True) 1507 self.add_param("mmllmax", -1.0, cut=True) 1508 self.add_param("mmnl", 0.0, cut=True) 1509 self.add_param("mmnlmax", -1.0, cut=True) 1510 #minimum/max pt for sum of leptons 1511 self.add_param("ptllmin", 0.0, cut=True) 1512 self.add_param("ptllmax", -1.0, cut=True) 1513 self.add_param("xptj", 0.0, cut=True) 1514 self.add_param("xptb", 0.0, cut=True) 1515 self.add_param("xpta", 0.0, cut=True) 1516 self.add_param("xptl", 0.0, cut=True) 1517 # ordered pt jet 1518 self.add_param("ptj1min", 0.0, cut=True) 1519 self.add_param("ptj1max", -1.0, cut=True) 1520 self.add_param("ptj2min", 0.0, cut=True) 1521 self.add_param("ptj2max", -1.0, cut=True) 1522 self.add_param("ptj3min", 0.0, cut=True) 1523 self.add_param("ptj3max", -1.0, cut=True) 1524 self.add_param("ptj4min", 0.0, cut=True) 1525 self.add_param("ptj4max", -1.0, cut=True) 1526 self.add_param("cutuse", 0, cut=True) 1527 # ordered pt lepton 1528 self.add_param("ptl1min", 0.0, cut=True) 1529 self.add_param("ptl1max", -1.0, cut=True) 1530 self.add_param("ptl2min", 0.0, cut=True) 1531 self.add_param("ptl2max", -1.0, cut=True) 1532 self.add_param("ptl3min", 0.0, cut=True) 1533 self.add_param("ptl3max", -1.0, cut=True) 1534 self.add_param("ptl4min", 0.0, cut=True) 1535 self.add_param("ptl4max", -1.0, cut=True) 1536 # Ht sum of jets 1537 self.add_param("htjmin", 0.0, cut=True) 1538 self.add_param("htjmax", -1.0, cut=True) 1539 self.add_param("ihtmin", 0.0, cut=True) 1540 self.add_param("ihtmax", -1.0, cut=True) 1541 self.add_param("ht2min", 0.0, cut=True) 1542 self.add_param("ht3min", 0.0, cut=True) 1543 self.add_param("ht4min", 0.0, cut=True) 1544 self.add_param("ht2max", -1.0, cut=True) 1545 self.add_param("ht3max", -1.0, cut=True) 1546 self.add_param("ht4max", -1.0, cut=True) 1547 # photon isolation 1548 self.add_param("ptgmin", 0.0, cut=True) 1549 self.add_param("r0gamma", 0.4) 1550 self.add_param("xn", 1.0) 1551 self.add_param("epsgamma", 1.0) 1552 self.add_param("isoem", True) 1553 self.add_param("xetamin", 0.0, cut=True) 1554 self.add_param("deltaeta", 0.0, cut=True) 1555 self.add_param("ktdurham", -1.0, fortran_name="kt_durham", cut=True) 1556 self.add_param("dparameter", 0.4, fortran_name="d_parameter", cut=True) 1557 self.add_param("maxjetflavor", 4) 1558 self.add_param("xqcut", 0.0, cut=True) 1559 self.add_param("use_syst", True) 1560 self.add_param("sys_scalefact", "0.5 1 2", include=False) 1561 self.add_param("sys_alpsfact", "0.5 1 2", include=False) 1562 self.add_param("sys_matchscale", "30 50", include=False) 1563 self.add_param("sys_pdf", "Ct10nlo.LHgrid", include=False) 1564 self.add_param("sys_scalecorrelation", -1, include=False) 1565 1566 #parameter not in the run_card by default 1567 self.add_param('gridrun', False, hidden=True) 1568 self.add_param('fixed_couplings', True, hidden=True) 1569 self.add_param('mc_grouped_subproc', True, hidden=True) 1570 self.add_param('xmtcentral', 0.0, hidden=True, fortran_name="xmtc") 1571 self.add_param('d', 1.0, hidden=True) 1572 self.add_param('gseed', 0, hidden=True, include=False) 1573 self.add_param('issgridfile', '', hidden=True) 1574 #job handling of the survey/ refine 1575 self.add_param('job_strategy', 0, hidden=True, include=False) 1576 self.add_param('survey_splitting', -1, hidden=True, include=False) 1577 self.add_param('refine_evt_by_job', -1, hidden=True, include=False)
1578 1579 1580 1581
1582 - def check_validity(self):
1583 """ """ 1584 #Make sure that nhel is only either 0 (i.e. no MC over hel) or 1585 #1 (MC over hel with importance sampling). In particular, it can 1586 #no longer be > 1. 1587 if 'nhel' not in self.user_set: 1588 raise InvalidRunCard, "Parameter nhel is not defined in the run_card." 1589 if self['nhel'] not in [1,0]: 1590 raise InvalidRunCard, "Parameter nhel can only be '0' or '1', "+\ 1591 "not %s." % self['nhel'] 1592 if int(self['maxjetflavor']) > 6: 1593 raise InvalidRunCard, 'maxjetflavor should be lower than 5! (6 is partly supported)' 1594 1595 # some cut need to be deactivated in presence of isolation 1596 if self['ptgmin'] > 0: 1597 if self['pta'] > 0: 1598 logger.warning('pta cut discarded since photon isolation is used') 1599 self['pta'] = 0.0 1600 if self['draj'] > 0: 1601 logger.warning('draj cut discarded since photon isolation is used') 1602 self['draj'] = 0.0 1603 1604 # special treatment for gridpack use the gseed instead of the iseed 1605 if self['gridrun']: 1606 self['iseed'] = self['gseed'] 1607 1608 #Some parameter need to be fixed when using syscalc 1609 if self['use_syst']: 1610 if self['scalefact'] != 1.0: 1611 logger.warning('Since use_syst=T, We change the value of \'scalefact\' to 1') 1612 self['scalefact'] = 1.0 1613 1614 # CKKW Treatment 1615 if self['ickkw'] > 0: 1616 if self['use_syst']: 1617 # some additional parameter need to be fixed for Syscalc + matching 1618 if self['alpsfact'] != 1.0: 1619 logger.warning('Since use_syst=T, We change the value of \'alpsfact\' to 1') 1620 self['alpsfact'] =1.0 1621 if self['maxjetflavor'] == 6: 1622 raise InvalidRunCard, 'maxjetflavor at 6 is NOT supported for matching!' 1623 if self['drjj'] != 0: 1624 logger.warning('Since icckw>0, We change the value of \'drjj\' to 0') 1625 self['drjj'] = 0 1626 if self['drjl'] != 0: 1627 logger.warning('Since icckw>0, We change the value of \'drjl\' to 0') 1628 self['drjl'] = 0 1629 if not self['auto_ptj_mjj']: 1630 if self['mmjj'] > self['xqcut']: 1631 logger.warning('mmjj > xqcut (and auto_ptj_mjj = F). MMJJ set to 0') 1632 self['mmjj'] = 0.0 1633 if self['ickkw'] == 2: 1634 # add warning if ckkw selected but the associate parameter are empty 1635 self.get_default('highestmult', log_level=20) 1636 self.get_default('issgridfile', 'issudgrid.dat', log_level=20) 1637 1638 # check validity of the pdf set 1639 possible_set = ['lhapdf','mrs02nl','mrs02nn', 'mrs0119','mrs0117','mrs0121','mrs01_j', 'mrs99_1','mrs99_2','mrs99_3','mrs99_4','mrs99_5','mrs99_6', 'mrs99_7','mrs99_8','mrs99_9','mrs9910','mrs9911','mrs9912', 'mrs98z1','mrs98z2','mrs98z3','mrs98z4','mrs98z5','mrs98ht', 'mrs98l1','mrs98l2','mrs98l3','mrs98l4','mrs98l5', 'cteq3_m','cteq3_l','cteq3_d', 'cteq4_m','cteq4_d','cteq4_l','cteq4a1','cteq4a2', 'cteq4a3','cteq4a4','cteq4a5','cteq4hj','cteq4lq', 'cteq5_m','cteq5_d','cteq5_l','cteq5hj','cteq5hq', 'cteq5f3','cteq5f4','cteq5m1','ctq5hq1','cteq5l1', 'cteq6_m','cteq6_d','cteq6_l','cteq6l1', 'nn23lo','nn23lo1','nn23nlo'] 1640 if self['pdlabel'] not in possible_set: 1641 raise InvalidRunCard, 'Invalid PDF set (argument of pdlabel): %s. Possible choice are:\n %s' % (self['pdlabel'], ', '.join(possible_set)) 1642 if self['pdlabel'] == 'lhapdf': 1643 #add warning if lhaid not define 1644 self.get_default('lhaid', log_level=20) 1645 1646 for name in self.legacy_parameter: 1647 if self[name] != self.legacy_parameter[name]: 1648 logger.warning("The parameter %s is not supported anymore this parameter will be ignored." % name)
1649 1650 1651 1652
1653 - def create_default_for_process(self, proc_characteristic, history, proc_def):
1654 """Rules 1655 process 1->N all cut set on off. 1656 loop_induced -> MC over helicity 1657 e+ e- beam -> lpp:0 ebeam:500 1658 p p beam -> set maxjetflavor automatically 1659 more than one multiplicity: ickkw=1 xqcut=30 use_syst=F 1660 """ 1661 1662 if proc_characteristic['loop_induced']: 1663 self['nhel'] = 1 1664 1665 if proc_characteristic['ninitial'] == 1: 1666 #remove all cut 1667 self.remove_all_cut() 1668 else: 1669 # check for beam_id 1670 beam_id = set() 1671 for proc in proc_def: 1672 for oneproc in proc: 1673 for leg in oneproc['legs']: 1674 if not leg['state']: 1675 beam_id.add(leg['id']) 1676 if any(i in beam_id for i in [1,-1,2,-2,3,-3,4,-4,5,-5,21,22]): 1677 maxjetflavor = max([4]+[abs(i) for i in beam_id if -7< i < 7]) 1678 self['maxjetflavor'] = maxjetflavor 1679 self['asrwgtflavor'] = maxjetflavor 1680 pass 1681 elif 11 in beam_id or -11 in beam_id: 1682 self['lpp1'] = 0 1683 self['lpp2'] = 0 1684 self['ebeam1'] = 500 1685 self['ebeam2'] = 500 1686 else: 1687 self['lpp1'] = 0 1688 self['lpp2'] = 0 1689 1690 # Check if need matching 1691 min_particle = 99 1692 max_particle = 0 1693 for proc in proc_def: 1694 min_particle = min(len(proc[0]['legs']), min_particle) 1695 max_particle = max(len(proc[0]['legs']), max_particle) 1696 if min_particle != max_particle: 1697 #take one of the process with min_particle 1698 for procmin in proc_def: 1699 if len(procmin[0]['legs']) != min_particle: 1700 continue 1701 else: 1702 idsmin = [l['id'] for l in procmin[0]['legs']] 1703 break 1704 matching = False 1705 for procmax in proc_def: 1706 if len(procmax[0]['legs']) != max_particle: 1707 continue 1708 idsmax = [l['id'] for l in procmax[0]['legs']] 1709 for i in idsmin: 1710 if i not in idsmax: 1711 continue 1712 else: 1713 idsmax.remove(i) 1714 for j in idsmax: 1715 if j not in [1,-1,2,-2,3,-3,4,-4,5,-5,21]: 1716 break 1717 else: 1718 # all are jet => matching is ON 1719 matching=True 1720 break 1721 1722 if matching: 1723 self['ickkw'] = 1 1724 self['xqcut'] = 30 1725 self['use_syst'] = False 1726 self['drjj'] = 0 1727 self['drjl'] = 0
1728 1729
1730 - def remove_all_cut(self):
1731 """remove all the cut""" 1732 1733 for name in self.cuts_parameter: 1734 targettype = type(self[name]) 1735 if targettype == bool: 1736 self[name] = False 1737 elif 'min' in name: 1738 self[name] = 0 1739 elif 'max' in name: 1740 self[name] = -1 1741 elif 'eta' in name: 1742 self[name] = -1 1743 else: 1744 self[name] = 0
1745
1746 - def write(self, output_file, template=None, python_template=False):
1747 """Write the run_card in output_file according to template 1748 (a path to a valid run_card)""" 1749 1750 if not template: 1751 if not MADEVENT: 1752 template = pjoin(MG5DIR, 'Template', 'LO', 'Cards', 1753 'run_card.dat') 1754 python_template = True 1755 else: 1756 template = pjoin(MEDIR, 'Cards', 'run_card_default.dat') 1757 python_template = False 1758 1759 super(RunCardLO, self).write(output_file, template=template, 1760 python_template=python_template)
1761
1762 1763 -class RunCardNLO(RunCard):
1764 """A class object for the run_card for a (aMC@)NLO pocess""" 1765 1766
1767 - def default_setup(self):
1768 """define the default value""" 1769 1770 self.add_param('run_tag', 'tag_1', include=False) 1771 self.add_param('nevents', 10000) 1772 self.add_param('req_acc', -1.0, include=False) 1773 self.add_param('nevt_job', -1, include=False) 1774 self.add_param('event_norm', 'average') 1775 #FO parameter 1776 self.add_param('req_acc_fo', 0.01, include=False) 1777 self.add_param('npoints_fo_grid', 5000, include=False) 1778 self.add_param('niters_fo_grid', 4, include=False) 1779 self.add_param('npoints_fo', 10000, include=False) 1780 self.add_param('niters_fo', 6, include=False) 1781 #seed and collider 1782 self.add_param('iseed', 0) 1783 self.add_param('lpp1', 1, fortran_name='lpp(1)') 1784 self.add_param('lpp2', 1, fortran_name='lpp(2)') 1785 self.add_param('ebeam1', 6500, fortran_name='ebeam(1)') 1786 self.add_param('ebeam2', 6500, fortran_name='ebeam(2)') 1787 self.add_param('pdlabel', 'nn23nlo') 1788 self.add_param('lhaid', 244600) 1789 #shower and scale 1790 self.add_param('parton_shower', 'HERWIG6', fortran_name='shower_mc') 1791 self.add_param('fixed_ren_scale', False) 1792 self.add_param('fixed_fac_scale', False) 1793 self.add_param('mur_ref_fixed', 91.118) 1794 self.add_param('muf1_ref_fixed', 91.118) 1795 self.add_param('muf2_ref_fixed', 91.118) 1796 self.add_param("dynamical_scale_choice", -1) 1797 self.add_param('fixed_qes_scale', False) 1798 self.add_param('qes_ref_fixed', 91.118) 1799 self.add_param('mur_over_ref', 1.0) 1800 self.add_param('muf1_over_ref', 1.0) 1801 self.add_param('muf2_over_ref', 1.0) 1802 self.add_param('qes_over_ref', 1.0) 1803 self.add_param('reweight_scale', True, fortran_name='do_rwgt_scale') 1804 self.add_param('rw_rscale_down', 0.5) 1805 self.add_param('rw_rscale_up', 2.0) 1806 self.add_param('rw_fscale_down', 0.5) 1807 self.add_param('rw_fscale_up', 2.0) 1808 self.add_param('reweight_pdf', False, fortran_name='do_rwgt_pdf') 1809 self.add_param('pdf_set_min', 244601) 1810 self.add_param('pdf_set_max', 244700) 1811 #merging 1812 self.add_param('ickkw', 0) 1813 self.add_param('bwcutoff', 15.0) 1814 #cuts 1815 self.add_param('jetalgo', 1.0) 1816 self.add_param('jetradius', 0.7, hidden=True) 1817 self.add_param('ptj', 10.0 , cut=True) 1818 self.add_param('etaj', -1.0, cut=True) 1819 self.add_param('ptl', 0.0, cut=True) 1820 self.add_param('etal', -1.0, cut=True) 1821 self.add_param('drll', 0.0, cut=True) 1822 self.add_param('drll_sf', 0.0, cut=True) 1823 self.add_param('mll', 0.0, cut=True) 1824 self.add_param('mll_sf', 30.0, cut=True) 1825 self.add_param('ptgmin', 20.0, cut=True) 1826 self.add_param('etagamma', -1.0) 1827 self.add_param('r0gamma', 0.4) 1828 self.add_param('xn', 1.0) 1829 self.add_param('epsgamma', 1.0) 1830 self.add_param('isoem', True) 1831 self.add_param('maxjetflavor', 4) 1832 self.add_param('iappl', 0) 1833 1834 self.add_param('lhe_version', 3, hidden=True, include=False)
1835
1836 - def check_validity(self):
1837 """check the validity of the various input""" 1838 1839 # For FxFx merging, make sure that the following parameters are set correctly: 1840 if self['ickkw'] == 3: 1841 # 1. Renormalization and factorization (and ellis-sexton scales) are not fixed 1842 scales=['fixed_ren_scale','fixed_fac_scale','fixed_QES_scale'] 1843 for scale in scales: 1844 if self[scale]: 1845 logger.warning('''For consistency in the FxFx merging, \'%s\' has been set to false''' 1846 % scale,'$MG:color:BLACK') 1847 self[scale]= False 1848 #and left to default dynamical scale 1849 if self["dynamical_scale_choice"] != -1: 1850 self["dynamical_scale_choice"] = -1 1851 logger.warning('''For consistency in the FxFx merging, dynamical_scale_choice has been set to -1 (default)''' 1852 ,'$MG:color:BLACK') 1853 1854 1855 # 2. Use kT algorithm for jets with pseudo-code size R=1.0 1856 jetparams=['jetradius','jetalgo'] 1857 for jetparam in jetparams: 1858 if float(self[jetparam]) != 1.0: 1859 logger.info('''For consistency in the FxFx merging, \'%s\' has been set to 1.0''' 1860 % jetparam ,'$MG:color:BLACK') 1861 self[jetparam] = 1.0 1862 elif self['ickkw'] == -1 and self["dynamical_scale_choice"] != -1: 1863 self["dynamical_scale_choice"] = -1 1864 self["dynamical_scale_choice"] = -1 1865 logger.warning('''For consistency with the jet veto, the scale which will be used is ptj. dynamical_scale_choice will be set at -1.''' 1866 ,'$MG:color:BLACK') 1867 1868 1869 # For interface to APPLGRID, need to use LHAPDF and reweighting to get scale uncertainties 1870 if self['iappl'] != 0 and self['pdlabel'].lower() != 'lhapdf': 1871 raise InvalidRunCard('APPLgrid generation only possible with the use of LHAPDF') 1872 if self['iappl'] != 0 and not self['reweight_scale']: 1873 raise InvalidRunCard('APPLgrid generation only possible with including' +\ 1874 ' the reweighting to get scale dependence') 1875 1876 # check that the pdf is set correctly 1877 possible_set = ['lhapdf','mrs02nl','mrs02nn', 'mrs0119','mrs0117','mrs0121','mrs01_j', 'mrs99_1','mrs99_2','mrs99_3','mrs99_4','mrs99_5','mrs99_6', 'mrs99_7','mrs99_8','mrs99_9','mrs9910','mrs9911','mrs9912', 'mrs98z1','mrs98z2','mrs98z3','mrs98z4','mrs98z5','mrs98ht', 'mrs98l1','mrs98l2','mrs98l3','mrs98l4','mrs98l5', 'cteq3_m','cteq3_l','cteq3_d', 'cteq4_m','cteq4_d','cteq4_l','cteq4a1','cteq4a2', 'cteq4a3','cteq4a4','cteq4a5','cteq4hj','cteq4lq', 'cteq5_m','cteq5_d','cteq5_l','cteq5hj','cteq5hq', 'cteq5f3','cteq5f4','cteq5m1','ctq5hq1','cteq5l1', 'cteq6_m','cteq6_d','cteq6_l','cteq6l1', 'nn23lo','nn23lo1','nn23nlo'] 1878 if self['pdlabel'] not in possible_set: 1879 raise InvalidRunCard, 'Invalid PDF set (argument of pdlabel) possible choice are:\n %s' % ','.join(possible_set) 1880 1881 # check that we use lhapdf if reweighting is ON 1882 if self['reweight_pdf'] and self['pdlabel'] != "lhapdf": 1883 raise InvalidRunCard, 'Reweight PDF option requires to use pdf sets associated to lhapdf. Please either change the pdlabel or set reweight_pdf to False.'
1884
1885 - def write(self, output_file, template=None, python_template=False):
1886 """Write the run_card in output_file according to template 1887 (a path to a valid run_card)""" 1888 1889 if not template: 1890 if not MADEVENT: 1891 template = pjoin(MG5DIR, 'Template', 'NLO', 'Cards', 1892 'run_card.dat') 1893 python_template = True 1894 else: 1895 template = pjoin(MEDIR, 'Cards', 'run_card_default.dat') 1896 python_template = False 1897 1898 super(RunCardNLO, self).write(output_file, template=template, 1899 python_template=python_template)
1900 1901
1902 - def create_default_for_process(self, proc_characteristic, history, proc_def):
1903 """Rules 1904 e+ e- beam -> lpp:0 ebeam:500 1905 p p beam -> set maxjetflavor automatically 1906 """ 1907 1908 # check for beam_id 1909 beam_id = set() 1910 for proc in proc_def: 1911 for leg in proc[0]['legs']: 1912 if not leg['state']: 1913 beam_id.add(leg['id']) 1914 if any(i in beam_id for i in [1,-1,2,-2,3,-3,4,-4,5,-5,21,22]): 1915 maxjetflavor = max([4]+[abs(i) for i in beam_id if -7< i < 7]) 1916 self['maxjetflavor'] = maxjetflavor 1917 pass 1918 elif 11 in beam_id or -11 in beam_id: 1919 self['lpp1'] = 0 1920 self['lpp2'] = 0 1921 self['ebeam1'] = 500 1922 self['ebeam2'] = 500 1923 else: 1924 self['lpp1'] = 0 1925 self['lpp2'] = 0
1926
1927 -class MadLoopParam(ConfigFile):
1928 """ a class for storing/dealing with the file MadLoopParam.dat 1929 contains a parser to read it, facilities to write a new file,... 1930 """ 1931 1932 1933
1934 - def default_setup(self):
1935 """initialize the directory to the default value""" 1936 1937 self.add_param("MLReductionLib", "1|4|3|2") 1938 self.add_param("IREGIMODE", 2) 1939 self.add_param("IREGIRECY", True) 1940 self.add_param("CTModeRun", -1) 1941 self.add_param("MLStabThres", 1e-3) 1942 self.add_param("NRotations_DP", 1) 1943 self.add_param("NRotations_QP", 0) 1944 self.add_param("ImprovePSPoint", 2) 1945 self.add_param("CTLoopLibrary", 2) 1946 self.add_param("CTStabThres", 1e-2) 1947 self.add_param("CTModeInit", 1) 1948 self.add_param("CheckCycle", 3) 1949 self.add_param("MaxAttempts", 10) 1950 self.add_param("ZeroThres", 1e-9) 1951 self.add_param("OSThres", 1.0e-8) 1952 self.add_param("DoubleCheckHelicityFilter", True) 1953 self.add_param("WriteOutFilters", True) 1954 self.add_param("UseLoopFilter", False) 1955 self.add_param("HelicityFilterLevel", 2) 1956 self.add_param("LoopInitStartOver", False) 1957 self.add_param("HelInitStartOver", False)
1958
1959 - def read(self, finput):
1960 """Read the input file, this can be a path to a file, 1961 a file object, a str with the content of the file.""" 1962 1963 if isinstance(finput, str): 1964 if "\n" in finput: 1965 finput = finput.split('\n') 1966 elif os.path.isfile(finput): 1967 finput = open(finput) 1968 else: 1969 raise Exception, "No such file %s" % input 1970 1971 previous_line= '' 1972 for line in finput: 1973 if previous_line.startswith('#'): 1974 name = previous_line[1:].split()[0] 1975 value = line.strip() 1976 if len(value) and value[0] not in ['#', '!']: 1977 self.__setitem__(name, value, change_userdefine=True) 1978 previous_line = line
1979 1980
1981 - def write(self, outputpath, template=None,commentdefault=False):
1982 1983 if not template: 1984 if not MADEVENT: 1985 template = pjoin(MG5DIR, 'Template', 'loop_material', 'StandAlone', 1986 'Cards', 'MadLoopParams.dat') 1987 else: 1988 template = pjoin(MEDIR, 'SubProcesses', 'MadLoop5_resources', 1989 'MadLoopParams.dat' ) 1990 if not os.path.exists(template): 1991 template = pjoin(MEDIR, 'Cards', 'MadLoopParams.dat') 1992 fsock = open(template, 'r') 1993 template = fsock.readlines() 1994 fsock.close() 1995 1996 if isinstance(outputpath, str): 1997 output = open(outputpath, 'w') 1998 else: 1999 output = outputpath 2000 2001 def f77format(value): 2002 if isinstance(value, bool): 2003 if value: 2004 return '.true.' 2005 else: 2006 return '.false.' 2007 elif isinstance(value, int): 2008 return value 2009 elif isinstance(value, float): 2010 tmp ='%e' % value 2011 return tmp.replace('e','d') 2012 elif isinstance(value, str): 2013 return value 2014 else: 2015 raise Exception, "Can not format input %s" % type(value)
2016 2017 name = '' 2018 done = set() 2019 for line in template: 2020 if name: 2021 done.add(name) 2022 if commentdefault and name.lower() not in self.user_set : 2023 output.write('!%s\n' % f77format(self[name])) 2024 else: 2025 output.write('%s\n' % f77format(self[name])) 2026 name='' 2027 continue 2028 elif line.startswith('#'): 2029 name = line[1:].split()[0] 2030 output.write(line)
2031