Package madgraph :: Package interface :: Module reweight_interface
[hide private]
[frames] | no frames]

Source Code for Module madgraph.interface.reweight_interface

   1  ################################################################################ 
   2  # 
   3  # Copyright (c) 2009 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  """ Command interface for Re-Weighting """ 
  16  from __future__ import division 
  17  import difflib 
  18  import logging 
  19  import math 
  20  import os 
  21  import re 
  22  import shutil 
  23  import sys 
  24  import tempfile 
  25  import time 
  26  import subprocess 
  27  from subprocess import Popen, PIPE, STDOUT 
  28   
  29   
  30  pjoin = os.path.join 
  31   
  32  import madgraph.interface.extended_cmd as extended_cmd 
  33  import madgraph.interface.madgraph_interface as mg_interface 
  34  import madgraph.interface.master_interface as master_interface 
  35  import madgraph.interface.common_run_interface as common_run_interface 
  36  import madgraph.interface.madevent_interface as madevent_interface 
  37  import madgraph.iolibs.files as files 
  38  #import MadSpin.interface_madspin as madspin_interface 
  39  import madgraph.various.misc as misc 
  40  import madgraph.various.banner as banner 
  41  import madgraph.various.lhe_parser as lhe_parser 
  42  import madgraph.various.combine_plots as combine_plots 
  43  import madgraph.various.cluster as cluster 
  44  import madgraph.fks.fks_common as fks_common 
  45  import madgraph.core.diagram_generation as diagram_generation 
  46   
  47  import models.import_ufo as import_ufo 
  48  import models.check_param_card as check_param_card  
  49  #import MadSpin.decay as madspin 
  50   
  51   
  52  logger = logging.getLogger('decay.stdout') # -> stdout 
  53  logger_stderr = logging.getLogger('decay.stderr') # ->stderr 
  54  cmd_logger = logging.getLogger('cmdprint2') # -> print 
  55   
  56  # global to check which f2py module have been already loaded. (to avoid border effect) 
  57  dir_to_f2py_free_mod = {} 
  58  nb_f2py_module = 0 # each time the process/model is changed this number is modified to  
  59                     # forced the python module to re-create an executable 
  60   
  61  lhapdf = None 
