Skip to content
Snippets Groups Projects
Commit 01b3ec0a authored by Antonello, Dr. Massimiliano's avatar Antonello, Dr. Massimiliano
Browse files

add an example in user

parent 785a5319
Branches
No related tags found
No related merge requests found
import os
import numpy as np
import pandas as pd
from PeakOTron import PeakOTron
import re
C_tau = lambda V, V_bd, V_0: (V - V_bd)/V_0
f_tau = lambda V, V_bd, V_0: -1/np.log((1-np.exp(C_tau(V, V_bd, V_0)*np.exp(-1)))/(1 - np.exp(C_tau(V, V_bd, V_0))))
V_bd_hmt = 26.1
V_0_hmt = 1.4
tau = 146.0 ##SLOW COMPONENT OF SIPM PULSE
t_0 = 100.0 ## PRE-INTEGRATION TIME
t_gate = 100.0 ## GATE LENGTH
bin_0=-100.0 ## SELECT FIRST BIN OF SPECTRUM (CAN BE AUTOMATIC)
truncate_nsigma0_up = 2.0 ## SCAN SPECTRUM FROM Q < Q_0 - 4 sigma_0
truncate_nsigma0_do = 2.0 ## EVALUATE SPECTRUM CHI2 IN Q_0 - x*sigma_0 < Q < Q_0 + 2*sigma_0
prefit_only=False ## FIT THE WHOLE SPECTRUM
out_dict = {}
files_to_fit = []
## Find all histograms in directory
folder = './data/ketek'
for root, dirs, files in os.walk(folder):
for file in files:
if file.endswith(".Wfm.csv"):
files_to_fit.append([file, os.path.join(root, file)])
## Print files.
print("Files to fit:")
for i, (file, _) in enumerate(files_to_fit):
print('File {0}: {1}'.format(i, file))
## Loop thorough files
for i, (file, path) in enumerate(files_to_fit):
items = file.split('_')
print("\n\n")
print("===============================================================")
print("FIT {:d} - {:s}".format(i, file))
print("===============================================================")
print("\n\n")
## Load files.
data = np.loadtxt(path, skiprows=0)
## Create a PeakOTron Fit Object.
f_data = PeakOTron(verbose=True)
match = re.search(r'scale(\d+)', file, re.IGNORECASE)
if match:
number = int(match.group(1))
print("Extracted number:", number)
else:
print("No number found in the string.")
V = float(number/100)
print(V)
# V = 41.0
f_tau_hmt = f_tau(V, V_bd_hmt, V_0_hmt)
## Perform fit.
f_data.Fit(data,
tau=tau, #SLOW PULSE COMPONENT TIME CONSTANT (ns)
t_gate=t_gate, #GATE LENGTH (ns)
t_0 = t_0, #INTEGRATION TIME BEFORE GATE (ns)
tau_R=f_tau_hmt*tau,
bin_0 = None,
truncate_nsigma0_up = None,
truncate_nsigma0_do = None
)
# f_data.PlotPeaks()
f_data.PlotFit(xlabel="$q_0$",
title="Charge Spectrum {}".format(file)
)
prefit_val, prefit_err = f_data.GetPrefitResults(bin_units=False)
print("Prefit values", prefit_val, prefit_err)
fit_val, fit_err = f_data.GetFitResults(bin_units=False)
print("Fit values", fit_val, fit_err)
fit_out = {}
for key, value in fit_val.items():
fit_out["{:s}".format(key)] = value
for key, value in fit_err.items():
fit_out["d_{:s}".format(key)] = value
for key, value in prefit_val.items():
fit_out["prefit_{:s}".format(key)] = value
for key, value in prefit_err.items():
fit_out["prefit_d_{:s}".format(key)] = value
df = pd.DataFrame.from_dict([fit_out])
df.to_csv("{}/fit_results_{:s}.csv".format(folder,file))
# dump(f_data, "{}/dump_{}.csv".format(folder,file))
# break
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment