Ejemplo n.º 1
0
 def _set_rad_bins(self, radedges, nradbins):
     rmin = math.floor(np.amin(
         self.rad)) if radedges is None else radedges[0]
     rmax = math.ceil(np.amax(
         self.rad)) if radedges is None else radedges[-1]
     if rmin < 1: rmin = 1
     self.rb = HBins((rmin, rmax), nradbins)
Ejemplo n.º 2
0
def test_hbins():
    print('In pyalgos.test_hbins')
    from psana.pyalgos.generic.HBins import HBins
    o = HBins((1, 6), 5)
    print('  binedges():', o.binedges())
    assert (np.array_equal(o.binedges(),
                           np.array((1, 2, 3, 4, 5, 6), dtype=np.float)))
Ejemplo n.º 3
0
    def set_histogram_from_arr(self, arr, nbins=1000, amin=None, amax=None, frmin=0.001, frmax=0.999, edgemode=0, update_hblimits=True):
        #if np.array_equal(arr, self.arr_old): return
        if arr is self.arr_old: return
        self.arr_old = arr
        if arr.size<1: return

        aravel = arr.ravel()

        vmin, vmax = self.hbins.limits() if self.hbins is not None else (None, None)

        if self.hbins is None or update_hblimits:
          vmin = amin if amin is not None else\
               aravel.min() if frmin in (0,None) else\
               np.quantile(aravel, frmin, axis=0, interpolation='lower')
          vmax = amax if amax is not None else\
               aravel.max() if frmax in (1,None) else\
               np.quantile(aravel, frmax, axis=0, interpolation='higher')
          if not vmin<vmax: vmax=vmin+1

        hb = HBins((vmin,vmax), nbins=nbins)
        hb.set_bin_data_from_array(aravel, dtype=np.float64, edgemode=edgemode)

        hmin, hmax = 0, hb.bin_data_max()
        #logger.debug('set_histogram_from_arr %s\n    vmin(%.5f%%):%.3f vmax(%.5f%%):%.3f hmin: %.3f hmax: %.3f'%\
        #             (info_ndarr(aravel, 'arr.ravel'), frmin,vmin,frmax,vmax,hmin,hmax))
        hgap = 0.05*(hmax-hmin)

        rs0 = self.scene().sceneRect()
        rsy, rsh = (hb.vmin(), hb.vmax()-hb.vmin()) if update_hblimits else (rs0.y(), rs0.height())
        rs = QRectF(hmin-hgap, rsy, hmax-hmin+2*hgap, rsh)
        self.set_rect_scene(rs, set_def=update_hblimits)

        self.update_my_scene(hbins=hb)
        self.hbins = hb
Ejemplo n.º 4
0
def test_histogram():
    import psana.pyalgos.generic.NDArrGenerators as ag
    from psana.pyalgos.generic.HBins import HBins
    nbins = 1000
    arr = ag.random_standard((nbins,), mu=50, sigma=10, dtype=ag.np.float64)
    hb = HBins((0,nbins), nbins=nbins)
    hb.set_bin_data(arr, dtype=ag.np.float64)
    return hb
Ejemplo n.º 5
0
 def _set_phi_bins(self, phiedges, nphibins):
     if phiedges[-1] > phiedges[0]+360\
     or phiedges[-1] < phiedges[0]-360:
         raise ValueError('Difference between angular edges should not exceed 360 degree;'\
                          ' phiedges: %.0f, %.0f' % (phiedges[0], phiedges[-1]))
     self.pb = HBins(phiedges, nphibins)
     phi1, phi2 = self.pb.limits()
     self.is360 = math.fabs(math.fabs(phi2 - phi1) - 360) < 1e-3
