Example #1
0
def fit(final_params_file):
    config = ConfigLoader(
        "config.yml"
    )  # We use ConfigLoader to read the information in the configuration file
    # config.set_params("gen_params.json") # If not set, we will use random initial parameters
    fit_result = config.fit(method="BFGS")

    errors = config.get_params_error(
        fit_result)  # calculate Hesse errors of the parameters
    print("\n########## fit parameters:")
    for key, value in config.get_params().items():
        print(key, error_print(value, errors.get(key, None)))

    fit_result.save_as(final_params_file)  # save fit_result to a json file
    config.plot_partial_wave(
        fit_result
    )  # Plot distributions of variables indicated in the configuration file

    fit_frac, err_frac = config.cal_fitfractions()
    print("\n########## fit fractions:")
    for i in fit_frac:
        if not isinstance(i, tuple):  # fit fraction
            name = i
        else:
            name = "{}x{}".format(*i)  # interference term
        print(name + ": " + error_print(fit_frac[i], err_frac.get(i, None)))
Example #2
0
def fit(config="config.yml", init_params="init_params.json", method="BFGS"):
    """
    simple fit script
    """
    # load config.yml
    config = ConfigLoader(config)

    # set initial parameters if have
    try:
        config.set_params(init_params)
        print("using {}".format(init_params))
    except Exception as e:
        if str(e) != "[Errno 2] No such file or directory: 'init_params.json'":
            print(e)
        print("\nusing RANDOM parameters", flush=True)

    # print("\n########### initial parameters")
    # json_print(config.get_params())

    # fit
    data, phsp, bg, inmc = config.get_all_data()
    try:
        fit_result = config.fit(batch=65000, method=method)
    except KeyboardInterrupt:
        config.save_params("break_params.json")
        raise
    except Exception as e:
        print(e)
        config.save_params("break_params.json")
        raise
    json_print(fit_result.params)
    fit_result.save_as("final_params.json")

    # calculate parameters error
    fit_error = config.get_params_error(fit_result, batch=13000)
    fit_result.set_error(fit_error)
    fit_result.save_as("final_params.json")
    pprint(fit_error)

    print("\n########## fit results:")
    for k, v in config.get_params().items():
        print(k, error_print(v, fit_error.get(k, None)))

    # plot partial wave distribution
    config.plot_partial_wave(fit_result, plot_pull=True)

    # calculate fit fractions
    phsp_noeff = config.get_phsp_noeff()
    fit_frac, err_frac = config.cal_fitfractions({}, phsp_noeff)

    print("########## fit fractions")
    fit_frac_string = ""
    for i in fit_frac:
        if isinstance(i, tuple):
            name = "{}x{}".format(*i)
        else:
            name = i
        fit_frac_string += "{} {}\n".format(
            name, error_print(fit_frac[i], err_frac.get(i, None)))
    print(fit_frac_string)
Example #3
0
def single_fit(config_dict, data, phsp, bg):
    config = ConfigLoader(config_dict)

    print("\n########### initial parameters")
    pprint(config.get_params())
    print(config.full_decay)
    fit_result = config.fit(data, phsp, bg=bg)
    pprint(fit_result.params)
    # fit_result.save_as("final_params.json")
    return fit_result.min_nll, fit_result.ndf
Example #4
0
# We set parameters to a blance value. And we can generate some toy data and calclute the weights
#

input_params = {
    "A->R1_a.BR1_a->C.D_total_0r": 6.0,
    "A->R1_b.BR1_b->C.D_total_0r": 1.0,
    "A->R2.CR2->B.D_total_0r": 2.0,
    "A->R3.DR3->B.C_total_0r": 1.0,
}
config.set_params(input_params)

data = config.generate_toy(1000)
phsp = config.generate_phsp(10000)

# You can also fit the data fit to the data
fit_result = config.fit([data], [phsp])
err = config.get_params_error(fit_result, [data], [phsp])

# %%
# we can see that thre fit results consistant with inputs, the first one is fixed.

for var in input_params:
    print(
        f"in: {input_params[var]} => out: {fit_result.params[var]} +/- {err.get(var, 0.)}"
    )

# %%
# We can use the amplitude to plot the fit results

amp = config.get_amplitude()
weight = amp(phsp)