Ejemplo n.º 1
0
def noise_a_chn(rmsdata,
                chnno,
                fft_en=True,
                fft_s=2000,
                fft_avg_cycle=50,
                wibno=0,
                fembno=0):
    asicchn = chnno % 16
    chnrmsdata = rmsdata[0][7][asicchn]
    feed_loc = rmsdata[0][8]
    len_chnrmsdata = len(chnrmsdata)
    if (len_chnrmsdata > 200000):
        len_chnrmsdata = 200000
    chnrmsdata = chnrmsdata[0:len_chnrmsdata]
    rms = np.std(chnrmsdata[0:10000])
    ped = np.mean(chnrmsdata[0:10000])
    if len(feed_loc) > 2:
        data_slice = chnrmsdata
        data_200ms_slice = chnrmsdata[0:200000:200]
    else:
        data_slice = chnrmsdata
        data_200ms_slice = chnrmsdata[0:200000:200]

    avg_cycle_l = 2
    if (len_chnrmsdata >= 200000):
        fft_s_l = 200000 // avg_cycle_l
    else:
        fft_s_l = len(chnrmsdata)

    if (fft_en):
        f, p = chn_rfft_psd(chnrmsdata, fft_s=fft_s, avg_cycle=fft_avg_cycle)
        f_l, p_l = chn_rfft_psd(chnrmsdata,
                                fft_s=fft_s_l,
                                avg_cycle=avg_cycle_l)
    else:
        f = None
        p = None
        f_l = None
        p_l = None


#   data after highpass filter
    flt_chn_data = hp_flt_applied(chnrmsdata,
                                  fs=2000000,
                                  passfreq=1000,
                                  flt_order=3)
    flt_chn_data = np.array(flt_chn_data) + ped
    hfped = ped
    hfrms = np.std(flt_chn_data)
    if (fft_en):
        hff, hfp = chn_rfft_psd(flt_chn_data,
                                fft_s=fft_s,
                                avg_cycle=fft_avg_cycle)
        hff_l, hfp_l = chn_rfft_psd(flt_chn_data,
                                    fft_s=fft_s_l,
                                    avg_cycle=avg_cycle_l)
    else:
        hff = None
        hfp = None
        hff_l = None
        hfp_l = None

    if len(feed_loc) > 2:
        #hfdata_slice = flt_chn_data[feed_loc[0]:feed_loc[0] + 5000]
        hfdata_slice = flt_chn_data
    else:
        hfdata_slice = flt_chn_data
    hfdata_100us_slice = flt_chn_data[0:100000:200]

    #   data after stuck code filter
    tmp_data = []
    lenonechn_data = len(chnrmsdata)
    for tmp in chnrmsdata:
        if (tmp % 64 == 63) or (tmp % 64 == 0) or (tmp % 64 == 1) or (
                tmp % 64 == 62) or (tmp % 64 == 2):
            pass
        else:
            tmp_data.append(tmp)
    len_tmp_data = len(tmp_data)
    unstk_ratio = 1.0 * len_tmp_data / lenonechn_data
    #    if ( unstk_ratio > 0.95 ):
    #        stuck_type = "Small"
    #    elif ( unstk_ratio > 0.8 ):
    #        stuck_type = "Middle"
    #    else:
    #        stuck_type = "Large"
    sfrms = np.std(tmp_data[0:10000])
    sfped = np.mean(tmp_data[0:10000])

    chn_noise_paras = [
        chnno, rms, ped, data_slice, data_200ms_slice, f, p, hfrms, hfped,
        hfdata_slice, hfdata_100us_slice, hff, hfp, sfrms, sfped, unstk_ratio,
        f_l, p_l, hff_l, hfp_l, wibno, fembno
    ]
    return chn_noise_paras
