def test_create_variable_scalar():
    ws = ROOT.RooWorkspace()
    r = countingworkspace.create_variables(ws, 'lumi', values=10.)
    lumi = ws.var('lumi')
    assert lumi
    assert lumi.isConstant()
    assert lumi.getVal() == 10.
    assert r.getVal() == 10.

    countingworkspace.create_variables(ws,
                                       'lumi2',
                                       values=11.,
                                       ranges=[1., 20.])
    lumi2 = ws.var('lumi2')
    assert lumi2
    assert not lumi2.isConstant()
    assert lumi2.getVal() == 11.
    assert lumi2.getMin() == 1.
    assert lumi2.getMax() == 20.

    countingworkspace.create_variables(ws, 'theta', values=0, ranges=(-5, 5))
    theta = ws.var('theta')
    assert theta
    assert not theta.isConstant()
    assert theta.getVal(0) == 0.
    assert theta.getMin() == -5.
    assert theta.getMax() == 5.
def test_create_variable_matrix():
    ws = ROOT.RooWorkspace()
    eff = np.arange(6).reshape(2, 3)
    r = countingworkspace.create_variables(ws,
                                           'myeff_cat{cat}_proc{proc}',
                                           values=eff,
                                           index_names=('cat', 'proc'))
    assert r

    for r1, v1 in zip(r, eff):
        for r2, v2 in zip(r1, v1):
            assert r2.getVal() == v2

    for y in range(2):
        for x in range(3):
            v = ws.var('myeff_cat{cat}_proc{proc}'.format(
                cat=format_index(y), proc=format_index(x)))
            assert v
            assert v.getVal() == eff[y][x]

    bins_proc = 'A', 'B', 'C'
    bins_cat = 'X', 'Y'
    countingworkspace.create_variables(ws,
                                       'myeff2_cat{cat}_proc{proc}',
                                       values=eff,
                                       bins=[bins_cat, bins_proc],
                                       index_names=('cat', 'proc'))
    for icat, cat in enumerate(bins_cat):
        for iproc, proc in enumerate(bins_proc):
            v = ws.var('myeff2_cat{cat}_proc{proc}'.format(cat=cat, proc=proc))
            assert v
            assert v.getVal() == eff[icat][iproc]
def test_create_expected_true():
    ws = ROOT.RooWorkspace()
    countingworkspace.create_variables(ws, 'lumi', values=10.)
    assert ws.var('lumi')
    NPROC = 4
    xsvalues = np.arange(1, NPROC + 1)
    countingworkspace.create_variables(ws,
                                       'xsec_{proc}',
                                       nbins=NPROC,
                                       values=xsvalues)
    assert ws.allVars().getSize() == NPROC + 1
def test_add():
    ws = ROOT.RooWorkspace()
    a = np.arange(10)
    b = np.arange(10) - 1.5
    countingworkspace.create_variables(ws, 'a_{index0}', nbins=10, values=a)
    countingworkspace.create_variables(ws, 'b_{index0}', nbins=10, values=b)
    countingworkspace.add(ws, 'a_{index0}', 'b_{index0}', nvar=10)
    for i, c in enumerate(a + b):
        assert ws.obj('a_plus_b_%s' % format_index(i)).getVal() == c

    countingworkspace.add(ws, 'a_{index0}', 'b_{index0}', 'd_{index0}')
    for i, c in enumerate(a + b):
        assert ws.obj('d_%s' % format_index(i)).getVal() == c
def test_create_variable_vector():
    ws = ROOT.RooWorkspace()
    values = [1., 3., 10.]

    r = countingworkspace.create_variables(ws,
                                           'foo_{myindex}',
                                           values=values,
                                           index_names='myindex')
    v = ws.allVars().selectByName('foo_*')
    assert (v.getSize() == len(values))
    assert (len(r) == len(values))
    for vv1, vv2, rr in zip(iter_collection(v), values, r):
        assert (vv1.getVal() == vv2)
        assert (rr.getVal() == vv2)

    countingworkspace.create_variables(ws, 'bar_{myindex2}', values=values)
    v = ws.allVars().selectByName('bar_*')
    assert (v.getSize() == len(values))
    for vv1, vv2 in zip(iter_collection(v), values):
        assert (vv1.getVal() == vv2)

    countingworkspace.create_variables(ws,
                                       'zoo_{myindex2}',
                                       values=values,
                                       bins=['one', 'two', 'three'])
    v = ws.allVars().selectByName('bar_*')
    assert (v.getSize() == len(values))
    for vv1, vv2 in zip(iter_collection(v), values):
        assert (vv1.getVal() == vv2)
    assert (ws.var('zoo_one').getVal() == values[0])
    assert (ws.var('zoo_two').getVal() == values[1])
    assert (ws.var('zoo_three').getVal() == values[2])

    bins = list(map(str, range(len(values))))
    countingworkspace.create_variables(ws,
                                       'a_{proc}',
                                       bins=bins,
                                       values=values,
                                       ranges=(-10000, 50000))
    for b, v in zip(bins, values):
        a = ws.var('a_%s' % b)
        assert a
        np.testing.assert_allclose(a.getVal(), v)
        np.testing.assert_allclose(a.getMin(), -10000)
        np.testing.assert_allclose(a.getMax(), 50000)

    countingworkspace.create_variables(ws,
                                       'x_{proc}',
                                       bins=[bins],
                                       nbins=(len(bins), ),
                                       values=values)
    for b, v in zip(bins, values):
        x = ws.var('x_%s' % b)
        assert x
        np.testing.assert_allclose(x.getVal(), v)
