def make_hist( data, hist, reg_bin, output_hist ) :

    for ptbin in range( 1, hist.GetNbinsX()+1 ) :
        min = int(hist.GetXaxis().GetBinLowEdge(ptbin))
        max = int(hist.GetXaxis().GetBinUpEdge(ptbin))

        if max <= 15 :
            continue

        maxval = str(max)
        if ptbin == hist.GetNbinsX() :
            maxval = 'max'

        databin = ( reg_bin, str( min), maxval )

        print data['stat+syst']['result'].keys()
        hist.SetBinContent( ptbin, data['stat+syst']['result'][databin].n )
        hist.SetBinError( ptbin, data['stat+syst']['result'][databin].s )

    save_hist( output_hist, hist )
def make_hist_from_pickle(sampMan, input_file, output_hist, tag, regions):

    # get jet fake background estimate
    if not os.path.isfile(input_file):
        print 'Could not find input file ', input_file
        return

    ofile = open(input_file, 'r')

    data = pickle.load(ofile)

    ofile.close()

    samp_list = sampMan.get_samples()
    for reg in regions:
        hist = None
        for s in samp_list:
            if s.hist is not None:
                hist = s.hist.Clone('pt_leadph12_%s_%s' % (tag, reg))
                break

        for ptbin in range(1, hist.GetNbinsX() + 1):
            min = int(hist.GetXaxis().GetBinLowEdge(ptbin))
            max = int(hist.GetXaxis().GetBinUpEdge(ptbin))

            if max <= 15:
                continue

            maxval = str(max)
            if ptbin == hist.GetNbinsX():
                maxval = 'max'

            databin = (reg, str(min), maxval)

            hist.SetBinContent(ptbin, data['stat+syst']['result'][databin].n)
            hist.SetBinError(ptbin, data['stat+syst']['result'][databin].s)

        save_hist(output_hist, hist)
def make_hist_from_pickle( sampMan, input_file, output_hist, tag, regions ) :

    # get jet fake background estimate
    if not os.path.isfile( input_file) :
        print 'Could not find input file ', input_file
        return

    ofile = open( input_file, 'r' )

    data = pickle.load(ofile)

    ofile.close()

    samp_list = sampMan.get_samples()
    for reg in regions :
        hist = None
        for s in samp_list :
            if s.hist is not None :
                hist = s.hist.Clone( 'pt_leadph12_%s_%s' %(tag, reg) )
                break

        for ptbin in range( 1, hist.GetNbinsX()+1 ) :
            min = int(hist.GetXaxis().GetBinLowEdge(ptbin))
            max = int(hist.GetXaxis().GetBinUpEdge(ptbin))

            if max <= 15 :
                continue

            maxval = str(max)
            if max > 100 :
                maxval = 'max'

            databin = ( reg, str( min), maxval )

            hist.SetBinContent( ptbin, data['stat+syst']['result'][databin].n )
            hist.SetBinError( ptbin, data['stat+syst']['result'][databin].s )

        save_hist( output_hist, hist )
def draw_singleph_hists( var, selection, bins, data_sample, baseDir, plotDir, hist_name ) :

    samplesLG.Draw( var, selection, bins)

    hist_data_mg = samplesLG.get_samples(name=data_sample)[0].hist.Clone(hist_name)
    save_hist( '%s/%s/Data/hist.root' %(baseDir, plotDir), hist_data_mg )

    hist_wg_mg  = samplesLG.get_samples(name='Wgamma')[0].hist.Clone(hist_name)
    save_hist( '%s/%s/Wg/hist.root' %(baseDir, plotDir), hist_wg_mg )

    hist_zg_mg  = samplesLG.get_samples(name='Zgamma')[0].hist.Clone(hist_name)
    save_hist( '%s/%s/Zg/hist.root' %(baseDir, plotDir), hist_zg_mg )
def MakeSingleBkgEstimatePlots( baseDir, plotDir, channels=[], makeEleFake=True ) :

    # first make the nominal estimates

    regions = ['EB','EE']

    for ch in channels :
        for reg in regions :


            samplesLG.deactivate_all_samples()
            samplesLG.activate_sample( 'Wgamma' )
            samplesLG.activate_sample( 'Zgamma' )
            samplesLG.activate_sample( 'DYJetsToLLPhOlap' )

            data_sample = None
            if ch.count('mu') :
                data_sample = 'Muon'
            if ch.count('el') :
                data_sample = 'Electron'
    
            samplesLG.activate_sample( data_sample )
            samplesLG.Draw( 'ph_pt[0]', ' PUWeight * ( %s && ph_Is%s[0]  ) ' %(el_cuts[ch], reg), _ptbins)

            hist_data_mg = samplesLG.get_samples(name=data_sample)[0].hist.Clone('pt_leadph12_%s_%s'%(ch,reg))
            save_hist( '%s/%s/Data/hist.root' %(baseDir, plotDir), hist_data_mg )

            hist_wg_mg  = samplesLG.get_samples(name='Wgamma')[0].hist.Clone('pt_leadph12_%s_%s'%(ch,reg))
            save_hist( '%s/%s/Wg/hist.root' %(baseDir, plotDir), hist_wg_mg )

            hist_zg_mg  = samplesLG.get_samples(name='Zgamma')[0].hist.Clone('pt_leadph12_%s_%s'%(ch,reg))
            save_hist( '%s/%s/Zg/hist.root' %(baseDir, plotDir), hist_zg_mg )

            hist_zjets_mg  = samplesLG.get_samples(name='DYJetsToLLPhOlap')[0].hist.Clone('pt_leadph12_%s_%s'%(ch,reg))
            save_hist( '%s/%s/ZJets/hist.root' %(baseDir, plotDir), hist_zjets_mg )

        make_hist_from_pickle( samplesLG, baseDir + '/jet_fake_results__%s.pickle'%ch            , '%s/%s/JetFake/hist.root' %(baseDir, plotDir), tag=ch, regions=regions )

    if makeEleFake :
        make_hist_from_pickle( samplesLG, baseDir + '/electron_fake_results__zcr.pickle'       , '%s/%s/EleFake/hist.root' %(baseDir, plotDir), tag='elwzcr', regions=regions)
        make_hist_from_pickle( samplesLG, baseDir + '/electron_fake_results.pickle'            , '%s/%s/EleFake/hist.root' %(baseDir, plotDir), tag='elw', regions=regions)
def draw_singleph_hists_proj( var, selection, bins, data_sample, baseDir, plotDir, hist_name, eta_bins ) :

    #samplesLG.Draw( var, selection, bins)

    #hist_data_inc = samplesLG.get_samples( name=data_sample )[0].hist
    #hist_wg_inc   = samplesLG.get_samples( name='Wgamma'    )[0].hist
    #hist_zg_inc   = samplesLG.get_samples( name='Zgamma'    )[0].hist

    data_samp = samplesLG.get_samples( name=data_sample )[0]
    wg_samp   = samplesLG.get_samples( name='Wgamma')[0]
    zg_samp   = samplesLG.get_samples( name='Zgamma')[0]

    samplesLG.create_hist(data_samp, var, selection, bins )
    samplesLG.create_hist(wg_samp, var, selection, bins )
    samplesLG.create_hist(zg_samp, var, selection, bins )

    hist_data_inc = data_samp.hist.Clone( '%s_inc' %data_sample )
    hist_wg_inc   = wg_samp.hist.Clone( 'Wgamma_inc' )
    hist_zg_inc   = zg_samp.hist.Clone( 'Zgamma_inc' )

    for etamin, etamax in eta_bins :

        full_hist_name = '%s_%.2f-%.2f' %( hist_name, etamin, etamax )

        etabinmin = hist_data_inc.GetXaxis().FindBin( etamin )
        etabinmax = hist_data_inc.GetXaxis().FindBin( etamax ) - 1

        hist_data_mg = hist_data_inc.ProjectionY( full_hist_name, etabinmin, etabinmax );
        hist_wg_mg   = hist_wg_inc  .ProjectionY( full_hist_name, etabinmin, etabinmax );
        hist_zg_mg   = hist_zg_inc  .ProjectionY( full_hist_name, etabinmin, etabinmax );

        save_hist( '%s/%s/Data/hist.root' %(baseDir, plotDir), hist_data_mg )

        save_hist( '%s/%s/Wg/hist.root' %(baseDir, plotDir), hist_wg_mg )

        save_hist( '%s/%s/Zg/hist.root' %(baseDir, plotDir), hist_zg_mg )
def MakeSingleBkgEstimatePlots(baseDir,
                               plotDir,
                               channels=[],
                               makeEleFake=True):

    # first make the nominal estimates

    regions = ['EB', 'EE']

    for ch in channels:
        for reg in regions:

            samplesLG.deactivate_all_samples()
            samplesLG.activate_sample('Wgamma')
            samplesLG.activate_sample('Zgamma')
            samplesLG.activate_sample('DYJetsToLLPhOlap')

            data_sample = None
            if ch.count('mu'):
                data_sample = 'Muon'
            if ch.count('el'):
                data_sample = 'Electron'

            samplesLG.activate_sample(data_sample)
            samplesLG.Draw(
                'ph_pt[0]',
                ' PUWeight * ( %s && ph_Is%s[0]  ) ' % (el_cuts[ch], reg),
                _ptbins)

            hist_data_mg = samplesLG.get_samples(
                name=data_sample)[0].hist.Clone('pt_leadph12_%s_%s' %
                                                (ch, reg))
            save_hist('%s/%s/Data/hist.root' % (baseDir, plotDir),
                      hist_data_mg)

            hist_wg_mg = samplesLG.get_samples(name='Wgamma')[0].hist.Clone(
                'pt_leadph12_%s_%s' % (ch, reg))
            save_hist('%s/%s/Wg/hist.root' % (baseDir, plotDir), hist_wg_mg)

            hist_zg_mg = samplesLG.get_samples(name='Zgamma')[0].hist.Clone(
                'pt_leadph12_%s_%s' % (ch, reg))
            save_hist('%s/%s/Zg/hist.root' % (baseDir, plotDir), hist_zg_mg)

            hist_zjets_mg = samplesLG.get_samples(
                name='DYJetsToLLPhOlap')[0].hist.Clone('pt_leadph12_%s_%s' %
                                                       (ch, reg))
            save_hist('%s/%s/ZJets/hist.root' % (baseDir, plotDir),
                      hist_zjets_mg)

        make_hist_from_pickle(samplesLG,
                              baseDir + '/jet_fake_results__%s.pickle' % ch,
                              '%s/%s/JetFake/hist.root' % (baseDir, plotDir),
                              tag=ch,
                              regions=regions)

    if makeEleFake:
        make_hist_from_pickle(samplesLG,
                              baseDir + '/electron_fake_results__zcr.pickle',
                              '%s/%s/EleFake/hist.root' % (baseDir, plotDir),
                              tag='elwzcr',
                              regions=regions)
        make_hist_from_pickle(samplesLG,
                              baseDir + '/electron_fake_results.pickle',
                              '%s/%s/EleFake/hist.root' % (baseDir, plotDir),
                              tag='elw',
                              regions=regions)