def calculate_xsections(normalisation, category, channel):
    global variable, met_type, path_to_JSON
    # calculate the x-sections
    TTJet_xsection = calculate_xsection(normalisation['TTJet_measured'], luminosity, 0.15)  # L in pb1
    TTJet_xsection_unfolded = calculate_xsection(normalisation['TTJet_unfolded'], luminosity, 0.15)  # L in pb1
    MADGRAPH_xsection = calculate_xsection(normalisation['MADGRAPH'], luminosity, 0.15)  # L in pb1
    POWHEG_xsection = calculate_xsection(normalisation['POWHEG'], luminosity, 0.15)  # L in pb1
    MCATNLO_xsection = calculate_xsection(normalisation['MCATNLO'], luminosity, 0.15)  # L in pb1
    matchingdown_xsection = calculate_xsection(normalisation['matchingdown'], luminosity, 0.15)  # L in pb1
    matchingup_xsection = calculate_xsection(normalisation['matchingup'], luminosity, 0.15)  # L in pb1
    scaledown_xsection = calculate_xsection(normalisation['scaledown'], luminosity, 0.15)  # L in pb1
    scaleup_xsection = calculate_xsection(normalisation['scaleup'], luminosity, 0.15)  # L in pb1
    
    xsection_unfolded = {'TTJet_measured' : TTJet_xsection,
                         'TTJet_unfolded' : TTJet_xsection_unfolded,
                         'MADGRAPH': MADGRAPH_xsection,
                         'POWHEG': POWHEG_xsection,
                         'MCATNLO': MCATNLO_xsection,
                         #systematics
                         'matchingdown': matchingdown_xsection,
                         'matchingup': matchingup_xsection,
                         'scaledown': scaledown_xsection,
                         'scaleup': scaleup_xsection
                         }
    write_data_to_JSON(xsection_unfolded, path_to_JSON + '/' + variable + '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) + '/' + category + '/xsection_' + channel + '_' + met_type + '.txt')
def calculate_normalised_xsections(normalisation, category, channel, normalise_to_one = False):
    global variable, met_type, path_to_JSON
    TTJet_normalised_xsection = calculate_normalised_xsection(normalisation['TTJet_measured'], bin_widths[variable], normalise_to_one)
    TTJet_normalised_xsection_unfolded = calculate_normalised_xsection(normalisation['TTJet_unfolded'], bin_widths[variable], normalise_to_one)
    MADGRAPH_normalised_xsection = calculate_normalised_xsection(normalisation['MADGRAPH'], bin_widths[variable], normalise_to_one)
    POWHEG_normalised_xsection = calculate_normalised_xsection(normalisation['POWHEG'], bin_widths[variable], normalise_to_one)
    MCATNLO_normalised_xsection = calculate_normalised_xsection(normalisation['MCATNLO'], bin_widths[variable], normalise_to_one)
    matchingdown_normalised_xsection = calculate_normalised_xsection(normalisation['matchingdown'], bin_widths[variable], normalise_to_one)
    matchingup_normalised_xsection = calculate_normalised_xsection(normalisation['matchingup'], bin_widths[variable], normalise_to_one)
    scaledown_normalised_xsection = calculate_normalised_xsection(normalisation['scaledown'], bin_widths[variable], normalise_to_one)
    scaleup_normalised_xsection = calculate_normalised_xsection(normalisation['scaleup'], bin_widths[variable], normalise_to_one)
    
    normalised_xsection = {'TTJet_measured' : TTJet_normalised_xsection,
                           'TTJet_unfolded' : TTJet_normalised_xsection_unfolded,
                           'MADGRAPH': MADGRAPH_normalised_xsection,
                           'POWHEG': POWHEG_normalised_xsection,
                           'MCATNLO': MCATNLO_normalised_xsection,
                           #systematics
                           'matchingdown': matchingdown_normalised_xsection,
                           'matchingup': matchingup_normalised_xsection,
                           'scaledown': scaledown_normalised_xsection,
                           'scaleup': scaleup_normalised_xsection
                           }
    
    filename = path_to_JSON + '/' + variable + '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) + '/' + category + '/normalised_xsection_' + channel + '_' + met_type + '.txt'
    if normalise_to_one:
        filename = filename.replace('normalised_xsection', 'normalised_to_one_xsection')
    write_data_to_JSON(normalised_xsection, filename)
Exemplo n.º 3
0
def write_normalised_xsection_measurement(options, measurement, measurement_unfolded, summary = '' ):
    '''
    Writes the list of normalised measurements and normalised unfolded measurements of the form: 
    [Central Value, Lower Systemtic, Upper Systematic] to a json. Different combinations of 
    systematic uncertainty are stored as different json by appending different 'summary'
    '''
    path_to_JSON=options['path_to_JSON']
    method=options['method']
    channel=options['channel']

    output_file = '{path_to_JSON}/central/normalised_xsection_{channel}_{method}_with_errors.txt'
    output_file = output_file.format(
        path_to_JSON = path_to_JSON,
        channel = channel,
        method = method,
    )
    if not summary == '':
        output_file = output_file.replace( 'with_errors', summary + '_errors' )
    
    output = {'TTJet_measured':measurement, 'TTJet_unfolded': measurement_unfolded}
    
    write_data_to_JSON( output, output_file )

    print("Data written to : ", output_file)
    return
def save_pulls(pulls, method, channel, tau_value, output_folder):
    '''
        Saves pull distributions in JSON format
    '''
    file_template = 'Pull_data_%s_%s_%.4g.txt'
    output_file = output_folder + \
        file_template % (method, channel, tau_value)
    write_data_to_JSON(pulls, output_file)
    print('Pulls saved in file: ', output_file)
    return output_file
def save_pulls(pulls, method, channel, tau_value, output_folder):
    '''
        Saves pull distributions in JSON format
    '''
    file_template = 'Pull_data_%s_%s_%.4g.txt'
    output_file = output_folder + \
        file_template % (method, channel, tau_value)
    write_data_to_JSON(pulls, output_file)
    print('Pulls saved in file: ', output_file)
    return output_file
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():
    global config, options
    parser = OptionParser()
    parser.add_option("-p",
                      "--path",
                      dest="path",
                      default='data/fit_checks/no_merging',
                      help="set path to JSON files")
    parser.add_option('--create_fit_data',
                      dest="create_fit_data",
                      action="store_true",
                      help="create the fit data for testing.")
    parser.add_option('--refit',
                      dest="refit",
                      action="store_true",
                      help="Fit again even if the output already exists")
    parser.add_option('--test',
                      dest="test",
                      action="store_true",
                      help="Test only: run just one selected sample")
    variables = config.histogram_path_templates.keys()
    fit_variables = fit_var_inputs
    mc_samples = ['TTJet', 'SingleTop', 'QCD', 'V+Jets']
    tests = closure_tests
    channels = ['electron', 'muon']
    COMEnergies = ['7', '8']

    (options, _) = parser.parse_args()
    print 'Running from path', options.path

    if (options.create_fit_data):
        create_fit_data(options.path,
                        variables,
                        fit_variables,
                        mc_samples,
                        COMEnergies=COMEnergies,
                        channels=channels)

    output_file = options.path + '/fit_test_output.txt'
    if options.test:
        output_file = options.path + '/fit_test_output_test.txt'
    if options.refit or not os.path.isfile(output_file) or options.test:
        if os.path.isfile(options.path + '/fit_check_data.txt'):
            fit_data = read_data_from_JSON(options.path +
                                           '/fit_check_data.txt')
            results = run_tests(fit_data, COMEnergies, variables,
                                fit_variables, mc_samples, channels, tests)
            write_data_to_JSON(results, output_file)
        else:
            print 'Please run bin/prepare_data_for_fit_checks first'
            print 'Then run this script with the option --create_fit_data.'

    results = read_data_from_JSON(output_file)
    plot_results(results)
def calculate_normalised_xsections( normalisation, category, channel, normalise_to_one = False ):
    global variable, met_type, path_to_JSON, phase_space

    binWidths = None
    if phase_space == 'VisiblePS':
        binWidths = bin_widths_visiblePS
    elif phase_space == 'FullPS':
        binWidths = bin_widths

    TTJet_normalised_xsection = calculate_normalised_xsection( normalisation['TTJet_measured'], binWidths[variable], normalise_to_one )
    TTJet_withoutFakes_normalised_xsection = calculate_normalised_xsection( normalisation['TTJet_measured_withoutFakes'], binWidths[variable], normalise_to_one )
    TTJet_normalised_xsection_unfolded = calculate_normalised_xsection( normalisation['TTJet_unfolded'], binWidths[variable], normalise_to_one )

    normalised_xsection = {'TTJet_measured' : TTJet_normalised_xsection,
                           'TTJet_measured_withoutFakes' : TTJet_withoutFakes_normalised_xsection,
                           'TTJet_unfolded' : TTJet_normalised_xsection_unfolded
                           }

    if category == 'central':
        powhegPythia8_normalised_xsection = calculate_normalised_xsection( normalisation['powhegPythia8'], binWidths[variable], normalise_to_one )
        amcatnlo_normalised_xsection = calculate_normalised_xsection( normalisation['amcatnlo'], binWidths[variable], normalise_to_one )
        powhegHerwig_normalised_xsection = calculate_normalised_xsection( normalisation['powhegHerwig'], binWidths[variable], normalise_to_one )
        # amcatnloHerwig_normalised_xsection = calculate_normalised_xsection( normalisation['amcatnloHerwig'], binWidths[variable], normalise_to_one )
        madgraphMLM_normalised_xsection = calculate_normalised_xsection( normalisation['madgraphMLM'], binWidths[variable], normalise_to_one )

        scaledown_normalised_xsection = calculate_normalised_xsection( normalisation['scaledown'], binWidths[variable], normalise_to_one )
        scaleup_normalised_xsection = calculate_normalised_xsection( normalisation['scaleup'], binWidths[variable], normalise_to_one )
        massdown_normalised_xsection = calculate_normalised_xsection( normalisation['massdown'], binWidths[variable], normalise_to_one )
        massup_normalised_xsection = calculate_normalised_xsection( normalisation['massup'], binWidths[variable], normalise_to_one )

        normalised_xsection['powhegPythia8'] = powhegPythia8_normalised_xsection
        normalised_xsection['amcatnlo'] = amcatnlo_normalised_xsection
        normalised_xsection['madgraphMLM' ] = madgraphMLM_normalised_xsection
        normalised_xsection['powhegHerwig'] = powhegHerwig_normalised_xsection
        # normalised_xsection['amcatnloHerwig'] = amcatnloHerwig_normalised_xsection

        normalised_xsection['scaledown'] = scaledown_normalised_xsection
        normalised_xsection['scaleup'] = scaleup_normalised_xsection
        normalised_xsection['massdown'] = massdown_normalised_xsection
        normalised_xsection['massup'] = massup_normalised_xsection

    file_template = '{path_to_JSON}/{category}/normalised_xsection_{channel}_{method}.txt'
    filename = file_template.format(
                path_to_JSON = path_to_JSON,
                category = category,
                channel = channel,
                method = method,
                )

    if normalise_to_one:
        filename = filename.replace( 'normalised_xsection', 'normalised_to_one_xsection' )
    write_data_to_JSON( normalised_xsection, filename )
def write_fit_results( channel, category, fit_results ):
    global variable, met_type, output_path
    folder_template = '%(path)s/%(fit_vars)s/%(CoM)dTeV/%(variable)s/%(category)s/'
    inputs = {
              'path':output_path,
              'CoM': measurement_config.centre_of_mass_energy,
              'variable': variable,
              'category': category,
              'fit_vars': '_'.join( fit_variables )              
              }
    output_folder = folder_template % inputs

    write_data_to_JSON( fit_results, output_folder + 'normalisation_' + channel + '_' + met_type + '.txt' )
Exemplo n.º 10
0
def get_unfolded_normalisation(TTJet_fit_results, category, channel):
    global variable, met_type, path_to_JSON
    h_truth, h_measured, h_response = get_unfold_histogram_tuple(
        file_for_unfolding, variable, channel, met_type)
    MADGRAPH_results = hist_to_value_error_tuplelist(h_truth)
    POWHEG_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_powheg, variable, channel,
                                   met_type)[0])
    MCATNLO_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_mcatnlo, variable, channel,
                                   met_type)[0])

    matchingdown_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_matchingdown, variable, channel,
                                   met_type)[0])
    matchingup_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_matchingup, variable, channel,
                                   met_type)[0])
    scaledown_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_scaledown, variable, channel,
                                   met_type)[0])
    scaleup_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_scaleup, variable, channel,
                                   met_type)[0])

    TTJet_fit_results_unfolded = unfold_results(
        TTJet_fit_results, category, channel, h_truth, h_measured, h_response,
        'RooUnfoldSvd'
        #                                                'TSVDUnfold'
    )

    normalisation_unfolded = {
        'TTJet_measured': TTJet_fit_results,
        'TTJet_unfolded': TTJet_fit_results_unfolded,
        'MADGRAPH': MADGRAPH_results,
        #other generators
        'POWHEG': POWHEG_results,
        'MCATNLO': MCATNLO_results,
        #systematics
        'matchingdown': matchingdown_results,
        'matchingup': matchingup_results,
        'scaledown': scaledown_results,
        'scaleup': scaleup_results
    }
    write_data_to_JSON(
        normalisation_unfolded, path_to_JSON + '/' + variable +
        '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) +
        '/' + category + '/normalisation_' + channel + '_' + met_type + '.txt')

    return normalisation_unfolded
    def save(self, output_path):
        if not self.have_normalisation:
            self.calculate_normalisation()

        folder_template = '{path}/normalisation/{method}/{CoM}TeV/{variable}/'
        folder_template += '{phase_space}/{category}/'
        inputs = {
            'path': output_path,
            'CoM': self.config.centre_of_mass_energy,
            'variable': self.variable,
            'category': self.category,
            'method': self.method_string(),
            'phase_space': self.phase_space,
        }
        output_folder = folder_template.format(**inputs)

        file_template = '{type}_{channel}_{met_type}.txt'
        inputs = {
            'channel': self.channel,
            'met_type': self.met_type,
        }
        write_data_to_JSON(self.normalisation,
                           output_folder + file_template.format(type='normalisation', **inputs))
        write_data_to_JSON(self.initial_normalisation,
                           output_folder + file_template.format(type='initial_normalisation', **inputs))
        write_data_to_JSON(self.templates,
                           output_folder + file_template.format(type='templates', **inputs))
        write_data_to_JSON(self.auxiliary_info,
                           output_folder + file_template.format(type='auxiliary_info', **inputs))

        return output_folder