def test_create_formula():
    ws = ROOT.RooWorkspace()
    countingworkspace.create_variables(ws, 'a', values=10.)
    assert ws.var('a').getVal() == 10.
    countingworkspace.create_variables(ws, 'theta', values=0., ranges=(-5, 5))
    assert ws.var('theta').getVal() == 0.
    assert ws.var('theta').getMin() == -5.
    assert ws.var('theta').getMax() == 5.
    countingworkspace.create_variables(ws, 'prod:X(a, b[20])')
    assert ws.var('b').getVal() == 20.
    assert ws.obj('X').getVal() == 10. * 20.

    NPROC = 4
    xsvalues = np.arange(1, NPROC + 1)
    countingworkspace.create_variables(ws,
                                       'xsec_{proc}',
                                       nbins=NPROC,
                                       values=xsvalues)
    countingworkspace.create_variables(
        ws, 'prod:ntrue_{proc}(lumi[100], xsec_{proc})', nbins=NPROC)
    for i, xs in enumerate(xsvalues):
        assert (ws.obj('ntrue_%s' % format_index(i)) == 100 * (i + 1))
Exemple #7
0
### arguments
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--fit', action='store', required=True, help='What to fit (kappa or mu)')
parser.add_argument('-s', '--selection', action='store', required=True, help='Selection to be used')
args = parser.parse_args()


########
# ATLAS Run 2 Luminosity
lumi = (36104.16 + 43593.8 + 58450.1) / 1000.0
combWS = ROOT.RooWorkspace("combWS","combWS")
combWS.factory('lumi[%f]' % lumi)

## add cross sections into workspace
ntrue = create_variables(combWS, 'xsec_{proc}',bins=SAMPLES,values=xsec_production_modes)

if args.selection == "nominal":

    create_workspace(NCATEGORIES, SAMPLES, efficiencies=EFFICIENCIES, nexpected_bkg_cat = EXPECTED_BKG_CAT,expression_nsignal_gen='prod:nsignal_gen_proc{proc}(mu_{proc}[1, -4, 5], lumi, xsec_{proc})',ws=combWS)

if args.selection == "new":

    create_workspace(newNCATEGORIES, SAMPLES, efficiencies=newEFFICIENCIES, nexpected_bkg_cat = newEXPECTED_BKG_CAT,expression_nsignal_gen='prod:nsignal_gen_proc{proc}(mu_{proc}[1, -4, 5], lumi, xsec_{proc})',ws=combWS)

combWS.set('all_exp').Print('V')

## set all the POIs to constant with the exception of ttH

for sample in SAMPLES:
    if sample != "ttH":
