Exemple #1
0
def test_vars3():

    logger = getLogger('test_vars3')

    a = ROOT.RooRealVar('A', 'a', 0.05, 0, 1)
    b = ROOT.RooRealVar('B', 'b', 0.02, -0.05, 0.1)
    c = ROOT.RooRealVar('C', 'c', 0.005, 0, 0.1)

    import ostap.fitting.roofuncs as R
    from ostap.fitting.funbasic import Fun1D
    X = Fun1D(x, xvar=x, name='X')

    ##F   = (X**2) * c + X * b + a
    F = a + b * X + c * X**2

    eff2 = Efficiency1D('E5', F, cut=acc, xvar=x)

    r2 = eff2.fitTo(ds)

    logger.info("Fit result using-Fun1D \n%s" % r2.table(prefix="# "))
    logger.info("Compare with true efficiency (using Fun1D)\n%s" %
                make_table(eff2, title='using Fnu1D'))

    with wait(2), use_canvas('test_vars3'):
        f2 = eff2.draw(ds, nbins=25)
def test_pyvar():
    ## if 1 < 2 :

    from ostap.fitting.pyvar import PyVAR

    class MyVar(PyVAR):
        def evaluate(self):

            vlist = self.varlist

            _x = float(vlist[0])
            _a = float(vlist[1])
            _b = float(vlist[2])
            _c = float(vlist[3])
            _x0 = float(vlist[4])

            r = eff(_x, _a, _b, _c, _x0)

            return r

    myEff2 = MyVar('myEff2', vars=vars, title='title')

    eff2 = Efficiency1D('E2', myEff2.var, cut=acc, xvar=x)

    r2 = eff2.fitTo(ds)
    r2 = eff2.fitTo(ds)
    f2 = eff2.draw(ds, nbins=20)
    print(r2)
    for p in points:
        print(' Point/Eff %4.1f %s%% %.2f%%' %
              (p, (100 * eff2(p, error=True)).toString('(%5.2f+-%4.2f)'),
               100 * eff0(p)))
Exemple #3
0
def test_vars2 () :
    
    logger = getLogger ( 'test_vars2' )

    from ostap.fitting.roofuncs import MonotonicPoly as MP 

    f      = MP ( 'G' , xvar = x , increasing = True , power = 4 )
    f.pars = 0.6 , 0.8 , -0.1 , -0.6
    f.a    = 0.06
    f.b    = 2.72
    f.a.release ()
    f.b.release ()

    eff2   = Efficiency1D ( 'E4' , f , cut = acc  , xvar = x )
    
    r2     = eff2.fitTo ( ds )
    
    with use_canvas ( 'test_vars2' ) : 
        f2     = eff2.draw  ( ds )
    
    for p in points :
        e  = eff2 ( p , error = True )
        ev = e.value()
        e0 = eff0 ( p ) / emax  
        logger.info (' Point/Eff %4.1f %s%% (%.2f%%)'   % ( p , (100*e).toString ( '(%5.2f+-%4.2f)' ) ,  e0 * 100 ) )
def test_vars3():

    a = ROOT.RooRealVar('A', 'a', 0.05, 0, 1)
    b = ROOT.RooRealVar('B', 'b', 0.02, -0.05, 0.1)
    c = ROOT.RooRealVar('C', 'c', 0.005, 0, 0.1)

    import ostap.fitting.roofuncs as R
    from ostap.fitting.funbasic import Fun1D
    X = Fun1D(x, xvar=x, name='X')

    ##F   = (X**2) * c + X * b + a
    F = a + b * X + c * X**2

    eff2 = Efficiency1D('E5', F, cut=acc, xvar=x)

    r2 = eff2.fitTo(ds)
    f2 = eff2.draw(ds)

    print(r2)

    for p in points:
        e = eff2(p, error=True)
        ev = e.value()
        e0 = eff0(p) / emax
        print(' Point/Eff %4.1f %s%% (%.2f%%)' %
              (p, (100 * e).toString('(%5.2f+-%4.2f)'), e0 * 100))
Exemple #5
0
def test_pdf():
    effPdf = Models.PolyPos_pdf('B', xvar=x, power=6)

    eff2 = Efficiency1D('E2', effPdf, cut=acc)
    r2 = eff2.fitTo(ds)
    f2 = eff2.draw(ds)
    print r2
    for p in points:
        print ' Point/Eff %.1f %s%%' % (p, (
            100 * eff2(p, error=True)).toString('(%5.2f+-%4.2f)'))
Exemple #6
0
def test_formula():

    effFunc = ROOT.RooFormulaVar("effFunc", "a+0.5*b*(1+tanh((x-x0)*c))", vars)

    eff1 = Efficiency1D('E1', effFunc, cut=acc, xvar=x)
    r1 = eff1.fitTo(ds)
    r1 = eff1.fitTo(ds)
    f1 = eff1.draw(ds, nbins=20)
    print(r1)
    for p in points:
        print(' Point/Eff %4.1f %s%% %.2f%%' %
              (p, (100 * eff1(p, error=True)).toString('(%5.2f+-%4.2f)'),
               100 * eff0(p)))
Exemple #7
0
def test_formula():

    a = ROOT.RooRealVar('a', 'a', 0.0001, 1.e-6, 0.95)
    b = ROOT.RooRealVar('b', 'b', 1, 0, 2)
    c = ROOT.RooRealVar('c', 'c', 1, 0.01, 10)
    effFunc = ROOT.RooFormulaVar("effFunc", "(1-a)+a*cos((x-b)/c)",
                                 ROOT.RooArgList(x, a, b, c))

    eff1 = Efficiency1D('E1', effFunc, cut=acc, xvar=x)
    r1 = eff1.fitTo(ds)
    f1 = eff1.draw(ds)
    print r1
    for p in points:
        print ' Point/Eff %.1f %s%%' % (p, (
            100 * eff1(p, error=True)).toString('(%5.2f+-%4.2f)'))
def test_pyvar2():

    from ostap.fitting.pyvar import PyVAR2

    myEff3 = PyVAR2(name='myEff3', vars=vars, function=eff)

    eff3 = Efficiency1D('E3', myEff3.var, cut=acc, xvar=x)

    r2 = eff3.fitTo(ds)
    r2 = eff3.fitTo(ds)
    f2 = eff3.draw(ds, nbins=20)
    print(r2)
    for p in points:
        print(' Point/Eff %4.1f %s%% %.2f%%' %
              (p, (100 * eff3(p, error=True)).toString('(%5.2f+-%4.2f)'),
               100 * eff0(p)))
Exemple #9
0
def test_vars1():

    from ostap.fitting.roofuncs import BernsteinPoly as BP

    logger = getLogger('test_vars1')

    f = BP('G', xvar=x, power=4)
    f.pars = 0.2, 0.2, 0.2, 0.2

    eff2 = Efficiency1D('E3', f.fun, cut=acc, xvar=x)

    r2 = eff2.fitTo(ds)

    logger.info("Fit result using-BernsteinPoly \n%s" % r2.table(prefix="# "))
    logger.info("Compare with true efficiency (using BernsteinPoly)\n%s" %
                make_table(eff2, title='using BernsteinPoly'))

    with wait(2), use_canvas('test_pdf'):
        f2 = eff2.draw(ds, nbins=25)
Exemple #10
0
def test_vars1 () :

    from ostap.fitting.roofuncs import BernsteinPoly as BP 
    
    f      = BP ( 'G' , xvar = x , power = 4 )
    f.pars = 0.2 , 0.2 , 0.2 , 0.2 
        
    eff2   = Efficiency1D ( 'E3' , f.fun , cut = acc  , xvar = x )
    
    r2     = eff2.fitTo ( ds )
    f2     = eff2.draw  ( ds )
    
    print (r2)
    
    for p in points :
        e  = eff2 ( p , error = True )
        ev = e.value()
        e0 = eff0 ( p ) / emax  
        print (' Point/Eff %4.1f %s%% (%.2f%%)'   % ( p , (100*e).toString ( '(%5.2f+-%4.2f)' ) ,  e0 * 100 ) )
