Esempio n. 1
0
    def tf2  ( self        ,
               xmin = None ,
               xmax = None ,
               ymin = None ,
               ymax = None ) :

        d = self.dalitz
        s = self.s  
        M = self.M
        
        if not self.T : 
            xmin = xmin if not xmin is None else -1.0 
            xmax = xmax if not xmax is None else +1.0
            ymin = ymin if not ymin is None else d.s2_min (   ) ** 0.5 
            ymax = ymax if not ymax is None else d.s2_max ( M ) ** 0.5 
        else :
            xmin = xmin if not xmin is None else d.s2_min (   ) ** 0.5 
            xmax = xmax if not xmax is None else d.s2_max ( M ) ** 0.5 
            ymin = ymin if not ymin is None else -1.0 
            ymax = ymax if not ymax is None else +1.0

        assert xmin < xmax, 'Invalid xmin/xmax %s/%s' %  ( xmin , xmax )
        assert ymin < ymax, 'Invalid ymin/ymax %s/%s' %  ( ymin , ymax )
        
        self.__tf = ROOT.TF2 ( fID () , self.eval , xmin , xmax , ymin , ymax )
        return self.__tf
Esempio n. 2
0
    def tf2(self, xmin=None, xmax=None, ymin=None, ymax=None):

        d = self.__dalitz
        s = self.__s
        M = self.__M

        xmin = xmin if not xmin is None else d.s1_min()
        xmax = xmax if not xmax is None else d.s1_max(M)
        ymin = ymin if not ymin is None else d.s2_min()
        ymax = ymax if not ymax is None else d.s2_max(M)

        assert xmin < xmax, 'Invalid xmin/xmax %s/%s' % (xmin, xmax)
        assert ymin < ymax, 'Invalid ymin/ymax %s/%s' % (ymin, ymax)

        self.__tf = ROOT.TF2(fID(), self.eval, xmin, xmax, ymin, ymax)
        return self.__tf
