def main():
    global measurement_config, histogram_files
    global electron_fit_variables, muon_fit_variables, fit_variable_properties
    global b_tag_bin, category, histogram_files, variables
    global b_tag_bin_ctl
    
    title_template = 'CMS Preliminary, $\mathcal{L} = %.1f$ fb$^{-1}$  at $\sqrt{s}$ = %d TeV \n %s'
    e_title = title_template % ( measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy, 'e+jets, $\geq$ 4 jets' )
    met_type = 'patType1CorrectedPFMet'
    for variable in variables:
        variable_bins = variable_bins_ROOT[variable]
        histogram_template = get_histogram_template( variable )
        
        for fit_variable in electron_fit_variables:
            if '_bl' in fit_variable:
                b_tag_bin_ctl = '1orMoreBtag'
            else:
                b_tag_bin_ctl = '0orMoreBtag'
            save_path = 'plots/%dTeV/fit_variables/%s/%s/' % (measurement_config.centre_of_mass_energy, variable, fit_variable)
            make_folder_if_not_exists(save_path)
            make_folder_if_not_exists(save_path + 'qcd/')
            make_folder_if_not_exists(save_path + 'vjets/')
            for bin_range in variable_bins:
                params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable}
                fit_variable_distribution = histogram_template % params
                qcd_fit_variable_distribution = fit_variable_distribution.replace( 'Ref selection', 'QCDConversions' )
                qcd_fit_variable_distribution = qcd_fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl )
                histograms = get_histograms_from_files( [fit_variable_distribution, qcd_fit_variable_distribution], histogram_files )
                plot_fit_variable( histograms, fit_variable, variable, bin_range, fit_variable_distribution, qcd_fit_variable_distribution, e_title, save_path )
        compare_qcd_control_regions(variable, met_type, e_title)
        compare_vjets_btag_regions(variable, met_type, e_title)
def get_fit_inputs(template, variable, channel):

    inputs = {}
    for var_bin in variable_bins_ROOT[variable]:
        print var_bin
        histogram = template % var_bin
        histograms = get_histograms_from_files([histogram], histogram_files)
        for sample in [channel, "TTJet", "V+Jets", "SingleTop"]:
            n_bins = histograms[sample][histogram].GetNbinsX()
            error = Double(0)
            integral = histograms[sample][histogram].IntegralAndError(1, n_bins, error)
            if inputs.has_key(sample):
                inputs[sample].append((integral, error))
            else:
                inputs[sample] = [(integral, error)]

    inputs["QCD"] = []
    for data, ttjet, vjets, singletop in zip(inputs[channel], inputs["TTJet"], inputs["V+Jets"], inputs["SingleTop"]):
        qcd = ufloat(data) - ufloat(ttjet) - ufloat(vjets) - ufloat(singletop)
        inputs["QCD"].append((qcd.nominal_value, qcd.std_dev))
    print inputs
    return inputs
