Esempio n. 1
0
def _rfr_table_ ( r , title = '' , prefix = '' , more_vars = {} ) :
    """ print RooFitResult  as a table
    >>> result = ...
    >>> result.table() 
    """

    from  ostap.fitting.utils    import fit_status, cov_qual
    rows = []

    ##  1. fit status
    status = r.status() 
    if status :
        row = attention ( 'Status' )  , '' , attention ( fit_status ( status ) ) , '' 
        rows.append ( row )
    else :
        row = 'Status'                , '' , allright  ( fit_status ( status ) ) , '' 
        rows.append ( row )

    ## 2. minumum NLL
    s , n = pretty_float ( r.minNll() )
    if n : n = '[10^%+d]' % n
    else : n = '' 

    rows.append ( ( "Minimized FCN/NLL value"    , n , '  ' + s , '' ) )

    s , n = pretty_float ( r.edm () )
    if n : n = '[10^%+d]' % n
    else : n = '' 

    rows.append ( ( 'Estimated distance to minimum' , n , '  ' + s , '' ) )
    
    cq = r.covQual()
    cn = '' 
    if  -1 == cq :
        cn = cov_qual  ( cq ) 
    elif 3 == cq :
        cn = allright  ( cov_qual ( cq ) )
    elif cq in (  0 , 1 , 2 ) :
        cn = attention ( cov_qual ( cq ) )
    else :
        cn = cov_qual  ( cq ) 
        
    rows.append ( ( 'Covariance matrix quality'     , '' , '  ' + cn , '' ) )
    
    for i in  range ( r.numStatusHistory() ) :
        label =  r.statusLabelHistory ( i )
        code  =  r.statusCodeHistory  ( i )
        row   =  'Status: %s '% label   , '' ,             '%d' % code             
        if not code in ( 0 , -1 ) :
            row = attention  ( row [ 0 ] ) , row [ 1 ] , '   ' + attention ( row [ 2 ] ) , '' 
        else    :
            row =              row [ 0 ]   , row [ 1 ] , '   ' + allright  ( row [ 2 ] ) , ''
        rows.append ( row )

    nbadnll = r.numInvalidNLL()
    if 0 < nbadnll :
        rows.append ( ( 'Invalid FCN/NLL evaluations' , '' , '  %d' % nbadnll , '' ) )


    with_globcorr = not ( (6,24) <= root_info < (6,28) )


    if with_globcorr : rows = [ ( '', 'Unit', 'Value' , 'Global/max correlation [%]') ] + rows
    else             : rows = [ ( '', 'Unit', 'Value' , 'Max correlation [%]') ] + rows

    pars_all   = r.params ( float_only = False )
    pars_float = r.params ( float_only = True  )

    ## constant/fix parameters 
    crows = [] 
    for p in pars_all :

        if p in pars_float : continue 
        v , a = pars_all [ p ]

        s , n = pretty_float  ( v.value()  ) 

        if n : n = '[10^%+d]' % n
        else : n = '' 
        row = p , n , '  ' + s + ' (fix)' , ''  
        crows.append ( row ) 

    ## floating parameters
    max_corr = False
    frows    = []
    for p in pars_float :

        v , a = pars_float [ p ]

        if not a.hasAsymError() :
            s , n = pretty_ve  ( v ) 
        else :
            s , n = pretty_2ve (  a.getVal() , a.getAsymErrorHi() , a.getAsymErrorLo() )

        if n : n = '[10^%+d]' % n
        else : n = '' 


        if 0 <= cq and 1 < len ( pars_float ) :

            mxr , mxv = r.max_cor    ( p )

            if with_globcorr :
                
                gc = -1.0 
                gc    = r.globalCorr ( p ) if 3 == cq else -1.00
                if 0 <= gc :  cc = '% +5.1f/(% +5.1f,%s)' % ( gc*100 , mxr*100   , mxv )
                else       :  cc = '% +5.1f : %-s'        % (          mxr * 100 , mxv )                
                if 0.95 < abs ( mxr ) or 0.95 < gc : cc = attention ( cc )

            else :
                
                cc = '% +5.1f : %-s'  % ( mxr * 100 , mxv )                
                if 0.95 < abs ( mxr ) : cc = attention ( cc )

                max_corr = True

            row = p , n , s , cc
            
        else :
            
            row = p , n , s
            
        frows.append ( row ) 

    ## more parameters
    mrows = []
    for p in sorted ( more_vars ) :

        func  = more_vars [ p ]
        
        v     = func ( r )
        
        s , n = pretty_ve  ( v ) 
        
        if n : n = '[10^%+d]' % n
        else : n = '' 

        cc = 'derived'
        row = p , n , s , cc
        mrows.append ( row ) 

    crows.sort()
    frows.sort()

    all = rows + crows + frows + mrows  

    import ostap.logger.table as T

    return T.table ( all , title = title if title else r.GetTitle() , prefix = prefix , alignment = 'llll' )
