示例#1
0
def test_morphing1():

    logger = getLogger('test_morphing1')
    if ROOT.gROOT.GetVersionInt() < 62301:
        logger.warning('Test is disabled for ROOT version %s' %
                       ROOT.gROOT.GetVersion())
        return

    pdf1 = Models.Gauss_pdf('G1', xvar=mass, mean=10, sigma=1)
    pdf2 = Models.Gauss_pdf('G2', xvar=mass, mean=10, sigma=2)
    pdf3 = Models.Gauss_pdf('G3', xvar=mass, mean=10, sigma=3)
    pdf4 = Models.Gauss_pdf('G4', xvar=mass, mean=10, sigma=4)

    pdf = Morphing1D_pdf('M1', {
        1.0: pdf1,
        2.0: pdf2,
        3.0: pdf3,
        4.0: pdf4,
    },
                         xvar=mass)

    for mu in vrange(1, 3, 6):
        pdf.mu = mu
        logger.info('Mu= %s' % mu)
        pdf.draw()

    r, f = pdf.fitHisto(h1, draw=True, nbins=100, silent=True)
    logger.info('Morphing: \n%s' % r.table(prefix="# "))
示例#2
0
def test_morphing1():

    pdf1 = Models.Gauss_pdf('G1', xvar=mass, mean=10, sigma=1)
    pdf2 = Models.Gauss_pdf('G2', xvar=mass, mean=10, sigma=2)
    pdf3 = Models.Gauss_pdf('G3', xvar=mass, mean=10, sigma=3)
    pdf4 = Models.Gauss_pdf('G4', xvar=mass, mean=10, sigma=4)

    pdf = Morphing1D_pdf('M', {
        1.0: pdf1,
        2.0: pdf2,
        3.0: pdf3,
        4.0: pdf4,
    },
                         xvar=mass)

    for mu in vrange(1, 3, 6):
        pdf.mu = mu
        logger.info('Mu= %s' % mu)
        pdf.draw()

    r, f = pdf.fitHisto(h1, draw=True, nbins=100, silent=True)
    logger.info('Morphing: \n%s' % r.table(prefix="# "))
    rw.reweight(original=dmc, target=ddata)

    ## new weights
    wnew = rw.weight(original=dmc)
    mc.chain.add_new_branch('w', wnew)

    ## reload data
    data = Data('DATA_tree', testdata)
    mc = Data(tag_mc, testdata)

    wsum = mc.chain.sumVar('w')
    wvar = '%d*w/%s' % (len(data.chain), wsum.value())

    nn = '%s' % (len(data.chain) * 1.0 / len(mc.chain))

    for phi in vrange(0, 2 * math.pi, 10):

        dvar = '%.5f*x+%.5f*y' % (math.cos(phi), math.sin(phi))

        mn, mx = data.chain.statVar(dvar).minmax()
        h1 = ROOT.TH1D(hID(), '', 100, *axis_range(mn, mx, delta=0.05))
        h2 = h1.clone()
        h3 = h1.clone()

        data.chain.project(h1, dvar)  ## data
        mc.chain.project(h2, dvar, nn)  ## original (non-weighted) MC
        mc.chain.project(h3, dvar, wvar)  ## weighted  MC

        mn, mx = h1.minmax()
        mn, mx = axis_range(0, mx, delta=0.7)
        h1.SetMaximum(mx)
示例#4
0
文件: dalitz.py 项目: OstapHEP/ostap
def _dp0_points_(dp, M, npoints=250):
    """Make a graph of Dalitz plot
    >>> d = Dalitz ( 0.1   , 0.2 , 0.3 )
    >>> points = d.points ( M = 5 )
    """

    m1 = dp.m1()
    m2 = dp.m2()
    m3 = dp.m3()

    assert m1 + m2 + m3 <= M, \
           'Dalitz0.points: Invalid mass %s>%s+%s+%s' %  ( M , m1 , m2 , m3 )

    s = M * M

    s1_min = dp.s1_min()
    s1_max = dp.s1_max(M)

    s2_min = dp.s2_min()
    s2_max = dp.s2_max(M)

    s3_min = dp.s3_min()
    s3_max = dp.s3_max(M)

    ## four reference minmax points ..
    P = [(s1_min, dp.s2_minmax_for_s_s1(s, s1_min).first),
         (dp.s1_minmax_for_s_s2(s, s2_max).second, s2_max),
         (s1_max, dp.s2_minmax_for_s_s1(s, s1_max).second),
         (dp.s1_minmax_for_s_s2(s, s2_min).first, s2_min)]

    P = [p for p in P if s1_min <= p[0] <= s1_max and s2_min <= p[1] <= s2_max]

    pnts = []

    from ostap.utils.utils import vrange

    ## fill branches 1 and 3
    for v in vrange(s1_min, s1_max, npoints):

        s1 = v
        s2 = dp.s2_minmax_for_s_s1(s, s1)

        s2min = s2.first
        s2max = s2.second
        if not s2min < s2max: continue

        x = s1
        y1, y2 = s2min, s2max

        if s1_min <= x <= s1_max:
            if s2_min <= y1 <= s2_max: pnts.append((x, y1))
            if s2_min <= y2 <= s2_max: pnts.append((x, y2))

    ## fill branches 2 and 4 :
    for v in vrange(s2_min, s2_max, npoints):

        s2 = v
        s1 = dp.s1_minmax_for_s_s2(s, s2)

        s1min = s1.first
        s1max = s1.second
        if s1min < s1max:

            y = s2
            x1, x2 = s1min, s1max

            if s2_min <= y <= s2_max:
                if s1_min <= x1 <= s1_max: pnts.append((x1, y))
                if s1_min <= x2 <= s1_max: pnts.append((x2, y))

    pnts = P + pnts
    pnts = [
        p for p in pnts if dp.inside(s, p[0], p[1])
        or abs(dp.distance(s, p[0], p[1])) < 1.e-8 * s
    ]

    ## find some point "inside" Dalitz plot
    ## first guess
    x0 = 0.5 * (s1_min + s1_max)
    y0 = 0.5 * (s2_min + s2_max)
    ## find another point if needed
    while not dp.inside(s, x0, y0):
        import random
        x0 = random.uniform(s1_min, s1_max)
        y0 = random.uniform(s2_min, s2_max)

    ## collect, eliminate the duplicates and ogranize all points according to phi

    from ostap.math.base import isequal

    points = set()
    for p in pnts:
        x, y = p
        dx = x - x0
        dy = y - y0
        phi = math.atan2(dy, dx)

        in_list = False
        for entry in points:
            if isequal(phi, entry[0]):
                in_list = True
                break

        if in_list: continue

        point = phi, x, y
        points.add(point)

    ## convert set to the list
    points = list(points)
    ## sort the list according to phi
    points.sort()
    ## make a closed loop  from the points
    if points and points[0][1:] != points[-1][1:]:
        points.append(points[0])

    return tuple([p[1:] for p in points])
