~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to madgraph/interface/madevent_interface.py

mwrge with 2.7.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
800
800
        
801
801
        self.allowed_madspin = []
802
802
        if 'MadSpin'  in self.available_module:
803
 
            self.allowed_madspin = ['OFF',"ON",'onshell']
 
803
            self.allowed_madspin = ['OFF',"ON",'onshell',"full"]
804
804
        return self.allowed_madspin
805
805
    
806
806
    def check_value_madspin(self, value):
837
837
        if value == 'onshell':
838
838
            return ["edit madspin_card --replace_line='set spinmode' --before_line='decay' set spinmode onshell"]
839
839
        elif value in ['full', 'madspin']:
840
 
            return ["edit madspin_card --replace_line='set spinmode' --before_line='decay' set spinmode madspin"]
 
840
            return ["edit madspin_card --replace_line='set spinmode' --before_line='decay' set spinmode full"]
841
841
        elif value == 'none':
842
842
            return ["edit madspin_card --replace_line='set spinmode' --before_line='decay' set spinmode none"]
843
843
        else:
1294
1294
                raise self.InvalidCmd('No run_name currently define. Unable to run refine')
1295
1295
 
1296
1296
        if len(args) > 2:
1297
 
            self.help_refine()
1298
1297
            raise self.InvalidCmd('Too many argument for refine command')
1299
1298
        else:
1300
1299
            try:
2500
2499
                          postcmd=False)
2501
2500
            self.exec_cmd('combine_events', postcmd=False)
2502
2501
            self.exec_cmd('store_events', postcmd=False)
2503
 
            self.exec_cmd('decay_events -from_cards', postcmd=False)
 
2502
            with misc.TMP_variable(self, 'run_name', self.run_name):
 
2503
                self.exec_cmd('decay_events -from_cards', postcmd=False)
2504
2504
            self.exec_cmd('create_gridpack', postcmd=False)
2505
2505
        else:
2506
2506
            # Regular run mode
2529
2529
            
2530
2530
            #we can bypass the following if scan and first result is zero
2531
2531
            if not bypass_run:
2532
 
                self.exec_cmd('refine %s' % nb_event, postcmd=False)
 
2532
                self.exec_cmd('refine %s --treshold=%s' % (nb_event,self.run_card['second_refine_treshold'])
 
2533
                              , postcmd=False)
2533
2534
            
2534
2535
                self.exec_cmd('combine_events', postcmd=False,printcmd=False)
2535
2536
                self.print_results_in_shell(self.results.current)
2885
2886
                        particle = 0
2886
2887
                # Read BRs for this decay
2887
2888
                line = param_card[line_number]
2888
 
                while line.startswith('#') or line.startswith(' '):
 
2889
                while re.search('^(#|\s|\d)', line):
2889
2890
                    line = param_card.pop(line_number)
2890
2891
                    if not particle or line.startswith('#'):
2891
2892
                        line=param_card[line_number]
3357
3358
        if float(self.run_card['mmjj']) > 0.01 * (float(self.run_card['ebeam1'])+float(self.run_card['ebeam2'])):
3358
3359
            self.pass_in_difficult_integration_mode()
3359
3360
        elif self.run_card['hard_survey']:
3360
 
            self.pass_in_difficult_integration_mode()
 
3361
            self.pass_in_difficult_integration_mode(self.run_card['hard_survey'])
3361
3362
            
3362
3363
        jobs, P_zero_result = ajobcreator.launch()
3363
3364
        # Check if all or only some fails
3385
3386
        self.update_status('End survey', 'parton', makehtml=False)
3386
3387
 
3387
3388
    ############################################################################
3388
 
    def pass_in_difficult_integration_mode(self):
 
3389
    def pass_in_difficult_integration_mode(self, rate=1):
3389
3390
        """be more secure for the integration to not miss it due to strong cut"""
3390
3391
        
3391
3392
        # improve survey options if default
3392
3393
        if self.opts['points'] == self._survey_options['points'][1]:
3393
 
            self.opts['points'] = 3 * self._survey_options['points'][1]
 
3394
            self.opts['points'] = (rate+2) * self._survey_options['points'][1]