Exemplo n.º 12
0
def calculate_normalised_xsections(normalisation,
                                   category,
                                   channel,
                                   normalise_to_one=False):
    global variable, met_type, path_to_JSON
    TTJet_normalised_xsection = calculate_normalised_xsection(
        normalisation['TTJet_measured'], bin_widths[variable],
        normalise_to_one)
    TTJet_normalised_xsection_unfolded = calculate_normalised_xsection(
        normalisation['TTJet_unfolded'], bin_widths[variable],
        normalise_to_one)
    MADGRAPH_normalised_xsection = calculate_normalised_xsection(
        normalisation['MADGRAPH'], bin_widths[variable], normalise_to_one)
    POWHEG_normalised_xsection = calculate_normalised_xsection(
        normalisation['POWHEG'], bin_widths[variable], normalise_to_one)
    MCATNLO_normalised_xsection = calculate_normalised_xsection(
        normalisation['MCATNLO'], bin_widths[variable], normalise_to_one)
    matchingdown_normalised_xsection = calculate_normalised_xsection(
        normalisation['matchingdown'], bin_widths[variable], normalise_to_one)
    matchingup_normalised_xsection = calculate_normalised_xsection(
        normalisation['matchingup'], bin_widths[variable], normalise_to_one)
    scaledown_normalised_xsection = calculate_normalised_xsection(
        normalisation['scaledown'], bin_widths[variable], normalise_to_one)
    scaleup_normalised_xsection = calculate_normalised_xsection(
        normalisation['scaleup'], bin_widths[variable], normalise_to_one)

    normalised_xsection = {
        'TTJet_measured': TTJet_normalised_xsection,
        'TTJet_unfolded': TTJet_normalised_xsection_unfolded,
        'MADGRAPH': MADGRAPH_normalised_xsection,
        'POWHEG': POWHEG_normalised_xsection,
        'MCATNLO': MCATNLO_normalised_xsection,
        #systematics
        'matchingdown': matchingdown_normalised_xsection,
        'matchingup': matchingup_normalised_xsection,
        'scaledown': scaledown_normalised_xsection,
        'scaleup': scaleup_normalised_xsection
    }

    filename = path_to_JSON + '/' + variable + '/xsection_measurement_results' + '/kv' + str(
        unfoldCfg.SVD_k_value
    ) + '/' + category + '/normalised_xsection_' + channel + '_' + met_type + '.txt'
    if normalise_to_one:
        filename = filename.replace('normalised_xsection',
                                    'normalised_to_one_xsection')
    write_data_to_JSON(normalised_xsection, filename)
