Exemple #1
0
def _pickled_setitem_(self, key, value):
    """ Add object (pickle if needed)  to dbase
    >>> db['A/B/C'] = obj
    """
    ##
    if self.writeback:
        self.cache[key] = value

    ## not TObject? pickle it and convert to Ostap.BLOB
    if not isinstance(value, ROOT.TObject):
        ## (1) pickle it
        f = BytesIO()
        p = Pickler(f, self.protocol)
        p.dump(value)
        ## (2) zip it
        z = zlib.compress(f.getvalue(), self.compresslevel)
        ## (3) put it into  BLOB
        from ostap.core.core import Ostap
        blob = Ostap.BLOB(key)
        status = Ostap.blob_from_bytes(blob, z)
        value = blob
        del z, f, p

    ## finally use ROOT
    self.dict[key] = value
Exemple #2
0
def _fr_statCov_ ( frame       ,
                   expression1 ,
                   expression2 ,
                   cuts = ''   ) :
    """Get the statistic for pair of expressions in DataFrame
    
    >>>  frame  = ...
    >>>  stat1 , stat2 , cov2 , len = framw.statCov( 'x' , 'y' )
    
    Apply some cuts:
    >>> stat1 , stat2 , cov2 , len = frame.statCov( 'x' , 'y' , 'z>0' )
    
    """
    import ostap.math.linalg 
    stat1  = Ostap.WStatEntity       ()
    stat2  = Ostap.WStatEntity       ()
    cov2   = Ostap.Math.SymMatrix2x2 ()

    length = Ostap.StatVar.statCov ( frame       ,
                                     expression1 ,
                                     expression2 ,
                                     cuts        ,    
                                     stat1       ,
                                     stat2       ,
                                     cov2        ) 
        
    return stat1 , stat2 , cov2 , length
Exemple #3
0
def _fr_len_(f):
    """Get the length/size of the data frame
    >>> frame = ...
    >>> print len(frame)
    """
    cpf = Ostap.DataFrame(f)  ## make independent loop?
    return cpf.Count().GetValue()
Exemple #4
0
    def __getitem__(self, key):
        """ Get object (unpickle if needed)  from dbase
        >>> obj = db['A/B/C']
        """

        try:
            value = self.cache[key]
        except KeyError:

            ## value = self.dict [ key ]
            tkey, value = self.dict.get_key_object(key)
            self.__sizes[key] = tkey.GetNbytes()

            ## blob ?
            from ostap.core.core import Ostap
            if isinstance(value, Ostap.BLOB):
                ## unpack it!
                z = Ostap.blob_to_bytes(value)
                u = zlib.decompress(z)
                ## unpickle it!
                f = BytesIO(u)
                value = Unpickler(f).load()
                del z, u, f
            if self.writeback:
                self.cache[key] = value

        return value
Exemple #5
0
def test_covtransform1():

    logger.info('Descartes to polar tranformation')

    ## Descartes coordinates
    X = 1, 4

    ##  their covarinac ematrix
    C = Ostap.SymMatrix(2)()
    C[0, 0] = 0.20
    C[0, 1] = 0.05
    C[1, 1] = 0.30

    R = C.correlations()

    ##  Polar coordinated
    r = lambda x, y: math.sqrt(x * x + y * y)
    phi = lambda x, y: math.atan2(y, x)

    C_polar = transform(C, X, r, phi)

    R_polar = C_polar.correlations()

    logger.info('Descartes Covariance  :\n%s' % C)
    logger.info('Descartes Correlation :\n%s' % R)
    logger.info('Polar     Covariance  :\n%s' % C_polar)
    logger.info('Polar     Correlation :\n%s' % R_polar)
Exemple #6
0
def two_yields(total, fraction):
    """Construct two yields from the total yield and fraction
    >>> total = ...
    >>> fraction = ...
    >>> y1 , y2 = two_yields ( total , fraction )
    """
    var1 = total
    var2 = fraction

    vnames = var1.name, var2.name

    name = 'Yield1_%s_%s' % vnames
    title = 'Yield1 (%s) and (%s)' % vnames

    yield1 = scale_var(total, fraction, name, title)

    name = 'Yield2_%s_%s' % vnames
    title = 'Yield2 (%s) and (%s)' % vnames

    formula = '(%s)*(1-(%s))' % vnames
    varlist = ROOT.RooArgList(var1, var2)
    yield2 = Ostap.FormulaVar(name, title, formula, varlist)

    yield2._varlist = [var1, var2, varlist]

    return yield1, yield2