Ejemplo n.º 2
0
def noise_a_chn_fast(rmsdata,
                     chnno,
                     fft_en=True,
                     fft_s=2000,
                     fft_avg_cycle=50,
                     wibno=0,
                     fembno=0):
    asicchn = chnno % 16
    chnrmsdata = rmsdata[0][7][asicchn][0:20000]
    feed_loc = rmsdata[0][8]
    len_chnrmsdata = len(chnrmsdata)
    if (len_chnrmsdata > 200000):
        len_chnrmsdata = 200000
    rms = np.std(chnrmsdata[0:10000])
    ped = np.mean(chnrmsdata[0:10000])
    data_slice = chnrmsdata[feed_loc[0]:feed_loc[1]]
    data_200ms_slice = chnrmsdata[0:20000:20]

    avg_cycle_l = 1
    if (len(chnrmsdata) >= 400000):
        fft_s_l = 400000 // avg_cycle_l

    if (False):
        f, p = chn_rfft_psd(chnrmsdata, fft_s=fft_s, avg_cycle=fft_avg_cycle)
        f_l, p_l = chn_rfft_psd(chnrmsdata,
                                fft_s=fft_s_l,
                                avg_cycle=avg_cycle_l)
    else:
        f = None
        p = None
        f_l = None
        p_l = None

    if (False):
        flt_chn_data = hp_flt_applied(chnrmsdata,
                                      fs=2000000,
                                      passfreq=1000,
                                      flt_order=3)
        flt_chn_data = np.array(flt_chn_data) + ped
        hfped = ped
        hfrms = np.std(flt_chn_data)
        if (fft_en):
            hff, hfp = chn_rfft_psd(flt_chn_data,
                                    fft_s=fft_s,
                                    avg_cycle=fft_avg_cycle)
            hff_l, hfp_l = chn_rfft_psd(flt_chn_data,
                                        fft_s=fft_s_l,
                                        avg_cycle=avg_cycle_l)
        else:
            hff = None
            hfp = None
            hff_l = None
            hfp_l = None

        hfdata_slice = flt_chn_data[feed_loc[0]:feed_loc[1]]
        hfdata_100us_slice = flt_chn_data[0:100000:200]
    else:
        hfped = ped
        hfrms = rms
        hff = None
        hfp = None
        hff_l = None
        hfp_l = None
        hfdata_slice = data_slice
        hfdata_100us_slice = chnrmsdata[0:10000:10]

    tmp_data = []
    lenonechn_data = len(chnrmsdata)
    for tmp in chnrmsdata:
        if (tmp % 64 == 63) or (tmp % 64 == 0) or (tmp % 64 == 1) or (
                tmp % 64 == 62) or (tmp % 64 == 2):
            pass
        else:
            tmp_data.append(tmp)
    len_tmp_data = len(tmp_data)
    unstk_ratio = 1.0 * len_tmp_data / lenonechn_data
    sfrms = np.std(tmp_data[0:10000])
    sfped = np.mean(tmp_data[0:10000])

    chn_noise_paras = [
        chnno, rms, ped, data_slice, data_200ms_slice, f, p, hfrms, hfped,
        hfdata_slice, hfdata_100us_slice, hff, hfp, sfrms, sfped, unstk_ratio,
        f_l, p_l, hff_l, hfp_l, wibno, fembno
    ]
    return chn_noise_paras
