Example #1
0
def test_overflow() :
    h = Histogram(axis=Axis(100, -50., 50.))
    assert h.overflow() ==0
    h.fill(100.)
    h.fill(200.)
    h.fill(10.)
    assert h.overflow() == 2.
Example #2
0
def test_fill():
    h = Histogram(axis=Axis(100, -50, 50))
    weight = 0.25
    for i in xrange(-50,50) :
        h.fill(i+0.5, weight)
    assert h.entries() == 100
    assert h.integral() == 100*weight
Example #3
0
def test_integral() :
    h = Histogram(axis=Axis(100, -50., 50.))
    weight = 0.5
    for x in xrange(10) :
        h.fill(-49.99, weight) # should be 0th bin
        h.fill(49.99, weight) # should be 100th bin, ie index 99
        h.fill(0.1, 2*weight)
    assert h.integral() == 10*4*weight
Example #4
0
def test_binheight() :
    h = Histogram(axis=Axis(100, -50., 50.))
    weight = 0.5
    h.fill(-49.99, weight) # should be 0th bin
    h.fill(49.99, weight) # should be 100th bin, ie index 99
    h.fill(0.1, 2*weight)
    assert h.binHeight(0) == weight
    assert h.binHeight(99) == weight
    assert h.binHeight(50) == 2*weight
Example #5
0
def test_mean() :
    h0 = Histogram(axis=Axis(100, -50, 50))
    h1 = Histogram(axis=Axis(100, -50, 50))
    for x in xrange(-50, 50) :
        h0.fill(x + 0.5)
        if x + 0.5 > 0 :
            h1.fill(x+0.5)
    assert h1.mean() == 25.
    assert h0.mean() == 0
Example #6
0
def test_multiply_histograms() :
    from random import gauss
    h0 = Histogram(axis=Axis(100, -50., 50.))
    h1 = Histogram(axis=Axis(100, -50., 50.))
    dist = [gauss(0.,20) for x in xrange(100)]
    h0.fill(dist)
    h1.fill(dist, 2.5)
    h2 = h0*h1
    for bin in xrange(h2.axis.nbins):
        assert h2.binHeight(bin) == h0.binHeight(bin) * h1.binHeight(bin)
Example #7
0
def test_divide_histograms() :
    from random import gauss
    h0 = Histogram(axis=Axis(100, -50., 50.))
    h1 = Histogram(axis=Axis(100, -50., 50.))
    dist = [gauss(0.,10) for x in xrange(100)]
    h0.fill(dist)
    h1.fill(dist, 5.)
    h2 = h0/h1
    for bin in xrange(h2.axis.nbins):
        if h1.binHeight(bin) != 0 :
            assert h2.binHeight(bin) == h0.binHeight(bin) / h1.binHeight(bin)
Example #8
0
def ntuple_column_histo(ntuple, tag, cut=None, bins=100):
    '''
    Create and return a histogram.Histogram object constructed from the 
    contents of an ntuple.NTuple column.
    '''
    from PyAna.pyhistogram.histogram import Histogram, Axis
    col = ntuple.column(tag, cut)
    min_range = min(col)
    max_range = max(col)
    width = (max_range - min_range) / bins
    max_range += width / 2.
    max_range -= width / 2.
    histo = Histogram(axis=Axis(bins, min_range, max_range, label=tag))
    for x in col:
        histo.fill(x)
    return histo
Example #9
0
def test_underflow() :
    h = Histogram(axis=Axis(100, -50., 50.))
    assert h.underflow() == 0.
    h.fill(-100.)
    assert h.underflow() == 1.
Example #10
0
def test_fill_with_weight():
    import math
    h = Histogram(axis=Axis(100, -50, 50))
    for i in xrange(-50,50) :
        w = math.sin(i)
        h.fill(i, w)
Example #11
0
def test_fill_from_list():
    h = Histogram(axis=Axis(100, -50, 50))
    x = [float(i) for i in xrange(-49,50)]
    h.fill(x)
    assert h.entries() == 99
Example #12
0
def test_min_and_max() :
    h0 = Histogram(axis=Axis(20, -10., 10.))
    h0.fill([-5.5,6.5])
    assert h0.min() == -6.0
    assert h0.max() == 7.0
Example #13
0
def test_sigma() :
    h0 = Histogram(axis=Axis(100, -50., 50.))
    h0.fill([5.5,-5.5,100.5,-100.5])
    assert h0.sigma() == 5.5
Example #14
0
def test_subtract_histograms() :
    h0 = Histogram(axis=Axis(100, -50., 50.))
    weight = 0.5
    h0.fill([-49.99,49.99,-48.99,48.99], weight) # should be 0th bin
    h0.fill(0.1, 4*weight)
    h0.fill(-75., 32.)
    h0.fill(100., 55)
    h1 = Histogram(axis=Axis(100, -50., 50.))
    h1.fill([-48.99,48.99], weight) # should be 0th bin
    h1.fill(0.1, 2*weight)
    h1.fill(-101.)
    h1.fill(200.,2)
    h2 = h0 - h1
    
    assert h2.axis == h0.axis
    assert h2.entries() == h0.entries() - h1.entries()
    assert h2.overflow() == h0.overflow() - h1.overflow()
    assert h2.underflow() == h0.underflow() - h1.underflow()

    for bin in xrange(h2.axis.nbins):
        assert h2.binHeight(bin) == h0.binHeight(bin) - h1.binHeight(bin)
Example #15
0
def test_entries_does_not_count_out_of_range_entries() :
    h = Histogram(axis=Axis(100, -50., 50.))
    h.fill(45.5, 10.)
    h.fill(100.)
    h.fill(-100)
    assert h.entries() == 3.