Exemple #7
0
def transform(C, X, *Y):
    """ Transform the covariance nmatrix C at point X to the variables Y(X)
    >>> X = 1 , 2
    >>> C = Ostap.SymMatrix(2)()
    >>> C [ 0 , 0 ] = 0.20
    >>> C [ 0 , 1 ] = 0.05
    >>> C [ 1 , 1 ] = 0.30
    >>> r   = lambda x , y : (x*x+y*y)**2
    >>> phi = lambda x , y : math.atan2 ( y , x )
    >>> C_polar = transform ( C , X , r , phi ) 
    """

    ny = len(Y)
    assert 1 <= ny, 'Invalid size of Y!'

    nx = len(X)
    if C is None and 1 <= nx:
        C = Ostap.SymMatrix(nx)()
        for i, x in enumerate(X):
            xx = VE(x)
            C[i, i] = xx.cov2()

    shape = C.shape
    assert shape[0] == shape[1] and shape[0] == nx, 'Invalid shape of matrix C!'

    CC = Ostap.SymMatrix(nx)()
    for i in range(CC.kRows):
        CC[i, i] = C(i, i)
        for j in range(i + 1, CC.kCols):
            v = 0.5 * (C(i, j) + C(j, i))
            CC[i, j] = v

    XX = Ostap.Vector(nx)()
    for i, x in enumerate(X):
        XX[i] = float(x)

    ## get vector-with-errors
    XX = Ostap.VectorE(nx)(XX, CC)

    R = XX.transform(*Y)

    return R.cov2()
Exemple #8
0
def var_from_name ( w , varset ) :
    """ Convert name/expression into variable/formula
    """
    w = w.strip() 
    if    0 <= w.find('(') < what.find(')') : pass
    elif  0 <  w.find('*')                  : pass
    elif  0 <  w.find('/')                  : pass
    elif  0 <  w.find('%')                  : pass 
    elif  0 <  w.find('+')                  : pass
    elif  0 <  w.find('-')                  : pass
    else :
        v = varset[w]
        return v
    ##
    
    vlst = ROOT.RooArgList()
    for s in varset : vlst.add ( s )
    #
    f = Ostap.FormulaVar( w , w , vlst )
    return f 
