1
2
3
4
5
6
7
8
9
10
11
12
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
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
50
51
52 logger = logging.getLogger('decay.stdout')
53 logger_stderr = logging.getLogger('decay.stderr')
54 cmd_logger = logging.getLogger('cmdprint2')
55
56
57 dir_to_f2py_free_mod = {}
58 nb_f2py_module = 0
65 """Basic interface for reweighting operation"""
66
67 prompt = 'Reweight>'
68 debug_output = 'Reweight_debug'
69
70 @misc.mute_logger()
71 - def __init__(self, event_path=None, allow_madspin=False, mother=None, *completekey, **stdin):
72 """initialize the interface with potentially an event_path"""
73
74 if not event_path:
75 cmd_logger.info('************************************************************')
76 cmd_logger.info('* *')
77 cmd_logger.info('* Welcome to Reweight Module *')
78 cmd_logger.info('* *')
79 cmd_logger.info('************************************************************')
80 extended_cmd.Cmd.__init__(self, *completekey, **stdin)
81
82 self.model = None
83 self.has_standalone_dir = False
84 self.mother= mother
85 self.multicore=False
86
87 self.options = {'curr_dir': os.path.realpath(os.getcwd()),
88 'rwgt_name':None}
89
90 self.events_file = None
91 self.processes = {}
92 self.second_model = None
93 self.second_process = None
94 self.mg5cmd = master_interface.MasterCmd()
95 if mother:
96 self.mg5cmd.options.update(mother.options)
97 self.seed = None
98 self.output_type = "default"
99 self.helicity_reweighting = True
100 self.rwgt_mode = ''
101 self.has_nlo = False
102 self.rwgt_dir = None
103 self.exitted = False
104
105 if event_path:
106 logger.info("Extracting the banner ...")
107 self.do_import(event_path, allow_madspin=allow_madspin)
108
109
110 self.calculator = {}
111 self.calculator_nbcall = {}
112
113
114 self.all_cross_section = {}
115
116 - def do_import(self, inputfile, allow_madspin=False):
117 """import the event file"""
118
119 args = self.split_arg(inputfile)
120 if not args:
121 return self.InvalidCmd, 'import requires arguments'
122
123
124 self.options['curr_dir'] = os.path.realpath(os.path.dirname(inputfile))
125 if os.path.basename(os.path.dirname(os.path.dirname(inputfile))) == 'Events':
126 self.options['curr_dir'] = pjoin(self.options['curr_dir'],
127 os.path.pardir, os.pardir)
128
129
130 if not os.path.exists(inputfile):
131 if inputfile.endswith('.gz'):
132 if not os.path.exists(inputfile[:-3]):
133 raise self.InvalidCmd('No such file or directory : %s' % inputfile)
134 else:
135 inputfile = inputfile[:-3]
136 elif os.path.exists(inputfile + '.gz'):
137 inputfile = inputfile + '.gz'
138 else:
139 raise self.InvalidCmd('No such file or directory : %s' % inputfile)
140
141 if inputfile.endswith('.gz'):
142 misc.gunzip(inputfile)
143 inputfile = inputfile[:-3]
144
145
146 self.lhe_input = lhe_parser.EventFile(os.path.realpath(inputfile))
147 if not self.lhe_input.banner:
148 value = self.ask("What is the path to banner", 0, [0], "please enter a path", timeout=0)
149 self.lhe_input.banner = open(value).read()
150 self.banner = self.lhe_input.get_banner()
151
152
153 if 'init' not in self.banner:
154 self.orig_cross = (0,0)
155
156 else:
157 for line in self.banner['init'].split('\n'):
158 split = line.split()
159 if len(split) == 4:
160 cross, error = float(split[0]), float(split[1])
161 self.orig_cross = (cross, error)
162
163
164
165
166 if 'slha' not in self.banner:
167 self.events_file = None
168 raise self.InvalidCmd('Event file does not contain model information')
169 elif 'mg5proccard' not in self.banner:
170 self.events_file = None
171 raise self.InvalidCmd('Event file does not contain generation information')
172
173 if 'madspin' in self.banner and not allow_madspin:
174 raise self.InvalidCmd('Reweight should be done before running MadSpin')
175
176
177
178 process = self.banner.get_detail('proc_card', 'generate')
179 if '[' in process and isinstance(self.banner.get('run_card'), banner.RunCardNLO):
180 if not self.banner.get_detail('run_card', 'store_rwgt_info'):
181 logger.warning("The information to perform a proper NLO reweighting is not present in the event file.")
182 logger.warning(" We will perform a LO reweighting instead. This does not guarantee NLO precision.")
183 self.rwgt_mode = 'LO'
184
185 if 'OLP' in self.mother.options:
186 if self.mother.options['OLP'].lower() != 'madloop':
187 logger.warning("Accurate NLO mode only works for OLP=MadLoop not for OLP=%s. An approximate (LO) reweighting will be performed instead")
188 self.rwgt_mode = 'LO'
189
190 if 'lhapdf' in self.mother.options and not self.mother.options['lhapdf']:
191 logger.warning('NLO accurate reweighting requires lhapdf to be installed. Pass in approximate LO mode.')
192 self.rwgt_mode = 'LO'
193 else:
194 self.rwgt_mode = 'LO'
195
196 if not process:
197 msg = 'Invalid proc_card information in the file (no generate line):\n %s' % self.banner['mg5proccard']
198 raise Exception, msg
199 process, option = mg_interface.MadGraphCmd.split_process_line(process)
200 self.proc_option = option
201
202 logger.info("process: %s" % process)
203 logger.info("options: %s" % option)
204
205
206 @staticmethod
208 """return the LO definitions of the process corresponding to the born/real"""
209
210
211 process, order, final = re.split('\[\s*(.*)\s*\]', proc)
212
213 commandline="add process %s %s --no_warning=duplicate;" % (process, final)
214 if not order:
215
216 return proc
217 elif not order.startswith(('virt','loonly','noborn')):
218
219 if real_only:
220 commandline= ''
221
222 if '=' in order:
223
224 order = order.split('=',1)[1]
225
226
227 pert = fks_common.find_pert_particles_interactions(model,
228 pert_order = order)['soft_particles']
229 commandline += "define pert_%s = %s;" % (order.replace(' ',''), ' '.join(map(str,pert)) )
230
231
232
233 if '%s=' % order in process or '%s<=' % order in process:
234 result=re.split(' ',process)
235 process=''
236 for r in result:
237 if '%s=' % order in r:
238 ior=re.split('=',r)
239 r='QCD=%i' % (int(ior[1])+1)
240 elif '%s<=' % order in r:
241 ior=re.split('=',r)
242 r='QCD<=%i' % (int(ior[1])+1)
243 process=process+r+' '
244
245 result = re.split('([/$@]|\w+(?:^2)?(?:=|<=|>)?\w+)', process, 1)
246 if len(result) ==3:
247 process, split, rest = result
248 commandline+="add process %s pert_%s %s%s %s --no_warning=duplicate;" % (process, order.replace(' ','') ,split, rest, final)
249 else:
250 commandline +='add process %s pert_%s %s --no_warning=duplicate;' % (process,order.replace(' ',''), final)
251 elif order.startswith(('noborn=')):
252
253 return "add process %s ;" % proc.replace('noborn=', 'sqrvirt=')
254
255 else:
256
257 return "add process %s ;" % proc
258 return commandline
259
260
262 """Check some basic property of the events file"""
263
264 sum_of_weight = 0
265 sum_of_abs_weight = 0
266 negative_event = 0
267 positive_event = 0
268
269 start = time.time()
270 for event_nb,event in enumerate(self.lhe_input):
271
272 if (event_nb % max(int(10**int(math.log10(float(event_nb)+1))),10)==0):
273 running_time = misc.format_timer(time.time()-start)
274 logger.info('Event nb %s %s' % (event_nb, running_time))
275 if (event_nb==10001): logger.info('reducing number of print status. Next status update in 10000 events')
276
277 try:
278 event.check()
279 except Exception, error:
280 print event
281 raise error
282 sum_of_weight += event.wgt
283 sum_of_abs_weight += abs(event.wgt)
284 if event.wgt < 0 :
285 negative_event +=1
286 else:
287 positive_event +=1
288
289 logger.info("total cross-section: %s" % sum_of_weight)
290 logger.info("total abs cross-section: %s" % sum_of_abs_weight)
291 logger.info("fraction of negative event %s", negative_event/(negative_event+positive_event))
292 logger.info("total number of events %s", (negative_event+positive_event))
293 logger.info("negative event %s", negative_event)
294
295
296
297
298 @extended_cmd.debug()
300 "Complete the import command"
301
302 args=self.split_arg(line[0:begidx])
303
304 if len(args) == 1:
305 base_dir = '.'
306 else:
307 base_dir = args[1]
308
309 return self.path_completion(text, base_dir)
310
311
312 if os.path.sep in args[-1] + text:
313 return self.path_completion(text,
314 pjoin(*[a for a in args if \
315 a.endswith(os.path.sep)]))
316
318 """help for change command"""
319
320 print "change model X :use model X for the reweighting"
321 print "change process p p > e+ e-: use a new process for the reweighting"
322 print "change process p p > mu+ mu- --add : add one new process to existing ones"
323
325 """allow to define a second model/processes"""
326
327 global nb_f2py_module
328
329 args = self.split_arg(line)
330 if len(args)<2:
331 logger.critical("not enough argument (need at least two). Discard line")
332 if args[0] == "model":
333 nb_f2py_module += 1
334 self.second_model = " ".join(args[1:])
335 if self.has_standalone_dir:
336 self.terminate_fortran_executables()
337 self.has_standalone_dir = False
338 elif args[0] == "process":
339 nb_f2py_module += 1
340 if self.has_standalone_dir:
341 self.terminate_fortran_executables()
342 self.has_standalone_dir = False
343 if args[-1] == "--add":
344 self.second_process.append(" ".join(args[1:-1]))
345 else:
346 self.second_process = [" ".join(args[1:])]
347 elif args[0] == "output":
348 if args[1] in ['default', '2.0', 'unweight']:
349 self.output_type = args[1]
350 elif args[0] == "helicity":
351 self.helicity_reweighting = banner.ConfigFile.format_variable(args[1], bool, "helicity")
352 elif args[0] == "mode":
353 if args[1] != 'LO':
354 if 'OLP' in self.mother.options and self.mother.options['OLP'].lower() != 'madloop':
355 logger.warning("Only LO reweighting is allowed for OLP!=MadLoop. Keeping the mode to LO.")
356 self.rwgt_mode = 'LO'
357 elif not self.banner.get_detail('run_card','store_rwgt_info', default=False):
358 logger.warning("Missing information for NLO type of reweighting. Keeping the mode to LO.")
359 self.rwgt_mode = 'LO'
360 elif 'lhapdf' in self.mother.options and not self.mother.options['lhapdf']:
361 logger.warning('NLO accurate reweighting requires lhapdf to be installed. Pass in approximate LO mode.')
362 self.rwgt_mode = 'LO'
363 else:
364 self.rwgt_mode = args[1]
365 else:
366 self.rwgt_mode = args[1]
367 elif args[0] == "rwgt_dir":
368 self.rwgt_dir = args[1]
369 if not os.path.exists(self.rwgt_dir):
370 os.mkdir(self.rwgt_dir)
371 elif args[0] == 'multicore':
372 pass
373
374
375 else:
376 logger.critical("unknown option! %s. Discard line." % args[0])
377
378
380 """check the validity of the launch command"""
381
382 if not self.lhe_input:
383 if isinstance(self.lhe_input, lhe_parser.EventFile):
384 self.lhe_input = lhe_parser.EventFile(self.lhe_input.name)
385 else:
386 raise self.InvalidCmd("No events files defined.")
387
388 opts = {'rwgt_name':None}
389 if any(a.startswith('--') for a in args):
390 for a in args[:]:
391 if a.startswith('--') and '=' in a:
392 key,value = a[2:].split('=')
393 opts[key] = value .replace("'","") .replace('"','')
394 return opts
395
397 """help for the launch command"""
398
399 logger.info('''Add to the loaded events a weight associated to a
400 new param_card (to be define). The weight returned is the ratio of the
401 square matrix element by the squared matrix element of production.
402 All scale are kept fix for this re-weighting.''')
403
404
406 """ return the various name for the computed weights """
407
408 if self.rwgt_mode == 'LO':
409 return ['']
410 elif self.rwgt_mode == 'NLO':
411 return ['_nlo']
412 elif self.rwgt_mode == 'LO+NLO':
413 return ['_lo', '_nlo']
414 elif self.rwgt_mode == 'NLO_tree':
415 return ['_tree']
416 elif not self.rwgt_mode and self.has_nlo :
417 return ['_nlo']
418 else:
419 return ['']
420
421 @misc.mute_logger()
423 """end of the configuration launched the code"""
424
425 args = self.split_arg(line)
426 opts = self.check_launch(args)
427 if opts['rwgt_name']:
428 self.options['rwgt_name'] = opts['rwgt_name']
429
430 model_line = self.banner.get('proc_card', 'full_model_line')
431
432 if not self.has_standalone_dir:
433 if self.rwgt_dir and os.path.exists(pjoin(self.rwgt_dir,'rw_me','rwgt.pkl')):
434 self.load_from_pickle()
435 if not self.rwgt_dir:
436 self.me_dir = self.rwgt_dir
437 elif self.multicore == 'wait':
438 while not os.path.exists(pjoin(self.me_dir,'rw_me','rwgt.pkl')):
439 time.sleep(60)
440 print 'wait for pickle'
441 print "loading from pickle"
442 if not self.rwgt_dir:
443 self.rwgt_dir = self.me_dir
444 self.load_from_pickle(keep_name=True)
445 else:
446 self.create_standalone_directory()
447 if self.multicore == 'create':
448 if not self.rwgt_dir:
449 self.rwgt_dir = self.me_dir
450 self.save_to_pickle()
451
452 if self.rwgt_dir:
453 path_me =self.rwgt_dir
454 else:
455 path_me = self.me_dir
456
457 if self.second_model or self.second_process:
458 rw_dir = pjoin(path_me, 'rw_me_second')
459 else:
460 rw_dir = pjoin(path_me, 'rw_me')
461
462 if not '--keep_card' in args:
463 ff = open(pjoin(rw_dir,'Cards', 'param_card.dat'), 'w')
464 ff.write(self.banner['slha'])
465 ff.close()
466 if self.has_nlo and self.rwgt_mode != "LO":
467 rwdir_virt = rw_dir.replace('rw_me', 'rw_mevirt')
468 files.ln(ff.name, starting_dir=pjoin(rwdir_virt, 'Cards'))
469 ff = open(pjoin(path_me, 'rw_me','Cards', 'param_card_orig.dat'), 'w')
470 ff.write(self.banner['slha'])
471 ff.close()
472 if self.has_nlo and self.rwgt_mode != "LO":
473 files.ln(ff.name, starting_dir=pjoin(path_me, 'rw_mevirt', 'Cards'))
474 cmd = common_run_interface.CommonRunCmd.ask_edit_card_static(cards=['param_card.dat'],
475 ask=self.ask, pwd=rw_dir, first_cmd=self.stored_line)
476 self.stored_line = None
477
478
479 type_rwgt = self.get_weight_names()
480
481
482 new_card = open(pjoin(rw_dir, 'Cards', 'param_card.dat')).read()
483 pattern_scan = re.compile(r'''^[\s\d]*scan''', re.I+re.M)
484 param_card_iterator = []
485 if pattern_scan.search(new_card):
486 try:
487 import internal.extended_cmd as extended_internal
488 Shell_internal = extended_internal.CmdShell
489 except:
490 Shell_internal = None
491 import madgraph.interface.extended_cmd as extended_cmd
492 if not isinstance(self.mother, (extended_cmd.CmdShell, Shell_internal)):
493 raise Exception, "scan are not allowed on the Web"
494
495 main_card = check_param_card.ParamCardIterator(new_card)
496 if self.options['rwgt_name']:
497 self.options['rwgt_name'] = '%s_0' % self.options['rwgt_name']
498
499 param_card_iterator = main_card
500 first_card = param_card_iterator.next(autostart=True)
501 new_card = first_card.write()
502 first_card.write(pjoin(rw_dir, 'Cards', 'param_card.dat'))
503
504 if "auto" in new_card.lower():
505 self.mother.check_param_card(pjoin(rw_dir, 'Cards', 'param_card.dat'))
506 new_card = open(pjoin(rw_dir, 'Cards', 'param_card.dat')).read()
507
508
509
510 if 'initrwgt' in self.banner:
511 if 'name=\'mg_reweighting\'' in self.banner['initrwgt']:
512 blockpat = re.compile(r'''<weightgroup name=\'mg_reweighting\'\s*>(?P<text>.*?)</weightgroup>''', re.I+re.M+re.S)
513 before, content, after = blockpat.split(self.banner['initrwgt'])
514 header_rwgt_other = before + after
515 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)
516 mg_rwgt_info = pattern.findall(content)
517
518 maxid = 0
519 for k,(i, fulltag, nlotype, diff) in enumerate(mg_rwgt_info):
520 if i:
521 if int(i) > maxid:
522 maxid = int(i)
523 mg_rwgt_info[k] = (i, nlotype, diff)
524 else:
525 mg_rwgt_info[k] = (fulltag, nlotype, diff)
526
527 maxid += 1
528 rewgtid = maxid
529 if self.options['rwgt_name']:
530
531 for (i, nlotype, diff) in mg_rwgt_info[:]:
532 for flag in type_rwgt:
533 if 'rwgt_%s' % i == '%s%s' %(self.options['rwgt_name'],flag) or \
534 i == '%s%s' % (self.options['rwgt_name'], flag):
535 logger.warning("tag %s%s already defines, will replace it", self.options['rwgt_name'],flag)
536 mg_rwgt_info.remove((i, nlotype, diff))
537
538 else:
539 header_rwgt_other = self.banner['initrwgt']
540 mg_rwgt_info = []
541 rewgtid = 1
542 else:
543 self.banner['initrwgt'] = ''
544 header_rwgt_other = ''
545 mg_rwgt_info = []
546 rewgtid = 1
547
548
549
550 s_orig = self.banner['slha']
551 s_new = new_card
552
553
554 if self.options['rwgt_name']:
555 tag = self.options['rwgt_name']
556 else:
557 tag = str(rewgtid)
558
559 if not self.second_model:
560 old_param = check_param_card.ParamCard(s_orig.splitlines())
561 new_param = check_param_card.ParamCard(s_new.splitlines())
562 card_diff = old_param.create_diff(new_param)
563 if card_diff == '' and not self.second_process:
564 if not __debug__:
565 logger.warning(' REWEIGHTING: original card and new card are identical. Bypass this run')
566 return
567 else:
568 logger.warning(' REWEIGHTING: original card and new card are identical. Run it due to debug mode')
569
570 try:
571 if old_param['sminputs'].get(3)- new_param['sminputs'].get(3) > 1e-3 * new_param['sminputs'].get(3):
572 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.")
573 except Exception, error:
574 logger.debug("error in check of alphas: %s" % str(error))
575 pass
576 if not self.second_process:
577 for name in type_rwgt:
578 mg_rwgt_info.append((tag, name, card_diff))
579 else:
580 str_proc = "\n change process ".join([""]+self.second_process)
581 for name in type_rwgt:
582 mg_rwgt_info.append((tag, name, str_proc + '\n'+ card_diff))
583 else:
584 str_info = "change model %s" % self.second_model
585 if self.second_process:
586 str_info += "\n change process ".join([""]+self.second_process)
587 card_diff = str_info
588 str_info += '\n' + s_new
589 for name in type_rwgt:
590 mg_rwgt_info.append((tag, name, str_info))
591
592 self.banner['initrwgt'] = header_rwgt_other
593 self.banner['initrwgt'] += '\n<weightgroup name=\'mg_reweighting\'>\n'
594 for tag, rwgttype, diff in mg_rwgt_info:
595 if tag.isdigit():
596 self.banner['initrwgt'] += '<weight id=\'rwgt_%s%s\'>%s</weight>\n' % \
597 (tag, rwgttype, diff)
598 else:
599 self.banner['initrwgt'] += '<weight id=\'%s%s\'>%s</weight>\n' % \
600 (tag, rwgttype, diff)
601 self.banner['initrwgt'] += '\n</weightgroup>\n'
602 self.banner['initrwgt'] = self.banner['initrwgt'].replace('\n\n', '\n')
603
604
605 start = time.time()
606 cross, ratio, ratio_square,error = {},{},{}, {}
607 for name in type_rwgt + ['orig']:
608 cross[name], error[name] = 0.,0.
609 ratio[name],ratio_square[name] = 0., 0.
610
611 if self.output_type == "default":
612 output = open( self.lhe_input.name +'rw', 'w')
613
614 self.banner.write(output, close_tag=False)
615 else:
616 output = {}
617 for name in type_rwgt:
618 output[name] = open( self.lhe_input.name +'rw'+name, 'w')
619
620 self.banner.write(output[name], close_tag=False)
621
622 logger.info('starts to compute weight for events with the following modification to the param_card:')
623 logger.info(card_diff.replace('\n','\nKEEP:'))
624
625 if self.mother:
626 out_path = pjoin(self.mother.me_dir, 'Events', 'reweight.lhe')
627 output2 = open(out_path, 'w')
628 lha_strategy = self.banner.get_lha_strategy()
629 self.banner.set_lha_strategy(4*lha_strategy/abs(lha_strategy))
630 self.banner.write(output2, close_tag=False)
631 self.banner.set_lha_strategy(lha_strategy)
632 new_banner = banner.Banner(self.banner)
633 if not hasattr(self, 'run_card'):
634 self.run_card = new_banner.charge_card('run_card')
635 self.run_card['run_tag'] = 'reweight_%s' % rewgtid
636 new_banner['slha'] = s_new
637 del new_banner['initrwgt']
638 assert 'initrwgt' in self.banner
639 ff = open(pjoin(self.mother.me_dir,'Events',self.mother.run_name, '%s_%s_banner.txt' % \
640 (self.mother.run_name, self.run_card['run_tag'])),'w')
641 new_banner.write(ff)
642 ff.close()
643
644
645 if self.options['rwgt_name']:
646 tag_name = self.options['rwgt_name']
647 else:
648 tag_name = 'rwgt_%s' % rewgtid
649
650 os.environ['GFORTRAN_UNBUFFERED_ALL'] = 'y'
651 if self.lhe_input.closed:
652 self.lhe_input = lhe_parser.EventFile(self.lhe_input.name)
653
654
655 nb_core = 1
656
657
658
659 self.lhe_input.seek(0)
660 for event_nb,event in enumerate(self.lhe_input):
661
662 if (event_nb % max(int(10**int(math.log10(float(event_nb)+1))),10)==0):
663 running_time = misc.format_timer(time.time()-start)
664 logger.info('Event nb %s %s' % (event_nb, running_time))
665 if (event_nb==10001): logger.info('reducing number of print status. Next status update in 10000 events')
666
667 if nb_core > 1:
668
669 while 1:
670 if multicore.queue.qsize() < 100 * nb_core:
671 multicore.submit(self.write_reweighted_event, argument=[event, tag_name])
672 break
673
674
675 continue
676 else:
677 weight = self.calculate_weight(event)
678 if not isinstance(weight, dict):
679 weight = {'':weight}
680
681 for name in weight:
682 cross[name] += weight[name]
683 ratio[name] += weight[name]/event.wgt
684 ratio_square[name] += (weight[name]/event.wgt)**2
685
686
687
688 for tag in type_rwgt:
689 try:
690 event.reweight_order.remove('%s%s' % (tag_name,tag))
691 except ValueError:
692 continue
693
694 event.reweight_order += ['%s%s' % (tag_name,name) for name in type_rwgt]
695 if self.output_type == "default":
696 for name in weight:
697 if 'orig' in name:
698 continue
699 event.reweight_data['%s%s' % (tag_name,name)] = weight[name]
700
701 output.write(str(event))
702 if self.mother:
703 event.wgt = weight[type_rwgt[0]]
704 event.reweight_data = {}
705 output2.write(str(event))
706 else:
707 for i,name in enumerate(weight):
708 event.wgt = weight[name]
709 event.reweight_data = {}
710 if self.mother and len(weight)==1:
711 output2.write(str(event))
712 elif self.mother and i == 0:
713 output[name].write(str(event))
714 output2.write(str(event))
715 else:
716 output[name].write(str(event))
717
718
719 if 'event_norm' in self.run_card:
720 if self.run_card['event_norm'] == 'average':
721 for key, value in cross.items():
722 cross[key] = value / (event_nb+1)
723
724 running_time = misc.format_timer(time.time()-start)
725 logger.info('All event done (nb_event: %s) %s' % (event_nb+1, running_time))
726
727
728 if self.output_type == "default":
729 output.write('</LesHouchesEvents>\n')
730 output.close()
731 else:
732 for key in output:
733 output[key].write('</LesHouchesEvents>\n')
734 output.close()
735
736 os.environ['GFORTRAN_UNBUFFERED_ALL'] = 'n'
737
738 if self.mother:
739 output2.write('</LesHouchesEvents>\n')
740 output2.close()
741
742 if hasattr(self.mother, 'results'):
743 run_name = self.mother.run_name
744 results = self.mother.results
745 results.add_run(run_name, self.run_card, current=True)
746 results.add_detail('nb_event', event_nb+1)
747 name = type_rwgt[0]
748 results.add_detail('cross', cross[name])
749 event_nb +=1
750 for name in type_rwgt:
751 variance = ratio_square[name]/event_nb - (ratio[name]/event_nb)**2
752 orig_cross, orig_error = self.orig_cross
753 error[name] = variance/math.sqrt(event_nb) * orig_cross + ratio[name]/event_nb * orig_error
754 results.add_detail('error', error[type_rwgt[0]])
755 import madgraph.interface.madevent_interface as ME_interface
756 if isinstance(self.mother, ME_interface.MadEventCmd):
757 self.mother.create_plot(mode='reweight', event_path=output2.name,
758 tag=self.run_card['run_tag'])
759
760 if 'plot' in results.current.reweight:
761 html_dir = pjoin(self.mother.me_dir, 'HTML', run_name)
762 td = pjoin(self.mother.options['td_path'], 'td')
763 MA = pjoin(self.mother.options['madanalysis_path'])
764 path1 = pjoin(html_dir, 'plots_parton')
765 path2 = pjoin(html_dir, 'plots_%s' % self.run_card['run_tag'])
766 outputplot = path2
767 combine_plots.merge_all_plots(path2, path1, outputplot, td, MA)
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782 self.lhe_input.close()
783 if not self.mother or self.output_type != "default" :
784 target = pjoin(self.mother.me_dir, 'Events', run_name, 'events.lhe')
785 else:
786 target = self.lhe_input.name
787
788 if self.output_type == "default":
789 files.mv(output.name, target)
790 logger.info('Event %s have now the additional weight' % self.lhe_input.name)
791 elif self.output_type == "unweight":
792 output2.close()
793 lhe = lhe_parser.EventFile(output2.name)
794 nb_event = lhe.unweight(target)
795 if self.mother and hasattr(self.mother, 'results'):
796 results = self.mother.results
797 results.add_detail('nb_event', nb_event)
798 results.current.parton.append('lhe')
799 logger.info('Event %s is now unweighted under the new theory' % output2.name)
800 else:
801 files.mv(output2.name, self.lhe_input.name)
802 if self.mother and hasattr(self.mother, 'results'):
803 results = self.mother.results
804 results.current.parton.append('lhe')
805 logger.info('Event %s is now created with new central weight' % output2.name)
806
807 if self.multicore != 'create':
808 for name in cross:
809 if name == 'orig':
810 continue
811 logger.info('new cross-section is %s: %g pb (indicative error: %g pb)' %\
812 ('(%s)' %name if name else '',cross[name], error[name]))
813
814 self.terminate_fortran_executables(new_card_only=True)
815
816 for name in cross:
817 if name == 'orig':
818 self.all_cross_section[name] = (cross[name], error[name])
819 else:
820 self.all_cross_section[(tag_name,name)] = (cross[name], error[name])
821
822
823 if param_card_iterator:
824 for i,card in enumerate(param_card_iterator):
825 if self.options['rwgt_name']:
826 self.options['rwgt_name'] = '%s_%s' % (self.options['rwgt_name'].rsplit('_',1)[0], i+1)
827 card.write(pjoin(rw_dir, 'Cards', 'param_card.dat'))
828 self.exec_cmd("launch --keep_card", printcmd=False, precmd=True)
829
830 self.options['rwgt_name'] = None
831
833 "Not in help"
834
835 logger.warning("Invalid Syntax. The command 'set' should be placed after the 'launch' one. Continuing by adding automatically 'launch'")
836 self.stored_line = "set %s" % line
837 return self.exec_cmd("launch")
838
839 - def default(self, line, log=True):
840 """Default action if line is not recognized"""
841
842 if os.path.isfile(line):
843 if log:
844 logger.warning("Invalid Syntax. The path to a param_card' should be placed after the 'launch' command. Continuing by adding automatically 'launch'")
845 self.stored_line = line
846 return self.exec_cmd("launch")
847 else:
848 return super(ReweightInterface,self).default(line, log=log)
849
851 """a function for running in multicore"""
852
853 if not hasattr(opt['thread_space'], "calculator"):
854 opt['thread_space'].calculator = {}
855 opt['thread_space'].calculator_nbcall = {}
856 opt['thread_space'].cross = 0
857 opt['thread_space'].output = open( self.lhe_input.name +'rw.%s' % opt['thread_id'], 'w')
858 if self.mother:
859 out_path = pjoin(self.mother.me_dir, 'Events', 'reweight.lhe.%s' % opt['thread_id'])
860 opt['thread_space'].output2 = open(out_path, 'w')
861
862 weight = self.calculate_weight(event, space=opt['thread_space'])
863 opt['thread_space'].cross += weight
864 if self.output_type == "default":
865 event.reweight_data[tag_name] = weight
866
867 opt['thread_space'].output.write(str(event))
868 if self.mother:
869 event.wgt = weight
870 event.reweight_data = {}
871 opt['thread_space'].output2.write(str(event))
872 else:
873 event.wgt = weight
874 event.reweight_data = {}
875 if self.mother:
876 opt['thread_space'].output2.write(str(event))
877 else:
878 opt['thread_space'].output.write(str(event))
879
880 return 0
881
884
886 """space defines where to find the calculator (in multicore)"""
887
888 if self.has_nlo and self.rwgt_mode != "LO":
889 return self.calculate_nlo_weight(event, space)
890
891 if not space:
892 space = self
893 event.parse_reweight()
894
895
896 w_orig = self.calculate_matrix_element(event, 0, space)
897 w_new = self.calculate_matrix_element(event, 1, space)
898 if w_orig == 0:
899 tag, order = event.get_tag_and_order()
900 orig_order, Pdir, hel_dict = self.id_to_path[tag]
901 misc.sprint(w_orig, w_new)
902 misc.sprint(event)
903 misc.sprint(self.invert_momenta(event.get_momenta(orig_order)))
904 misc.sprint(event.get_momenta(orig_order))
905 misc.sprint(event.aqcd)
906 hel_order = event.get_helicity(orig_order)
907 if self.helicity_reweighting and 9 not in hel_order:
908 nhel = hel_dict[tuple(hel_order)]
909 else:
910 nhel = 0
911 misc.sprint(nhel, Pdir, hel_dict)
912 raise Exception, "Invalid matrix element for original computation (weight=0)"
913
914 return {'orig': event.wgt, '': w_new/w_orig*event.wgt}
915
917
918
919 type_nlo = self.get_weight_names()
920 final_weight = {'orig': event.wgt}
921
922 if not space:
923 space = self
924
925 event.parse_reweight()
926 event.parse_nlo_weight()
927
928
929 scales2 = []
930 pdg = []
931 bjx = []
932 wgt_tree = []
933 wgt_virt = []
934 base_wgt = []
935 gs=[]
936 qcdpower = []
937 ref_wgts = []
938
939 orig_wgt = 0
940 for cevent in event.nloweight.cevents:
941
942 need_V = False
943 all_ctype = [w.type for w in cevent.wgts]
944 if '_nlo' in type_nlo and any(c in all_ctype for c in [2,14,15]):
945 need_V =True
946
947 w_orig = self.calculate_matrix_element(cevent, 0, space)
948 w_new = self.calculate_matrix_element(cevent, 1, space)
949 ratio_T = w_new/w_orig
950 if need_V:
951 scale2 = cevent.wgts[0].scales2[0]
952
953 w_origV = self.calculate_matrix_element(cevent, 'V0', space, scale2=scale2)
954 w_newV = self.calculate_matrix_element(cevent, 'V1', space, scale2=scale2)
955 ratio_BV = (w_newV + w_new) / (w_origV + w_orig)
956 ratio_V = w_newV/w_origV
957 else:
958 ratio_V = "should not be used"
959 ratio_BV = "should not be used"
960
961 for c_wgt in cevent.wgts:
962 orig_wgt += c_wgt.ref_wgt
963
964 scales2.append(c_wgt.scales2)
965 pdg.append(c_wgt.pdgs[:2])
966
967 bjx.append(c_wgt.bjks)
968 qcdpower.append(c_wgt.qcdpower)
969 gs.append(c_wgt.gs)
970 ref_wgts.append(c_wgt.ref_wgt)
971
972 if '_nlo' in type_nlo:
973 if c_wgt.type in [2,14,15]:
974 R = ratio_BV
975 else:
976 R = ratio_T
977
978 new_wgt = [c_wgt.pwgt[0] * R,
979 c_wgt.pwgt[1] * ratio_T,
980 c_wgt.pwgt[2] * ratio_T]
981 wgt_virt.append(new_wgt)
982
983 if '_tree' in type_nlo:
984 new_wgt = [c_wgt.pwgt[0] * ratio_T,
985 c_wgt.pwgt[1] * ratio_T,
986 c_wgt.pwgt[2] * ratio_T]
987 wgt_tree.append(new_wgt)
988 base_wgt.append(c_wgt.pwgt[:3])
989
990
991 scales2 = self.invert_momenta(scales2)
992 pdg = self.invert_momenta(pdg)
993 bjx = self.invert_momenta(bjx)
994
995 base_wgt = self.invert_momenta(base_wgt)
996
997 orig_wgt_check, partial_check = self.combine_wgt(scales2, pdg, bjx, base_wgt, gs, qcdpower, 1., 1.)
998
999 if '_nlo' in type_nlo:
1000 wgt = self.invert_momenta(wgt_virt)
1001 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1002 new_out, partial = self.combine_wgt(scales2, pdg, bjx, wgt, gs, qcdpower, 1., 1.)
1003
1004 avg = [partial_check[i]/ref_wgts[i] for i in range(len(ref_wgts))]
1005 out = sum(partial[i]/avg[i] if 0.85<avg[i]<1.15 else 0 \
1006 for i in range(len(avg)))
1007 final_weight['_nlo'] = out/orig_wgt*event.wgt
1008
1009
1010 if '_tree' in type_nlo:
1011 wgt = self.invert_momenta(wgt_tree)
1012 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1013 out, partial = self.combine_wgt(scales2, pdg, bjx, wgt, gs, qcdpower, 1., 1.)
1014
1015 avg = [partial_check[i]/ref_wgts[i] for i in range(len(ref_wgts))]
1016 new_out = sum(partial[i]/avg[i] if 0.85<avg[i]<1.15 else partial[i] \
1017 for i in range(len(avg)))
1018 final_weight['_tree'] = new_out/orig_wgt*event.wgt
1019
1020 if '_lo' in type_nlo:
1021 w_orig = self.calculate_matrix_element(event, 0, space)
1022 w_new = self.calculate_matrix_element(event, 1, space)
1023 final_weight['_lo'] = w_new/w_orig*event.wgt
1024
1025 return final_weight
1026
1027
1028 @staticmethod
1030 """ fortran/C-python do not order table in the same order"""
1031 new_p = []
1032 for i in range(len(p[0])): new_p.append([0]*len(p))
1033 for i, onep in enumerate(p):
1034 for j, x in enumerate(onep):
1035 new_p[j][i] = x
1036 return new_p
1037
1038 @staticmethod
1040 if tag == 2:
1041 return
1042 if os.path.exists(pjoin(Pdir, 'matrix%spy.so' % tag)):
1043 return
1044 else:
1045 open(pjoin(Pdir, 'matrix%spy.so' % tag),'w').write(open(pjoin(Pdir, 'matrix2py.so')
1046 ).read().replace('matrix2py', 'matrix%spy' % tag))
1047
1049 """routine to return the matrix element"""
1050
1051 if self.has_nlo:
1052 nb_retry, sleep = 10, 60
1053 else:
1054 nb_retry, sleep = 5, 20
1055
1056 tag, order = event.get_tag_and_order()
1057 if isinstance(hypp_id, str) and hypp_id.startswith('V'):
1058 tag = (tag,'V')
1059 hypp_id = int(hypp_id[1:])
1060 base = "rw_mevirt"
1061 else:
1062 base = "rw_me"
1063
1064 if (not self.second_model and not self.second_process) or hypp_id==0:
1065 orig_order, Pdir, hel_dict = self.id_to_path[tag]
1066 else:
1067 orig_order, Pdir, hel_dict = self.id_to_path_second[tag]
1068
1069
1070 run_id = (tag, hypp_id)
1071
1072 assert space == self
1073 start = False
1074 if run_id in space.calculator:
1075 external = space.calculator[run_id]
1076
1077
1078
1079
1080
1081
1082 elif (not self.second_model and not self.second_process) or hypp_id==0:
1083
1084 subdir = pjoin(self.me_dir, base, 'SubProcesses')
1085 if self.me_dir not in sys.path:
1086 sys.path.insert(0,self.me_dir)
1087 if self.rwgt_dir and self.rwgt_dir not in sys.path:
1088 sys.path.insert(0,self.rwgt_dir)
1089 Pname = os.path.basename(Pdir)
1090 if hypp_id == 0:
1091 if (Pdir, 0) not in dir_to_f2py_free_mod:
1092 metag = 1
1093 dir_to_f2py_free_mod[(Pdir,0)] = (metag, nb_f2py_module)
1094 else:
1095 metag, old_module = dir_to_f2py_free_mod[(Pdir,0)]
1096 if old_module != nb_f2py_module:
1097 metag += 1
1098 dir_to_f2py_free_mod[(Pdir,0)] = (metag, nb_f2py_module)
1099 os.environ['MENUM'] = '2'
1100 if not self.rwgt_dir or not os.path.exists(pjoin(Pdir, 'matrix2py.so')):
1101 misc.multiple_try(nb_retry,sleep)(misc.compile)(['matrix2py.so'], cwd=Pdir)
1102 self.rename_f2py_lib(Pdir, 2*metag)
1103 try:
1104 mymod = __import__('%s.SubProcesses.%s.matrix%spy' % (base, Pname, 2*metag), globals(), locals(), [],-1)
1105 except:
1106 import platform
1107 if platform.system() == 'Darwin':
1108 os.system('install_name_tool -change libMadLoop.dylib %s/libMadLoop.dylib matrix%spy.so' % (Pdir,2*metag))
1109 mymod = __import__('%s.SubProcesses.%s.matrix%spy' % (base, Pname, 2*metag), globals(), locals(), [],-1)
1110 else:
1111 misc.sprint("fail compilation")
1112 raise
1113 S = mymod.SubProcesses
1114 P = getattr(S, Pname)
1115 mymod = getattr(P, 'matrix%spy' % (2*metag))
1116 with misc.chdir(Pdir):
1117 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1118 mymod.initialise('param_card_orig.dat')
1119
1120
1121 if hypp_id == 1:
1122
1123 metag = dir_to_f2py_free_mod[(Pdir,0)][0]
1124 newtag = 2*metag+1
1125 self.rename_f2py_lib(Pdir, newtag)
1126 try:
1127 mymod = __import__('%s.SubProcesses.%s.matrix%spy' % (base, Pname, newtag), globals(), locals(), [],-1)
1128 except Exception, error:
1129 newtag = "L%s" % newtag
1130 os.environ['MENUM'] = newtag
1131 misc.multiple_try(nb_retry,sleep)(misc.compile)(['matrix%spy.so' % newtag], cwd=Pdir)
1132 mymod = __import__('%s.SubProcesses.%s.matrix%spy' % (base, Pname, newtag), globals(), locals(), [],-1)
1133
1134 S = mymod.SubProcesses
1135 P = getattr(S, Pname)
1136 mymod = getattr(P, 'matrix%spy' % newtag)
1137 with misc.chdir(Pdir):
1138 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1139 mymod.initialise('param_card.dat')
1140
1141 space.calculator[run_id] = mymod.get_me
1142 space.calculator[(run_id,'module')] = mymod
1143 external = space.calculator[run_id]
1144 else:
1145 subdir = pjoin(self.me_dir,'%s_second' % base, 'SubProcesses')
1146 if self.me_dir not in sys.path:
1147 sys.path.append(self.me_dir)
1148
1149 assert hypp_id == 1
1150 Pname = os.path.basename(Pdir)
1151 os.environ['MENUM'] = '2'
1152 if not self.rwgt_dir or not os.path.exists(pjoin(Pdir, 'matrix2py.so')):
1153 misc.multiple_try(nb_retry,sleep)(misc.compile)(['matrix2py.so'], cwd=pjoin(subdir, Pdir))
1154 if (Pdir, 1) not in dir_to_f2py_free_mod:
1155 metag = 1
1156 dir_to_f2py_free_mod[(Pdir,1)] = (metag, nb_f2py_module)
1157 else:
1158 metag, old_module = dir_to_f2py_free_mod[(Pdir,1)]
1159 if old_module != nb_f2py_module:
1160 metag += 1
1161 dir_to_f2py_free_mod[(Pdir,1)] = (metag, nb_f2py_module)
1162 self.rename_f2py_lib(Pdir, metag)
1163 try:
1164 mymod = __import__("%s_second.SubProcesses.%s.matrix%spy" % (base, Pname, metag))
1165 except ImportError:
1166 metag = "L%s" % metag
1167 os.environ['MENUM'] = str(metag)
1168 misc.multiple_try(nb_retry,sleep)(misc.compile)(['matrix%spy.so' % metag], cwd=pjoin(subdir, Pdir))
1169 mymod = __import__("%s_second.SubProcesses.%s.matrix%spy" % (base, Pname, metag))
1170
1171 reload(mymod)
1172 S = mymod.SubProcesses
1173 P = getattr(S, Pname)
1174 mymod = getattr(P, 'matrix%spy' % metag)
1175 with misc.chdir(Pdir):
1176 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1177 mymod.initialise('param_card.dat')
1178 space.calculator[run_id] = mymod.get_me
1179 space.calculator[(run_id,'module')] = mymod
1180 external = space.calculator[run_id]
1181
1182
1183
1184 p = event.get_momenta(orig_order)
1185
1186
1187 hel_order = event.get_helicity(orig_order)
1188 if self.helicity_reweighting and 9 not in hel_order:
1189 nhel = hel_dict[tuple(hel_order)]
1190 if event[1].status == -1:
1191
1192 pboost = lhe_parser.FourMomentum(p[0]) + lhe_parser.FourMomentum(p[1])
1193 for i,thisp in enumerate(p):
1194 p[i] = lhe_parser.FourMomentum(thisp).zboost(pboost).get_tuple()
1195 assert p[0][1] == p[0][2] == 0 == p[1][2] == p[1][2] == 0
1196
1197
1198 else:
1199 nhel = 0
1200 pold = list(p)
1201 p = self.invert_momenta(p)
1202
1203 with misc.chdir(Pdir):
1204 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1205 if 'V' in tag or \
1206 (hypp_id ==1 and self.second_process and any('sqrvirt' in l for l in self.second_process)):
1207 me_value = external(p,event.aqcd, math.sqrt(scale2), nhel)
1208 else:
1209 try:
1210 me_value = external(p,event.aqcd, nhel)
1211 except TypeError:
1212 me_value = external(p,event.aqcd, math.sqrt(scale2), nhel)
1213
1214
1215 if isinstance(me_value, tuple):
1216 me_value, code = me_value
1217
1218 hundred_value = (code % 1000) //100
1219 if hundred_value in [4]:
1220 me_value = 0.
1221
1222 return me_value
1223
1225 """routine to terminate all fortran executables"""
1226
1227 for (mode, production) in dict(self.calculator):
1228
1229 if new_card_only and production == 0:
1230 continue
1231 del self.calculator[(mode, production)]
1232
1234 if self.exitted:
1235 return
1236 self.exitted = True
1237
1238 if 'init' in self.banner:
1239 cross = 0
1240 error = 0
1241 for line in self.banner['init'].split('\n'):
1242 split = line.split()
1243 if len(split) == 4:
1244 cross, error = float(split[0]), float(split[1])
1245
1246 if not self.multicore == 'create':
1247
1248 if 'orig' not in self.all_cross_section:
1249 logger.info('Original cross-section: %s +- %s pb' % (cross, error))
1250 else:
1251 logger.info('Original cross-section: %s +- %s pb (cross-section from sum of weights: %s)' % (cross, error, self.all_cross_section['orig'][0]))
1252 logger.info('Computed cross-section:')
1253 keys = self.all_cross_section.keys()
1254 keys.sort()
1255 for key in keys:
1256 if key == 'orig':
1257 continue
1258 logger.info('%s : %s +- %s pb' % (key[0] if not key[1] else '%s%s' % key,
1259 self.all_cross_section[key][0],self.all_cross_section[key][1] ))
1260 self.terminate_fortran_executables()
1261
1262 if self.rwgt_dir and self.multicore == False:
1263 self.save_to_pickle()
1264
1265 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1266 for run_id in self.calculator:
1267 del self.calculator[run_id]
1268 del self.calculator
1269
1270
1273
1274
1275 - def adding_me(self, matrix_elements, path):
1276 """Adding one element to the list based on the matrix element"""
1277
1278
1279 @misc.mute_logger()
1281 """generate the various directory for the weight evaluation"""
1282
1283 data={}
1284 if not second:
1285 data['paths'] = ['rw_me', 'rw_mevirt']
1286
1287 info = self.banner.get('proc_card', 'full_model_line')
1288 if '-modelname' in info:
1289 data['mg_names'] = False
1290 else:
1291 data['mg_names'] = True
1292 data['model_name'] = self.banner.get('proc_card', 'model')
1293
1294 data['processes'] = [line[9:].strip() for line in self.banner.proc_card
1295 if line.startswith('generate')]
1296 data['processes'] += [' '.join(line.split()[2:]) for line in self.banner.proc_card
1297 if re.search('^\s*add\s+process', line)]
1298
1299 self.id_to_path = {}
1300 data['id2path'] = self.id_to_path
1301 else:
1302 data['paths'] = ['rw_me_second', 'rw_mevirt_second']
1303
1304 if self.second_model:
1305 data['mg_names'] = True
1306 if ' ' in self.second_model:
1307 args = self.second_model.split()
1308 if '--modelname' in args:
1309 data['mg_names'] = False
1310 data['model_name'] = args[0]
1311 else:
1312 data['model_name'] = self.second_model
1313 else:
1314 data['model_name'] = None
1315
1316 if self.second_process:
1317 data['processes'] = self.second_process
1318 else:
1319 data['processes'] = [line[9:].strip() for line in self.banner.proc_card
1320 if line.startswith('generate')]
1321 data['processes'] += [' '.join(line.split()[2:])
1322 for line in self.banner.proc_card
1323 if re.search('^\s*add\s+process', line)]
1324
1325 self.id_to_path_second = {}
1326 data['id2path'] = self.id_to_path_second
1327
1328
1329 if not self.rwgt_dir:
1330 path_me = self.me_dir
1331 else:
1332 path_me = self.rwgt_dir
1333 try:
1334 shutil.rmtree(pjoin(path_me,data['paths'][0]))
1335 except Exception:
1336 pass
1337 try:
1338 shutil.rmtree(pjoin(path_me, data['paths'][1]))
1339 except Exception:
1340 pass
1341
1342
1343 mgcmd = self.mg5cmd
1344 complex_mass = False
1345 has_cms = re.compile(r'''set\s+complex_mass_scheme\s*(True|T|1|true|$|;)''')
1346 for line in self.banner.proc_card:
1347 if line.startswith('set'):
1348 mgcmd.exec_cmd(line, printcmd=False, precmd=False, postcmd=False)
1349 if has_cms.search(line):
1350 complex_mass = True
1351 elif line.startswith('define'):
1352 try:
1353 mgcmd.exec_cmd(line, printcmd=False, precmd=False, postcmd=False)
1354 except Exception:
1355 pass
1356
1357
1358 if not data['model_name'] and not second:
1359 raise self.InvalidCmd('Only UFO model can be loaded in this module.')
1360 elif data['model_name']:
1361 self.load_model(data['model_name'], data['mg_names'], complex_mass)
1362 modelpath = self.model.get('modelpath')
1363 if os.path.basename(modelpath) != mgcmd._curr_model['name']:
1364 name, restrict = mgcmd._curr_model['name'].rsplit('-',1)
1365 if os.path.exists(pjoin(os.path.dirname(modelpath),name, 'restrict_%s.dat' % restrict)):
1366 modelpath = pjoin(os.path.dirname(modelpath), mgcmd._curr_model['name'])
1367
1368 commandline="import model %s " % modelpath
1369 if not data['mg_names']:
1370 commandline += ' -modelname '
1371 mgcmd.exec_cmd(commandline)
1372
1373
1374 for name, content in self.banner.get('proc_card', 'multiparticles'):
1375 mgcmd.exec_cmd("define %s = %s" % (name, content))
1376
1377
1378 has_nlo = False
1379 mgcmd.exec_cmd("set group_subprocesses False")
1380
1381 if not second:
1382 logger.info('generating the square matrix element for reweighting')
1383 else:
1384 logger.info('generating the square matrix element for reweighting (second model and/or processes)')
1385 start = time.time()
1386 commandline=''
1387 for i,proc in enumerate(data['processes']):
1388 if '[' not in proc:
1389 commandline += "add process %s ;" % proc
1390 else:
1391 has_nlo = True
1392 if self.banner.get('run_card','ickkw') == 3:
1393 if len(proc) == min([len(p.strip()) for p in data['processes']]):
1394 commandline += self.get_LO_definition_from_NLO(proc,self.model)
1395 else:
1396 commandline += self.get_LO_definition_from_NLO(proc,self.model, real_only=True)
1397 else:
1398 commandline += self.get_LO_definition_from_NLO(proc,self.model)
1399
1400 commandline = commandline.replace('add process', 'generate',1)
1401 logger.info(commandline)
1402 try:
1403 mgcmd.exec_cmd(commandline, precmd=True, errorhandling=False)
1404 except diagram_generation.NoDiagramException:
1405 commandline=''
1406 for proc in data['processes']:
1407 if '[' not in proc:
1408 raise
1409
1410 base, post = proc.split('[',1)
1411 nlo_order, post = post.split(']',1)
1412 if '=' not in nlo_order:
1413 nlo_order = 'virt=%s' % nlo_order
1414 elif 'noborn' in nlo_order:
1415 nlo_order = nlo_order.replace('noborn', 'virt')
1416 commandline += "add process %s [%s] %s;" % (base,nlo_order,post)
1417 commandline = commandline.replace('add process', 'generate',1)
1418 logger.info("RETRY with %s", commandline)
1419 mgcmd.exec_cmd(commandline, precmd=True)
1420 has_nlo = False
1421 except Exception, error:
1422 raise
1423
1424 commandline = 'output standalone_rw %s' % pjoin(path_me,data['paths'][0])
1425 mgcmd.exec_cmd(commandline, precmd=True)
1426 logger.info('Done %.4g' % (time.time()-start))
1427 self.has_standalone_dir = True
1428
1429
1430
1431 matrix_elements = mgcmd._curr_matrix_elements.get_matrix_elements()
1432
1433 to_check = []
1434 for me in matrix_elements:
1435 for proc in me.get('processes'):
1436 initial = []
1437 final = [l.get('id') for l in proc.get('legs')\
1438 if l.get('state') or initial.append(l.get('id'))]
1439 order = (initial, final)
1440 tag = proc.get_initial_final_ids()
1441 decay_finals = proc.get_final_ids_after_decay()
1442
1443 if tag[1] != decay_finals:
1444 order = (initial, list(decay_finals))
1445 decay_finals.sort()
1446 tag = (tag[0], tuple(decay_finals))
1447 Pdir = pjoin(path_me, data['paths'][0], 'SubProcesses',
1448 'P%s' % me.get('processes')[0].shell_string())
1449
1450 if not os.path.exists(Pdir):
1451 to_check.append(tag)
1452 continue
1453
1454 if self.banner.run_card['ickkw']!=3 and tag in data['id2path']:
1455 if not Pdir == data['id2path'][tag][1]:
1456 misc.sprint(tag, Pdir, data['id2path'][tag][1])
1457 raise self.InvalidCmd, '2 different process have the same final states. This module can not handle such situation'
1458 else:
1459 continue
1460
1461 hel_nb = 0
1462 hel_dict = {9:0}
1463 for helicities in me.get_helicity_matrix():
1464 hel_nb +=1
1465 hel_dict[tuple(helicities)] = hel_nb
1466
1467 data['id2path'][tag] = [order, Pdir, hel_dict]
1468
1469 for tag in to_check:
1470 if tag not in self.id_to_path:
1471 logger.warning("no valid path for %s" % (tag,))
1472
1473
1474
1475 if os.path.exists(pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat')):
1476 MLCard = banner.MadLoopParam(pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat'))
1477 MLCard.set('WriteOutFilters', False)
1478 MLCard.set('UseLoopFilter', False)
1479 MLCard.set("DoubleCheckHelicityFilter", False)
1480 MLCard.set("HelicityFilterLevel", 0)
1481 MLCard.write(pjoin(path_me, data['paths'][0], 'SubProcesses', 'MadLoopParams.dat'),
1482 pjoin(path_me, data['paths'][0], 'Cards', 'MadLoopParams.dat'),
1483 commentdefault=False)
1484
1485 if self.multicore == 'create':
1486 try:
1487 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][0],'SubProcesses'),
1488 nb_core=self.mother.options['nb_core'])
1489 except:
1490 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][0],'SubProcesses'))
1491
1492 if os.path.exists(pjoin(path_me, data['paths'][1], 'Cards', 'MadLoopParams.dat')):
1493 if self.multicore == 'create':
1494 print "compile OLP", data['paths'][1]
1495 try:
1496 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'),
1497 nb_core=self.mother.options['nb_core'])
1498 except:
1499 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'))
1500
1501
1502
1503 if has_nlo and 'NLO' in self.rwgt_mode:
1504
1505 start = time.time()
1506 commandline=''
1507 for proc in data['processes']:
1508 if '[' not in proc:
1509 pass
1510 else:
1511 proc = proc.replace('[', '[ virt=')
1512 commandline += "add process %s ;" % proc
1513
1514 old_options = dict(mgcmd.options)
1515 if mgcmd.options['golem'] or mgcmd.options['pjfry']:
1516 logger.info(" When doing NLO reweighting, MG5aMC cannot use the loop reduction algorithms Golem and/or PJFry++")
1517 mgcmd.options['golem'] = None
1518 mgcmd.options['pjfry'] = None
1519 commandline = commandline.replace('add process', 'generate',1)
1520 logger.info(commandline)
1521 mgcmd.exec_cmd(commandline, precmd=True)
1522 commandline = 'output standalone_rw %s -f' % pjoin(path_me, data['paths'][1])
1523 mgcmd.exec_cmd(commandline, precmd=True)
1524
1525
1526 mgcmd.options['golem'] = old_options['golem']
1527 mgcmd.options['pjfry'] = old_options['pjfry']
1528
1529 m_opts = {}
1530 if mgcmd.options['lhapdf']:
1531
1532
1533 m_opts['lhapdf'] = True
1534 m_opts['f2pymode'] = True
1535 m_opts['lhapdfversion'] = 5
1536 m_opts['llhapdf'] = self.mother.get_lhapdf_libdir()
1537 else:
1538 raise Exception, "NLO reweighting requires LHAPDF to work correctly"
1539
1540 path = pjoin(path_me,data['paths'][1], 'Source', 'make_opts')
1541 common_run_interface.CommonRunCmd.update_make_opts_full(path, m_opts)
1542 logger.info('Done %.4g' % (time.time()-start))
1543
1544
1545
1546 common_run_interface.CommonRunCmd.install_lhapdf_pdfset_static(\
1547 mgcmd.options['lhapdf'], None, self.banner.run_card.get_lhapdf_id())
1548
1549
1550 matrix_elements = mgcmd._curr_matrix_elements.get_matrix_elements()
1551 for me in matrix_elements:
1552 for proc in me.get('processes'):
1553 initial = []
1554 final = [l.get('id') for l in proc.get('legs')\
1555 if l.get('state') or initial.append(l.get('id'))]
1556 order = (initial, final)
1557 tag = proc.get_initial_final_ids()
1558 decay_finals = proc.get_final_ids_after_decay()
1559
1560 if tag[1] != decay_finals:
1561 order = (initial, list(decay_finals))
1562 decay_finals.sort()
1563 tag = (tag[0], tuple(decay_finals))
1564 Pdir = pjoin(path_me, data['paths'][1], 'SubProcesses',
1565 'P%s' % me.get('processes')[0].shell_string())
1566 assert os.path.exists(Pdir), "Pdir %s do not exists" % Pdir
1567 if (tag,'V') in data['id2path']:
1568 if not Pdir == data['id2path'][(tag,'V')][1]:
1569 misc.sprint(tag, Pdir, self.id_to_path[(tag,'V')][1])
1570 raise self.InvalidCmd, '2 different process have the same final states. This module can not handle such situation'
1571 else:
1572 continue
1573
1574 hel_nb = 0
1575 hel_dict = {9:0}
1576 for helicities in me.get_helicity_matrix():
1577 hel_nb +=1
1578 hel_dict[tuple(helicities)] = hel_nb
1579
1580 data['id2path'][(tag,'V')] = [order, Pdir, hel_dict]
1581
1582
1583 misc.compile(cwd=pjoin(path_me, data['paths'][1], 'Source'))
1584
1585 if path_me not in sys.path:
1586 sys.path.insert(0, os.path.realpath(path_me))
1587 with misc.chdir(pjoin(path_me)):
1588 mymod = __import__('%s.Source.rwgt2py' % data['paths'][1], globals(), locals(), [],-1)
1589 mymod = mymod.Source.rwgt2py
1590 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1591 mymod.initialise([self.banner.run_card['lpp1'],
1592 self.banner.run_card['lpp2']],
1593 self.banner.run_card.get_lhapdf_id())
1594 self.combine_wgt = mymod.get_wgt
1595
1596 if self.multicore == 'create':
1597 print "compile OLP", data['paths'][1]
1598 misc.compile(['OLP_static'], cwd=pjoin(path_me, data['paths'][1],'SubProcesses'),
1599 nb_core=self.mother.options['nb_core'])
1600
1601 elif has_nlo and not second and self.rwgt_mode == ['NLO_tree']:
1602
1603
1604
1605 start = time.time()
1606 commandline='import model loop_sm;generate g g > e+ ve [virt=QCD]'
1607
1608 old_options = dict(mgcmd.options)
1609 mgcmd.options['golem'] = None
1610 mgcmd.options['pjfry'] = None
1611 commandline = commandline.replace('add process', 'generate',1)
1612 logger.info(commandline)
1613 mgcmd.exec_cmd(commandline, precmd=True)
1614 commandline = 'output standalone_rw %s -f' % pjoin(path_me, data['paths'][1])
1615 mgcmd.exec_cmd(commandline, precmd=True)
1616
1617 mgcmd.options['golem'] = old_options['golem']
1618 mgcmd.options['pjfry'] = old_options['pjfry']
1619
1620 m_opts = {}
1621 if mgcmd.options['lhapdf']:
1622
1623
1624 m_opts['lhapdf'] = True
1625 m_opts['f2pymode'] = True
1626 m_opts['lhapdfversion'] = 5
1627 m_opts['llhapdf'] = self.mother.get_lhapdf_libdir()
1628 else:
1629 raise Exception, "NLO_tree reweighting requires LHAPDF to work correctly"
1630
1631 path = pjoin(path_me,data['paths'][1], 'Source', 'make_opts')
1632 common_run_interface.CommonRunCmd.update_make_opts_full(path, m_opts)
1633 logger.info('Done %.4g' % (time.time()-start))
1634
1635
1636 common_run_interface.CommonRunCmd.install_lhapdf_pdfset_static(\
1637 mgcmd.options['lhapdf'], None, self.banner.run_card.get_lhapdf_id())
1638
1639
1640 misc.compile(cwd=pjoin(path_me, data['paths'][1], 'Source'))
1641
1642 with misc.chdir(pjoin(path_me)):
1643 if path_me not in sys.path:
1644 sys.path.insert(0, 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
1654
1655 if (self.second_model or self.second_process) and not second:
1656 self.create_standalone_directory(second=True)
1657
1658 if not second:
1659 self.has_nlo = has_nlo
1660
1661
1662
1663
1664 - def load_model(self, name, use_mg_default, complex_mass=False):
1682
1683
1685 import madgraph.iolibs.save_load_object as save_load_object
1686
1687 to_save = {}
1688 to_save['id_to_path'] = self.id_to_path
1689 if hasattr(self, 'id_to_path_second'):
1690 to_save['id_to_path_second'] = self.id_to_path_second
1691 else:
1692 to_save['id_to_path_second'] = {}
1693 to_save['all_cross_section'] = self.all_cross_section
1694 to_save['processes'] = self.processes
1695 to_save['second_process'] = self.second_process
1696 if self.second_model:
1697 to_save['second_model'] =True
1698 else:
1699 to_save['second_model'] = None
1700 to_save['rwgt_dir'] = self.rwgt_dir
1701 to_save['has_nlo'] = self.has_nlo
1702 to_save['rwgt_mode'] = self.rwgt_mode
1703 to_save['rwgt_name'] = self.options['rwgt_name']
1704
1705 name = pjoin(self.rwgt_dir, 'rw_me', 'rwgt.pkl')
1706 save_load_object.save_to_file(name, to_save)
1707
1708
1710 import madgraph.iolibs.save_load_object as save_load_object
1711
1712 obj = save_load_object.load_from_file( pjoin(self.rwgt_dir, 'rw_me', 'rwgt.pkl'))
1713
1714 self.has_standalone_dir = True
1715 self.options = {'curr_dir': os.path.realpath(os.getcwd()),
1716 'rwgt_name': None}
1717 if keep_name:
1718 self.options['rwgt_name'] = obj['rwgt_name']
1719
1720 old_rwgt = obj['rwgt_dir']
1721
1722
1723 self.id_to_path = {}
1724 for key , (order, Pdir, hel_dict) in obj['id_to_path'].items():
1725 new_P = Pdir.replace(old_rwgt, self.rwgt_dir)
1726 self.id_to_path[key] = [order, new_P, hel_dict]
1727
1728
1729 self.id_to_path_second = {}
1730 for key , (order, Pdir, hel_dict) in obj['id_to_path_second'].items():
1731 new_P = Pdir.replace(old_rwgt, self.rwgt_dir)
1732 self.id_to_path_second[key] = [order, new_P, hel_dict]
1733
1734 self.all_cross_section = obj['all_cross_section']
1735 self.processes = obj['processes']
1736 self.second_process = obj['second_process']
1737 self.second_model = obj['second_model']
1738 self.has_nlo = obj['has_nlo']
1739 if not self.rwgt_mode:
1740 self.rwgt_mode = obj['rwgt_mode']
1741 logger.info("mode set to %s" % self.rwgt_mode)
1742 if self.has_nlo and 'NLO' in self.rwgt_mode:
1743 path = pjoin(obj['rwgt_dir'], 'rw_mevirt','Source')
1744 sys.path.insert(0, path)
1745 try:
1746 mymod = __import__('rwgt2py', globals(), locals())
1747 except ImportError:
1748 misc.compile(['rwgt2py.so'], cwd=path)
1749 mymod = __import__('rwgt2py', globals(), locals())
1750 with misc.stdchannel_redirected(sys.stdout, os.devnull):
1751 mymod.initialise([self.banner.run_card['lpp1'],
1752 self.banner.run_card['lpp2']],
1753 self.banner.run_card.get_lhapdf_id())
1754 self.combine_wgt = mymod.get_wgt
1755