コード例 #1
0
def _weights2map_ ( weights_files ) :
    
    from ostap.core.core import cpp, std, Ostap
    MAP = std.map   ( 'std::string', 'std::string' )
    
    weights  = WeightsFiles ( weights_files )
    _weights = MAP() 
    for method , xml in weights.files.iteritems() :
        _weights [ method ] = xml

    assert not _weights .empty() , \
           'Invalid MAP size %s for' % ( _weights.size() , weights )
    
    assert not _weights.empty() , "Invalid weights_files: %s"  % weights.files
    return _weights 
コード例 #2
0
def combined_hdata(sample, varset, histograms, name='', title=''):

    MAP = std.map('std::string', 'TH1*')
    PAIR = std.pair('const std::string', 'TH1*')
    mm = MAP()
    for key in histograms:
        mm.insert(PAIR(key, histograms[key]))

    name = name if name else dsID()
    title = title if title else 'Data for simultaneous fit/%s' % sample.GetName(
    )

    varlst = ROOT.RooArgList()
    if isinstance(varset, ROOT.RooAbsReal): varlst.add(varset)
    else:
        for v in varset:
            varlst.add(v)

    return ROOT.RooDataHist(name, title, varlst, sample, mm)
コード例 #3
0
def _inputs2map_ ( inputs ) :
    """Convert input structure to Ostap.TMVA.MAPS
    """
    from ostap.core.core import cpp, std, Ostap
    MAP     = std.map ( 'std::string', 'std::string' )
    
    _inputs = MAP()
    assert isinstance ( inputs , ( dict , tuple , list ) ) , \
           'Invalid type of "inputs": %s' % inputs
    
    if   isinstance ( inputs , dict  ) :
        for k , v  in inputs.itertems() : _inputs[k] = v
    elif isinstance ( inputs , ( tuple , list ) ) :
        for i in inputs :
            if isinstance ( i , str ) : k , v = i , i
            else                      : k , v = i
            _inputs[k] = v 

    ## 
    assert not _inputs .empty() and _inputs.size() == len ( inputs ), \
           'Invalid MAP size %s for %s' % ( _inputs.size() , inputs ) 

    return _inputs 
コード例 #4
0
ファイル: chopping.py プロジェクト: mazurov/ostap
def addChoppingResponse(dataset,
                        chopper,
                        N,
                        inputs,
                        weights_files,
                        category_name='chopping',
                        prefix='tmva_',
                        suffix='_response',
                        aux=0.9):
    """
    Helper function to add TMVA  response into dataset
    >>> tar_file = trainer.tar_file
    >>> dataset  = ...
    >>> inputs = [ 'var1' , 'var2' , 'var2' ]
    >>> dataset.addTMVAResponce (  inputs , tar_file , prefix = 'tmva_' )
    """
    assert isinstance(N, int) and 1 < N < 10000, 'Invalid "N" %s' % N

    if isinstance(chopper, str):

        if chopper in dataset:
            chopper = getattr(varset, chopper)
        else:
            varset = dataset.get()
            varlist = ROOT.RooArgList()
            for v in varset:
                varlist.add(v)
            chopper = ROOT.RooFormulaVar('chopping', chopper, varlist)
            logger.debug('Create chopping function %s' % chopper)

    assert isinstance(chopper,
                      ROOT.RooAbsReal), 'Invalid choper type %s' % chopper

    category = ROOT.RooCategory(
        category_name, 'Chopping category: (%s)%%%d' % (chopper.GetTitle(), N))
    for i in range(N):

        if N < 10: cn = category_name + '_%d' % i
        if N < 100: cn = category_name + '_%02d' % i
        elif N < 1000: cn = category_name + '_%03d' % i
        elif N < 10000: cn = category_name + '_%04d' % i
        else: cn = category_name + '_%d' % i
        category.defineType(cn, i)

    ## decode inputs&weights

    from ostap.tools.tmva import _inputs2map_, _weights2map_

    _inputs = _inputs2map_(inputs)

    files = WeightsFiles(weights_files).files
    files_ = [_weights2map_(f) for f in files]

    from ostap.core.core import cpp, std, Ostap
    MAP = std.map('std::string', 'std::string')
    MAPS = std.vector(MAP)
    _maps = MAPS()
    for m in files_:
        _maps.push_back(m)

    sc = Ostap.TMVA.addChoppingResponse(dataset, chopper, category, N, _inputs,
                                        _maps, prefix, suffix, aux)
    if sc.isFailure():
        logger.error('Error from Ostap::TMVA::addChoppingResponse %s' % sc)
    return sc
コード例 #5
0
def combined_hdata ( sample        ,
                     varset        ,
                     histograms    ,
                     name     = '' ,
                     title    = '' ) :
    """Create combined binned dataset for simultaneous fit
    - combine 2D histograms:

    >>> sample = ROOT.RooCategory ( 'sample' , 'fitting sample' , 'A' , 'B' )
    >>> hA  = ...
    >>> hB  = ...
    >>> var = ROOT.RooRealVar ( ... )
    >>> ds  = combined_hdata ( sample , var , { 'A' : hA , 'B' : hB } )  

    - combine 2D histograms:

    >>> sample = ROOT.RooCategory ( 'sample' , 'fitting sample' , 'A' , 'B' )
    >>> hA   = ...
    >>> hB   = ...
    >>> xvar = ROOT.RooRealVar ( ... )
    >>> yvar = ROOT.RooRealVar ( ... )
    >>> ds   = combined_hdata ( sample , (xvar, yvar) , { 'A' : hA , 'B' : hB } )  

    - combine 3D histograms:

    >>> sample = ROOT.RooCategory ( 'sample' , 'fitting sample' , 'A' , 'B' )
    >>> hA   = ...
    >>> hB   = ...
    >>> xvar = ROOT.RooRealVar ( ... )
    >>> yvar = ROOT.RooRealVar ( ... )
    >>> zvar = ROOT.RooRealVar ( ... )
    >>> ds   = combined_hdata ( sample , (xvar, yvar, zvar) , { 'A' : hA , 'B' : hB } )  
    """

    MAP  = std.map  ( 'std::string'       , 'TH1*' )
    PAIR = std.pair ( 'const std::string' , 'TH1*' )
    mm   = MAP()
    
    d1   = 0 
    d2   = 0 
    d3   = 0
    
    labels = sample.labels ()
    
    for label in labels :
        
        histo = histograms.pop  ( label ) 
        
        if   isinstance ( histo , ROOT.TH3 ) : d3 += 1
        elif isinstance ( histo , ROOT.TH2 ) : d2 += 1
        elif isinstance ( histo , ROOT.TH1 ) : d1 += 1
        
        mm.insert ( PAIR ( label , histo ) )


    assert not historgams, 'Unknown histograms: %s' % histograms.keys() 
        
    assert ( d3 or d2 ) and ( d3 or d1 ) and ( d2 or d1 ), \
           'Histograms of different dimensions cannot be combined !'
        
    name  = name  if name  else dsID()
    title = title if title else 'Data for simultaneous fit/%s' % sample.GetName()

    varlst = ROOT.RooArgList()
    if isinstance ( varset , ROOT.RooAbsReal ) : varlst.add ( varset )
    else :  
        for v in varset : varlst.add ( v )

    assert ( d3 and 3 == len ( varlst ) ) or \
           ( d2 and 2 == len ( varlst ) ) or \
           ( d1 and 1 == len ( varlst ) )  , \
           'Invalid dimension of dataset!'
    
    return ROOT.RooDataHist ( name , title , varlst , sample  , mm )