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