File indexing completed on 2026-04-09 07:48:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 def _cf_dump( msg, val, bins, label):
0022 log.warning("%s for \"%s\" " % (msg, label) )
0023 log.warning(" val %s " % repr(val) )
0024 log.warning(" bins %s " % repr(bins) )
0025
0026 def __cf_hist( ax, val, bins, log_, label):
0027 """
0028 ax.hist gives errors for empty histos so use np.histogram and plot counts and bins afterwards
0029 """
0030 c, b = np.histogram(val, bins=bins)
0031 p = ax.plot( bins[:-1], c , drawstyle="steps", label=label )
0032 if log_:
0033 ax.set_yscale('log')
0034 return c, b, p
0035
0036 def _cf_hist( ax, val, bins, log_, label):
0037 c, b, p = None, None, None
0038 try:
0039 c,b,p = __cf_hist(ax, val, bins, log_, label)
0040 except IndexError:
0041 _cf_dump("_cf_hist IndexError", val, bins, label)
0042 except ValueError:
0043 _cf_dump("_cf_hist ValueError", val, bins, label)
0044 pass
0045 return c, b, p
0046
0047
0048 def _cf_plot(ax, aval, bval, bins, labels, log_=False):
0049 cnt = {}
0050 bns = {}
0051 ptc = {}
0052
0053 cnt[0], bns[0], ptc[0] = __cf_hist(ax, aval, bins=bins, log_=log_, label=labels[0])
0054 cnt[1], bns[1], ptc[1] = __cf_hist(ax, bval, bins=bins, log_=log_, label=labels[1])
0055
0056 return cnt, bns
0057
0058 def _chi2_plot(ax, _bins, counts, cut=30):
0059 a,b = counts[0],counts[1]
0060
0061 if a is None or b is None:
0062 log.warning("skip chi2 plot as got None %s %s " % (repr(a), repr(b)))
0063 return 0
0064
0065 c2, c2n, c2c = chi2(a, b, cut=cut)
0066 ndf = max(c2n - 1, 1)
0067
0068 c2p = c2.sum()/ndf
0069
0070 label = "chi2/ndf %4.2f [%d]" % (c2p, ndf)
0071
0072 ax.plot( _bins[:-1], c2, drawstyle='steps', label=label )
0073
0074 return c2p
0075
0076
0077 def old_cfplot(fig, gss, _bins, aval, bval, labels=["A","B"], log_=False, c2_cut=30, c2_ymax=10, logyfac=3., linyfac=1.3):
0078
0079 ax = fig.add_subplot(gss[0])
0080
0081 cnt, bns = _cf_plot(ax, aval, bval, bins=_bins, labels=labels, log_=log_)
0082
0083 ymin = 1 if log_ else 0
0084 yfac = logyfac if log_ else linyfac
0085
0086 ymax = 0
0087 for k,v in cnt.items():
0088 if v is None:continue
0089 vmax = v.max()
0090 ymax = max(ymax, vmax)
0091 pass
0092
0093 ylim = [ymin,ymax*yfac]
0094
0095 ax.set_ylim(ylim)
0096 ax.legend()
0097 xlim = ax.get_xlim()
0098
0099
0100 ax = fig.add_subplot(gss[1])
0101
0102 c2p = _chi2_plot(ax, _bins, cnt, cut=c2_cut)
0103
0104 ax.set_xlim(xlim)
0105 ax.legend()
0106 ax.set_ylim([0,c2_ymax])
0107
0108 return c2p
0109
0110
0111 def old_qwns_plot(ab, qwns, irec, log_=False, c2_cut=30):
0112
0113 log.info("qwns_plot(scf, \"%s\", %d, log_=%s, c2_cut=%d )" % (qwns, irec, log_ , c2_cut))
0114
0115 fig = plt.figure()
0116
0117 ab.irec = irec
0118
0119 fig.suptitle(ab.suptitle)
0120
0121 nx = len(qwns)
0122
0123 gs = gridspec.GridSpec(2, nx, height_ratios=[3,1])
0124
0125 c2ps = []
0126 for ix in range(nx):
0127
0128 gss = [gs[ix], gs[nx+ix]]
0129
0130 qwn = qwns[ix]
0131
0132 bns, aval, bval, labels = ab.rqwn(qwn, irec)
0133
0134 c2p = cfplot(fig, gss, bns, aval, bval, labels=labels, log_=log_, c2_cut=c2_cut )
0135
0136 c2ps.append(c2p)
0137 pass
0138
0139 qd = odict(zip(list(qwns),c2ps))
0140 return qd
0141
0142
0143
0144
0145
0146 def old_qwn_plot(ab, qwn, irec, log_=False, c2_cut=30, c2_ymax=10):
0147
0148 ab.irec = irec
0149
0150 rqwn_bins, aval, bval, labels = ab.rqwn(qwn, irec)
0151
0152 fig = plt.figure()
0153 fig.suptitle(ab.suptitle)
0154
0155 nx,ix = 1,0
0156 gs = gridspec.GridSpec(2, nx, height_ratios=[3,1])
0157 gss = [gs[ix], gs[nx+ix]]
0158
0159 c2p = cfplot(fig, gss, rqwn_bins, aval, bval, labels=labels, log_=log_, c2_cut=c2_cut, c2_ymax=c2_ymax)
0160 c2ps = [c2p]
0161
0162
0163
0164 qd = odict(zip(list(qwn),c2ps))
0165 return qd
0166
0167
0168
0169 def test_old_cfplot():
0170
0171 aval = np.random.standard_normal(8000)
0172 bval = np.random.standard_normal(8000)
0173 bins = np.linspace(-4,4,200)
0174 log_ = False
0175
0176 fig = plt.figure()
0177 fig.suptitle("cfplot test")
0178
0179 nx = 4
0180 gs = gridspec.GridSpec(2, nx, height_ratios=[3,1])
0181 for ix in range(nx):
0182 gss = [gs[ix], gs[nx+ix]]
0183 cfplot(fig, gss, bins, aval, bval, labels=["A test", "B test"], log_=log_ )
0184
0185
0186