def MakeSingleJetBkgEstimate(base_dir_jet, channels=[], outputDir=None):

    if not isinstance(channels, list):
        channels = [channels]

    if outputDir is not None:
        if not os.path.isdir(outputDir):
            os.makedirs(outputDir)

    regions = ['EB', 'EE']

    # get the pt bins in the format expected
    # in the pickle files
    pt_bins_jetfile = [str(x) for x in _ptbins[:-1]]
    pt_bins_jetfile.append('max')

    # regex expression to match directories
    # where jet fake results should be found
    # if the Corr directories are used
    # it will prioritize to the minimum values
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'

    # get the directories mapped to the integer values matched
    # if no values are matched the directory is matched ot (0,0,0)
    jet_dir_key_map = get_mapped_directory(base_dir_jet, jet_dirs_key)

    for channel in channels:
        file_key = 'results__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' % channel
        file_key_syst = 'results__syst__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' % channel

        jet_files = get_dirs_and_files(base_dir_jet, jet_dirs_key, file_key)
        jet_files_syst = get_dirs_and_files(base_dir_jet, jet_dirs_key,
                                            file_key_syst)

        jet_pred = get_jet_single_fake_results(jet_files, jet_files_syst,
                                               regions, pt_bins_jetfile,
                                               jet_dir_key_map, base_dir_jet)

        print 'Predicted Jet fakes for channel %s' % channel

        for r1 in regions:

            for idx, ptmin in enumerate(pt_bins_jetfile[:-1]):
                ptmax = pt_bins_jetfile[idx + 1]

                bin = (r1, ptmin, ptmax)

                print 'Region %s, pt %s-%s' % (r1, ptmin, ptmax)
                print 'Predicted Stat r = %s, predicted f = %s' % (
                    jet_pred['stat']['r'][bin], jet_pred['stat']['f'][bin])
                print 'Predicted Syst r = %s, predicted f = %s' % (
                    jet_pred['syst']['r'][bin], jet_pred['syst']['f'][bin])
                print 'Predicted Stat+Syst r = %s, predicted f = %s' % (
                    jet_pred['stat+syst']['r'][bin],
                    jet_pred['stat+syst']['f'][bin])

        file = open(outputDir + '/jet_fake_results__%s.pickle' % channel, 'w')
        pickle.dump(jet_pred, file)
        file.close()
def MakeSingleJetBkgEstimate( base_dir_jet, channels=[], outputDir=None ) :

    if not isinstance(channels, list ) :
        channels = [channels]

    if outputDir is not None :
        if not os.path.isdir( outputDir ) :
            os.makedirs( outputDir )

    regions = ['EB', 'EE']

    # get the pt bins in the format expected
    # in the pickle files
    pt_bins_jetfile = [str(x) for x in _ptbins[:-1]]
    pt_bins_jetfile.append( 'max')

    # regex expression to match directories
    # where jet fake results should be found
    # if the Corr directories are used 
    # it will prioritize to the minimum values
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'

    # get the directories mapped to the integer values matched
    # if no values are matched the directory is matched ot (0,0,0)
    jet_dir_key_map = get_mapped_directory( base_dir_jet, jet_dirs_key )

    for channel in channels :
        file_key = 'results__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %channel
        file_key_syst = 'results__syst__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %channel

        jet_files      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key      )
        jet_files_syst = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_syst )

        jet_pred     = get_jet_single_fake_results( jet_files    , jet_files_syst    , regions, pt_bins_jetfile,  jet_dir_key_map, base_dir_jet ) 

        print 'Predicted Jet fakes for channel %s' %channel

        for r1 in regions :

            for idx, ptmin in enumerate(pt_bins_jetfile[:-1] ) :
                ptmax = pt_bins_jetfile[idx+1]

                bin = (r1,ptmin,ptmax)

                print 'Region %s, pt %s-%s' %( r1,ptmin,ptmax)
                print 'Predicted Stat r = %s, predicted f = %s' %( jet_pred['stat']['r'][bin], jet_pred['stat']['f'][bin] ) 
                print 'Predicted Syst r = %s, predicted f = %s' %( jet_pred['syst']['r'][bin], jet_pred['syst']['f'][bin] )
                print 'Predicted Stat+Syst r = %s, predicted f = %s' %( jet_pred['stat+syst']['r'][bin], jet_pred['stat+syst']['f'][bin] )

        file = open( outputDir + '/jet_fake_results__%s.pickle' %channel, 'w' )
        pickle.dump( jet_pred, file )
        file.close()