def compare_vjets_btag_regions( variable = 'MET', met_type = 'patType1CorrectedPFMet', title = 'Untitled'):
    ''' Compares the V+Jets template in different b-tag bins'''
    global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl
    b_tag_bin_ctl = '0orMoreBtag'
    variable_bins = variable_bins_ROOT[variable]
    histogram_template = get_histogram_template( variable )
    
    for fit_variable in electron_fit_variables:
        if '_bl' in fit_variable:
                b_tag_bin_ctl = '1orMoreBtag'
        else:
            b_tag_bin_ctl = '0orMoreBtag'
        save_path = 'plots/fit_variables/%dTeV/%s/%s/' % (measurement_config.centre_of_mass_energy, variable, fit_variable)
        make_folder_if_not_exists(save_path + '/vjets/')
        histogram_properties = Histogram_properties()
        histogram_properties.x_axis_title = fit_variable_properties[fit_variable]['x-title']
        histogram_properties.y_axis_title = fit_variable_properties[fit_variable]['y-title']
        histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace('Events', 'a.u.')
        histogram_properties.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']]
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin_ctl]
        for bin_range in variable_bins:
            params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable}
            fit_variable_distribution = histogram_template % params
            fit_variable_distribution_ctl = fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl )
            # format: histograms['data'][qcd_fit_variable_distribution]
            histograms = get_histograms_from_files( [fit_variable_distribution, fit_variable_distribution_ctl], {'V+Jets' : histogram_files['V+Jets']} )
            prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale )
            histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin_ctl + '_VJets_template_comparison'
            histograms['V+Jets'][fit_variable_distribution].Scale(1/histograms['V+Jets'][fit_variable_distribution].Integral())
            histograms['V+Jets'][fit_variable_distribution_ctl].Scale(1/histograms['V+Jets'][fit_variable_distribution_ctl].Integral())
            compare_measurements(models = {'no b-tag' : histograms['V+Jets'][fit_variable_distribution_ctl]}, 
                             measurements = {'$>=$ 2 b-tags': histograms['V+Jets'][fit_variable_distribution]}, 
                             show_measurement_errors = True, 
                             histogram_properties = histogram_properties, 
                             save_folder = save_path + '/vjets/', 
                             save_as = save_as)
    path_to_files = '/storage/TopQuarkGroup/results/histogramfiles/AN-11-265_V1/'
    lumi = 1959.75
    data = 'data'
    pfmuon = ''
    histogram_files = {
            'TTJet': path_to_files + 'TTJet_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'data' : path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon),
            'WJets': path_to_files + 'WJetsToLNu_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'ZJets': path_to_files + 'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'QCD': path_to_files + 'QCD_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
                       }

    control_region = 'QCDStudy/PFIsolation_controlRegion_0btag'
    
    histograms = get_histograms_from_files([control_region], histogram_files)
    prepare_histograms(histograms, rebin=rebin)
    

    nonQCDMC = histograms['TTJet'][control_region] + histograms['WJets'][control_region] + histograms['ZJets'][control_region] + histograms['SingleTop'][control_region]
    make_control_region_data_mc_comparision(histograms, control_region, 'PFIsolation_0btag',
                                            x_label='relative isolation', x_min=0, x_max=1.6, y_label='Events/0.1')

    make_control_region_comparison(histograms['data'][control_region],
                                   histograms['QCD'][control_region],
                                   'data',
                                   'QCD MC',
                                   'PFIsolation_0btag',
                                   x_label='relative isolation', x_min=0, x_max=1.6, y_label='Events/0.1', y_max = 0.4)
    make_control_region_comparison(histograms['data'][control_region] - nonQCDMC,
                                   histograms['QCD'][control_region],
    SingleTop_file = path_to_files + 'central/SingleTop_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    W1Jet_file = path_to_files + 'central/W1Jet_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    W2Jets_file = path_to_files + 'central/W2Jets_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    W3Jets_file = path_to_files + 'central/W3Jets_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    W4Jets_file = path_to_files + 'central/W4Jets_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    ZJets_file = path_to_files + 'central/DYJetsToLL_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    
    data_file_electron = path_to_files + 'central/ElectronHad_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    data_file_muon = path_to_files + 'central/SingleMu_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'

#    electron_QCD_file = File(path_to_files + 'QCD_data_electron.root')
#    muon_QCD_file = File(path_to_files + 'QCD_data_muon.root')
    
    histograms_of_interest = csm.get_histograms_of_interest_7TeV()
    #get histograms
    histograms = get_histograms_from_files(histograms_of_interest, {'data':data_file_electron})
    #store normalisation of all samples
    #for now OK.
    
    output_files = {}
    luminosity = 5050
    for b_tag_bin in summations.all_b_tag_bins:
        file_name = "TTbar_plus_X_analysis_%dpbinv_%s.root" %(luminosity, b_tag_bin)
        output_files[b_tag_bin] = File(file_name, "recreate")
        
    #sum samples up    
        
    #write them all up
    for sample, histogram_list  in histograms.iteritems():
        for histogram_path, histogram in histogram_list.iteritems():
            directory, histogram_name, b_tag_bin = get_histogram_info_tuple(histogram_path)
    data = 'SingleElectron'
    pfmuon = 'PFMuon_'
    histogram_files = {
            'data' : path_to_data + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon),
            'TTJet': path_to_files + 'TTJet_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'WJets': path_to_files + 'WJets_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'ZJets': path_to_files + 'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'QCD': path_to_files + 'QCD_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix)
    }

    electron_selection = 'EventCount/TTbarEplusJetsRefSelection'
    muon_selection = 'EventCount/TTbarMuPlusJetsRefSelection'

    cuts = cuts_electrons
    histograms = get_histograms_from_files([electron_selection], histogram_files)
    print '='*50
    printCutFlow(histograms, electron_selection)

    data = 'SingleMu'
    histogram_files['data'] = path_to_data + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon)
    histogram_files['QCD'] = path_to_files + 'QCD_Muon_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix)
    histograms = get_histograms_from_files([muon_selection], histogram_files)

    cuts = cuts_muons
    print '='*50
    printCutFlow(histograms, muon_selection)



    histogram_files = {
                       'TTJet': path_to_files + 'TTJet_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'data' : path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon),
            'WJets': path_to_files + 'WJetsToLNu_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'ZJets': path_to_files + 'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
            'QCD': '/storage/TopQuarkGroup/results/histogramfiles/AN-11-265_V2/QCD_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(1959.75), ''),
            'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
                       }

    control_region_1 = 'topReconstruction/backgroundShape/mttbar_3jets_conversions_withMETAndAsymJets_0btag'
    control_region_2 = 'topReconstruction/backgroundShape/mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag'
    control_region_3 = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets_0btag'
    control_region_4 = 'topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets_0btag'
    
    histograms_to_read = [control_region_1, control_region_2, control_region_3, control_region_4]
    histograms = get_histograms_from_files(histograms_to_read, histogram_files)
    prepare_histograms(histograms, rebin=rebin)
    for _,histogram in histograms['QCD'].iteritems():
        histogram.Scale(5028./1959.75)
    
    make_control_region_data_mc_comparision(histograms, control_region_1, 'mttbar_3jets_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_2, 'mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_3, 'mttbar_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_4, 'mttbar_antiIsolated_withMETAndAsymJets_0btag')

    make_control_region_comparison(histograms['data'][control_region_1],
                                   histograms['data'][control_region_2],
                                   'conversions',
                                   'non-isolated electrons',
                                   'mttbar_3jets_withMETAndAsymJets_0btag')
    make_control_region_comparison(histograms['data'][control_region_3],
def do_shape_check(
    channel,
    control_region_1,
    control_region_2,
    variable,
    normalisation,
    title,
    x_title,
    y_title,
    x_limits,
    y_limits,
    name_region_1="conversions",
    name_region_2="non-isolated electrons",
    name_region_3="fit results",
    rebin=1,
):
    global b_tag_bin
    # QCD shape comparison
    if channel == "electron":
        histograms = get_histograms_from_files([control_region_1, control_region_2], histogram_files)

        region_1 = (
            histograms[channel][control_region_1].Clone()
            - histograms["TTJet"][control_region_1].Clone()
            - histograms["V+Jets"][control_region_1].Clone()
            - histograms["SingleTop"][control_region_1].Clone()
        )
        region_2 = (
            histograms[channel][control_region_2].Clone()
            - histograms["TTJet"][control_region_2].Clone()
            - histograms["V+Jets"][control_region_2].Clone()
            - histograms["SingleTop"][control_region_2].Clone()
        )

        region_1.Rebin(rebin)
        region_2.Rebin(rebin)

        histogram_properties = Histogram_properties()
        histogram_properties.name = "QCD_control_region_comparison_" + channel + "_" + variable + "_" + b_tag_bin
        histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = "arbitrary units/(0.1)"
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = "upper right"
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_2,
            histogram_properties=histogram_properties,
            save_folder=output_folder,
        )

        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1], histogram_files)

        region_1_tmp = (
            histograms[channel][control_region_1].Clone()
            - histograms["TTJet"][control_region_1].Clone()
            - histograms["V+Jets"][control_region_1].Clone()
            - histograms["SingleTop"][control_region_1].Clone()
        )
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

        fit_results_QCD = normalisation[variable]["QCD"]
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges[variable])

        histogram_properties = Histogram_properties()
        histogram_properties.name = (
            "QCD_control_region_comparison_" + channel + "_" + variable + "_fits_with_conversions_" + b_tag_bin
        )
        histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = "arbitrary units/(0.1)"
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = "upper right"
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_3,
            histogram_properties=histogram_properties,
            save_folder=output_folder,
        )

    histograms = get_histograms_from_files([control_region_2], histogram_files)

    region_1_tmp = (
        histograms[channel][control_region_2].Clone()
        - histograms["TTJet"][control_region_2].Clone()
        - histograms["V+Jets"][control_region_2].Clone()
        - histograms["SingleTop"][control_region_2].Clone()
    )
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

    fit_results_QCD = normalisation[variable]["QCD"]
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges[variable])

    histogram_properties = Histogram_properties()
    histogram_properties.name = (
        "QCD_control_region_comparison_" + channel + "_" + variable + "_fits_with_noniso_" + b_tag_bin
    )
    histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = "arbitrary units/(0.1)"
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = "upper right"
    make_control_region_comparison(
        region_1,
        region_2,
        name_region_1=name_region_2,
        name_region_2=name_region_3,
        histogram_properties=histogram_properties,
        save_folder=output_folder,
    )