Exemple #9
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 
Exemple #10
0
def test_linalg2() :
    """The main function to test linear algebra
    """
    
    logger.info('Test Linaear Algebra: ')
    
    LA3 = Ostap.Vector(3)
    l1  = LA3(0,1,2)
    l2  = LA3(3,4,5)
    
    logger.info ( 'l1 , l2 : %s %s '  % ( l1 , l2  ) )
    logger.info ( 'l1 + l2 : %s    '  % ( l1 + l2  ) )
    
    logger.info ( 'l1 - l2 : %s    '  % ( l1 - l2  ) )
    logger.info ( 'l1 * l2 : %s    '  % ( l1 * l2  ) )
    logger.info ( 'l1 *  2 : %s    '  % ( l1 *  2  ) )
    logger.info ( ' 2 * l2 : %s    '  % ( 2  * l2  ) )
    logger.info ( 'l1 /  2 : %s    '  % ( l1 /  2  ) )
    
    l1 /= 2 
    logger.info ( 'l1 /= 2 : %s    '  % l1 )
    
    l1 *= 2 
    logger.info ( 'l1 *= 2 : %s    '  % l1 )


    ## if ( 3 , 5 ) <= python_version :
        
    ##     logger.info ( 'l1 @ l2 : %s    '  % ( l1 @ l2  ) )
    ##     logger.info ( 'l1 @  2 : %s    '  % ( l1 @  2  ) )
    ##     logger.info ( ' 2 @ l2 : %s    '  % ( 2  @ l2  ) )
        
    logger.info('TEST matrices: ')
    
    m22 = Ostap.Math.Matrix(2,2) ()
    m23 = Ostap.Math.Matrix(2,3) ()
    s22 = Ostap.Math.SymMatrix(2)()
    
    l2  = Ostap.Math.Vector(2)()
    l3  = Ostap.Math.Vector(3)()
    
    l2[0]    = 1
    l2[1]    = 2
    
    l3[0]    = 1
    l3[1]    = 2
    l3[1]    = 3
    
    logger.info ( 'l2 , l3 : %s %s '  % ( l2 , l3  ) )
    
    m22[0,0] = 1
    m22[0,1] = 1
    m22[1,1] = 1
    
    m23[0,0] = 1
    m23[1,1] = 1
    m23[0,2] = 1
    
    s22[0,0] = 2
    s22[1,0] = 1
    s22[1,1] = 3
    
    logger.info ( 'm22\n%s'    % m22     ) 
    logger.info ( 's22\n%s'    % s22     ) 
    logger.info ( 'm23\n%s'    % m23     ) 
    logger.info ( 'm22/3\n%s'  % (m22/3) )
    
    logger.info ( 'm23*3\n%s'  % (m23*3) ) 

    logger.info ( 'm22**3\n%s' % m22**3  ) 
    logger.info ( 's22**4\n%s' % s22**4  ) 

    logger.info ( 'm22 * m23 :\n%s' % ( m22 * m23 ) ) 
    logger.info ( 'm22 *  l2 : %s ' % ( m22 * l2  ) ) 
    logger.info ( 'l2  * m22 : %s ' % ( l2  * m22 ) ) 
    logger.info ( 'm23 *  l3 : %s ' % ( m23 * l3  ) ) 
    logger.info ( 'l2  * m23 : %s ' % ( l2  * m23 ) )
    
    logger.info ( 'm22 * s22 + 2 * m22 :\n%s ' %  ( m22*s22 + 2*m22  ) )
    logger.info ( 'm22 == m22*1.0 : %s ' % (  m22 == m22 * 1.0 ) )
    logger.info ( 'm22 != m22*1.1 : %s ' % (  m22 != m22 * 1.1 ) )
    logger.info ( 'm23 == m23*1.0 : %s ' % (  m23 == m23 * 1.0 ) )
    logger.info ( 'm23 != m23*1.1 : %s ' % (  m23 != m23 * 1.1 ) )
    logger.info ( 'l1  == l1 *1.0 : %s ' % (  l1  == l1  * 1.0 ) )
    logger.info ( 'l1  != l1 *1.1 : %s ' % (  l1  != l1  * 1.1 ) )
    logger.info ( 's22 == s22*1.0 : %s ' % (  s22 == s22 * 1.0 ) )
    logger.info ( 's22 != s22*1.1 : %s ' % (  s22 != s22 * 1.1 ) )
    
    logger.info ( ' l1 == (0,1,2) : %s ' % (  l1 == ( 0 , 1 , 2 ) ) )
    logger.info ( ' l1 == [0,1,2] : %s ' % (  l1 == [ 0 , 1 , 2 ] ) )

    
    ## if ( 3 , 5 ) <= python_version :
        
    ##     logger.info ( 'm23 @ 3   :\n%s' % ( m23 @ 3   ) ) 
    ##     logger.info ( 'm22 @ m23 :\n%s' % ( m22 @ m23 ) ) 
    ##     logger.info ( 'm22 @  l2 : %s ' % ( m22 @ l2  ) ) 
    ##     logger.info ( 'm23 @  l3 : %s ' % ( m23 @ l3  ) ) 
         

    m22[0,0] = 1
    m22[0,1] = 2
    m22[1,0] = 2
    m22[1,1] = 3
    
    s22[0,0] = 1
    s22[0,1] = 2
    s22[1,1] = 3
    
    logger.info ( ' m22 == s22     : %s ' % ( m22 == s22       ) )
    logger.info ( ' m22 == s22*1.0 : %s ' % ( m22 == s22 * 1.0 ) )
    logger.info ( ' m22 != s22*1.1 : %s ' % ( m22 != s22 * 1.1 ) )


    m22 += m22
    logger.info ( ' m22 += m22  :\n%s ' % m22 )

    m22 -= m22*2
    logger.info ( ' m22 += m22*2 :\n%s ' % m22 )

    m22 += s22*0
    logger.info ( ' m22 += s22*0 :\n%s ' % m22 )

    m22 -= s22*2
    logger.info ( ' m22 -= s22*2 :\n%s ' % m22 )

    s22 += s22*2
    logger.info ( ' s22 += s22*2 :\n%s ' % s22 )

    s22 -= s22*2
    logger.info ( ' s22 -= s22*2 :\n%s ' % s22 )
    

    if np :
        logger.info ( 'Operations with numpy objects')
        
        v2 = np.array ( [1.0,2.0]      )
        v3 = np.array ( [1.0,2.0,3.0 ] )

        logger.info ( 'v2  * l2  : %s' % ( v2  * l2  ) )
        logger.info ( 'l3  * v3  : %s' % ( l3  * v3  ) )
        logger.info ( 's22 * v2  : %s' % ( s22 * v2  ) )
        logger.info ( 'm22 * v2  : %s' % ( m22 * v2  ) )
        logger.info ( 'm23 * v3  : %s' % ( m23 * v3  ) )
        
        logger.info ( 'm22  * m22(np) :\n%s' % ( m22 * m22.to_numpy() ) )
        logger.info ( 's22  * s22(np) :\n%s' % ( s22 * s22.to_numpy() ) )
        logger.info ( 's22  * m23(np) :\n%s' % ( s22 * m23.to_numpy() ) )        
        logger.info ( 'l2   * m22(np) :\n%s' % ( l2  * m22.to_numpy() ) )

        
    logger.info ( 'SVector with errors')

    v2  = Ostap.Math.VectorE (2)()

    v2 [ 0 ] = 3
    v2 [ 1 ] = 4
    
    v2 . cov2 () [ 0 , 0 ] = 0.10
    v2 . cov2 () [ 0 , 1 ] = 0.05
    v2 . cov2 () [ 1 , 1 ] = 0.20

    rho = lambda x,y : ( x * x + y * y ) **  0.5
    phi = lambda x,y : math.atan2 ( y , x ) 
    

    r1 = v2.transform ( rho , phi )
    logger.info ( " -> rho, phi %s " % r1 )

    r2 = v2.transform ( rho  )
    logger.info ( " -> rho      %s " % r2 )
