Package models :: Module check_param_card
[hide private]
[frames] | no frames]

Source Code for Module models.check_param_card

   1  from __future__ import division 
   2  import xml.etree.ElementTree as ET 
   3  import math 
   4  import os 
   5  import shutil 
   6  import logging 
   7   
   8  logger = logging.getLogger('madgraph.models') # -> stdout 
   9   
  10  try: 
  11      import madgraph.iolibs.file_writers as file_writers 
  12      import madgraph.various.misc as misc     
  13  except: 
  14      import internal.file_writers as file_writers 
  15      import internal.misc as misc 
16 17 -class InvalidParamCard(Exception):
18 """ a class for invalid param_card """ 19 pass
20
21 -class Parameter (object):
22 """A class for a param_card parameter""" 23
24 - def __init__(self, param=None, block=None, lhacode=None, value=None, comment=None):
25 """Init the parameter""" 26 27 self.format = 'float' 28 if param: 29 block = param.lhablock 30 lhacode = param.lhacode 31 value = param.value 32 comment = param.comment 33 format = param.format 34 35 self.lhablock = block 36 if lhacode: 37 self.lhacode = lhacode 38 else: 39 self.lhacode = [] 40 self.value = value 41 self.comment = comment
42
43 - def set_block(self, block):
44 """ set the block name """ 45 46 self.lhablock = block
47
48 - def load_str(self, text):
49 """ initialize the information from a str""" 50 51 if '#' in text: 52 data, self.comment = text.split('#',1) 53 else: 54 data, self.comment = text, "" 55 56 57 data = data.split() 58 if not len(data): 59 return 60 try: 61 self.lhacode = tuple([int(d) for d in data[:-1]]) 62 except Exception: 63 self.lhacode = tuple([int(d) for d in data[:-1] if d.isdigit()]) 64 self.value= ' '.join(data[len(self.lhacode):]) 65 else: 66 self.value = data[-1] 67 68 # convert to number when possible 69 try: 70 self.value = float(self.value) 71 except: 72 self.format = 'str' 73 pass 74 else: 75 if self.lhablock == 'modsel': 76 self.format = 'int' 77 self.value = int(self.value)
78
79 - def load_decay(self, text):
80 """ initialize the decay information from a str""" 81 82 if '#' in text: 83 data, self.comment = text.split('#',1) 84 else: 85 data, self.comment = text, "" 86 87 88 data = data.split() 89 if not len(data): 90 return 91 self.lhacode = [int(d) for d in data[2:]] 92 self.lhacode.sort() 93 self.lhacode = tuple([len(self.lhacode)] + self.lhacode) 94 95 self.value = float(data[0]) 96 self.format = 'decay_table'
97
98 - def __str__(self):
99 """ return a SLAH string """ 100 101 if self.format == 'float': 102 if self.lhablock == 'decay' and not isinstance(self.value,basestring): 103 return 'DECAY %s %e # %s' % (' '.join([str(d) for d in self.lhacode]), self.value, self.comment) 104 elif self.lhablock == 'decay': 105 return 'DECAY %s Auto # %s' % (' '.join([str(d) for d in self.lhacode]), self.comment) 106 elif self.lhablock and self.lhablock.startswith('qnumbers'): 107 return ' %s %i # %s' % (' '.join([str(d) for d in self.lhacode]), int(self.value), self.comment) 108 else: 109 return ' %s %e # %s' % (' '.join([str(d) for d in self.lhacode]), self.value, self.comment) 110 elif self.format == 'int': 111 return ' %s %i # %s' % (' '.join([str(d) for d in self.lhacode]), int(self.value), self.comment) 112 elif self.format == 'str': 113 if self.lhablock == 'decay': 114 return 'DECAY %s Auto # %s' % (' '.join([str(d) for d in self.lhacode]), self.comment) 115 return ' %s %s # %s' % (' '.join([str(d) for d in self.lhacode]), self.value, self.comment) 116 elif self.format == 'decay_table': 117 return ' %e %s # %s' % ( self.value,' '.join([str(d) for d in self.lhacode]), self.comment) 118 elif self.format == 'int': 119 return ' %s %i # %s' % (' '.join([str(d) for d in self.lhacode]), int(self.value), self.comment) 120 else: 121 if self.lhablock == 'decay': 122 return 'DECAY %s %d # %s' % (' '.join([str(d) for d in self.lhacode]), self.value, self.comment) 123 else: 124 return ' %s %d # %s' % (' '.join([str(d) for d in self.lhacode]), self.value, self.comment)
125
126 127 -class Block(list):
128 """ list of parameter """ 129
130 - def __init__(self, name=None):
131 if name: 132 self.name = name.lower() 133 else: 134 self.name = name 135 self.scale = None 136 self.comment = '' 137 self.decay_table = {} 138 self.param_dict={} 139 list.__init__(self)
140
141 - def get(self, lhacode, default=None):
142 """return the parameter associate to the lhacode""" 143 if not self.param_dict: 144 self.create_param_dict() 145 try: 146 return self.param_dict[tuple(lhacode)] 147 except KeyError: 148 if default is None: 149 raise KeyError, 'id %s is not in %s' % (tuple(lhacode), self.name) 150 else: 151 return Parameter(block=self, lhacode=lhacode, value=default, 152 comment='not define')
153
154 - def remove(self, lhacode):
155 """ remove a parameter """ 156 list.remove(self, self.get(lhacode)) 157 # update the dictionary of key 158 return self.param_dict.pop(tuple(lhacode))
159
160 - def __eq__(self, other, prec=1e-4):
161 """ """ 162 if len(self) != len(other): 163 return False 164 return not any(abs(param.value-other.param_dict[key].value)> prec 165 for key, param in self.param_dict.items())
166
167 - def __ne__(self, other, prec=1e-4):
168 return not self.__eq__(other, prec)
169
170 - def append(self, obj):
171 172 assert isinstance(obj, Parameter) 173 if not hasattr(self, 'name'): #can happen if loeaded from pickle 174 self.__init__(obj.lhablock) 175 assert not obj.lhablock or obj.lhablock == self.name 176 177 #The following line seems/is stupid but allow to pickle/unpickle this object 178 #this is important for madspin (in gridpack mode) 179 if not hasattr(self, 'param_dict'): 180 self.param_dict = {} 181 182 if tuple(obj.lhacode) in self.param_dict: 183 if self.param_dict[tuple(obj.lhacode)].value != obj.value: 184 raise InvalidParamCard, '%s %s is already define to %s impossible to assign %s' % \ 185 (self.name, obj.lhacode, self.param_dict[tuple(obj.lhacode)].value, obj.value) 186 return 187 list.append(self, obj) 188 # update the dictionary of key 189 self.param_dict[tuple(obj.lhacode)] = obj
190
191 - def create_param_dict(self):
192 """create a link between the lhacode and the Parameter""" 193 for param in self: 194 self.param_dict[tuple(param.lhacode)] = param 195 196 return self.param_dict
197
198 - def def_scale(self, scale):
199 """ """ 200 self.scale = scale
201
202 - def load_str(self, text):
203 "set inforamtion from the line" 204 205 if '#' in text: 206 data, self.comment = text.split('#',1) 207 else: 208 data, self.commant = text, "" 209 210 data = data.lower() 211 data = data.split() 212 self.name = data[1] # the first part of data is model 213 if len(data) == 3: 214 if data[2].startswith('q='): 215 #the last part should be of the form Q= 216 self.scale = float(data[2][2:]) 217 elif self.name == 'qnumbers': 218 self.name += ' %s' % data[2] 219 elif len(data) == 4 and data[2] == 'q=': 220 #the last part should be of the form Q= 221 self.scale = float(data[3]) 222 223 return self
224
225 - def keys(self):
226 """returns the list of id define in this blocks""" 227 228 return [p.lhacode for p in self]
229
230 - def __str__(self):
231 """ return a str in the SLAH format """ 232 233 text = """###################################""" + \ 234 """\n## INFORMATION FOR %s""" % self.name.upper() +\ 235 """\n###################################\n""" 236 237 #special case for decay chain 238 if self.name == 'decay': 239 for param in self: 240 pid = param.lhacode[0] 241 param.set_block('decay') 242 text += str(param)+ '\n' 243 if self.decay_table.has_key(pid): 244 text += str(self.decay_table[pid])+'\n' 245 return text 246 elif self.name.startswith('decay'): 247 text = '' # avoid block definition 248 #general case 249 elif not self.scale: 250 text += 'BLOCK %s # %s\n' % (self.name.upper(), self.comment) 251 else: 252 text += 'BLOCK %s Q= %e # %s\n' % (self.name.upper(), self.scale, self.comment) 253 254 text += '\n'.join([str(param) for param in self]) 255 return text + '\n'
256
257 258 -class ParamCard(dict):
259 """ a param Card: list of Block """ 260 mp_prefix = 'MP__' 261 262 header = \ 263 """######################################################################\n""" + \ 264 """## PARAM_CARD AUTOMATICALY GENERATED BY MG5 ####\n""" + \ 265 """######################################################################\n""" 266 267
268 - def __init__(self, input_path=None):
269 self.order = [] 270 271 self.input_path = input_path 272 if input_path: 273 self.read(input_path)
274
275 - def read(self, input_path):
276 """ read a card and full this object with the content of the card """ 277 278 if isinstance(input_path, str): 279 input = open(input_path) 280 else: 281 input = input_path #Use for banner loading and test 282 283 284 cur_block = None 285 for line in input: 286 line = line.strip() 287 if not line or line[0] == '#': 288 continue 289 line = line.lower() 290 if line.startswith('block'): 291 cur_block = Block() 292 cur_block.load_str(line) 293 self.append(cur_block) 294 continue 295 296 if line.startswith('decay'): 297 if not self.has_block('decay'): 298 cur_block = Block('decay') 299 self.append(cur_block) 300 else: 301 cur_block = self['decay'] 302 param = Parameter() 303 param.set_block(cur_block.name) 304 param.load_str(line[6:]) 305 cur_block.append(param) 306 continue 307 308 if cur_block is None: 309 continue 310 311 if cur_block.name == 'decay': 312 # This is a decay table 313 id = cur_block[-1].lhacode[0] 314 cur_block = Block('decay_table_%s' % id) 315 self['decay'].decay_table[id] = cur_block 316 317 318 319 320 if cur_block.name.startswith('decay_table'): 321 param = Parameter() 322 param.load_decay(line) 323 try: 324 cur_block.append(param) 325 except InvalidParamCard: 326 pass 327 else: 328 param = Parameter() 329 param.set_block(cur_block.name) 330 param.load_str(line) 331 cur_block.append(param) 332 333 return self
334
335 - def write(self, outpath):
336 """schedular for writing a card""" 337 338 # order the block in a smart way 339 blocks = self.order_block() 340 text = self.header 341 text += ''.join([str(block) for block in blocks]) 342 343 if not outpath: 344 return text 345 elif isinstance(outpath, str): 346 file(outpath,'w').write(text) 347 else: 348 outpath.write(text) # for test purpose
349
350 - def create_diff(self, new_card):
351 """return a text file allowing to pass from this card to the new one 352 via the set command""" 353 354 diff = '' 355 for blockname, block in self.items(): 356 for param in block: 357 lhacode = param.lhacode 358 value = param.value 359 new_value = new_card[blockname].get(lhacode).value 360 if not misc.equal(value, new_value, 6, zero_limit=False): 361 lhacode = ' '.join([str(i) for i in lhacode]) 362 diff += 'set param_card %s %s %s # orig: %s\n' % \ 363 (blockname, lhacode , new_value, value) 364 return diff
365 366 367 368
369 - def write_inc_file(self, outpath, identpath, default):
370 """ write a fortran file which hardcode the param value""" 371 372 fout = file_writers.FortranWriter(outpath) 373 defaultcard = ParamCard(default) 374 for line in open(identpath): 375 if line.startswith('c ') or line.startswith('ccccc'): 376 continue 377 split = line.split() 378 if len(split) < 3: 379 continue 380 block = split[0] 381 lhaid = [int(i) for i in split[1:-1]] 382 variable = split[-1] 383 if block in self: 384 try: 385 value = self[block].get(tuple(lhaid)).value 386 except KeyError: 387 value =defaultcard[block].get(tuple(lhaid)).value 388 logger.warning('information about \"%s %s" is missing using default value: %s.' %\ 389 (block, lhaid, value)) 390 391 else: 392 value =defaultcard[block].get(tuple(lhaid)).value 393 logger.warning('information about \"%s %s" is missing (full block missing) using default value: %s.' %\ 394 (block, lhaid, value)) 395 value = str(value).lower() 396 fout.writelines(' %s = %s' % (variable, str(value).replace('e','d')))
397 398
399 - def append(self, obj):
400 """add an object to this""" 401 402 assert isinstance(obj, Block) 403 self[obj.name] = obj 404 if not obj.name.startswith('decay_table'): 405 self.order.append(obj)
406 407 408
409 - def has_block(self, name):
410 return self.has_key(name)
411
412 - def order_block(self):
413 """ reorganize the block """ 414 return self.order
415
416 - def rename_blocks(self, name_dict):
417 """ rename the blocks """ 418 419 for old_name, new_name in name_dict.items(): 420 self[new_name] = self.pop(old_name) 421 self[new_name].name = new_name 422 for param in self[new_name]: 423 param.lhablock = new_name
424
425 - def remove_block(self, name):
426 """ remove a blocks """ 427 assert len(self[name])==0 428 [self.order.pop(i) for i,b in enumerate(self.order) if b.name == name] 429 self.pop(name)
430
431 - def remove_param(self, block, lhacode):
432 """ remove a parameter """ 433 if self.has_param(block, lhacode): 434 self[block].remove(lhacode) 435 if len(self[block]) == 0: 436 self.remove_block(block)
437
438 - def has_param(self, block, lhacode):
439 """check if param exists""" 440 441 try: 442 self[block].get(lhacode) 443 except: 444 return False 445 else: 446 return True
447
448 - def copy_param(self,old_block, old_lha, block=None, lhacode=None):
449 """ make a parameter, a symbolic link on another one """ 450 451 # Find the current block/parameter 452 old_block_obj = self[old_block] 453 parameter = old_block_obj.get(old_lha) 454 if not block: 455 block = old_block 456 if not lhacode: 457 lhacode = old_lha 458 459 self.add_param(block, lhacode, parameter.value, parameter.comment)
460
461 - def add_param(self,block, lha, value, comment=''):
462 463 parameter = Parameter(block=block, lhacode=lha, value=value, 464 comment=comment) 465 try: 466 new_block = self[block] 467 except KeyError: 468 # If the new block didn't exist yet 469 new_block = Block(block) 470 self.append(new_block) 471 new_block.append(parameter)
472 473
474 - def mod_param(self, old_block, old_lha, block=None, lhacode=None, 475 value=None, comment=None):
476 """ change a parameter to a new one. This is not a duplication.""" 477 478 # Find the current block/parameter 479 old_block = self[old_block] 480 try: 481 parameter = old_block.get(old_lha) 482 except: 483 if lhacode is not None: 484 lhacode=old_lha 485 self.add_param(block, lhacode, value, comment) 486 return 487 488 489 # Update the parameter 490 if block: 491 parameter.lhablock = block 492 if lhacode: 493 parameter.lhacode = lhacode 494 if value: 495 parameter.value = value 496 if comment: 497 parameter.comment = comment 498 499 # Change the block of the parameter 500 if block: 501 old_block.remove(old_lha) 502 if not len(old_block): 503 self.remove_block(old_block.name) 504 try: 505 new_block = self[block] 506 except KeyError: 507 # If the new block didn't exist yet 508 new_block = Block(block) 509 self.append(new_block) 510 new_block.append(parameter) 511 elif lhacode: 512 old_block.param_dict[tuple(lhacode)] = \ 513 old_block.param_dict.pop(tuple(old_lha))
514 515
516 - def check_and_remove(self, block, lhacode, value):
517 """ check that the value is coherent and remove it""" 518 519 if self.has_param(block, lhacode): 520 param = self[block].get(lhacode) 521 if param.value != value: 522 error_msg = 'This card is not suitable to be convert to SLAH1\n' 523 error_msg += 'Parameter %s %s should be %s' % (block, lhacode, value) 524 raise InvalidParamCard, error_msg 525 self.remove_param(block, lhacode)
526
527 528 -class ParamCardMP(ParamCard):
529 """ a param Card: list of Block with also MP definition of variables""" 530
531 - def write_inc_file(self, outpath, identpath, default):
532 """ write a fortran file which hardcode the param value""" 533 534 fout = file_writers.FortranWriter(outpath) 535 defaultcard = ParamCard(default) 536 for line in open(identpath): 537 if line.startswith('c ') or line.startswith('ccccc'): 538 continue 539 split = line.split() 540 if len(split) < 3: 541 continue 542 block = split[0] 543 lhaid = [int(i) for i in split[1:-1]] 544 variable = split[-1] 545 if block in self: 546 try: 547 value = self[block].get(tuple(lhaid)).value 548 except KeyError: 549 value =defaultcard[block].get(tuple(lhaid)).value 550 else: 551 value =defaultcard[block].get(tuple(lhaid)).value 552 #value = str(value).lower() 553 fout.writelines(' %s = %s' % (variable, ('%e' % value).replace('e','d'))) 554 fout.writelines(' %s%s = %s_16' % (self.mp_prefix, 555 variable, ('%e' % value)))
556
557 558 -class ParamCardRule(object):
559 """ A class for storing the linked between the different parameter of 560 the param_card. 561 Able to write a file 'param_card_rule.dat' 562 Able to read a file 'param_card_rule.dat' 563 Able to check the validity of a param_card.dat 564 """ 565 566
567 - def __init__(self, inputpath=None):
568 """initialize an object """ 569 570 # constraint due to model restriction 571 self.zero = [] 572 self.one = [] 573 self.identical = [] 574 self.opposite = [] 575 576 # constraint due to the model 577 self.rule = [] 578 579 if inputpath: 580 self.load_rule(inputpath)
581
582 - def add_zero(self, lhablock, lhacode, comment=''):
583 """add a zero rule""" 584 self.zero.append( (lhablock, lhacode, comment) )
585
586 - def add_one(self, lhablock, lhacode, comment=''):
587 """add a one rule""" 588 self.one.append( (lhablock, lhacode, comment) )
589
590 - def add_identical(self, lhablock, lhacode, lhacode2, comment=''):
591 """add a rule for identical value""" 592 self.identical.append( (lhablock, lhacode, lhacode2, comment) )
593
594 - def add_opposite(self, lhablock, lhacode, lhacode2, comment=''):
595 """add a rule for identical value""" 596 self.opposite.append( (lhablock, lhacode, lhacode2, comment) )
597 598
599 - def add_rule(self, lhablock, lhacode, rule, comment=''):
600 """add a rule for constraint value""" 601 self.rule.append( (lhablock, lhacode, rule) )
602
603 - def write_file(self, output=None):
604 605 text = """<file>###################################################################### 606 ## VALIDITY RULE FOR THE PARAM_CARD #### 607 ######################################################################\n""" 608 609 # ZERO 610 text +='<zero>\n' 611 for name, id, comment in self.zero: 612 text+=' %s %s # %s\n' % (name, ' '.join([str(i) for i in id]), 613 comment) 614 # ONE 615 text +='</zero>\n<one>\n' 616 for name, id, comment in self.one: 617 text+=' %s %s # %s\n' % (name, ' '.join([str(i) for i in id]), 618 comment) 619 # IDENTICAL 620 text +='</one>\n<identical>\n' 621 for name, id,id2, comment in self.identical: 622 text+=' %s %s : %s # %s\n' % (name, ' '.join([str(i) for i in id]), 623 ' '.join([str(i) for i in id2]), comment) 624 625 # OPPOSITE 626 text +='</identical>\n<opposite>\n' 627 for name, id,id2, comment in self.opposite: 628 text+=' %s %s : %s # %s\n' % (name, ' '.join([str(i) for i in id]), 629 ' '.join([str(i) for i in id2]), comment) 630 631 # CONSTRAINT 632 text += '</opposite>\n<constraint>\n' 633 for name, id, rule, comment in self.rule: 634 text += ' %s %s : %s # %s\n' % (name, ' '.join([str(i) for i in id]), 635 rule, comment) 636 text += '</constraint>\n</file>' 637 638 if isinstance(output, str): 639 output = open(output,'w') 640 if hasattr(output, 'write'): 641 output.write(text) 642 return text
643
644 - def load_rule(self, inputpath):
645 """ import a validity rule file """ 646 647 648 try: 649 tree = ET.parse(inputpath) 650 except IOError: 651 if '\n' in inputpath: 652 # this is convinient for the tests 653 tree = ET.fromstring(inputpath) 654 else: 655 raise 656 657 #Add zero element 658 element = tree.find('zero') 659 if element is not None: 660 for line in element.text.split('\n'): 661 line = line.split('#',1)[0] 662 if not line: 663 continue 664 lhacode = line.split() 665 blockname = lhacode.pop(0) 666 lhacode = [int(code) for code in lhacode ] 667 self.add_zero(blockname, lhacode, '') 668 669 #Add one element 670 element = tree.find('one') 671 if element is not None: 672 for line in element.text.split('\n'): 673 line = line.split('#',1)[0] 674 if not line: 675 continue 676 lhacode = line.split() 677 blockname = lhacode.pop(0) 678 lhacode = [int(code) for code in lhacode ] 679 self.add_one(blockname, lhacode, '') 680 681 #Add Identical element 682 element = tree.find('identical') 683 if element is not None: 684 for line in element.text.split('\n'): 685 line = line.split('#',1)[0] 686 if not line: 687 continue 688 line, lhacode2 = line.split(':') 689 lhacode = line.split() 690 blockname = lhacode.pop(0) 691 lhacode = [int(code) for code in lhacode ] 692 lhacode2 = [int(code) for code in lhacode2.split() ] 693 self.add_identical(blockname, lhacode, lhacode2, '') 694 695 #Add Opposite element 696 element = tree.find('opposite') 697 if element is not None: 698 for line in element.text.split('\n'): 699 line = line.split('#',1)[0] 700 if not line: 701 continue 702 line, lhacode2 = line.split(':') 703 lhacode = line.split() 704 blockname = lhacode.pop(0) 705 lhacode = [int(code) for code in lhacode ] 706 lhacode2 = [int(code) for code in lhacode2.split() ] 707 self.add_opposite(blockname, lhacode, lhacode2, '') 708 709 #Add Rule element 710 element = tree.find('rule') 711 if element is not None: 712 for line in element.text.split('\n'): 713 line = line.split('#',1)[0] 714 if not line: 715 continue 716 line, rule = line.split(':') 717 lhacode = line.split() 718 blockname = lhacode.pop(0) 719 self.add_rule(blockname, lhacode, rule, '')
720 721 @staticmethod
722 - def read_param_card(path):
723 """ read a param_card and return a dictionary with the associated value.""" 724 725 output = ParamCard(path) 726 727 728 729 return output
730 731 @staticmethod
732 - def write_param_card(path, data):
733 """ read a param_card and return a dictionary with the associated value.""" 734 735 output = {} 736 737 if isinstance(path, str): 738 output = open(path, 'w') 739 else: 740 output = path # helpfull for the test 741 742 data.write(path)
743 744
745 - def check_param_card(self, path, modify=False):
746 """Check that the restriction card are applied""" 747 748 card = self.read_param_card(path) 749 750 # check zero 751 for block, id, comment in self.zero: 752 try: 753 value = float(card[block].get(id).value) 754 except KeyError: 755 if modify: 756 new_param = Parameter(block=block,lhacode=id, value=0, 757 comment='fixed by the model') 758 if block in card: 759 card[block].append(new_param) 760 else: 761 new_block = Block(block) 762 card.append(new_block) 763 new_block.append(new_param) 764 else: 765 if value != 0: 766 if not modify: 767 raise InvalidParamCard, 'parameter %s: %s is not at zero' % \ 768 (block, ' '.join([str(i) for i in id])) 769 else: 770 param = card[block].get(id) 771 param.value = 0.0 772 param.comment += ' fixed by the model' 773 774 # check one 775 for block, id, comment in self.one: 776 try: 777 value = card[block].get(id).value 778 except KeyError: 779 if modify: 780 new_param = Parameter(block=block,lhacode=id, value=1, 781 comment='fixed by the model') 782 if block in card: 783 card[block].append(new_param) 784 else: 785 new_block = Block(block) 786 card.append(new_block) 787 new_block.append(new_param) 788 else: 789 if value != 1: 790 if not modify: 791 raise InvalidParamCard, 'parameter %s: %s is not at one but at %s' % \ 792 (block, ' '.join([str(i) for i in id]), value) 793 else: 794 param = card[block].get(id) 795 param.value = 1.0 796 param.comment += ' fixed by the model' 797 798 799 # check identical 800 for block, id1, id2, comment in self.identical: 801 if block not in card: 802 logger.warning('''Param card is not complete: Block %s is simply missing. 803 We will use model default for all missing value! Please cross-check that 804 this correspond to your expectation.''' % block) 805 continue 806 value2 = float(card[block].get(id2).value) 807 try: 808 param = card[block].get(id1) 809 except KeyError: 810 if modify: 811 new_param = Parameter(block=block,lhacode=id1, value=value2, 812 comment='must be identical to %s' %id2) 813 card[block].append(new_param) 814 else: 815 value1 = float(param.value) 816 817 if value1 != value2: 818 if not modify: 819 raise InvalidParamCard, 'parameter %s: %s is not to identical to parameter %s' % \ 820 (block, ' '.join([str(i) for i in id1]), 821 ' '.join([str(i) for i in id2])) 822 else: 823 param = card[block].get(id1) 824 param.value = value2 825 param.comment += ' must be identical to %s' % id2 826 827 # check opposite 828 for block, id1, id2, comment in self.opposite: 829 value2 = float(card[block].get(id2).value) 830 try: 831 param = card[block].get(id1) 832 except KeyError: 833 if modify: 834 new_param = Parameter(block=block,lhacode=id1, value=-value2, 835 comment='must be opposite to to %s' %id2) 836 card[block].append(new_param) 837 else: 838 value1 = float(param.value) 839 840 if value1 != -value2: 841 if not modify: 842 raise InvalidParamCard, 'parameter %s: %s is not to opposite to parameter %s' % \ 843 (block, ' '.join([str(i) for i in id1]), 844 ' '.join([str(i) for i in id2])) 845 else: 846 param = card[block].get(id1) 847 param.value = -value2 848 param.comment += ' must be opposite to %s' % id2 849 850 return card
851
852 853 -def convert_to_slha1(path, outputpath=None ):
854 """ """ 855 856 if not outputpath: 857 outputpath = path 858 card = ParamCard(path) 859 if not 'usqmix' in card: 860 #already slha1 861 card.write(outputpath) 862 return 863 864 # Mass 865 #card.reorder_mass() # needed? 866 card.copy_param('mass', [6], 'sminputs', [6]) 867 card.copy_param('mass', [15], 'sminputs', [7]) 868 card.copy_param('mass', [23], 'sminputs', [4]) 869 # Decay: Nothing to do. 870 871 # MODSEL 872 card.add_param('modsel',[1], value=1) 873 card['modsel'].get([1]).format = 'int' 874 875 # find scale 876 scale = card['hmix'].scale 877 if not scale: 878 scale = 1 # Need to be define (this is dummy value) 879 880 # SMINPUTS 881 if not card.has_param('sminputs', [2]): 882 aem1 = card['sminputs'].get([1]).value 883 mz = card['mass'].get([23]).value 884 mw = card['mass'].get([24]).value 885 gf = math.pi / math.sqrt(2) / aem1 * mz**2/ mw**2 /(mz**2-mw**2) 886 card.add_param('sminputs', [2], gf, 'G_F [GeV^-2]') 887 888 # USQMIX 889 card.check_and_remove('usqmix', [1,1], 1.0) 890 card.check_and_remove('usqmix', [2,2], 1.0) 891 card.check_and_remove('usqmix', [4,4], 1.0) 892 card.check_and_remove('usqmix', [5,5], 1.0) 893 card.mod_param('usqmix', [3,3], 'stopmix', [1,1]) 894 card.mod_param('usqmix', [3,6], 'stopmix', [1,2]) 895 card.mod_param('usqmix', [6,3], 'stopmix', [2,1]) 896 card.mod_param('usqmix', [6,6], 'stopmix', [2,2]) 897 898 # DSQMIX 899 card.check_and_remove('dsqmix', [1,1], 1.0) 900 card.check_and_remove('dsqmix', [2,2], 1.0) 901 card.check_and_remove('dsqmix', [4,4], 1.0) 902 card.check_and_remove('dsqmix', [5,5], 1.0) 903 card.mod_param('dsqmix', [3,3], 'sbotmix', [1,1]) 904 card.mod_param('dsqmix', [3,6], 'sbotmix', [1,2]) 905 card.mod_param('dsqmix', [6,3], 'sbotmix', [2,1]) 906 card.mod_param('dsqmix', [6,6], 'sbotmix', [2,2]) 907 908 909 # SELMIX 910 card.check_and_remove('selmix', [1,1], 1.0) 911 card.check_and_remove('selmix', [2,2], 1.0) 912 card.check_and_remove('selmix', [4,4], 1.0) 913 card.check_and_remove('selmix', [5,5], 1.0) 914 card.mod_param('selmix', [3,3], 'staumix', [1,1]) 915 card.mod_param('selmix', [3,6], 'staumix', [1,2]) 916 card.mod_param('selmix', [6,3], 'staumix', [2,1]) 917 card.mod_param('selmix', [6,6], 'staumix', [2,2]) 918 919 # FRALPHA 920 card.mod_param('fralpha', [1], 'alpha', [' ']) 921 922 #HMIX 923 if not card.has_param('hmix', [3]): 924 aem1 = card['sminputs'].get([1]).value 925 tanb = card['hmix'].get([2]).value 926 mz = card['mass'].get([23]).value 927 mw = card['mass'].get([24]).value 928 sw = math.sqrt(mz**2 - mw**2)/mz 929 ee = 2 * math.sqrt(1/aem1) * math.sqrt(math.pi) 930 vu = 2 * mw *sw /ee * math.sin(math.atan(tanb)) 931 card.add_param('hmix', [3], vu, 'higgs vev(Q) MSSM DRb') 932 card['hmix'].scale= scale 933 934 # VCKM 935 card.check_and_remove('vckm', [1,1], 1.0) 936 card.check_and_remove('vckm', [2,2], 1.0) 937 card.check_and_remove('vckm', [3,3], 1.0) 938 939 #SNUMIX 940 card.check_and_remove('snumix', [1,1], 1.0) 941 card.check_and_remove('snumix', [2,2], 1.0) 942 card.check_and_remove('snumix', [3,3], 1.0) 943 944 #UPMNS 945 card.check_and_remove('upmns', [1,1], 1.0) 946 card.check_and_remove('upmns', [2,2], 1.0) 947 card.check_and_remove('upmns', [3,3], 1.0) 948 949 # Te 950 ye = card['ye'].get([3, 3]).value 951 te = card['te'].get([3, 3]).value 952 card.mod_param('te', [3,3], 'ae', [3,3], value= te/ye, comment='A_tau(Q) DRbar') 953 card.add_param('ae', [1,1], 0, 'A_e(Q) DRbar') 954 card.add_param('ae', [2,2], 0, 'A_mu(Q) DRbar') 955 card['ae'].scale = scale 956 card['ye'].scale = scale 957 958 # Tu 959 yu = card['yu'].get([3, 3]).value 960 tu = card['tu'].get([3, 3]).value 961 card.mod_param('tu', [3,3], 'au', [3,3], value= tu/yu, comment='A_t(Q) DRbar') 962 card.add_param('au', [1,1], 0, 'A_u(Q) DRbar') 963 card.add_param('au', [2,2], 0, 'A_c(Q) DRbar') 964 card['au'].scale = scale 965 card['yu'].scale = scale 966 967 # Td 968 yd = card['yd'].get([3, 3]).value 969 td = card['td'].get([3, 3]).value 970 card.mod_param('td', [3,3], 'ad', [3,3], value= td/yd, comment='A_b(Q) DRbar') 971 card.add_param('ad', [1,1], 0, 'A_d(Q) DRbar') 972 card.add_param('ad', [2,2], 0, 'A_s(Q) DRbar') 973 card['ad'].scale = scale 974 card['yd'].scale = scale 975 976 # MSL2 977 value = card['msl2'].get([1, 1]).value 978 card.mod_param('msl2', [1,1], 'msoft', [31], math.sqrt(value)) 979 value = card['msl2'].get([2, 2]).value 980 card.mod_param('msl2', [2,2], 'msoft', [32], math.sqrt(value)) 981 value = card['msl2'].get([3, 3]).value 982 card.mod_param('msl2', [3,3], 'msoft', [33], math.sqrt(value)) 983 card['msoft'].scale = scale 984 985 # MSE2 986 value = card['mse2'].get([1, 1]).value 987 card.mod_param('mse2', [1,1], 'msoft', [34], math.sqrt(value)) 988 value = card['mse2'].get([2, 2]).value 989 card.mod_param('mse2', [2,2], 'msoft', [35], math.sqrt(value)) 990 value = card['mse2'].get([3, 3]).value 991 card.mod_param('mse2', [3,3], 'msoft', [36], math.sqrt(value)) 992 993 # MSQ2 994 value = card['msq2'].get([1, 1]).value 995 card.mod_param('msq2', [1,1], 'msoft', [41], math.sqrt(value)) 996 value = card['msq2'].get([2, 2]).value 997 card.mod_param('msq2', [2,2], 'msoft', [42], math.sqrt(value)) 998 value = card['msq2'].get([3, 3]).value 999 card.mod_param('msq2', [3,3], 'msoft', [43], math.sqrt(value)) 1000 1001 # MSU2 1002 value = card['msu2'].get([1, 1]).value 1003 card.mod_param('msu2', [1,1], 'msoft', [44], math.sqrt(value)) 1004 value = card['msu2'].get([2, 2]).value 1005 card.mod_param('msu2', [2,2], 'msoft', [45], math.sqrt(value)) 1006 value = card['msu2'].get([3, 3]).value 1007 card.mod_param('msu2', [3,3], 'msoft', [46], math.sqrt(value)) 1008 1009 # MSD2 1010 value = card['msd2'].get([1, 1]).value 1011 card.mod_param('msd2', [1,1], 'msoft', [47], math.sqrt(value)) 1012 value = card['msd2'].get([2, 2]).value 1013 card.mod_param('msd2', [2,2], 'msoft', [48], math.sqrt(value)) 1014 value = card['msd2'].get([3, 3]).value 1015 card.mod_param('msd2', [3,3], 'msoft', [49], math.sqrt(value)) 1016 1017 1018 1019 ################# 1020 # WRITE OUTPUT 1021 ################# 1022 card.write(outputpath)
1023
1024 1025 1026 -def convert_to_mg5card(path, outputpath=None, writting=True):
1027 """ """ 1028 1029 if not outputpath: 1030 outputpath = path 1031 card = ParamCard(path) 1032 if 'usqmix' in card: 1033 #already mg5(slha2) format 1034 card.write(outputpath) 1035 return 1036 1037 1038 # SMINPUTS 1039 card.remove_param('sminputs', [2]) 1040 card.remove_param('sminputs', [4]) 1041 card.remove_param('sminputs', [6]) 1042 card.remove_param('sminputs', [7]) 1043 # Decay: Nothing to do. 1044 1045 # MODSEL 1046 card.remove_param('modsel',[1]) 1047 1048 1049 # USQMIX 1050 card.add_param('usqmix', [1,1], 1.0) 1051 card.add_param('usqmix', [2,2], 1.0) 1052 card.add_param('usqmix', [4,4], 1.0) 1053 card.add_param('usqmix', [5,5], 1.0) 1054 card.mod_param('stopmix', [1,1], 'usqmix', [3,3]) 1055 card.mod_param('stopmix', [1,2], 'usqmix', [3,6]) 1056 card.mod_param('stopmix', [2,1], 'usqmix', [6,3]) 1057 card.mod_param('stopmix', [2,2], 'usqmix', [6,6]) 1058 1059 # DSQMIX 1060 card.add_param('dsqmix', [1,1], 1.0) 1061 card.add_param('dsqmix', [2,2], 1.0) 1062 card.add_param('dsqmix', [4,4], 1.0) 1063 card.add_param('dsqmix', [5,5], 1.0) 1064 card.mod_param('sbotmix', [1,1], 'dsqmix', [3,3]) 1065 card.mod_param('sbotmix', [1,2], 'dsqmix', [3,6]) 1066 card.mod_param('sbotmix', [2,1], 'dsqmix', [6,3]) 1067 card.mod_param('sbotmix', [2,2], 'dsqmix', [6,6]) 1068 1069 1070 # SELMIX 1071 card.add_param('selmix', [1,1], 1.0) 1072 card.add_param('selmix', [2,2], 1.0) 1073 card.add_param('selmix', [4,4], 1.0) 1074 card.add_param('selmix', [5,5], 1.0) 1075 card.mod_param('staumix', [1,1], 'selmix', [3,3]) 1076 card.mod_param('staumix', [1,2], 'selmix', [3,6]) 1077 card.mod_param('staumix', [2,1], 'selmix', [6,3]) 1078 card.mod_param('staumix', [2,2], 'selmix', [6,6]) 1079 1080 # FRALPHA 1081 card.mod_param('alpha', [], 'fralpha', [1]) 1082 1083 #HMIX 1084 card.remove_param('hmix', [3]) 1085 1086 # VCKM 1087 card.add_param('vckm', [1,1], 1.0) 1088 card.add_param('vckm', [2,2], 1.0) 1089 card.add_param('vckm', [3,3], 1.0) 1090 1091 #SNUMIX 1092 card.add_param('snumix', [1,1], 1.0) 1093 card.add_param('snumix', [2,2], 1.0) 1094 card.add_param('snumix', [3,3], 1.0) 1095 1096 #UPMNS 1097 card.add_param('upmns', [1,1], 1.0) 1098 card.add_param('upmns', [2,2], 1.0) 1099 card.add_param('upmns', [3,3], 1.0) 1100 1101 # Te 1102 ye = card['ye'].get([1, 1], default=0).value 1103 ae = card['ae'].get([1, 1], default=0).value 1104 card.mod_param('ae', [1,1], 'te', [1,1], value= ae * ye, comment='T_e(Q) DRbar') 1105 if ae * ye: 1106 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1107 Parameter ae [1, 1] times ye [1,1] should be 0''' 1108 card.remove_param('ae', [1,1]) 1109 #2 1110 ye = card['ye'].get([2, 2], default=0).value 1111 1112 ae = card['ae'].get([2, 2], default=0).value 1113 card.mod_param('ae', [2,2], 'te', [2,2], value= ae * ye, comment='T_mu(Q) DRbar') 1114 if ae * ye: 1115 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1116 Parameter ae [2, 2] times ye [2,2] should be 0''' 1117 card.remove_param('ae', [2,2]) 1118 #3 1119 ye = card['ye'].get([3, 3], default=0).value 1120 ae = card['ae'].get([3, 3], default=0).value 1121 card.mod_param('ae', [3,3], 'te', [3,3], value= ae * ye, comment='T_tau(Q) DRbar') 1122 1123 # Tu 1124 yu = card['yu'].get([1, 1], default=0).value 1125 au = card['au'].get([1, 1], default=0).value 1126 card.mod_param('au', [1,1], 'tu', [1,1], value= au * yu, comment='T_u(Q) DRbar') 1127 if au * yu: 1128 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1129 Parameter au [1, 1] times yu [1,1] should be 0''' 1130 card.remove_param('au', [1,1]) 1131 #2 1132 ye = card['yu'].get([2, 2], default=0).value 1133 1134 ae = card['au'].get([2, 2], default=0).value 1135 card.mod_param('au', [2,2], 'tu', [2,2], value= au * yu, comment='T_c(Q) DRbar') 1136 if au * yu: 1137 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1138 Parameter au [2, 2] times yu [2,2] should be 0''' 1139 card.remove_param('au', [2,2]) 1140 #3 1141 yu = card['yu'].get([3, 3]).value 1142 au = card['au'].get([3, 3]).value 1143 card.mod_param('au', [3,3], 'tu', [3,3], value= au * yu, comment='T_t(Q) DRbar') 1144 1145 # Td 1146 yd = card['yd'].get([1, 1], default=0).value 1147 ad = card['ad'].get([1, 1], default=0).value 1148 card.mod_param('ad', [1,1], 'td', [1,1], value= ad * yd, comment='T_d(Q) DRbar') 1149 if ad * yd: 1150 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1151 Parameter ad [1, 1] times yd [1,1] should be 0''' 1152 card.remove_param('ad', [1,1]) 1153 #2 1154 ye = card['yd'].get([2, 2], default=0).value 1155 1156 ae = card['ad'].get([2, 2], default=0).value 1157 card.mod_param('ad', [2,2], 'td', [2,2], value= ad * yd, comment='T_s(Q) DRbar') 1158 if ad * yd: 1159 raise InvalidParamCard, '''This card is not suitable to be converted to MSSM UFO model 1160 Parameter ad [2, 2] times yd [2,2] should be 0''' 1161 card.remove_param('ad', [2,2]) 1162 #3 1163 yd = card['yd'].get([3, 3]).value 1164 ad = card['ad'].get([3, 3]).value 1165 card.mod_param('ad', [3,3], 'td', [3,3], value= ad * yd, comment='T_b(Q) DRbar') 1166 1167 1168 # MSL2 1169 value = card['msoft'].get([31]).value 1170 card.mod_param('msoft', [31], 'msl2', [1,1], value**2) 1171 value = card['msoft'].get([32]).value 1172 card.mod_param('msoft', [32], 'msl2', [2,2], value**2) 1173 value = card['msoft'].get([33]).value 1174 card.mod_param('msoft', [33], 'msl2', [3,3], value**2) 1175 1176 # MSE2 1177 value = card['msoft'].get([34]).value 1178 card.mod_param('msoft', [34], 'mse2', [1,1], value**2) 1179 value = card['msoft'].get([35]).value 1180 card.mod_param('msoft', [35], 'mse2', [2,2], value**2) 1181 value = card['msoft'].get([36]).value 1182 card.mod_param('msoft', [36], 'mse2', [3,3], value**2) 1183 1184 # MSQ2 1185 value = card['msoft'].get([41]).value 1186 card.mod_param('msoft', [41], 'msq2', [1,1], value**2) 1187 value = card['msoft'].get([42]).value 1188 card.mod_param('msoft', [42], 'msq2', [2,2], value**2) 1189 value = card['msoft'].get([43]).value 1190 card.mod_param('msoft', [43], 'msq2', [3,3], value**2) 1191 1192 # MSU2 1193 value = card['msoft'].get([44]).value 1194 card.mod_param('msoft', [44], 'msu2', [1,1], value**2) 1195 value = card['msoft'].get([45]).value 1196 card.mod_param('msoft', [45], 'msu2', [2,2], value**2) 1197 value = card['msoft'].get([46]).value 1198 card.mod_param('msoft', [46], 'msu2', [3,3], value**2) 1199 1200 # MSD2 1201 value = card['msoft'].get([47]).value 1202 card.mod_param('msoft', [47], 'msd2', [1,1], value**2) 1203 value = card['msoft'].get([48]).value 1204 card.mod_param('msoft', [48], 'msd2', [2,2], value**2) 1205 value = card['msoft'].get([49]).value 1206 card.mod_param('msoft', [49], 'msd2', [3,3], value**2) 1207 1208 ################# 1209 # WRITE OUTPUT 1210 ################# 1211 if writting: 1212 card.write(outputpath) 1213 return card
1214
1215 1216 -def make_valid_param_card(path, restrictpath, outputpath=None):
1217 """ modify the current param_card such that it agrees with the restriction""" 1218 1219 if not outputpath: 1220 outputpath = path 1221 1222 cardrule = ParamCardRule() 1223 cardrule.load_rule(restrictpath) 1224 try : 1225 cardrule.check_param_card(path, modify=False) 1226 except InvalidParamCard: 1227 new_data = cardrule.check_param_card(path, modify=True) 1228 cardrule.write_param_card(outputpath, new_data) 1229 else: 1230 if path != outputpath: 1231 shutil.copy(path, outputpath) 1232 return cardrule
1233
1234 -def check_valid_param_card(path, restrictpath=None):
1235 """ check if the current param_card agrees with the restriction""" 1236 1237 if restrictpath is None: 1238 restrictpath = os.path.dirname(path) 1239 restrictpath = os.path.join(restrictpath, os.pardir, os.pardir, 'Source', 1240 'MODEL', 'param_card_rule.dat') 1241 if not os.path.exists(restrictpath): 1242 restrictpath = os.path.dirname(path) 1243 restrictpath = os.path.join(restrictpath, os.pardir, 'Source', 1244 'MODEL', 'param_card_rule.dat') 1245 if not os.path.exists(restrictpath): 1246 return True 1247 1248 cardrule = ParamCardRule() 1249 cardrule.load_rule(restrictpath) 1250 cardrule.check_param_card(path, modify=False)
1251 1252 if '__main__' == __name__: 1253 1254 1255 #make_valid_param_card('./Cards/param_card.dat', './Source/MODEL/param_card_rule.dat', 1256 # outputpath='tmp1.dat') 1257 import sys 1258 args = sys.argv 1259 sys.path.append(os.path.dirname(__file__)) 1260 convert_to_slha1(args[1] , args[2]) 1261