Skip to content

Commit

Permalink
step back?
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Apr 3, 2024
1 parent 0a3b2b5 commit 7a3b2f0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 116 deletions.
158 changes: 63 additions & 95 deletions ostap/fitting/tests/test_fitting_pypdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# =============================================================================
import ostap.fitting.roofit
from builtins import range
from ostap.core.meta_info import root_info
from ostap.core.core import VE, dsID, Ostap
from ostap.fitting.pdfbasic import Generic1D_pdf
from ostap.fitting.fit1d import PEAK , Fit1D
Expand Down Expand Up @@ -63,8 +62,6 @@
mean = ( 3.080 , 3.05 , 3.15 ) ,
sigma = ( 0.010 , 0.005 , 0.020 ) )


my_exp = math.exp
# =============================================================================
## Test pure python PDF: <code>PyPDF</code>
# @attention For *OLD* PyROOT only!
Expand All @@ -78,7 +75,7 @@ def test_PyPDF() :
logger = getLogger("test_PyPDF")

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

logger.info ("Test pure python PDF: PyPDF ")
Expand Down Expand Up @@ -123,7 +120,7 @@ def evaluate ( self ) :
s = self.variable ( 2 )

dx = ( x - m ) / s
return my_exp ( -0.5 * dx * dx ) * NORM / s
return math.exp ( -0.5 * dx * dx ) * NORM / s

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

Expand All @@ -141,7 +138,7 @@ def evaluate ( self ) :
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 = "# " ) )
logger.info ("Fit result for``pure python'' PDF: PyPDF \n%s" % r.table ( prefix = "# " ) )


# =============================================================================
Expand All @@ -157,7 +154,7 @@ def test_PyPDF_AI() :
logger = getLogger("test_PyPDF_AI")

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

logger.info ("Test pure python PDF: PyPDF with analytical integral")
Expand Down Expand Up @@ -249,7 +246,7 @@ def analytical_integral ( self ) :
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 = "# " ) )
logger.info ("Fit result for``pure python'' PDF: PyPDF with analytical integral \n%s" % r.table ( prefix = "# " ) )


# =============================================================================
Expand All @@ -269,7 +266,7 @@ def test_PyPDF2 () :
## @class PyGauss2
# local ``pure-python'' PDF
class PyGauss2(PEAK,PyPDF2) :
"""Local ``pure-python'' PxDF """
"""Local ``pure-python'' PDF """
def __init__ ( self ,
name ,
function ,
Expand Down Expand Up @@ -314,10 +311,7 @@ def function ( x , m , s ) :
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 = "# " ) )

del r, model, gauss

logger.info ("Fit result for``pure python'' PDF: PyPDF2 with python function \n%s" % r.table ( prefix = "# " ) )

# =============================================================================
## Test pure python PDF: <code>PyPdf</code>
Expand All @@ -331,38 +325,27 @@ def test_PyPdf() :

logger = getLogger("test_PyPdf")

logger.info ("Test pure python PDF: PyPdf ")

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

if (6,31) <= root_info :
logger.warning ( 'Test is TEMPORARILY disabled for ROOT>6.31/01 %s' % ROOT.gROOT.GetVersion() )
return
logger.info ("Test pure python PDF: PyPdf ")

# =============================================================================
## @class MyGauss1
## @class PyGauss
# local ``pure-python'' PDF
class MyGauss1(Ostap.Models.PyPdf) :
class MyGauss(Ostap.Models.PyPdf) :
"""Local ``pure-python'' PDF
"""
def __init__ ( self , name , xvar = None , mean = None , sigma = None , clone = None ) :


if clone and isinstance ( clone, ROOT.RooAbsPdf ) :

super(MyGauss1,self).__init__ ( clone , name )

else :

vars = ROOT.RooArgList()

vars.add ( xvar )
vars.add ( mean )
vars.add ( sigma )

super(MyGauss1,self).__init__ ( name , 'title' , vars )
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 ) :
Expand All @@ -378,18 +361,25 @@ def evaluate ( self ) :

def clone ( self , newname ) :

