Example #1
0
def _h1_cmp_pdf_(h1, h2, density=False, draw=True, silent=True):
    """Compare histograms by refit of the first with functions,
    extracted from the second one

    >>> h1 = ... ## the first histo
    >>> h2 = ... ## the second histo
    >>> r  = h1.cmp_fit ( h2 )
    >>> if r : print r.Prob()    
    """
    if density:
        h1_ = h1.density()
        h2_ = h2.density()
        cmp = _h1_cmp_pdf_(h1_, h2_, density=False, draw=draw, silent=silent)
        del h1_
        del h2_
        return cmp

    from ostap.fitting.basic import H1D_pdf, Fit1D
    from ostap.fitting.background import Bkg_pdf

    pdf2 = H1D_pdf('_H1', h2, density=True, silent=True)
    model2 = Fit1D(signal=pdf2,
                   background=Bkg_pdf('_B2', mass=pdf2.mass, power=0, tau=0))
    model2.b.fix(0)

    from ostap.utils.utils import invisibleCanvas
    with invisibleCanvas():
        r2, f2 = model2.chi2fitTo(h1,
                                  silent=silent,
                                  draw=draw,
                                  density=False,
                                  sumw2=True)

    return r2, f2.chiSquare()
Example #2
0
def test_pygauss():

    logger.info('Test PyGauss:  simple Gaussian signal')

    gauss = PyGauss('PyGauss', xvar=mass)

    ## model
    model = Fit1D(signal=gauss, background=None, name='M0')

    ## r1, f1  = gauss  .fitTo ( dataset , draw = True , silent = True , ncpu=1 )
    ## print (r1)

    r2, f2 = model.fitTo(dataset, draw=True, silent=True, ncpu=1)
    print(r2)
Example #3
0
def test_pygauss2():

    ## the function
    def function(x, m, s):
        dx = (x - m) / s
        return math.exp(-0.5 * dx * dx) * NORM / s

    ## construct PDF
    gauss = PyGauss2(name='G2', function=function, xvar=mass)

    ## model
    model = Fit1D(signal=gauss, background='p0', name='Q2')

    ## r1, f1  = gauss  .fitTo ( dataset , draw = True , silent = True , ncpu=1 )
    ## print (r1)

    r2, f2 = model.fitTo(dataset, draw=True, silent=True, ncpu=1)
    print(r2)
def test_PyPDF() :
    """Test pure python PDF: pyPDF
    - For *OLD* PyROOT only!
    - see Ostap.Models.PyPdf 
    """

    logger = getLogger("test_PyPDF")
    
    if not old_PyROOT :
        logger.warning("test enabled only for OLD PyROOT!")
        return
    
    logger.info  ("Test pure python PDF: PyPDF ")

    from   ostap.fitting.pypdf  import PyPDF    
    # =============================================================================
    ## @class PyGauss
    #  local ``pure-python'' PDF 
    class PyGauss(MASS,PyPDF) :
        """Local ``pure-python'' PDF
        """    
        def __init__ ( self         ,
                       name         ,
                       xvar         ,
                       mean  = ( 3.080 , 3.05  , 3.15  ) ,
                       sigma = ( 0.010 , 0.005 , 0.020 ) ,
                       pypdf = None ) :
            
            
            MASS .__init__ ( self , name      , xvar , mean , sigma ) 
            PyPDF.__init__ ( self , self.name , ( self.xvar  ,
                                                  self.mean  ,
                                                  self.sigma ) , pypdf = pypdf )
            
            self.pdf = self.pypdf
            
            self.config = {
                'name'  : self.name  ,
                'xvar'  : self.xvar  ,
                'mean'  : self.mean  ,
                'sigma' : self.sigma ,
                'pypdf' : None         ## attention! 
                }
            
        ## the  main method 
        def evaluate ( self ) :
            
            vlist = self.varlist
            
            x = self.variable ( 0 ) 
            m = self.variable ( 1 )  
            s = self.variable ( 2 )
            
            dx = ( x - m ) / s        
            return math.exp ( -0.5 * dx * dx ) * NORM / s 

    with timing ("Using-PyPDF", logger ) :
        
        pdf.mean  = random.gauss ( 3.100 , 0.010 )
        pdf.sigma = random.gauss ( 0.012 , 0.001 )

        ## create PDF 
        gauss   = PyGauss( 'PyGauss'   , xvar = mass ,  
                           mean = pdf.mean , sigma = pdf.sigma )
        
        ## build fit model 
        model = Fit1D ( signal = gauss , background = None  , name = 'M0' )
        
        ##  fit!
        r, _ = model  .fitTo ( dataset , draw = False , silent = True , ncpu=1 )
        with wait ( 1 ) , use_canvas ( "test_PyPDF" ) : 
            r, f = model  .fitTo ( dataset , draw = True  , silent = True , ncpu=1 )            
            logger.info  ("Fit result for``pure python'' PDF: PyPDF \n%s" % r.table ( prefix = "# " ) )