def write_measurement(options, measurement, norm_method):
    '''
    Write the config
    '''
    base_path = 'config/measurements/{norm_method}/{energy}TeV/{channel}/{variable}/{phase_space}/'
    path = base_path + '{category}.json'

    path = path.format(
        norm_method = norm_method,
        energy = options['com'],
        channel = options['channel'],
        variable = options['variable'],
        phase_space = options['ps'],
        category = options['category'],
    )
    write_data_to_JSON(measurement, path, indent = True)
    return
def write_measurement(options, measurement, norm_method):
    '''
    Write the config
    '''
    base_path = 'config/measurements/{norm_method}/{energy}TeV/{channel}/{variable}/{phase_space}/'
    path = base_path + '{category}.json'

    path = path.format(
        norm_method=norm_method,
        energy=options['com'],
        channel=options['channel'],
        variable=options['variable'],
        phase_space=options['ps'],
        category=options['category'],
    )
    write_data_to_JSON(measurement, path, indent=True)
    return
def calculate_xsections( normalisation, category, channel ):
    global variable, met_type, path_to_JSON
    # calculate the x-sections
    branching_ratio = 0.15
    if channel == 'combined':
        branching_ratio = branching_ratio * 2
    TTJet_xsection = calculate_xsection( normalisation['TTJet_measured'], luminosity, branching_ratio )  # L in pb1
    TTJet_withoutFakes_xsection = calculate_xsection( normalisation['TTJet_measured_withoutFakes'], luminosity, branching_ratio )  # L in pb1
    TTJet_xsection_unfolded = calculate_xsection( normalisation['TTJet_unfolded'], luminosity, branching_ratio )  # L in pb1

    xsection_unfolded = {'TTJet_measured' : TTJet_xsection,
                         'TTJet_measured_withoutFakes' : TTJet_withoutFakes_xsection,
                         'TTJet_unfolded' : TTJet_xsection_unfolded,
                         }

    if category == 'central':
        powhegPythia8_xsection = calculate_xsection( normalisation['powhegPythia8'], luminosity, branching_ratio )  # L in pb1
        amcatnlo_xsection = calculate_xsection( normalisation['amcatnlo'], luminosity, branching_ratio )  # L in pb1
        powhegHerwig_xsection = calculate_xsection( normalisation['powhegHerwig'], luminosity, branching_ratio )  # L in pb1
        # amcatnloHerwig_xsection = calculate_xsection( normalisation['amcatnloHerwig'], luminosity, branching_ratio )  # L in pb1
        madgraphMLM_xsection = calculate_xsection( normalisation['madgraphMLM'], luminosity, branching_ratio )

        scaledown_xsection = calculate_xsection( normalisation['scaledown'], luminosity, branching_ratio )  # L in pb1
        scaleup_xsection = calculate_xsection( normalisation['scaleup'], luminosity, branching_ratio )  # L in pb1
        massdown_xsection = calculate_xsection( normalisation['massdown'], luminosity, branching_ratio )  # L in pb1
        massup_xsection = calculate_xsection( normalisation['massup'], luminosity, branching_ratio )  # L in pb1

        xsection_unfolded['powhegPythia8'] =  powhegPythia8_xsection
        xsection_unfolded['amcatnlo'] =  amcatnlo_xsection
        xsection_unfolded['madgraphMLM'] =  madgraphMLM_xsection
        xsection_unfolded['powhegHerwig'] =  powhegHerwig_xsection
        # xsection_unfolded['amcatnloHerwig'] =  amcatnloHerwig_xsection

        xsection_unfolded['scaledown'] =  scaledown_xsection
        xsection_unfolded['scaleup'] =  scaleup_xsection
        xsection_unfolded['massdown'] =  massdown_xsection
        xsection_unfolded['massup'] =  massup_xsection
    file_template = '{path_to_JSON}/{category}/xsection_{channel}_{method}.txt'
    filename = file_template.format(
                path_to_JSON = path_to_JSON,
                category = category,
                channel = channel,
                method = method,
                )

    write_data_to_JSON( xsection_unfolded, filename )
