def make_phi_hist_with_noise(rid, is_parallel, hist_dim, noise_stdev, normalize): """Makes a histogram of the phi angle with added noise.""" hist = TH1D("blurredHist" + str(rid), "blurredHist" + str(rid), hist_dim[0], hist_dim[1], hist_dim[2]) if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str( rid) + "_" + letter + ".root" f = ROOT.TFile.Open(file_path) phi_list = [] for event in f.hitTree: phi_list.append(gRandom.Gaus(event.phi, noise_stdev)) mean = srkmisc.reduce_periodics(phi_list) stdev = srkmisc.careful_std(phi_list) for x in phi_list: if normalize: hist.Fill((x - mean) / stdev) else: hist.Fill(x) f.Close() ROOT.SetOwnership(hist, True) return hist
def make_alpha_vs_phi_plot(run_id, is_parallel): if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str( run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() num_events = hit_tree.GetEntries() phi_list = [] alpha_list = [] radius = srkdata.get_data_for_rids_from_database([run_id], "ChamberRadius")[0][0] print radius for i in xrange(num_events): hit_tree.GetEntry(i) phi_list.append(hit_tree.phi) alpha = get_alpha_angle_2d(radius, hit_tree.pos0, hit_tree.vel0) alpha_list.append(alpha) root_file.Close() phi_mean = srkmisc.reduce_periodics(phi_list) delta_phi_list = map(lambda x: x - phi_mean, phi_list) plt.scatter(alpha_list, delta_phi_list)
def calc_delta_stats_same_tracks(run_id, use_wrapping=True): """Calculates differences in orientation of fields when tracks are the same""" hit_trees = [] root_files = [] for is_parallel in [True, False]: if is_parallel: letter = 'P' else: letter = 'A' file_path = srkglobal.results_dir + "Results_RID" + str( run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_files.append(TFile(file_path, "READ")) hit_trees.append(gDirectory.Get('hitTree')) gROOT.cd() delta_phi_list = [] for i in xrange(hit_trees[0].GetEntries()): hit_trees[0].GetEntry(i) hit_trees[1].GetEntry(i) delta_phi = hit_trees[0].phi - hit_trees[1].phi delta_phi_list.append(delta_phi) if use_wrapping: delta_phi_mean = srkmisc.reduce_periodics(delta_phi_list) else: delta_phi_mean = srkmisc.careful_mean(delta_phi_list) delta_phi_std = srkmisc.careful_std(delta_phi_list) root_files[0].Close() root_files[1].Close() return [delta_phi_mean, delta_phi_std]
def make_alpha_vs_phi_plot(run_id, is_parallel): if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() num_events = hit_tree.GetEntries() phi_list = [] alpha_list = [] radius = srkdata.get_data_for_rids_from_database([run_id], "ChamberRadius")[0][0] print radius for i in xrange(num_events): hit_tree.GetEntry(i) phi_list.append(hit_tree.phi) alpha = get_alpha_angle_2d(radius, hit_tree.pos0, hit_tree.vel0) alpha_list.append(alpha) root_file.Close() phi_mean = srkmisc.reduce_periodics(phi_list) delta_phi_list = map(lambda x: x - phi_mean, phi_list) plt.scatter(alpha_list, delta_phi_list)
def make_phi_hist_with_noise(rid, is_parallel, hist_dim, noise_stdev, normalize): """Makes a histogram of the phi angle with added noise.""" hist = TH1D("blurredHist" + str(rid), "blurredHist" + str(rid), hist_dim[0], hist_dim[1], hist_dim[2]) if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(rid) + "_" + letter + ".root" f = ROOT.TFile.Open(file_path) phi_list = [] for event in f.hitTree: phi_list.append(gRandom.Gaus(event.phi, noise_stdev)) mean = srkmisc.reduce_periodics(phi_list) stdev = srkmisc.careful_std(phi_list) for x in phi_list: if normalize: hist.Fill((x - mean) / stdev) else: hist.Fill(x) f.Close() ROOT.SetOwnership(hist, True) return hist
def calc_delta_stats_same_tracks(run_id, use_wrapping=True): """Calculates differences in orientation of fields when tracks are the same""" hit_trees = [] root_files = [] for is_parallel in [True, False]: if is_parallel: letter = 'P' else: letter = 'A' file_path = srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_files.append(TFile(file_path, "READ")) hit_trees.append(gDirectory.Get('hitTree')) gROOT.cd() delta_phi_list = [] for i in xrange(hit_trees[0].GetEntries()): hit_trees[0].GetEntry(i) hit_trees[1].GetEntry(i) delta_phi = hit_trees[0].phi - hit_trees[1].phi delta_phi_list.append(delta_phi) if use_wrapping: delta_phi_mean = srkmisc.reduce_periodics(delta_phi_list) else: delta_phi_mean = srkmisc.careful_mean(delta_phi_list) delta_phi_std = srkmisc.careful_std(delta_phi_list) root_files[0].Close() root_files[1].Close() return [delta_phi_mean, delta_phi_std]
def make_sz_prob_dist(run_id, is_parallel, use_wrapping=True): """Make a spin direction (after flip) detection probability histogram""" if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str( run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return [] root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() num_events = hit_tree.GetEntries() phi_list = [] theta_list = [] for i in xrange(num_events): hit_tree.GetEntry(i) phi_list.append(hit_tree.phi) theta_list.append(hit_tree.theta) root_file.Close() if use_wrapping: phi_mean = srkmisc.reduce_periodics(phi_list) else: phi_mean = srkmisc.careful_mean(phi_list) phi_std = srkmisc.careful_std(phi_list) theta_std = srkmisc.careful_std(theta_list) comb_std = np.sqrt(phi_std * phi_std + theta_std * theta_std) print "Combined Standard deviation: %e" % (comb_std) x = np.linspace(-30, 30, num=60) y = [] for num_std_dev in x: sz_det_prob = 0 for phi, theta in zip(phi_list, theta_list): sz_det_prob += calc_opposite_spin_prob( phi - phi_mean + comb_std * num_std_dev, theta) sz_det_prob /= num_events y.append(1 - sz_det_prob) return x, y
def make_sz_prob_dist(run_id, is_parallel, use_wrapping=True): """Make a spin direction (after flip) detection probability histogram""" if is_parallel: letter = 'P' else: letter = 'A' file_path = srkdata.srkglobal.results_dir + "Results_RID" + str(run_id) + "_" + letter + ".root" if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return [] root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() num_events = hit_tree.GetEntries() phi_list = [] theta_list = [] for i in xrange(num_events): hit_tree.GetEntry(i) phi_list.append(hit_tree.phi) theta_list.append(hit_tree.theta) root_file.Close() if use_wrapping: phi_mean = srkmisc.reduce_periodics(phi_list) else: phi_mean = srkmisc.careful_mean(phi_list) phi_std = srkmisc.careful_std(phi_list) theta_std = srkmisc.careful_std(theta_list) comb_std = np.sqrt(phi_std * phi_std + theta_std * theta_std) print "Combined Standard deviation: %e" % (comb_std) x = np.linspace(-30, 30, num=60) y = [] for num_std_dev in x: sz_det_prob = 0 for phi, theta in zip(phi_list, theta_list): sz_det_prob += calc_opposite_spin_prob(phi - phi_mean + comb_std * num_std_dev, theta) sz_det_prob /= num_events y.append(1 - sz_det_prob) return x, y
def calc_stats_for_results_file(file_path, runtype="nedm", use_wrapping=False): """Calculates summary stats for a SRK results ROOT file.""" stats = srkdata.default_file_stats(runtype) # Check if file exists and is valid if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() stats['NumEventsRun'] = hit_tree.GetEntries() # Get arrays for phi and theta from file phi_array = np.empty(hit_tree.GetEntries()) theta_array = np.empty(hit_tree.GetEntries()) for i in xrange(stats['NumEventsRun']): hit_tree.GetEntry(i) phi_array[i] = hit_tree.phi theta_array[i] = hit_tree.theta root_file.Close() # Calculate the mean phi's and thetas. Depending on purpose, wrap around at two pi depending on mean location. if use_wrapping: stats['PhiMean'] = srkmisc.reduce_periodics(phi_array) stats['ThetaMean'] = srkmisc.reduce_periodics(theta_array) else: stats['PhiMean'] = srkmisc.careful_mean(phi_array) stats['ThetaMean'] = srkmisc.careful_mean(theta_array) if runtype != "g2": # Calculate probability of detecting the spin in the opposite direction stats['SZDetProb'] = 0. for phi, theta in zip(phi_array, theta_array): stats['SZDetProb'] += calc_opposite_spin_prob( phi - stats['PhiMean'], theta) stats['SZDetProb'] /= stats['NumEventsRun'] # Calculate other summary info stats['PhiStDev'] = srkmisc.careful_std(phi_array) stats['ThetaStDev'] = srkmisc.careful_std(theta_array) stats['PhiError'] = stats['PhiStDev'] / math.sqrt(len(phi_array)) stats['ThetaError'] = stats['ThetaStDev'] / math.sqrt(len(theta_array)) if runtype != "g2": stats['PhiKurtosis'] = kurtosis(phi_array) stats['ThetaKurtosis'] = kurtosis(theta_array) stats['PhiSkewness'] = skew(phi_array) stats['ThetaSkewness'] = skew(theta_array) print "Phi Kurtosis: %f" % stats['PhiKurtosis'] percentile_lower, percentile_upper = np.percentile( phi_array, (16.5, 83.50)) percentile_width = percentile_upper - percentile_lower stats['PhiPercentileWidth'] = percentile_width percentile_lower, percentile_upper = np.percentile( theta_array, (16.5, 83.5)) percentile_width = percentile_upper - percentile_lower stats['ThetaPercentileWidth'] = percentile_width # Fit phi to Tsallis q-Gaussian function (old form) power, error = make_tsallis_fit(phi_array, stats['PhiMean'], stats['PhiStDev']) stats['PhiTsallisPower'] = power stats['PhiTsallisPowerError'] = error # Fit theta to Tsallis q-Gaussian function (old form) power, error = make_tsallis_fit(theta_array, stats['ThetaMean'], stats['ThetaStDev']) stats['ThetaTsallisPower'] = power stats['ThetaTsallisPowerError'] = error # Fit phi to Tsallis q-Gaussian function (new form) power, error = make_qgaussian_fit(phi_array, stats['PhiMean'], stats['PhiStDev']) stats['PhiQGaussianQ'] = power stats['PhiQGaussianQError'] = error # Fit theta to Tsallis q-Gaussian function (new form) power, error = make_qgaussian_fit(theta_array, stats['ThetaMean'], stats['ThetaStDev']) stats['ThetaQGaussianQ'] = power stats['ThetaQGaussianQError'] = error return stats
def calc_stats_for_results_file(file_path, use_wrapping=False): """Calculates summary stats for a SRK results ROOT file.""" stats = srkdata.default_file_stats() # Check if file exists and is valid if not srkmisc.file_exits_and_not_zombie(file_path): print file_path + " doesn't exist or is zombie." return {} root_file = TFile(file_path, "READ") hit_tree = gDirectory.Get('hitTree') gROOT.cd() stats['NumEventsRun'] = hit_tree.GetEntries() # Get arrays for phi and theta from file phi_array = np.empty(hit_tree.GetEntries()) theta_array = np.empty(hit_tree.GetEntries()) for i in xrange(stats['NumEventsRun']): hit_tree.GetEntry(i) phi_array[i] = hit_tree.phi theta_array[i] = hit_tree.theta root_file.Close() # Calculate the mean phi's and thetas. Depending on purpose, wrap around at two pi depending on mean location. if use_wrapping: stats['PhiMean'] = srkmisc.reduce_periodics(phi_array) stats['ThetaMean'] = srkmisc.reduce_periodics(theta_array) else: stats['PhiMean'] = srkmisc.careful_mean(phi_array) stats['ThetaMean'] = srkmisc.careful_mean(theta_array) # Calculate probability of detecting the spin in the opposite direction stats['SZDetProb'] = 0. for phi, theta in zip(phi_array, theta_array): stats['SZDetProb'] += calc_opposite_spin_prob(phi - stats['PhiMean'], theta) stats['SZDetProb'] /= stats['NumEventsRun'] # Calculate other summary info stats['PhiStDev'] = srkmisc.careful_std(phi_array) stats['ThetaStDev'] = srkmisc.careful_std(theta_array) stats['PhiError'] = stats['PhiStDev'] / math.sqrt(len(phi_array)) stats['ThetaError'] = stats['ThetaStDev'] / math.sqrt(len(theta_array)) stats['PhiKurtosis'] = kurtosis(phi_array) stats['ThetaKurtosis'] = kurtosis(theta_array) stats['PhiSkewness'] = skew(phi_array) stats['ThetaSkewness'] = skew(theta_array) print "Phi Kurtosis: %f" % stats['PhiKurtosis'] percentile_lower, percentile_upper = np.percentile(phi_array, (16.5, 83.50)) percentile_width = percentile_upper - percentile_lower stats['PhiPercentileWidth'] = percentile_width percentile_lower, percentile_upper = np.percentile(theta_array, (16.5, 83.5)) percentile_width = percentile_upper - percentile_lower stats['ThetaPercentileWidth'] = percentile_width # Fit phi to Tsallis q-Gaussian function (old form) power, error = make_tsallis_fit(phi_array, stats['PhiMean'], stats['PhiStDev']) stats['PhiTsallisPower'] = power stats['PhiTsallisPowerError'] = error # Fit theta to Tsallis q-Gaussian function (old form) power, error = make_tsallis_fit(theta_array, stats['ThetaMean'], stats['ThetaStDev']) stats['ThetaTsallisPower'] = power stats['ThetaTsallisPowerError'] = error # Fit phi to Tsallis q-Gaussian function (new form) power, error = make_qgaussian_fit(phi_array, stats['PhiMean'], stats['PhiStDev']) stats['PhiQGaussianQ'] = power stats['PhiQGaussianQError'] = error # Fit theta to Tsallis q-Gaussian function (new form) power, error = make_qgaussian_fit(theta_array, stats['ThetaMean'], stats['ThetaStDev']) stats['ThetaQGaussianQ'] = power stats['ThetaQGaussianQError'] = error return stats