Ejemplo n.º 6
0
    def proc_waveforms(self, wfs, wts) :
        """
        """
        # if waveforms are already processed
        if wfs is self._wfs_old : return

        self._init_arrays()
 
        #print_ndarr(wfs, '  waveforms : ', last=4)
        assert (self.NUM_CHANNELS==wfs.shape[0]),\
               'expected number of channels in not consistent with waveforms array shape'

        if self.VERSION == 2 : std = wfs[:,self.IOFFSETBEG:self.IOFFSETEND].std(axis=1)

        if self.VERSION == 4:
            self.wfsprep = wfs[:,self.WFBINBEG:self.WFBINEND]
        else:      
            offsets = wfs[:,self.IOFFSETBEG:self.IOFFSETEND].mean(axis=1)
            #print('  XXX offsets: %s' % str(offsets))        
            self.wfsprep = wfs[:,self.WFBINBEG:self.WFBINEND] - offsets.reshape(-1, 1) # subtract wf-offset
        self.wtsprep = wts[:,self.WFBINBEG:self.WFBINEND] # sec

        for ch in range(self.NUM_CHANNELS) :

            wf = self.wfsprep[ch,:]
            wt = self.wtsprep[ch,:]

            npeaks = None
            if self.VERSION == 3 :
                npeaks, self.wfgi, self.wff, self.wfg, self.thrg, self.edges =\
                peak_finder_v3(wf, self.SIGMABINS, self.BASEBINS, self.NSTDTHR, self.GAPBINS, self.DEADBINS,\
                                        self._pkvals[ch,:], self._pkinds[ch,:])
            elif self.VERSION == 2 :
                self.THR = self.NSTDTHR*std[ch]
                npeaks = peak_finder_v2(wf, self.SIGMABINS, self.THR, self.DEADBINS,\
                                        self._pkvals[ch,:], self._pkinds[ch,:])
            elif self.VERSION == 4 :
                t_list = self.PyCFDs[ch].CFD(wf,wt)
                npeaks = self._pkinds[ch,:].size if self._pkinds[ch,:].size<=len(t_list) else len(t_list)
                # need it in V4 to convert _pktsec to _pkinds and _pkvals
                if self.tbins is None :
                    from psana.pyalgos.generic.HBins import HBins
                    self.tbins = HBins(list(wt))

            else : # self.VERSION == 1
                npeaks = wfpkfinder_cfd(wf, self.BASE, self.THR, self.CFR, self.DEADTIME, self.LEADINGEDGE,\
                                        self._pkvals[ch,:], self._pkinds[ch,:])

            #print(' npeaks:', npeaks)
            #assert (npeaks<self.NUM_HITS), 'number of found peaks exceeds reserved array shape'
            if npeaks>=self.NUM_HITS : npeaks = self.NUM_HITS
            self._number_of_hits[ch] = npeaks
            if self.VERSION == 4 :
                self._pktsec[ch, :npeaks] = np.array(t_list)[:npeaks]
            else:
                self._pktsec[ch, :npeaks] = wt[self._pkinds[ch, :npeaks]] #sec

        self._wfs_old = wfs
Ejemplo n.º 7
0
def do_work():

    prefix = './%s-figs-ti_vs_tj' % gu.str_tstamp(
        fmt='%Y-%m-%d', time_sec=None)  # '%Y-%m-%dT%H:%M:%S%z'
    gu.create_directory(prefix, mode=0o775)

    path = os.path.abspath(os.path.dirname(__file__))
    print('path to npy flies dir:', path)

    ti_vs_tj = np.load('%s/ti_vs_tj.npy' % path)
    t_all = np.load('%s/t_all.npy' % path)

    trange = (1400., 2900.)

    print_ndarr(ti_vs_tj, 'ti_vs_tj:\n')
    print_ndarr(t_all, 't_all:\n')

    sum_bkg = t_all.sum()
    sum_cor = ti_vs_tj.sum()

    print('sum_bkg:', sum_bkg)
    print('sum_cor:', sum_cor)

    imrange = trange + trange  # (1400., 2900., 1400., 2900.)
    axim = gr.plotImageLarge(ti_vs_tj, img_range=imrange, amp_range=(0,500), figsize=(11,10),\
                             title='ti_vs_tj', origin='lower', window=(0.10, 0.08, 0.88, 0.88), cmap='inferno')
    gr.save('%s/fig-ti_vs_tj.png' % prefix)

    bkg = np.outer(t_all, t_all) / sum_bkg
    print_ndarr(bkg, 'bkg:\n')
    axim = gr.plotImageLarge(bkg, img_range=imrange, amp_range=(0,500), figsize=(11,10),\
                             title='bkg', origin='lower', window=(0.10, 0.08, 0.88, 0.88), cmap='inferno')
    gr.save('%s/fig-ti_vs_tj-bkg.png' % prefix)

    harr = t_all
    nbins = harr.size
    ht = HBins(trange, nbins, vtype=np.float32)  # ht.binedges()
    fig, axhi, hi = gr.hist1d(ht.bincenters(), bins=nbins, amp_range=ht.limits(), weights=harr, color='b', show_stat=True,\
                              log=True, figsize=(7,6), axwin=(0.10, 0.10, 0.88, 0.85), title='1-d bkg',\
                              xlabel='time of all hits (ns)', ylabel='number of hits', titwin='1-d bkg')
    gr.save('%s/fig-time-hits.png' % prefix)
    gr.show()