Esempio n. 2
0
def _fit_table_ ( rfit , title = '' , prefix = '' ) :
    """Print <code>TFitResult</code> as a table
    """
    from  ostap.fitting.utils    import fit_status, cov_qual
    from ostap.logger.colorized  import attention, allright
    from ostap.logger.utils      import pretty_float, pretty_ve, pretty_2ve, fmt_pretty_ve 
    
    header = ( '', 'Unit' , 'Value' )

    rows = []

    ##  0. minimized type
    row = "Minimizer Type" , '' , rfit.MinimizerType() 
    rows.append ( row )

    ##  0. minimized type
    v = rfit.IsValid()
    if v : row = "Valid"   , '' , 'True'
    else : row = "Valid"   , '' , attention ( 'False') 
    rows.append ( row )
    
    ##  1. fit status
    status = rfit.Status() 
    if status :
        row = attention ( 'Status' )  , '' , attention ( fit_status ( status ) ) 
        rows.append ( row )
    else :
        row =             'Status' , '' , allright ( fit_status ( status ) )   
        rows.append ( row )

    ## 4. covariance status
    cq = rfit.CovMatrixStatus() 
    cn = '' 
    if  -1 == cq              : cn = cov_qual  ( cq ) 
    elif 3 == cq              : cn = allright  ( cov_qual ( cq ) )
    elif cq in (  0 , 1 , 2 ) : cn = attention ( cov_qual ( cq ) )
    else                      : cn = cov_qual  ( cq )         
    rows.append ( ( 'Covariance matrix quality'     , '' , '  ' + cn  ) )


    ## 3-6. chi2,nDoF,chi2/nDoF,minFCN
    chi2 = rfit.Chi2 () 
    s , n = pretty_float ( chi2 )
    if n : n = '[10^%+d]' % n
    else : n = '' 
    rows.append ( ( "Chi2"       , n , '  ' + s ) )
    ##
    ndf = rfit.Ndf()
    rows.append ( ( "nDoF"       , '' , '  ' + '%d' % ndf   ) )
    ##
    c2ndf = rfit.Chi2 () /  ndf  
    s , n = pretty_float ( c2ndf  )
    if n : n = '[10^%+d]' % n
    else : n = '' 
    rows.append ( ( "Chi2/nDoF"  , n , '  ' + s   ) )
    ##
    minfcn = rfit.MinFcnValue() 
    s , n = pretty_float ( minfcn  )
    if n : n = '[10^%+d]' % n
    else : n = '' 
    rows.append ( ( "Minimal FCN"  , n , '  ' + s   ) )

    ## 7.Probability in %[%]
    prob = rfit.Prob() / 100
    rows.append ( ( "Probability"  , '[%]' , '  %5.3e' % prob ) )

    ## 8. distrance to minimum 
    edm  = rfit.Edm()
    s , n = pretty_float ( edm  )
    if n : n = '[10^%+d]' % n
    else : n = '' 
    rows.append ( ( "Estimated distance to minimum" , n , '  ' + s ) )

    ncalls = rfit.NCalls()
    rows.append ( ( "FCN calls" , '' , '  ' + '%d' % ncalls  ) )
    ##
    
    has_minos = False
    for i in rfit :
        if not rfit.HasMinosError( i ) : continue 
        has_minos = True
        break

    if has_minos :
        rows   = [ row + ('','','') for row in rows ]
        header = header + ( 'neg-minos' , 'pos-minos' , 'Global corr.' )   
    else         :
        rows = [ row + ('',)      for row in rows ] 
        header = header + ( 'Global corr.' , )   

    
    for i in rfit :
        
        pname  = rfit.GetParameterName ( i )
        value  = rfit.Value            ( i )  
        
        fixed  = rfit.IsParameterFixed ( i )
        fmte = '' 
        if fixed :
            v = value
            s  , n = pretty_float ( v )
            s = s + '(fixed)'
            nv = n
        else :
            error = rfit.Error ( i )
            v     = VE ( value , error * error )
            ##
            fmt , fmtv , fmte , n = fmt_pretty_ve  ( v )
            s = fmt % ( value / 10**n , error / 10**n )
            nv = n 
        if n : n = '[10^%+d]' % n
        else : n = '' 
        pname = "%-2d: %s"% ( i , pname )

        row = pname , n , '  ' + s             
        if not fixed and rfit.HasMinosError( i ) :
            if fmte : 
                error_low  = fmte   % ( rfit.LowerError ( i ) / 10**nv ) 
                error_up   = fmte   % ( rfit.UpperError ( i ) / 10**nv )
            else : 
                error_low  = "%+8g" % ( rfit.LowerError ( i ) / 10**nv ) 
                error_up   = "%+8g" % ( rfit.UpperError ( i ) / 10**nv ) 
        else :
            error_low  = '' 
            error_up   = '' 

        if has_minos  :
            row  = row  + ( error_low , error_up )

            
        gcc    = rfit.GlobalCC         ( i ) * 100 
        gcc  = '%+5.1f%%' % gcc
        row = row + ( gcc, )
        
        rows.append ( row ) 
        
    if not title :  title = rfit.GetTitle()
    
    import ostap.logger.table as T

    rows  = [ header ] + rows  

    return T.table ( rows , title = title , prefix = prefix )