def test_PyPdf_AI() :
    """Test pure python PDF: pyPDF
    - For *NEW* PyROOT only!
    - see Ostap.Models.PyPdf 
    """

    logger = getLogger("test_PyPdf_AI")
    
    if old_PyROOT :
        logger.warning("test enabled only for NEW PyROOT!")
        return
    
    logger.info  ("Test pure python PDF: PyPdf with analytical  integral")

    # =============================================================================
    ## @class PyGauss
    #  local ``pure-python'' PDF 
    class MyGauss(Ostap.Models.PyPdf) :
        """Local ``pure-python'' PDF
        """    
        def __init__ ( self , name , xvar , mean , sigma ) :
            
            vars = ROOT.RooArgList()
            
            vars.add ( xvar  )
            vars.add ( mean  )
            vars.add ( sigma )
            
            super(MyGauss,self).__init__ ( name , 'title' , vars )
            
        ## the  main method 
        def evaluate ( self ) :
            
            x = self.variable ( 0 ) 
            m = self.variable ( 1 )  
            s = self.variable ( 2 )
            
            dx = ( x - m ) / s        
            return math.exp ( -0.5 * dx * dx ) * NORM / s 

        def clone ( self , newname ) :

            name = newname if newname else self.name
            
            vlist = self.variables() 
            
            xvar  = vlist[0]  
            mean  = vlist[1]
            sigma = vlist[2] 

            cl = MyGauss( name , xvar , mean , sigma ) 
            ROOT.SetOwnership ( cl  , False )
            
            return cl
        
        ## declare analytical integral 
        def get_analytical_integral ( self ) :
            """Declare the analytical integral"""
            
            x  = self.variables()[0]
            
            if self.matchArgs ( x ) : return 1 ## get the integration code
            
            return 0
        
        ## calculate the analytical integral 
        def analytical_integral ( self ) :
            """Calculate the analytical integral"""
            
            assert 1 == self.intCode () , 'Invalid integration code!'
            
            vlist = self.variables() 
            
            rn    = self.rangeName()        
            xv    = vlist [ 0 ]        
            xmax  = xv.getMax ( rn )
            xmin  = xv.getMin ( rn )
            
            m     = float ( vlist [ 1 ] ) 
            s     = float ( vlist [ 2 ] )        
            
            return CDF ( xmax , m , s  ) - CDF ( xmin , m , s  )

    with timing ("Using-PyPdf+AI", logger ) :

        pdf.mean  = random.gauss ( 3.100 , 0.010 )
        pdf.sigma = random.gauss ( 0.012 , 0.001 )

        pdf_  = MyGauss ( 'MyGauss' , pdf.xvar , pdf.mean , pdf.sigma )
        gauss = Generic1D_pdf  ( pdf_ , xvar = pdf.xvar )
        
        ## build fit model 
        model = Fit1D ( signal = gauss , background = None  , name = 'M3' )
        
        ##  fit!
        r, _ = model  .fitTo ( dataset , draw = False , silent = True , ncpu=1 )
        with wait ( 1 ) , use_canvas ( "test_PyPdf_AI" ) : 
            r, f = model  .fitTo ( dataset , draw = True  , silent = True , ncpu=1 )            
            logger.info  ("Fit result for``pure python'' PDF: PyPdf with analytical integral\n%s" % r.table ( prefix = "# " ) )