def test_create_workspace_luminosity():

    # workspace where nsignal_gen[p] = xsec[p] * lumi
    ws_with_lumi = ROOT.RooWorkspace()
    ws_with_lumi.factory('lumi[%f]' % LUMI)
    countingworkspace.create_variables(ws_with_lumi,
                                       'xsec_{proc}',
                                       nbins=NPROCESS,
                                       values=XSECFID_X_BR_PRODUCTION_MODES,
                                       ranges=[-1000, 10000])

    create_workspace(
        NCATEGORIES,
        NPROCESS,
        None,
        EFFICIENCIES,
        EXPECTED_BKG_CAT,
        expression_nsignal_gen='prod:nsignal_gen_proc{proc}(lumi, xsec_{proc})',
        ws=ws_with_lumi)

    # workspace where nsignal_gen[p] = mu[p] * xsec[p] * lumi
    ws_with_4mu = ROOT.RooWorkspace()
    ws_with_4mu.factory('lumi[%f]' % LUMI)
    countingworkspace.create_variables(ws_with_4mu,
                                       'xsec_{proc}',
                                       nbins=NPROCESS,
                                       values=XSECFID_X_BR_PRODUCTION_MODES)
    create_workspace(
        NCATEGORIES,
        NPROCESS,
        None,
        EFFICIENCIES,
        EXPECTED_BKG_CAT,
        expression_nsignal_gen=
        'prod:nsignal_gen_proc{proc}(mu_{proc}[1, -4, 5], lumi, xsec_{proc})',
        ws=ws_with_4mu)

    # workspace where nsignal_gen[p] = mu * mu[p] * xsec[p] * lumi
    # where true yield is created externally
    ws_with_4mu_x_mu = ROOT.RooWorkspace()
    ws_with_4mu_x_mu.factory('lumi[%f]' % LUMI)
    countingworkspace.create_variables(ws_with_4mu_x_mu,
                                       'xsec_{proc}',
                                       nbins=NPROCESS,
                                       values=XSECFID_X_BR_PRODUCTION_MODES)
    countingworkspace.create_variables(
        ws_with_4mu_x_mu,
        'prod:nsignal_gen_proc{proc}(mu[1, -4, 5], mu_{proc}[1, -4, 5], lumi, xsec_{proc})',
        nbins=NPROCESS)

    create_workspace(NCATEGORIES,
                     NPROCESS,
                     None,
                     EFFICIENCIES,
                     EXPECTED_BKG_CAT,
                     expression_nsignal_gen='nsignal_gen_proc{proc}',
                     ws=ws_with_4mu_x_mu)

    # same, but with names
    ws_with_4mu_x_mu_names = ROOT.RooWorkspace()
    ws_with_4mu_x_mu_names.factory('lumi[%f]' % LUMI)
    countingworkspace.create_variables(ws_with_4mu_x_mu_names,
                                       'xsec_{proc}',
                                       bins=list(
                                           map(format_index, range(NPROCESS))),
                                       values=XSECFID_X_BR_PRODUCTION_MODES)
    countingworkspace.create_variables(
        ws_with_4mu_x_mu_names,
        'prod:nsignal_gen_proc{proc}(mu[1, -4, 5], mu_{proc}[1, -4, 5], lumi, xsec_{proc})',
        bins=list(map(format_index, range(NPROCESS))))

    create_workspace(list(map(format_index, range(NCATEGORIES))),
                     list(map(format_index, range(NPROCESS))),
                     None,
                     EFFICIENCIES,
                     EXPECTED_BKG_CAT,
                     expression_nsignal_gen='nsignal_gen_proc{proc}',
                     ws=ws_with_4mu_x_mu_names)

    # nominal workspace for reference
    ws = create_workspace(NCATEGORIES, NPROCESS, NTRUE, EFFICIENCIES,
                          EXPECTED_BKG_CAT)

    all_vars = ws.allVars()
    for v in iter_collection(all_vars):
        v_lumi = ws_with_lumi.obj(v.GetName())
        assert v_lumi
        np.testing.assert_allclose(v.getVal(), v_lumi.getVal())

        v_4mu = ws_with_4mu.obj(v.GetName())
        assert v_4mu
        np.testing.assert_allclose(v.getVal(), v_4mu.getVal())

        v_4mu_x_mu = ws_with_4mu_x_mu.obj(v.GetName())
        assert v_4mu_x_mu
        np.testing.assert_allclose(v.getVal(), v_4mu_x_mu.getVal())

        v_4mu_x_mu_names = ws_with_4mu_x_mu_names.obj(v.GetName())
        assert v_4mu_x_mu_names
        np.testing.assert_allclose(v.getVal(), v_4mu_x_mu_names.getVal())

    all_f = ws.allFunctions()
    for f in iter_collection(all_f):
        f_lumi = ws_with_lumi.obj(f.GetName())
        assert f_lumi
        np.testing.assert_allclose(f.getVal(), f_lumi.getVal())

        f_4mu = ws_with_4mu.obj(f.GetName())
        assert f_4mu
        np.testing.assert_allclose(f.getVal(), f_4mu.getVal())

        f_4mu_x_mu = ws_with_4mu_x_mu.obj(f.GetName())
        assert f_4mu_x_mu
        np.testing.assert_allclose(f.getVal(), f_4mu_x_mu.getVal())

        f_4mu_x_mu_names = ws_with_4mu_x_mu_names.obj(f.GetName())
        assert f_4mu_x_mu_names
        np.testing.assert_allclose(f.getVal(), f_4mu_x_mu_names.getVal())

    all_pdf = ws.allPdfs()
    for p in iter_collection(all_pdf):
        p_lumi = ws_with_lumi.pdf(p.GetName())
        assert p_lumi
        np.testing.assert_allclose(p.getVal(), p_lumi.getVal())

        p_4mu = ws_with_4mu.pdf(p.GetName())
        assert p_4mu
        np.testing.assert_allclose(p.getVal(), p_4mu.getVal())

        p_4mu_x_mu = ws_with_4mu.pdf(p.GetName())
        assert p_4mu_x_mu
        np.testing.assert_allclose(p.getVal(), p_4mu_x_mu.getVal())

        p_4mu_x_mu_names = ws_with_4mu_x_mu_names.pdf(p.GetName())
        assert p_4mu_x_mu_names
        np.testing.assert_allclose(p.getVal(), p_4mu_x_mu_names.getVal())

    assert countingworkspace.utils.get_free_variables(ws).getSize() == \
           countingworkspace.utils.get_free_variables(ws_with_lumi).getSize() == \
           countingworkspace.utils.get_free_variables(ws_with_4mu).getSize() == \
           countingworkspace.utils.get_free_variables(ws_with_4mu_x_mu).getSize() - 1 == \
           countingworkspace.utils.get_free_variables(ws_with_4mu_x_mu_names).getSize() - 1 == \
           NPROCESS