Esempio n. 3
0
def _mn_table_ ( self , title =  '' , prefix = '' ) :
    """Build parameter table from (T)Minuit
    """
    
    header = ( 'Parameter' , '' , 'Value' )
    rows   = [] 

    from ostap.fitting.utils import fit_status  , cov_qual
    from ostap.logger.utils  import pretty_float, pretty_ve, pretty_2ve 

    status = self.GetStatus()
    if status :
        status = fit_status ( status ) 
        row = '  Status' , '' , status 
        rows.append ( row )

    stat   = _mn_stat_ (  self )

    istat  = stat.pop ( 'ISTAT'  , None )
    if not istat is None : 
        cq = ''
        if   -1 == istat             : cq =              cov_qual ( istat )
        elif  3 == istat             : cq = allright   ( cov_qual ( istat ) )
        elif  istat in ( 0 , 1 , 2 ) : cq = attentiont ( cov_qual ( istat ) )
        else                         : cq =              cov_qual ( istat )
        row = 'Covariance matrix quality' , '' , cq 
        rows.append  ( row ) 
    
    fmin   = stat.pop ( 'FMIN'   , None )
    if not fmin is None :
        s , n = pretty_float ( fmin ) 
        if n : n = '[10^%+d]' % n
        else : n = '' 
        row = 'Minimized FCN value' ,  n , s  
        rows.append ( row ) 
    
    fedm   = stat.pop ( 'FEDM'   , None )
    if not fedm is None :
        s , n = pretty_float ( fedm ) 
        if n : n = '[10^%+d]' % n
        else : n = '' 
        row = 'Estimated distance to minimum' ,  n , s 
        rows.append ( row ) 
    
    errdef = stat.pop ( 'ERRDEF' , None ) 
    ## needed ? 

    has_limits = False
    has_minos  = False

    val  = ctypes.c_double ( 0 )
    err  = ctypes.c_double ( 0 )
    low  = ctypes.c_double ( 0 ) 
    high = ctypes.c_double ( 0 ) 
    idx  = ctypes.c_int    ( 0 )
    
    dct_pars  = {} 
    ## loop over all parameters 
    for i in  self :
        
        name = ROOT.TString() 
        self.mnpout ( i , name , val , err , low , high , idx )
        if not 0 <= idx.value : continue
        
        dct = {}
        
        dct [ 'name' ] = '%-2d: %s' % ( i , str ( name ).strip() ) 
        dct [ 'value'] = val.value
        
        if low.value < high.value :
            dct [ 'low' ] = low .value
            dct [ 'high'] = high.value
            has_limits = True 
        if 0 <= err.value :
            dct [ 'error' ] = err.value
            mn_plus , mn_minus = _mn_minerr_ ( self , str ( name ) )
            if 0 < mn_plus or mn_minus < 0 :
                dct [ 'minos+' ] = mn_plus 
                dct [ 'minos-' ] = mn_minus 
                has_minos = True 
        dct_pars[ i ] = dct

    
    if has_minos :
        ## some parameters have MINOS errors, add columns
        header = ( header ) + ( 'neg-minos' , 'pos-minos' ) 
        rows   = [ r + ( '','' ) for r in rows ] 
        
    if has_limits :
        ## some parameters have LIMITS, add columns 
        header = ( header ) + ( 'low limit' , 'high limit' ) 
        rows   = [ r + ( '','' ) for r in rows ]         

    for p in dct_pars :
        
        pdict  = dct_pars [ p ]

        row = []
        
        row.append ( pdict.pop ( 'name' )      )
        
        value =      pdict.pop ( 'value'       )
        error =      pdict.pop ( 'error', None ) 

        if error is None :
            s , n = pretty_float ( value )
            s = "%s(fixed)" % s
        else :
            s , n = pretty_ve    ( VE ( value , error * error ) )

        if n : row.append ( '[10^%+d]' % n ) 
        else : row.append ( '' )

        row.append ( s )
        
        if has_minos :
            mn_plus  = pdict.pop ( 'minos+' , None ) 
            mn_minus = pdict.pop ( 'minos-' , None )
            
            if mn_plus  is None : mn_plus  = ''
            else                : mn_plus  = '%8f' % ( mn_plus   * 10** n )
            if mn_minus is None : mn_minus = ''
            else                : mn_minus = '%8f' % ( mn_minus  * 10** n )

            row.append ( mn_minus )  
            row.append ( mn_plus  )  

        if has_limits :
            low  = pdict.pop ( 'low'  , None ) 
            high = pdict.pop ( 'high' , None )
            if low  is None : low  = ''
            else            : low  = '%8f' %  ( low  * 10 ** n ) 
            if high is None : high = ''
            else            : high = '%8f' %  ( high * 10 ** n ) 
            row.append ( low ) 
            row.append ( high )
            
        row = tuple ( row )
        rows.append ( row ) 
        
    rows = [ header ] + rows
    
    from  ostap.logger.table import  table

    return   table ( rows ,  title = title  , prefix = prefix ) 