def test_pyVAR2():
    """Use PyVAR2
    """

    logger = getLogger("test_pyVAR2")

    from ostap.fitting.pyvar import PyVAR2

    myEff3 = PyVAR2(name='myEff3', vars=vars, function=eff)

    with timing("Using-PyVAR2", logger):

        eff3 = Efficiency1D('E3', myEff3.var, cut=acc, xvar=x)

        a.fix(A)
        b.fix(B)
        c.value = C
        x0.value = X0

        r3 = eff3.fitTo(ds, silent=True)

        a.release()
        b.release()

        c.fix()
        x0.fix()

        r3 = eff3.fitTo(ds, silent=True)

        c.release()
        x0.release()

        r3 = eff3.fitTo(ds, silent=True)

        logger.info("Fit result using-PyVAR2 \n%s" % r3.table(prefix="# "))
        logger.info("Compare with true efficiency (using PyVAR2)\n%s" %
                    make_table(eff3, title='using PyVAR2'))

        with use_canvas("test_pyVAR2"):
            eff3.draw(ds)

    time.sleep(2)
def test_formula():
    """Use RooFormulaVar to parameterise efficiency:
    """
    logger = getLogger("test_formula")

    ## create RooFormularVar
    effFunc = ROOT.RooFormulaVar("effFunc", "a+0.5*b*(1+tanh((x-x0)*c))", vars)

    with timing("Using-RooFormularVar", logger):

        eff1 = Efficiency1D('E1', effFunc, cut=acc, xvar=x)

        a.fix(A)
        b.fix(B)
        c.value = C
        x0.value = X0

        r1 = eff1.fitTo(ds, silent=True)

        a.release()
        b.release()

        c.fix()
        x0.fix()

        r1 = eff1.fitTo(ds, silent=True)

        c.release()
        x0.release()

        r1 = eff1.fitTo(ds, silent=True)

        logger.info("Fit result using-RooFormularVar \n%s" %
                    r1.table(prefix="# "))
        logger.info("Compare with true efficiency (using  RooFormulaVar)\n%s" %
                    make_table(eff1, title='using RooFormulaVer'))

        with use_canvas("test_formula"):
            eff1.draw(ds)

    time.sleep(2)
Exemple #13
0
def test_pdf () :

    effPdf = Models.Monotonic_pdf ( 'P6' , xvar = x , power = 3 , increasing = True )

    maxe   = margin * effPdf ( xmax )
    
    s0     = min ( 1.0 / emax , 1.0 / maxe ) 
    scale  = ROOT.RooRealVar ( 'scaleX' , 'scaleX' , s0 , 0.2 * s0 , 5.0 * s0  )
    
    eff2   = Efficiency1D ( 'E2' , effPdf , cut = acc  , scale = scale )
    
    r2     = eff2.fitTo ( ds )
    f2     = eff2.draw  ( ds )
    
    print (r2)
    
    for p in points :
        e  = eff2 ( p , error = True )
        ev = e.value()
        e0 = eff0 ( p ) / emax  
        print (' Point/Eff %4.1f %s%% (%.2f%%)'   % ( p , (100*e).toString ( '(%5.2f+-%4.2f)' ) ,  e0 * 100 ) )
Exemple #14
0
def test_pdf():

    logger = getLogger('test_pdf')

    effPdf = Models.Monotonic_pdf('P6', xvar=x, power=4, increasing=True)

    maxe = margin * effPdf(xmax)

    s0 = min(1.0 / emax, 1.0 / maxe)
    scale = ROOT.RooRealVar('scaleX', 'scaleX', s0, 0.2 * s0, 5.0 * s0)

    eff2 = Efficiency1D('E2', effPdf, cut=acc, scale=scale)

    r2 = eff2.fitTo(ds)

    logger.info("Fit result using-Monotonic_pdf \n%s" % r2.table(prefix="# "))
    logger.info("Compare with true efficiency (using Monotonic_pdf)\n%s" %
                make_table(eff2, title='using Monotonic_pdf'))

    with wait(2), use_canvas('test_pdf'):
        f2 = eff2.draw(ds, nbins=25)