def MakeSingleEleBkgEstimate(base_dir_ele,
                             base_dir_jet,
                             file_bin_map,
                             file_bin_map_syst,
                             outputDir=None,
                             el_selection='el',
                             namePostfix=''):

    el_acc = ['elw', 'elwzcr']
    if el_selection not in el_acc:
        print 'Input region not recognized, must be %s' % (','.join(el_acc))
        return

    samplesLLG.deactivate_all_samples()
    samplesLLG.activate_sample('Data')

    eta_bins = {
        'EB': [0.0, 0.1, 0.5, 1.0, 1.44],
        'EE': [1.57, 2.1, 2.2, 2.3, 2.4, 2.5]
    }
    regions = ['EB', 'EE']

    results_nom = get_ele_single_fakefactors(base_dir_ele, file_bin_map,
                                             regions, eta_bins, el_selection)
    results_syst = get_ele_single_fakefactors(base_dir_ele, file_bin_map_syst,
                                              regions, eta_bins, el_selection)

    file_key_lead = 'results__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' % el_selection
    file_key_lead_syst = 'results__syst__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' % el_selection
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'

    jet_dir_key_map = get_mapped_directory(base_dir_jet, jet_dirs_key)

    jet_files_lead = get_dirs_and_files(base_dir_jet, jet_dirs_key,
                                        file_key_lead)
    jet_files_lead_syst = get_dirs_and_files(base_dir_jet, jet_dirs_key,
                                             file_key_lead_syst)

    print 'jet_files_lead'
    print jet_files_lead

    pt_bins_jetfile = [str(x) for x in _ptbins[:-1]]
    pt_bins_jetfile.append('max')
    pred_lead = get_jet_single_fake_results(jet_files_lead,
                                            jet_files_lead_syst, regions,
                                            pt_bins_jetfile, jet_dir_key_map,
                                            base_dir_jet)

    # get fake factors and binning from file
    rfile_coarse = ROOT.TFile.Open(
        '%s/ElectronFakeFitsRatioCoarseEta/results.root' % base_dir_ele)
    rhist_coarse = rfile_coarse.Get('ff')

    scaled_f = {}
    for r1 in regions:

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]):
            ptmax = pt_bins_jetfile[idx + 1]

            ff_lead = -1
            bin_eb = rhist_coarse.GetYaxis().FindBin(1.0)
            bin_ee = rhist_coarse.GetYaxis().FindBin(2.0)
            bin_pt = rhist_coarse.GetXaxis().FindBin(int(ptmin))
            if r1 == 'EB':
                ff_lead = rhist_coarse.GetBinContent(bin_pt, bin_eb)
                print 'ptmin = %s, ptmax = %s, histptmin = %d, histptmax = %d' % (
                    ptmin, ptmax,
                    rhist_coarse.GetXaxis().GetBinLowEdge(bin_pt),
                    rhist_coarse.GetXaxis().GetBinUpEdge(bin_pt))
                print 'reg = EB, histetamin = %f, histetamax = %f' % (
                    rhist_coarse.GetYaxis().GetBinLowEdge(bin_eb),
                    rhist_coarse.GetYaxis().GetBinUpEdge(bin_eb))
            if r1 == 'EE':
                ff_lead = rhist_coarse.GetBinContent(bin_pt, bin_ee)
                print 'reg = EE, histetamin = %f, histetamax = %f' % (
                    rhist_coarse.GetYaxis().GetBinLowEdge(bin_ee),
                    rhist_coarse.GetYaxis().GetBinUpEdge(bin_ee))

            scaled_f[(r1, ptmin,
                      ptmax)] = pred_lead['stat+syst']['f'][(r1, ptmin,
                                                             ptmax)] * ff_lead

    for r1 in regions:

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]):
            ptmax = pt_bins_jetfile[idx + 1]
            print '%s, pt %s-%s' % (r1, ptmin, ptmax)

            print 'Pred f = %s, scaled f = %s' % (pred_lead['stat+syst']['f'][
                (r1, ptmin, ptmax)], scaled_f[(r1, ptmin, ptmax)])

    results_subtracted = {}
    results_subtracted['stat+syst'] = {}
    results_subtracted['stat+syst']['result'] = {}
    for (r1, ptmin,
         ptmax), val in results_nom['stat+syst']['result'].iteritems():
        print 'Reg = %s, pt %s-%s, N Pix CR* ff = %s, N Jet Pix CR * ff = %s, pred = %s' % (
            r1, ptmin, ptmax, val, scaled_f[(r1, ptmin, ptmax)],
            val - scaled_f[(r1, ptmin, ptmax)])
        results_subtracted['stat+syst']['result'][(
            r1, ptmin, ptmax)] = val - scaled_f[(r1, ptmin, ptmax)]

    if outputDir is not None:
        if not os.path.isdir(outputDir):
            os.makedirs(outputDir)

        file_raw = open(
            outputDir +
            '/electron_fake_results%s__noJetFakeSubtraction.pickle' %
            namePostfix, 'w')
        pickle.dump(results_nom, file_raw)
        file_raw.close()

        file_sub = open(
            outputDir + '/electron_fake_results%s.pickle' % namePostfix, 'w')
        pickle.dump(results_subtracted, file_sub)
        file_sub.close()