Ejemplo n.º 3
0
def All_FEMBs_results(path,
                      rundir,
                      APA="ProtoDUNE",
                      APAno=1,
                      gain=3,
                      mode=0,
                      wib_np=[0, 1, 2, 3, 4],
                      tp=2,
                      jumbo_flag=True,
                      feed_freq=500,
                      hp_filter=False,
                      t_no=1):
    apamap.APA = APA
    runpath = path + rundir + "/"
    start = timer()
    for root, dirs, files in os.walk(runpath):
        break

    alldata = []
    allresult = []
    for wib in wib_np:
        wibinfo_rt = "WIB" + format(wib,
                                    "02d") + "step" + str(gain) + str(mode)
        path_wib_rt = runpath + wibinfo_rt + "/"
        wibinfo_ln = "WIB" + format(wib,
                                    "02d") + "step" + str(gain) + str(mode)
        path_wib_ln = runpath + wibinfo_ln + "/"
        missing_wib = False
        if os.path.isdir(path_wib_rt):
            wibinfo = wibinfo_rt
            path_wib = path_wib_rt
            #print "%s"%path_wib
        elif os.path.isdir(path_wib_ln):
            wibinfo = wibinfo_ln
            path_wib = path_wib_ln
            #print "%s"%path_wib
        else:
            path_wib = path_wib_ln
            print "%s, path doesn't exist!!!" % path_wib
            missing_wib = True

        if (missing_wib == False):
            for root1, dirs1, rawfiles in os.walk(path_wib):
                break
            print("APAno", APAno)
            femb_pos_np = femb_position(APAno)
            #print("femb_pos_np",femb_pos_np)
            apa_femb_loc, X_sort, V_sort, U_sort = apamap.apa_femb_mapping_pd()
            for rawfile in rawfiles:
                rawfilep = path_wib + rawfile
                #print("rawfilep",rawfilep)
                if (rawfilep.find(".bin") >= 0) and (rawfilep.find(wibinfo) >=
                                                     0):
                    wib = int(rawfilep[(rawfilep.find("WIB") +
                                        3):(rawfilep.find("WIB") + 5)])
                    femb = int(rawfilep[rawfilep.find("FEMB") + 4])
                    chip = int(rawfilep[rawfilep.find("CHIP") + 4])
                    filetp = int(rawfilep[rawfilep.find("CHIP") + 6])
                    if os.path.isfile(rawfilep) and (filetp == tp):
                        with open(rawfilep, 'rb') as f:
                            raw_data = f.read()
                            len_file = len(raw_data)
                        for apa_loc in femb_pos_np:
                            if (apa_loc[1] == "WIB" + format(wib, "02d") +
                                    "_" + "FEMB" + str(femb)):
                                break
                        smps = (len_file - 1024) / 2 / 16
                        if (smps > 100000):
                            smps = 100000
                        elif (smps > 20000):
                            smps = 20000
                        elif (smps > 10000):
                            smps = 10000
                        else:
                            pass
                        chn_data, feed_loc, chn_peakp, chn_peakn = raw_convertor_peak(
                            raw_data, smps, jumbo_flag)
                        for chn in range(16):
                            fembchn = chip * 16 + chn
                            for apa_info in apa_femb_loc:
                                if int(apa_info[1]) == fembchn:
                                    break

                            rms_data = []
                            for oneloc in feed_loc[0:-1]:
                                rms_data = rms_data + chn_data[chn][
                                    oneloc + 100:oneloc + feed_freq]

                            if (hp_filter == True):
                                flt_tmp_data = hp_flt_applied(chn_data[chn],
                                                              fs=2000000,
                                                              passfreq=1000,
                                                              flt_order=2)
                                flt_tmp_data = np.array(
                                    flt_tmp_data) + np.mean(rms_data)
                                rms_data_tmp = []
                                for oneloc in feed_loc[0:-1]:
                                    rms_data_tmp = rms_data_tmp + (
                                        flt_tmp_data[oneloc + 100:oneloc +
                                                     feed_freq].tolist())
                                rms_data = rms_data_tmp
                                chn_full_data = flt_tmp_data
                            else:
                                rms_data = rms_data
                                chn_full_data = chn_data[chn]

                            rms_data_raw = rms_data

                            rms_len = len(rms_data)
                            raw_rms0 = np.std(rms_data[0:int(rms_len / 2)])
                            raw_rms1 = np.std(rms_data[int(rms_len /
                                                           2):rms_len])
                            if (raw_rms0 < raw_rms1):
                                rms_data = rms_data[0:int(rms_len / 2)]
                            else:
                                rms_data = rms_data[int(rms_len / 2):rms_len]
                            raw_mean = np.mean(rms_data)
                            raw_rms = np.std(rms_data)

                            sf_raw_rms = []
                            for tmp in rms_data:
                                if (tmp % 64 == 63) or (tmp % 64 == 0) or (
                                        tmp % 64 == 1) or (tmp % 64
                                                           == 62) or (tmp % 64
                                                                      == 2):
                                    pass
                                else:
                                    sf_raw_rms.append(tmp)
                            if (len(sf_raw_rms) > 2):
                                sf_mean = np.mean(sf_raw_rms)
                                sf_rms = np.std(sf_raw_rms)
                            else:
                                sf_rms = raw_rms
                                sf_mean = raw_mean
                            sf_ratio = (len(sf_raw_rms)) * 1.0 / (
                                len(rms_data))

                            chn_peakp_avg = np.mean(chn_peakp[chn])
                            chn_peakn_avg = np.mean(chn_peakn[chn])
                            alldata.append( [apa_loc, apa_info, wib, femb, chip, \
                                             chn, raw_mean, raw_rms, sf_mean, sf_rms, \
                                             sf_ratio, chn_peakp_avg, chn_peakn_avg, rms_data_raw, chn_full_data, \
                                             feed_loc, chn_peakp[chn], chn_peakn[chn] ] )

                            pulsemax_data = np.max(
                                chn_full_data[feed_loc[0]:feed_loc[0] + 100])
                            pulsemax_data_loc = np.where(
                                chn_full_data[feed_loc[0]:feed_loc[0] +
                                              100] == pulsemax_data)
                            ppeak_oft_feed = pulsemax_data_loc[0][0]

                            pulsemin_data = np.min(
                                chn_full_data[feed_loc[0]:feed_loc[0] + 100])
                            pulsemin_data_loc = np.where(
                                chn_full_data[feed_loc[0]:feed_loc[0] +
                                              100] == pulsemin_data)
                            npeak_oft_feed = pulsemin_data_loc[0][0]

                            allresult.append( [apa_loc[0], apa_loc[1], apa_info[0], apa_info[1], apa_info[2], apa_info[3], \
                                               wib, femb, chip, chn, \
                                               raw_mean, raw_rms, len(rms_data), \
                                               sf_mean, sf_rms, len(sf_raw_rms), sf_ratio, \
                                               chn_peakp_avg, chn_peakn_avg, ppeak_oft_feed, npeak_oft_feed ] )
            print "time passed = %d" % (timer() - start)

    import pickle
    resultpath = path + "results/" + rundir + "/"

    if (os.path.exists(resultpath)):
        pass
    else:
        try:
            os.makedirs(resultpath)
        except OSError:
            print "Error to create a folder"
            exit()

    savefile = resultpath + apamap.APA + "_APA" + str(APAno) + '_gain' + str(
        gain) + "_tp" + str(tp) + '_results.bin'
    if (os.path.isfile(savefile)):
        pass
    else:
        with open(savefile, "wb") as fp:
            pickle.dump(allresult, fp)

    if gain == 3:
        str_gain = 25.0
        egain = 78
    elif gain == 1:
        str_gain = 14.0
        egain = 145
    elif gain == 2:
        str_gain = 7.8
        egain = 250
    elif gain == 0:
        str_gain = 4.7
        egain = 425

    if tp == 3:
        str_tp = 2.0
    elif tp == 1:
        str_tp = 3.0
    elif tp == 2:
        str_tp = 0.5
    elif tp == 0:
        str_tp = 1.0

    item_n = ["APA_LOC", "WIB_FEMB", "Wire", "FEMBchn(0-127)", "FEMBasic(1-8)", "ASICchn(0-15)", \
              "WIB(0-4)", "FEMB(0-4)", "ASIC(0-7)", "ASICchn(0-15)", \
              "Baseline (Raw data) \ bin", "RMS(raw data) \ bin", "Sample count (raw data)", \
              "Baseline (SF data) \ bin", "RMS(SF data) \ bin", "Sample count (SF data)", "SF ratio", \
              "Pos_Amplitude \ bin", "Neg_Amplitude \ bin", "1st_PPeak_LOC \ bin", "1st_NPeak_LOC \ bin", \
              "Gain \ mV/fC", "Shape Time / us", "Inverted gain \ e-/bin" ]

    fe_paras = [str_gain, str_tp, egain]
    strdate_pos = resultpath.find("Rawdata_")
    strdate = resultpath[strdate_pos:strdate_pos + 18]
    csvfile = resultpath + "Test" + format(
        t_no, "03d"
    ) + "_" + strdate + "_" + rundir + "_" + apamap.APA + "_APA" + str(
        APAno) + '_gain' + str(gain) + "_tp" + str(tp) + '_results.csv'

    with open(csvfile, 'w') as fp:
        fp.write(",".join(str(i) for i in item_n) + "," + "\n")
        for x in allresult:
            fp.write(",".join(str(i) for i in x) + "," +
                     ",".join(str(i) for i in fe_paras) + "," + "\n")

    return alldata