Exemple #15
0
def test_vars2():

    logger = getLogger('test_vars2')

    from ostap.fitting.roofuncs import MonotonicPoly as MP

    f = MP('G', xvar=x, increasing=True, power=4)
    f.pars = 0.6, 0.8, -0.1, -0.6
    f.a = 0.06
    f.b = 2.72
    f.a.release()
    f.b.release()

    eff2 = Efficiency1D('E4', f, cut=acc, xvar=x)

    r2 = eff2.fitTo(ds)

    logger.info("Fit result using-MonotonicPoly \n%s" % r2.table(prefix="# "))
    logger.info("Compare with true efficiency (using MonotonicPoly)\n%s" %
                make_table(eff2, title='using MonotonicPoly'))

    with wait(2), use_canvas('test_pdf'):
        f2 = eff2.draw(ds, nbins=25)
Exemple #16
0
def test_pyVar():
    """use PyVar stuff
    - For *NEW* PyROOT only!
    """

    logger = getLogger("test_pyVar")

    if old_PyROOT:
        logger.warning("test is enabled only for *NEW* PyROOT!")
        return

    # =========================================================================
    ## @class MyEff4
    #  Local ``pythonic'' variable
    class MyEff4(Ostap.Functions.PyVar):
        def __init__(self, name, title, variables):

            vlist = ROOT.RooArgList()
            for v in variables:
                vlist.add(v)
            super(MyEff4, self).__init__(name, title, vlist)

        def clone(self, newname):

            name = newname if newname else self.name
            nv = MyEff4(name, self.title, self.variables())
            ROOT.SetOwnership(nv, False)
            return nv

        def evaluate(self):

            vlist = self.variables()

            _x = float(vlist[0])
            _a = float(vlist[1])
            _b = float(vlist[2])
            _c = float(vlist[3])
            _x0 = float(vlist[4])

            return eff(_x, _a, _b, _c, _x0)

    myEff4 = MyEff4('myEff4', variables=vars, title='title')
    the_fun = myEff4

    with timing("Using-PyVar", logger):

        eff4 = Efficiency1D('E4', the_fun, cut=acc, xvar=x)

        a.fix(A)
        b.fix(B)
        c.value = C
        x0.value = X0

        r4 = eff4.fitTo(ds, silent=True)

        a.release()
        b.release()

        c.fix()
        x0.fix()

        r4 = eff4.fitTo(ds, silent=True)

        c.release()
        x0.release()

        r4 = eff4.fitTo(ds, silent=True)

        logger.info("Fit result using-PyVar \n%s" % r4.table(prefix="# "))
        logger.info("Compare with true efficiency (using  PyVar)\n%s" %
                    make_table(eff4, title='using PyVar'))

    time.sleep(2)
Exemple #17
0
def test_pyVAR():
    """Yse PyVAR stuff
    - For *old* PyROOT only!
    """
    logger = getLogger("test_pyVAR")

    if not old_PyROOT:
        logger.warning("test is enabled only for *OLD* PyROOT!")
        return

    from ostap.fitting.pyvar import PyVAR

    # =========================================================================
    ## @class MyEff2
    #  Local "pythonic" variable
    #  @see PyVAR
    class MyEff2(PyVAR):
        """Local ``pythonic'' variable
        """
        def evaluate(self):

            vlist = self.varlist

            _x = float(vlist[0])
            _a = float(vlist[1])
            _b = float(vlist[2])
            _c = float(vlist[3])
            _x0 = float(vlist[4])

            return eff(_x, _a, _b, _c, _x0)

    myEff2 = MyEff2('myEff2', vars=vars, title='title')
    the_fun = myEff2.var

    with timing("Using-PyVAR", logger):

        eff2 = Efficiency1D('E2', the_fun, cut=acc, xvar=x)

        a.fix(A)
        b.fix(B)
        c.value = C
        x0.value = X0

        r2 = eff2.fitTo(ds, silent=True)

        a.release()
        b.release()

        c.fix()
        x0.fix()

        r2 = eff2.fitTo(ds, silent=True)

        c.release()
        x0.release()

        r2 = eff2.fitTo(ds, silent=True)

        logger.info("Fit result using-PyVAR \n%s" % r2.table(prefix="# "))
        logger.info("Compare with true efficiency (using PyVAR)\n%s" %
                    make_table(eff2, title='using PyVAR'))

    time.sleep(2)