def main():
    data, data_ctl = get_data()
    signal, bkg, signal_ctl, bkg_ctl = get_templates()
    edges = list( data.xedges() )
#     print list(data.y())
#     data, be = hist_to_numpy(data)
#     print data
#     data_ctl, _ = hist_to_numpy(data_ctl)
#     signal, _ = hist_to_numpy(signal)
#     signal_ctl, _ = hist_to_numpy(signal_ctl)
#     bkg, _ = hist_to_numpy(bkg)
#     bkg_ctl, _ = hist_to_numpy(bkg_ctl)
    sig_pdf = hist_to_pdf( signal )
    bkg_pdf = hist_to_pdf( bkg )
    epsig = Extended( sig_pdf, extname = 'N1' )
    epbkg = Extended( bkg_pdf, extname = 'N2' )
    pdf = AddPdf( epbkg, epsig )
    fitdata = array( list( data.y() ) )
    blh = BinnedLH( pdf, fitdata, bins = n_bins, bound = ( min_x, max_x ), extended = True )
    m = Minuit( blh, N1 = 2000, N2 = 30000, error_N1 = 44, error_N2 = 200 )
    m.migrad()
    blh.draw( m, parts = True )
 run_uncut = n_hits_all_uncut[i]
 # Convert to numpy array
 run_np = np.zeros(len(run))
 for j in range(len(run)):
     run_np[j] = run[j]
 # Determine number of bins
 n_hits_bins_plot = max(run_uncut) - min(run_uncut) + 1
 n_hits_bins_fit = max(run) - min(run) + 1
 # Make histogram
 hist(run_uncut, bins=n_hits_bins_plot, histtype='step')
 norm_const = len(run) # Normalization constant
 # Get PDF
 pdf_nh = Normalized(gaussian, (min(run), max(run)))
 pdf_nh = Extended(pdf_nh)
 # Do fit
 blh_nh = BinnedLH(pdf_nh, run_np, bound=(low_fit_bounds[i], high_fit_bounds[i]), bins=(high_fit_bounds[i] - low_fit_bounds[i] + 1), extended=True)
 m_nh = Minuit(blh_nh, mean=150., sigma= 12., N=400, error_mean=10., error_sigma=1.,limit_sigma=(0.,10000.), error_N=10., limit_mean=(0,800))
 m_nh.set_up(0.5)
 m_nh.migrad()
 nh_means.append(m_nh.values["mean"])
 blh_nh.show(m_nh);
 # Output plot
 title("Number of photons incident on APD, 511 keV gamma " + str(dist[i]) + "cm away from APD")
 xlabel("Number of photons")
 ylabel("Number of events")
 savefig("n_hits" + str(i) + ".pdf")
 figure()
 # Energy. Same procedure as for the number of hits.
 run = energy_all[i]
 run_uncut = energy_all_uncut[i]
 run_np = np.zeros(len(run))
# Poisson binned log likelihood with minimum subtractacted(aka likelihood ratio).

# <codecell>

from probfit import Extended, BinnedLH
seed(0)
gdata = randn(10000)

# <codecell>

mypdf = gaussian
describe(mypdf) # just basically N*gaussian(x,mean,sigma)

# <codecell>

blh = BinnedLH(mypdf, gdata, bound=(-3,3))#create cost function
#it can also do extended one if you pass it an extended pdf and pass extended=True to BinnedLH
blh.show(args={'mean':1.0, 'sigma':1.0})

# <codecell>

m = Minuit(blh, mean=1.0, sigma=1)
m.set_up(0.5)
m.migrad()
blh.show(m)

# <markdowncell>

# ####$\chi^2$ Regression
# Some time you just want a simple line fit as opposed to fitting pdf.
Exemple #4
0
from iminuit import Minuit
from probfit import BinnedLH, Extended, AddPdf, gen_toy
from probfit.pdf import HistogramPdf
from probfit.plotting import draw_pdf
import numpy as np

bound = (0, 10)
np.random.seed(0)
bkg = gen_toy(lambda x: x**2, 100000, bound=bound)  # a parabola background
sig = np.random.randn(50000) + 5  # a Gaussian signal
data = np.concatenate([sig, bkg])
# fill histograms with large statistics
hsig, be = np.histogram(sig, bins=40, range=bound)
hbkg, be = np.histogram(bkg, bins=be, range=bound)
# randomize data
data = np.random.permutation(data)
fitdata = data[:1000]

psig = HistogramPdf(hsig, be)
pbkg = HistogramPdf(hbkg, be)
epsig = Extended(psig, extname='N1')
epbkg = Extended(pbkg, extname='N2')
pdf = AddPdf(epbkg, epsig)

blh = BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True)
m = Minuit(blh, N1=330, N2=670, error_N1=20, error_N2=30)
#m.migrad()
blh.draw(m, parts=True)
Exemple #5
0
from iminuit import Minuit
from probfit import BinnedLH, gaussian, Extended
from matplotlib import pyplot as plt
from numpy.random import randn

data = randn(1000) * 2 + 1

#Unextended

