示例#1
0
        wjets_ss.cut = cut.cut.replace('l1_charge != l2_charge',
                                       'l1_charge == l2_charge')

        stacknow_highmt_os.cut = cut.cut.replace('mt<30', 'mt>70') + '&& mt>70'
        stacknow_highmt_ss.cut = cut.cut.replace(
            'l1_charge != l2_charge', 'l1_charge == l2_charge').replace(
                'mt<30', 'mt>70') + '&& mt>70'

        wjets_incl_os.cut = cut.cut.replace('mt<30', '1.')
        wjets_incl_ss.cut = cut.cut.replace('l1_charge != l2_charge',
                                            'l1_charge == l2_charge').replace(
                                                'mt<30', '1.')

        wjets_highmt_os.cut = cut.cut.replace('mt<30', 'mt>70') + '&& mt>70'

        plot_w_os = createHistogram(wjets_incl_os, verbose=False)
        plot_w_ss = createHistogram(wjets_incl_ss, verbose=False)

        r_w_os_ss = plot_w_os.GetStack().totalHist.Yield(
        ) / plot_w_ss.GetStack().totalHist.Yield()
        print 'Inclusive W OS/SS ratio:', r_w_os_ss

        plot_highmt_os = createHistogram(stacknow_highmt_os,
                                         all_stack=True,
                                         verbose=False)
        plot_highmt_ss = createHistogram(stacknow_highmt_ss,
                                         all_stack=True,
                                         verbose=False)
        createDefaultGroups(plot_highmt_os)
        createDefaultGroups(plot_highmt_ss)
示例#2
0
        weight=total_weight + ' * ((genmet_pt < 0.) - (l2_gen_match == 5))')

    cfg_tight.cut = cuts[cut_name]
    cfg_loose.cut = cuts[cut_name].replace(
        'l2_byCombinedIsolationDeltaBetaCorrRaw3Hits<1.5', '1.')
    cfg_data_tight.cut = cuts[cut_name]
    cfg_data_loose.cut = cuts[cut_name].replace(
        'l2_byCombinedIsolationDeltaBetaCorrRaw3Hits<1.5', '1.')

    for variable in variables:
        cfg_tight.var = variable
        cfg_loose.var = variable
        cfg_data_tight.var = variable
        cfg_data_loose.var = variable

        plot_tight = createHistogram(cfg_tight, verbose=False)
        plot_loose = createHistogram(cfg_loose, verbose=False)
        plot_data_tight = createHistogram(cfg_data_tight,
                                          verbose=False,
                                          all_stack=True)
        plot_data_loose = createHistogram(cfg_data_loose,
                                          verbose=False,
                                          all_stack=True)
        for plot in [plot_tight, plot_loose, plot_data_tight, plot_data_loose]:
            plot.Group('VV', ['ZZ', 'WZ', 'WW', 'T_tWch', 'TBar_tWch'])
            out_dir = 'fakeplots/' + cut_name if plot is plot_tight else 'fakeplots/loose' + cut_name
            # HistDrawer.draw(plot, plot_dir='fakeplots/'+cut_name)

        plotDataOverMCEff(
            plot_tight.GetStack().totalHist.weighted,
            plot_loose.GetStack().totalHist.weighted,
示例#3
0
        if sample.name != 'Data':
            # Subtract background from data
            sample.scale = -1.

    qcd = HistogramCfg(name='QCD', var=None, cfgs=samples_ss, cut=inc_cut, lumi=40.0)

    samples.append(qcd)

# Taken from Variables.py, can get subset with e.g. getVars(['mt', 'mvis'])
variables = all_vars

cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=inc_cut, lumi=40.0)


for cut_name in cuts:
    cfg_example.cut = cuts[cut_name]
    if qcd_from_same_sign and 'OS' in cut_name:
        qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge', 'l1_charge == l2_charge')

    for variable in variables:
        cfg_example.var = variable
        if qcd_from_same_sign:
            qcd.var = variable # Can put into function but we will not want it by default if we take normalisations from e.g. high MT
        
        plot = createHistogram(cfg_example, verbose=True)
        plot.Group('Diboson', ['ZZ', 'WZ', 'WW'])
        plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])
        plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style)
        HistDrawer.draw(plot, plot_dir='plots/'+cut_name)