Ejemplo n.º 8
0
    def __init__(self, proc, **kwargs):

        self.proc = proc

        logger.info('In set_parameters, **kwargs: %s' % str(kwargs))
        self.STAT_NHITS = kwargs.get('STAT_NHITS', True)
        self.STAT_TIME_CH = kwargs.get('STAT_TIME_CH', True)
        self.STAT_UVW = kwargs.get('STAT_UVW', True)
        self.STAT_TIME_SUMS = kwargs.get('STAT_TIME_SUMS', True)
        self.STAT_CORRELATIONS = kwargs.get('STAT_CORRELATIONS', True)
        self.STAT_XY_COMPONENTS = kwargs.get('STAT_XY_COMPONENTS', True)
        self.STAT_XY_2D = kwargs.get('STAT_XY_2D', True)
        self.STAT_MISC = kwargs.get('STAT_MISC', True)
        self.STAT_REFLECTIONS = kwargs.get('STAT_REFLECTIONS', True)
        self.STAT_PHYSICS = kwargs.get('STAT_PHYSICS', True)
        self.STAT_XY_RESOLUTION = kwargs.get('STAT_XY_RESOLUTION',
                                             False)  # not available for QUAD

        if self.STAT_TIME_CH:
            self.lst_u1 = []
            self.lst_u2 = []
            self.lst_v1 = []
            self.lst_v2 = []
            self.lst_w1 = []
            self.lst_w2 = []
            self.lst_mcp = []

        if self.STAT_NHITS:
            self.lst_nhits_u1 = []
            self.lst_nhits_u2 = []
            self.lst_nhits_v1 = []
            self.lst_nhits_v2 = []
            self.lst_nhits_w1 = []
            self.lst_nhits_w2 = []
            self.lst_nhits_mcp = []
            self.lst_nparts = []

        if self.STAT_UVW or self.STAT_CORRELATIONS:
            self.lst_u_ns = []
            self.lst_v_ns = []
            self.lst_w_ns = []
            self.lst_u = []
            self.lst_v = []
            self.lst_w = []

        if self.STAT_TIME_SUMS or self.STAT_CORRELATIONS:
            self.lst_time_sum_u = []
            self.lst_time_sum_v = []
            self.lst_time_sum_w = []

            self.lst_time_sum_u_corr = []
            self.lst_time_sum_v_corr = []
            self.lst_time_sum_w_corr = []

        if self.STAT_XY_COMPONENTS:
            self.lst_Xuv = []
            self.lst_Xuw = []
            self.lst_Xvw = []
            self.lst_Yuv = []
            self.lst_Yuw = []
            self.lst_Yvw = []

        if self.STAT_MISC:
            self.list_dr = []
            self.lst_consist_indicator = []
            self.lst_rec_method = []

        if self.STAT_XY_RESOLUTION:
            self.lst_binx = []
            self.lst_biny = []
            self.lst_resol_fwhm = []

        if self.STAT_REFLECTIONS:
            self.lst_refl_u1 = []
            self.lst_refl_u2 = []
            self.lst_refl_v1 = []
            self.lst_refl_v2 = []
            self.lst_refl_w1 = []
            self.lst_refl_w2 = []

        if self.STAT_XY_2D:
            # images
            nbins = 360
            self.img_x_bins = HBins((-45., 45.), nbins, vtype=np.float32)
            self.img_y_bins = HBins((-45., 45.), nbins, vtype=np.float32)
            self.img_xy_uv = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_uw = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_vw = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_1 = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_2 = np.zeros((nbins, nbins), dtype=np.float32)

        if self.STAT_PHYSICS:
            t_ns_nbins = 300
            self.t_ns_bins = HBins((1400., 2900.),
                                   t_ns_nbins,
                                   vtype=np.float32)
            #self.t1_vs_t0 = np.zeros((t_ns_nbins, t_ns_nbins), dtype=np.float32)

            self.ti_vs_tj = np.zeros((t_ns_nbins, t_ns_nbins),
                                     dtype=np.float32)
            self.t_all = np.zeros((t_ns_nbins, ), dtype=np.float32)
            self.lst_t_all = []

            x_mm_nbins = 200
            y_mm_nbins = 200
            r_mm_nbins = 200
            self.x_mm_bins = HBins((-50., 50.), x_mm_nbins, vtype=np.float32)
            self.y_mm_bins = HBins((-50., 50.), y_mm_nbins, vtype=np.float32)
            self.r_mm_bins = HBins((-50., 50.), r_mm_nbins, vtype=np.float32)
            #self.x_vs_t0 = np.zeros((x_mm_nbins, t_ns_nbins), dtype=np.float32)
            #self.y_vs_t0 = np.zeros((y_mm_nbins, t_ns_nbins), dtype=np.float32)
            self.rsx_vs_t = np.zeros((r_mm_nbins, t_ns_nbins),
                                     dtype=np.float32)
            self.rsy_vs_t = np.zeros((r_mm_nbins, t_ns_nbins),
                                     dtype=np.float32)
