def create_fit_data( path, variables, fit_variables, mc_samples, COMEnergies, channels ):
    '''
        Creates the fit data in path + fit_check_data.txt in the JSON format.
        The resulting dictionary is of the form
        {centre-of-mass-energy: {
            channel : {
                variable : {
                    variable_bin : { 
                        fit_variable : {
                            templates : { sample : []},
                            initial_values : { sample : []}
                                }
                            }
                        }
                    }
                }
        }
    '''
    # first step is to read the data
    raw_data = {}
    # the folder structure is
    # path/fit_variable/centre-of-mass-energy/variable/fit_results/central/*
    for fit_variable in fit_variables:
        raw_data[fit_variable] = {}
        for COMEnergy in COMEnergies:
            raw_data[fit_variable][COMEnergy] = {}
            for variable in variables:
                raw_data[fit_variable][COMEnergy][variable] = {}
                for channel in channels:
                    raw_data[fit_variable][COMEnergy][variable][channel] = {}
                    data_path = path + '/' + fit_variable + '/' + str( COMEnergy ) + 'TeV'
                    templates = read_fit_templates( data_path, variable,
                                                   channel = channel )
                    initial_values = read_fit_input( data_path,
                                                    variable,
                                                    channel = channel )
                    raw_data[fit_variable][COMEnergy][variable][channel]['templates'] = templates
                    raw_data[fit_variable][COMEnergy][variable][channel]['initial_values'] = initial_values
    # put it into the new structure
    fit_data = {}
    for COMEnergy in COMEnergies:
        fit_data[COMEnergy] = {}
        for channel in channels:
            fit_data[COMEnergy][channel] = {}
            for variable in variables:
                fit_data[COMEnergy][channel][variable] = {}
                for v_bin in variable_bins_ROOT[variable]:
                    fit_data[COMEnergy][channel][variable][v_bin] = {}
                    for fit_variable in fit_variables:
                        fit_data[COMEnergy][channel][variable][v_bin][fit_variable] = {}
                        fit_data[COMEnergy][channel][variable][v_bin][fit_variable]['templates'] = raw_data[fit_variable][COMEnergy][variable][channel]['templates']
                        fit_data[COMEnergy][channel][variable][v_bin][fit_variable]['initial_values'] = raw_data[fit_variable][COMEnergy][variable][channel]['initial_values']
                    
    write_data_to_JSON( fit_data, path + '/fit_check_data.txt', indent = False )