예제 #4
0
def MakeSingleJetBkgEstimate( base_dir_jet, channel='', pt_bins=[],eta_bins={}, outputDir=None ) :

    if outputDir is not None :
        if not os.path.isdir( outputDir ) :
            os.makedirs( outputDir )

    regions = ['EB', 'EE']

    # get the pt bins in the format expected
    # in the pickle files
    pt_bins_jetfile = [str(x) for x in pt_bins[:-1]]
    pt_bins_jetfile.append( 'max')

    # regex expression to match directories
    # where jet fake results should be found
    # if the Corr directories are used 
    # it will prioritize to the minimum values
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'

    # get the directories mapped to the integer values matched
    # if no values are matched the directory is matched ot (0,0,0)
    jet_dir_key_map = get_mapped_directory( base_dir_jet, jet_dirs_key )

    file_key_eta = 'results__%s__(\d\.\d\d-\d\.\d\d)__pt_(\d+)-(\d+|max).pickle' %channel
    file_key_eta_syst = 'results__syst__%s__(\d\.\d\d-\d\.\d\d)__pt_(\d+)-(\d+|max).pickle' %channel
    file_key = 'results__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %channel
    file_key_syst = 'results__syst__%s__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %channel

    #jet_files_eta      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_eta      )
    #jet_files_eta_syst = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_eta_syst )
    jet_files      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key      )
    jet_files_syst = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_syst )

    #update_jet_files_dict( jet_files, jet_files_eta )
    #update_jet_files_dict( jet_files_syst, jet_files_eta_syst )

    jet_pred     = get_jet_single_fake_results( jet_files    , jet_files_syst    , regions, pt_bins_jetfile,  jet_dir_key_map, base_dir_jet, eta_bins=eta_bins ) 

    print 'Predicted Jet fakes for channel %s' %channel

    for r1 in regions :

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1] ) :
            ptmax = pt_bins_jetfile[idx+1]

            if eta_bins :
                for etamin, etamax in eta_bins[r1] :

                    bin = ('%.2f-%.2f'%(etamin,etamax), ptmin, ptmax)

                    print 'Region %s, pt %s-%s' %( bin[0],ptmin,ptmax)
                    print 'Predicted Stat r = %s, predicted f = %s' %( jet_pred['stat']['r'][bin], jet_pred['stat']['f'][bin] ) 
                    print 'Predicted Syst r = %s, predicted f = %s' %( jet_pred['syst']['r'][bin], jet_pred['syst']['f'][bin] )
                    print 'Predicted Stat+Syst r = %s, predicted f = %s' %( jet_pred['stat+syst']['r'][bin], jet_pred['stat+syst']['f'][bin] )
            else :
                bin = (r1,ptmin,ptmax)

                print 'Region %s, pt %s-%s' %( r1,ptmin,ptmax)
                print 'Predicted Stat r = %s, predicted f = %s' %( jet_pred['stat']['r'][bin], jet_pred['stat']['f'][bin] ) 
                print 'Predicted Syst r = %s, predicted f = %s' %( jet_pred['syst']['r'][bin], jet_pred['syst']['f'][bin] )
                print 'Predicted Stat+Syst r = %s, predicted f = %s' %( jet_pred['stat+syst']['r'][bin], jet_pred['stat+syst']['f'][bin] )



    return jet_pred