def main():
    global config, options
    parser = OptionParser()
    parser.add_option( "-p", "--path", dest = "path", default = 'data/fit_checks/no_merging',
                  help = "set path to JSON files" )
    parser.add_option( '--create_fit_data', dest = "create_fit_data", action = "store_true",
                      help = "create the fit data for testing." )
    parser.add_option( '--refit', dest = "refit", action = "store_true",
                      help = "Fit again even if the output already exists" )
    parser.add_option( '--test', dest = "test", action = "store_true",
                      help = "Test only: run just one selected sample" )
    variables = config.histogram_path_templates.keys()
    fit_variables = fit_var_inputs
    mc_samples = ['TTJet', 'SingleTop', 'QCD', 'V+Jets']
    tests = closure_tests
    channels = ['electron', 'muon']
    COMEnergies = ['7', '8']
    
    ( options, _ ) = parser.parse_args()
    print 'Running from path', options.path 
    
    if ( options.create_fit_data ):
        create_fit_data( options.path, variables, fit_variables, mc_samples,
                        COMEnergies = COMEnergies, channels = channels )
     
    output_file = options.path + '/fit_test_output.txt'
    if options.test:
        output_file = options.path + '/fit_test_output_test.txt'
    if options.refit or not os.path.isfile( output_file ) or options.test:
        if os.path.isfile( options.path + '/fit_check_data.txt' ):
            fit_data = read_data_from_JSON( options.path + '/fit_check_data.txt' )
            results = run_tests( fit_data,
                                COMEnergies,
                                variables,
                                fit_variables,
                                mc_samples,
                                channels,
                                tests )
            write_data_to_JSON(results, output_file )
        else:
            print 'Please run bin/prepare_data_for_fit_checks first'
            print 'Then run this script with the option --create_fit_data.'
     
    results = read_data_from_JSON(output_file)
    plot_results( results )
    def save(self, output_path):
        if not self.have_normalisation:
            self.calculate_normalisation()

        file_template = '{type}_{channel}.txt'
        folder_template = '{path}/normalisation/{method}/{CoM}TeV/{variable}/{phase_space}/{category}/'
        output_folder = folder_template.format(
            path = output_path,
            CoM = self.config.centre_of_mass_energy,
            variable = self.variable,
            category = self.category,
            method = 'background_subtraction',
            phase_space = self.phase_space,
        )

        write_data_to_JSON(
            self.normalisation,
            output_folder + file_template.format(type='normalisation', channel=self.channel)
        )
        write_data_to_JSON(
            self.initial_normalisation, 
            output_folder + file_template.format(type='initial_normalisation', channel=self.channel)
        )
        # write_data_to_JSON(
        #     self.unity_normalisation,
        #     output_folder + file_template.format(type='unity_normalisation', channel=self.channel)
        # )
        write_data_to_JSON(
            self.auxiliary_info,
            output_folder + file_template.format(type='auxiliary_info', channel=self.channel)
        )
        return output_folder
    def save(self, output_path):
        if not self.have_normalisation:
            self.calculate_normalisation()

        file_template = '{type}_{channel}.txt'
        folder_template = '{path}/normalisation/{method}/{CoM}TeV/{variable}/{phase_space}/{category}/'
        output_folder = folder_template.format(
            path=output_path,
            CoM=self.config.centre_of_mass_energy,
            variable=self.variable,
            category=self.category,
            method='background_subtraction',
            phase_space=self.phase_space,
        )

        write_data_to_JSON(
            self.normalisation, output_folder +
            file_template.format(type='normalisation', channel=self.channel))
        write_data_to_JSON(
            self.initial_normalisation,
            output_folder + file_template.format(type='initial_normalisation',
                                                 channel=self.channel))
        # write_data_to_JSON(
        #     self.unity_normalisation,
        #     output_folder + file_template.format(type='unity_normalisation', channel=self.channel)
        # )
        write_data_to_JSON(
            self.auxiliary_info, output_folder +
            file_template.format(type='auxiliary_info', channel=self.channel))
        return output_folder
