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)))
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)
def plot(params_file): config = ConfigLoader("config.yml") with open(params_file) as f: params = yaml.safe_load(f) params = params["value"] config.plot_partial_wave(params, plot_pull=True, prefix="fig/", save_pdf=True, save_root=True)