def test_PyPdf() :
    """Test pure python PDF: pyPDF
    - For *NEW* PyROOT only!
    - see Ostap.Models.PyPdf 
    """

    logger = getLogger("test_PyPdf")
    
    if old_PyROOT :
        logger.warning("test enabled only for NEW PyROOT!")
        return
    
    logger.info  ("Test pure python PDF: PyPdf ")

    # =============================================================================
    ## @class PyGauss
    #  local ``pure-python'' PDF 
    class MyGauss(Ostap.Models.PyPdf) :
        """Local ``pure-python'' PDF
        """    
        def __init__ ( self , name , xvar , mean , sigma ) :
            
            vars = ROOT.RooArgList()
            
            vars.add ( xvar  )
            vars.add ( mean  )
            vars.add ( sigma )
            
            super(MyGauss,self).__init__ ( name , 'title' , vars )
            
        ## the  main method 
        def evaluate ( self ) :
            
            vlist = self.varlist
            
            x = self.variable ( 0 ) 
            m = self.variable ( 1 )  
            s = self.variable ( 2 )
            
            dx = ( x - m ) / s        
            return math.exp ( -0.5 * dx * dx ) * NORM / s 

        def clone ( self , newname ) :

            name = newname if newname else self.name
            
            vlist = self.variables() 
            
            xvar  = vlist[0]  
            mean  = vlist[1]
            sigma = vlist[2] 

            cl = MyGauss( name , xvar , mean , sigma ) 
            ROOT.SetOwnership ( cl  , False )
            
            return cl
        
    with timing ("Using-PyPdf", logger ) :

        pdf.mean  = random.gauss ( 3.100 , 0.010 )
        pdf.sigma = random.gauss ( 0.012 , 0.001 )

        pdf_  = MyGauss ( 'MyGauss' , pdf.xvar , pdf.mean , pdf.sigma )
        gauss = Generic1D_pdf  ( pdf_ , xvar = pdf.xvar )
                
        ## build fit model 
        model = Fit1D ( signal = gauss , background = None  , name = 'M3' )
        
        ##  fit!
        r, _ = model  .fitTo ( dataset , draw = False , silent = True , ncpu=1 )
        with wait ( 1 ) , use_canvas ( "test_PyPdf" ) : 
            r, f = model  .fitTo ( dataset , draw = True  , silent = True , ncpu=1 )        
            logger.info  ("Fit result for``pure python'' PDF: PyPdf \n%s" % r.table ( prefix = "# " ) )
def test_PyPDF2 () :
    """Test pure python PDF: PyPDF2
    - see Ostap.Models.PyPdf2 
    """

    logger = getLogger("test_PyPDF2")
    
    logger.info  ("Test pure python PDF: PyPDF2 with python function")

        
    from   ostap.fitting.pypdf  import PyPDF2
    # =============================================================================
    ## @class PyGauss2
    #  local ``pure-python'' PDF 
    class PyGauss2(MASS,PyPDF2) :
        """Local ``pure-python'' PDF """    
        def __init__ ( self         ,
                       name         ,
                       function     , 
                       xvar         ,                   
                       mean  = ( 3.080 , 3.05  , 3.15  ) ,
                       sigma = ( 0.010 , 0.005 , 0.020 ) ,
                       title = '' 
                       ) :
            
            MASS  .__init__ ( self , name      , xvar , mean , sigma ) 
            PyPDF2.__init__ ( self      ,
                              name     = self.name ,
                              function = function  ,
                              vars     = ( self.xvar , self.mean , self.sigma ) )
            
            self.config = {
                'name'     : self.name     ,
                'function' : self.function , 
                'xvar'     : self.xvar     ,
                'mean'     : self.mean     ,
                'sigma'    : self.sigma    ,
                }

    ## the function
    def function ( x , m , s ) :
        dx = ( x - m ) / s        
        return math.exp ( -0.5 * dx * dx ) * NORM / s 
    
    with timing ("Using-PyPDF2", logger ) :
        
        pdf.mean  = random.gauss ( 3.100 , 0.010 )
        pdf.sigma = random.gauss ( 0.012 , 0.001 )

        ## construct PDF
        gauss = PyGauss2 ( name  ='G2' , function = function ,
                           xvar = mass , mean = pdf.mean , sigma = pdf.sigma )
        
        ## model 
        model = Fit1D ( signal = gauss , background = 'p0' , name = 'M2' )
        
        ##  fit!
        r, _ = model  .fitTo ( dataset , draw = False , silent = True , ncpu=1 )
        with wait ( 1 ) , use_canvas ( "test_PyPDF2" ) : 
            r, f = model  .fitTo ( dataset , draw = True  , silent = True , ncpu=1 )
            logger.info  ("Fit result for``pure python'' PDF: PyPDF2 with python function \n%s" % r.table ( prefix = "# " ) )
