Пример #1
0
def pmaps_from_hdf(filename):
    """ read the pmaps from a h5 file (official production)
    inputs:
        filename : (str)  the filename of the h5 data
    output:
        pmaps    : (DFPmap) the pmaps (s1, s2, s2i) dataframes
    """
    #try:
    #    hdf = pd.HDFStore(filename)
#   #     dat = [hdf['s1'], hdf['s2'], hdf['s2si'], hdf['s1pmt'], hdf['s2pmt']]
#        dat = (hdf['s1'], hdf['s2'], hdf['s2si'])
#        return DFPmap(*dat)
    #except:
    try:
        s1, s2, s2i, _, _  = pmio.load_pmaps_as_df(filename)
        return PMap(s1, s2, s2i)
    except:
        try:
            hd = pd.HDFStore(filename)
            keys = hd.keys()
            ok = ('/s1' in keys) & ('/s2' in keys) & ('/s2si' in keys)
            if (not ok):
                raise IOError
            s1, s2, s2i = hd['s1'], hd['s2'], hd['s2si']
            return PMap(s1, s2, s2i)
        except:
            raise IOError
Пример #2
0
def relative_pmt_response():
    """
    Script which uses pmaps (will be generalised in future to check XY dependence)
    to look at the relative response of the PMTs in Kr events and
    compares to the results on Poisson mu from calibrations
    """

    pmap_file_base = sys.argv[1]
    dst_file_base = sys.argv[2]

    run_number = pmap_file_base.split('/')[2][1:]

    pmt_dats = DB.DataPMT(int(run_number))

    s1hists = {x: [] for x in range(12)}
    s2hists = {x: [] for x in range(12)}
    s1sumh = []
    s2sumh = []
    hitPMTdist = {x: [] for x in range(12)}
    hitPMTZpos = {x: [] for x in range(12)}

    pmap_sorter = sorter_func(pmap_file_base)
    pmap_file_list = sorted(glob(pmap_file_base + '*.h5'), key=pmap_sorter)

    dst_sorter = sorter_func(dst_file_base)
    dst_file_list = sorted(glob(dst_file_base + '*.h5'), key=dst_sorter)

    ## dst_frame = load_dsts(dst_file_list, 'DST', 'Events')
    dst_frame = load_dsts(dst_file_list, 'RECO', 'Events')

    dst_evt_list = dst_frame['event'].unique()

    #for fn in iglob(pmap_file_base + '*.h5'):
    for fn in pmap_file_list:

        ## This version just using pmt databases
        s1df, s2df, _, s1pmtdf, s2pmtdf = load_pmaps_as_df(fn)

        common_evts = np.intersect1d(s1pmtdf['event'].unique(), dst_evt_list)

        for evt in common_evts:
            #for evt in s1pmtdf['event'].unique():
            #evt    = dst_evt_iter[0]
            s1evt = s1pmtdf[s1pmtdf['event'] == evt]
            s2evt = s2pmtdf[s2pmtdf['event'] == evt]
            s1sevt = s1df[s1df['event'] == evt]
            s2sevt = s2df[s2df['event'] == evt]
            hit_evt = dst_frame[dst_frame['event'] == evt]
            ## if hit_evt['nS2'].iloc[0] == 1 and len(s2evt['peak'].unique()) == 1 and len(s1evt['peak'].unique()) == 1:
            if hit_evt['npeak'].nunique() == 1 and len(
                    s2evt['peak'].unique()) == 1 and len(
                        s1evt['peak'].unique()) == 1:
                ## Not well defined for multi-S2 events
                hit_x = hit_evt['X'].iloc[0]
                hit_y = hit_evt['Y'].iloc[0]
                hit_z = hit_evt['Z'].iloc[0]
                for peak in s1evt['peak'].unique():
                    s1peak = s1evt[s1evt['peak'] == peak]
                    s1sumh.append(s1sevt[s1sevt['peak'] == peak]['ene'].sum())
                    pmt1Q = s1peak[s1peak['npmt'] == 1]['ene'].sum()
                    for pmt in s1peak['npmt'].unique():
                        hitPMTdist[pmt].append(
                            np.sqrt(
                                np.power(
                                    hit_x - pmt_dats[pmt_dats['SensorID'] ==
                                                     pmt].X.values, 2) +
                                np.power(
                                    hit_y - pmt_dats[pmt_dats['SensorID'] ==
                                                     pmt].Y.values, 2)))
                        hitPMTZpos[pmt].append(hit_z)
                        if pmt != 1:
                            s1hists[pmt].append(
                                s1peak[s1peak['npmt'] == pmt]['ene'].sum() /
                                pmt1Q)
                        else:
                            s1hists[pmt].append(pmt1Q)

                for peak in s2evt['peak'].unique():
                    s2peak = s2evt[s2evt['peak'] == peak]
                    s2sumh.append(s2sevt[s2sevt['peak'] == peak]['ene'].sum())
                    if s2sumh[-1] > 4000:  # and s2sumh[-1] < 12000:
                        ## pmt1Q = s2peak[s2peak['npmt'] == 1]['ene'].values[5:-5]
                        pmt1Q = s2peak[s2peak['npmt'] == 1]['ene'].sum()
                        for pmt in s2peak['npmt'].unique():
                            if pmt != 1:
                                ## s2hists[pmt].append(s2peak[s2peak['npmt'] == pmt]['ene'].values[5:-5]/pmt1Q)
                                s2hists[pmt].append(
                                    s2peak[s2peak['npmt'] == pmt]['ene'].sum()
                                    / pmt1Q)
                            else:
                                s2hists[pmt].append(pmt1Q)

            #dst_evt_iter.iternext()

    ## Make the plots
    s1sumh = np.array(s1sumh)
    s2sumh = np.array(s2sumh)
    figs0, axes0 = plt.subplots(nrows=1, ncols=2)
    axes0[0].hist(s1sumh)
    axes0[0].set_title('PMT sum S1 distribution')
    axes0[1].hist(s2sumh)
    axes0[1].set_title('PMT sum S2 distribution')
    plt.tight_layout()
    figs0.show()
    figs0.savefig('SumChargescharge_R' + run_number + '.png')
    figs1, axess1 = plt.subplots(nrows=3, ncols=4, figsize=(20, 6))
    s1pmt1 = np.array(s1hists[1])
    s1bins = np.arange(-2, 4, 0.1)
    s2bins = np.arange(0.4, 1.1, 0.005)
    s1select = (s1sumh > 2) & (s1sumh < 150)
    for (key, val), ax in zip(s1hists.items(), axess1.flatten()):
        if key == 1:
            ax.hist(np.array(val)[s1select], bins=100)
            #ax.scatter(s1sumh[s1select], np.array(val)[s1select])
            ## ax.scatter(np.array(hitPMTdist[key])[s1select], np.array(val)[s1select])
            #ax.scatter(np.array(hitPMTZpos[key])[s1select], np.array(val)[s1select])
            ax.set_title('PMT 1 S1 charge')
            #ax.set_xlabel('integrated charge in PMT sum (pe)')
            #ax.set_xlabel('z pos.')
            #ax.set_ylabel('integrated charge in PMT1 (pe)')
            ax.set_ylabel('AU)')
            ax.set_xlabel('integrated charge in PMT1 (pe)')
            sh_hits = np.array(hitPMTZpos[key])[s1select].shape
            sh_val = np.array(val)[s1select].shape
            covar = np.cov(
                np.array(hitPMTZpos[key])[s1select].reshape(1, sh_hits[0]),
                np.array(val)[s1select].reshape(1, sh_val[0]))[0, 1]
            corr_coef = covar / (
                np.std(np.array(val)[s1select], ddof=1) *
                np.std(np.array(hitPMTZpos[key])[s1select], ddof=1))
            print('Sensor ', key, ' correlation coefficient = ', corr_coef)
        else:
            vals, bins, _ = ax.hist(np.array(val)[s1select], bins=s1bins)
            ## ax.scatter(s1pmt1[np.abs(val) < 10], np.array(val)[np.abs(val) < 10])
            #ax.scatter(s1sumh[s1select], np.array(val)[s1select])
            ## ax.scatter(np.array(hitPMTdist[key])[s1select & (np.abs(val) < 10)], np.array(val)[s1select & (np.abs(val) < 10)])
            #ax.scatter(np.array(hitPMTZpos[key])[s1select & (np.abs(val) < 10)], np.array(val)[s1select & (np.abs(val) < 10)])
            ax.set_title('PMT ' + str(key) + ' S1 relative charge')
            #ax.set_xlabel('integrated charge in PMT sum (pe)')
            ## ax.set_xlabel('PMT-hit dist. (mm)')
            #ax.set_xlabel('hit Z pos')
            #ax.set_ylabel('pmt q / pmt1 q')
            ax.set_ylabel('AU')
            ax.set_xlabel('pmt q / pmt1 q')
            ## sh_hits = np.array(hitPMTZpos[key])[s1select].shape
            ## sh_val = np.array(val)[s1select].shape
            ## covar = np.cov(np.array(hitPMTZpos[key])[s1select].reshape(1, sh_hits[0]), np.array(val)[s1select].reshape(1, sh_val[0]))[0, 1]
            ## corr_coef = covar / (np.std(np.array(val)[s1select], ddof=1)*np.std(np.array(hitPMTZpos[key])[s1select], ddof=1))
            s1select2 = s1select & (np.array(val) > 0) & (np.array(val) <= 2)
            print('Sensor ', key, ' mean = ',
                  np.mean(np.array(val)[s1select2]))
            useful_bins = np.argwhere(vals >= 100)
            b1 = useful_bins[0][0]
            b2 = useful_bins[-1][0]
            errs = np.sqrt(vals[b1:b2])
            fvals = fitf.fit(fitf.gauss,
                             shift_to_bin_centers(bins)[b1:b2],
                             vals[b1:b2],
                             seed=(vals.sum(), bins[vals.argmax()], 0.1),
                             sigma=errs)
            ax.plot(
                shift_to_bin_centers(bins)[b1:b2],
                fvals.fn(shift_to_bin_centers(bins)[b1:b2]))
            print('Fit S1 ' + str(key), fvals.values, fvals.errors, fvals.chi2)
    plt.tight_layout()
    figs1.show()
    figs1.savefig('s1relativechargeThzoom_R' + run_number + '.png')

    fitVals = {}
    figs2, axess2 = plt.subplots(nrows=3, ncols=4, figsize=(20, 6))
    s2pmt1 = np.array(s2hists[1])
    for (key, val), ax in zip(s2hists.items(), axess2.flatten()):
        if key == 1:
            ## ax.set_title('PMT 1 S2 charge')
            ax.set_title('PMT 1 S2 charge vs s2 sum charge')
            ax.set_xlabel('S2pmt sum charge (pe)')
            #ax.set_xlabel('integrated charge in PMT sum (pe)')
            #ax.set_xlabel('z pos')
            ax.set_ylabel('integrated charge in PMT1 (pe)')
            #ax.set_ylabel('AU')
            ax.set_xlabel('integrated charge in PMT1 (pe)')
            ## ax.hist(np.array(val)[(s2sumh>4000) & (s2sumh<12000)], bins=100)
            ##ax.hist(np.concatenate(val), bins=100)
            ## ax.hist(np.array(val)[s2sumh>4000], bins=100)
            ax.scatter(s2sumh[s2sumh > 4000], np.array(val)[s2sumh > 4000])
            #ax.scatter(s2sumh[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            ## ax.scatter(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            #ax.scatter(np.array(hitPMTZpos[key])[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            ## sh_hits = np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)].shape
            ## sh_val = np.array(val)[(s2sumh>4000) & (s2sumh<12000)].shape
            ## covar = np.cov(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)].reshape(1, sh_hits[0]), np.array(val)[(s2sumh>4000) & (s2sumh<12000)].reshape(1, sh_val[0]))[0, 1]
            ## corr_coef = covar / (np.std(np.array(val)[(s2sumh>4000) & (s2sumh<12000)], ddof=1)*np.std(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)], ddof=1))
            ## print('Sensor ', key, ' correlation coefficient = ', corr_coef)
        else:
            ## ax.set_title('PMT '+str(key)+' S2 relative charge')
            ax.set_title('PMT ' + str(key) + ' S2 relative charge vs pmt sum')
            ax.set_ylabel('pmt q / pmt1 q')
            #ax.set_xlabel('zpos')
            ax.set_xlabel('integrated charge in PMT sum (pe)')
            #ax.set_xlabel('pmt q / pmt1 q')
            #ax.set_ylabel('AU')
            #ax.scatter(s2pmt1[np.abs(val) < 10], np.array(val)[np.abs(val) < 10])
            #ax.scatter(s2sumh[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            ## vals, bins, _ = ax.hist(np.array(val)[(s2sumh>4000) & (s2sumh<12000)], bins=s2bins)
            ## vals, bins, _ = ax.hist(np.concatenate(val), bins=s2bins)
            ## vals, bins, _ = ax.hist(np.array(val)[s2sumh>4000], bins=s2bins)
            ax.scatter(s2sumh[s2sumh > 4000], np.array(val)[s2sumh > 4000])
            ## ax.scatter(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            #ax.scatter(np.array(hitPMTZpos[key])[(s2sumh>4000) & (s2sumh<12000)], np.array(val)[(s2sumh>4000) & (s2sumh<12000)])
            sh_sum = s2sumh[s2sumh > 4000].shape
            sh_vals = np.array(val)[s2sumh > 4000].shape
            covar = np.cov(s2sumh[s2sumh > 4000].reshape(1, sh_sum[0]),
                           np.array(val)[s2sumh > 4000].reshape(1,
                                                                sh_vals[0]))[0,
                                                                             1]
            corr_coef = covar / (np.std(np.array(val)[s2sumh > 4000], ddof=1) *
                                 np.std(s2sumh[s2sumh > 4000], ddof=1))
            ## sh_hits = np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)].shape
            ## sh_val = np.array(val)[(s2sumh>4000) & (s2sumh<12000)].shape
            ## covar = np.cov(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)].reshape(1, sh_hits[0]), np.array(val)[(s2sumh>4000) & (s2sumh<12000)].reshape(1, sh_val[0]))[0, 1]
            ## corr_coef = covar / (np.std(np.array(val)[(s2sumh>4000) & (s2sumh<12000)], ddof=1)*np.std(np.array(hitPMTdist[key])[(s2sumh>4000) & (s2sumh<12000)], ddof=1))
            print('Sensor ', key, ' correlation coefficient = ', corr_coef)
            ## limit fit to region with stat error <= 10% Poisson
            ## useful_bins = np.argwhere(vals>=200)
            ## b1 = useful_bins[0][0]
            ## b2 = useful_bins[-1][0]
            ## errs = np.sqrt(vals[b1:b2])
            ## print('Seed check: ', (vals.sum(), bins[vals.argmax()], 0.02))
            ## fvals = fitf.fit(fitf.gauss, shift_to_bin_centers(bins)[b1:b2], vals[b1:b2],
            ##                  seed=(vals.sum(), bins[vals.argmax()], 0.02),
            ##                  sigma=errs, bounds=[(0, 0, 0.00001), (1e10, 2, 3)])
            ## ax.plot(shift_to_bin_centers(bins),
            ##         fitf.gauss(shift_to_bin_centers(bins), *fvals.values))
            ## fitVals[key] = (fvals.values[1], fvals.values[2])
            ## print('Fit PMT '+str(key), fvals.values, fvals.errors, fvals.chi2)
    plt.tight_layout()
    figs2.show()
    figs2.savefig('s2relativechargeThvsSum_R' + run_number + '.png')

    ## figcal, axcal = plt.subplots()
    ## axcal.errorbar(list(fitVals.keys()),
    ##                np.fromiter((x[0] for x in fitVals.values()), np.float),
    ##                yerr=np.fromiter((x[1] for x in fitVals.values()), np.float),
    ##                label='Average response of PMTs to Kr relative to PMT 1')
    ## ## Get the calibration info for comparison.
    ## cal_files = [ fname for fname in sys.argv[3:] ]
    ## read_params = partial(spr, table_name='FIT_pmt_scaled_dark_pedestal',
    ##                       param_names=['poisson_mu'])
    ## ## Assumes ordering, ok?
    ## for i, fn in enumerate(cal_files):
    ##     cal_run = fn.split('_')[1]
    ##     with tb.open_file(fn) as cal_in:
    ##         pmt1Val = 0
    ##         pmt1Err = 0
    ##         cVals = []
    ##         cErrs = []
    ##         for sens, (pars, errs) in read_params(cal_in):
    ##             if sens != 1:
    ##                 cVals.append(pars['poisson_mu'])
    ##                 cErrs.append(errs['poisson_mu'])
    ##             else:
    ##                 pmt1Val = pars['poisson_mu']
    ##                 pmt1Err = errs['poisson_mu']
    ##         normVals = np.array(cVals) / pmt1Val
    ##         normErrs = normVals * np.sqrt(np.power(np.array(cErrs)/np.array(cVals), 2) +
    ##                                       np.power(pmt1Err/pmt1Val, 2))
    ##         axcal.errorbar(list(fitVals.keys()), normVals,
    ##                        yerr=normErrs, label='Calibration '+cal_run)
    ## axcal.legend()
    ## axcal.set_xlabel('PMT sensor ID')
    ## axcal.set_ylabel('Response relative to that of PMT 1')
    ## figcal.show()
    ## figcal.savefig('calPoisKrRelCompStatsFILT.png')
    input('plots good?')