def makeClosureTestTable():
    fitVariable = "absolute_eta_M3_angle_bl"
    for channel in channels:
        print "CHANNEL :", channel
        for variable in variables:
            print "--->", variable
            # Read fit results
            dir = "data/" + fitVariable + "/" + com + "/"
            if closureTest:
                dir = "data/closure_test/simple/" + fitVariable + "/" + com + "/"
            fit_results = read_fit_results(dir, variable, "central", channel, "patType1CorrectedPFMet")
            # Read initial values
            initial_values = read_fit_input(dir, variable, "central", channel, "patType1CorrectedPFMet")

            #             for whichBin in range (0,len(bin_edges[variable])-1):
            for whichBin in range(0, 1):
                for process in processes:
                    scale = closure_tests["simple"][process]

                    line = "%s " % (samples_latex[process])
                    line += "& %.0f " % initial_values[process][whichBin][0]
                    line += "& %.0f " % (initial_values[process][whichBin][0] * scale)
                    line += "& %.0f \pm %.0f " % (fit_results[process][whichBin][0], fit_results[process][whichBin][1])
                    line += "\\\\"
                    print line
                    #                     print process
                    #                     print scale
                    #                     print initial_values[process][whichBin][0]
                    #                     print initial_values[process][whichBin][0]*scale
                    #                     print fit_results[process][whichBin][0],'+/-',fit_results[process][whichBin][1]

                    pass
                pass
            print "\n"
            pass
        pass
    pass
     path = path_to_JSON + fit_variable + '/' + str( come ) + 'TeV/'
     fit_results_electron[fit_variable] = read_fit_results( path,
                                             variable,
                                             category,
                                             'electron',
                                              met_type )
     fit_results_muon[fit_variable] = read_fit_results( path,
                                             variable,
                                             category,
                                             'muon',
                                              met_type )
 # it doesn't matter which one to use, all of them are identical
 # so lets use the 2011 and 2012 default, |eta|
 initial_values_electron = read_fit_input( path_to_JSON + 'absolute_eta' + '/' + str( come ) + 'TeV/',
                                          variable,
                                          category,
                                          'electron',
                                          met_type )
 initial_values_muon = read_fit_input( path_to_JSON + 'absolute_eta' + '/' + str( come ) + 'TeV/',
                                          variable,
                                          category,
                                          'muon',
                                          met_type )
 
 if not 'closure' in path_to_JSON:
     fit_results_electron['before'] =  read_fit_results( 'data_single_var_fit/8TeV/',
                                             variable,
                                             category,
                                             'electron',
                                              met_type )
     fit_results_muon['before'] = read_fit_results( 'data_single_var_fit/8TeV/',
        "absolute_eta_M_bl_angle_bl",
        "absolute_eta_M3_angle_bl",
        "absolute_eta_M_bl_M3",
        "absolute_eta_M_bl_M3_angle_bl",
    ]

    fit_results_electron = {}
    fit_results_muon = {}
    initial_values_electron = {}
    initial_values_muon = {}
    for fit_var_input in fit_var_inputs:
        path = path_to_JSON + fit_var_input + "/" + str(come) + "TeV/"
        fit_results_electron[fit_var_input] = read_fit_results(path, variable, category, "electron", met_type)
        fit_results_muon[fit_var_input] = read_fit_results(path, variable, category, "muon", met_type)
    # it doesn't matter which one to use, all of them are identical
    # so lets use the 2011 and 2012 default, |eta|
    initial_values_electron = read_fit_input(
        path_to_JSON + "absolute_eta" + "/" + str(come) + "TeV/", variable, category, "electron", met_type
    )
    initial_values_muon = read_fit_input(
        path_to_JSON + "absolute_eta" + "/" + str(come) + "TeV/", variable, category, "muon", met_type
    )

    if not "closure" in path_to_JSON:
        fit_results_electron["before"] = read_fit_results(
            "data_single_var_fit/8TeV/", variable, category, "electron", met_type
        )
        fit_results_muon["before"] = read_fit_results("data_single_var_fit/8TeV/", variable, category, "muon", met_type)
    plot_fit_results(fit_results_electron, initial_values_electron, "electron")
    plot_fit_results(fit_results_muon, initial_values_muon, "muon")
     
     fit_results_ = read_fit_results( 'data/closure_test/'+test+'/absolute_eta_M3_angle_bl/8TeV/',
                                 variable,
                                 'central',
                                 'electron',
                                  'patType1CorrectedPFMet' )
 
     fit_templates_ = read_fit_templates( 'data/closure_test/'+test+'/absolute_eta_M3_angle_bl/8TeV/',
                                     variable,
                                     'central',
                                     'electron',
                                      'patType1CorrectedPFMet' )
     
     initial_values_ = read_fit_input( 'data/closure_test/'+test+'/absolute_eta_M3_angle_bl/8TeV/',
                                     variable,
                                     'central',
                                     'electron',
                                      'patType1CorrectedPFMet' )
 
     for whichBin in range (0,len(fit_results_['TTJet'])):
         for fitVariable in fit_variable_properties:
             fitTemplates = fit_templates_[fitVariable]
             
             histograms = {}
             for template in fitTemplates:
                 nBins = len(fitTemplates[template][whichBin])
                 
                 histograms[template] = Hist(nBins,fit_variable_properties[fitVariable]['min'],fit_variable_properties[fitVariable]['max'],name=template)
                 
                 for bin in range(0,nBins):
                     histograms[template].SetBinContent( bin, fitTemplates[template][whichBin][bin] )