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
     
     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] )
Exemple #4
0
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,
        )
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:
                    nBins = len(fitTemplates[template][whichBin])

                    histograms[template] = Hist(
                        nBins,
                        fit_variable_properties[fitVariable]['min'],
                        fit_variable_properties[fitVariable]['max'],
                        name=template)
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,
        )
     path = path_to_JSON + fit_variable + '/' + str( come ) + 'TeV/'
     fit_results_electron[fit_variable] = read_normalisation( path,
                                             variable,
                                             category,
                                             'electron',
                                              met_type )
     fit_results_muon[fit_variable] = read_normalisation( 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_initial_normalisation( path_to_JSON + 'absolute_eta' + '/' + str( come ) + 'TeV/',
                                          variable,
                                          category,
                                          'electron',
                                          met_type )
 initial_values_muon = read_initial_normalisation( 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_normalisation( 'data_single_var_fit/8TeV/',
                                             variable,
                                             category,
                                             'electron',
                                              met_type )
     fit_results_muon['before'] = read_normalisation( 'data_single_var_fit/8TeV/',
    fit_variables = fit_var_inputs

    fit_results_electron = {}
    fit_results_muon = {}
    initial_values_electron = {}
    initial_values_muon = {}
    for fit_variable in fit_variables:
        path = path_to_JSON + fit_variable + '/' + str(come) + 'TeV/'
        fit_results_electron[fit_variable] = read_normalisation(
            path, variable, category, 'electron', met_type)
        fit_results_muon[fit_variable] = read_normalisation(
            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_initial_normalisation(
        path_to_JSON + 'absolute_eta' + '/' + str(come) + 'TeV/', variable,
        category, 'electron', met_type)
    initial_values_muon = read_initial_normalisation(
        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_normalisation(
            'data_single_var_fit/8TeV/', variable, category, 'electron',
            met_type)
        fit_results_muon['before'] = read_normalisation(
            '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')
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)