def MakeSingleEleBkgEstimate(base_dir_ele, base_dir_jet, file_bin_map, file_bin_map_syst, eta_bins, elffdir, el_selection='elw', outputDir=None) :

    el_acc = ['elw', 'elwzcr', 'elwzcrloose', 'elwsr', 'elwsrlowmt']
    if el_selection not in el_acc :
        print 'Input region not recognized, must be %s' %(','.join(el_acc) )
        return

    samplesLLG.deactivate_all_samples()
    samplesLLG.activate_sample('Data')

    regions = ['EB','EE']

    results_nom = get_ele_single_fakefactors( base_dir_ele, file_bin_map, regions, eta_bins, el_selection )
    # prevent killing from drawing too many plots
    #results_syst = get_ele_single_fakefactors( base_dir_ele, file_bin_map_syst, regions, eta_bins, el_selection )

    file_key_lead_eta = 'results__%sinvpixlead__(\d\.\d\d-\d\.\d\d)__pt_(\d+)-(\d+|max).pickle' %el_selection
    file_key_lead_syst_eta = 'results__syst__%sinvpixlead__(\d\.\d\d-\d\.\d\d)__pt_(\d+)-(\d+|max).pickle' %el_selection
    file_key_lead = 'results__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %el_selection
    file_key_lead_syst = 'results__syst__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %el_selection
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'

    jet_dir_key_map = get_mapped_directory( base_dir_jet, jet_dirs_key )

    jet_files_lead_eta      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead_eta      )
    jet_files_lead_syst_eta = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead_syst_eta )
    jet_files_lead      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead      )
    jet_files_lead_syst = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead_syst )

    update_jet_files_dict( jet_files_lead, jet_files_lead_eta )
    update_jet_files_dict( jet_files_lead_syst, jet_files_lead_syst_eta )

    print 'jet_files_lead'
    print jet_files_lead

    pt_bins_jetfile = [str(x) for x in _ptbins[:-1]]
    pt_bins_jetfile.append( 'max')
    pred_lead = get_jet_single_fake_results( jet_files_lead, jet_files_lead_syst, regions, pt_bins_jetfile,  jet_dir_key_map, base_dir_jet, eta_bins=eta_bins) 

    # get fake factors and binning from file
    ff_man_coarse = FakeFactorManager( '%s/%s/results.pickle' %(base_dir_ele, elffdir), ['fake_ratio'] )
    #ff_man_coarse = FakeFactorManager( '%s/ElectronFakeFitsRatio/results.pickle' %base_dir_ele, ['fake_ratio'] )
    #ff_man_coarse = FakeFactorManager( '%s/ElectronFakeFitsRatioTAndPMCTemplate/results.pickle' %base_dir_ele, ['fake_ratio'] )
    #ff_man_coarse = FakeFactorManager( '%s/ElectronFakeManualWithOlap2/results.pickle' %base_dir_ele, ['fake_ratio'] )

    # determine number of eta bins used
    # if only one eta bin is requested
    # per region, some different behavior occurrs

    eta_lens = []
    for reg in regions :
        eta_lens.append( len(eta_bins[reg] ) )

    eta_lens = list(set(eta_lens))

    single_eta_bin = False
    if len(eta_lens) == 1 and eta_lens[0]==1 :
        single_eta_bin = True


    scaled_f = {}
    for r1 in regions :

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]) :
            ptmax = pt_bins_jetfile[idx+1]

            if r1 == 'EB' :
                ff_lead = ff_man_coarse.get_pt_eta_ff( ptmin, ptmax, 0.0, 1.44 )
            if r1 == 'EE' :
                ff_lead = ff_man_coarse.get_pt_eta_ff( ptmin, ptmax, 1.57, 2.5 )

            scaled_f[ (r1,ptmin,ptmax) ] = pred_lead['stat+syst']['f'][( r1,ptmin,ptmax)]*ff_lead

            
            if not single_eta_bin :
                for etamin, etamax in eta_bins[r1] :
                    ff_lead = ff_man_coarse.get_pt_eta_ff( ptmin, ptmax, etamin, etamax)
                    etabin = '%.2f-%.2f' %( etamin, etamax) 
                    scaled_f [(etabin, ptmin, ptmax) ] =pred_lead['stat+syst']['f'][( etabin,ptmin,ptmax)]*ff_lead


    for r1 in regions :

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]) :
            ptmax = pt_bins_jetfile[idx+1]
            print '%s, pt %s-%s' %( r1, ptmin, ptmax)
    
            print 'Pred f = %s, scaled f = %s' %( pred_lead['stat+syst']['f'][(r1,ptmin,ptmax)], scaled_f[(r1,ptmin,ptmax)] )

            if not single_eta_bin :
            #if eta_bins :
                for etamin, etamax in eta_bins[r1] :
                    etabin = '%.2f-%.2f' %( etamin, etamax) 
                    print '%s, pt %s-%s' %( etabin, ptmin, ptmax)
    
                    print 'Pred f = %s, scaled f = %s' %( pred_lead['stat+syst']['f'][(etabin,ptmin,ptmax)], scaled_f[(etabin,ptmin,ptmax)] )

    results_subtracted = {}
    results_subtracted['stat+syst'] = {}
    results_subtracted['stat+syst']['result'] = {}


    for reg_bin, val in results_nom['stat+syst']['result'].iteritems() :
        r1 = reg_bin[0]
        ptmin = reg_bin[1]
        ptmax = reg_bin[2]
        if reg_bin not in scaled_f :
            continue
        print 'Reg = %s, pt %s-%s, N Pix CR* ff = %s, N Jet Pix CR * ff = %s, pred = %s' %( r1, ptmin, ptmax, val, scaled_f[reg_bin], val- scaled_f[reg_bin] )
        results_subtracted['stat+syst']['result'][reg_bin] = val-scaled_f[ reg_bin ] 
    if not single_eta_bin :
        for r1 in regions :
            for etamin, etamax in eta_bins[r1] :
                etabin = '%.2f-%.2f' %( etamin, etamax) 
                print 'Reg = %s, pt %s-%s, N Pix CR* ff = %s, N Jet Pix CR * ff = %s, pred = %s' %( etabin, ptmin, ptmax, val, scaled_f[(etabin, ptmin, ptmax)], val- scaled_f[(etabin, ptmin, ptmax)] )
                results_subtracted['stat+syst']['result'][(etabin, ptmin, ptmax)] = val-scaled_f[ (etabin,ptmin,ptmax) ] 


    if outputDir is not None :
        if not os.path.isdir( outputDir ) :
            os.makedirs( outputDir )

        file_raw = open( outputDir + '/electron_fake_results%s__noJetFakeSubtraction.pickle' %el_selection, 'w' )
        pickle.dump( results_nom, file_raw )
        file_raw.close()

        file_sub = open( outputDir + '/electron_fake_results%s.pickle' %el_selection, 'w' )
        pickle.dump( results_subtracted, file_sub )
        file_sub.close()
