Пример #1
0
def test_modify_initial_tree(NEXP=10):
    """Add pseudoexepriments into TTree/TChain
    """

    files = prepare_data(1, 100000)

    logger.info('Add %s pseudoexepriments into TTree/TChain' % NEXP)

    logger.info('#files:    %s' % len(files))
    data = Data('S', files)
    logger.info('Initial Tree/Chain:\n%s' % data.chain.table(prefix='# '))

    ## pseudo experiments
    for e in progress_bar(range(NEXP)):
        h2_new = h2.sample()
        func = Ostap.Functions.FuncTH2(h2_new, 'pt', 'eta')
        data.chain.add_new_branch('w%d' % e, func)

    data = Data('S', files)
    logger.info('Tree/Chain after:\n%s' % data.chain.table(prefix='# '))

    counter = SE()
    for e in range(NEXP):
        weight = 'w%d' % e
        accepted = data.chain.sumVar('1', weight * cut)
        rejected = data.chain.sumVar('1', weight * ~cut)
        efficiency = 1 / (1 + rejected / accepted)
        logger.info("Experiment %3d, accepted/rejected %s/%s , eff = %s " %
                    (e, accepted, rejected, efficiency))
        counter += efficiency
    logger.info('Statistics of pseudoexperiments %s' % counter)
    logger.info('Mean/rms: %s[%%]/%.4f]%%]' %
                (counter.mean() * 100, counter.rms() * 100))
Пример #2
0
    def __init__ ( self                   ,
                   dbase   = "weights.db" , ## the name of data base with the weights 
                   factors = []           ) :
        
        #
        ## make some statistic
        #
        self._counter = SE ()
        self._nzeroes = 0 

        self.vars = [] 
        if not factors : return

        ## open database 
        with DBASE.open ( dbase , 'r' ) as db : ## READONLY
            
            for k in db :
                e = db[k]
                if hasattr ( e , '__len__' ) :  
                    logger.debug( 'DBASE "%.15s" key "%.15s" #%d' % ( dbase ,  k, len( e ) ) ) 
                
            ## loop over the weighting factors and build the function
            for f in factors :

                funval  = f[0]  ## accessor to the variable 
                funname = f[1]  ## address  in database 

                if isinstance ( funval , str ) :
                    varnam = funval 
                    funval = lambda s : getattr ( s , varnam )
                    
                ## 
                functions  = db.get ( funname , [] ) ## db[ funname ]
                if not functions :
                    logger.warning('No reweighting is available for %s, skip it' % funname )

                merge = True
                if 2 < len ( f ) : merge = f[2] 
                
                if not isinstance (  functions , ( list , tuple ) ) :
                    functions = [ functions ]
                    
                ## merge list of functions into single function 
                if merge and 1 < len ( functions)  : 
                
                    single_func = functions[0] * functions [1] 
                    
                    for fun in functions [2:] :
                        single_func *= fun
                            
                    functions  = [ single_func ]
                    
                self.vars += [ ( funname , funval , functions , SE() ) ]  
Пример #3
0
def test_add_to_dataset(NEXP=10):
    """Add pseudoexepriments into RooDataSet
    """

    logger.info('Add %s pseudoexepriments into RooDataSet' % NEXP)

    files = prepare_data(1, 100000)

    logger.info('#files:    %s' % len(files))
    data = Data('S', files)
    logger.info('Initial Tree/Chain:\n%s' % data.chain.table(prefix='# '))

    import ostap.fitting.pyselectors
    dataset, _ = data.chain.fill_dataset(['mass', 'pt', 'eta'])

    logger.info('Initial dataset:\n%s' % dataset.table(prefix='# '))

    ## pseudo experiments
    for e in progress_bar(range(NEXP)):
        h2_new = h2.sample()
        func = Ostap.Functions.FuncRooTH2(h2_new, 'pt', 'eta')
        dataset.add_new_var('w%d' % e, func)

    logger.info('Dataset after:\n%s' % dataset.table(prefix='# '))

    counter = SE()
    for e in range(NEXP):
        weight = 'w%d' % e
        accepted = dataset.sumVar('1', weight * cut)
        rejected = dataset.sumVar('1', weight * ~cut)
        efficiency = 1 / (1 + rejected / accepted)
        logger.info("Experiment %3d, accepted/rejected %s/%s , eff = %s " %
                    (e, accepted, rejected, efficiency))

        counter += efficiency
    logger.info('Statistics of pseudoexperiments %s' % counter)
    logger.info('Mean/rms: %s[%%]/%.4f[%%]' %
                (counter.mean() * 100, counter.rms() * 100))