Esempio n. 4
0
def _rfr_table_(r, title='', prefix=''):
    """ print RooFitResult  as a table
    >>> result = ...
    >>> result.table() 
    """

    from ostap.fitting.utils import fit_status, cov_qual
    rows = []
    if r.status():
        row = attention(' Status'), '', attention(fit_status(r.status())), ''
        rows.append(row)

    s, n = pretty_float(r.minNll())
    if n: n = '[10^%+d]' % n
    else: n = ''

    rows.append(("Minimized FCN/NLL value", n, '  ' + s, ''))

    s, n = pretty_float(r.edm())
    if n: n = '[10^%+d]' % n
    else: n = ''

    rows.append(('Estimated distance to minimum', n, '  ' + s, ''))

    cq = r.covQual()
    cn = ''
    if -1 == cq:
        cn = cov_qual(cq)
    elif 3 == cq:
        cn = allright(cov_qual(cq))
    elif cq in (0, 1, 2):
        cn = attention(cov_qual(cq))
    else:
        cn = cov_qual(cq)

    rows.append(('Covariance matrix quality', '', '  ' + cn, ''))

    for i in range(r.numStatusHistory()):
        label = r.statusLabelHistory(i)
        code = r.statusCodeHistory(i)
        row = 'Status: %s ' % label, '', '%d' % code
        if not code in (0, -1):
            row = attention(row[0]), row[1], '   ' + attention(row[2]), ''
        else:
            row = row[0], row[1], '   ' + allright(row[2]), ''
        rows.append(row)

    nbadnll = r.numInvalidNLL()
    if 0 < nbadnll:
        rows.append(('Invalid FCN/NLL evaluations', '', '  %d' % nbadnll, ''))

    rows = [('', 'Unit', 'Value', 'Global/max correlation')] + rows

    pars_all = r.params(float_only=False)
    pars_float = r.params(float_only=True)

    ## constant/fix parameters
    crows = []
    for p in pars_all:
        if p in pars_float: continue
        v, a = pars_all[p]

        s, n = pretty_float(v.value())

        if n: n = '[10^%+d]' % n
        else: n = ''
        row = p, n, '  ' + s, ''
        crows.append(row)

    ## floating parameters
    frows = []
    for p in pars_float:
        v, a = pars_float[p]

        if not a.hasAsymError():
            s, n = pretty_ve(v)
        else:
            s, n = pretty_2ve(a.getVal(), a.getAsymErrorHi(),
                              a.getAsymErrorLo())

        if n: n = '[10^%+d]' % n
        else: n = ''

        cc = 'Not available'
        if 0 <= cq:
            mxr, mxv = r.max_cor(p)
            gc = r.globalCorr(p)

            cc = '%+5.3f/(%+5.3f,%s)' % (gc, mxr, mxv)
            if 0.95 < abs(gc) or 0.95 < abs(mxr): cc = attention(cc)

        row = p, n, s, cc
        frows.append(row)

    crows.sort()
    frows.sort()

    all = rows + crows + frows

    import ostap.logger.table as T

    all = T.align_column(all, 0, 'left')
    all = T.align_column(all, 1, 'left')
    all = T.align_column(all, 2, 'left')
    all = T.align_column(all, 3, 'left')

    for l in range(len(rows), len(all)):
        line = all[l]
        line = list(line)
        line[0] = allright(line[0])
        all[l] = tuple(line)

    if title:
        return T.table(all, title=title, prefix=prefix)
    else:
        return T.table(all, title=r.GetTitle(), prefix=prefix)