3394
3395
        if self.opts['iterations'] == self._survey_options['iterations'][1]:
3395
 
            self.opts['iterations'] = 2 + self._survey_options['iterations'][1]
 
3396
            self.opts['iterations'] = 1 + rate + self._survey_options['iterations'][1]
3396
3397
        if self.opts['accuracy'] == self._survey_options['accuracy'][1]:
3397
 
            self.opts['accuracy'] = self._survey_options['accuracy'][1]/3  
 
3398
            self.opts['accuracy'] = self._survey_options['accuracy'][1]/(rate+2)  
3398
3399
        
3399
3400
        # Modify run_config.inc in order to improve the refine
3400
3401
        conf_path = pjoin(self.me_dir, 'Source','run_config.inc')
3401
3402
        files.cp(conf_path, conf_path + '.bk')
3402
3403
        #
3403
3404
        text = open(conf_path).read()
3404
 
        text = re.sub('''\(min_events = \d+\)''', '''(min_events = 7500 )''', text)
3405
 
        text = re.sub('''\(max_events = \d+\)''', '''(max_events = 40000 )''', text)
 
3405
        min_evt, max_evt = 2500 *(2+rate), 10000*(rate+1) 
 
3406
        
 
3407
        text = re.sub('''\(min_events = \d+\)''', '(min_events = %i )' % min_evt, text)
 
3408
        text = re.sub('''\(max_events = \d+\)''', '(max_events = %i )' % max_evt, text)
3406
3409
        fsock = open(conf_path, 'w')
3407
3410
        fsock.write(text)
3408
3411
        fsock.close()
3418
3421
        devnull = open(os.devnull, 'w')  
3419
3422
        self.nb_refine += 1
3420
3423
        args = self.split_arg(line)
 
3424
        treshold=None
 
3425
        for a in args:
 
3426
            if a.startswith('--treshold='):
 
3427
                treshold = float(a.split('=',1)[1])
 
3428
                old_xsec = self.results.current['prev_cross']
 
3429
                new_xsec = self.results.current['cross']
 
3430
                if old_xsec > new_xsec * treshold:
 
3431
                    logger.info('No need for second refine due to stability of cross-section')
 
3432
                    return
 
3433
                else:
 
3434
                    args.remove(a)
 
3435
                    break
3421
3436
        # Check argument's validity
3422
3437
        self.check_refine(args)
3423
3438
        
3474
3489
            cross, error = x_improve.update_html() #update html results for survey
3475
3490
            if  cross == 0:
3476
3491
                return
3477
 
            logger.info("Current estimate of cross-section: %s +- %s" % (cross, error))
3478
 
        
 
3492
            logger.info("- Current estimate of cross-section: %s +- %s" % (cross, error))
3479
3493
        if isinstance(x_improve, gen_ximprove.gen_ximprove_v4):
3480
3494
            # Non splitted mode is based on writting ajob so need to track them
3481
3495
            # Splitted mode handle the cluster submition internally.
3817
3831
        self.update_status('Creating gridpack', level='parton')
3818
3832
        # compile gen_ximprove
3819
3833
        misc.compile(['../bin/internal/gen_ximprove'], cwd=pjoin(self.me_dir, "Source"))
 
3834
        
 
3835
        Gdir = self.get_Gdir()
 
3836
        Pdir = set([os.path.dirname(G) for G in Gdir])
 
3837
        for P in Pdir: 
 
3838
            allG = misc.glob('G*', path=P)
 
3839
            for G in allG:
 
3840
                if pjoin(P, G) not in Gdir:
 
3841
                    logger.debug('removing %s', pjoin(P,G))
 
3842
                    shutil.rmtree(pjoin(P,G))
 
3843
                    
 
3844
        
3820
3845
        args = self.split_arg(line)
3821
3846
        self.check_combine_events(args)
3822
3847
        if not self.run_tag: self.run_tag = 'tag_1'
4675
4700
                            devnull.close()
4676
4701
                            if pid == 0:
4677
4702
                                misc.call('head -n -1 %s | tail -n +%d > %s/tmpfile' %
4678
 
                                          (hepmc_file, n_head, os.path.dirname(hepmc_file)), shell=True)
 
4703
                                          (hepmc_file, n_head+1, os.path.dirname(hepmc_file)), shell=True)
4679
4704
                                misc.call(['mv', 'tmpfile', os.path.basename(hepmc_file)], cwd=os.path.dirname(hepmc_file))
4680
4705
                            elif sys.platform == 'darwin':
4681
4706
                                # sed on MAC has slightly different synthax than on
5974
5999
 
5975
6000
        eradir = self.options['exrootanalysis_path']
5976
6001
        totar = False
 
6002
        torm = False
5977
6003
        if input.endswith('.gz'):
5978
 
            misc.gunzip(input, keep=True)
5979
 
            totar = True
5980
 
            input = input[:-3]
 
6004
            if not os.path.exists(input) and os.path.exists(input[:-3]):
 
6005
                totar = True
 
6006
                input = input[:-3]
 
6007
            else:
 
6008
                misc.gunzip(input, keep=True)
 
6009
                totar = False
 
6010
                torm = True
 
6011
                input = input[:-3]
5981
6012
            
5982
6013
        try:
5983
6014
            misc.call(['%s/ExRootLHEFConverter' % eradir, 
5989
6020
        if totar:
5990
6021
            if os.path.exists('%s.gz' % input):
5991
6022
                try:
5992
 
                    os.remove(input)
 
6023
                    os.remove('%s.gz' % input)
5993
6024
                except:
5994
6025
                    pass
5995
6026
            else:
5996
6027
                misc.gzip(input)
5997
 
            
 
6028
        if torm:
 
6029
            os.remove(input)
5998
6030
    
5999
6031
    def run_syscalc(self, mode='parton', event_path=None, output=None):
6000
6032
        """create the syscalc output""" 
6464
6496
            os.chdir(self.me_dir)
6465
6497
        else:
6466
6498
            for line in open(pjoin(self.me_dir,'SubProcesses','subproc.mg')):
6467
 
                os.mkdir(line.strip())
 
6499
                p = line.strip()
 
6500
                os.mkdir(p)
 
6501
                files.cp(pjoin(self.me_dir,'SubProcesses',p,'symfact.dat'),
 
6502
                         pjoin(p, 'symfact.dat'))
6468
6503
            
6469
6504
 
6470
6505
    def launch(self, nb_event, seed):
6478
6513
            misc.call([pjoin(self.me_dir,'bin','internal','restore_data'),
6479
6514
                         'default'], cwd=self.me_dir)
6480
6515
 
 
6516
        if self.run_card['python_seed'] == -2:
 
6517
            import random
 
6518
            random.seed(seed)
 
6519
        elif self.run_card['python_seed'] > 0:
 
6520
            import random
 
6521
            random.seed(self.run_card['python_seed'])            
6481
6522
        # 2) Run the refine for the grid
6482
6523
        self.update_status('Generating Events', level=None)
6483
6524
        #misc.call([pjoin(self.me_dir,'bin','refine4grid'),
6654
6695
                sum_axsec += result.get('axsec')*gscalefact[Gdir]
6655
6696
                
6656
6697
                if len(AllEvent) >= 80: #perform a partial unweighting
6657
 
                    AllEvent.unweight(pjoin(self.me_dir, "Events", self.run_name, "partials%s.lhe.gz" % partials),
 
6698
                    AllEvent.unweight(pjoin(outdir, self.run_name, "partials%s.lhe.gz" % partials),
6658
6699
                          get_wgt, log_level=5,  trunc_error=1e-2, event_target=self.nb_event)
6659
6700
                    AllEvent = lhe_parser.MultiEventFile()
6660
6701
                    AllEvent.banner = self.banner
6661
 
                    AllEvent.add(pjoin(self.me_dir, "Events", self.run_name, "partials%s.lhe.gz" % partials),
 
6702
                    AllEvent.add(pjoin(outdir, self.run_name, "partials%s.lhe.gz" % partials),
6662
6703
                                 sum_xsec,
6663
6704
                                 math.sqrt(sum(x**2 for x in sum_xerru)),
6664
6705
                                 sum_axsec)