Exemple #11
0
def test_linalgt():
    """The main function to test linear algebra
    """

    logger.info('Test Linear Algebra: ')

    logger.info('TEST vectors: ')

    l1 = Ostap.TVector(3)
    l2 = Ostap.TVector(3)

    l1[0], l1[1], l1[2] = 0, 1, 2
    l2[0], l2[1], l2[2] = 3, 4, 5

    logger.info('l1 , l2 : %s %s ' % (l1, l2))
    logger.info('l1 + l2 : %s    ' % (l1 + l2))

    logger.info('l1 - l2 : %s    ' % (l1 - l2))
    logger.info('l1 * l2 : %s    ' % (l1 * l2))
    logger.info('l1 *  2 : %s    ' % (l1 * 2))
    logger.info(' 2 * l2 : %s    ' % (2 * l2))
    logger.info('l1 /  2 : %s    ' % (l1 / 2))

    l1 /= 2
    logger.info('l1 /= 2 : %s    ' % l1)
    l1 *= 2
    logger.info('l1 *= 2 : %s    ' % l1)

    ## if ( 3 , 5 ) <= python_version :

    ##     logger.info ( 'l1 @ l2 : %s    '  % ( l1 @ l2  ) )
    ##     logger.info ( 'l1 @  2 : %s    '  % ( l1 @  2  ) )
    ##     logger.info ( ' 2 @ l2 : %s    '  % ( 2  @ l2  ) )

    logger.info('TEST matrices: ')

    m22 = Ostap.Math.TMatrix(2, 2)
    m23 = Ostap.Math.TMatrix(2, 3)
    s22 = Ostap.Math.TMatrixSym(2)

    l2 = Ostap.TVector(2)
    l3 = Ostap.TVector(3)

    l2[0] = 1
    l2[1] = 2

    l3[0] = 1
    l3[1] = 2
    l3[1] = 3

    logger.info('l2 , l3 : %s %s ' % (l2, l3))

    ## if ( 3 , 5 ) <= python_version :

    ##     logger.info ( 'm23 @ 3   :\n%s' % ( m23 @ 3   ) )
    ##     logger.info ( 'm22 @ m23 :\n%s' % ( m22 @ m23 ) )
    ##     logger.info ( 'm22 @  l2 : %s ' % ( m22 @ l2  ) )
    ##     logger.info ( 'm23 @  l3 : %s ' % ( m23 @ l3  ) )

    m22[0, 0] = 1
    m22[0, 1] = 1
    m22[1, 1] = 1

    m23[0, 0] = 1
    m23[1, 1] = 1
    m23[0, 2] = 1

    s22[0, 0] = 2
    s22[1, 0] = 1
    s22[1, 1] = 3

    logger.info('m22\n%s' % m22)
    logger.info('s22\n%s' % s22)
    logger.info('m23\n%s' % m23)
    logger.info('m22/3\n%s' % (m22 / 3))
    logger.info('m23*3\n%s' % (m23 * 3))

    logger.info('m22**3\n%s' % m22**3)
    logger.info('s22**4\n%s' % s22**4)

    logger.info('m22 * m23 :\n%s' % (m22 * m23))
    logger.info('m22 *  l2 : %s ' % (m22 * l2))
    logger.info('l2  * m22 : %s ' % (l2 * m22))
    logger.info('m23 *  l3 : %s ' % (m23 * l3))
    logger.info('l2  * m23 : %s ' % (l2 * m23))

    logger.info('m22 * s22 + 2 * m22 :\n%s ' % (m22 * s22 + 2 * m22))
    logger.info('m22 == m22*1.0 : %s ' % (m22 == m22 * 1.0))
    logger.info('m22 != m22*1.1 : %s ' % (m22 != m22 * 1.1))
    logger.info('m23 == m23*1.0 : %s ' % (m23 == m23 * 1.0))
    logger.info('m23 != m23*1.1 : %s ' % (m23 != m23 * 1.1))
    logger.info('l1  == l1 *1.0 : %s ' % (l1 == l1 * 1.0))
    logger.info('l1  != l1 *1.1 : %s ' % (l1 != l1 * 1.1))
    logger.info('s22 == s22*1.0 : %s ' % (s22 == s22 * 1.0))
    logger.info('s22 != s22*1.1 : %s ' % (s22 != s22 * 1.1))

    logger.info(' l1 == (0,1,2) : %s ' % (l1 == (0, 1, 2)))
    logger.info(' l1 == [0,1,2] : %s ' % (l1 == [0, 1, 2]))

    m22[0, 0] = 1
    m22[0, 1] = 2
    m22[1, 0] = 2
    m22[1, 1] = 3

    s22[0, 0] = 1
    s22[0, 1] = 2
    s22[1, 1] = 3

    logger.info(' m22 == s22     : %s ' % (m22 == s22))
    logger.info(' m22 == s22*1.0 : %s ' % (m22 == s22 * 1.0))
    logger.info(' m22 != s22*1.1 : %s ' % (m22 != s22 * 1.1))

    m22 += m22 * 2
    m22 -= m22 * 1

    m22 += s22 * 2
    m22 -= s22 * 1

    s22 += s22 * 2
    s22 -= s22 * 1

    ## DISABLE!!!
    if np and False:

        logger.info('Operations with numpy objects')

        v2 = np.array([1.0, 2.0])
        v3 = np.array([1.0, 2.0, 3.0])

        print('v1,l2:', v2, l2)

        logger.info('v2  * l2  : %s' % (v2 * l2))
        logger.info('l3  * v3  : %s' % (l3 * v3))
        logger.info('s22 * v2  : %s' % (s22 * v2))
        logger.info('m22 * v2  : %s' % (m22 * v2))
        logger.info('m23 * v3  : %s' % (m23 * v3))

        n22_m = m22.to_numpy()
        n22_s = s22.to_numpy()
        n23 = m23.to_numpy()

        if 62006 <= root_version_int:
            logger.warning("Tests with numpy are broken for ROOT %s" %
                           root_version_int)
        else:
            logger.info('m22  * m22(np) :\n%s' % (m22 * m22.to_numpy()))
            logger.info('s22  * s22(np) :\n%s' % (s22 * s22.to_numpy()))
            logger.info('s22  * m23(np) :\n%s' % (s22 * m23.to_numpy()))
            logger.info('l2   * m22(np) :\n%s' % (l2 * m22.to_numpy()))