示例#4
0
                                         ' * ((l2_gen_match == 6))')

                tight_cut = inc_cut + cuts[cut_name] + cut_extra
                loose_cut = tight_cut.replace(
                    'l2_byIsolationMVArun2v1DBoldDMwLT>2.5', '1.')
                loose_cut = tight_cut.replace(
                    'l2_byIsolationMVArun2v1DBoldDMwLT>2.5',
                    'l2_byIsolationMVArun2v1DBoldDMwLT>0.5')

                print 'Tight cut:', tight_cut
                print 'Loose cut:', loose_cut

                cfg_tight.cut = tight_cut
                cfg_loose.cut = loose_cut

                cfg_tight.var = variable
                cfg_loose.var = variable

                plot_tight = createHistogram(cfg_tight, verbose=False)
                plot_loose = createHistogram(cfg_loose, verbose=False)

                tight_hist = plot_tight.GetStack().totalHist.weighted
                loose_hist = plot_loose.GetStack().totalHist.weighted

                plot_inputs.append((tight_hist, loose_hist, sample_def['name'],
                                    sample_def['colour'], sample_def['style']))

            plotMCEffs(
                plot_inputs, 'mcfakeplots/tight_to_loose' + set_name + '_' +
                variable.name + cut_name + '.pdf')
示例#5
0
def qcd_estimation(B_cut,
                   C_cut,
                   D_cut,
                   all_samples,
                   int_lumi,
                   total_weight,
                   scale=1.,
                   verbose=True,
                   friend_func=None):
    '''ABCD method.
    
     A | B
    -------
     C | D
     
    A is the signal region
    B is where the shape is taken from
    C/D gives the scale factor to be applied to the shape in B
    
    Returns an updated list of samples that includes the QCD HistgramCfg.
    '''

    norm_var = dict_all_vars['_norm_']

    QCD_C_region_cut = C_cut
    QCD_D_region_cut = D_cut
    QCD_B_region_cut = B_cut

    samples_qcd_copy = copy.deepcopy(
        [s for s in all_samples if s.name != 'QCD' and not s.is_signal])
    samples_qcd_copy = [s for s in samples_qcd_copy if not s.is_signal]

    for sample in samples_qcd_copy:
        sample.scale = scale if sample.name == 'data_obs' else -scale

    qcd_c_region = HistogramCfg(name='QCD_C_region',
                                var=norm_var,
                                cfgs=samples_qcd_copy,
                                cut=str(QCD_C_region_cut),
                                lumi=int_lumi,
                                weight=total_weight)
    qcd_d_region = HistogramCfg(name='QCD_D_region',
                                var=norm_var,
                                cfgs=samples_qcd_copy,
                                cut=str(QCD_D_region_cut),
                                lumi=int_lumi,
                                weight=total_weight)

    # samples_qcd = [qcd_c_region, qcd_d_region]

    # cfg_qcd = HistogramCfg(name='QCD_aux', var=None, cfgs=samples_qcd, cut=None, lumi=int_lumi, weight=total_weight)

    plot_qcd_c = createHistogram(qcd_c_region,
                                 all_stack=True,
                                 friend_func=friend_func)
    plot_qcd_d = createHistogram(qcd_d_region,
                                 all_stack=True,
                                 friend_func=friend_func)

    if verbose:
        print 'Histogram C region'
        print plot_qcd_c
        print 'Histogram D region'
        print plot_qcd_d

    yield_c = plot_qcd_c.GetStack().totalHist.Yield()
    yield_d = plot_qcd_d.GetStack().totalHist.Yield()

    if yield_d == 0.:
        print 'WARNING: no events left for the QCD estimation. Set to 0'
        qcd_scale = 0.
    else:
        qcd_scale = yield_c / yield_d

    if qcd_scale < 0.:
        print 'WARNING: negative QCD scaling; set it to zero'
        qcd_scale = 0.
        verbose = True

    if verbose:
        print 'QCD estimation: '
        print '  Yield C:', yield_c, ' yield D:', yield_d
        print '  Ratio C/D', qcd_scale

    qcd_b_region_hist = HistogramCfg(name='QCD',
                                     var=None,
                                     cfgs=samples_qcd_copy,
                                     cut=str(QCD_B_region_cut),
                                     lumi=int_lumi,
                                     weight=total_weight,
                                     total_scale=qcd_scale)

    all_samples_qcd = copy.deepcopy(all_samples)
    all_samples_qcd = [qcd_b_region_hist] + all_samples_qcd

    return all_samples_qcd
                                   var=None,
                                   cfgs=samples_qcdfromss,
                                   cut=inc_cut,
                                   lumi=int_lumi,
                                   weight=total_weight)
    else:
        cfg_example = HistogramCfg(name='example',
                                   var=None,
                                   cfgs=samples,
                                   cut=inc_cut,
                                   lumi=int_lumi,
                                   weight=total_weight)

    cfg_example.cut = cuts[cut_name]
    if qcd_from_same_sign and not 'SS' in cut_name:
        qcd.cut = cuts[cut_name].replace('l1_charge != l2_charge',
                                         'l1_charge == l2_charge')

    for variable in variables:
        cfg_example.var = variable
        if qcd_from_same_sign:
            qcd.var = variable  # Can put into function but we will not want it by default if we take normalisations from e.g. high MT

        plot = createHistogram(cfg_example, verbose=True)
        plot.Group('VV', ['ZZ', 'WZ', 'WW', 'T_tWch', 'TBar_tWch'])
        # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])
        # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style)
        HistDrawer.draw(plot, plot_dir='plots/RemoveLowMET_' + cut_name)

        plot.WriteDataCard(filename='datacard_mvis.root', dir='mt_' + cut_name)