def MakeSingleEleBkgEstimate(base_dir_ele, base_dir_jet, file_bin_map, file_bin_map_syst, outputDir=None, el_selection='el', namePostfix='') :

    el_acc = ['elw', 'elwzcr']
    if el_selection not in el_acc :
        print 'Input region not recognized, must be %s' %(','.join(el_acc) )
        return

    samplesLLG.deactivate_all_samples()
    samplesLLG.activate_sample('Data')

    eta_bins = {'EB': [0.0, 0.1, 0.5, 1.0, 1.44 ], 'EE' : [1.57, 2.1, 2.2, 2.3, 2.4, 2.5] }
    regions = ['EB','EE']

    results_nom = get_ele_single_fakefactors( base_dir_ele, file_bin_map, regions, eta_bins, el_selection )
    results_syst = get_ele_single_fakefactors( base_dir_ele, file_bin_map_syst, regions, eta_bins, el_selection )

    file_key_lead = 'results__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %el_selection
    file_key_lead_syst = 'results__syst__%sinvpixlead__(EB|EE)__pt_(\d+)-(\d+|max).pickle' %el_selection
    #jet_dirs_key = 'JetFakeTemplateFitPlotsCorr(\d+)-(\d+)-(\d+)AsymIso'
    jet_dirs_key = 'JetSinglePhotonFakeNomIso'

    jet_dir_key_map = get_mapped_directory( base_dir_jet, jet_dirs_key )

    jet_files_lead      = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead      )
    jet_files_lead_syst = get_dirs_and_files( base_dir_jet, jet_dirs_key, file_key_lead_syst )

    print 'jet_files_lead'
    print jet_files_lead

    pt_bins_jetfile = [str(x) for x in _ptbins[:-1]]
    pt_bins_jetfile.append( 'max')
    pred_lead = get_jet_single_fake_results( jet_files_lead, jet_files_lead_syst, regions, pt_bins_jetfile,  jet_dir_key_map, base_dir_jet ) 

    # get fake factors and binning from file
    rfile_coarse = ROOT.TFile.Open( '%s/ElectronFakeFitsRatioCoarseEta/results.root' %base_dir_ele )
    rhist_coarse = rfile_coarse.Get( 'ff') 

    scaled_f = {}
    for r1 in regions :

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]) :
            ptmax = pt_bins_jetfile[idx+1]

            ff_lead = -1
            bin_eb = rhist_coarse.GetYaxis().FindBin( 1.0 )
            bin_ee = rhist_coarse.GetYaxis().FindBin( 2.0 )
            bin_pt = rhist_coarse.GetXaxis().FindBin( int(ptmin ) )
            if r1 == 'EB' :
                ff_lead = rhist_coarse.GetBinContent( bin_pt, bin_eb )
                print 'ptmin = %s, ptmax = %s, histptmin = %d, histptmax = %d'  %( ptmin, ptmax, rhist_coarse.GetXaxis().GetBinLowEdge( bin_pt ), rhist_coarse.GetXaxis().GetBinUpEdge( bin_pt ) )
                print 'reg = EB, histetamin = %f, histetamax = %f'  %( rhist_coarse.GetYaxis().GetBinLowEdge( bin_eb ), rhist_coarse.GetYaxis().GetBinUpEdge( bin_eb ) )
            if r1 == 'EE' :
                ff_lead = rhist_coarse.GetBinContent( bin_pt, bin_ee )
                print 'reg = EE, histetamin = %f, histetamax = %f'  %(  rhist_coarse.GetYaxis().GetBinLowEdge( bin_ee ), rhist_coarse.GetYaxis().GetBinUpEdge( bin_ee ) )

            scaled_f[ (r1,ptmin,ptmax) ] = pred_lead['stat+syst']['f'][( r1,ptmin,ptmax)]*ff_lead

    for r1 in regions :

        for idx, ptmin in enumerate(pt_bins_jetfile[:-1]) :
            ptmax = pt_bins_jetfile[idx+1]
            print '%s, pt %s-%s' %( r1, ptmin, ptmax)
    
            print 'Pred f = %s, scaled f = %s' %( pred_lead['stat+syst']['f'][(r1,ptmin,ptmax)], scaled_f[(r1,ptmin,ptmax)] )


    results_subtracted = {}
    results_subtracted['stat+syst'] = {}
    results_subtracted['stat+syst']['result'] = {}
    for (r1, ptmin, ptmax), val in results_nom['stat+syst']['result'].iteritems() :
        print 'Reg = %s, pt %s-%s, N Pix CR* ff = %s, N Jet Pix CR * ff = %s, pred = %s' %( r1, ptmin, ptmax, val, scaled_f[(r1, ptmin, ptmax)], val- scaled_f[(r1, ptmin, ptmax)] )
        results_subtracted['stat+syst']['result'][(r1, ptmin, ptmax)] = val-scaled_f[ (r1,ptmin,ptmax) ] 


    if outputDir is not None :
        if not os.path.isdir( outputDir ) :
            os.makedirs( outputDir )

        file_raw = open( outputDir + '/electron_fake_results%s__noJetFakeSubtraction.pickle' %namePostfix, 'w' )
        pickle.dump( results_nom, file_raw )
        file_raw.close()

        file_sub = open( outputDir + '/electron_fake_results%s.pickle' %namePostfix, 'w' )
        pickle.dump( results_subtracted, file_sub )
        file_sub.close()