Exemplo n.º 19
0
def calculate_xsections(normalisation, category, channel):
    global variable, met_type, path_to_JSON
    # calculate the x-sections
    TTJet_xsection = calculate_xsection(normalisation['TTJet_measured'],
                                        luminosity, 0.15)  # L in pb1
    TTJet_xsection_unfolded = calculate_xsection(
        normalisation['TTJet_unfolded'], luminosity, 0.15)  # L in pb1
    MADGRAPH_xsection = calculate_xsection(normalisation['MADGRAPH'],
                                           luminosity, 0.15)  # L in pb1
    POWHEG_xsection = calculate_xsection(normalisation['POWHEG'], luminosity,
                                         0.15)  # L in pb1
    MCATNLO_xsection = calculate_xsection(normalisation['MCATNLO'], luminosity,
                                          0.15)  # L in pb1
    matchingdown_xsection = calculate_xsection(normalisation['matchingdown'],
                                               luminosity, 0.15)  # L in pb1
    matchingup_xsection = calculate_xsection(normalisation['matchingup'],
                                             luminosity, 0.15)  # L in pb1
    scaledown_xsection = calculate_xsection(normalisation['scaledown'],
                                            luminosity, 0.15)  # L in pb1
    scaleup_xsection = calculate_xsection(normalisation['scaleup'], luminosity,
                                          0.15)  # L in pb1

    xsection_unfolded = {
        'TTJet_measured': TTJet_xsection,
        'TTJet_unfolded': TTJet_xsection_unfolded,
        'MADGRAPH': MADGRAPH_xsection,
        'POWHEG': POWHEG_xsection,
        'MCATNLO': MCATNLO_xsection,
        #systematics
        'matchingdown': matchingdown_xsection,
        'matchingup': matchingup_xsection,
        'scaledown': scaledown_xsection,
        'scaleup': scaleup_xsection
    }
    write_data_to_JSON(
        xsection_unfolded, path_to_JSON + '/' + variable +
        '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) +
        '/' + category + '/xsection_' + channel + '_' + met_type + '.txt')