示例#7
0
def makePlots(variables,
              cuts,
              total_weight,
              sample_dict,
              hist_dict,
              qcd_from_same_sign,
              w_qcd_mssm_method,
              mt_cut,
              friend_func,
              dc_postfix,
              make_plots=True):
    for cut in cuts:
        if qcd_from_same_sign and not 'SS' in cut.name and not w_qcd_mssm_method:
            cfg_main = HistogramCfg(name=cut.name,
                                    var=None,
                                    cfgs=sample_dict['samples_qcdfromss'],
                                    cut=cut.cut,
                                    lumi=int_lumi,
                                    weight=total_weight)
        elif w_qcd_mssm_method:
            cfg_main = HistogramCfg(name=cut.name,
                                    var=None,
                                    cfgs=sample_dict['samples_mssm_method'],
                                    cut=cut.cut,
                                    lumi=int_lumi,
                                    weight=total_weight)
            hist_dict[
                'wjets'].cut = cut.cut  # since wjets is a sub-HistogramCfg
        else:
            cfg_main = HistogramCfg(name=cut.name,
                                    var=None,
                                    cfgs=sample_dict['all_samples'],
                                    cut=cut.cut,
                                    lumi=int_lumi,
                                    weight=total_weight)

        if qcd_from_same_sign and not 'SS' in cut.name:
            hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge',
                                                   'l1_charge == l2_charge')

        if w_qcd_mssm_method:
            hist_dict['stacknow_highmt_os'].name = 'HighMTOS' + cut.name
            hist_dict['stacknow_highmt_ss'].name = 'HighMTSS' + cut.name
            hist_dict['wjets_incl_os'].name = 'WInclOS' + cut.name
            hist_dict['wjets_incl_ss'].name = 'WInclSS' + cut.name
            hist_dict['wjets_highmt_os'].name = 'WHighMTOS' + cut.name
            hist_dict['wjets_ss'].name = 'WJetsSS' + cut.name

            hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge',
                                                   'l1_charge == l2_charge')
            hist_dict['wjets_ss'].cut = cut.cut.replace(
                'l1_charge != l2_charge', 'l1_charge == l2_charge')

            hist_dict['stacknow_highmt_os'].cut = cut.cut.replace(
                mt_cut, 'mt>70') + '&& mt>70'
            hist_dict['stacknow_highmt_ss'].cut = cut.cut.replace(
                'l1_charge != l2_charge', 'l1_charge == l2_charge').replace(
                    mt_cut, 'mt>70') + '&& mt>70'

            hist_dict['wjets_incl_os'].cut = cut.cut.replace(mt_cut, '1.')
            hist_dict['wjets_incl_ss'].cut = cut.cut.replace(
                'l1_charge != l2_charge',
                'l1_charge == l2_charge').replace(mt_cut, '1.')

            hist_dict['wjets_highmt_os'].cut = cut.cut.replace(
                mt_cut, 'mt>70') + '&& mt>70'

            plot_w_os = createHistogram(hist_dict['wjets_incl_os'],
                                        verbose=False,
                                        friend_func=friend_func)
            plot_w_ss = createHistogram(hist_dict['wjets_incl_ss'],
                                        verbose=False,
                                        friend_func=friend_func)

            r_w_os_ss = plot_w_os.GetStack().totalHist.Yield(
            ) / plot_w_ss.GetStack().totalHist.Yield()
            print 'Inclusive W OS/SS ratio:', r_w_os_ss

            plot_highmt_os = createHistogram(hist_dict['stacknow_highmt_os'],
                                             all_stack=True,
                                             verbose=False,
                                             friend_func=friend_func)
            plot_highmt_ss = createHistogram(hist_dict['stacknow_highmt_ss'],
                                             all_stack=True,
                                             verbose=False,
                                             friend_func=friend_func)
            createDefaultGroups(plot_highmt_os)
            createDefaultGroups(plot_highmt_ss)

            yield_highmt_os = plot_highmt_os.GetStack().totalHist.Yield()
            yield_highmt_ss = plot_highmt_ss.GetStack().totalHist.Yield()

            plot_w_highmt_os = createHistogram(hist_dict['wjets_highmt_os'],
                                               verbose=False,
                                               friend_func=friend_func)

            yield_w_highmt_os = plot_w_highmt_os.GetStack().totalHist.Yield()

            if r_w_os_ss < r_qcd_os_ss:
                print 'WARNING, OS/SS ratio larger for QCD than for W+jets!', r_w_os_ss, r_qcd_os_ss

            yield_estimation = r_w_os_ss * (yield_highmt_os -
                                            r_qcd_os_ss * yield_highmt_ss) / (
                                                r_w_os_ss - r_qcd_os_ss)

            print 'High MT W+jets estimated yield', yield_estimation
            print 'High MT W+jets MC yield', yield_w_highmt_os

            w_sf = 0.
            if yield_w_highmt_os:
                w_sf = yield_estimation / yield_w_highmt_os
            else:
                print 'Warning: no MC events in high MT W+jets'

            print 'W+jets scale factor:', w_sf, '\n'
            if cut.name == 'vbf_highmva0':
                print 'VBF very tight category, fixing W+jets SF to 1'
                w_sf = 1.
            hist_dict['wjets'].total_scale = w_sf
            hist_dict['wjets_ss'].total_scale = -w_sf

        cfg_main.vars = variables
        if qcd_from_same_sign:
            hist_dict[
                'qcd'].vars = variables  # Can put into function but we will not want it by default if we take normalisations from e.g. high MT
        if w_qcd_mssm_method:
            hist_dict[
                'wjets'].vars = variables  # Can put into function but we will not want it by default if we take normalisations from e.g. high MT
            hist_dict['qcd'].vars = variables
            hist_dict['wjets_ss'].vars = variables

        for variable in variables:
            if variable.name in [
                    'svfit_mass', 'svfit_transverse_mass', 'mvis'
            ] and 'mssm' in mode:
                if cut.name in ['inclusive', 'nobtag']:
                    variable.binning = binning_mssm
                elif cut.name in ['btag']:
                    variable.binning = binning_mssm_btag

        plots = createHistograms(cfg_main,
                                 verbose=False,
                                 friend_func=friend_func)
        for variable in variables:
            # for plot in plots.itervalues():
            plot = plots[variable.name]
            createDefaultGroups(plot)
            if not w_qcd_mssm_method:
                plot.Group('W', ['W', 'W1Jets', 'W2Jets', 'W3Jets', 'W4Jets'])
            # plot.Group('Electroweak', ['Diboson', 'W'])
            # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])
            # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style)
            if make_plots:
                HistDrawer.draw(plot, plot_dir='plots/' + cut.name)
            if variable.name in [
                    'mvis', 'svfit_transverse_mass', 'svfit_mass', 'mva',
                    'mva2div1', 'mva1', 'mva2', 'l2_nc_ratio'
            ]:
                plot.WriteDataCard(
                    filename='datacard_{mode}_{var}.root'.format(
                        mode=mode, var=variable.name),
                    dir='mt_' + cut.name,
                    mode='UPDATE',
                    postfix=dc_postfix)  #mt = mu-tau
