Skip to content
Snippets Groups Projects
Select Git revision
  • 89aec55b64861c7761b7736d59a04a9c2d5ea0f2
  • development default
  • production protected
3 results

trustrank.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    RunFit.py 2.40 KiB
    import os
    import sys
    import numpy as np
    import pandas as pd
    from PeakOTron import PeakOTron
    
    
    print("--------------------")
    print('EXAMPLE SIPM CALIBRATION RUN')
    print("--------------------")
    
    f_data = PeakOTron(verbose=False)
    f_data.SetMaxNPeaks(15) #SET MAX N PEAKS - THRESHOLDING REQUIRED WHERE LAMBDA INCORRECTLY ESTIMATED, OR FIT WILL GO ON FOREVER.
    f_data.SetMaxNDCRPeaks(6)
    
    out_dict = {}
    files_to_fit = []
    for root, dirs, files in os.walk("./SiPMSpectra/"):
         
            for file in files:
                
                if file.endswith(".dat") and "light" in file:
                    files_to_fit.append([file, os.path.join(root, file)])
                    
                
    print("Files to fit:")
    for i, (file, _) in enumerate(files_to_fit):
        print('File {0}: {1}'.format(i, file))
        
        
        
    for i, (file, path) in enumerate(files_to_fit):
    
        items = file.split('_')
    
        V = float(items[1].replace('U', '').replace('p', '.'))
        SiPM = items[3]
    
        print("\n Fit {0}: {1} \n".format(i, file))
        data = np.loadtxt(path)
        bw = 1.1*(data[:,0][1] -  data[:,0][0])
    
    
        f_data.Fit(data, 
              tau=20,  #SLOW PULSE COMPONENT TIME CONSTANT (ns)
              tgate=100, #GATE LENGTH (ns)
              r_fast=0.1, #FRACTION OF FAST PULSE COMPONENT 
              t_0 = 100, #INTEGRATION TIME BEFORE GATE (ns)
              pedestal = 0,
              prominence=1e-3,
              min_n_peak_evts=200,
              bandwidth_kde="optimise",
              bw=bw
    
            ) #BINNING RULE "knuth", "freedman", "scott" - use bw= #### to override. it is still required for DCR calculation. 
    
        if(not f_data.Failed()):
            f_data.PlotSummary(save_directory="./Results/{0}_init.png".format(os.path.splitext(file)[0]))
            f_data.PlotFit(scaled=False, title=file, xlabel="Charge [Vs]", y_limits=[1e7, 1e11], save_directory="./Results/{0}_fit.png".format(os.path.splitext(file)[0]))
    
            fit_out = {}
            fit_val, fit_err = f_data.GetFitResults()
            fit_out["SiPM"] = SiPM
            fit_out["V"] = V
    
            for key, value in fit_err.items():
                fit_out["d_{:s}".format(key)] = value
    
            fit_out.update(fit_val)
            out_dict.update()
            if out_dict == {}:
                for key in fit_out.keys():
                    out_dict[key] = []
    
            for key in fit_out.keys():
                out_dict[key].append(fit_out[key])
    
    
    
        else:
            print("Fit failed. \n")
    
    
    df = pd.DataFrame.from_dict(out_dict)
    df.to_csv("./Results/fit_results.csv")