def get_unfolded_normalisation(TTJet_fit_results, category, channel):
    global variable, met_type, path_to_JSON
    h_truth, h_measured, h_response = get_unfold_histogram_tuple(file_for_unfolding, variable, channel, met_type)
    MADGRAPH_results = hist_to_value_error_tuplelist(h_truth)
    POWHEG_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_powheg, variable, channel, met_type)[0])
    MCATNLO_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_mcatnlo, variable, channel, met_type)[0])
    
    matchingdown_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_matchingdown, variable, channel, met_type)[0])
    matchingup_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_matchingup, variable, channel, met_type)[0])
    scaledown_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_scaledown, variable, channel, met_type)[0])
    scaleup_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_scaleup, variable, channel, met_type)[0])
    
    TTJet_fit_results_unfolded = unfold_results(TTJet_fit_results,
                                                category,
                                                channel,
                                                h_truth,
                                                h_measured,
                                                h_response,
                                                'RooUnfoldSvd'
#                                                'TSVDUnfold'
                                                )
        
    normalisation_unfolded = {
                              'TTJet_measured' : TTJet_fit_results,
                              'TTJet_unfolded' : TTJet_fit_results_unfolded,
                              'MADGRAPH': MADGRAPH_results,
                              #other generators
                              'POWHEG': POWHEG_results,
                              'MCATNLO': MCATNLO_results,
                              #systematics
                              'matchingdown': matchingdown_results,
                              'matchingup': matchingup_results,
                              'scaledown': scaledown_results,
                              'scaleup': scaleup_results
                              }
    write_data_to_JSON(normalisation_unfolded, path_to_JSON + '/' + variable + '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) + '/' + category + '/normalisation_' + channel + '_' + met_type + '.txt')
    
    return normalisation_unfolded
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 save_pulls(pulls, test, method, channel):    
    global use_N_toy, skip_N_toy
    file_template = 'Pulls_%s_%s_%s_toy_MC_%d_to_%d.txt'
    output_file = output_folder + file_template % (test, method, channel, skip_N_toy + 1, use_N_toy + skip_N_toy)
    write_data_to_JSON(pulls, output_file)
Exemplo n.º 23
0
def save_pulls(pulls, test, method, channel):
    global use_N_toy, skip_N_toy
    file_template = 'Pulls_%s_%s_%s_toy_MC_%d_to_%d.txt'
    output_file = output_folder + file_template % (
        test, method, channel, skip_N_toy + 1, use_N_toy + skip_N_toy)
    write_data_to_JSON(pulls, output_file)
Exemplo n.º 24
0
def main():
    '''
    Step 1: Get the 2D histogram for every sample (channel and/or centre of mass energy)
    Step 2: Change the size of the first bin until it fulfils the minimal criteria
    Step 3: Check if it is true for all other histograms. If not back to step 2
    Step 4: Repeat step 2 & 3 until no mo bins can be created
    '''
    parser = ArgumentParser()
    parser.add_argument( '-v', 
        dest    = "visiblePhaseSpace", 
        action  = "store_true",
        help    = "Consider visible phase space or not" 
    )
    parser.add_argument( '-c', 
        dest    = "combined", 
        action  = "store_true",
        help    = "Combine channels" 
    )
    parser.add_argument( '-r', 
        dest    = "redo_resolution", 
        action  = "store_true",
        help    = "Recalculate the resolution plots" 
    )
    args = parser.parse_args()

    measurement_config = XSectionConfig(13)

    # Initialise binning parameters
    bin_choices = {}

    # Min Purity and Stability
    p_min = 0.6
    s_min = 0.6
    # 0.5 for MET

    # Min events in bin for appropriate stat unc
    # error = 1/sqrt(N) [ unc=5% : (1/0.05)^2 = 400]
    n_min = 500
    n_min_lepton = 500
     
    variables = measurement_config.variables
    for variable in variables:
        global var
        var=variable
        print('--- Doing variable',variable)
        variableToUse = variable
        if 'Rap' in variable:
            variableToUse = 'abs_%s' % variable
        histogram_information = get_histograms( measurement_config, variableToUse, args )

        # Remake the resolution plots from the fine binned unfolding matrix
        if args.redo_resolution:
            rs.generate_resolution_plots(histogram_information, variable)

        # Claculate the best binning
        if variable == 'HT':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], x_min=120. )
        elif variable == 'ST':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], x_min=146. )
        elif variable == 'MET':
            best_binning, histogram_information = get_best_binning( histogram_information , 0.5, 0.5, n_min, minimum_bin_width[variable], nice_bin_width[variable] )
        elif variable == 'NJets':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], x_min=3.5 )
        elif variable == 'lepton_pt':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min_lepton, minimum_bin_width[variable], nice_bin_width[variable], x_min=26. )
        elif variable == 'abs_lepton_eta':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min_lepton, minimum_bin_width[variable], nice_bin_width[variable] )
        elif variable == 'NJets':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], is_NJet=True)
        else:
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable] )

        # Symmetric binning for lepton_eta
        if 'Rap' in variable:
            for b in list(best_binning):
                if b != 0.0:
                    best_binning.append(-1.0*b)
            best_binning.sort()

        # Make last bin smaller if huge
        # Won't change final results
        if len(best_binning) >= 4:
            lastBinWidth = best_binning[-1] - best_binning[-2]
            penultimateBinWidth = best_binning[-2] - best_binning[-3]
            if lastBinWidth / penultimateBinWidth > 5:
                newLastBinWidth = penultimateBinWidth * 5
                best_binning[-1] = best_binning[-2] + newLastBinWidth

        # Smooth bin edges
        if variable == 'abs_lepton_eta':
            best_binning = [ round(i,2) for i in best_binning ]
        elif variable != 'NJets' :
            best_binning = [ round(i) for i in best_binning ]

        bin_choices[variable] = best_binning

        # Print the best binning to screen and JSON
        print('The best binning for', variable, 'is:')
        print('bin edges =', best_binning)
        print('N_bins    =', len( best_binning ) - 1)
        print('The corresponding purities and stabilities are:')
        for info in histogram_information:
            outputInfo = {}
            outputInfo['p_i'] = info['p_i']
            outputInfo['s_i'] = info['s_i']
            outputInfo['N']   = info['N']
            outputInfo['res'] = info['res']
            outputJsonFile = 'unfolding/13TeV/binningInfo_%s_%s_FullPS.txt' % ( variable, info['channel'] )
            if args.visiblePhaseSpace:
                outputJsonFile = 'unfolding/13TeV/binningInfo_%s_%s_VisiblePS.txt' % ( variable, info['channel'] )
            write_data_to_JSON( outputInfo, outputJsonFile )
            print_latex_table(info, variable, best_binning)
        for key in outputInfo:
            print (key,outputInfo[key])
        print('-' * 120)

    # Final print of all binnings to screen
    print('=' * 120)
    print('For config/variable_binning.py')
    print('=' * 120)
    for variable in bin_choices:
        print('\''+variable+'\' : '+str(bin_choices[variable])+',')