62 63 64 -class ReweightInterface(extended_cmd.Cmd):
65 """Basic interface for reweighting operation""" 66 67 prompt = 'Reweight>' 68 debug_output = 'Reweight_debug' 69 rwgt_dir_possibility = ['rw_me','rw_me_second','rw_mevirt','rw_mevirt_second'] 70 71 @misc.mute_logger()
72 - def __init__(self, event_path=None, allow_madspin=False, mother=None, *completekey, **stdin):
73 """initialize the interface with potentially an event_path""" 74 75 76 self.me_dir = os.getcwd() 77 if not event_path: 78 cmd_logger.info('************************************************************') 79 cmd_logger.info('* *') 80 cmd_logger.info('* Welcome to Reweight Module *') 81 cmd_logger.info('* *') 82 cmd_logger.info('************************************************************') 83 extended_cmd.Cmd.__init__(self, *completekey, **stdin) 84 85 self.model = None 86 self.has_standalone_dir = False 87 self.mother= mother # calling interface 88 self.multicore=False 89 90 self.options = {'curr_dir': os.path.realpath(os.getcwd()), 91 'rwgt_name':None, 92 "allow_missing_finalstate":False} 93 94 self.events_file = None 95 self.processes = {} 96 self.f2pylib = {} 97 self.second_model = None 98 self.second_process = None 99 self.dedicated_path = {} 100 self.soft_threshold = None 101 self.systematics = False # allow to run systematics in ouput2.0 mode 102 self.boost_event = False 103 self.mg5cmd = master_interface.MasterCmd() 104 if mother: 105 self.mg5cmd.options.update(mother.options) 106 self.seed = None 107 self.output_type = "default" 108 self.helicity_reweighting = True 109 self.rwgt_mode = '' # can be LO, NLO, NLO_tree, '' is default 110 self.has_nlo = False 111 self.rwgt_dir = None 112 self.exitted = False # Flag to know if do_quit was already called. 113 self.keep_ordering = False 114 if event_path: 115 logger.info("Extracting the banner ...") 116 self.do_import(event_path, allow_madspin=allow_madspin) 117 118 # dictionary to fortan evaluator 119 self.calculator = {} 120 self.calculator_nbcall = {} 121 122 #all the cross-section for convenience 123 self.all_cross_section = {}
124
125 - def do_import(self, inputfile, allow_madspin=False):
126 """import the event file""" 127 128 args = self.split_arg(inputfile) 129 if not args: 130 return self.InvalidCmd, 'import requires arguments' 131 132 # change directory where to write the output 133 self.options['curr_dir'] = os.path.realpath(os.path.dirname(inputfile)) 134 if os.path.basename(os.path.dirname(os.path.dirname(inputfile))) == 'Events': 135 self.options['curr_dir'] = pjoin(self.options['curr_dir'], 136 os.path.pardir, os.pardir) 137 138 139 if not os.path.exists(inputfile): 140 if inputfile.endswith('.gz'): 141 if not os.path.exists(inputfile[:-3]): 142 raise self.InvalidCmd('No such file or directory : %s' % inputfile) 143 else: 144 inputfile = inputfile[:-3] 145 elif os.path.exists(inputfile + '.gz'): 146 inputfile = inputfile + '.gz' 147 else: 148 raise self.InvalidCmd('No such file or directory : %s' % inputfile) 149 150 if inputfile.endswith('.gz'): 151 misc.gunzip(inputfile) 152 inputfile = inputfile[:-3] 153 154 # Read the banner of the inputfile 155 self.lhe_input = lhe_parser.EventFile(os.path.realpath(inputfile)) 156 if not self.lhe_input.banner: 157 value = self.ask("What is the path to banner", 0, [0], "please enter a path", timeout=0) 158 self.lhe_input.banner = open(value).read() 159 self.banner = self.lhe_input.get_banner() 160 161 #get original cross-section/error 162 if 'init' not in self.banner: 163 self.orig_cross = (0,0) 164 #raise self.InvalidCmd('Event file does not contain init information') 165 else: 166 for line in self.banner['init'].split('\n'): 167 split = line.split() 168 if len(split) == 4: 169 cross, error = float(split[0]), float(split[1]) 170 self.orig_cross = (cross, error) 171 172 173 174 # Check the validity of the banner: 175 if 'slha' not in self.banner: 176 self.events_file = None 177 raise self.InvalidCmd('Event file does not contain model information') 178 elif 'mg5proccard' not in self.banner: 179 self.events_file = None 180 raise self.InvalidCmd('Event file does not contain generation information') 181 182 if 'madspin' in self.banner and not allow_madspin: 183 raise self.InvalidCmd('Reweight should be done before running MadSpin') 184 185 186 # load information 187 process = self.banner.get_detail('proc_card', 'generate') 188 if '[' in process and isinstance(self.banner.get('run_card'), banner.RunCardNLO): 189 if not self.banner.get_detail('run_card', 'store_rwgt_info'): 190 logger.warning("The information to perform a proper NLO reweighting is not present in the event file.") 191 logger.warning(" We will perform a LO reweighting instead. This does not guarantee NLO precision.") 192 self.rwgt_mode = 'LO' 193 194 if self.mother and 'OLP' in self.mother.options: 195 if self.mother.options['OLP'].lower() != 'madloop': 196 logger.warning("Accurate NLO mode only works for OLP=MadLoop not for OLP=%s. An approximate (LO) reweighting will be performed instead") 197 self.rwgt_mode = 'LO' 198 199 if self.mother and 'lhapdf' in self.mother.options and not self.mother.options['lhapdf']: 200 logger.warning('NLO accurate reweighting requires lhapdf to be installed. Pass in approximate LO mode.') 201 self.rwgt_mode = 'LO' 202 else: 203 self.rwgt_mode = 'LO' 204 205 if not process: 206 msg = 'Invalid proc_card information in the file (no generate line):\n %s' % self.banner['mg5proccard'] 207 raise Exception, msg 208 process, option = mg_interface.MadGraphCmd.split_process_line(process) 209 self.proc_option = option 210 self.is_decay = len(process.split('>',1)[0].split()) == 1 211 212 logger.info("process: %s" % process) 213 logger.info("options: %s" % option)
214 215 216 @staticmethod
217 - def get_LO_definition_from_NLO(proc, model, real_only=False):
218 """return the LO definitions of the process corresponding to the born/real""" 219 220 # split the line definition with the part before and after the NLO tag 221 process, order, final = re.split('\[\s*(.*)\s*\]', proc) 222 if process.strip().startswith(('generate', 'add process')): 223 process = process.replace('generate', '') 224 process = process.replace('add process','') 225 226 # add the part without any additional jet. 227 commandline="add process %s %s --no_warning=duplicate;" % (process, final) 228 if not order: 229 #NO NLO tag => nothing to do actually return input 230 return proc 231 elif not order.startswith(('virt','LOonly','noborn')): 232 # OK this a standard NLO process 233 if real_only: 234 commandline= '' 235 236 if '=' in order: 237 # get the type NLO QCD/QED/... 238 order = order.split('=',1)[1].strip() 239 240 # define the list of particles that are needed for the radiation 241 pert = fks_common.find_pert_particles_interactions(model, 242 pert_order = order)['soft_particles'] 243 commandline += "define pert_%s = %s;" % (order.replace(' ',''), ' '.join(map(str,pert)) ) 244 245 # check if we have to increase by one the born order 246 247 if '%s=' % order in process or '%s<=' % order in process: 248 result=re.split(' ',process) 249 process='' 250 for r in result: 251 if '%s=' % order in r: 252 ior=re.split('=',r) 253 r='QCD=%i' % (int(ior[1])+1) 254 elif '%s<=' % order in r: 255 ior=re.split('=',r) 256 r='QCD<=%i' % (int(ior[1])+1) 257 process=process+r+' ' 258 #handle special tag $ | / @ 259 result = re.split('([/$@]|\w+(?:^2)?(?:=|<=|>)+\w+)', process, 1) 260 if len(result) ==3: 261 process, split, rest = result 262 commandline+="add process %s pert_%s %s%s %s --no_warning=duplicate;" % (process, order.replace(' ','') ,split, rest, final) 263 else: 264 commandline +='add process %s pert_%s %s --no_warning=duplicate;' % (process,order.replace(' ',''), final) 265 elif order.startswith(('noborn=')): 266 # pass in sqrvirt= 267 return "add process %s [%s] %s;" % (process, order.replace('noborn=', 'sqrvirt='), final) 268 elif order.startswith('LOonly'): 269 #remove [LOonly] flag 270 return "add process %s %s;" % (process, final) 271 else: 272 #just return the input. since this Madloop. 273 if order: 274 return "add process %s [%s] %s ;" % (process, order,final) 275 else: 276 return "add process %s %s ;" % (process, final) 277 return commandline
278 279
280 - def check_events(self):
281 """Check some basic property of the events file""" 282 283 sum_of_weight = 0 284 sum_of_abs_weight = 0 285 negative_event = 0 286 positive_event = 0 287 288 start = time.time() 289 for event_nb,event in enumerate(self.lhe_input): 290 #control logger 291 if (event_nb % max(int(10**int(math.log10(float(event_nb)+1))),10)==0): 292 running_time = misc.format_timer(time.time()-start) 293 logger.info('Event nb %s %s' % (event_nb, running_time)) 294 if (event_nb==10001): logger.info('reducing number of print status. Next status update in 10000 events') 295 296 try: 297 event.check() #check 4 momenta/... 298 except Exception, error: 299 print event 300 raise error 301 sum_of_weight += event.wgt 302 sum_of_abs_weight += abs(event.wgt) 303 if event.wgt < 0 : 304 negative_event +=1 305 else: 306 positive_event +=1 307 308 logger.info("total cross-section: %s" % sum_of_weight) 309 logger.info("total abs cross-section: %s" % sum_of_abs_weight) 310 logger.info("fraction of negative event %s", negative_event/(negative_event+positive_event)) 311 logger.info("total number of events %s", (negative_event+positive_event)) 312 logger.info("negative event %s", negative_event)
313 314 315 316 317 @extended_cmd.debug()
318 - def complete_import(self, text, line, begidx, endidx):
319 "Complete the import command" 320 321 args=self.split_arg(line[0:begidx]) 322 323 if len(args) == 1: 324 base_dir = '.' 325 else: 326 base_dir = args[1] 327 328 return self.path_completion(text, base_dir) 329 330 # Directory continuation 331 if os.path.sep in args[-1] + text: 332 return self.path_completion(text, 333 pjoin(*[a for a in args if \ 334 a.endswith(os.path.sep)]))
335
336 - def help_change(self):
337 """help for change command""" 338 339 print "change model X :use model X for the reweighting" 340 print "change process p p > e+ e-: use a new process for the reweighting" 341 print "change process p p > mu+ mu- --add : add one new process to existing ones" 342 print "change output [default|2.0|unweight]:" 343 print " default: add weight(s) to the current file"
344
345 - def do_change(self, line):
346 """allow to define a second model/processes""" 347 348 global nb_f2py_module 349 350 args = self.split_arg(line) 351 if len(args)<2: 352 logger.critical("not enough argument (need at least two). Discard line") 353 if args[0] == "model": 354 nb_f2py_module += 1 # tag to force the f2py to reload 355 self.second_model = " ".join(args[1:]) 356 if self.has_standalone_dir: 357 self.terminate_fortran_executables() 358 self.has_standalone_dir = False 359 elif args[0] == "keep_ordering": 360 self.keep_ordering = banner.ConfigFile.format_variable(args[1], bool, "keep_ordering") 361 elif args[0] == "allow_missing_finalstate": 362 self.options["allow_missing_finalstate"] = banner.ConfigFile.format_variable(args[1], bool, "allow_missing_finalstate") 363 elif args[0] == "process": 364 nb_f2py_module += 1 365 if self.has_standalone_dir: 366 self.terminate_fortran_executables() 367 self.has_standalone_dir = False 368 if args[-1] == "--add": 369 self.second_process.append(" ".join(args[1:-1])) 370 else: 371 self.second_process = [" ".join(args[1:])] 372 elif args[0] == "boost": 373 self.boost_event = eval(' '.join(args[1:])) 374 elif args[0] in ['virtual_path', 'tree_path']: 375 self.dedicated_path[args[0]] = os.path.abspath(args[1]) 376 elif args[0] == "output": 377 if args[1] in ['default', '2.0', 'unweight']: 378 self.output_type = args[1] 379 elif args[0] == "helicity": 380 self.helicity_reweighting = banner.ConfigFile.format_variable(args[1], bool, "helicity") 381 elif args[0] == "mode": 382 if args[1] != 'LO': 383 if 'OLP' in self.mother.options and self.mother.options['OLP'].lower() != 'madloop': 384 logger.warning("Only LO reweighting is allowed for OLP!=MadLoop. Keeping the mode to LO.") 385 self.rwgt_mode = 'LO' 386 elif not self.banner.get_detail('run_card','store_rwgt_info', default=False): 387 logger.warning("Missing information for NLO type of reweighting. Keeping the mode to LO.") 388 self.rwgt_mode = 'LO' 389 elif 'lhapdf' in self.mother.options and not self.mother.options['lhapdf']: 390 logger.warning('NLO accurate reweighting requires lhapdf to be installed. Pass in approximate LO mode.') 391 self.rwgt_mode = 'LO' 392 else: 393 self.rwgt_mode = args[1] 394 else: 395 self.rwgt_mode = args[1] 396 elif args[0] == "rwgt_dir": 397 self.rwgt_dir = args[1] 398 if not os.path.exists(self.rwgt_dir): 399 os.mkdir(self.rwgt_dir) 400 self.rwgt_dir = os.path.abspath(self.rwgt_dir) 401 elif args[0] == 'systematics': 402 if self.output_type == 'default' and args[1].lower() not in ['none', 'off']: 403 logger.warning('systematics can only be computed for non default output type. pass to output mode \'2.0\'') 404 self.output_type = '2.0' 405 if len(args) == 2: 406 try: 407 self.systematics = banner.ConfigFile.format_variable(args[1], bool) 408 except Exception, error: 409 self.systematics = args[1:] 410 else: 411 self.systematics = args[1:] 412 elif args[0] == 'soft_threshold': 413 self.soft_threshold = banner.ConfigFile.format_variable(args[1], float, 'soft_threshold') 414 elif args[0] == 'multicore': 415 pass 416 # this line is meant to be parsed by common_run_interface and change the way this class is called. 417 #It has no direct impact on this class. 418 else: 419 logger.critical("unknown option! %s. Discard line." % args[0])
420 421
422 - def check_launch(self, args):
423 """check the validity of the launch command""" 424 425 if not self.lhe_input: 426 if isinstance(self.lhe_input, lhe_parser.EventFile): 427 self.lhe_input = lhe_parser.EventFile(self.lhe_input.name) 428 else: 429 raise self.InvalidCmd("No events files defined.") 430 431 opts = {'rwgt_name':None, 'rwgt_info':None} 432 if any(a.startswith('--') for a in args): 433 for a in args[:]: 434 if a.startswith('--') and '=' in a: 435 key,value = a[2:].split('=') 436 opts[key] = value .replace("'","") .replace('"','') 437 438 return opts
439
440 - def help_launch(self):
441 """help for the launch command""" 442 443 logger.info('''Add to the loaded events a weight associated to a 444 new param_card (to be define). The weight returned is the ratio of the 445 square matrix element by the squared matrix element of production. 446 All scale are kept fix for this re-weighting.''')
447 448
449 - def get_weight_names(self):
450 """ return the various name for the computed weights """ 451 452 if self.rwgt_mode == 'LO': 453 return [''] 454 elif self.rwgt_mode == 'NLO': 455 return ['_nlo'] 456 elif self.rwgt_mode == 'LO+NLO': 457 return ['_lo', '_nlo'] 458 elif self.rwgt_mode == 'NLO_tree': 459 return ['_tree'] 460 elif not self.rwgt_mode and self.has_nlo : 461 return ['_nlo'] 462 else: 463 return ['']
464 465 @misc.mute_logger()
466 - def do_launch(self, line):
467 """end of the configuration launched the code""" 468 469 args = self.split_arg(line) 470 opts = self.check_launch(args) 471 if opts['rwgt_name']: 472 self.options['rwgt_name'] = opts['rwgt_name'] 473 if opts['rwgt_info']: 474 self.options['rwgt_info'] = opts['rwgt_info'] 475 model_line = self.banner.get('proc_card', 'full_model_line') 476 477 if not self.has_standalone_dir: 478 if self.rwgt_dir and os.path.exists(pjoin(self.rwgt_dir,'rw_me','rwgt.pkl')): 479 self.load_from_pickle() 480 if opts['rwgt_name']: 481 self.options['rwgt_name'] = opts['rwgt_name'] 482 if not self.rwgt_dir: 483 self.me_dir = self.rwgt_dir 484 self.load_module() # load the fortran information from the f2py module 485 elif self.multicore == 'wait': 486 i=0 487 while not os.path.exists(pjoin(self.me_dir,'rw_me','rwgt.pkl')): 488 time.sleep(10+i) 489 i+=5 490 print 'wait for pickle' 491 print "loading from pickle" 492 if not self.rwgt_dir: 493 self.rwgt_dir = self.me_dir 494 self.load_from_pickle(keep_name=True) 495 self.load_module() 496 else: 497 self.create_standalone_directory() 498 self.compile() 499 self.load_module() 500 if self.multicore == 'create': 501 self.load_module() 502 if not self.rwgt_dir: 503 self.rwgt_dir = self.me_dir 504 self.save_to_pickle() 505 506 # get the mode of reweighting #LO/NLO/NLO_tree/... 507 type_rwgt = self.get_weight_names() 508 # get iterator over param_card and the name associated to the current reweighting. 509 param_card_iterator, tag_name = self.handle_param_card(model_line, args, type_rwgt) 510 511 if self.rwgt_dir: 512 path_me =self.rwgt_dir 513 else: 514 path_me = self.me_dir 515 516 if self.second_model or self.second_process or self.dedicated_path: 517 rw_dir = pjoin(path_me, 'rw_me_second') 518 else: 519 rw_dir = pjoin(path_me, 'rw_me') 520 521 start = time.time() 522 # initialize the collector for the various re-weighting 523 cross, ratio, ratio_square,error = {},{},{}, {} 524 for name in type_rwgt + ['orig']: 525 cross[name], error[name] = 0.,0. 526 ratio[name],ratio_square[name] = 0., 0.# to compute the variance and associate error 527 528 if self.output_type == "default": 529 output = open( self.lhe_input.name +'rw', 'w') 530 #write the banner to the output file 531 self.banner.write(output, close_tag=False) 532 else: 533 output = {} 534 if tag_name.isdigit(): 535 name_tag= 'rwgt_%s' % tag_name 536 else: 537 name_tag = tag_name 538 base = os.path.dirname(self.lhe_input.name) 539 for rwgttype in type_rwgt: 540 output[(name_tag,rwgttype)] = lhe_parser.EventFile(pjoin(base,'rwgt_events%s_%s.lhe.gz' %(rwgttype,tag_name)), 'w') 541 #write the banner to the output file 542 self.banner.write(output[(name_tag,rwgttype)], close_tag=False) 543 544 if self.lhe_input.closed: 545 self.lhe_input = lhe_parser.EventFile(self.lhe_input.name) 546 547 self.lhe_input.seek(0) 548 for event_nb,event in enumerate(self.lhe_input): 549 #control logger 550 if (event_nb % max(int(10**int(math.log10(float(event_nb)+1))),10)==0): 551 running_time = misc.format_timer(time.time()-start) 552 logger.info('Event nb %s %s' % (event_nb, running_time)) 553 if (event_nb==10001): logger.info('reducing number of print status. Next status update in 10000 events') 554 if (event_nb==100001): logger.info('reducing number of print status. Next status update in 100000 events') 555 556 weight = self.calculate_weight(event) 557 if not isinstance(weight, dict): 558 weight = {'':weight} 559 560 for name in weight: 561 cross[name] += weight[name] 562 ratio[name] += weight[name]/event.wgt 563 ratio_square[name] += (weight[name]/event.wgt)**2 564 565 # ensure to have a consistent order of the weights. new one are put 566 # at the back, remove old position if already defines 567 for tag in type_rwgt: 568 try: 569 event.reweight_order.remove('%s%s' % (tag_name,tag)) 570 except ValueError: 571 continue 572 573 event.reweight_order += ['%s%s' % (tag_name,name) for name in type_rwgt] 574 if self.output_type == "default": 575 for name in weight: 576 if 'orig' in name: 577 continue 578 event.reweight_data['%s%s' % (tag_name,name)] = weight[name] 579 #write this event with weight 580 output.write(str(event)) 581 else: 582 for i,name in enumerate(weight): 583 if 'orig' in name: 584 continue 585 if weight[name] == 0: 586 continue 587 new_evt = lhe_parser.Event(str(event)) 588 new_evt.wgt = weight[name] 589 new_evt.parse_reweight() 590 new_evt.reweight_data = {} 591 output[(tag_name,name)].write(str(new_evt)) 592 593 # check normalisation of the events: 594 if 'event_norm' in self.run_card: 595 if self.run_card['event_norm'] in ['average','bias']: 596 for key, value in cross.items(): 597 cross[key] = value / (event_nb+1) 598 599 running_time = misc.format_timer(time.time()-start) 600 logger.info('All event done (nb_event: %s) %s' % (event_nb+1, running_time)) 601 602 603 if self.output_type == "default": 604 output.write('</LesHouchesEvents>\n') 605 output.close() 606 else: 607 for key in output: 608 output[key].write('</LesHouchesEvents>\n') 609 output[key].close() 610 if self.systematics and len(output) ==1: 611 try: 612 logger.info('running systematics computation') 613 import madgraph.various.systematics as syst 614 615 if not isinstance(self.systematics, bool): 616 args = [output[key].name, output[key].name] + self.systematics 617 else: 618 args = [output[key].name, output[key].name] 619 if self.mother and self.mother.options['lhapdf']: 620 args.append('--lhapdf_config=%s' % self.mother.options['lhapdf']) 621 syst.call_systematics(args, result=open('rwg_syst_%s.result' % key[0],'w'), 622 log=logger.info) 623 except Exception: 624 logger.error('fail to add systematics') 625 raise 626 # add output information 627 if self.mother and hasattr(self.mother, 'results'): 628 run_name = self.mother.run_name 629 results = self.mother.results 630 results.add_run(run_name, self.run_card, current=True) 631 results.add_detail('nb_event', event_nb+1) 632 name = type_rwgt[0] 633 results.add_detail('cross', cross[name]) 634 event_nb +=1 635 for name in type_rwgt: 636 variance = ratio_square[name]/event_nb - (ratio[name]/event_nb)**2 637 orig_cross, orig_error = self.orig_cross 638 error[name] = math.sqrt(max(0,variance/math.sqrt(event_nb))) * orig_cross + ratio[name]/event_nb * orig_error 639 results.add_detail('error', error[type_rwgt[0]]) 640 import madgraph.interface.madevent_interface as ME_interface 641 642 self.lhe_input.close() 643 if not self.mother: 644 name, ext = self.lhe_input.name.rsplit('.',1) 645 target = '%s_out.%s' % (name, ext) 646 elif self.output_type != "default" : 647 target = pjoin(self.mother.me_dir, 'Events', run_name, 'events.lhe') 648 else: 649 target = self.lhe_input.name 650 651 if self.output_type == "default": 652 files.mv(output.name, target) 653 logger.info('Event %s have now the additional weight' % self.lhe_input.name) 654 elif self.output_type == "unweight": 655 for key in output: 656 #output[key].write('</LesHouchesEvents>\n') 657 #output.close() 658 lhe = lhe_parser.EventFile(output[key].name) 659 nb_event = lhe.unweight(target) 660 if self.mother and hasattr(self.mother, 'results'): 661 results = self.mother.results 662 results.add_detail('nb_event', nb_event) 663 results.current.parton.append('lhe') 664 logger.info('Event %s is now unweighted under the new theory: %s(%s)' % (lhe.name, target, nb_event)) 665 else: 666 if self.mother and hasattr(self.mother, 'results'): 667 results = self.mother.results 668 results.current.parton.append('lhe') 669 logger.info('Eventfiles is/are now created with new central weight') 670 671 if self.multicore != 'create': 672 for name in cross: 673 if name == 'orig': 674 continue 675 logger.info('new cross-section is %s: %g pb (indicative error: %g pb)' %\ 676 ('(%s)' %name if name else '',cross[name], error[name])) 677 678 self.terminate_fortran_executables(new_card_only=True) 679 #store result 680 for name in cross: 681 if name == 'orig': 682 self.all_cross_section[name] = (cross[name], error[name]) 683 else: 684 self.all_cross_section[(tag_name,name)] = (cross[name], error[name]) 685 686 # perform the scanning 687 if param_card_iterator: 688 for i,card in enumerate(param_card_iterator): 689 if self.options['rwgt_name']: 690 self.options['rwgt_name'] = '%s_%s' % (self.options['rwgt_name'].rsplit('_',1)[0], i+1) 691 card.write(pjoin(rw_dir, 'Cards', 'param_card.dat')) 692 self.exec_cmd("launch --keep_card", printcmd=False, precmd=True) 693 694 self.options['rwgt_name'] = None
695 696
697 - def handle_param_card(self, model_line, args, type_rwgt):
698 699 if self.rwgt_dir: 700 path_me =self.rwgt_dir 701 else: 702 path_me = self.me_dir 703 704 if self.second_model or self.second_process or self.dedicated_path: 705 rw_dir = pjoin(path_me, 'rw_me_second') 706 else: 707 rw_dir = pjoin(path_me, 'rw_me') 708 709 710 if not '--keep_card' in args: 711 ff = open(pjoin(rw_dir,'Cards', 'param_card.dat'), 'w') 712 ff.write(self.banner['slha']) 713 ff.close() 714 if self.has_nlo and self.rwgt_mode != "LO": 715 rwdir_virt = rw_dir.replace('rw_me', 'rw_mevirt') 716 files.ln(ff.name, starting_dir=pjoin(rwdir_virt, 'Cards')) 717 ff = open(pjoin(path_me, 'rw_me','Cards', 'param_card_orig.dat'), 'w') 718 ff.write(self.banner['slha']) 719 ff.close() 720 if self.has_nlo and self.rwgt_mode != "LO": 721 files.ln(ff.name, starting_dir=pjoin(path_me, 'rw_mevirt', 'Cards')) 722 cmd = common_run_interface.CommonRunCmd.ask_edit_card_static(cards=['param_card.dat'], 723 ask=self.ask, pwd=rw_dir, first_cmd=self.stored_line) 724 self.stored_line = None 725 726 # check for potential scan in the new card 727 new_card = open(pjoin(rw_dir, 'Cards', 'param_card.dat')).read() 728 pattern_scan = re.compile(r'''^[\s\d]*scan''', re.I+re.M) 729 param_card_iterator = [] 730 if pattern_scan.search(new_card): 731 try: 732 import internal.extended_cmd as extended_internal 733 Shell_internal = extended_internal.CmdShell 734 except: 735 Shell_internal = None 736 import madgraph.interface.extended_cmd as extended_cmd 737 if not isinstance(self.mother, (extended_cmd.CmdShell, Shell_internal)): 738 raise Exception, "scan are not allowed on the Web" 739 # at least one scan parameter found. create an iterator to go trough the cards 740 main_card = check_param_card.ParamCardIterator(new_card) 741 if self.options['rwgt_name']: 742 self.options['rwgt_name'] = '%s_0' % self.options['rwgt_name'] 743 744 param_card_iterator = main_card 745 first_card = param_card_iterator.next(autostart=True) 746 new_card = first_card.write() 747 first_card.write(pjoin(rw_dir, 'Cards', 'param_card.dat')) 748 749 # check if "Auto" is present for a width parameter 750 tmp_card = new_card.lower().split('block',1)[1] 751 if "auto" in tmp_card: 752 self.mother.check_param_card(pjoin(rw_dir, 'Cards', 'param_card.dat')) 753 new_card = open(pjoin(rw_dir, 'Cards', 'param_card.dat')).read() 754 755 756 # Find new tag in the banner and add information if needed 757 if 'initrwgt' in self.banner and self.output_type == 'default': 758 if 'name=\'mg_reweighting\'' in self.banner['initrwgt']: 759 blockpat = re.compile(r'''<weightgroup name=\'mg_reweighting\'\s*weight_name_strategy=\'includeIdInWeightName\'>(?P<text>.*?)</weightgroup>''', re.I+re.M+re.S) 760 before, content, after = blockpat.split(self.banner['initrwgt']) 761 header_rwgt_other = before + after 762 pattern = re.compile('<weight id=\'(?:rwgt_(?P<id>\d+)|(?P<id2>[_\w]+))(?P<rwgttype>\s*|_\w+)\'>(?P<info>.*?)</weight>', re.S+re.I+re.M) 763 mg_rwgt_info = pattern.findall(content) 764 765 maxid = 0 766 for k,(i, fulltag, nlotype, diff) in enumerate(mg_rwgt_info): 767 if i: 768 if int(i) > maxid: 769 maxid = int(i) 770 mg_rwgt_info[k] = (i, nlotype, diff) # remove the pointless fulltag tag 771 else: 772 mg_rwgt_info[k] = (fulltag, nlotype, diff) # remove the pointless id tag 773 774 maxid += 1 775 rewgtid = maxid 776 if self.options['rwgt_name']: 777 #ensure that the entry is not already define if so overwrites it 778 for (i, nlotype, diff) in mg_rwgt_info[:]: 779 for flag in type_rwgt: 780 if 'rwgt_%s' % i == '%s%s' %(self.options['rwgt_name'],flag) or \ 781 i == '%s%s' % (self.options['rwgt_name'], flag): 782 logger.warning("tag %s%s already defines, will replace it", self.options['rwgt_name'],flag) 783 mg_rwgt_info.remove((i, nlotype, diff)) 784 785 else: 786 header_rwgt_other = self.banner['initrwgt'] 787 mg_rwgt_info = [] 788 rewgtid = 1 789 else: 790 self.banner['initrwgt'] = '' 791 header_rwgt_other = '' 792 mg_rwgt_info = [] 793 rewgtid = 1 794 795 # add the reweighting in the banner information: 796 #starts by computing the difference in the cards. 797 s_orig = self.banner['slha'] 798 s_new = new_card 799 self.new_param_card = check_param_card.ParamCard(s_new.splitlines()) 800 801 #define tag for the run 802 if self.options['rwgt_name']: 803 tag = self.options['rwgt_name'] 804 else: 805 tag = str(rewgtid) 806 807 if 'rwgt_info' in self.options and self.options['rwgt_info']: 808 card_diff = self.options['rwgt_info'] 809 for name in type_rwgt: 810 mg_rwgt_info.append((tag, name, self.options['rwgt_info'])) 811 elif not self.second_model and not self.dedicated_path: 812 old_param = check_param_card.ParamCard(s_orig.splitlines()) 813 new_param = self.new_param_card 814 card_diff = old_param.create_diff(new_param) 815 if card_diff == '' and not self.second_process: 816 logger.warning(' REWEIGHTING: original card and new card are identical.') 817 try: 818 if old_param['sminputs'].get(3)- new_param['sminputs'].get(3) > 1e-3 * new_param['sminputs'].get(3): 819 logger.warning("We found different value of alpha_s. Note that the value of alpha_s used is the one associate with the event and not the one from the cards.") 820 except Exception, error: 821 logger.debug("error in check of alphas: %s" % str(error)) 822 pass #this is a security 823 if not self.second_process: 824 for name in type_rwgt: 825 mg_rwgt_info.append((tag, name, card_diff)) 826 else: 827 str_proc = "\n change process ".join([""]+self.second_process) 828 for name in type_rwgt: 829 mg_rwgt_info.append((tag, name, str_proc + '\n'+ card_diff)) 830 else: 831 if self.second_model: 832 str_info = "change model %s" % self.second_model 833 else: 834 str_info ='' 835 if self.second_process: 836 str_info += "\n change process ".join([""]+self.second_process) 837 if self.dedicated_path: 838 for k,v in self.dedicated_path.items(): 839 str_info += "\n change %s %s" % (k,v) 840 card_diff = str_info 841 str_info += '\n' + s_new 842 for name in type_rwgt: 843 mg_rwgt_info.append((tag, name, str_info)) 844 # re-create the banner. 845 self.banner['initrwgt'] = header_rwgt_other 846 if self.output_type == 'default': 847 self.banner['initrwgt'] += '\n<weightgroup name=\'mg_reweighting\' weight_name_strategy=\'includeIdInWeightName\'>\n' 848 else: 849 self.banner['initrwgt'] += '\n<weightgroup name=\'main\'>\n' 850 for tag, rwgttype, diff in mg_rwgt_info: 851 if tag.isdigit(): 852 self.banner['initrwgt'] += '<weight id=\'rwgt_%s%s\'>%s</weight>\n' % \ 853 (tag, rwgttype, diff) 854 else: 855 self.banner['initrwgt'] += '<weight id=\'%s%s\'>%s</weight>\n' % \ 856 (tag, rwgttype, diff) 857 self.banner['initrwgt'] += '\n</weightgroup>\n' 858 self.banner['initrwgt'] = self.banner['initrwgt'].replace('\n\n', '\n') 859 860 logger.info('starts to compute weight for events with the following modification to the param_card:') 861 logger.info(card_diff.replace('\n','\nKEEP:')) 862 self.run_card = banner.Banner(self.banner).charge_card('run_card') 863 864 if self.options['rwgt_name']: 865 tag_name = self.options['rwgt_name'] 866 else: 867 tag_name = 'rwgt_%s' % rewgtid 868 869 #initialise module. 870 for (path,tag), module in self.f2pylib.items(): 871 with misc.chdir(pjoin(os.path.dirname(rw_dir), path)): 872 with misc.stdchannel_redirected(sys.stdout, os.devnull): 873 if 'second' in path or tag == 3: 874 module.initialise(pjoin(rw_dir, 'Cards', 'param_card.dat')) 875 else: 876 module.initialise(pjoin(path_me, 'rw_me', 'Cards', 'param_card_orig.dat')) 877 878 return param_card_iterator, tag_name
879 880
881 - def do_set(self, line):
882 "Not in help" 883 884 logger.warning("Invalid Syntax. The command 'set' should be placed after the 'launch' one. Continuing by adding automatically 'launch'") 885 self.stored_line = "set %s" % line 886 return self.exec_cmd("launch")
887
888 - def default(self, line, log=True):
889 """Default action if line is not recognized""" 890 891 if os.path.isfile(line): 892 if log: 893 logger.warning("Invalid Syntax. The path to a param_card' should be placed after the 'launch' command. Continuing by adding automatically 'launch'") 894 self.stored_line = line 895 return self.exec_cmd("launch") 896 else: 897 return super(ReweightInterface,self).default(line, log=log)
898
899 - def write_reweighted_event(self, event, tag_name, **opt):
900 """a function for running in multicore""" 901 902 if not hasattr(opt['thread_space'], "calculator"): 903 opt['thread_space'].calculator = {} 904 opt['thread_space'].calculator_nbcall = {} 905 opt['thread_space'].cross = 0 906 opt['thread_space'].output = open( self.lhe_input.name +'rw.%s' % opt['thread_id'], 'w') 907 if self.mother: 908 out_path = pjoin(self.mother.me_dir, 'Events', 'reweight.lhe.%s' % opt['thread_id']) 909 opt['thread_space'].output2 = open(out_path, 'w') 910 911 weight = self.calculate_weight(event, space=opt['thread_space']) 912 opt['thread_space'].cross += weight 913 if self.output_type == "default": 914 event.reweight_data[tag_name] = weight 915 #write this event with weight 916 opt['thread_space'].output.write(str(event)) 917 if self.mother: 918 event.wgt = weight 919 event.reweight_data = {} 920 opt['thread_space'].output2.write(str(event)) 921 else: 922 event.wgt = weight 923 event.reweight_data = {} 924 if self.mother: 925 opt['thread_space'].output2.write(str(event)) 926 else: 927 opt['thread_space'].output.write(str(event)) 928 929 return 0
930
931 - def do_compute_widths(self, line):
932 return self.mother.do_compute_widths(line)
933 934 935 dynamical_scale_warning=True
936 - def change_kinematics(self, event):
937 938 939 if isinstance(self.run_card, banner.RunCardLO): 940 jac = event.change_ext_mass(self.new_param_card) 941 new_event = event 942 else: 943 jac =1 944 new_event = event 945 946 if jac != 1: 947 if self.output_type == 'default': 948 logger.critical('mass reweighting requires dedicated lhe output!. Please include "change output 2.0" in your reweight_card') 949 raise Exception 950 mode = self.run_card['dynamical_scale_choice'] 951 if mode == -1: 952 if self.dynamical_scale_warning: 953 logger.warning('dynamical_scale is set to -1. New sample will be with HT/2 dynamical scale for renormalisation scale') 954 mode = 3 955 new_event.scale = event.get_scale(mode) 956 new_event.aqcd = self.lhe_input.get_alphas(new_event.scale, lhapdf_config=self.mother.options['lhapdf']) 957 958 return jac, new_event
959 960
961 - def calculate_weight(self, event):
962 """space defines where to find the calculator (in multicore)""" 963 964 global lhapdf 965 966 if self.has_nlo and self.rwgt_mode != "LO": 967 return self.calculate_nlo_weight(event) 968 969 event.parse_reweight() 970 orig_wgt = event.wgt 971 # LO reweighting 972 w_orig = self.calculate_matrix_element(event, 0) 973 974 # reshuffle event for mass effect # external mass only 975 # carefull that new_event can sometimes be = to event 976 # (i.e. change can be in place) 977 jac, new_event = self.change_kinematics(event) 978 979 980 if event.wgt != 0: # impossible reshuffling 981 w_new = self.calculate_matrix_element(new_event, 1) 982 else: 983 w_new = 0 984 985 if w_orig == 0: 986 tag, order = event.get_tag_and_order() 987 orig_order, Pdir, hel_dict = self.id_to_path[tag] 988 misc.sprint(w_orig, w_new) 989 misc.sprint(event) 990 misc.sprint(self.invert_momenta(event.get_momenta(orig_order))) 991 misc.sprint(event.get_momenta(orig_order)) 992 misc.sprint(event.aqcd) 993 hel_order = event.get_helicity(orig_order) 994 if self.helicity_reweighting and 9 not in hel_order: 995 nhel = hel_dict[tuple(hel_order)] 996 else: 997 nhel = 0 998 misc.sprint(nhel, Pdir, hel_dict) 999 raise Exception, "Invalid matrix element for original computation (weight=0)" 1000 1001 return {'orig': orig_wgt, '': w_new/w_orig*orig_wgt*jac}
1002
1003 - def calculate_nlo_weight(self, event):
1004 1005 1006 type_nlo = self.get_weight_names() 1007 final_weight = {'orig': event.wgt} 1008 1009 event.parse_reweight() 1010 event.parse_nlo_weight(threshold=self.soft_threshold) 1011 if self.output_type != 'default': 1012 event.nloweight.modified = True # the internal info will be changed 1013 # so set this flage to True to change 1014 # the writting of those data 1015 1016 #initialise the input to the function which recompute the weight 1017 scales2 = [] 1018 pdg = [] 1019 bjx = [] 1020 wgt_tree = [] # reweight for loop-improved type 1021 wgt_virt = [] #reweight b+v together 1022 base_wgt = [] 1023 gs=[] 1024 qcdpower = [] 1025 ref_wgts = [] #for debugging 1026 1027 orig_wgt = 0 1028 for cevent in event.nloweight.cevents: 1029 #check if we need to compute the virtual for that cevent 1030 need_V = False # the real is nothing else than the born for a N+1 config 1031 all_ctype = [w.type for w in cevent.wgts] 1032 if '_nlo' in type_nlo and any(c in all_ctype for c in [2,14,15]): 1033 need_V =True 1034 1035 w_orig = self.calculate_matrix_element(cevent, 0) 1036 w_new = self.calculate_matrix_element(cevent, 1) 1037 ratio_T = w_new/w_orig 1038 if need_V: 1039 scale2 = cevent.wgts[0].scales2[0] 1040 #for scale2 in set(c.scales2[1] for c in cevent.wgts): 1041 w_origV = self.calculate_matrix_element(cevent, 'V0', scale2=scale2) 1042 w_newV = self.calculate_matrix_element(cevent, 'V1', scale2=scale2) 1043 ratio_BV = (w_newV + w_new) / (w_origV + w_orig) 1044 ratio_V = w_newV/w_origV 1045 else: 1046 ratio_V = "should not be used" 1047 ratio_BV = "should not be used" 1048 for c_wgt in cevent.wgts: 1049 orig_wgt += c_wgt.ref_wgt 1050 #add the information to the input 1051 scales2.append(c_wgt.scales2) 1052 pdg.append(c_wgt.pdgs[:2]) 1053 1054 bjx.append(c_wgt.bjks) 1055 qcdpower.append(c_wgt.qcdpower) 1056 gs.append(c_wgt.gs) 1057 ref_wgts.append(c_wgt.ref_wgt) 1058 1059 if '_nlo' in type_nlo: 1060 if c_wgt.type in [2,14,15]: 1061 R = ratio_BV 1062 else: 1063 R = ratio_T 1064 1065 new_wgt = [c_wgt.pwgt[0] * R, 1066 c_wgt.pwgt[1] * ratio_T, 1067 c_wgt.pwgt[2] * ratio_T] 1068 wgt_virt.append(new_wgt) 1069 1070 if '_tree' in type_nlo: 1071 new_wgt = [c_wgt.pwgt[0] * ratio_T, 1072 c_wgt.pwgt[1] * ratio_T, 1073 c_wgt.pwgt[2] * ratio_T] 1074 wgt_tree.append(new_wgt) 1075 1076 base_wgt.append(c_wgt.pwgt[:3]) 1077 1078 #change the ordering to the fortran one: 1079 scales2 = self.invert_momenta(scales2) 1080 pdg = self.invert_momenta(pdg) 1081 bjx = self.invert_momenta(bjx) 1082 # re-compute original weight to reduce numerical inacurracy 1083 base_wgt = self.invert_momenta(base_wgt) 1084 1085 orig_wgt_check, partial_check = self.combine_wgt(scales2, pdg, bjx, base_wgt, gs, qcdpower, 1., 1.) 1086 1087 if '_nlo' in type_nlo: 1088 wgt = self.invert_momenta(wgt_virt) 1089 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1090 new_out, partial = self.combine_wgt(scales2, pdg, bjx, wgt, gs, qcdpower, 1., 1.) 1091 # try to correct for precision issue 1092 avg = [partial_check[i]/ref_wgts[i] for i in range(len(ref_wgts))] 1093 out = sum(partial[i]/avg[i] if 0.85<avg[i]<1.15 else 0 \ 1094 for i in range(len(avg))) 1095 final_weight['_nlo'] = out/orig_wgt*event.wgt 1096 1097 1098 if '_tree' in type_nlo: 1099 wgt = self.invert_momenta(wgt_tree) 1100 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1101 out, partial = self.combine_wgt(scales2, pdg, bjx, wgt, gs, qcdpower, 1., 1.) 1102 # try to correct for precision issue 1103 avg = [partial_check[i]/ref_wgts[i] for i in range(len(ref_wgts))] 1104 new_out = sum(partial[i]/avg[i] if 0.85<avg[i]<1.15 else partial[i] \ 1105 for i in range(len(avg))) 1106 final_weight['_tree'] = new_out/orig_wgt*event.wgt 1107 1108 1109 if '_lo' in type_nlo: 1110 w_orig = self.calculate_matrix_element(event, 0) 1111 w_new = self.calculate_matrix_element(event, 1) 1112 final_weight['_lo'] = w_new/w_orig*event.wgt 1113 1114 1115 if self.output_type != 'default' and len(type_nlo)==1 and '_lo' not in type_nlo: 1116 to_write = [partial[i]/ref_wgts[i]*partial_check[i] 1117 if 0.85<avg[i]<1.15 else 0 1118 for i in range(len(ref_wgts))] 1119 for cevent in event.nloweight.cevents: 1120 for c_wgt in cevent.wgts: 1121 c_wgt.ref_wgt = to_write.pop(0) 1122 if '_tree' in type_nlo: 1123 c_wgt.pwgt = wgt_tree.pop(0) 1124 else: 1125 c_wgt.pwgt = wgt_virt.pop(0) 1126 assert not to_write 1127 assert not wgt_tree 1128 return final_weight
1129 1130 1131 @staticmethod
1132 - def invert_momenta(p):
1133 """ fortran/C-python do not order table in the same order""" 1134 new_p = [] 1135 for i in range(len(p[0])): new_p.append([0]*len(p)) 1136 for i, onep in enumerate(p): 1137 for j, x in enumerate(onep): 1138 new_p[j][i] = x 1139 return new_p
1140 1141 @staticmethod
1142 - def rename_f2py_lib(Pdir, tag):
1143 if tag == 2: 1144 return 1145 if os.path.exists(pjoin(Pdir, 'matrix%spy.so' % tag)): 1146 return 1147 else: 1148 open(pjoin(Pdir, 'matrix%spy.so' % tag),'w').write(open(pjoin(Pdir, 'matrix2py.so') 1149 ).read().replace('matrix2py', 'matrix%spy' % tag))
1150
1151 - def calculate_matrix_element(self, event, hypp_id, scale2=0):
1152 """routine to return the matrix element""" 1153 1154 if self.has_nlo: 1155 nb_retry, sleep = 10, 60 1156 else: 1157 nb_retry, sleep = 5, 20 1158 1159 tag, order = event.get_tag_and_order() 1160 if self.keep_ordering: 1161 old_tag = tuple(tag) 1162 tag = (tag[0], tuple(order[1])) 1163 if isinstance(hypp_id, str) and hypp_id.startswith('V'): 1164 tag = (tag,'V') 1165 hypp_id = int(hypp_id[1:]) 1166 # base = "rw_mevirt" 1167 #else: 1168 # base = "rw_me" 1169 1170 if (not self.second_model and not self.second_process and not self.dedicated_path) or hypp_id==0: 1171 orig_order, Pdir, hel_dict = self.id_to_path[tag] 1172 else: 1173 try: 1174 orig_order, Pdir, hel_dict = self.id_to_path_second[tag] 1175 except KeyError: 1176 if self.options['allow_missing_finalstate']: 1177 return 0.0 1178 else: 1179 logger.critical('The following initial/final state %s can not be found in the new model/process. If you want to set the weights of such events to zero use "change allow_missing_finalstate False"', tag) 1180 raise Exception 1181 1182 base = os.path.basename(os.path.dirname(Pdir)) 1183 if '_second' in base: 1184 moduletag = (base, 2) 1185 else: 1186 moduletag = (base, 2+hypp_id) 1187 1188 module = self.f2pylib[moduletag] 1189 1190 p = event.get_momenta(orig_order) 1191 # add helicity information 1192 1193 hel_order = event.get_helicity(orig_order) 1194 if self.helicity_reweighting and 9 not in hel_order: 1195 nhel = hel_dict[tuple(hel_order)] 1196 else: 1197 nhel = -1 1198 1199 # For 2>N pass in the center of mass frame 1200 # - required for helicity by helicity re-weighitng 1201 # - Speed-up loop computation 1202 if (hypp_id == 0 and ('frame_id' in self.banner.run_card and self.banner.run_card['frame_id'] !=6)): 1203 import copy 1204 new_event = copy.deepcopy(event) 1205 pboost = FourMomenta() 1206 to_inc = bin(self.banner.run_card['frame_id'])[2:] 1207 to_inc.reverse() 1208 nb_ext = 0 1209 for p in new_event: 1210 if p.status in [-1,1]: 1211 nb_ext += 1 1212 if to_inc[nb_ext]: 1213 pboost += p 1214 new_event.boost(pboost) 1215 p = new_event.get_momenta(orig_order) 1216 elif (hypp_id == 1 and self.boost_event): 1217 if self.boost_event is not True: 1218 import copy 1219 new_event = copy.deepcopy(event) 1220 new_event.boost(self.boost_event) 1221 p = new_event.get_momenta(orig_order) 1222 elif (hasattr(event[1], 'status') and event[1].status == -1) or \ 1223 (event[1].px == event[1].py == 0.): 1224 pboost = lhe_parser.FourMomentum(p[0]) + lhe_parser.FourMomentum(p[1]) 1225 for i,thisp in enumerate(p): 1226 p[i] = lhe_parser.FourMomentum(thisp).zboost(pboost).get_tuple() 1227 assert p[0][1] == p[0][2] == 0 == p[1][2] == p[1][2] == 0 1228 1229 pold = list(p) 1230 p = self.invert_momenta(p) 1231 pdg = list(orig_order[0])+list(orig_order[1]) 1232 1233 with misc.chdir(Pdir): 1234 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1235 me_value = module.smatrixhel(pdg,p, event.aqcd, scale2, nhel) 1236 1237 # for loop we have also the stability status code 1238 if isinstance(me_value, tuple): 1239 me_value, code = me_value 1240 #if code points unstability -> returns 0 1241 hundred_value = (code % 1000) //100 1242 if hundred_value in [4]: 1243 me_value = 0. 1244 1245 return me_value
1246
1247 - def terminate_fortran_executables(self, new_card_only=False):
1248 """routine to terminate all fortran executables""" 1249 1250 for (mode, production) in dict(self.calculator): 1251 1252 if new_card_only and production == 0: 1253 continue 1254 del self.calculator[(mode, production)]
1255
1256 - def do_quit(self, line):
1257 if self.exitted: 1258 return 1259 self.exitted = True 1260 1261 if 'init' in self.banner: 1262 cross = 0 1263 error = 0 1264 for line in self.banner['init'].split('\n'): 1265 split = line.split() 1266 if len(split) == 4: 1267 cross, error = float(split[0]), float(split[1]) 1268 1269 if not self.multicore == 'create': 1270 # No print of results for the multicore mode for the one printed on screen 1271 if 'orig' not in self.all_cross_section: 1272 logger.info('Original cross-section: %s +- %s pb' % (cross, error)) 1273 else: 1274 logger.info('Original cross-section: %s +- %s pb (cross-section from sum of weights: %s)' % (cross, error, self.all_cross_section['orig'][0])) 1275 logger.info('Computed cross-section:') 1276 keys = self.all_cross_section.keys() 1277 keys.sort() 1278 for key in keys: 1279 if key == 'orig': 1280 continue 1281 logger.info('%s : %s +- %s pb' % (key[0] if not key[1] else '%s%s' % key, 1282 self.all_cross_section[key][0],self.all_cross_section[key][1] )) 1283 self.terminate_fortran_executables() 1284 1285 if self.rwgt_dir and self.multicore == False: 1286 self.save_to_pickle() 1287 1288 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1289 for run_id in self.calculator: 1290 del self.calculator[run_id] 1291 del self.calculator
1292 1293
1294 - def __del__(self):
1295 self.do_quit('')
1296 1297
1298 - def adding_me(self, matrix_elements, path):
1299 """Adding one element to the list based on the matrix element"""
1300 1301 1302 @misc.mute_logger()
1303 - def create_standalone_tree_directory(self, data ,second=False):
1304 """generate the various directory for the weight evaluation""" 1305 1306 mgcmd = self.mg5cmd 1307 path_me = data['path'] 1308 # 2. compute the production matrix element ----------------------------- 1309 has_nlo = False 1310 mgcmd.exec_cmd("set group_subprocesses False") 1311 1312 if not second: 1313 logger.info('generating the square matrix element for reweighting') 1314 else: 1315 logger.info('generating the square matrix element for reweighting (second model and/or processes)') 1316 start = time.time() 1317 commandline='' 1318 for i,proc in enumerate(data['processes']): 1319 if '[' not in proc: 1320 commandline += "add process %s ;" % proc 1321 else: 1322 has_nlo = True 1323 if self.banner.get('run_card','ickkw') == 3: 1324 if len(proc) == min([len(p.strip()) for p in data['processes']]): 1325 commandline += self.get_LO_definition_from_NLO(proc, self.model) 1326 else: 1327 commandline += self.get_LO_definition_from_NLO(proc, 1328 self.model, real_only=True) 1329 else: 1330 commandline += self.get_LO_definition_from_NLO(proc, self.model) 1331 1332 commandline = commandline.replace('add process', 'generate',1) 1333 logger.info(commandline) 1334 try: 1335 mgcmd.exec_cmd(commandline, precmd=True, errorhandling=False) 1336 except diagram_generation.NoDiagramException: 1337 commandline='' 1338 for proc in data['processes']: 1339 if '[' not in proc: 1340 raise 1341 # pass to virtsq= 1342 base, post = proc.split('[',1) 1343 nlo_order, post = post.split(']',1) 1344 if '=' not in nlo_order: 1345 nlo_order = 'virt=%s' % nlo_order 1346 elif 'noborn' in nlo_order: 1347 nlo_order = nlo_order.replace('noborn', 'virt') 1348 commandline += "add process %s [%s] %s;" % (base,nlo_order,post) 1349 commandline = commandline.replace('add process', 'generate',1) 1350 logger.info("RETRY with %s", commandline) 1351 mgcmd.exec_cmd(commandline, precmd=True) 1352 has_nlo = False 1353 except Exception, error: 1354 raise 1355 1356 commandline = 'output standalone_rw %s --prefix=int' % pjoin(path_me,data['paths'][0]) 1357 mgcmd.exec_cmd(commandline, precmd=True) 1358 logger.info('Done %.4g' % (time.time()-start)) 1359 self.has_standalone_dir = True 1360 1361 1362 # 3. Store id to directory information --------------------------------- 1363 if False: 1364 # keep this for debugging 1365 matrix_elements = mgcmd._curr_matrix_elements.get_matrix_elements() 1366 1367 to_check = [] # list of tag that do not have a Pdir at creation time. 1368 for me in matrix_elements: 1369 for proc in me.get('processes'): 1370 initial = [] #filled in the next line 1371 final = [l.get('id') for l in proc.get('legs')\ 1372 if l.get('state') or initial.append(l.get('id'))] 1373 order = (initial, final) 1374 tag = proc.get_initial_final_ids() 1375 decay_finals = proc.get_final_ids_after_decay() 1376 1377 if tag[1] != decay_finals: 1378 order = (initial, list(decay_finals)) 1379 decay_finals.sort() 1380 tag = (tag[0], tuple(decay_finals)) 1381 Pdir = pjoin(path_me, data['paths'][0], 'SubProcesses', 1382 'P%s' % me.get('processes')[0].shell_string()) 1383 1384 if not os.path.exists(Pdir): 1385 to_check.append(tag) 1386 continue 1387 if tag in data['id2path']: 1388 if not Pdir == data['id2path'][tag][1]: 1389 misc.sprint(tag, Pdir, data['id2path'][tag][1]) 1390 raise self.InvalidCmd, '2 different process have the same final states. This module can not handle such situation' 1391 else: 1392 continue 1393 # build the helicity dictionary 1394 hel_nb = 0 1395 hel_dict = {9:0} # unknown helicity -> use full ME 1396 for helicities in me.get_helicity_matrix(): 1397 hel_nb +=1 #fortran starts at 1 1398 hel_dict[tuple(helicities)] = hel_nb 1399 1400 data['id2path'][tag] = [order, Pdir, hel_dict] 1401 1402 for tag in to_check: 1403 if tag not in self.id_to_path: 1404 logger.warning("no valid path for %s" % (tag,)) 1405 #raise self.InvalidCmd, "no valid path for %s" % (tag,) 1406 1407 # 4. Check MadLoopParam for Loop induced 1408 if os.path.exists(pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat')): 1409 MLCard = banner.MadLoopParam(pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat')) 1410 MLCard.set('WriteOutFilters', False) 1411 MLCard.set('UseLoopFilter', False) 1412 MLCard.set("DoubleCheckHelicityFilter", False) 1413 MLCard.set("HelicityFilterLevel", 0) 1414 MLCard.write(pjoin(path_me, data['paths'][0], 'SubProcesses', 'MadLoopParams.dat'), 1415 pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat'), 1416 commentdefault=False) 1417 1418 #if self.multicore == 'create': 1419 # print "compile OLP", data['paths'][0] 1420 # misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][0],'SubProcesses'), 1421 # nb_core=self.mother.options['nb_core']) 1422 1423 if os.path.exists(pjoin(path_me, data['paths'][1], 'Cards', 'MadLoopParams.dat')): 1424 if self.multicore == 'create': 1425 print "compile OLP", data['paths'][1] 1426 # It is potentially unsafe to use several cores, We limit ourself to one for now 1427 # n_cores = self.mother.options['nb_core'] 1428 n_cores = 1 1429 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'), 1430 nb_core=self.mother.options['nb_core']) 1431 1432 return has_nlo
1433 1434 1435 @misc.mute_logger()
1436 - def create_standalone_virt_directory(self, data ,second=False):
1437 """generate the various directory for the weight evaluation""" 1438 1439 mgcmd = self.mg5cmd 1440 path_me = data['path'] 1441 # Do not pass here for LO/NLO_tree 1442 start = time.time() 1443 commandline='' 1444 for proc in data['processes']: 1445 if '[' not in proc: 1446 pass 1447 else: 1448 proc = proc.replace('[', '[ virt=') 1449 commandline += "add process %s ;" % proc 1450 commandline = re.sub('@\s*\d+', '', commandline) 1451 # deactivate golem since it creates troubles 1452 old_options = dict(mgcmd.options) 1453 if mgcmd.options['golem'] or mgcmd.options['pjfry']: 1454 logger.info(" When doing NLO reweighting, MG5aMC cannot use the loop reduction algorithms Golem and/or PJFry++") 1455 mgcmd.options['golem'] = None 1456 mgcmd.options['pjfry'] = None 1457 commandline = commandline.replace('add process', 'generate',1) 1458 logger.info(commandline) 1459 mgcmd.exec_cmd(commandline, precmd=True) 1460 commandline = 'output standalone_rw %s --prefix=int -f' % pjoin(path_me, data['paths'][1]) 1461 mgcmd.exec_cmd(commandline, precmd=True) 1462 1463 #put back golem to original value 1464 mgcmd.options['golem'] = old_options['golem'] 1465 mgcmd.options['pjfry'] = old_options['pjfry'] 1466 # update make_opts 1467 m_opts = {} 1468 if mgcmd.options['lhapdf']: 1469 #lhapdfversion = subprocess.Popen([mgcmd.options['lhapdf'], '--version'], 1470 # stdout = subprocess.PIPE).stdout.read().strip()[0] 1471 m_opts['lhapdf'] = True 1472 m_opts['f2pymode'] = True 1473 m_opts['lhapdfversion'] = 5 # 6 always fail on my computer since 5 is compatible but slower always use 5 1474 m_opts['llhapdf'] = self.mother.get_lhapdf_libdir() 1475 else: 1476 raise Exception, "NLO reweighting requires LHAPDF to work correctly" 1477 1478 path = pjoin(path_me,data['paths'][1], 'Source', 'make_opts') 1479 common_run_interface.CommonRunCmd.update_make_opts_full(path, m_opts) 1480 logger.info('Done %.4g' % (time.time()-start)) 1481 1482 1483 # Download LHAPDF SET 1484 common_run_interface.CommonRunCmd.install_lhapdf_pdfset_static(\ 1485 mgcmd.options['lhapdf'], None, self.banner.run_card.get_lhapdf_id()) 1486 1487 # now store the id information 1488 if False: 1489 # keep it for debugging purposes 1490 matrix_elements = mgcmd._curr_matrix_elements.get_matrix_elements() 1491 for me in matrix_elements: 1492 for proc in me.get('processes'): 1493 initial = [] #filled in the next line 1494 final = [l.get('id') for l in proc.get('legs')\ 1495 if l.get('state') or initial.append(l.get('id'))] 1496 order = (initial, final) 1497 tag = proc.get_initial_final_ids() 1498 decay_finals = proc.get_final_ids_after_decay() 1499 1500 if tag[1] != decay_finals: 1501 order = (initial, list(decay_finals)) 1502 decay_finals.sort() 1503 tag = (tag[0], tuple(decay_finals)) 1504 Pdir = pjoin(path_me, data['paths'][1], 'SubProcesses', 1505 'P%s' % me.get('processes')[0].shell_string()) 1506 assert os.path.exists(Pdir), "Pdir %s do not exists" % Pdir 1507 if (tag,'V') in data['id2path']: 1508 if not Pdir == data['id2path'][(tag,'V')][1]: 1509 misc.sprint(tag, Pdir, self.id_to_path[(tag,'V')][1]) 1510 raise self.InvalidCmd, '2 different process have the same final states. This module can not handle such situation' 1511 else: 1512 continue 1513 # build the helicity dictionary 1514 hel_nb = 0 1515 hel_dict = {9:0} # unknown helicity -> use full ME 1516 for helicities in me.get_helicity_matrix(): 1517 hel_nb +=1 #fortran starts at 1 1518 hel_dict[tuple(helicities)] = hel_nb 1519 1520 data['id2path'][(tag,'V')] = [order, Pdir, hel_dict]
1521 1522 1523 @misc.mute_logger()
1524 - def create_standalone_directory(self, second=False):
1525 """generate the various directory for the weight evaluation""" 1526 1527 data={} 1528 if not second: 1529 data['paths'] = ['rw_me', 'rw_mevirt'] 1530 # model 1531 info = self.banner.get('proc_card', 'full_model_line') 1532 if '-modelname' in info: 1533 data['mg_names'] = False 1534 else: 1535 data['mg_names'] = True 1536 data['model_name'] = self.banner.get('proc_card', 'model') 1537 #processes 1538 data['processes'] = [line[9:].strip() for line in self.banner.proc_card 1539 if line.startswith('generate')] 1540 data['processes'] += [' '.join(line.split()[2:]) for line in self.banner.proc_card 1541 if re.search('^\s*add\s+process', line)] 1542 #object_collector 1543 #self.id_to_path = {} 1544 #data['id2path'] = self.id_to_path 1545 else: 1546 data['paths'] = ['rw_me_second', 'rw_mevirt_second'] 1547 # model 1548 if self.second_model: 1549 data['mg_names'] = True 1550 if ' ' in self.second_model: 1551 args = self.second_model.split() 1552 if '--modelname' in args: 1553 data['mg_names'] = False 1554 data['model_name'] = args[0] 1555 else: 1556 data['model_name'] = self.second_model 1557 else: 1558 data['model_name'] = None 1559 #processes 1560 if self.second_process: 1561 data['processes'] = self.second_process 1562 else: 1563 data['processes'] = [line[9:].strip() for line in self.banner.proc_card 1564 if line.startswith('generate')] 1565 data['processes'] += [' '.join(line.split()[2:]) 1566 for line in self.banner.proc_card 1567 if re.search('^\s*add\s+process', line)] 1568 #object_collector 1569 #self.id_to_path_second = {} 1570 #data['id2path'] = self.id_to_path_second 1571 1572 # 0. clean previous run ------------------------------------------------ 1573 if not self.rwgt_dir: 1574 path_me = self.me_dir 1575 else: 1576 path_me = self.rwgt_dir 1577 data['path'] = path_me 1578 try: 1579 shutil.rmtree(pjoin(path_me,data['paths'][0])) 1580 except Exception: 1581 pass 1582 try: 1583 shutil.rmtree(pjoin(path_me, data['paths'][1])) 1584 except Exception: 1585 pass 1586 1587 # 1. prepare the interface---------------------------------------------- 1588 mgcmd = self.mg5cmd 1589 complex_mass = False 1590 has_cms = re.compile(r'''set\s+complex_mass_scheme\s*(True|T|1|true|$|;)''') 1591 for line in self.banner.proc_card: 1592 if line.startswith('set'): 1593 mgcmd.exec_cmd(line, printcmd=False, precmd=False, postcmd=False) 1594 if has_cms.search(line): 1595 complex_mass = True 1596 elif line.startswith('define'): 1597 try: 1598 mgcmd.exec_cmd(line, printcmd=False, precmd=False, postcmd=False) 1599 except Exception: 1600 pass 1601 1602 # 1. Load model--------------------------------------------------------- 1603 if not data['model_name'] and not second: 1604 raise self.InvalidCmd('Only UFO model can be loaded in this module.') 1605 elif data['model_name']: 1606 self.load_model(data['model_name'], data['mg_names'], complex_mass) 1607 modelpath = self.model.get('modelpath') 1608 if os.path.basename(modelpath) != mgcmd._curr_model['name']: 1609 name, restrict = mgcmd._curr_model['name'].rsplit('-',1) 1610 if os.path.exists(pjoin(os.path.dirname(modelpath),name, 'restrict_%s.dat' % restrict)): 1611 modelpath = pjoin(os.path.dirname(modelpath), mgcmd._curr_model['name']) 1612 1613 commandline="import model %s " % modelpath 1614 if not data['mg_names']: 1615 commandline += ' -modelname ' 1616 mgcmd.exec_cmd(commandline) 1617 1618 #multiparticles 1619 for name, content in self.banner.get('proc_card', 'multiparticles'): 1620 mgcmd.exec_cmd("define %s = %s" % (name, content)) 1621 1622 if second and 'tree_path' in self.dedicated_path: 1623 files.ln(self.dedicated_path['tree_path'], path_me,name=data['paths'][0]) 1624 if 'virtual_path' in self.dedicated_path: 1625 has_nlo=True 1626 else: 1627 has_nlo=False 1628 else: 1629 has_nlo = self.create_standalone_tree_directory(data, second) 1630 1631 1632 # 5. create the virtual for NLO reweighting --------------------------- 1633 if second and 'virtual_path' in self.dedicated_path: 1634 files.ln(self.dedicated_path['virtual_path'], path_me, name=data['paths'][1]) 1635 elif has_nlo and 'NLO' in self.rwgt_mode: 1636 self.create_standalone_virt_directory(data, second) 1637 1638 if not second: 1639 #compile the module to combine the weight 1640 misc.compile(cwd=pjoin(path_me, data['paths'][1], 'Source')) 1641 #link it 1642 if path_me not in sys.path: 1643 sys.path.insert(0, os.path.realpath(path_me)) 1644 with misc.chdir(pjoin(path_me)): 1645 mymod = __import__('%s.Source.rwgt2py' % data['paths'][1], globals(), locals(), [],-1) 1646 mymod = mymod.Source.rwgt2py 1647 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1648 mymod.initialise([self.banner.run_card['lpp1'], 1649 self.banner.run_card['lpp2']], 1650 self.banner.run_card.get_lhapdf_id()) 1651 self.combine_wgt = mymod.get_wgt 1652 1653 if self.multicore == 'create': 1654 print "compile OLP", data['paths'][1] 1655 try: 1656 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'), 1657 nb_core=self.mother.options['nb_core']) 1658 except: 1659 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'), 1660 nb_core=1) 1661 elif has_nlo and not second and self.rwgt_mode == ['NLO_tree']: 1662 # We do not have any virtual reweighting to do but we still have to 1663 #combine the weights. 1664 #Idea:create a fake directory. 1665 start = time.time() 1666 commandline='import model loop_sm;generate g g > e+ ve [virt=QCD]' 1667 # deactivate golem since it creates troubles 1668 old_options = dict(mgcmd.options) 1669 mgcmd.options['golem'] = None 1670 mgcmd.options['pjfry'] = None 1671 commandline = commandline.replace('add process', 'generate',1) 1672 logger.info(commandline) 1673 mgcmd.exec_cmd(commandline, precmd=True) 1674 commandline = 'output standalone_rw %s --prefix=int -f' % pjoin(path_me, data['paths'][1]) 1675 mgcmd.exec_cmd(commandline, precmd=True) 1676 #put back golem to original value 1677 mgcmd.options['golem'] = old_options['golem'] 1678 mgcmd.options['pjfry'] = old_options['pjfry'] 1679 # update make_opts 1680 m_opts = {} 1681 if mgcmd.options['lhapdf']: 1682 #lhapdfversion = subprocess.Popen([mgcmd.options['lhapdf'], '--version'], 1683 # stdout = subprocess.PIPE).stdout.read().strip()[0] 1684 m_opts['lhapdf'] = True 1685 m_opts['f2pymode'] = True 1686 m_opts['lhapdfversion'] = 5 # 6 always fail on my computer since 5 is compatible but slower always use 5 1687 m_opts['llhapdf'] = self.mother.get_lhapdf_libdir() 1688 else: 1689 raise Exception, "NLO_tree reweighting requires LHAPDF to work correctly" 1690 1691 path = pjoin(path_me,data['paths'][1], 'Source', 'make_opts') 1692 common_run_interface.CommonRunCmd.update_make_opts_full(path, m_opts) 1693 logger.info('Done %.4g' % (time.time()-start)) 1694 1695 # Download LHAPDF SET 1696 common_run_interface.CommonRunCmd.install_lhapdf_pdfset_static(\ 1697 mgcmd.options['lhapdf'], None, self.banner.run_card.get_lhapdf_id()) 1698 1699 #compile the module to combine the weight 1700 misc.compile(cwd=pjoin(path_me, data['paths'][1], 'Source')) 1701 #link it 1702 with misc.chdir(pjoin(path_me)): 1703 if path_me not in sys.path: 1704 sys.path.insert(0, path_me) 1705 mymod = __import__('%s.Source.rwgt2py' % data['paths'][1], globals(), locals(), [],-1) 1706 mymod = mymod.Source.rwgt2py 1707 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1708 mymod.initialise([self.banner.run_card['lpp1'], 1709 self.banner.run_card['lpp2']], 1710 self.banner.run_card.get_lhapdf_id()) 1711 self.combine_wgt = mymod.get_wgt 1712 1713 1714 # 6. If we need a new model/process------------------------------------- 1715 if (self.second_model or self.second_process or self.dedicated_path) and not second : 1716 self.create_standalone_directory(second=True) 1717 1718 if not second: 1719 self.has_nlo = has_nlo
1720 1721 1722
1723 - def compile(self):
1724 """compile the code""" 1725 1726 if self.multicore=='wait': 1727 return 1728 1729 if not self.rwgt_dir: 1730 path_me = self.me_dir 1731 else: 1732 path_me = self.rwgt_dir 1733 for onedir in self.rwgt_dir_possibility: 1734 if not os.path.isdir(pjoin(path_me,onedir)): 1735 continue 1736 pdir = pjoin(path_me, onedir, 'SubProcesses') 1737 if self.mother: 1738 nb_core = self.mother.options['nb_core'] if self.mother.options['run_mode'] !=0 else 1 1739 else: 1740 nb_core = 1 1741 os.environ['MENUM'] = '2' 1742 misc.compile(['allmatrix2py.so'], cwd=pdir, nb_core=nb_core) 1743 if not (self.second_model or self.second_process or self.dedicated_path): 1744 os.environ['MENUM'] = '3' 1745 misc.compile(['allmatrix3py.so'], cwd=pdir, nb_core=nb_core)
1746
1747 - def load_module(self, metag=1):
1748 """load the various module and load the associate information""" 1749 1750 if not self.rwgt_dir: 1751 path_me = self.me_dir 1752 else: 1753 path_me = self.rwgt_dir 1754 1755 self.id_to_path = {} 1756 self.id_to_path_second = {} 1757 for onedir in self.rwgt_dir_possibility: 1758 if not os.path.exists(pjoin(path_me,onedir)): 1759 continue 1760 pdir = pjoin(path_me, onedir, 'SubProcesses') 1761 for tag in [2*metag,2*metag+1]: 1762 with misc.TMP_variable(sys, 'path', [pjoin(path_me)]+sys.path): 1763 mod_name = '%s.SubProcesses.allmatrix%spy' % (onedir, tag) 1764 #mymod = __import__('%s.SubProcesses.allmatrix%spy' % (onedir, tag), globals(), locals(), [],-1) 1765 if mod_name in sys.modules.keys(): 1766 del sys.modules[mod_name] 1767 tmp_mod_name = mod_name 1768 while '.' in tmp_mod_name: 1769 tmp_mod_name = tmp_mod_name.rsplit('.',1)[0] 1770 del sys.modules[tmp_mod_name] 1771 mymod = __import__(mod_name, globals(), locals(), [],-1) 1772 else: 1773 mymod = __import__(mod_name, globals(), locals(), [],-1) 1774 1775 S = mymod.SubProcesses 1776 mymod = getattr(S, 'allmatrix%spy' % tag) 1777 1778 # Param card not available -> no initialisation 1779 self.f2pylib[(onedir,tag)] = mymod 1780 if hasattr(mymod, 'set_madloop_path'): 1781 mymod.set_madloop_path(pjoin(path_me,onedir,'SubProcesses','MadLoop5_resources')) 1782 if (self.second_model or self.second_process or self.dedicated_path): 1783 break 1784 1785 data = self.id_to_path 1786 if '_second' in onedir: 1787 data = self.id_to_path_second 1788 1789 # get all the information 1790 all_pdgs = mymod.get_pdg_order() 1791 all_pdgs = [[pdg for pdg in pdgs if pdg!=0] for pdgs in mymod.get_pdg_order()] 1792 all_prefix = [''.join(j).strip().lower() for j in mymod.get_prefix()] 1793 prefix_set = set(all_prefix) 1794 1795 1796 hel_dict={} 1797 for prefix in prefix_set: 1798 if hasattr(mymod,'%sprocess_nhel' % prefix): 1799 nhel = getattr(mymod, '%sprocess_nhel' % prefix).nhel 1800 hel_dict[prefix] = {} 1801 for i, onehel in enumerate(zip(*nhel)): 1802 hel_dict[prefix][tuple(onehel)] = i+1 1803 elif hasattr(mymod, 'set_madloop_path') and \ 1804 os.path.exists(pjoin(path_me,onedir,'SubProcesses','MadLoop5_resources', '%sHelConfigs.dat' % prefix.upper())): 1805 hel_dict[prefix] = {} 1806 for i,line in enumerate(open(pjoin(path_me,onedir,'SubProcesses','MadLoop5_resources', '%sHelConfigs.dat' % prefix.upper()))): 1807 onehel = [int(h) for h in line.split()] 1808 hel_dict[prefix][tuple(onehel)] = i+1 1809 else: 1810 misc.sprint(pjoin(path_me,onedir,'SubProcesses','MadLoop5_resources', '%sHelConfigs.dat' % prefix.upper() )) 1811 misc.sprint(os.path.exists(pjoin(path_me,onedir,'SubProcesses','MadLoop5_resources', '%sHelConfigs.dat' % prefix.upper()))) 1812 continue 1813 1814 for i,pdg in enumerate(all_pdgs): 1815 if self.is_decay: 1816 incoming = [pdg[0]] 1817 outgoing = pdg[1:] 1818 else: 1819 incoming = pdg[0:2] 1820 outgoing = pdg[2:] 1821 order = (list(incoming), list(outgoing)) 1822 incoming.sort() 1823 if not self.keep_ordering: 1824 outgoing.sort() 1825 tag = (tuple(incoming), tuple(outgoing)) 1826 if 'virt' in onedir: 1827 tag = (tag, 'V') 1828 prefix = all_prefix[i] 1829 if prefix in hel_dict: 1830 hel = hel_dict[prefix] 1831 else: 1832 hel = {} 1833 if tag in data: 1834 oldpdg = data[tag][0][0]+data[tag][0][1] 1835 if all_prefix[all_pdgs.index(pdg)] == all_prefix[all_pdgs.index(oldpdg)]: 1836 for i in range(len(pdg)): 1837 if pdg[i] == oldpdg[i]: 1838 continue 1839 if not self.model or not hasattr(self.model, 'get_mass'): 1840 continue 1841 if self.model.get_mass(int(pdg[i])) == self.model.get_mass(int(oldpdg[i])): 1842 continue 1843 misc.sprint(tag, onedir) 1844 misc.sprint(data[tag][:-1]) 1845 misc.sprint(order, pdir,) 1846 raise Exception 1847 else: 1848 misc.sprint(all_prefix[all_pdgs.index(pdg)]) 1849 misc.sprint(all_prefix[all_pdgs.index(oldpdg)]) 1850 misc.sprint(tag, onedir) 1851 misc.sprint(data[tag][:-1]) 1852 misc.sprint(order, pdir,) 1853 raise Exception, "two different matrix-element have the same initial/final state. Leading to an ambiguity. If your events are ALWAYS written in the correct-order (look at the numbering in the Feynman Diagram). Then you can add inside your reweight_card the line 'change keep_ordering True'." 1854 1855 data[tag] = order, pdir, hel
1856 1857
1858 - def load_model(self, name, use_mg_default, complex_mass=False):
1859 """load the model""" 1860 1861 loop = False 1862 1863 logger.info('detected model: %s. Loading...' % name) 1864 model_path = name 1865 1866 # Import model 1867 base_model = import_ufo.import_model(name, decay=False, 1868 complex_mass_scheme=complex_mass) 1869 1870 if use_mg_default: 1871 base_model.pass_particles_name_in_mg_default() 1872 1873 self.model = base_model 1874 self.mg5cmd._curr_model = self.model 1875 self.mg5cmd.process_model()
1876 1877
1878 - def save_to_pickle(self):
1879 import madgraph.iolibs.save_load_object as save_load_object 1880 1881 to_save = {} 1882 to_save['id_to_path'] = self.id_to_path 1883 if hasattr(self, 'id_to_path_second'): 1884 to_save['id_to_path_second'] = self.id_to_path_second 1885 else: 1886 to_save['id_to_path_second'] = {} 1887 to_save['all_cross_section'] = self.all_cross_section 1888 to_save['processes'] = self.processes 1889 to_save['second_process'] = self.second_process 1890 if self.second_model: 1891 to_save['second_model'] =True 1892 else: 1893 to_save['second_model'] = None 1894 to_save['rwgt_dir'] = self.rwgt_dir 1895 to_save['has_nlo'] = self.has_nlo 1896 to_save['rwgt_mode'] = self.rwgt_mode 1897 to_save['rwgt_name'] = self.options['rwgt_name'] 1898 1899 name = pjoin(self.rwgt_dir, 'rw_me', 'rwgt.pkl') 1900 save_load_object.save_to_file(name, to_save)
1901 1902
1903 - def load_from_pickle(self, keep_name=False):
1904 import madgraph.iolibs.save_load_object as save_load_object 1905 1906 obj = save_load_object.load_from_file( pjoin(self.rwgt_dir, 'rw_me', 'rwgt.pkl')) 1907 1908 self.has_standalone_dir = True 1909 self.options = {'curr_dir': os.path.realpath(os.getcwd()), 1910 'rwgt_name': None} 1911 if keep_name: 1912 self.options['rwgt_name'] = obj['rwgt_name'] 1913 1914 old_rwgt = obj['rwgt_dir'] 1915 1916 # path to fortran executable 1917 self.id_to_path = {} 1918 for key , (order, Pdir, hel_dict) in obj['id_to_path'].items(): 1919 new_P = Pdir.replace(old_rwgt, self.rwgt_dir) 1920 self.id_to_path[key] = [order, new_P, hel_dict] 1921 1922 # path to fortran executable (for second directory) 1923 self.id_to_path_second = {} 1924 for key , (order, Pdir, hel_dict) in obj['id_to_path_second'].items(): 1925 new_P = Pdir.replace(old_rwgt, self.rwgt_dir) 1926 self.id_to_path_second[key] = [order, new_P, hel_dict] 1927 1928 self.all_cross_section = obj['all_cross_section'] 1929 self.processes = obj['processes'] 1930 self.second_process = obj['second_process'] 1931 self.second_model = obj['second_model'] 1932 self.has_nlo = obj['has_nlo'] 1933 if not self.rwgt_mode: 1934 self.rwgt_mode = obj['rwgt_mode'] 1935 logger.info("mode set to %s" % self.rwgt_mode) 1936 if self.has_nlo and 'NLO' in self.rwgt_mode: 1937 path = pjoin(obj['rwgt_dir'], 'rw_mevirt','Source') 1938 sys.path.insert(0, path) 1939 try: 1940 mymod = __import__('rwgt2py', globals(), locals()) 1941 except ImportError: 1942 misc.compile(['rwgt2py.so'], cwd=path) 1943 mymod = __import__('rwgt2py', globals(), locals()) 1944 with misc.stdchannel_redirected(sys.stdout, os.devnull): 1945 mymod.initialise([self.banner.run_card['lpp1'], 1946 self.banner.run_card['lpp2']], 1947 self.banner.run_card.get_lhapdf_id()) 1948 self.combine_wgt = mymod.get_wgt
1949