Exemple #12
0
    def __init__(self,
                 mass,
                 name='Y',
                 power=0,
                 m1s=None,
                 sigma=None,
                 alphaL=1.5462,
                 alphaR=1.6952,
                 nL=1.3110,
                 nR=1.5751e+01):

        #
        PDF.__init__(self, name, mass)
        #
        if 9460. in self.mass and 10023. in self.mass and 10355. in self.mass:
            gev_ = 1000
        elif 9.460 in self.mass and 10.023 in self.mass and 10.355 in self.mass:
            gev_ = 1
        else:
            raise AttributeError("Illegal mass range %s<m<%s" % self.xminmax())

        m_y1s = 9.46030 * gev_
        s_y1s = 4.03195e-02 * gev_
        dm_y2s = 10.02326 * gev_ - m_y1s
        dm_y3s = 10.3552 * gev_ - m_y1s

        # =====================================================================

        # =====================================================================
        ## Y(1S)
        # =====================================================================
        self.__aL = self.make_var(alphaL, "aL_%s" % name,
                                  "#alpha_{L}(%s)" % name, alphaL, 1.5462, 0.1,
                                  10)
        self.__nL = self.make_var(nL, "nL_%s" % name, "n_{L}(%s)" % name, nL,
                                  1.3119, 1.e-5, 25)
        self.__aR = self.make_var(alphaR, "aR_%s" % name,
                                  "#alpha_{R}(%s)" % name, alphaR, 1.6952e+00,
                                  0.1, 10)
        self.__nR = self.make_var(nR, "nR_%s" % name, "n_{R}(%s)" % name, nR,
                                  1.5751e+01, 1.e-5, 25)

        self.__m1s = self.make_var(m1s, "m1S_%s" % name, "mass Y1S(%s)" % name,
                                   m1s, m_y1s, m_y1s - 0.15 * s_y1s,
                                   m_y1s + 0.15 * s_y1s)

        self.__s1s = self.make_var(sigma, "s1S_%s" % name,
                                   "sigma Y1S(%s)" % name, sigma, s_y1s,
                                   0.3 * s_y1s, 4 * s_y1s)
        self.__sigma = self.__s1s

        self.__Y1S = CB2_pdf(name + '1S',
                             xvar=self.mass,
                             mean=self.__m1s,
                             sigma=self.__s1s,
                             alphaL=self.__aL,
                             alphaR=self.__aR,
                             nL=self.__nL,
                             nR=self.__nR)

        # =====================================================================
        ## Y(2S)
        # =====================================================================
        self.__dm2s = self.make_var(None, "dm2s" + name, "dm2s(%s)" % name,
                                    dm_y2s, dm_y2s - 0.20 * s_y1s,
                                    dm_y2s + 0.20 * s_y1s)

        self.__aset11 = ROOT.RooArgList(self.__m1s, self.__dm2s)

        ## self.__m2s    = ROOT.RooFormulaVar (
        ##     "m_" + name + '2S'   ,
        ##     "m2s(%s)"  % name    ,
        ##     "%s+%s" % ( self.__m1s.GetName() , self.__dm2s.GetName()  ) ,
        ##    self.__aset11       )
        self.__m2s = Ostap.MoreRooFit.Addition("m_" + name + '2S',
                                               "m2s(%s)" % name, self.__m1s,
                                               self.__dm2s)

        self.__aset12 = ROOT.RooArgList(self.__sigma, self.__m1s, self.__m2s)
        self.s2s = Ostap.FormulaVar(
            "sigma_" + name + '2S', "#sigma_{Y2S}(%s)" % name,
            "%s*(%s/%s)" % (self.__sigma.GetName(), self.__m2s.GetName(),
                            self.__m1s.GetName()), self.__aset12)

        self.__Y2S = CB2_pdf(name + '2S',
                             xvar=self.mass,
                             mean=self.__m2s,
                             sigma=self.__s2s,
                             alphaL=self.__aL,
                             alphaR=self.__aR,
                             nL=self.__nL,
                             nR=self.__nR)

        # =====================================================================
        ## Y(3S)
        # =====================================================================
        self.__dm3s = self.make_var(None, "dm3s" + name, "dm3s(%s)" % name,
                                    dm_y3s, dm_y3s - 0.20 * s_y1s,
                                    dm_y3s + 0.20 * s_y1s)

        self.__aset21 = ROOT.RooArgList(self.__m1s, self.__dm3s)
        ## self.__m3s    = ROOT.RooFormulaVar (
        ##    "m_"       + name + '(3S)' ,
        ##    "m3s(%s)"  % name          ,
        ##    "%s+%s" % ( self.__m1s.GetName() , self.__dm3s.GetName() ) ,
        ##    self.__aset21       )
        self.__m3s = Ostap.MoreRooFit.Addition("m_" + name + '3S',
                                               "m3s(%s)" % name, self.__m1s,
                                               self.__dm3s)

        self.__aset22 = ROOT.RooArgList(self.__sigma, self.__m1s, self.__m3s)
        self.__s3s = Ostap.FormulaVar(
            "sigma_" + name + '3S', "#sigma_{Y3S}(%s)" % name,
            "%s*(%s/%s)" % (self.__sigma.GetName(), self.__m3s.GetName(),
                            self.__m1s.GetName()), self.__aset22)

        self.__Y3S = CB2_pdf(name + '3S',
                             xvar=self.mass,
                             mean=self.__m3s,
                             sigma=self.__s3s,
                             alphaL=self.__aL,
                             alphaR=self.__aR,
                             nL=self.__nL,
                             nR=self.__nR)

        #
        ## the actual signal PDFs
        #
        self.__y1s = self.__Y1S.pdf
        self.__y2s = self.__Y2S.pdf
        self.__y3s = self.__Y3S.pdf

        ## use helper function to create background
        self.background = self.make_bkg(power, 'Bkg%s' % name, self.mass)

        self.__n1s = self.make_var(None, "N1S" + name, "Signal(Y1S)", None,
                                   1000, 0, 1.e+7)
        self.__n2s = self.make_var(None, "N2S" + name, "Signal(Y2S)", None,
                                   300, 0, 1.e+6)
        self.__n3s = self.make_var(None, "N3S" + name, "Signal(Y3S)", None,
                                   100, 0, 1.e+6)
        self.__b = self.make_var(None, "B" + name, "Background", None, 100, 0,
                                 1.e+8)

        self.alist1 = ROOT.RooArgList(self.__y1s, self.__y2s, self.__y3s)
        self.alist2 = ROOT.RooArgList(self.__n1s, self.__n2s, self.__n3s)

        self.alist1.add(self.background.pdf)
        self.alist2.add(self.__b)

        self.pdf = ROOT.RooAddPdf("manca_%s" % name, "manca(%s)" % name,
                                  self.alist1, self.alist2)

        self.__dm2s.setConstant(True)
        self.__dm3s.setConstant(True)

        self._splots = []

        self.s1_name = self.N1S.GetName()
        self.s2_name = self.N2S.GetName()
        self.s3_name = self.N3S.GetName()
        self.b_name = self.B.GetName()

        #
        ## finally declare components
        #
        self.signals().add(self.__y1s)
        self.signals().add(self.__y2s)
        self.signals().add(self.__y3s)
        self.backgrounds().add(self.background.pdf)

        ## save configurtaion
        self.config = {
            'mass': self.mass,
            'name': self.name,
            'power': self.power,
            'm1s': self.m1s,
            'sigma': self.sigma,
            'alphaL': self.aL,
            'alphaR': self.aR,
            'nL': self.nL,
            'nR': self.nR,
        }