Exemplo n.º 25
0
 def toJSON(self, json_file):
     d = self.toDict()
     write_data_to_JSON(d, json_file)
Exemplo n.º 26
0
 def toJSON(self, JSON_file):
     output = self.toDict()
     filename = JSON_file.split('/')[-1]
     directory = JSON_file.replace(filename, '')
     make_folder_if_not_exists(directory)
     write_data_to_JSON(output, JSON_file)
Exemplo n.º 27
0
def main():
    '''
    Step 1: Get the 2D histogram for every sample (channel and/or centre of mass energy)
    Step 2: Change the size of the first bin until it fulfils the minimal criteria
    Step 3: Check if it is true for all other histograms. If not back to step 2
    Step 4: Repeat step 2 & 3 until no mo bins can be created
    '''

    parser = OptionParser()
    parser.add_option( '-v', dest = "visiblePhaseSpace", action = "store_true",
                      help = "Consider visible phase space or not" )
    parser.add_option( '-c', dest = "combined", action = "store_true",
                      help = "Combine channels" )
    parser.add_option( '-r', dest = "redo_resolution", action = "store_true",
                      help = "Recalculate the resolution plots" )
    ( options, _ ) = parser.parse_args()

    measurement_config = XSectionConfig(13)

    p_min = 0.6 # 0.5 for MET
    s_min = 0.6
    # we also want the statistical error to be larger than 5%
    # this translates (error -= 1/sqrt(N)) to (1/0.05)^2 = 400
    n_min = 500
    n_min_lepton = 500