Пример #3
0
def generate_pdfs():
    """
    Generate SiPM PDFs using Kr RAW data.
    Multiple types of PDF are generated:
    Full spectrum PDFs : using full buffer
    Z vetoed PDFs      : Vetoing all sipms for regions in z where there is an identified s1 or s2
    1 ring vetoed PDFs : Vetoing sipms within 1 ring distance of a hit.
    2 ring vetoed PDFs : As above for 2 rings
    ...
    """

    pmap_file_base = sys.argv[1]
    hit_file_base = sys.argv[2]
    raw_file_base = sys.argv[3]

    pmap_sorter = sorter_func(pmap_file_base)
    pmap_files = sorted(glob(pmap_file_base + '*.h5'), key=pmap_sorter)
    hit_sorter = sorter_func(hit_file_base)
    hit_files = sorted(glob(hit_file_base + '*.h5'), key=hit_sorter)
    raw_sorter = sorter_func(raw_file_base)
    raw_files = sorted(glob(raw_file_base + '*.h5'), key=raw_sorter)

    ## Details of raw waveforms to aid vetoing (assumes all same in run)
    with tb.open_file(raw_files[0]) as rwf_in:
        sipmrwf = rwf_in.root.RD.sipmrwf[0][0]
        wf_range = np.arange(len(sipmrwf))

    run_no = int(sys.argv[4])
    ## Gains and sensor positions
    sipm_gains = DB.DataSiPM(run_no).adc_to_pes.values
    sipm_xy = DB.DataSiPM(run_no)[['X', 'Y']].values

    ## output
    histbins = np.arange(-10, 300, 0.1)
    bin_centres = shift_to_bin_centers(histbins)
    with tb.open_file('vetoedPDFs_R' + str(run_no) + '.h5', 'w') as pdf_out:
        HIST = partial(hist_writer,
                       pdf_out,
                       group_name='HIST',
                       n_sensors=1792,
                       n_bins=len(bin_centres),
                       bin_centres=bin_centres)
        full_spec = HIST(table_name='full_spec')
        z_vetoed = HIST(table_name='z_vetoed')
        one_ring = HIST(table_name='one_ring_vetoed')
        two_ring = HIST(table_name='two_ring_vetoed')
        thr_ring = HIST(table_name='thr_ring_vetoed')
        inv_thre = HIST(table_name='thr_ring_avetoed')

        ## Hit info
        hit_positions = load_dsts(hit_files, 'DST',
                                  'Events')[['event', 'X', 'Y']].values
        ## Start assuming KR data and Kdst
        ## For each event [evt_no, list tuples start and end veto areas]
        reduced_pulse_info = []
        for pmf in pmap_files:
            #print(pmf)
            sys.stdout.write(pmf + '\n')
            sys.stdout.flush()
            try:
                ## pmap_dict = load_pmaps(pmf)
                s1s, s2s, _, _, _ = load_pmaps_as_df(pmf)
            except (ValueError, tb.exceptions.NoSuchNodeError):
                print("Empty file. Skipping.")
                continue

            ## for key, pmap in pmap_dict.items():
            for evtNo in s1s['event'].unique():
                evtS1 = s1s[s1s['event'] == evtNo]
                evtS2 = s2s[s2s['event'] == evtNo]
                mask_list = []
                ## for s1 in pmap.s1s:
                for is1 in evtS1['peak'].unique():
                    s1 = evtS1[evtS1['peak'] == is1]
                    mask_list.append(
                        (wf_range < s1['time'].iloc[0] / units.mus - 1)
                        | (wf_range > s1['time'].iloc[-1] / units.mus + 1))
                for is2 in evtS2['peak'].unique():
                    s2 = evtS2[evtS2['peak'] == is2]
                    mask_list.append(
                        (wf_range < s2['time'].iloc[0] / units.mus - 2)
                        | (wf_range > s2['time'].iloc[-1] / units.mus + 2))
                reduced_pulse_info.append(
                    [evtNo, np.logical_and.reduce(mask_list)])
        print('masking info stored')
        mask_counter = 0
        pmap_evts = np.fromiter((x[0] for x in reduced_pulse_info), np.int)
        for rawf in raw_files:
            #print(rawf)
            sys.stdout.write(rawf + '\n')
            sys.stdout.flush()
            if mask_counter >= len(reduced_pulse_info):
                continue
            try:
                ## empty arrays for histograms
                shape = 1792, len(bin_centres)
                hist_full_spec = np.zeros(shape, dtype=np.int)
                hist_z_vetoed = np.zeros(shape, dtype=np.int)
                hist_1_vetoed = np.zeros(shape, dtype=np.int)
                hist_2_vetoed = np.zeros(shape, dtype=np.int)
                hist_3_vetoed = np.zeros(shape, dtype=np.int)
                hist_3_aveto = np.zeros(shape, dtype=np.int)
                with tb.open_file(rawf) as raw_in:
                    revent_nos = np.fromiter(
                        (x[0] for x in raw_in.root.Run.events), np.int)

                    #evt_no = reduced_pulse_info[mask_counter][0]
                    #indx = np.argwhere(revent_nos==evt_no)
                    #print(reduced_pulse_info)
                    #pmap_evts = np.array(reduced_pulse_info)[:, 0]
                    pmap_overlap_indx = np.arange(revent_nos.shape[0])[np.in1d(
                        revent_nos, pmap_evts)]
                    hit_overlap_indx = np.arange(revent_nos.shape[0])[np.in1d(
                        revent_nos, hit_positions[:, 0])]
                    hit_indcs = np.arange(hit_positions[:,
                                                        0].shape[0])[np.in1d(
                                                            hit_positions[:,
                                                                          0],
                                                            revent_nos)]
                    hindx = 0
                    #print(indx, indx[0][0])
                    #while indx.shape[0] != 0:
                    for indx in pmap_overlap_indx:
                        #print(indx[0][0])
                        #rwf = raw_in.root.RD.sipmrwf[indx[0][0]]
                        ## cwf = csf.sipm_processing["subtract_mode_calibrate"](raw_in.root.RD.sipmrwf[indx[0][0]], sipm_gains)
                        cwf = csf.sipm_processing["subtract_mode_calibrate"](
                            raw_in.root.RD.sipmrwf[indx], sipm_gains)

                        hist_full_spec += cf.bin_waveforms(cwf, histbins)
                        z_veto = reduced_pulse_info[mask_counter][1]
                        hist_z_vetoed += cf.bin_waveforms(
                            cwf[:, z_veto], histbins)

                        #dst_indx = np.argwhere(hit_positions[:, 0]==evt_no)
                        #if dst_indx.shape[0] != 0:
                        if indx in hit_overlap_indx:
                            ## hit_p = hit_positions[dst_indx[0][0], 1:]
                            hit_p = hit_positions[hit_indcs[hindx], 1:]
                            hindx += 1
                            hist_1_vetoed += cf.bin_waveforms(
                                ring_veto(cwf, 1, z_veto, hit_p, sipm_xy),
                                histbins)
                            hist_2_vetoed += cf.bin_waveforms(
                                ring_veto(cwf, 2, z_veto, hit_p, sipm_xy),
                                histbins)
                            thrVeto = ring_veto(cwf, 3, z_veto, hit_p, sipm_xy)
                            hist_3_vetoed += cf.bin_waveforms(
                                thrVeto, histbins)
                            hist_3_aveto += cf.bin_waveforms(
                                thrVeto[:, np.invert(z_veto)], histbins)

                        mask_counter += 1
                        #if mask_counter < len(reduced_pulse_info):
                        #    evt_no = reduced_pulse_info[mask_counter][0]
                        #    indx = np.argwhere(revent_nos==evt_no)
                        #else:
                        ## dummy evt_no to definitely give no info
                        #    indx = np.argwhere(revent_nos==-100)
                        #print(indx, indx[0][0])
                    full_spec(hist_full_spec)
                    z_vetoed(hist_z_vetoed)
                    one_ring(hist_1_vetoed)
                    two_ring(hist_2_vetoed)
                    thr_ring(hist_3_vetoed)
                    inv_thre(hist_3_aveto)
            except tb.HDF5ExtError:
                print('corrupt file')
                continue