blh = BinnedLH(gaussian, data)
#if you wonder what it looks like call describe(blh)
m = Minuit(blh, mean=0., sigma=0.5)

plt.figure(figsize=(8, 6))
plt.subplot(221)
blh.draw(m)
plt.title('Unextended Before')

m.migrad()  # fit

plt.subplot(222)
blh.draw(m)
plt.title('Unextended After')

#Extended

ext_gauss = Extended(gaussian)

blh = BinnedLH(ext_gauss, data, extended=True)
m = Minuit(blh, mean=0., sigma=0.5, N=900.)
# Poisson binned log likelihood with minimum subtractacted(aka likelihood ratio).

# <codecell>

from probfit import Extended, BinnedLH
seed(0)
gdata = randn(10000)

# <codecell>

mypdf = gaussian
describe(mypdf) # just basically N*gaussian(x,mean,sigma)

# <codecell>

blh = BinnedLH(mypdf, gdata, bound=(-3,3))#create cost function
#it can also do extended one if you pass it an extended pdf and pass extended=True to BinnedLH
blh.show(args={'mean':1.0, 'sigma':1.0})

# <codecell>

m = Minuit(blh, mean=1.0, sigma=1)
m.set_up(0.5)
m.migrad()
blh.show(m)

# <markdowncell>

# ####$\chi^2$ Regression
# Some time you just want a simple line fit as opposed to fitting pdf.
Exemple #7
0
from matplotlib import pyplot as plt
from numpy.random import randn
import numpy as np

peak1 = randn(1000)*0.5 + 1.
peak2 = randn(500)*0.5 + 0.
#two peaks data with shared width
data = np.concatenate([peak1, peak2])

#Share the width
#If you use Normalized here. Do not reuse the object.
#It will be really slow due to cache miss. Read Normalized doc for more info.
pdf1 = rename(gaussian, ('x', 'm_1', 'sigma'))
pdf2 = rename(gaussian, ('x', 'm_2', 'sigma'))

compdf = AddPdfNorm(pdf1, pdf2) # merge by name (merge sigma)

ulh = BinnedLH(compdf, data, extended=False)
m = Minuit(ulh, m_1=1.1, m_2=-0.1, sigma=0.48, f_0=0.6, limit_f_0=(0,1))

plt.figure(figsize=(8, 3))
plt.subplot(121)
ulh.draw(m, parts=True)
plt.title('Before')

m.migrad() # fit

plt.subplot(122)
ulh.draw(m, parts=True)
plt.title('After')
Exemple #8
0
from iminuit import Minuit
from probfit import BinnedLH, Extended, AddPdf, gen_toy
from probfit.pdf import HistogramPdf
import numpy as np

bound = (0, 10)
np.random.seed(0)
bkg = gen_toy(lambda x : x**2, 100000, bound=bound) # a parabola background
sig= np.random.randn(50000)+5  # a Gaussian signal
data= np.concatenate([sig,bkg])
# fill histograms with large statistics
hsig,be= np.histogram(sig, bins=40, range=bound);
hbkg,be= np.histogram(bkg, bins=be, range=bound);
# randomize data 
data= np.random.permutation(data)
fitdata= data[:1000] 

psig= HistogramPdf(hsig,be)
pbkg= HistogramPdf(hbkg,be)
epsig= Extended(psig, extname='N1')
epbkg= Extended(pbkg, extname='N2')
pdf= AddPdf(epbkg,epsig)

blh= BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True)
m= Minuit(blh, N1=330, N2= 670, error_N1=20, error_N2=30)
#m.migrad()
blh.draw(m, parts=True)
Exemple #9
0
from iminuit import Minuit
from probfit import BinnedLH, gaussian, Extended
from matplotlib import pyplot as plt
from numpy.random import randn

data = randn(1000)*2 + 1

#Unextended

blh = BinnedLH(gaussian, data)
#if you wonder what it loos like call desceibe(blh)
m = Minuit(blh, mean=0., sigma=0.5)

plt.figure(figsize=(8, 6))
plt.subplot(221)
blh.draw(m)
plt.title('Unextended Before')

m.migrad() # fit

plt.subplot(222)
blh.draw(m)
plt.title('Unextended After')

#Extended

ext_gauss = Extended(gaussian)

blh = BinnedLH(ext_gauss, data, extended=True)
m = Minuit(blh, mean=0., sigma=0.5, N=900.)
Exemple #10
0
                                bins=bins)
binned_asimov_errors = np.sqrt(binned_asimov + syst * binned_asimov**2)

print(f"Running fits...")
results = {}
nominal_template = AddPdf(
    Extended(signal_templates[(1.0, 1.0)][0], extname="s"),
    Extended(bkg_template[0], extname="b"),
)

nominal_lh = BinnedLH(
    nominal_template,
    bin_centers,
    bins=n_bins,
    bound=bounds,
    weights=binned_asimov,
    weighterrors=binned_asimov_errors,
    use_w2=True,
    nint_subdiv=3,
    extended=True,
)
print(f"{Style.RESET_ALL}", end="")
nominal_lh_val = 2 * nominal_lh(signal_templates[(1.0, 1.0)][1],
                                bkg_template[1])
nominal_lh.draw()
nominal_lh = nominal_lh_val
plt.savefig("nominal.pdf")
plt.close()

for (yuk, lam), (pdf, norm) in signal_templates.items():
    bkg_norm = bkg_template[1]
Exemple #11
0
signal = HistiSample("signal")
signal.SetHisto(hsig)
signal.AddNorm("SigXSecOverSM", 0.5, 0, 3)

background = HistiSample("background1")
background.SetHisto(hbkg)

background.AddOverallSys("JES", 0.9, 1.1)

chan = HistiChannel("SR")
chan.AddSample(signal)
chan.AddSample(background)

m.AddChannel(chan)

data = gen_toy(lambda x: m.pdf(x, 1, 1), 150, (0, 10))
chan.SetData(data)

blh = BinnedLH(m.pdf, data, bins=10, bound=bound, extended=True)

minimiser = Minuit(blh,
                   SigXSecOverSM=0.5,
                   JES=1.,
                   error_SigXSecOverSM=1.,
                   error_JES=.1)

print 'about to test SigXSecOverSM at value', minimiser.values['SigXSecOverSM']

minimiser.migrad()
print 'migrad gives SigXSecOverSM as value', minimiser.values['SigXSecOverSM']
Exemple #12
0
peak1 = randn(1000) * 0.5 + 1.0
peak2 = randn(500) * 0.5 + 0.0
# two peaks data with shared width
data = np.concatenate([peak1, peak2])

# Share the width
# If you use Normalized here. Do not reuse the object.
# It will be really slow due to cache miss. Read Normalized doc for more info.
pdf1 = rename(gaussian, ("x", "m_1", "sigma"))
pdf2 = rename(gaussian, ("x", "m_2", "sigma"))

ext_pdf1 = Extended(pdf1, extname="N_1")
ext_pdf2 = Extended(pdf2, extname="N_2")

compdf = AddPdf(ext_pdf1, ext_pdf2)  # merge by name (merge sigma)

ulh = BinnedLH(compdf, data, extended=True)
m = Minuit(ulh, m_1=0.1, m_2=-0.1, sigma=0.1, N_1=900, N_2=480)

plt.figure(figsize=(8, 3))
plt.subplot(121)
ulh.draw(m, parts=True)
plt.title("Before")

m.migrad()  # fit

plt.subplot(122)
ulh.draw(m, parts=True)
plt.title("After")
 run_np = np.zeros(len(run))
 for j in range(len(run)):
     run_np[j] = run[j]
 # Determine number of bins
 n_hits_bins_plot = max(run_uncut) - min(run_uncut) + 1
 n_hits_bins_fit = max(run) - min(run) + 1
 # Make histogram
 hist(run_uncut, bins=n_hits_bins_plot, histtype='step')
 norm_const = len(run)  # Normalization constant
 # Get PDF
 pdf_nh = Normalized(gaussian, (min(run), max(run)))
 pdf_nh = Extended(pdf_nh)
 # Do fit
 blh_nh = BinnedLH(pdf_nh,
                   run_np,
                   bound=(low_fit_bounds[i], high_fit_bounds[i]),
                   bins=(high_fit_bounds[i] - low_fit_bounds[i] + 1),
                   extended=True)
 m_nh = Minuit(blh_nh,
               mean=150.,
               sigma=12.,
               N=400,
               error_mean=10.,
               error_sigma=1.,
               limit_sigma=(0., 10000.),
               error_N=10.,
               limit_mean=(0, 800))
 m_nh.set_up(0.5)
 m_nh.migrad()
 nh_means.append(m_nh.values["mean"])
 blh_nh.show(m_nh)
Exemple #14
0
# Do the same for the full histogram
n_hits_bins_uncut = (max(n_hits_proc_uncut) - min(n_hits_proc_uncut) + 1)/300.
print n_hits_bins_uncut
# Make histogram
hist(n_hits_proc_uncut, bins=n_hits_bins_uncut, histtype='step');

# <codecell>

hist(n_hits_proc_uncut, bins=n_hits_bins_uncut, histtype='step');
# Determine a first guess for the normalization constant
norm_const = len(n_hits_proc_np)
# Get the PDF
pdf_nh = Normalized(gaussian, (min(n_hits_proc_np), max(n_hits_proc_np)))
pdf_nh = Extended(pdf_nh)
# Do the fit
blh_nh = BinnedLH(pdf_nh, n_hits_proc_np, bins=n_hits_bins, extended=True)
m_nh = Minuit(blh_nh, mean=8000., sigma= 1000., N=norm_const, error_mean=100.,
    error_sigma=10.,limit_sigma=(0.,10000.), error_N=100., limit_mean=(0,100000))
m_nh.set_up(0.5)
m_nh.migrad()
blh_nh.show(m_nh);
# Generate and save figure for the number of photons
title("Number of photons detected by APD")
xlabel("Number of photons")
ylabel("Number of events")
savefig("n_ph.pdf")

# <codecell>

# Now repeat the procedure for the energy deposit
# Create a new figure