import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import sys, os, re, glob
import uproot
from landaupy import landau
from scipy.stats import median_abs_deviation
# FUNZIONI DELLA GIULIA
def myLandau(x, x_mpv, xi,A):
return A*landau.pdf(x, x_mpv, xi)
def fittaLandau(x,y,vect, plotta, ax, fitcut_low = None,fitcut_up = None, **kwargs):
a = np.max(y)
x_mpvs = x[np.argmax(y)]
if not vect is None:
xi = median_abs_deviation(vect)/5
else:
xi = 60
xi = 20
print("DANGER !!! VALORE CHIAVATO A MANO")
# Default color
Lc = set(["c", "color"])
Lk = set(kwargs.keys())
if not Lc.intersection(Lk):
kwargs["c"] = "k"
# Default color
Lc = set(["ls", "linestyle"])
if not Lc.intersection(Lk):
kwargs["ls"] = "--"
p0 = [x_mpvs, xi, a]
if (fitcut_low is not None) & (fitcut_up is not None):
logi = (x >= fitcut_low) & (x <= fitcut_up)
else:
logi = (x > (x_mpvs-20*xi)) & (x < (x_mpvs+40*xi))
x_fit = x[logi]
y_fit = y[logi]
oP, pC = curve_fit(myLandau, xdata = x_fit, ydata = y_fit, sigma=np.sqrt(y_fit), p0 = p0, absolute_sigma=True)
if plotta:
x_plot = np.linspace(x[0], x[-1], 10000)
lab = f"Landau fit\nMPV = {oP[0]:.2f} $\pm$ {np.sqrt(np.diag(pC))[0]:.2f}\nMean = {np.sum(x*y)/y.sum():.2f}"
ax.plot(x_plot, myLandau(x_plot,*oP), label = lab, **kwargs)
linesStyle = {"ls" : "--", "c" : "grey"}
if not fitcut_low is None:
ax.axvline(fitcut_low, **linesStyle)
if not fitcut_up is None:
ax.axvline(fitcut_up, **linesStyle)
return oP, pC
fileToOpen = "run401238_histo.root"
with uproot.open(fileToOpen)["h1601"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_1 = b[0]
bins_1 = b[1]
binc_1 = bins_1[:-1] + (bins_1[1] - bins_1[0])/2
with uproot.open(fileToOpen)["h1602"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_2 = b[0]
bins_2 = b[1]
binc_2 = bins_2[:-1] + (bins_2[1] - bins_2[0])/2
h_primo = h_2
binc_primo = binc_2
fig, ax = plt.subplots(2, 1)
fig.set_size_inches(11, 6)
fig.subplots_adjust(hspace = .2)
ax = ax.flatten()
ax[0].set_title("PH distribution", fontsize = 20)
ax[0].plot(binc_1[5:], h_1[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 1")
ax[0].fill_between(binc_1[5:], h_1[5:], step = "mid", color = "lime", alpha = 1)
ax[1].plot(binc_2[5:], h_2[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 2")
ax[1].fill_between(binc_2[5:], h_2[5:], step = "mid", color = "lime", alpha = 1)
oP, pC = fittaLandau(binc_1, h_1, None, True, ax[0], 80, 400, ls = "--", c = 'k',)
oP, pC = fittaLandau(binc_2, h_2, None, True, ax[1], 50, 400, ls = "--", c = 'k',)
# ax.set_title("", fontsize = 16)
for i, a in enumerate(ax):
# a.set_xlabel("Energy [ADC]", fontsize = 14)
a.set_ylabel("Entries", fontsize = 14)
a.grid()
a.legend(fontsize = 14)
a.set_xlim(0, 800)
# fig.savefig(".pdf", format = "pdf", bbox_inches="tight", pad_inches=0)
plt.show()
DANGER !!! VALORE CHIAVATO A MANO DANGER !!! VALORE CHIAVATO A MANO
fileToOpen = "run401243_histo.root"
with uproot.open(fileToOpen)["h1601"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_1 = b[0]
bins_1 = b[1]
binc_1 = bins_1[:-1] + (bins_1[1] - bins_1[0])/2
with uproot.open(fileToOpen)["h1602"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_2 = b[0]
bins_2 = b[1]
binc_2 = bins_2[:-1] + (bins_2[1] - bins_2[0])/2
h_secondo = h_2
binc_secondo = binc_2
fig, ax = plt.subplots(2, 1)
fig.set_size_inches(11, 6)
fig.subplots_adjust(hspace = .2)
ax = ax.flatten()
ax[0].set_title("PH distribution", fontsize = 20)
ax[0].plot(binc_1[5:], h_1[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 1")
ax[0].fill_between(binc_1[5:], h_1[5:], step = "mid", color = "lime", alpha = 1)
ax[1].plot(binc_2[5:], h_2[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 2")
ax[1].fill_between(binc_2[5:], h_2[5:], step = "mid", color = "lime", alpha = 1)
oP, pC = fittaLandau(binc_1, h_1, None, True, ax[0], 80, 400, ls = "--", c = 'k',)
oP, pC = fittaLandau(binc_2, h_2, None, True, ax[1], 50, 400, ls = "--", c = 'k',)
# ax.set_title("", fontsize = 16)
for i, a in enumerate(ax):
# a.set_xlabel("Energy [ADC]", fontsize = 14)
a.set_ylabel("Entries", fontsize = 14)
a.grid()
a.legend(fontsize = 14)
a.set_xlim(0, 800)
# fig.savefig(".pdf", format = "pdf", bbox_inches="tight", pad_inches=0)
plt.show()
DANGER !!! VALORE CHIAVATO A MANO DANGER !!! VALORE CHIAVATO A MANO
fileToOpen = "run401252_histo.root"
with uproot.open(fileToOpen)["h1601"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_1 = b[0]
bins_1 = b[1]
binc_1 = bins_1[:-1] + (bins_1[1] - bins_1[0])/2
with uproot.open(fileToOpen)["h1602"] as f:
a = f.all_members
# Directly access the bins and height matrix
b = f.to_numpy()
# Extract bins and height matrix
h_2 = b[0]
bins_2 = b[1]
binc_2 = bins_2[:-1] + (bins_2[1] - bins_2[0])/2
h_cosmic = h_2
binc_cosmic = binc_2
fig, ax = plt.subplots(2, 1)
fig.set_size_inches(11, 6)
fig.subplots_adjust(hspace = .2)
ax = ax.flatten()
ax[0].set_title("PH distribution", fontsize = 20)
ax[0].plot(binc_1[5:], h_1[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 1")
ax[0].fill_between(binc_1[5:], h_1[5:], step = "mid", color = "lime", alpha = 1)
ax[1].plot(binc_2[5:], h_2[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Ph max - Mod. 2")
ax[1].fill_between(binc_2[5:], h_2[5:], step = "mid", color = "lime", alpha = 1)
oP, pC = fittaLandau(binc_1, h_1, None, True, ax[0], 100, 400, ls = "--", c = 'k',)
oP, pC = fittaLandau(binc_2, h_2, None, True, ax[1], 50, 400, ls = "--", c = 'k',)
# ax.set_title("", fontsize = 16)
for i, a in enumerate(ax):
# a.set_xlabel("Energy [ADC]", fontsize = 14)
a.set_ylabel("Entries", fontsize = 14)
a.grid()
a.legend(fontsize = 14)
a.set_xlim(0, 800)
# fig.savefig(".pdf", format = "pdf", bbox_inches="tight", pad_inches=0)
plt.show()
DANGER !!! VALORE CHIAVATO A MANO DANGER !!! VALORE CHIAVATO A MANO
fig, ax = plt.subplots(3, 1)
fig.set_size_inches(11, 6)
fig.subplots_adjust(hspace = .2)
ax = ax.flatten()
# ax[0].set_title("PH distribution", fontsize = 20)
ax[0].plot(binc_primo[5:], h_primo[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "First module crossed")
ax[0].fill_between(binc_primo[5:], h_primo[5:], step = "mid", color = "lime", alpha = 1)
ax[1].plot(binc_secondo[5:], h_secondo[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Second module crossed")
ax[1].fill_between(binc_secondo[5:], h_secondo[5:], step = "mid", color = "lime", alpha = 1)
ax[2].plot(binc_cosmic[5:], h_cosmic[5:], ds = "steps-mid", c = "darkgreen", lw = 2, label = "Cosmic")
ax[2].fill_between(binc_cosmic[5:], h_cosmic[5:], step = "mid", color = "lime", alpha = 1)
oP, pC = fittaLandau(binc_primo, h_primo, None, True, ax[0], 50, 400, ls = "--", c = 'k',)
oP, pC = fittaLandau(binc_secondo, h_secondo, None, True, ax[1], 50, 400, ls = "--", c = 'k',)
oP, pC = fittaLandau(binc_cosmic, h_cosmic, None, True, ax[2], 50, 400, ls = "--", c = 'k',)
# ax.set_title("", fontsize = 16)
for i, a in enumerate(ax):
# a.set_xlabel("Energy [ADC]", fontsize = 14)
a.set_ylabel("Entries", fontsize = 14)
a.grid()
a.legend(fontsize = 14)
a.set_xlim(0, 800)
ax[2].set_xlabel("Energy [ADC]", fontsize = 14)
fig.savefig("va1ta-first-second-cosmic.pdf", format = "pdf", bbox_inches="tight", pad_inches=0)
plt.show()
DANGER !!! VALORE CHIAVATO A MANO DANGER !!! VALORE CHIAVATO A MANO DANGER !!! VALORE CHIAVATO A MANO