cl = MyGauss1 ( newname , clone = self )
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_ = MyGauss1 ( 'MyGauss1' , pdf.xvar , pdf.mean , pdf.sigma )
pdf_ = MyGauss ( 'MyGauss' , pdf.xvar , pdf.mean , pdf.sigma )
gauss = Generic1D_pdf ( pdf_ , xvar = pdf.xvar )

## build fit model
Expand All @@ -399,79 +389,65 @@ def clone ( self , newname ) :
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 = "# " ) )
logger.info ("Fit result for``pure python'' PDF: PyPdf \n%s" % r.table ( prefix = "# " ) )

del r,f
del model, gauss,
del pdf_

# =============================================================================
## Test pure python PDF: <code>PyPdf</code> + analytical integrals
# @attention For *NEW* PyROOT only!
# @see Ostap::Models::PyPdf
def test_PyPdf_AI() :

"""Test pure python PDF: pyPDF
- For *NEW* PyROOT only!
- see Ostap.Models.PyPdf
"""

logger = getLogger("test_PyPdf_AI")
logger.info ( "Test pure python PDF: PyPdf with analytical integral")

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

if (6,31) <= root_info :
logger.warning ( 'Test is TEMPORARILY disabled for ROOT>6.31/01 %s' % ROOT.gROOT.GetVersion() )
logger.warning("test enabled only for NEW PyROOT!")
return

logger.info ("Test pure python PDF: PyPdf with analytical integral")

# =============================================================================
## @classMyGauss2
## @class PyGauss
# local ``pure-python'' PDF
class MyGauss2(Ostap.Models.PyPdf) :
"""Local `pure-python' PDF
class MyGauss(Ostap.Models.PyPdf) :
"""Local ``pure-python'' PDF
"""
def __init__ ( self , name , xvar = None , mean = None , sigma = None , clone = None ) :

print ( 'CONSTRUCTOR/1' , name )
if clone and isinstance ( clone , ROOT.RooAbsPdf ) :

print ( 'CONSTRUCTOR/C' , name )
super(MyGauss2,self).__init__ ( clone, name )

else :

print ( 'CONSTRUCTOR/R', name )
vars = ROOT.RooArgList()

vars.add ( xvar )
vars.add ( mean )
vars.add ( sigma )

super(MyGauss2,self).__init__ ( name , 'title' , vars )

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 ) :

if not newname : newname = self.name
cl = MyGauss2 ( newname , clone = self )
name = newname if newname else self.name

ROOT.SetOwnership ( cl , False )
vlist = self.variables()

xvar = vlist[0]
mean = vlist[1]
sigma = vlist[2]

for s in cl.servers():
print ( 'server' , s )
cl = MyGauss( name , xvar , mean , sigma )
ROOT.SetOwnership ( cl , False )

return cl

## declare analytical integral
Expand All @@ -483,7 +459,7 @@ def get_analytical_integral ( self ) :
if self.matchArgs ( x ) : return 1 ## get the integration code

return 0

## calculate the analytical integral
def analytical_integral ( self ) :
"""Calculate the analytical integral"""
Expand All @@ -501,17 +477,13 @@ def analytical_integral ( self ) :
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_ = MyGauss2 ( 'MyGauss2' , pdf.xvar , pdf.mean , pdf.sigma )

ROOT.SetOwnership ( pdf_ , True)

pdf_ = MyGauss ( 'MyGauss' , pdf.xvar , pdf.mean , pdf.sigma )
gauss = Generic1D_pdf ( pdf_ , xvar = pdf.xvar )

## build fit model
Expand All @@ -521,22 +493,18 @@ def analytical_integral ( self ) :
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 = "# " ) )
logger.info ("Fit result for``pure python'' PDF: PyPdf with analytical integral\n%s" % r.table ( prefix = "# " ) )

del r, f,
del model, gauss, pdf_

# =============================================================================
if '__main__' == __name__ :


test_PyPDF ()
test_PyPDF_AI ()
## test_PyPDF2 () ## ATTENTION!!!
test_PyPDF2 ()
test_PyPdf ()
test_PyPdf_AI ()



# =============================================================================
## The END
# =============================================================================
Loading

0 comments on commit 7a3b2f0

Please sign in to comment.