Ejemplo n.º 1
0
def single_tube(argv):
    allFiles = getFiles("/Users/yumiao/Documents/Works/Simulation/PMT_Paramters/202008Updates/TTS-caimei-data-update/", ".txt")

    # read matches
    nnvt_id, nnvt_sn = read_match("/Users/yumiao/Documents/Works/Simulation/PMT_Paramters/202008Updates/TTS-caimei-data-update/instruction/valid_NNVT.csv")
    ham_id, ham_sn = read_match("/Users/yumiao/Documents/Works/Simulation/PMT_Paramters/202008Updates/TTS-caimei-data-update/instruction/valid_HAM.csv")


    graph_dyn = TGraphErrors()
    graph_mcp = TGraphErrors()

    dyn_arr, mcp_arr = [[] for i in range(7)], [[] for i in range(7)]

    nnvt_num, ham_num = 0, 0

    """ loop all files and generate pdf output """
    for idx, afile in enumerate(allFiles):
        print("Processing scan file No {:d} -> {} " .format(idx, afile))
        data1 = pd.read_csv(afile, header=[0], nrows=24, sep="\s+")  ## TTS
        #data1 = pd.read_csv(afile, header=[25], nrows=24, sep='\s+') ## TT

        graph_dyn.Set(0)
        graph_mcp.Set(0)

        """ seperate MCP and Dynode PMTs """
        #scanid = int(re.sub("\D", "", afile))
        pattern = re.compile(r'(?<=scan)\d+')
        scanid = int(pattern.findall(afile)[0])

        if scanid in nnvt_id:
            print("It's a MCP PMT !!!")
            nnvt_num += 1
            
            """ average phi direction """
            for itheta in range(7):
                for iphi in range(24):
                    #print(float(np.array(data1.iloc[iphi, itheta+1])))
                    #mcp_arr[itheta].append( float(np.array(data1.iloc[iphi, itheta+1])) )
                    mcp_arr[itheta].append( np.sqrt(float(np.array(data1.iloc[iphi, itheta+1]))**2-2.1**2) )
            #for itheta in range(1, 8):
            #    mean = np.array(data1.iloc[::, itheta]).mean()
            #    sigma = np.std( np.array(data1.iloc[::, itheta]) )
            #    graph_mcp.SetPoint(itheta-1, itheta, mean)
            #    graph_mcp.SetPointError(itheta-1, 0, sigma)
            
            """average theta direction"""
            """
            for iphi in range(24):
                mean = np.array(data1.iloc[iphi, 1:8]).mean()
                sigma = np.std(np.array(data1.iloc[iphi, 1:8]))
                graph_mcp.SetPoint(iphi, iphi*15, mean)
                graph_mcp.SetPointError(iphi, 0, sigma)
            #plt.plot(graph_mcp.GetX(), graph_mcp.GetY(), "o-", ms=3)
            """

        elif scanid in ham_id:
            print("It's a Dynode PMT !!!")
            ham_num += 1
            for itheta in range(7):
                for iphi in range(24):
                    dyn_arr[itheta].append( np.sqrt(float(np.array(data1.iloc[iphi, itheta+1]))**2-2.1**2) )
            """ average phi direction """
            #for itheta in range(1, 8):
            #    mean = np.array(data1.iloc[::, itheta]).mean()
            #    sigma = np.std( np.array(data1.iloc[::, itheta]) )
            #    print("It's a Dynode PMT !!!")
            #    graph_dyn.SetPoint(itheta-1, itheta, mean)
            #    graph_dyn.SetPointError(itheta-1, 0, sigma)

            """average theta direction"""
            """
            for iphi in range(24):
                mean = np.array(data1.iloc[iphi, 1:8]).mean()
                sigma = np.std(np.array(data1.iloc[iphi, 1:8]))
                graph_dyn.SetPoint(iphi, iphi*15, mean)
                graph_dyn.SetPointError(iphi, 0, sigma)
            plt.plot(graph_dyn.GetX(), graph_dyn.GetY(), "o-", ms=3)
            """

    dyn_pos = [13, 28, 41, 55, 66, 79, 85]
    mcp_pos = [14, 30, 42.5, 55, 67, 77.5, 85]

    dyn_pos = [1, 2, 3, 4, 5, 6, 7]

    print([np.array(dyn_arr[i]).mean() for i in range(7)])

    def draw_plot(data, pos, edge_color, fill_color): 
        bp = ax.boxplot(data, positions=pos, patch_artist=True, sym="") 

        for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']: 
            plt.setp(bp[element], color=edge_color) 

        for patch in bp['boxes']: 
            patch.set(facecolor=fill_color)  

        return bp

    fig, ax = plt.subplots() 
    bp1 = draw_plot(dyn_arr, dyn_pos, 'red', 'tan') 
    bp2 = draw_plot(mcp_arr, dyn_pos, 'blue', 'cyan') 
    ax.set_ylim(0, 10) 
    ax.legend([bp1["boxes"][0], bp2["boxes"][0]], ["dynode", "mcp"], loc='best')
    ax.set_xlabel("theta/deg")
    ax.set_ylabel("TTS mean/ns")
    plt.show() 

    #plt.violinplot(dyn_arr, dyn_pos, points=1000, widths=0.8, showmeans=True, showextrema=True, showmedians=True)
    #plt.violinplot(mcp_arr, mcp_pos, points=1, widths=0.3, showmeans=True, showextrema=True, showmedians=True)

    #plt.violinplot(, pos, points=20, widths=0.3,
    #                  showmeans=True, showextrema=True, showmedians=True)
    #plt.set_title('Custom violinplot 1', fontsize=fs)

    #plt.title("Dyn PMT average all theta")
    #plt.xlabel("phi")
    #plt.ylabel("hittime/ns")
    #plt.show()
    #plt.savefig(argv[1])

    print(" Dynode PMT Number: %d" % ham_num)
    print(" MCP PMT Number: %d" % nnvt_num)