Esempio n. 3
0
def make_dataset ( tree , variables , selection = '' , name = '' , title = '' , silent = False ) :
    """Create the dataset from the tree
    >>> tree = ...
    >>> ds = tree.make_dataset ( [ 'px , 'py' , 'pz' ] ) 
    """
    import ostap.trees.cuts
    import ostap.fitting.roofit

    varset   = ROOT.RooArgSet()
    vars     = set()

    formulas  = []
    
    selection = str ( selection ) if isinstance ( selection , ROOT.TCut ) else selection  
    selection = selection.strip() if isinstance ( selection , str       ) else selection 
    
    cuts = [ selection ] if selection else [] 
    for v in variables :

        if   isinstance  ( v , str              ) : vv = Variable (   v )
        elif isinstance  ( v , ROOT.RooRealVar  ) : vv = Variable (   v )
        elif isinstance  ( v , ( tuple , list ) ) : vv = Variable (  *v )
        elif isinstance  ( v , dict             ) : vv = Variable ( **v )
        elif isinstance  ( v , Variable         ) : vv = v 
        else :
            logger.error("Do not know how to treat the variable %s/%s, skip it" % ( v , type ( v ) ) )
            continue

        if vv.trivial and vv.name == vv.formula : 
            
            assert hasattr  ( tree , vv.name ) , "Tree/Chain has no branch ``%s''" % vv.name
            assert hasattr  ( tree , vv.name ) , "Tree/Chain has no branch ``%s''" % vv.name
            
            varset.add  ( vv.var )
            vars.add ( vv )
            
        elif vv.formula :
            
            formulas.append ( vv )
            continue
        
        else :
            
            logger.error("Do not know how to treat the variable %s, skip it" % vv.name )
            continue 
        
        mn , mx = vv.minmax
        if _minv < mn : cuts.append ( "(%.16g <= %s)" % ( mn      , vv.name ) ) 
        if _maxv > mx : cuts.append ( "(%s <= %.16g)" % ( vv.name , mx      ) )

    ## 
    cuts = ROOT.TCut(' && '.join(cuts) ) if cuts else ROOT.TCut() 

    ## extended varset
    stor    = set() 
    varsete = ROOT.RooArgSet()
    for v in varset : varsete.add ( v )

    expressions = [ f.formula for f in formulas ]
    if selection : expressions.append ( selection ) 
        
    if expressions :

        tt = None 
        if isinstance ( tree , ROOT.TChain ) :
            nf = len ( tree.files() )
            for i in range ( nf ) :
                tt = tree[i]
                if tt : break 
        if not tt : tt = tree

        from   ostap.core.core import fID
        for expression in expressions : 
            tf   = Ostap.Formula ( fID () , str ( expression ) , tt )
            assert tf.ok() , 'Invalid formula %s' % expression 
            i    = 0 
            leaf = tf.GetLeaf( i )
            while leaf :
                lname = leaf.GetName()
                if not lname in varsete :
                    v = Variable ( lname )
                    varsete.add  ( v.var )
                    stor.add ( v ) 
                i   += 1
                leaf = tf.GetLeaf( i )                    
            del tf 
    
    if not name :
        from ostap.core.core import dsID 
        name = '%s_%s' % ( dsID() , tree.GetName() )
    if not title : title = '%s/%s' % ( name , tree.GetTitle() )

    total     = len ( tree )
    processed = tree.statVar ( '1' , selection    ).nEntries()
    skipped   = tree.statVar ( '1' , str ( cuts ) ).nEntries() 

    stat = total, processed , processed - skipped

    from ostap.logger.utils import rooSilent, rootError  
    with rooSilent ( ROOT.RooFit.ERROR  , True ) :
        with rootError( ROOT.kWarning ) :
            ds = ROOT.RooDataSet ( name  , title , tree , varsete , str( cuts ) )
            varsete = ds.get()
            
    ## add complex expressions 
    if formulas :
        # a
        vset   = ds.get()
        vlst = ROOT.RooArgList()
        for v in vset : vlst.add ( v )

        fcols = ROOT.RooArgList() 
        
        ffs   = []
        fcuts = [] 
        for f in formulas :            
            fv = ROOT.RooFormulaVar ( f.name , f.description , f.formula , vlst )
            assert fv.ok() , 'Invalid formula: %s' % f.formula 
            ffs.append ( fv )
            fcols.add  ( fv )
            mn , mx = f.minmax            
            if _minv < mn : fcuts.append ( "(%.16g <= %s)" % ( mn      , fv.name ) )
            if _maxv > mx : fcuts.append ( "(%s <= %.16g)" % ( fv.name , mx      ) )

        ds.addColumns ( fcols )
        ##  apply cuts (if any) for the  complex expressions 
        if fcuts :
            fcuts = [ '(%s)' % f for f in fcuts ]
            fcuts = ' && '.join ( fcuts )
            _vars = ds.get()
            ds1 = ROOT.RooDataSet ( dsID() , ds.title , ds , _vars , fcuts ) 
            ds.clear()
            del ds
            ds = ds1
            varsete = ds.get()
            
        nvars = ROOT.RooArgSet()
        for v in varset   : nvars.add ( v     )
        for v in formulas : nvars.add ( v.var )
        varset  = nvars 
        varsete = ds.get() 
        
    ##  remove all temporary variables  
    if len ( varset ) != len ( varsete ) :
        ds1 = ds.reduce ( varset )
        ds.clear()
        del ds
        ds = ds1
        
    if not silent : 
        skipped = 'Skipped:%d' % stat[2]
        skipped = '/' + attention ( skipped ) if stat[2] else '' 
        logger.info (
            'make_dataset: Events Total:%d/Processed:%s%s CUTS: "%s"\n# %s' % (
            stat[0] ,
            stat[1] ,
            skipped ,
            selection  , ds ) )            
        
    return ds , stat 