#     n_min = 200 # N = 200 -> 7.1 % stat error
     
    bin_choices = {}
    # variables = bin_edges_full.keys()
    variables = measurement_config.variables
    for variable in variables:
        global var
        var=variable
        print('--- Doing variable',variable)
        variableToUse = variable
        if 'Rap' in variable:
            variableToUse = 'abs_%s' % variable
        histogram_information = get_histograms( variableToUse, options )

        if options.redo_resolution:
            rs.generate_resolution_plots(histogram_information, variable)

        if variable == 'HT':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], x_min=100. )
        elif variable == 'ST':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], x_min=123. )
        elif variable == 'MET':
            best_binning, histogram_information = get_best_binning( histogram_information , 0.5, 0.5, n_min, minimum_bin_width[variable] )
        elif variable == 'NJets':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable], x_min=3.5 )
        elif variable == 'lepton_pt':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min_lepton, minimum_bin_width[variable], x_min=23. )
        elif variable == 'abs_lepton_eta':
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min_lepton, minimum_bin_width[variable] )
        else:
            best_binning, histogram_information = get_best_binning( histogram_information , p_min, s_min, n_min, minimum_bin_width[variable] )

        if 'Rap' in variable:
            for b in list(best_binning):
                if b != 0.0:
                    best_binning.append(-1.0*b)
            best_binning.sort()

        # Make last bin smaller if huge
        # Won't change final results
        if len(best_binning) >= 4:
            lastBinWidth = best_binning[-1] - best_binning[-2]
            penultimateBinWidth = best_binning[-2] - best_binning[-3]
            if lastBinWidth / penultimateBinWidth > 5:
                newLastBinWidth = penultimateBinWidth * 5
                best_binning[-1] = best_binning[-2] + newLastBinWidth

        if variable == 'abs_lepton_eta':
            best_binning = [ round(i,2) for i in best_binning ]
        elif variable != 'NJets' :
            best_binning = [ round(i) for i in best_binning ]

        bin_choices[variable] = best_binning

        print('The best binning for', variable, 'is:')
        print('bin edges =', best_binning)
        print('N_bins    =', len( best_binning ) - 1)
        print('The corresponding purities and stabilities are:')
        for info in histogram_information:
            # print_latex_table(info, variable, best_binning)
            outputInfo = {}
            outputInfo['p_i'] = info['p_i']
            outputInfo['s_i'] = info['s_i']
            outputInfo['N'] = info['N']
            outputInfo['res'] = info['res']
            outputJsonFile = 'unfolding/13TeV/binningInfo_%s_%s_FullPS.txt' % ( variable, info['channel'] )
            if options.visiblePhaseSpace:
                outputJsonFile = 'unfolding/13TeV/binningInfo_%s_%s_VisiblePS.txt' % ( variable, info['channel'] )
            write_data_to_JSON( outputInfo, outputJsonFile )
        for key in outputInfo:
            print (key,outputInfo[key])
        print('-' * 120)
    print('=' * 120)
    print('For config/variable_binning.py')
    print('=' * 120)
    for variable in bin_choices:
        print('\''+variable+'\' : '+str(bin_choices[variable])+',')
        file_template = '{path_to_JSON}/{category}/unfolded_normalisation_{channel}_{method}.txt'
        filename = ''

    #     # get unfolded normalisation
        unfolded_normalisation_electron = {}
        unfolded_normalisation_muon = {}

        # Electron channel
        unfolded_normalisation_electron = get_unfolded_normalisation( TTJet_fit_results_electron, category, 'electron', tau_value_electron, visiblePS = visiblePS )
        filename = file_template.format(
                            path_to_JSON = path_to_JSON,
                            category = category,
                            channel = 'electron',
                            method = method,
                            )
        write_data_to_JSON( unfolded_normalisation_electron, filename )
        # measure xsection
        calculate_xsections( unfolded_normalisation_electron, category, 'electron' )
        calculate_normalised_xsections( unfolded_normalisation_electron, category, 'electron' )
        calculate_normalised_xsections( unfolded_normalisation_electron, category, 'electron' , True )

        # Muon channel
        unfolded_normalisation_muon = get_unfolded_normalisation( TTJet_fit_results_muon, category, 'muon', tau_value_muon, visiblePS = visiblePS )
        filename = file_template.format(
                            path_to_JSON = path_to_JSON,
                            category = category,
                            channel = 'muon',
                            method = method,
                            )
        write_data_to_JSON( unfolded_normalisation_muon, filename )
        # measure xsection