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_initial_normalisation( 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 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_initial_normalisation(
                        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)
variables = [ 'MET', 'HT', 'ST', 'WPT', 'MT' ]


for test in closure_tests:
    if test != 'qcd_only' : continue;
    for variable in variables:
        
        fit_results_ = read_normalisation( '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_initial_normalisation( '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: