Beispiel #1
0
def __find_param(var, input_cats, split_pars_dict, split_cat_pars):
    from P2VV.RooFitWrappers import RooObject
    ws = RooObject.ws()
    split_cats = split_pars_dict.get( ws[ var.GetName() ], set() )
    if not var.isFundamental() :
        split = False
        for splVar, splCats in split_pars_dict.iteritems() :
            if var.dependsOn(splVar) : split = True
            split_cats |= splCats
        if not split : split_cats = set()
    if not split_cats :
        return var
    else :
        catLabels = [(cat.GetName(), cat.getLabel()) for cat in input_cats if cat in split_cats]
        catsStr = ';'.join(lab[1] for lab in catLabels)
        if len(catLabels) > 1 : catsStr = '{' + catsStr + '}'
        from P2VV.Utilities.General import getSplitPar
        splitVar = getSplitPar(var.GetName(), catsStr, split_cat_pars)
        assert splitVar, 'P2VV - ERROR: Bs2Jpsiphi_PdfBuilder: parameter "%s" is set to be constrained for category "%s", but it is not found in PDF'\
               % (var.GetName(), catsStr)
        from ROOT import RooRealVar, RooCategory, RooFormulaVar
        from P2VV.RooFitWrappers import RealVar, Category, FormulaVar
        wrappers = { RooRealVar : RealVar, RooCategory : Category, RooFormulaVar : FormulaVar }
        assert type(splitVar) in wrappers\
               , 'P2VV - ERROR: Bs2Jpsiphi_PdfBuilder: wrapping of RooFit object "%s" of type "%s" not implemented (yet)'\
                 % ( splitVar.GetName(), type(splitVar) )
        return wrappers[ type(splitVar) ]( Name = splitVar.GetName() )
Beispiel #2
0
from ROOT import RooBinning

split_cats = [[hlt1_excl_biased]]
split_pars = [[par for par in mass_pdf.Parameters() if par.getAttribute('Yield')]]

# build simultaneous mass PDF
sWeight_mass_pdf = SimultaneousPdf(mass_pdf.GetName() + '_simul',
                                   MasterPdf       = mass_pdf,
                                   SplitCategories = split_cats,
                                   SplitParameters = split_pars)

from math import sqrt
# set yields for categories
split_cat_pars = sWeight_mass_pdf.getVariables()
for ct in hlt1_excl_biased:
    sig_yield = getSplitPar('N_signal', ct.GetName(), split_cat_pars)
    bkg_yield = getSplitPar('N_background', ct.GetName(), split_cat_pars)
    
    sel_str = '!(%s-%d)' % (hlt1_excl_biased.GetName(), ct.getVal())
    
    nEv    = data.sumEntries()
    nEvBin = data.sumEntries(sel_str)
    
    sig_yield.setVal( sig_yield.getVal() * nEvBin / nEv )
    sig_yield.setError( sqrt( sig_yield.getVal() ) )
    sig_yield.setMin(0.)
    sig_yield.setMax(nEvBin)
    bkg_yield.setVal( bkg_yield.getVal() * nEvBin / nEv )
    bkg_yield.setError( sqrt( bkg_yield.getVal() ) )
    bkg_yield.setMin(0.)
    bkg_yield.setMax(nEvBin)
sWeightMassPdf = SimultaneousPdf(  massPdf.GetName() + '_simul'
                                 , MasterPdf       = massPdf
                                 , SplitCategories = [ pars[1] for pars in splitPars ]
                                 , SplitParameters = [ pars[0] for pars in splitPars ]
                                )
massPdfObs  = sWeightMassPdf.getObservables(preDataSet)
massPdfPars = sWeightMassPdf.getParameters(preDataSet)

if model == 'ipatia2' :
    # set values of constant Ipatia 2 parameters
    from P2VV.Utilities.General import getSplitPar
    for splitCat in sWeightMassPdf.indexCat() :
        sWeightMassPdf.indexCat().setIndex( splitCat.getVal() )
        KKState = observables['KKMassCat'].getLabel()
        for it, parName in enumerate(constSplitPars) :
            par = getSplitPar( parName, KKState, massPdfPars )
            par.setVal( constSplitVals[KKState][it] )

if parFileIn or parFileOut :
    # create PDF configuration object
    from P2VV.Parameterizations.FullPDFs import PdfConfiguration as PdfConfig
    pdfConfig = PdfConfig()

print 120 * '='
if parFileIn :
    # read parameters from file
    pdfConfig.readParametersFromFile( filePath = parFileIn )
    pdfConfig.setParametersInPdf(sWeightMassPdf)
print 'P2VV - INFO: createBs2JpsiKKDataSet: observables in mass PDF:'
massPdfObs.Print('v')
print 'P2VV - INFO: createBs2JpsiKKDataSet: parameters in mass PDF:'
        KKMassState = -1
        if splitCat.isFundamental() :
            selStr = '!(%s-%d)' % ( splitCat.GetName(), splitCatState.getVal() )
            if splitCat.GetName() == observables['KKMassCat'].GetName() : KKMassState = splitCatState.getVal()
        else :
            splitCat.setLabel( splitCatState.GetName() )
            selStr = ' && '.join( '!(%s-%d)' % ( cat.GetName(), cat.getIndex() ) for cat in splitCat.inputCatList() )
            if observables['KKMassCat'] in splitCats[0] :
                for cat in splitCat.inputCatList() :
                    if cat.GetName() == observables['KKMassCat'].GetName() : KKMassState = cat.getIndex()

        nEv    = dataSets['pre'][0].sumEntries()
        nEvBin = dataSets['pre'][0].sumEntries(selStr)
        for yName in yieldNames :
            stateName = splitCatState.GetName()
            yieldVar = getSplitPar( yName, stateName, massPdfPars )

            if len(eventFracs) > 1 and KKMassState >= 0 :
                sigFrac = eventFracs[ KKMassState + 1 ][yName] if eventFracs[ KKMassState + 1 ][yName]\
                          else 1. - sum( val for val in eventFracs[ KKMassState + 1 ].itervalues() if val )
                yieldVar.setVal( sigFrac * nEvBin )
            else :
                yieldVar.setVal( yieldVar.getVal() * nEvBin / nEv )

            yieldVar.setError( sqrt( yieldVar.getVal() ) )
            yieldVar.setMin(0.)
            yieldVar.setMax(nEvBin)

        splitCatState = splitCatIter.Next()

    # determine mass parameters in each subsample with a fit
Beispiel #5
0
    # build simultaneous mass PDF
    from P2VV.RooFitWrappers import SimultaneousPdf

    sWeight_mass_pdf = SimultaneousPdf(
        mass_pdf.GetName() + "_simul", MasterPdf=mass_pdf, SplitCategories=split_cats, SplitParameters=split_params
    )

    # set yields for categories
    split_cat = sWeight_mass_pdf.indexCat()
    split_vars = sWeight_mass_pdf.getVariables()
    from P2VV.Utilities.General import getSplitPar
    from math import sqrt

    for state in split_cat:
        sigYield = getSplitPar("N_signal", state.GetName(), split_vars)
        bkgYield = getSplitPar("N_background", state.GetName(), split_vars)

        selStr = "{0} == {0}::{1}".format(split_cat.GetName(), state.GetName())

        nEv = data.sumEntries()
        nEvBin = data.sumEntries(selStr)

        sigYield.setVal(sigYield.getVal() * nEvBin / nEv)
        sigYield.setError(sqrt(sigYield.getVal()))
        sigYield.setMin(0.0)
        sigYield.setMax(nEvBin)
        bkgYield.setVal(bkgYield.getVal() * nEvBin / nEv)
        bkgYield.setError(sqrt(bkgYield.getVal()))
        bkgYield.setMin(0.0)
        bkgYield.setMax(nEvBin)
###########################################

# create integration sets
from ROOT import RooArgSet
intSet = RooArgSet()
normSet = RooArgSet( massPdfObs.find('mass') )

# get signal and background mass PDFs
comps = [ 'sig', 'cbkg' ]
yields = dict( sig = { }, cbkg = { } )
pdfs   = dict( sig = { }, cbkg = { } )
from P2VV.Utilities.General import getSplitPar
splitCat = simMassPdf.indexCat()
for state in splitCat :
    stateName = state.GetName()
    yields['sig'][stateName]  = getSplitPar( 'N_sigMass',  stateName, massPdfPars ).getVal()
    yields['cbkg'][stateName] = getSplitPar( 'N_cbkgMass', stateName, massPdfPars ).getVal()
    if genMass :
        pdfs['sig'][stateName]  = simMassPdf.getPdf(stateName).pdfList().at(0)
        pdfs['cbkg'][stateName] = simMassPdf.getPdf(stateName).pdfList().at(1)
    else :
        pdfs['sig'][stateName]  = simMassPdf.getPdf(stateName).pdfList().at(0).createIntegral( intSet, normSet )
        pdfs['cbkg'][stateName] = simMassPdf.getPdf(stateName).pdfList().at(1).createIntegral( intSet, normSet )
for comp in comps :
    yields[comp]['total'] = sum( y for y in yields[comp].itervalues() )

print 'P2VV - INFO: signal and background yields:'
for comp in comps :
    print '  %4s: %9.2f' % ( comp, yields[comp]['total'] )