def compare_qcd_control_regions( variable = 'MET', met_type = 'patType1CorrectedPFMet', title = 'Untitled'):
    ''' Compares the templates from the control regions in different bins
     of the current variable'''
    global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl
    variable_bins = variable_bins_ROOT[variable]
    histogram_template = get_histogram_template( variable )
    
    for fit_variable in electron_fit_variables:
        all_hists = {}
        inclusive_hist = None
        if '_bl' in fit_variable:
                b_tag_bin_ctl = '1orMoreBtag'
        else:
            b_tag_bin_ctl = '0orMoreBtag'
        save_path = 'plots/fit_variables/%dTeV/%s/%s/' % (measurement_config.centre_of_mass_energy, variable, fit_variable)
        make_folder_if_not_exists(save_path + '/qcd/')
        
        max_bins = 3
        for bin_range in variable_bins[0:max_bins]:
            
            params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable}
            fit_variable_distribution = histogram_template % params
            qcd_fit_variable_distribution = fit_variable_distribution.replace( 'Ref selection', 'QCDConversions' )
            qcd_fit_variable_distribution = qcd_fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl )
            # format: histograms['data'][qcd_fit_variable_distribution]
            histograms = get_histograms_from_files( [qcd_fit_variable_distribution], histogram_files )
            prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale )

            histograms_for_cleaning = {'data':histograms['data'][qcd_fit_variable_distribution],
                               'V+Jets':histograms['V+Jets'][qcd_fit_variable_distribution],
                               'SingleTop':histograms['SingleTop'][qcd_fit_variable_distribution],
                               'TTJet':histograms['TTJet'][qcd_fit_variable_distribution]}
            qcd_from_data = clean_control_region( histograms_for_cleaning, subtract = ['TTJet', 'V+Jets', 'SingleTop'] )
            # clean
            all_hists[bin_range] = qcd_from_data
    
        # create the inclusive distributions
        inclusive_hist = deepcopy(all_hists[variable_bins[0]])
        for bin_range in variable_bins[1:max_bins]:
            inclusive_hist += all_hists[bin_range]
        for bin_range in variable_bins[0:max_bins]:
            if not all_hists[bin_range].Integral() == 0:
                all_hists[bin_range].Scale(1/all_hists[bin_range].Integral())
        # normalise all histograms
        inclusive_hist.Scale(1/inclusive_hist.Integral())
        # now compare inclusive to all bins
        histogram_properties = Histogram_properties()
        histogram_properties.x_axis_title = fit_variable_properties[fit_variable]['x-title']
        histogram_properties.y_axis_title = fit_variable_properties[fit_variable]['y-title']
        histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace('Events', 'a.u.')
        histogram_properties.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']]
#         histogram_properties.y_limits = [0, 0.5]
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin_ctl]
        histogram_properties.name = variable + '_' + fit_variable + '_' + b_tag_bin_ctl + '_QCD_template_comparison'
        measurements = {bin_range + ' GeV': histogram for bin_range, histogram in all_hists.iteritems()}
        measurements = OrderedDict(sorted(measurements.items()))
        compare_measurements(models = {'inclusive' : inclusive_hist}, 
                             measurements = measurements, 
                             show_measurement_errors = True, 
                             histogram_properties = histogram_properties, 
                             save_folder = save_path + '/qcd/', 
                             save_as = save_as)