Ejemplo n.º 2
0
def main():
    args = parser()

    filename = args.filename

    os.system('mkdir -p rootfiles')
    os.system('mkdir -p plots')

    f = tables.open_file(filename)
    print(f"Successfully opened file: {filename}.")

    data = f.get_node('/data')
    print("Read /data")

    event_ids = getDataCol('event_id', data)
    waveforms = getDataCol('waveform', data)
    times = getDataCol('time', data)
    timestamps = getDataCol('timestamp', data)
    thresFlags = getDataCol('thresholdFlags', data)
    i_1V1 = getDataCol('i_1V1', data)
    i_1V35 = getDataCol('i_1V35', data)
    i_1V8 = getDataCol('i_1V8', data)
    i_2V5 = getDataCol('i_2V5', data)
    i_3V3 = getDataCol('i_3V3', data)
    v_1V1 = getDataCol('v_1V1', data)
    v_1V35 = getDataCol('v_1V35', data)
    v_1V8 = getDataCol('v_1V8', data)
    v_2V5 = getDataCol('v_2V5', data)
    v_3V3 = getDataCol('v_3V3', data)

    nsamples = len(waveforms[0])
    fq = np.linspace(0, 240, nsamples)

    gmean = np.mean(waveforms[0])
    globalmean = np.mean(waveforms)
    print(np.mean(waveforms[0]))

    c = TCanvas("c", "c", 800, 600)
    c.Draw()
    c.SetGrid()

    of = TFile('rootfiles/{0}.root'.format(filename.split('.h', 1)[0]),
               "RECREATE")

    h = TH1D('qdist', 'qdist;ADC count;Entry', 500, -100, 900)
    hpk = TH1D('peak', 'peak;ADC count;Entry', 500, -100, 900)
    havg = TH1D('avgwf', 'Averaged Waveform;Sampling Bin;ADC count', nsamples,
                0, nsamples)
    havgfft = TH1D('avgfft',
                   'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]',
                   int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
    hifft = TH1D('Subtwf', 'Averaged Waveform;Sampling Bin;ADC count',
                 nsamples, 0, nsamples)
    hsfft = TH1D('Subtfft',
                 'FFT Averaged Waveform;Frequency [MHz];Amplitude [LSB]',
                 int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
    hsspe = TH1D('qdist_subt', 'qdist_subt;ADC count;Entry', 500, -100, 900)
    hmax = TH1D('hmax', 'hmax;ADC count;Entry', 500, -100, 900)
    bsfluc = TH1D('bsfluc', 'bsfluc;ADC count;Entry', 100,
                  int(globalmean) - 50,
                  int(globalmean) + 50)
    bsshift = TGraphErrors()
    bsshift.SetName('bsshift')
    bsshift.SetTitle('bsshift;Event Number;Baseline [LSB]')
    nrshift = TGraphErrors()
    nrshift.SetName('nrshift')
    nrshift.SetTitle('NoiseRateShift;Event Number;Noise Rate [Hz]')
    rmsshift = TGraphErrors()
    rmsshift.SetName('rmsshift')
    rmsshift.SetTitle('RMSNoiseShift;Event Number;RMS Noise [LSB]')
    rmstime = TGraphErrors()
    rmstime.SetName('rmstime')
    rmstime.SetTitle('rmstime;Unix time [s];RMS Noise [LSB]')
    powerc = TGraphErrors()
    powerc.SetName('powerc')
    powerc.SetTitle('powerc;Event Number;Power Consumption [W]')
    h_powerc = TH1D('h_powerc', 'h_powerc;Power Consumption [W];Entry', 100, 0,
                    5)

    winmin = args.minimum
    winmax = args.minimum + args.window
    bsstart = args.baselineEst

    print(f'Total #Events: {len(waveforms)}')
    print(
        f'Setting... Window [{winmin}:{winmax}] and Pedestal start from {bsstart}.\n'
    )

    topdir = gDirectory.GetDirectory(gDirectory.GetPath())
    subdir = topdir.mkdir("Waveforms")
    subdir2 = topdir.mkdir("FFT")
    subdir3 = topdir.mkdir("proj")
    thrdir = topdir.mkdir("thresFlags")

    fltwfs = []
    starttimestamp = timestamps[0]

    procN = len(waveforms)
    if args.nevents > 0:
        procN = args.nevents

    for i in tqdm(range(procN)):
        if i < args.nskip:
            continue

        waveform = waveforms[i]
        timestamp = timestamps[i]

        bsfluc.Fill(np.mean(waveform))
        n = bsshift.GetN()
        bsshift.Set(n + 1)
        bsshift.SetPoint(n, i, np.mean(waveform))
        bsshift.SetPointError(n, 0, np.std(waveform))

        n = rmsshift.GetN()
        rmsshift.Set(n + 1)
        rmsshift.SetPoint(n, i, np.std(waveform))
        rmsshift.SetPointError(n, 0, 0)

        hmax.Fill(np.max(waveform) - np.mean(waveform))

        timeinterval = ((timestamp - starttimestamp - i * nsamples) / 240.e6 -
                        i * 0.501)
        nr = 0
        nrerr = 0
        if timeinterval > 0:
            nr = (i + 1) / timeinterval
            nrerr = np.sqrt(i + 1) / timeinterval
        n = nrshift.GetN()
        nrshift.Set(n + 1)
        nrshift.SetPoint(n, i, nr)
        nrshift.SetPointError(n, 0, nrerr)

        # FFT & IFFT ###
        F_abs_amp = doFFT(waveform)

        F = np.fft.fft(waveform)
        F2 = np.copy(F)
        fc = 60
        df = 2
        F2[(fq > fc - df) & (fq < fc + df)] = 0
        F2[(fq > 2 * fc - df) & (fq < 2 * fc + df)] = 0
        F2[(fq > 240 - fc - df) & (fq < 240 - fc + df)] = 0
        F2_abs = np.abs(F2)
        F2_abs_amp = F2_abs / len(waveform) * 2
        F2_abs_amp[0] = F_abs_amp[0] / 2

        F2_ifft = np.fft.ifft(F2)
        F2_ifft_real = F2_ifft.real
        ######

        baseline_mean = np.mean(waveform[bsstart:nsamples])
        if args.fixbs:
            baseline_mean = globalmean

        center = int(baseline_mean)
        proj = TH1D(f'proj{i}', f'Projection waveform{i};ADC count;Entry', 300,
                    center - 150, center + 150)
        selected = waveform[waveform < np.mean(waveform) + 10]
        for j in range(len(selected)):
            proj.Fill(selected[j])
        #proj.Fit("gaus")
        #if len(selected) > 0:
        #    f = proj.GetFunction('gaus')
        #    norm = f.GetParameter(0)
        #    mean = f.GetParameter(1)
        #    sigma = f.GetParameter(2)

        reduced_waveform = waveform - baseline_mean
        scale = (nsamples - bsstart) / (winmax - winmin)
        #h.Fill(sum(waveform[winmin:winmax])-sum(waveform[bsstart:nsamples])/scale)
        h.Fill(sum(reduced_waveform[winmin:winmax]))

        hsspe.Fill(
            np.sum(F2_ifft_real[winmin:winmax]) - baseline_mean *
            (winmax - winmin))

        hpk.Fill(
            max(waveform[winmin:winmax]) - np.mean(waveform[bsstart:nsamples]))

        hfft = TH1D(f'FFT{i}', 'FFT{i};Frequency [MHz];Amplitude [LSB]',
                    int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
        hfft2 = TH1D(f'FFT2{i}', 'FFT2{i};Frequency [MHz];Amplitude [LSB]',
                     int(len(fq) / 2) + 1, 0, 120 + 240 / (nsamples - 1))
        for j in range(int(len(F_abs_amp) / 2. + 1)):
            hfft.Fill(fq[j], F_abs_amp[j])
            hfft2.Fill(fq[j], F2_abs_amp[j])

        h2 = TH1D(f'w{i}', 'Waveform{i};Sampling Bin;ADC count', nsamples, 0,
                  nsamples)
        for j in range(len(waveform)):
            h2.Fill(j, waveform[j])
        #h2.Draw("hist")

        #if np.max(F2_abs_amp[1:int(len(F2_abs_amp)/2.+1)]) > 0.5:
        #    c.SetLogy(0)
        #    h2.SetLineColor(4)
        #    h2.Draw("hist")
        #    c.Print(f"plots/w{i}.pdf(")
        #    c.SetLogy(1)
        #    hfft.SetLineColor(4)
        #    hfft.Draw("hist")
        #    c.Print(f"plots/w{i}.pdf)")

        htot = TH1D(f'thresflags{i}',
                    'ThresholdFlags{i};Sampling Bin;Threshold Flag', nsamples,
                    0, nsamples)
        for j in range(len(thresFlags[i])):
            htot.Fill(j, thresFlags[i][j])

        if max(waveform) - baseline_mean < args.threshold:
            continue

        fltwfs.append(waveform)

        if args.silent:
            del hfft
            del hfft2
            del h2
            del htot
            del proj
            del waveform
            del timestamp
            del F
            del F2
            gc.collect()
            continue

        subdir2.cd()
        hfft.Write()

        subdir.cd()
        h2.Write()

        subdir3.cd()
        proj.Write()

        thrdir.cd()
        htot.Write()

    print('')

    for i in range(len(i_1V1)):
        internal_power = v_1V1[i] * i_1V1[i] + v_1V35[i] * i_1V35[i] + v_1V8[
            i] * i_1V8[i] + v_2V5[i] * i_2V5[i] + v_3V3[i] * i_3V3[i]
        n = powerc.GetN()
        powerc.Set(n + 1)
        powerc.SetPoint(n, i, internal_power * 1e-3)
        powerc.SetPointError(n, 0, 0)
        h_powerc.Fill(internal_power * 1e-3)

    avgfltwfs = np.mean(fltwfs, axis=0)
    for i in range(len(avgfltwfs)):
        havg.Fill(i, avgfltwfs[i])

    Favg_abs_amp = doFFT(avgfltwfs)

    Favg = np.fft.fft(avgfltwfs)
    Favg2 = np.copy(Favg)
    Favg2[(fq > 59) & (fq < 61)] = 0
    Favg2[(fq > 119) & (fq < 121)] = 0
    Favg2[(fq > 179) & (fq < 181)] = 0

    Favg2_abs = np.abs(Favg2)
    Favg2_abs_amp = Favg2_abs / len(avgfltwfs) * 2
    Favg2_abs_amp[0] = Favg_abs_amp[0] / 2

    Favg2_ifft = np.fft.ifft(Favg2)
    Favg2_ifft_real = Favg2_ifft.real

    for i in range(int(len(Favg_abs_amp) / 2. + 1)):
        havgfft.Fill(fq[i], Favg_abs_amp[i])

    for i in range(int(len(Favg2_abs_amp) / 2. + 1)):
        hsfft.Fill(fq[i], Favg2_abs_amp[i])

    for i in range(len(Favg2_ifft_real)):
        hifft.Fill(i, Favg2_ifft_real[i])

    topdir.cd()
    h.Write()
    hpk.Write()
    havg.Write()
    havgfft.Write()
    hsfft.Write()
    hifft.Write()
    hsspe.Write()
    hmax.Write()
    bsfluc.Write()
    bsshift.Write()
    nrshift.Write()
    rmsshift.Write()
    powerc.Write()
    h_powerc.Write()

    of.Close()
    f.close()