Пример #4
0
    def __init__ ( self                   ,
                   dbase   = "weights.db" , ## the name of data base with the weights 
                   factors = []           ) :
        
        #
        ## make some statistic
        #
        self.__counter = SE ()
        self.__nzeroes = 0 

        self.__vars    = [] 
        if not factors : return

        ## open database 
        with DBASE.open ( dbase , 'r' ) as db : ## READONLY
            
            for k in db :
                e = db[k]
                if hasattr ( e , '__len__' ) :  
                    logger.debug( "DBASE ``%.15s'' key ``%.15s'' #%d" % ( dbase ,  k, len( e ) ) ) 
                
            ## loop over the weighting factors and build the function
            for wvar in factors :

                funval  = wvar.accessor  ## accessor to the variable 
                funname = wvar.address   ## address  in database 
                merge   = wvar.merge     ## merge sequence of callables?
                skip    = wvar.skip      ## skip   some of them?
                
                if isinstance ( funval , str ) :
                    ## funval = operator.attrgetter( funval ) 
                    funval = AttrGetter( funval ) 
                    
                ## 
                functions  = db.get ( funname , [] ) ## db[ funname ]
                if not functions :
                    logger.warning("No reweighting is available for ``%s'', skip it" % funname )
                    continue
                                
                if not isinstance (  functions , ( list , tuple ) ) :
                    functions = [ functions ]                    
                
                flen = len(functions) 
                if   0 < skip and skip      < flen :
                    logger.info  ("Use only %d first iterations for ``%s''" % ( skip , funname ) )
                    functions = functions[:skip] 
                elif 0 > skip and abs(skip) < flen :
                    logger.info  ("Skip last %d iterations for ``%s''" % ( skip , funname ) )
                    functions = functions[:-1*skip] 
                elif 0 == skip :
                    pass
                else :
                    logger.error("Invalid ``skip'' parameter %s/%d for ``%s''" % ( skip , flen , funname ) )
                
                ## nullify the uncertainties except for the last histogram
                _functions = []
                _first     = True 
                for f in reversed ( functions ) :
                    if isinstance ( f , ROOT.TH1 ) and _first : 
                        ff = f.clone()
                        for i in ff :
                            v     = float ( ff[i] )
                            ff[i] = VE(v,0)
                        _functions.append ( ff  )                        
                        _first = False 
                    else :
                        _functions.append ( f  )
                        
                _functions.reverse() 
                functions = _functions
                    
                ## merge list of functions into single function 
                if merge and 1 < len ( functions)  : 
                            
                    ## single_func = functions[0] * functions [1] 
                    single_func = MULT ( functions[0] , functions [1] )
                    
                    for fun in functions [2:] :

                        ## multiply it                               
                        ## single_func *= fun
                        single_func = MULT ( single_func , fun )
                            
                    functions  = [ single_func ]
                    
                self.__vars += [ ( funname , funval , functions , SE() ) ]
                
        self.__vars = tuple ( self.__vars ) 
Пример #5
0
    def __init__(
            self,
            dbase="weights.db",  ## the name of data base with the weights 
            factors=[]):

        #
        ## make some statistic
        #
        self.__counter = SE()
        self.__nzeroes = 0

        self.__vars = []
        if not factors: return
        self.__dbase = dbase

        ## open database

        self.__table = [('Reweighting', 'accessor', '#', 'merged?', 'skip')]
        rows = []

        with DBASE.open(dbase, 'r') as db:  ## READONLY

            logger.debug('Reweigting database: \n%s' % db.table(prefix='# '))

            ## loop over the weighting factors and build the function
            for wvar in factors:

                funval = wvar.accessor  ## accessor to the variable
                funname = wvar.address  ## address  in database
                merge = wvar.merge  ## merge sequence of callables?
                skip = wvar.skip  ## skip   some of them?

                row = []

                row.append(funname)

                if isinstance(funval, str):
                    row.append(funval)
                    ## funval = operator.attrgetter( funval )
                    funval = AttrGetter(funval)
                elif isinstance(funval, AttrGetter):
                    atts = funval.attributes
                    if 1 == len(atts): atts = atts[0]
                    row.append(str(atts))
                else:
                    row.append('')

                ##
                functions = db.get(funname, [])  ## db[ funname ]
                if not functions:
                    logger.warning(
                        "No reweighting is available for ``%s'', skip it" %
                        funname)
                    continue

                if not isinstance(functions, (list, tuple)):
                    functions = [functions]

                flen = len(functions)
                if 0 < skip and skip < flen:
                    logger.info("Use only %d first iterations for ``%s''" %
                                (skip, funname))
                    functions = functions[:skip]
                elif 0 > skip and abs(skip) < flen:
                    logger.info("Skip last %d iterations for ``%s''" %
                                (skip, funname))
                    functions = functions[:skip]
                elif 0 == skip:
                    pass
                else:
                    logger.error(
                        "Invalid ``skip'' parameter %s/%d for ``%s''" %
                        (skip, flen, funname))
                row.append('%d' % flen)

                ## nullify the uncertainties except for the last histogram
                _functions = []
                _first = True
                for f in reversed(functions):
                    if isinstance(f, ROOT.TH1) and _first:
                        ff = f.clone()
                        for i in ff:
                            v = float(ff[i])
                            ff[i] = VE(v, 0)
                        _functions.append(ff)
                        _first = False
                    else:
                        _functions.append(f)

                _functions.reverse()
                functions = _functions

                row.append('+' if merge else '-')
                row.append('%s' % skip)

                ## merge list of functions into single function
                if merge and 1 < len(functions):

                    ## single_func = functions[0] * functions [1]
                    single_func = MULT(functions[0], functions[1])

                    for fun in functions[2:]:

                        ## multiply it
                        ## single_func *= fun
                        single_func = MULT(single_func, fun)

                    functions = [single_func]

                self.__vars += [(funname, funval, functions, SE())]

                self.__table.append(row)

        self.__vars = tuple(self.__vars)