Exemple #13
0
    def __init__(self,
                 mass,
                 name='Y',
                 power=0,
                 m1s=None,
                 sigma=None,
                 a0=1.91,
                 a1=None,
                 a2=None):

        #
        PDF.__init__(self, name, mass)
        #
        if 9460. in self.mass and 10023. in self.mass and 10355. in self.mass:
            gev_ = 1000
        elif 9.460 in self.mass and 10.023 in self.mass and 10.355 in self.mass:
            gev_ = 1
        else:
            raise AttributeError("Illegal mass range %s<m<%s" % self.xminmax())

        m_y1s = 9.46030 * gev_
        s_y1s = 4.3679e-02 * gev_
        dm_y2s = 10.02326 * gev_ - m_y1s
        dm_y3s = 10.3552 * gev_ - m_y1s

        # =====================================================================
        ## Y(1S)
        # =====================================================================

        self.__a0 = self.make_var(a0, 'a0m_%s' % name,
                                  "a0 for Needham's function", a0, 1.91, 0.1,
                                  3.0)

        self.__a1 = self.make_var(a1, 'a1m_%s' % name,
                                  "a1 for Needham's function", a1,
                                  1.1174 / gev_, -10.0 / gev_, 10.0 / gev_)

        self.__a2 = self.make_var(a2, 'a2m_%s' % name,
                                  "a2 for Needham's function", a2,
                                  -5.299 / gev_**2, -100.0 / gev_**2,
                                  100.0 / gev_**2)

        ## default logic does not work nicely here, therefore we need to be explicit:
        if a0 is None and not self.a0.isConstant(): self.a0.fix(1.91)
        if a1 is None and not self.a1.isConstant(): self.a1.fix(1.1174 / gev_)
        if a2 is None and not self.a2.isConstant():
            self.a2.fix(-5.299 / gev_**2)

        self.__m1s = self.make_var(m1s, "m1S_%s" % name, "mass Y1S(%s)" % name,
                                   m1s, m_y1s, m_y1s - 0.15 * s_y1s,
                                   m_y1s + 0.15 * s_y1s)

        self.__s1s = self.make_var(sigma, "s1S_%s" % name,
                                   "sigma Y1S(%s)" % name, sigma, s_y1s,
                                   0.3 * s_y1s, 4 * s_y1s)

        self.__sigma = self.s1s
        self.__Y1S = Needham_pdf(name + '1S',
                                 xvar=self.mass,
                                 mean=self.m1s,
                                 sigma=self.s1s,
                                 a0=self.a0,
                                 a1=self.a1,
                                 a2=self.a2)

        # =====================================================================
        ## Y(2S)
        # =====================================================================
        self.__dm2s = self.make_var(None, "dm2s" + name, "dm2s(%s)" % name,
                                    dm_y2s, dm_y2s - 0.20 * s_y1s,
                                    dm_y2s + 0.20 * s_y1s)

        self.__aset11 = ROOT.RooArgList(self.__m1s, self.__dm2s)

        ## self.__m2s    = ROOT.RooFormulaVar (
        ## "m_" + name + '2S'   ,
        ## "m2s(%s)"  % name    ,
        ## "%s+%s" % ( self.__m1s.GetName() , self.__dm2s.GetName()  ) ,
        ## self.__aset11       )
        self.__m2s = Ostap.MoreRooFit.Addition("m_" + name + '2S',
                                               "m2s(%s)" % name, self.__m1s,
                                               self.__dm2s)

        self.__aset12 = ROOT.RooArgList(self.__sigma, self.__m1s, self.__m2s)
        self.__s2s = Ostap.FormulaVar(
            "sigma_" + name + '2S', "#sigma_{Y2S}(%s)" % name,
            "%s*(%s/%s)" % (self.__sigma.GetName(), self.__m2s.GetName(),
                            self.__m1s.GetName()), self.__aset12)

        self.__Y2S = Needham_pdf(name + '2S',
                                 xvar=self.mass,
                                 mean=self.m2s,
                                 sigma=self.s2s,
                                 a0=self.a0,
                                 a1=self.a1,
                                 a2=self.a2)

        # =====================================================================
        ## Y(3S)
        # =====================================================================
        self.__dm3s = self.make_var(None, "dm3s" + name, "dm3s(%s)" % name,
                                    dm_y3s, dm_y3s - 0.20 * s_y1s,
                                    dm_y3s + 0.20 * s_y1s)

        self.__aset21 = ROOT.RooArgList(self.__m1s, self.__dm3s)
        ## self.__m3s    = ROOT.RooFormulaVar (
        ## "m_"       + name + '(3S)' ,
        ## "m3s(%s)"  % name          ,
        ## "%s+%s" % ( self.__m1s.GetName() , self.__dm3s.GetName() ) ,
        ## self.__aset21       )
        self.__m3s = Ostap.MoreRooFit.Addition("m_" + name + '3S',
                                               "m3s(%s)" % name, self.__m1s,
                                               self.__dm3s)

        self.__aset22 = ROOT.RooArgList(self.__sigma, self.__m1s, self.__m3s)
        self.__s3s = Ostap.FormulaVar(
            "sigma_" + name + '3S', "#sigma_{Y3S}(%s)" % name,
            "%s*(%s/%s)" % (self.__sigma.GetName(), self.__m3s.GetName(),
                            self.__m1s.GetName()), self.__aset22)

        self.Y3S = Needham_pdf(name + '3S',
                               xvar=self.mass,
                               mean=self.m3s,
                               sigma=self.s3s,
                               a0=self.a0,
                               a1=self.a1,
                               a2=self.a2)

        #
        ## the actual signal PDFs
        #
        self.__y1s = self.Y1S.pdf
        self.__y2s = self.Y2S.pdf
        self.__y3s = self.Y3S.pdf

        self.__power = power
        ## use helper function to create background
        self.background = self.make_bkg(power, 'Bkg%s' % name, self.mass)

        self.__n1s = self.make_var(None, "N1S" + name, "Signal(Y1S)", None,
                                   1000, 0, 1.e+8)
        self.__n2s = self.make_var(None, "N2S" + name, "Signal(Y2S)", None,
                                   300, 0, 1.e+7)
        self.__n3s = self.make_var(None, "N3S" + name, "Signal(Y3S)", None,
                                   100, 0, 1.e+6)
        self.__b = self.make_var(None, "B" + name, "Background", None, 100, 0,
                                 1.e+8)

        self.alist1 = ROOT.RooArgList(self.__y1s, self.__y2s, self.__y3s)
        self.alist2 = ROOT.RooArgList(self.__n1s, self.__n2s, self.__n3s)

        self.alist1.add(self.background.pdf)
        self.alist2.add(self.__b)

        self.pdf = ROOT.RooAddPdf("manca_%s" % name, "manca(%s)" % name,
                                  self.alist1, self.alist2)

        self.__dm2s.setConstant(True)
        self.__dm3s.setConstant(True)

        self._splots = []

        self.s1_name = self.__n1s.GetName()
        self.s2_name = self.__n2s.GetName()
        self.s3_name = self.__n3s.GetName()

        #
        ## finally declare components
        #
        self.signals().add(self.__y1s)
        self.signals().add(self.__y2s)
        self.signals().add(self.__y3s)
        self.backgrounds().add(self.background.pdf)

        ## save configurtaion
        self.config = {
            'mass': self.mass,
            'name': self.name,
            'power': self.power,
            'm1s': self.m1s,
            'sigma': self.sigma,
            'a0': self.a0,
            'a1': self.a1,
            'a2': self.a2,
        }