Пример #4
0
def compare_sims():
    """
    Just a copy (more or less) of Paola's notebook for
    simulation low level plots adjusted to compare simulation
    with and without the low frequency noise.
    """

    run_number      = -4735
    DataSiPM        = DB.DataSiPM(run_number)
    data_xs         = DataSiPM.X.values
    data_ys         = DataSiPM.Y.values

    ## conf parameters
    ## n_rebin = 1
    n_files = 10
    drift_v = 1.

    ## Containers for plots ##
    s1Q_NO       = []
    s1H_NO       = []
    s1W_NO       = []
    s1per_evt_NO = []
    s1Q_O        = []
    s1H_O        = []
    s1W_O        = []
    s1per_evt_O  = []
    #maxPMTtot_ratio = []

    s2per_evt_NO = []
    s2Q_NO       = []
    s2H_NO       = []
    s2W_NO       = []
    s2per_evt_O  = []
    s2Q_O        = []
    s2H_O        = []
    s2W_O        = []

    no_iter = gl("/Users/laingandrew/Documents/NEXT/IC_stuff/simdats/noOsc_paola/Tl_DISK_CLOSE_TO_ANODE_7bar_noiseSiPM_pmaps.*.h5")
    o_iter  = gl("/Users/laingandrew/Documents/NEXT/IC_stuff/simdats/Tl_DISK_CLOSE_TO_ANODE_7bar_noiseSiPM_pmaps.*.h5")
    for no_file, o_file in zip(no_iter, o_iter):
    ##for ifile in range(n_files):
     
        ##PATH_IN_NOOSC = "/Users/laingandrew/Documents/NEXT/IC_stuff/simdats/noOsc_paola/Tl_DISK_CLOSE_TO_ANODE_7bar_noiseSiPM_pmaps.{}.h5".format(ifile)
        ##PATH_IN_OSC   = "/Users/laingandrew/Documents/NEXT/IC_stuff/simdats/Tl_DISK_CLOSE_TO_ANODE_7bar_noiseSiPM_pmaps.{}.h5".format(ifile)
        print(no_file)
        #pmapNO_dict = load_pmaps(no_file)
        s1sNO, s2sNO, _, _, _ = load_pmaps_as_df(no_file)
        print('NO pmaps got')
        #pmapO_dict  = load_pmaps(o_file)
        s1sO, s2sO, _, _, _ = load_pmaps_as_df(o_file)
        print('O pmaps got')
        
        ## event_numbers_NO, timestamps_NO = get_event_numbers_and_timestamps_from_file_name(PATH_IN_NOOSC)
        ## event_numbers_O, timestamps_O   = get_event_numbers_and_timestamps_from_file_name(PATH_IN_OSC)
        event_numbers_NO = s2sNO['event'].unique()
        event_numbers_O  = s2sO['event'].unique()
        print(len(event_numbers_NO), len(event_numbers_NO))
        for evtNO, evtO in zip(event_numbers_NO, event_numbers_O):
            ## pmapNO = pmapNO_dict.get(evtNO, None)
            ## pmapO  = pmapO_dict.get(evtO, None)
            s1s_NO = s1sNO[s1sNO['event']==evtNO]
            s1s_O  = s1sO[s1sO['event']==evtO]
            s2s_NO = s2sNO[s2sNO['event']==evtNO]
            s2s_O  = s2sO[s2sO['event']==evtO]
            #print(len(s1s_NO), len(s1s_O), len(s2s_NO), len(s2s_O))
            #print('evtNO = ', evtNO, ' evtO = ', evtO,' pmaps got')
            if len(s1s_NO) == 0 or len(s1s_O) == 0 or len(s2s_NO) == 0 or len(s2s_O) == 0:
                continue
            ## if pmapNO:
            ##     s1s_NO    = pmapNO.s1s
            ##     s2sraw_NO = pmapNO.s2s
            ## else:
            ##     continue
            ## if pmapO:
            ##     s1s_O    = pmapO.s1s
            ##     s2sraw_O = pmapO.s2s
            ## else:
            ##     continue

            #if not s1s_NO or not s2sraw_NO or not s1s_O or not s2sraw_O: continue

            s1per_evt_NO.append(s1s_NO['peak'].nunique())
            s1per_evt_O .append(s1s_O['peak'].nunique())
            #print('n_s1_nosc = ', s1s_NO['peak'].nunique(), ', n_s1_osc = ', s1s_O['peak'].nunique())
            #if s1s_NO['peak'].nunique() != 1: continue
            #if s1s_O['peak'].nunique()  != 1: continue

            ## s2sNO = [rebin_peak(s2raw_NO, n_rebin) for s2raw_NO in s2sraw_NO]
            ## s2sO  = [rebin_peak(s2raw_O, n_rebin) for s2raw_O in s2sraw_O]

            ## s1tNO = s1s_NO[0].times
            ## s1eNO = s1s_NO[0].pmts
            ## S1tNO = s1t_NO[np.argmax(s1eNO)]
            ## s1tO  = s1s_O[0].times
            ## s1eO  = s1s_O[0].pmts
            ## S1tO  = s1t_O[np.argmax(s1eO)]
            #print('Aqui?')
            #fill histograms
            for is1 in range(s1s_NO['peak'].nunique()):
                s1 = s1s_NO[s1s_NO['peak'] == is1]
                s1H_NO.append(s1['ene'].max())
                s1W_NO.append(s1['time'].iloc[-1] - s1['time'].iloc[0])
                s1Q_NO.append(s1['ene'].sum())
            for js1 in range(s1s_O['peak'].nunique()):
                s1 = s1s_O[s1s_O['peak'] == js1]
                s1H_O.append(s1['ene'].max())
                s1W_O.append(s1['time'].iloc[-1] - s1['time'].iloc[0])
                s1Q_O.append(s1['ene'].sum())

            s2per_evt_NO.append(s2s_NO['peak'].nunique())
            s2per_evt_O.append(s2s_O['peak'].nunique())

            for is2 in range(s2s_NO['peak'].nunique()):
                s2 = s2s_NO[s2s_NO['peak'] == is2]
                s2Q_NO.append(s2['ene'].sum())
                ## if s2['time'].iloc[-1] - s2['time'].iloc[0] < 0:
                ##     print(evtNO, ' has -ve width S2 in NO')
                s2W_NO.append(s2['time'].iloc[-1] - s2['time'].iloc[0])
                s2H_NO.append(s2['ene'].max())
            for js2 in range(s2s_O['peak'].nunique()):
                s2 = s2s_O[s2s_O['peak'] == js2]
                s2Q_O.append(s2['ene'].sum())
                ## if s2['time'].iloc[-1] - s2['time'].iloc[0] < 0:
                ##     print(evtO, ' has -ve width S2 in O')
                s2W_O.append(s2['time'].iloc[-1] - s2['time'].iloc[0])
                s2H_O.append(s2['ene'].max())

    print(min(s1per_evt_NO), max(s1per_evt_NO), min(s1per_evt_O), max(s1per_evt_O))
    print(min(s2per_evt_NO), max(s2per_evt_NO), min(s2per_evt_O), max(s2per_evt_O))
    print(min(s1Q_NO), max(s1Q_NO), min(s2Q_O), max(s2Q_O))
    print(min(s1H_NO), max(s1H_NO), min(s2H_O), max(s2H_O))
    print(min(s1W_NO), max(s1W_NO), min(s2W_NO), max(s2W_NO))
    print(min(s1W_O), max(s1W_O), min(s2W_O), max(s2W_O))

    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20,6))
    axes[0][0].hist(s1per_evt_NO, bins=10, range=(0, 10), label='No. S1s no low frequency noise', log=True, histtype='step')
    axes[0][0].hist(s1per_evt_O , bins=10, range=(0, 10), label='No. S1s with low frequency noise', log=True, histtype='step')
    axes[0][0].legend()
    axes[0][0].set_xlabel('No. s1 per event')
    axes[0][0].set_ylabel('AU')
    
    axes[0][1].hist(s1H_NO, bins=200, range=(0, 200), label='S1 height no low frequency noise', log=True, histtype='step')
    axes[0][1].hist(s1H_O , bins=200, range=(0, 200), label='S1 height with low frequency noise', log=True, histtype='step')
    axes[0][1].legend()
    axes[0][1].set_xlabel('S1 height (pes)')
    axes[0][1].set_ylabel('AU')

    axes[1][0].hist(s1Q_NO, bins=100, range=(0, 1500), label='S1 charge no low frequency noise', log=True, histtype='step')
    axes[1][0].hist(s1Q_O , bins=100, range=(0, 1500), label='S1 charge with low frequency noise', log=True, histtype='step')
    axes[1][0].legend()
    axes[1][0].set_xlabel('S1 charge (pes)')
    axes[1][0].set_ylabel('AU')

    axes[1][1].hist(s1W_NO, bins=40, range=(0, 1000), label='S1 width no low frequency noise', log=True, histtype='step')
    axes[1][1].hist(s1W_O , bins=40, range=(0, 1000), label='S1 width with low frequency noise', log=True, histtype='step')
    axes[1][1].legend()
    axes[1][1].set_xlabel('S1 width (ns)')
    axes[1][1].set_ylabel('AU')
    fig.show()
    fig.savefig('s1_plots_pmaps_oscNoosc.png')

    fig2, axes2 = plt.subplots(nrows=2, ncols=2, figsize=(20,6))
    axes2[0][0].hist(s2per_evt_NO, bins=10, range=(0, 10), label='No. S2s no low frequency noise', log=True, histtype='step')
    axes2[0][0].hist(s2per_evt_O , bins=10, range=(0, 10), label='No. S2s with low frequency noise', log=True, histtype='step')
    axes2[0][0].legend()
    axes2[0][0].set_xlabel('No. s2 per event')
    axes2[0][0].set_ylabel('AU')
    
    axes2[0][1].hist(s2H_NO, bins=1000, range=(10, 65000), label='S2 height no low frequency noise', log=True, histtype='step')
    axes2[0][1].hist(s2H_O , bins=1000, range=(10, 65000), label='S2 height with low frequency noise', log=True, histtype='step')
    axes2[0][1].legend()
    axes2[0][1].set_xlabel('S2 height (pes)')
    axes2[0][1].set_ylabel('AU')

    axes2[1][0].hist(s2Q_NO, bins=1000, range=(10, 750000), label='S2 charge no low frequency noise', log=True, histtype='step')
    axes2[1][0].hist(s2Q_O , bins=1000, range=(10, 750000), label='S2 charge with low frequency noise', log=True, histtype='step')
    axes2[1][0].legend()
    axes2[1][0].set_xlabel('S2 charge (pes)')
    axes2[1][0].set_ylabel('AU')

    axes2[1][1].hist(s2W_NO, bins=235, range=(1500, 350000), label='S2 width no low frequency noise', log=True, histtype='step')
    axes2[1][1].hist(s2W_O , bins=235, range=(1500, 350000), label='S2 width with low frequency noise', log=True, histtype='step')
    axes2[1][1].legend()
    axes2[1][1].set_xlabel('S2 width (ns)')
    axes2[1][1].set_ylabel('AU')
    fig2.show()
    fig2.savefig('s2_plots_pmaps_oscNoosc.png')
    input('Done with plots?')
Пример #5
0
def KrMC_pmaps_without_ipmt_dfs(KrMC_pmaps_without_ipmt_filename):
    s1df, s2df, sidf, s1pmtdf, s2pmtdf = load_pmaps_as_df(
        KrMC_pmaps_without_ipmt_filename)
    return pmp_dfs(s1df, s2df, sidf, s1pmtdf, s2pmtdf)