def get_best_k_from_global_correlation(regularisation_settings): ''' returns optimal_k, k_values, tau_values, rho_values - optimal_k: k-value with lowest rho - minimal_rho: lowest rho value - k_values: all scanned k-values - tau_values: tau values for all scanned k-values - rho_values: rho values for all scanned k-values ''' h_truth, h_response, h_measured, h_data = regularisation_settings.get_histograms( ) n_toy = regularisation_settings.n_toy # initialise variables optimal_k = 0 minimal_rho = 9999 n_bins = h_data.nbins() k_values = [] tau_values = [] rho_values = [] # first calculate one set to get the matrices # tau = 0 is equal to k = nbins unfolding = Unfolding( h_truth, h_measured, h_response, method='RooUnfoldSvd', tau=0., # no regularisation k_value=-1, ) unfolding.unfold(h_data) # get unfolding object svd_unfold = unfolding.Impl() # get covariance matrix cov = svd_unfold.get_data_covariance_matrix(h_data) # cache functions and save time in the loop SetTau = svd_unfold.SetTau GetCovMatrix = svd_unfold.GetUnfoldCovMatrix GetRho = svd_unfold.get_global_correlation kToTau = svd_unfold.kToTau add_k = k_values.append add_tau = tau_values.append add_rho = rho_values.append # now lets loop over all possible k-values for k in range(2, n_bins + 1): tau_from_k = kToTau(k) SetTau(tau_from_k) cov_matrix = GetCovMatrix(cov, n_toy, 1) rho = GetRho(cov_matrix, h_data) add_k(k) add_tau(tau_from_k) add_rho(rho) if rho < minimal_rho: optimal_k = k minimal_rho = rho return optimal_k, minimal_rho, k_values, tau_values, rho_values
def get_best_tau_from_global_correlation(regularisation_settings): ''' returns optimal_tau, tau_values, rho_values - optimal_tau: k-value with lowest rho - minimal_rho: lowest rho value - tau_values: all scanned tau values - rho_values: rho values for all scanned tau-values ''' h_truth, h_response, h_measured, h_data = regularisation_settings.get_histograms( ) n_toy = regularisation_settings.n_toy number_of_iterations = regularisation_settings.n_tau_scan_points tau_min = 0.1 tau_max = 1000 optimal_tau = 0 minimal_rho = 9999 tau_values = [] rho_values = [] # first calculate one set to get the matrices # tau = 0 is equal to k = nbins unfolding = Unfolding( h_truth, h_measured, h_response, method='RooUnfoldSvd', tau=0., # no regularisation k_value=-1, ) unfolding.unfold(h_data) # get unfolding object svd_unfold = unfolding.Impl() # get covariance matrix cov = svd_unfold.get_data_covariance_matrix(h_data) # cache functions and save time in the loop SetTau = svd_unfold.SetTau GetCovMatrix = svd_unfold.GetUnfoldCovMatrix GetRho = svd_unfold.get_global_correlation add_tau = tau_values.append add_rho = rho_values.append # now lets loop over all tau-values in range for current_tau in get_tau_range(tau_min, tau_max, number_of_iterations): SetTau(current_tau) cov_matrix = GetCovMatrix(cov, n_toy, 1) current_rho = GetRho(cov_matrix, h_data) add_tau(current_tau) add_rho(current_rho) if current_rho < minimal_rho: minimal_rho = current_rho optimal_tau = current_tau print 'Best tau for', regularisation_settings.channel, ':', optimal_tau return optimal_tau, minimal_rho, tau_values, rho_values
def get_tau_from_global_correlation( h_truth, h_measured, h_response, h_data = None ): global used_k # this gives 9.97e-05 with TUnfold tau_0 = 1 tau_max = 1000 number_of_iterations = int(100) n_toy = int(1000) # tau_step = ( tau_max - tau_0 ) / number_of_iterations optimal_tau = 0 minimal_rho = 9999 # bias_scale = 0. unfolding = Unfolding( h_truth, h_measured, h_response, method = 'RooUnfoldSvd', tau = tau_0, k_value = -1, ) data = None if h_data: data = h_data else: # closure test data = h_measured unfolding.unfold( data ) # get unfolding object tau_svd_unfold = unfolding.Impl() # get covariance matrix cov = tau_svd_unfold.get_data_covariance_matrix(data) # cache functions and save time in the loop SetTau = tau_svd_unfold.SetTau GetCovMatrix = tau_svd_unfold.GetUnfoldCovMatrix GetRho = tau_svd_unfold.get_global_correlation n_bins = h_data.nbins() print 'k to tau' to_return = None for k in range(2, n_bins + 1): tau_from_k = tau_svd_unfold.kToTau(k) SetTau( tau_from_k ) cov_matrix = GetCovMatrix(cov, n_toy, 1) rho = GetRho(cov_matrix, data) if k == used_k: to_return = (tau_from_k, rho) print 'k =', k, ', tau = ', tau_from_k, ', rho = ', rho, '<-- currently used' else: print 'k =', k, ', tau = ', tau_from_k, ', rho = ', rho #print 'used k (=%d) to tau' % used_k tau_from_k = tau_svd_unfold.kToTau(used_k) #SetTau( tau_from_k ) #cov_matrix = GetCovMatrix(cov, 10, 1) #rho_from_tau_from_k = GetRho(cov_matrix, data) #print "tau from k", tau_from_k #print 'rho for tau from used k', rho_from_tau_from_k # create lists tau_values = [] rho_values = [] add_tau = tau_values.append add_rho = rho_values.append # for current_tau in drange(tau_0, tau_max, tau_step): for current_tau in get_tau_range( tau_0, tau_max, number_of_iterations ): SetTau( current_tau ) cov_matrix = GetCovMatrix(cov, n_toy, 1) current_rho = GetRho(cov_matrix, data) add_tau( current_tau ) add_rho( current_rho ) if current_rho < minimal_rho: minimal_rho = current_rho optimal_tau = current_tau del unfolding print 'optimal tau = %.2f' % optimal_tau return optimal_tau, minimal_rho, tau_values, rho_values, to_return