Ejemplo n.º 9
0
    def __init__(self):

        # set default parameters
        self.set_parameters()

        if self.PLOT_TIME_CH:
            self.lst_u1 = []
            self.lst_u2 = []
            self.lst_v1 = []
            self.lst_v2 = []
            self.lst_w1 = []
            self.lst_w2 = []
            self.lst_mcp = []

        if self.PLOT_NHITS:
            self.lst_nhits_u1 = []
            self.lst_nhits_u2 = []
            self.lst_nhits_v1 = []
            self.lst_nhits_v2 = []
            self.lst_nhits_w1 = []
            self.lst_nhits_w2 = []
            self.lst_nhits_mcp = []
            self.lst_nparts = []

        if self.PLOT_UVW or self.PLOT_CORRELATIONS:
            self.lst_u_ns = []
            self.lst_v_ns = []
            self.lst_w_ns = []
            self.lst_u = []
            self.lst_v = []
            self.lst_w = []

        if self.PLOT_TIME_SUMS or self.PLOT_CORRELATIONS:
            self.lst_time_sum_u = []
            self.lst_time_sum_v = []
            self.lst_time_sum_w = []

            self.lst_time_sum_u_corr = []
            self.lst_time_sum_v_corr = []
            self.lst_time_sum_w_corr = []

        if self.PLOT_XY_COMPONENTS:
            self.lst_Xuv = []
            self.lst_Xuw = []
            self.lst_Xvw = []
            self.lst_Yuv = []
            self.lst_Yuw = []
            self.lst_Yvw = []

        if self.PLOT_MISC:
            self.list_dr = []
            self.lst_consist_indicator = []
            self.lst_rec_method = []

        #if self.PLOT_XY_RESOLUTION :
        #    self.lst_binx = []
        #    self.lst_biny = []
        #    self.lst_resol_fwhm = []

        if self.PLOT_REFLECTIONS:
            self.lst_refl_u1 = []
            self.lst_refl_u2 = []
            self.lst_refl_v1 = []
            self.lst_refl_v2 = []
            self.lst_refl_w1 = []
            self.lst_refl_w2 = []

        if self.PLOT_XY_2D:
            # images
            nbins = 360
            self.img_x_bins = HBins((-45., 45.), nbins, vtype=np.float32)
            self.img_y_bins = HBins((-45., 45.), nbins, vtype=np.float32)
            self.img_xy_uv = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_uw = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_vw = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_1 = np.zeros((nbins, nbins), dtype=np.float32)
            self.img_xy_2 = np.zeros((nbins, nbins), dtype=np.float32)

        if self.PLOT_PHYSICS:
            t_ns_nbins = 300
            self.t_ns_bins = HBins((1400., 2900.),
                                   t_ns_nbins,
                                   vtype=np.float32)
            #self.t1_vs_t0 = np.zeros((t_ns_nbins, t_ns_nbins), dtype=np.float32)

            self.ti_vs_tj = np.zeros((t_ns_nbins, t_ns_nbins),
                                     dtype=np.float32)
            self.t_all = np.zeros((t_ns_nbins, ), dtype=np.float32)
            self.lst_t_all = []

            x_mm_nbins = 200
            y_mm_nbins = 200
            r_mm_nbins = 200
            self.x_mm_bins = HBins((-50., 50.), x_mm_nbins, vtype=np.float32)
            self.y_mm_bins = HBins((-50., 50.), y_mm_nbins, vtype=np.float32)
            self.r_mm_bins = HBins((-50., 50.), r_mm_nbins, vtype=np.float32)
            #self.x_vs_t0 = np.zeros((x_mm_nbins, t_ns_nbins), dtype=np.float32)
            #self.y_vs_t0 = np.zeros((y_mm_nbins, t_ns_nbins), dtype=np.float32)
            self.rsx_vs_t = np.zeros((r_mm_nbins, t_ns_nbins),
                                     dtype=np.float32)
            self.rsy_vs_t = np.zeros((r_mm_nbins, t_ns_nbins),
                                     dtype=np.float32)