# create datasets
    split_params = [[par for par in mass_pdf.Parameters() if par.getAttribute('Yield')]]

    # build simultaneous mass PDF
    from P2VV.RooFitWrappers import SimultaneousPdf
    sWeight_mass_pdf = SimultaneousPdf(mass_pdf.GetName() + '_simul'
                                       , MasterPdf       = mass_pdf
                                       , SplitCategories = split_cats
                                       , SplitParameters = split_params)

    # set yields for categories
    split_cat  = sWeight_mass_pdf.indexCat()
    split_vars = sWeight_mass_pdf.getVariables()
    from P2VV.Utilities.General import getSplitPar
    from math import sqrt
    for state in split_cat:
        sigYield = getSplitPar( 'N_signal', state.GetName(), split_vars )
        bkgYield = getSplitPar( 'N_background', state.GetName(), split_vars )
        
        selStr = '{0} == {0}::{1}'.format(split_cat.GetName(), state.GetName())
        
        nEv    = data.sumEntries()
        nEvBin = data.sumEntries(selStr)
        
        sigYield.setVal( sigYield.getVal() * nEvBin / nEv )
        sigYield.setError( sqrt( sigYield.getVal() ) )
        sigYield.setMin(0.)
        sigYield.setMax(nEvBin)
        bkgYield.setVal( bkgYield.getVal() * nEvBin / nEv )
        bkgYield.setError( sqrt( bkgYield.getVal() ) )
        bkgYield.setMin(0.)
        bkgYield.setMax(nEvBin)
Beispiel #8
0
def printEventYields( **kwargs ) :
    # get arguments
    parSet     = kwargs.pop( 'ParameterSet',        [ ] )
    yieldNames = kwargs.pop( 'YieldNames',          [ ] )
    splitCats  = kwargs.pop( 'SplittingCategories', [ ] )

    assert parSet,     'P2VV - ERROR: printEventYields: no parameter set with yield variables found in arguments ("ParameterSet")'
    assert yieldNames, 'P2VV - ERROR: printEventYields: no yield names found in arguments ("YieldNames")'

    if splitCats :
        splitCats = sorted( list( set( cat for cat in splitCats ) ), cmp = lambda a, b : cmp( a.GetName(), b.GetName() ) )

    # variables for looping over splitting category states
    iters = { }
    inds  = { }
    labs  = { }
    for cat in splitCats :
        iters[cat] = 0
        inds[cat]  = [ ]
        labs[cat]  = [ ]
        catIter = cat.typeIterator()
        catState = catIter.Next()
        while catState :
            inds[cat].append( catState.getVal() )
            labs[cat].append( catState.GetName() )
            catState = catIter.Next()

    # print yields and fractions with error from S/(S+B) fraction only (no Poisson error for number of events!)
    print
    print '-'.join( dashes for dashes in [ ' ' * 4 + '-' * 26, '-' * 8 ] + [ '-' * num for name in yieldNames for num in ( 23, 19 ) ] )
    print ' '.join( ( '{0:^%ds}' % width ).format(title) for ( title, width ) in [ ( '', 30 ), ( ' total', 8 ) ]\
                   + [ ( name if num == 23 else 'f(' + name + ')', num ) for name in yieldNames for num in ( 23, 19 ) ] )
    print '-'.join( dashes for dashes in [ ' ' * 4 + '-' * 26, '-' * 8 ] + [ '-' * num for name in yieldNames for num in ( 23, 19 ) ] )

    from P2VV.Utilities.General import getSplitPar
    cont = True
    while cont :
        stateName = ';'.join( labs[cat][ iters[cat] ] for cat in splitCats )
        yields = [ getSplitPar( name, ( '{%s}' % stateName ) if stateName else '', parSet ) for name in yieldNames ]

        from math import sqrt
        nEv        = [ yieldVar.getVal()   for yieldVar in yields ]
        nEvErr     = [ yieldVar.getError() for yieldVar in yields ]
        nEvTot     = sum(nEv)
        frac       = [ num / nEvTot if nEvTot > 0. else 0. for num in nEv ]
        nEvCorrErr = [ sqrt( ( numErr**2 - num**2 / nEvTot ) if numErr**2 > num**2 / nEvTot else 0. ) for num, numErr in zip(nEv, nEvErr) ]
        fracErr    = [ err / nEvTot if nEvTot > 0. else 0. for err in nEvCorrErr ]

        print '     {0:>24s}   {1:>6.0f}  '.format( stateName, nEvTot )\
              + ' '.join( ' {0:>9.2f} +/- {1:>7.2f}   {2:>6.4f} +/- {3:>6.4f} '.format( num, err, fr, frErr )\
                         for ( num, err, fr, frErr ) in zip( nEv, nEvCorrErr, frac, fracErr ) )

        if not splitCats : break

        iters[ splitCats[-1] ] += 1
        for catIt in range( len(splitCats) ) :
            if iters[ splitCats[ -catIt - 1 ] ] >= splitCats[ -catIt - 1 ].numTypes() :
                if catIt == len(splitCats) - 1 :
                    cont = False
                else :
                    iters[ splitCats[ -catIt - 1 ] ] = 0
                    iters[ splitCats[ -catIt - 2 ] ] +=1
            else :
                continue

    print '-'.join( dashes for dashes in [ ' ' * 4 + '-' * 26, '-' * 8 ] + [ '-' * num for name in yieldNames for num in ( 23, 19 ) ] )
    print