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 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_normalisation( dir,
                                        variable,
                                        'central',
                                        channel,
                                        'patType1CorrectedPFMet' )
            # Read initial values
            initial_values = read_initial_normalisation( 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
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_normalisation(dir, variable, 'central', channel,
                                             'patType1CorrectedPFMet')
            # Read initial values
            initial_values = read_initial_normalisation(
                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
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 main(argv=None):
    '''Command line options.'''

    program_name = os.path.basename(sys.argv[0])
    program_version = "v0.1"
    program_build_date = "%s" % __updated__

    program_version_string = '%%prog %s (%s)' % (
        program_version, program_build_date)
    # program_usage = '''usage: spam two eggs''' # optional - will be
    # autogenerated by optparse
    program_longdesc = ''''''  # optional - give further explanation about what the program does
    program_license = "Copyright 2015 user_name (organization_name)                                            \
                Licensed under the Apache License 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0"

    if argv is None:
        argv = sys.argv[1:]
    # setup option parser
    parser = OptionParser(
        version=program_version_string, epilog=program_longdesc, description=program_license)
    parser.add_option(
        "-i", "--in", dest="input_path", help="set input path [default: %default]")
    parser.add_option(
        "-o", "--out", dest="output_path", help="set output path [default: %default]")
    parser.add_option("-v", "--variable", dest="variable",
                      help="set the variable to analyse (MET, HT, ST, MT, WPT) [default: %default]")
    parser.add_option("-c", "--centre-of-mass-energy", dest="com", type=int,
                      help="set the centre of mass energy in TeV for analysis. [default: %default]")
    parser.add_option('--visiblePS', dest="visiblePS", action="store_true",
                      help="Unfold to visible phase space")

    # set defaults
    parser.set_defaults(
        output_path="tables/background_subtraction/",
        input_path="data/normalisation/background_subtraction/",
        com=13,
        variable='MET',
    )

    # process options
    (opts, _) = parser.parse_args(argv)

    # MAIN BODY #
    config = XSectionConfig(opts.com)
    met_type = config.translate_options['type1']
    variable = opts.variable
    output_path = opts.output_path
    input_path = opts.input_path
    visiblePS = opts.visiblePS

    if not output_path.endswith('/'):
        output_path += '/'

    phase_space = 'FullPS'
    if visiblePS:
        phase_space = 'VisiblePS'

    path_to_JSON = '{path}/{com}TeV/{variable}/{phase_space}/'.format(
        path=input_path,
        com=opts.com,
        variable=variable,
        phase_space=phase_space,
    )
#         categories_and_prefixes = config.categories_and_prefixes
#         categories = deepcopy(categories_and_prefixes.keys())

    for channel in ['electron', 'muon']:  # , 'combined']:
        # read results
        unfolded_normalisation = read_unfolded_normalisation(
            path_to_JSON=path_to_JSON,
            category='central',
            channel=channel,
            met_type=met_type,
        )
        initial_normalisation = read_initial_normalisation(
            path_to_JSON=path_to_JSON,
            category='central',
            channel=channel,
            met_type=met_type,
        )
        mylog.debug('initial normalisation entries: ')
        mylog.debug(initial_normalisation.keys())
        mylog.debug('unfolded normalisation entries: ')
        mylog.debug(unfolded_normalisation.keys())

        print_result_table(
            config,
            channel, opts, initial_normalisation, unfolded_normalisation,
        )
            print_before_unfolding=False,
        )
        print_error_table(
            normalised_xsection_measured_unfolded,
            normalised_xsection_measured_errors,
            channel,
            toFile=True,
            print_before_unfolding=True,
        )

        if channel == "combined":
            print_typical_systematics_table(
                normalised_xsection_measured_unfolded,
                normalised_xsection_unfolded_errors,
                channel,
                toFile=True,
                print_before_unfolding=False,
            )
            print_typical_systematics_table(
                normalised_xsection_measured_unfolded,
                normalised_xsection_measured_errors,
                channel,
                toFile=True,
                print_before_unfolding=True,
            )

        if not channel == "combined" and not channel == "combinedBeforeUnfolding":
            fit_input = read_initial_normalisation(path_to_JSON, variable, "central", channel, met_type)
            fit_results = read_normalisation(path_to_JSON, variable, "central", channel, met_type)
            print_fit_results_table(fit_input, fit_results, channel, toFile=True)
     
     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:
                 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] )