示例#8
0
    SampleCfg(name='WJetsToLNu', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WJetsToLNu.xSection, sumweights=WJetsToLNu.nGenEvents),
    SampleCfg(name='TTJets', dir_name='TTLep_pow', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TT_pow.xSection, sumweights=TT_pow.nGenEvents),
    SampleCfg(name='T_tWch', dir_name='T_tWch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=T_tWch.xSection, sumweights=T_tWch.nGenEvents),
    SampleCfg(name='TBar_tWch', dir_name='TBar_tWch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TBar_tWch.xSection, sumweights=TBar_tWch.nGenEvents),
    SampleCfg(name='TToLeptons_tch', dir_name='TToLeptons_tch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TToLeptons_tch.xSection, sumweights=TBar_tWch.nGenEvents),
    SampleCfg(name='TToLeptons_sch', dir_name='TToLeptons_sch', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=TToLeptons_sch.xSection, sumweights=TToLeptons_sch.nGenEvents),
    SampleCfg(name='ZZ', dir_name='ZZp8', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=ZZp8.xSection, sumweights=ZZp8.nGenEvents),
    SampleCfg(name='WZ', dir_name='WZp8', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WZp8.xSection, sumweights=WZp8.nGenEvents),
    SampleCfg(name='WW', dir_name='WWTo2L2Nu', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=WWTo2L2Nu.xSection, sumweights=WWTo2L2Nu.nGenEvents),
    SampleCfg(name='QCD', dir_name='QCD_Mu15', ana_dir=analysis_dir, tree_prod_name=tree_prod_name, xsec=QCD_Mu15.xSection),
    SampleCfg(name='Data', dir_name='SingleMuon_Run2015B', ana_dir=data_dir, tree_prod_name=tree_prod_name, is_data=True),
]
# -> Can add cross sections for samples either explicitly, or from file, or from cfg

for sample in samples:
    setSumWeights(sample)

# Taken from Variables.py, can get subset with e.g. getVars(['mt', 'mvis'])
variables = all_vars

cfg_example = HistogramCfg(name='example', var=None, cfgs=samples, cut=example_cut, lumi=40.0)

for variable in variables:
    cfg_example.var = variable
    plot = createHistogram(cfg_example)
    plot.Group('Diboson', ['ZZ', 'WZ', 'WW'])
    plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])
    HistDrawer.draw(plot)
    # HistDrawer.drawRatio(plot)