Пример #6
0
def test_carlson_cmp():
    """Compare local/GSL and plain integration methods
    """

    logger = getLogger('test_carlson_1')
    logger.info('Compare local/GSL and plain integration methods  ')

    cF1 = SE()
    cF2 = SE()

    cD1 = SE()
    cD2 = SE()

    cC1 = SE()
    cC2 = SE()

    cJ1 = SE()
    cJ2 = SE()

    cG1 = SE()
    cG2 = SE()

    with gslCount():
        for i in range(10000):

            x = random.uniform(0, 1)
            y = random.uniform(0, 1)
            z = random.uniform(0, 1)
            p = random.uniform(0, 1)

            ## RF

            v1 = RF(x, y, z)
            v2 = RF_gsl(x, y, z)
            v3 = RF_int(x, y, z)
            cF1 += (v1 / v2) - 1
            cF2 += (v1 / v3) - 1

            v1 = RF(x, y, p)
            v2 = RF_gsl(x, y, p)
            v3 = RF_int(x, y, p)
            cF1 += (v1 / v2) - 1
            cF2 += (v1 / v3) - 1

            v1 = RF(x, p, z)
            v2 = RF_gsl(x, p, z)
            v3 = RF_int(x, p, z)
            cF1 += (v1 / v2) - 1
            cF2 += (v1 / v3) - 1

            v1 = RF(p, x, z)
            v2 = RF_gsl(p, x, z)
            v3 = RF_int(p, x, z)
            cF1 += (v1 / v2) - 1
            cF2 += (v1 / v3) - 1

            ## RJ

            v1 = RJ(x, y, z, p)
            v2 = RJ_gsl(x, y, z, p)
            v3 = RJ_int(x, y, z, p)
            cJ1 += (v1 / v2) - 1
            cJ2 += (v1 / v3) - 1

            v1 = RJ(x, y, p, z)
            v2 = RJ_gsl(x, y, p, z)
            v3 = RJ_int(x, y, p, z)
            cJ1 += (v1 / v2) - 1
            cJ2 += (v1 / v3) - 1

            v1 = RJ(x, p, z, y)
            v2 = RJ_gsl(x, p, z, y)
            v3 = RJ_int(x, p, z, y)
            cJ1 += (v1 / v2) - 1
            cJ2 += (v1 / v3) - 1

            v1 = RJ(p, y, z, x)
            v2 = RJ_gsl(p, y, z, x)
            v3 = RJ_int(p, y, z, x)
            cJ1 += (v1 / v2) - 1
            cJ2 += (v1 / v3) - 1

            ## RD

            v1 = RD(x, y, z)
            v2 = RD_gsl(x, y, z)
            v3 = RD_int(x, y, z)
            cD1 += (v1 / v2) - 1
            cD2 += (v1 / v3) - 1

            v1 = RD(x, y, p)
            v2 = RD_gsl(x, y, p)
            v3 = RD_int(x, y, p)
            cD1 += (v1 / v2) - 1
            cD2 += (v1 / v3) - 1

            v1 = RD(x, p, z)
            v2 = RD_gsl(x, p, z)
            v3 = RD_int(x, p, z)
            cD1 += (v1 / v2) - 1
            cD2 += (v1 / v3) - 1

            v1 = RD(p, y, z)
            v2 = RD_gsl(p, y, z)
            v3 = RD_int(p, y, z)
            cD1 += (v1 / v2) - 1
            cD2 += (v1 / v3) - 1

            ## RC

            v1 = RC(x, y)
            v2 = RC_gsl(x, y)
            v3 = RC_int(x, y)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            v1 = RC(x, z)
            v2 = RC_gsl(x, z)
            v3 = RC_int(x, z)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            v1 = RC(x, p)
            v2 = RC_gsl(x, p)
            v3 = RC_int(x, p)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            v1 = RC(y, z)
            v2 = RC_gsl(y, z)
            v3 = RC_int(y, z)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            v1 = RC(y, p)
            v2 = RC_gsl(y, p)
            v3 = RC_int(y, p)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            v1 = RC(z, p)
            v2 = RC_gsl(z, p)
            v3 = RC_int(z, p)
            cC1 += (v1 / v2) - 1
            cC2 += (v1 / v2) - 1

            ## RG

            v1 = RG(x, y, z)
            v2 = RG_gsl(x, y, z)
            v3 = RG_int(x, y, z)
            cG1 += (v1 / v2) - 1
            cG2 += (v1 / v2) - 1

            v1 = RG(x, y, p)
            v2 = RG_gsl(x, y, p)
            v3 = RG_int(x, y, p)
            cG1 += (v1 / v2) - 1
            cG2 += (v1 / v2) - 1

            v1 = RG(x, p, z)
            v2 = RG_gsl(x, p, z)
            v3 = RG_int(x, p, z)
            cG1 += (v1 / v2) - 1
            cG2 += (v1 / v2) - 1

            v1 = RG(p, y, z)
            v2 = RG_gsl(p, y, z)
            v3 = RG_int(p, y, z)
            cG1 += (v1 / v2) - 1
            cG2 += (v1 / v2) - 1

    rows = [('Name', '#', 'Mean', 'rms', 'min', 'max')]

    for n, c in [('RF', cF1), ('RJ', cJ1), ('RD', cD1), ('RC', cC1),
                 ('RG', cG1)]:

        mean = c.mean()
        tmean = '%+.5g' % mean
        if prec_TIGHT < abs(mean): tmean = attention(tmean)

        rms = c.rms()
        trms = '%+.5g' % rms
        if prec_TIGHT < abs(rms): trms = attention(trms)

        vmin = c.min()
        tmin = '%+.5g' % vmin
        if prec_TIGHT < abs(vmin): tmin = attention(tmin)

        vmax = c.max()
        tmax = '%+.5g' % vmax
        if prec_TIGHT < abs(vmax): tmax = attention(tmax)

        row = n, '%d' % c.nEntries(), tmean, trms, tmin, tmax
        rows.append(row)

    table = T.table(rows,
                    title='Test of Carlson forms, Local vs GSL ',
                    prefix='# ',
                    alignment='lrllll')

    logger.info('Test Carlson forms, Local vs GSL:\n%s' % table)

    rows = [('Name', '#', 'Mean', 'rms', 'min', 'max')]
    for n, c in [('RF', cF2), ('RJ', cJ2), ('RD', cD2), ('RC', cC2),
                 ('RG', cG2)]:

        mean = c.mean()
        tmean = '%+.5g' % mean
        if prec_TIGHT < abs(mean): tmean = attention(tmean)

        rms = c.rms()
        trms = '%+.5g' % rms
        if prec_TIGHT < abs(rms): trms = attention(trms)

        vmin = c.min()
        tmin = '%+.5g' % vmin
        if prec_TIGHT < abs(vmin): tmin = attention(tmin)

        vmax = c.max()
        tmax = '%+.5g' % vmax
        if prec_TIGHT < abs(vmax): tmax = attention(tmax)

        row = n, '%d' % c.nEntries(), tmean, trms, tmin, tmax
        rows.append(row)

    table = T.table(rows,
                    title='Test of Carlson forms, Local vs plain integration',
                    prefix='# ',
                    alignment='lrllll')

    logger.info('Test Carlson forms, Local vs plain integrtation:\n%s' % table)