示例#5
0
def test_gbreweight():

    logger = getLogger("test_gbreweight")

    try:

        from ostap.tools.reweighter import Reweighter
        rw = Reweighter()

    except ImportError:

        logger.error('GBReweighter is not available!')
        return

    if not os.path.exists(testdata):
        with timing("Prepare input data", logger=logger):
            prepare_data()

    # =========================================================================
    ## Input data/mc samples
    # =========================================================================
    data = Data('DATA_tree', testdata)
    mc = Data(tag_mc, testdata)

    ddata, wdata = data.chain.slice('x y', transpose=True)
    dmc, wmc = mc.chain.slice('x y', transpose=True)

    ## train BDT
    rw.reweight(original=dmc, target=ddata)

    ## new weights
    wnew = rw.weight(original=dmc)
    mc.chain.add_new_branch('w', wnew)

    ## reload data
    data = Data('DATA_tree', testdata)
    mc = Data(tag_mc, testdata)

    wsum = mc.chain.sumVar('w')
    wvar = '%d*w/%s' % (len(data.chain), wsum.value())

    nn = '%s' % (len(data.chain) * 1.0 / len(mc.chain))

    for phi in vrange(0, 2 * math.pi, 10):

        dvar = '%.5f*x+%.5f*y' % (math.cos(phi), math.sin(phi))

        mn, mx = data.chain.statVar(dvar).minmax()
        h1 = ROOT.TH1D(hID(), '', 100, *axis_range(mn, mx, delta=0.05))
        h2 = h1.clone()
        h3 = h1.clone()

        data.chain.project(h1, dvar)  ## data
        mc.chain.project(h2, dvar, nn)  ## original (non-weighted) MC
        mc.chain.project(h3, dvar, wvar)  ## weighted  MC

        mn, mx = h1.minmax()
        mn, mx = axis_range(0, mx, delta=0.7)
        h1.SetMaximum(mx)
        h1.blue()
        h2.green()
        h3.red()

        h1.draw('')
        h2.draw('same hist')
        h3.draw('same')

        time.sleep(2)
示例#6
0
        Berrut2nd   ( results  ) ,
        Barycentric ( results  )
        ] + [
        FloaterHormann ( results , i ) for i in range ( 10 ) ] 

    graphs = []
    with wait ( 5 ) , use_canvas ( 'test_fitresults' ) : 
        f1_draw ( sigma , linecolor=1 , linewidth = 4 , minimum = 0 , xmin = 0 , xmax = 100 )        
        gr.red()
        gr.draw ('pe1')
    
        K = 255 
        
        for i,f in enumerate ( interpolants ) : 
            g = ROOT.TGraphErrors ( K + 1 ) 
            for j,x in enumerate ( vrange ( 0 , 100 , K ) ) :
                g[j] = x , f ( x )
            graphs.append ( g )
                
        for i, g in enumerate ( graphs ) :
            g.draw ('lpe1', linecolor = i+3 , markercolor = i+3 ) 

        gr.draw ('pe1')
    
    
        
    
# =============================================================================
## if '__main__' == __name__ :
    
##    test_fitresults ()
示例#7
0
def uniform_abscissas(low, high, N):
    """Uniform interpoaltion abscissas
    for a in uniform_abscissas ( 0, 1 , 10 ) :
    ... print ( a ) 
    """
    return vrange(low, high, max(N, 2) - 1)