示例#9
0
for var_set in var_sets:
    print 'Variable set', var_set.name

    cfg_signal.cut += var_set.cut_s
    cfg_bg.cut += var_set.cut_b

    rocs = []
    sig_eff = None
    bg_eff = None
    for i_var, var in enumerate(var_set.vars):
        print '  variable:', var
        cfg_signal.var = var
        cfg_bg.var = var

        plot_signal = createHistogram(cfg_signal, verbose=False)
        plot_bg = createHistogram(cfg_bg, verbose=False)

        h_signal = plot_signal.GetStack().totalHist.weighted
        h_bg = plot_bg.GetStack().totalHist.weighted

        print 'Working point at cut 1.5:'

        if not sig_eff and not bg_eff:
            sig_eff = h_signal.Integral(
                0, h_signal.FindBin(1.5)) / h_signal.Integral(
                    0,
                    h_signal.GetNbinsX() + 1)
            bg_eff = h_bg.Integral(0, h_bg.FindBin(1.5)) / h_bg.Integral(
                0,
                h_bg.GetNbinsX() + 1)
示例#10
0
        cut.cut & iso_cut & (~charge_cut),
        cut.cut & iso_sideband_cut & (~charge_cut), all_samples, int_lumi,
        total_weight)

    # now include charge and isolation too
    cut = myCut(cut.name, cut.cut & iso_cut & charge_cut)

    for variable in variables:
        cfg_total = HistogramCfg(name=cut.name,
                                 var=variable,
                                 cfgs=all_samples_qcd,
                                 cut=str(cut.cut),
                                 lumi=int_lumi,
                                 weight=total_weight)
        all_samples_qcd[-1].var = variable
        plot = createHistogram(cfg_total)
        plot.Group('VV', [
            'ZZTo2L2Q', 'WWTo1L1Nu2Q', 'WZTo1L3Nu', 'ZZTo4L', 'WZTo3L',
            'WZTo2L2Q', 'WZTo1L1Nu2Q', 'T_tWch', 'TBar_tWch',
            'TToLeptons_tch_powheg', 'TBarToLeptons_tch_powheg'
        ])
        plot.Group('ZTT',
                   ['ZTT', 'ZTT1Jets', 'ZTT2Jets', 'ZTT3Jets', 'ZTT4Jets'])
        plot.Group('ZJ', ['ZJ', 'ZJ1Jets', 'ZJ2Jets', 'ZJ3Jets', 'ZJ4Jets'])
        plot.Group('ZL', ['ZL', 'ZL1Jets', 'ZL2Jets', 'ZL3Jets', 'ZL4Jets'])
        plot.Group('WJets', ['WJetsToLNu', 'W1JetsToLNu', 'W4JetsToLNu'])
        plot.Group('Single t',
                   ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])

        HistDrawer.draw(plot,
                        channel='#tau_{h}#tau_{h}',
示例#11
0
                                   cfgs=all_samples,
                                   cut=inc_cut,
                                   lumi=int_lumi,
                                   weight=total_weight)

    cfg_example.cut = cut.cut
    if qcd_from_same_sign and not 'SS' in cut.name:
        qcd.cut = cut.cut.replace('l1_charge != l2_charge',
                                  'l1_charge == l2_charge')

    for variable in variables:
        cfg_example.var = variable
        if qcd_from_same_sign:
            qcd.var = variable  # Can put into function but we will not want it by default if we take normalisations from e.g. high MT

        plot = createHistogram(cfg_example, verbose=False)
        plot.Group('VV', [
            'ZZTo2L2Q', 'WWTo1L1Nu2Q', 'WZTo1L3Nu', 'ZZTo4L', 'WZTo3L',
            'WZTo2L2Q', 'WZTo1L1Nu2Q', 'T_tWch', 'TBar_tWch',
            'TToLeptons_tch_powheg', 'TBarToLeptons_tch_powheg'
        ])
        plot.Group('ZTT',
                   ['ZTT', 'ZTT1Jets', 'ZTT2Jets', 'ZTT3Jets', 'ZTT4Jets'])
        plot.Group('ZJ', ['ZJ', 'ZJ1Jets', 'ZJ2Jets', 'ZJ3Jets', 'ZJ4Jets'])
        plot.Group('ZL', ['ZL', 'ZL1Jets', 'ZL2Jets', 'ZL3Jets', 'ZL4Jets'])
        # plot.Group('Single t', ['T_tWch', 'TBar_tWch', 'TToLeptons_sch', 'TToLeptons_tch'])
        # plot.Group('ZLL', ['Ztt_ZL', 'Ztt_ZJ'], style=plot.Hist('Ztt_ZL').style)
        HistDrawer.draw(plot, plot_dir='plots/' + cut.name)
        if variable.name == 'mvis':
            plot.WriteDataCard(filename='datacard_mvis.root',
                               dir='mt_' + cut.name,
def estimateQCDWMSSM(hist_dict,
                     cut,
                     mt_cut,
                     high_mt_cut='mt>70',
                     friend_func=None,
                     r_qcd_os_ss=1.17):
    hist_dict['stacknow_highmt_os'].name = 'HighMTOS' + cut.name
    hist_dict['stacknow_highmt_ss'].name = 'HighMTSS' + cut.name
    hist_dict['wjets_incl_os'].name = 'WInclOS' + cut.name
    hist_dict['wjets_incl_ss'].name = 'WInclSS' + cut.name
    hist_dict['wjets_highmt_os'].name = 'WHighMTOS' + cut.name
    hist_dict['wjets_ss'].name = 'WJetsSS' + cut.name

    hist_dict['qcd'].cut = cut.cut.replace('l1_charge != l2_charge',
                                           'l1_charge == l2_charge')
    hist_dict['wjets_ss'].cut = cut.cut.replace('l1_charge != l2_charge',
                                                'l1_charge == l2_charge')

    hist_dict['stacknow_highmt_os'].cut = cut.cut.replace(
        mt_cut, high_mt_cut) + '&& ' + high_mt_cut
    hist_dict['stacknow_highmt_ss'].cut = cut.cut.replace(
        'l1_charge != l2_charge', 'l1_charge == l2_charge').replace(
            mt_cut, high_mt_cut) + '&& ' + high_mt_cut

    hist_dict['wjets_incl_os'].cut = cut.cut.replace(mt_cut, '1.')
    hist_dict['wjets_incl_ss'].cut = cut.cut.replace(
        'l1_charge != l2_charge',
        'l1_charge == l2_charge').replace(mt_cut, '1.')

    hist_dict['wjets_highmt_os'].cut = cut.cut.replace(
        mt_cut, high_mt_cut) + '&& ' + high_mt_cut

    plot_w_os = createHistogram(hist_dict['wjets_incl_os'],
                                verbose=False,
                                friend_func=friend_func)
    plot_w_ss = createHistogram(hist_dict['wjets_incl_ss'],
                                verbose=False,
                                friend_func=friend_func)

    r_w_os_ss = plot_w_os.GetStack().totalHist.Yield() / plot_w_ss.GetStack(
    ).totalHist.Yield()
    print 'Inclusive W OS/SS ratio:', r_w_os_ss

    plot_highmt_os = createHistogram(hist_dict['stacknow_highmt_os'],
                                     all_stack=True,
                                     verbose=False,
                                     friend_func=friend_func)
    plot_highmt_ss = createHistogram(hist_dict['stacknow_highmt_ss'],
                                     all_stack=True,
                                     verbose=False,
                                     friend_func=friend_func)
    createDefaultGroups(plot_highmt_os)
    createDefaultGroups(plot_highmt_ss)

    yield_highmt_os = plot_highmt_os.GetStack().totalHist.Yield()
    yield_highmt_ss = plot_highmt_ss.GetStack().totalHist.Yield()

    plot_w_highmt_os = createHistogram(hist_dict['wjets_highmt_os'],
                                       verbose=False,
                                       friend_func=friend_func)

    print plot_w_highmt_os
    print plot_highmt_os
    print 'Having used cut', hist_dict['stacknow_highmt_os'].cut
    print plot_highmt_ss

    yield_w_highmt_os = plot_w_highmt_os.GetStack().totalHist.Yield()

    if r_w_os_ss < r_qcd_os_ss:
        print 'WARNING, OS/SS ratio larger for QCD than for W+jets!', r_w_os_ss, r_qcd_os_ss

    yield_estimation = r_w_os_ss * (
        yield_highmt_os - r_qcd_os_ss * yield_highmt_ss) / (r_w_os_ss -
                                                            r_qcd_os_ss)

    print 'High MT W+jets estimated yield', yield_estimation
    print 'High MT W+jets MC yield', yield_w_highmt_os

    w_sf = 0.
    if yield_w_highmt_os:
        w_sf = yield_estimation / yield_w_highmt_os
    else:
        print 'Warning: no MC events in high MT W+jets'

    print 'W+jets scale factor:', w_sf, '\n'

    if w_sf <= 0.25:
        print 'Warning: W+jets scale factor very small or negative', w_sf, 'setting to 1.'
        w_sf = 1.

    if w_sf > 2.5 and yield_w_highmt_os and yield_w_highmt_os < 10.:
        print 'Warning: W+jets scale factor very large', w_sf, ' but low stats, setting to 1.'
        w_sf = 1.

    # if cut.name in ['vbf_highmva0', '0jet_highmva0']:
    #     print 'Very tight category, fixing W+jets SF to 1'
    #     w_sf = 1.
    hist_dict['wjets'].total_scale = w_sf
    hist_dict['wjets_ss'].total_scale = -w_sf