Ejemplo n.º 4
0
def raw_convertor_filtered(raw_data, smps, jumbo_flag=True):
    chn_data, feed_loc = raw_convertor_feedloc(raw_data, smps, jumbo_flag)
    if (len(feed_loc)) > 2:
        chn_peakp = [
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
        ]
        chn_peakn = [
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
        ]
        for tmp in range(len(feed_loc) - 1):
            for chn in range(16):
                chn_peakp[chn].append(
                    np.max(chn_data[chn][feed_loc[tmp]:feed_loc[tmp] + 100]))
                chn_peakn[chn].append(
                    np.min(chn_data[chn][feed_loc[tmp]:feed_loc[tmp] + 100]))
    else:
        chn_peakp = None
        chn_peakn = None

    flt_chn_data = [
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
    ]
    for i in range(len(flt_chn_data)):
        flt_chn_data[i] = hp_flt_applied(chn_data[i],
                                         fs=2000000,
                                         passfreq=1000,
                                         flt_order=3)
        flt_chn_data[i] = np.array(flt_chn_data[i]) + np.mean(chn_data[i])
    return flt_chn_data, feed_loc, chn_peakp, chn_peakn
Ejemplo n.º 5
0
def one_chn_result(yuv_data_in,
                   chn,
                   apa="ProtoDUNE",
                   femb=0,
                   tp=1,
                   psd=True,
                   step="step001",
                   stuck_filter=True,
                   env="RT",
                   hp_fliter=False):
    APAMAP = APA_MAP()
    APAMAP.APA = apa
    [yuv_chndata, y_chndata, v_chndata, u_chndata] = yuv_data_in
    apa_yuv, apa_y, apa_v, apa_u = APAMAP.apa_mapping()

    wire_type = "B"
    y_wire = np.where(chn == np.array(apa_y))
    if (len(y_wire[0]) > 0):
        if (apa_y[y_wire[0][0]] == chn):
            wire_type = "Y"
    u_wire = np.where(chn == np.array(apa_u))
    if (len(u_wire[0]) > 0):
        if (apa_u[u_wire[0][0]] == chn):
            wire_type = "U"
    v_wire = np.where(chn == np.array(apa_v))
    if (len(v_wire[0]) > 0):
        if (apa_v[v_wire[0][0]] == chn):
            wire_type = "V"

    onechn_data = yuv_chndata[chn]

    stuck_type = "Small"
    len_tmp_data = 0
    if (stuck_filter == True):
        tmp_data = []
        lenonechn_data = len(onechn_data)
        for tmp in onechn_data:

            if (tmp % 64 == 63) or (tmp % 64 == 0) or (tmp % 64 == 1) or (
                    tmp % 64 == 62) or (tmp % 64 == 2):
                pass
            else:
                tmp_data.append(tmp)
        len_tmp_data = len(tmp_data)
        #if (len_tmp_data > (lenonechn_data//100)):
        if (len_tmp_data >= 100000):
            if (len_tmp_data > lenonechn_data * 0.95):
                stuck_type = "Small"
            else:
                if (env == "RT"):
                    stuck_type = "Small"
                else:
                    stuck_type = "Middle"

            if (hp_fliter == True):
                flt_tmp_data = hp_flt_applied(tmp_data,
                                              fs=2000000,
                                              passfreq=1000,
                                              flt_order=3)
                flt_tmp_data = np.array(flt_tmp_data) + np.mean(tmp_data)
                rms = np.std(flt_tmp_data[0:100000])
                ped_mean = np.mean(flt_tmp_data[0:100000])
            else:
                rms = np.std(tmp_data[0:100000])
                ped_mean = np.mean(tmp_data[0:100000])

#            co_onechn_data = tmp_data[0:100000]
        else:
            rms = np.std(onechn_data)
            ped_mean = np.mean(onechn_data)
            stuck_type = "Large"
#            co_onechn_data = onechn_data[0:100000]

    else:
        rms = np.std(onechn_data)
        ped_mean = np.mean(onechn_data)
#        co_onechn_data = onechn_data[0:100000]

    if (psd == True):
        if (hp_fliter == True) and (len_tmp_data >= 100000):
            f, p = chn_rfft_psd(flt_tmp_data, fft_s=2000, avg_cycle=50)
            onechn_data = flt_tmp_data
        else:
            f, p = chn_rfft_psd(onechn_data, fft_s=2000, avg_cycle=50)
            onechn_data = onechn_data
    else:
        f, p = chn_rfft(onechn_data, fft_s=2000, avg_cycle=50)
        onechn_data = onechn_data

    return step, onechn_data[
        0:
        100000], wire_type, f, p, ped_mean, rms, chn, stuck_type  #, co_onechn_data