Esempio n. 4
0
from ostap.utils.timing import timing

h1 = ROOT.TH1F(hID(), 'decreasing convex ', 100, 0, 1)
h1.Sumw2()
h2 = ROOT.TH1F(hID(), 'increasing convex ', 100, 0, 1)
h2.Sumw2()
h3 = ROOT.TH1F(hID(), 'increasing concave', 100, 0, 1)
h3.Sumw2()
h4 = ROOT.TH1F(hID(), 'decreasing concave', 100, 0, 1)
h4.Sumw2()
h5 = ROOT.TH1F(hID(), 'symmetric  convex ', 100, 0, 1)
h5.Sumw2()
h6 = ROOT.TH1F(hID(), 'symmetric  concave', 100, 0, 1)
h6.Sumw2()

f1 = ROOT.TF1(fID(), '(x-1)**2', 0, 1)
f2 = ROOT.TF1(fID(), 'x**2', 0, 1)
f3 = ROOT.TF1(fID(), '1-(x-1)**2', 0, 1)
f4 = ROOT.TF1(fID(), '1-x**2', 0, 1)
f5 = ROOT.TF1(fID(), '4*(x-0.5)**2', 0, 1)
f6 = ROOT.TF1(fID(), '1-4*(x-0.5)**2', 0, 1)

f_2 = ROOT.TF2(fID(), 'x*x+y*y', -1, 1, 0, 2)
f_3 = ROOT.TF3(fID(), 'x*x+y*y+z*z', -1, 1, 0, 2, -1, 2)

h_2 = ROOT.TH2F(hID(), '', 50, -1, 1, 50, 0, 2)
h_3 = ROOT.TH3F(hID(), '', 20, -1, 1, 20, 0, 2, 20, -1, 2)

h_2 += f_2
h_3 += f_3
    use_scipy = False 
    logger.warning ("Numpy/scipy-dependent are disables!")
    
# =============================================================================
from ostap.histos.param import legendre_sum, chebyshev_sum
from ostap.core.core    import hID , fID 
from ostap.utils.timing import timing

h1 = ROOT.TH1F ( hID () , 'decreasing convex ' , 100 , 0 , 1 ) ; h1.Sumw2 () 
h2 = ROOT.TH1F ( hID () , 'increasing convex ' , 100 , 0 , 1 ) ; h2.Sumw2 () 
h3 = ROOT.TH1F ( hID () , 'increasing concave' , 100 , 0 , 1 ) ; h3.Sumw2 () 
h4 = ROOT.TH1F ( hID () , 'decreasing concave' , 100 , 0 , 1 ) ; h4.Sumw2 () 
h5 = ROOT.TH1F ( hID () , 'symmetric  convex ' , 100 , 0 , 1 ) ; h5.Sumw2 () 
h6 = ROOT.TH1F ( hID () , 'symmetric  concave' , 100 , 0 , 1 ) ; h6.Sumw2 () 

f1 = ROOT.TF1  ( fID ()  , '(x-1)**2'       , 0 , 1 )
f2 = ROOT.TF1  ( fID ()  , 'x**2'           , 0 , 1 )
f3 = ROOT.TF1  ( fID ()  , '1-(x-1)**2'     , 0 , 1 )
f4 = ROOT.TF1  ( fID ()  , '1-x**2'         , 0 , 1 )
f5 = ROOT.TF1  ( fID ()  , '4*(x-0.5)**2'   , 0 , 1 )
f6 = ROOT.TF1  ( fID ()  , '1-4*(x-0.5)**2' , 0 , 1 )

entries = 1000000

## random.seed(10) 
for i in range ( 0 , entries ) :
    h1.Fill ( f1.GetRandom() )
    h2.Fill ( f2.GetRandom() )
    h3.Fill ( f3.GetRandom() )
    h4.Fill ( f4.GetRandom() )
    h5.Fill ( f5.GetRandom() )