def test_PyPDF_AI() :
    """Test pure python PDF: PyPDF with analytical integral 
    - For *OLD* PyROOT only!
    - see Ostap.Models.PyPdf 
    """

    logger = getLogger("test_PyPDF_AI")
    
    if not old_PyROOT :
        logger.warning("test enabled only for OLD PyROOT!")
        return
    
    logger.info  ("Test pure python PDF: PyPDF with analytical integral")


    from   ostap.fitting.pypdf  import PyPDF    
    # =========================================================================
    ## @class PyGaussAI
    #  local ``pure-python'' PDF with analytical integrals 
    class PyGaussAI(MASS,PyPDF) :
        """Local ``pure-python'' PDF with analytical integrals """    
        def __init__ ( self         ,
                       name         ,
                       xvar         ,
                       mean  = ( 3.080 , 3.05  , 3.15  ) ,
                       sigma = ( 0.010 , 0.005 , 0.020 ) ,
                       pypdf = None ) :
            
            
            MASS .__init__ ( self , name      , xvar , mean , sigma ) 
            PyPDF.__init__ ( self , self.name , ( self.xvar  ,
                                                  self.mean  ,
                                                  self.sigma ) , pypdf = pypdf )
            
            self.pdf = self.pypdf
            
            self.config = {
                'name'  : self.name  ,
                'xvar'  : self.xvar  ,
                'mean'  : self.mean  ,
                'sigma' : self.sigma ,
                'pypdf' : None         ## attention! 
                }
            
        ## the  main method 
        def evaluate ( self ) :
            
            vlist = self.varlist
            
            x = self.variable ( 0 ) 
            m = self.variable ( 1 )  
            s = self.variable ( 2 )
            
            dx = ( x - m ) / s        
            return math.exp ( -0.5 * dx * dx ) * NORM / s 


        ## declare analytical integral 
        def get_analytical_integral ( self ) :
            """Declare the analytical integral"""
            
            x  = self.varlist[0]
            
            if self.matchArgs ( x ) : return 1 ## get the integration code
            
            return 0
        
        ## calculate the analytical integral 
        def analytical_integral ( self ) :
            """Calculate the analytical integral"""
            
            assert 1 == self.intCode , 'Invalid integration code!'
            
            vlist = self.varlist
            
            rn    = self.rangeName        
            xv    = vlist [ 0 ]        
            xmax  = xv.getMax ( rn )
            xmin  = xv.getMin ( rn )
            
            m     = float ( vlist [ 1 ] ) 
            s     = float ( vlist [ 2 ] )        
            
            return CDF ( xmax , m , s  ) - CDF ( xmin , m , s  )

    with timing ("Using-PyPDF+AI", logger ) :

        pdf.mean  = random.gauss ( 3.100 , 0.010 )
        pdf.sigma = random.gauss ( 0.012 , 0.001 )

        ## create PDF 
        gauss   = PyGaussAI( 'PyGaussAI'   , xvar = mass ,
                             mean = pdf.mean , sigma = pdf.sigma )
        
        ## build fit model 
        model = Fit1D ( signal = gauss , background = None  , name = 'M1' )
        
        ##  fit!
        r, _ = model  .fitTo ( dataset , draw = False , silent = True , ncpu=1 )
        with wait ( 1 ) , use_canvas ( "test_PyPDF_AI" ) : 
            r, f = model  .fitTo ( dataset , draw = True  , silent = True , ncpu=1 )        
            logger.info  ("Fit result for``pure python'' PDF: PyPDF with analytical integral \n%s" % r.table ( prefix = "# " ) )