Пример #1
0
def chi_distribution(N):  #where N - number of signal samples
    Chi_s = np.zeros(10000)
    for i in range(0, 10000):
        s = sht.generate_data(N)
        heights, edges, patches_ = plt.hist(s, range=[104, 155], bins=30)
        Lambd = mean(s[N:])
        A_c = findA(Lambd, edges, heights)
        Chi_s[i] = (sht.get_B_chi(s, [104, 155], 30, A_c, Lambd)) * 28
    f = open('Chi_Set_background' + str(N), 'wb')
    pickle.dump(Chi_s, f)
    f.close()
    return print('P0 is saved')
"""
Created on Sat Jun  6 12:11:15 2020

@author: dd719
"""
#%%
#the firse cell is dedicated to importing all relevant packages, if a package is needed later down the line, add it to the list here
import STOM_higgs_tools  #this line imports all the funcyions contained in the file avaidible on bb, for this to work, make sure that file is visible in the files window in your ide
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
#new comment: every time you import STOM_higgs_tools you get a different data set, make sure you make your code general and don't use any values from your specific data set as they will change
#sometimes the data you get looks really bad and you seemingly get a second peak, if that happens to you, re-run every cell in order until you get a graph that looks reasonable
#%%
print('Part 1')
data_array = STOM_higgs_tools.generate_data()
#print(data_array)#there will likeley be a lot of prints for my peace of mind to chech what im working with at each point
#the sheet recommends a histogram function from matplotlib, but i will write my own as it will be easier to extract data later down the line, and can be made to look like their figure
n_bins = 30  #self-explanatory, this determines the number of bins to be made
bin_boundary_array = np.linspace(
    104, 155, (n_bins + 1))  #makes an array of consecutive bin boundaries
#print(bin_boundary_array)
bin_width = (155 - 104) / n_bins
#print(bin_width)
bins = [[] for _ in range(n_bins)
        ]  #makes the required number of empty arrays to act as bins
#print(bins)
counter1 = 0
for i in bin_boundary_array:  #this is a lot of comparisons made and may take a couple seconds to process
    if counter1 == n_bins:
        break  # this is vital as an error occurs when the loop looks for a nonexistant index of 31 in bin_boundary_array
Пример #3
0
np.random.seed(1)
start = 104
stop = 150
bin_num = 30
edges = sp.linspace(start, stop, num=bin_num+1, endpoint=True)
width = edges[1]-edges[0]
x_mid = (width/2 + edges)[:-1]

iters = 250
BG_opts = sp.zeros((iters, 2))
BG_chisqs = sp.zeros(iters)
BG_ps = sp.zeros(iters)

for i in range(iters):
    vals = stom.generate_data()
    means = sp.zeros(bin_num)
    freqs = sp.zeros(bin_num)
    bins = [ [] for i in range(bin_num) ]
    print(iters - i) #countdown
    for val in vals:
        if start < val and val < stop:
            bins[sp.searchsorted(edges, val)-1].append(val)
    for j, Bin in enumerate(bins):
        means[j] = sp.mean(Bin)
        freqs[j] = len(Bin)

    BG_opt, BG_cov = op.curve_fit(f=BG, xdata=means, ydata=freqs,
                                  p0=(20000, 30) )
    BG_opts[i] = BG_opt
    BG_chisqs[i], BG_ps[i] = st.chisquare(f_obs=freqs,
Пример #4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 28 21:40:17 2018

@author: sophie
"""

import numpy as np
import matplotlib.pyplot as plt
import STOM_higgs_tools as tools

data = tools.generate_data()
# Each list entry represents the rest mass reconstructed from a collision

# Generate histogram data
bin_heights, bin_edges = np.histogram(data, bins=30, range=(104, 155))
bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])
mean_std = np.sqrt(bin_heights)  # Using Poisson error sqrt for the height
widths = bin_edges[1:] - bin_edges[:-1]

# Plotting data with 30 bins
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 15), sharex=True)
ax1.bar(bin_centers,
        bin_heights,
        width=widths,
        color='orange',
        yerr=mean_std,
        error_kw=dict(elinewidth=0.5, ecolor='black'))
ax1.set_title('Bins = 30')
ax1.set_ylabel('Number of entries')