def test_outliers(): """ Check that out-of-range values work as expected """ H = Histogram(nbins=[5, 10], ranges=[[-2.5, 2.5], [-1, 1]]) H.fill(np.array([[1, 1], ])) val1 = H.get_value((100, 100), outlier_value=-10000) val2 = H.get_value((-100, 0), outlier_value=None) assert val1 == -10000 assert val2 == 0
def test_outliers(): """ Check that out-of-range values work as expected """ H = Histogram(nbins=[5, 10], ranges=[[-2.5, 2.5], [-1, 1]]) H.fill(np.array([ [1, 1], ])) val1 = H.get_value((100, 100), outlier_value=-10000) val2 = H.get_value((-100, 0), outlier_value=None) assert val1 == -10000 assert val2 == 0
def test_histogram_resample_inplace(): hist = Histogram(nbins=[5, 11], ranges=[[-2.5, 2.5], [-1, 1]]) hist.fill(np.array([[0, 0], [0, 0.5]])) for testpoint in [(0, 0), (0, 1), (1, 0), (3, 3)]: val0 = hist.get_value(testpoint) hist.resample_inplace((10, 22)) hist.resample_inplace((5, 11)) val2 = hist.get_value(testpoint) # at least check the resampling is undoable assert np.isclose(val0[0], val2[0])
def test_histogram_fits(histogram_file): """ Write to fits,read back, and check """ hist = Histogram(nbins=[5, 11], ranges=[[-2.5, 2.5], [-1, 1]]) hist.fill(np.array([[0, 0], [0, 0.5]])) hist.to_fits().writeto(histogram_file, overwrite=True) newhist = Histogram.from_fits(histogram_file) # check that the values are the same compare_histograms(hist, newhist)
def test_histogram_range_fill_and_read(): """ Check that the correct bin is read and written for multiple binnings and fill positions """ num = 100 for nxbins in np.arange(1, 50, 1): for xx in np.arange(-2.0, 2.0, 0.1): pp = (xx + 0.01829384, 0.1) coords = np.ones((num, 2)) * np.array(pp) hist = Histogram(nbins=[nxbins, 10], ranges=[[-2.5, 2.5], [-1, 1]]) hist.fill(coords) val = hist.get_value(pp)[0] assert val == num del hist
def test_histogram_fill_and_read(): hist = Histogram(nbins=[5, 10], ranges=[[-2.5, 2.5], [-1, 1]]) pa = (0.1, 0.1) pb = (-0.55, 0.55) a = np.ones((100, 2)) * pa # point at 0.1,0.1 b = np.ones((10, 2)) * pb # 10 points at -0.5,0.5 hist.fill(a) hist.fill(b) va = hist.get_value(pa)[0] vb = hist.get_value(pb)[0] assert va == 100 assert vb == 10
def plot_muon_efficiency(outputpath): """ Plot the muon efficiencies """ fig, ax = plt.subplots(1, 1, figsize=(10, 10)) figip, axip = plt.subplots(1, 1, figsize=(10, 10)) figrw, axrw = plt.subplots(1, 1, figsize=(10, 10)) nbins = 16 t = Table.read(str(outputpath) + '_muontable.fits') print('Reading muon efficiency from table', outputpath, t['MuonEff']) if len(t['MuonEff']) < 1: print("No muon events to plot") return else: print("Found", len(t['MuonEff']), "muon events") (mu, sigma) = norm.fit(t['MuonEff']) print('Gaussian fit with mu=', mu, 'sigma=', sigma) conteff = ax.hist(t['MuonEff'], nbins) ax.set_xlim(0.2 * min(t['MuonEff']), 1.2 * max(t['MuonEff'])) xtest = np.linspace(min(t['MuonEff']), max(t['MuonEff']), nbins) yg = mlab.normpdf(xtest, mu, sigma) print('mu', mu, 'sigma', sigma, 'yg', yg) ax.plot(xtest, yg, 'r', linewidth=2) ax.set_ylim(0., 1.2 * max(conteff[0])) ax.set_xlabel('Muon Efficiency') plt.draw() yimp = np.linspace(min(t['ImpactP']), max(t['ImpactP']), nbins) contimp = axip.hist(t['ImpactP'], nbins) axip.set_xlim(0.2 * min(t['ImpactP']), 1.2 * max(t['ImpactP'])) axip.set_ylim(0., 1.2 * max(contimp[0])) axip.set_xlabel('Impact Parameter (m)') plt.draw() heffimp = Histogram(nbins=[16, 16], ranges=[ (min(t['MuonEff']), max(t['MuonEff'])), (min(t['ImpactP']), max(t['ImpactP'])) ]) # ,axisNames=["MuonEfficiency","ImpactParameter"]) heffimp.fill([t['MuonEff'], t['ImpactP']]) heffimp.draw_2d() yrw = np.linspace(min(t['RingWidth']), max(t['RingWidth']), nbins) contrw = axrw.hist(t['RingWidth'], nbins) axrw.set_xlim(0.2 * min(t['RingWidth']), 1.2 * max(t['RingWidth'])) axrw.set_ylim(0., 1.2 * max(contrw[0])) axrw.set_xlabel('Ring Width ($^\circ$)') plt.draw() if outputpath is not None: print("saving figure at", outputpath) fig.savefig(str(outputpath) + '_MuonEff.png') figip.savefig(str(outputpath) + '_ImpactParameter.png') figrw.savefig(str(outputpath) + '_RingWidth.png') else: print("Not saving figure, no outputpath") plt.show()
def plot_muon_efficiency(outputpath): """ Plot the muon efficiencies """ fig, ax = plt.subplots(1, 1, figsize=(10, 10)) figip, axip = plt.subplots(1, 1, figsize=(10, 10)) figrw, axrw = plt.subplots(1, 1, figsize=(10, 10)) nbins = 16 t = Table.read(str(outputpath) + '_muontable.fits') print('Reading muon efficiency from table', outputpath, t['MuonEff']) if len(t['MuonEff']) < 1: print("No muon events to plot") return else: print("Found", len(t['MuonEff']), "muon events") (mu, sigma) = norm.fit(t['MuonEff']) print('Gaussian fit with mu=', mu, 'sigma=', sigma) #ax = figeff.add_subplot(1,3,1) conteff = ax.hist(t['MuonEff'], nbins) ax.set_xlim(0.2 * min(t['MuonEff']), 1.2 * max(t['MuonEff'])) xtest = np.linspace(min(t['MuonEff']), max(t['MuonEff']), nbins) yg = mlab.normpdf(xtest, mu, sigma) print('mu', mu, 'sigma', sigma, 'yg', yg) ax.plot(xtest, yg, 'r', linewidth=2) ax.set_ylim(0., 1.2 * max(conteff[0])) ax.set_xlabel('Muon Efficiency') # plt.figure(fig.number) plt.draw() #axip = figeff.add_subplot(1,3,1) yimp = np.linspace(min(t['ImpactP']), max(t['ImpactP']), nbins) contimp = axip.hist(t['ImpactP'], nbins) axip.set_xlim(0.2 * min(t['ImpactP']), 1.2 * max(t['ImpactP'])) axip.set_ylim(0., 1.2 * max(contimp[0])) axip.set_xlabel('Impact Parameter (m)') # plt.figure(figip.number) plt.draw() heffimp = Histogram(nbins=[16, 16], ranges=[(min(t['MuonEff']), max(t['MuonEff'])), (min(t['ImpactP']), max(t['ImpactP']))]) # ,axisNames=["MuonEfficiency","ImpactParameter"]) # embed() # heffimp.bin_lower_edges([xtest,yimp]) heffimp.fill([t['MuonEff'], t['ImpactP']]) heffimp.draw_2d() #axrw = figeff.add_subplot(1,3,1) yrw = np.linspace(min(t['RingWidth']), max(t['RingWidth']), nbins) contrw = axrw.hist(t['RingWidth'], nbins) axrw.set_xlim(0.2 * min(t['RingWidth']), 1.2 * max(t['RingWidth'])) axrw.set_ylim(0., 1.2 * max(contrw[0])) axrw.set_xlabel('Ring Width ($^\circ$)') # plt.figure(figrw.number) plt.draw() # plt.show() if outputpath is not None: print("saving figure at", outputpath) fig.savefig(str(outputpath) + '_MuonEff.png') figip.savefig(str(outputpath) + '_ImpactParameter.png') figrw.savefig(str(outputpath) + '_RingWidth.png') else: print("Not saving figure, no outputpath") plt.show()
def plot_muon_efficiency(outputpath): """ Plot the muon efficiencies """ fig, ax = plt.subplots(1, 1, figsize=(10, 10)) figip, axip = plt.subplots(1, 1, figsize=(10, 10)) figrw, axrw = plt.subplots(1, 1, figsize=(10, 10)) nbins = 16 t = Table.read(str(outputpath) + '_muontable.fits') logger.info('Reading muon efficiency from table "%s"', outputpath) if len(t['MuonEff']) < 1: logger.warning("No muon events to plot") return else: logger.info("Found %d muon events", len(t['MuonEff'])) (mu, sigma) = norm.fit(t['MuonEff']) logger.debug('Gaussian fit with mu=%f, sigma=%f', mu, sigma) conteff = ax.hist(t['MuonEff'], nbins) ax.set_xlim(0.2 * min(t['MuonEff']), 1.2 * max(t['MuonEff'])) xtest = np.linspace(min(t['MuonEff']), max(t['MuonEff']), nbins) yg = mlab.normpdf(xtest, mu, sigma) logger.debug('mu=%f sigma=%f yg=%f', mu, sigma, yg) ax.plot(xtest, yg, 'r', linewidth=2) ax.set_ylim(0., 1.2 * max(conteff[0])) ax.set_xlabel('Muon Efficiency') plt.draw() contimp = axip.hist(t['ImpactP'], nbins) axip.set_xlim(0.2 * min(t['ImpactP']), 1.2 * max(t['ImpactP'])) axip.set_ylim(0., 1.2 * max(contimp[0])) axip.set_xlabel('Impact Parameter (m)') plt.draw() heffimp = Histogram(nbins=[16, 16], ranges=[(min(t['MuonEff']), max(t['MuonEff'])), (min(t['ImpactP']), max(t[ 'ImpactP']))]) # # ,axisNames=["MuonEfficiency","ImpactParameter"]) heffimp.fill([t['MuonEff'], t['ImpactP']]) heffimp.draw_2d() contrw = axrw.hist(t['RingWidth'], nbins) axrw.set_xlim(0.2 * min(t['RingWidth']), 1.2 * max(t['RingWidth'])) axrw.set_ylim(0., 1.2 * max(contrw[0])) axrw.set_xlabel('Ring Width ($^\circ$)') plt.draw() if outputpath is not None: logger.info("saving figure to '%s'", outputpath) fig.savefig(str(outputpath) + '_MuonEff.png') figip.savefig(str(outputpath) + '_ImpactParameter.png') figrw.savefig(str(outputpath) + '_RingWidth.png') else: logger.info("Not saving figure, no outputpath") plt.show()