Exemple #1
0
def test_random_sample():
    funcs = [
        TF1("f1", "TMath::DiLog(x)"),
        TF2("f2", "sin(x)*sin(y)/(x*y)"),
        TF3("f3", "sin(x)*sin(y)*sin(z)/(x*y*z)"),
    ]
    hists = [
        TH1D("h1", "h1", 10, -3, 3),
        TH2D("h2", "h2", 10, -3, 3, 10, -3, 3),
        TH3D("h3", "h3", 10, -3, 3, 10, -3, 3, 10, -3, 3),
    ]
    for i, hist in enumerate(hists):
        hist.FillRandom(funcs[i].GetName())
    for obj in funcs + hists:
        yield check_random_sample, obj
Exemple #2
0
def get_fit_fcn(histo):
    fit_fcn = TF3(
        "expo3",
        "[0]*exp(-[1] - [2]*x - [3]*y - [4]*z)",
        histo.GetXaxis().GetXmin(),
        histo.GetXaxis().GetXmax(),
        histo.GetYaxis().GetXmin(),
        histo.GetYaxis().GetXmax(),
        histo.GetZaxis().GetXmin(),
        histo.GetZaxis().GetXmax(),
    )
    fit_fcn.SetNpx(histo.GetNbinsX())
    fit_fcn.SetNpy(histo.GetNbinsY())
    fit_fcn.SetNpz(histo.GetNbinsZ())

    fit_fcn.SetParameter(0, 1000)
    fit_fcn.SetParameter(1, 1)
    fit_fcn.SetParameter(2, 0.2)
    fit_fcn.SetParameter(3, 0.2)
    fit_fcn.SetParameter(4, 0.2)

    return fit_fcn
Exemple #3
0
                sigObj.Draw(
                    par3Name + ':' + par2Name + ':' + par1Name +
                    ' >> theBaseData_' + section + '_' + str(i),
                    weight.GetName() + '*(' + cfg.get(section, 'obsVar') +  #
                    ' > ' + str(bins[len(bins) - 2]) + ')',
                    'goff')

        if (model == "par1_TH1" or model == "par1_TF1"):
            func = TF1('bin_content_par1_' + str(i), func_string,
                       2 * par1GridMin, 2 * par1GridMax)
        elif (model == "par1par2_TH2" or model == "par1par2_TF2"):
            func = TF2('bin_content_par1_par2_' + str(i), func_string,
                       par1GridMin, par1GridMax, par2GridMin, par2GridMax)
        elif (model == "par1par2par3_TH3" or model == "par1par2par3_TF3"):
            func = TF3('bin_content_par1_par2_par3_' + str(i), func_string,
                       par1GridMin, par1GridMax, par2GridMin, par2GridMax,
                       par3GridMin, par3GridMax)

        print range(nGridPar1Bins)
        for k in range(nGridPar1Bins + 1):
            #            theBaseData.SetBinError(k,0.0000021);
            print "bin" + str(k) + ": ", theBaseData.GetBinContent(
                k), " +- ", theBaseData.GetBinError(k)

        func.SetLineColor(2)
        r = theBaseData.Fit(func, 'R0S', '')

        print "chi2: ", r.Chi2()
        print "par0: ", r.Parameter(0), "+-", r.ParError(
            0), " rel: ", r.ParError(0) / r.Parameter(0)
        print "par1: ", r.Parameter(1), "+-", r.ParError(
Exemple #4
0
def test_evaluate():
    # create functions and histograms
    f1 = TF1("f1", "x")
    f2 = TF2("f2", "x*y")
    f3 = TF3("f3", "x*y*z")
    h1 = TH1D("h1", "", 10, 0, 1)
    h1.FillRandom("f1")
    h2 = TH2D("h2", "", 10, 0, 1, 10, 0, 1)
    h2.FillRandom("f2")
    h3 = TH3D("h3", "", 10, 0, 1, 10, 0, 1, 10, 0, 1)
    h3.FillRandom("f3")
    # generate random arrays
    arr_1d = np.random.rand(5)
    arr_2d = np.random.rand(5, 2)
    arr_3d = np.random.rand(5, 3)
    arr_4d = np.random.rand(5, 4)
    # evaluate the functions
    assert_array_equal(rnp.evaluate(f1, arr_1d), map(f1.Eval, arr_1d))
    assert_array_equal(rnp.evaluate(f1.GetTitle(), arr_1d),
                       map(f1.Eval, arr_1d))
    assert_array_equal(rnp.evaluate(f2, arr_2d),
                       [f2.Eval(*x) for x in arr_2d])
    assert_array_equal(rnp.evaluate(f2.GetTitle(), arr_2d),
                       [f2.Eval(*x) for x in arr_2d])
    assert_array_equal(rnp.evaluate(f3, arr_3d),
                       [f3.Eval(*x) for x in arr_3d])
    assert_array_equal(rnp.evaluate(f3.GetTitle(), arr_3d),
                       [f3.Eval(*x) for x in arr_3d])
    # 4d formula
    f4 = TFormula('test', 'x*y+z*t')
    assert_array_equal(rnp.evaluate(f4, arr_4d),
                       [f4.Eval(*x) for x in arr_4d])
    # evaluate the histograms
    assert_array_equal(rnp.evaluate(h1, arr_1d),
                       [h1.GetBinContent(h1.FindBin(x)) for x in arr_1d])
    assert_array_equal(rnp.evaluate(h2, arr_2d),
                       [h2.GetBinContent(h2.FindBin(*x)) for x in arr_2d])
    assert_array_equal(rnp.evaluate(h3, arr_3d),
                       [h3.GetBinContent(h3.FindBin(*x)) for x in arr_3d])
    # create a graph
    g = TGraph(2)
    g.SetPoint(0, 0, 1)
    g.SetPoint(1, 1, 2)
    assert_array_equal(rnp.evaluate(g, [0, .5, 1]), [1, 1.5, 2])
    from ROOT import TSpline3
    s = TSpline3("spline", g)
    assert_array_equal(rnp.evaluate(s, [0, .5, 1]), map(s.Eval, [0, .5, 1]))
    # test exceptions
    assert_raises(TypeError, rnp.evaluate, object(), [1, 2, 3])
    assert_raises(ValueError, rnp.evaluate, h1, arr_2d)
    assert_raises(ValueError, rnp.evaluate, h2, arr_3d)
    assert_raises(ValueError, rnp.evaluate, h2, arr_1d)
    assert_raises(ValueError, rnp.evaluate, h3, arr_1d)
    assert_raises(ValueError, rnp.evaluate, h3, arr_2d)
    assert_raises(ValueError, rnp.evaluate, f1, arr_2d)
    assert_raises(ValueError, rnp.evaluate, f2, arr_3d)
    assert_raises(ValueError, rnp.evaluate, f2, arr_1d)
    assert_raises(ValueError, rnp.evaluate, f3, arr_1d)
    assert_raises(ValueError, rnp.evaluate, f3, arr_2d)
    assert_raises(ValueError, rnp.evaluate, g, arr_2d)
    assert_raises(ValueError, rnp.evaluate, s, arr_2d)
    assert_raises(ValueError, rnp.evaluate, "f", arr_1d)
    assert_raises(ValueError, rnp.evaluate, "x*y", arr_1d)
    assert_raises(ValueError, rnp.evaluate, "x", arr_2d)
    assert_raises(ValueError, rnp.evaluate, "x*y", arr_3d)
Exemple #5
0
def test_random_sample_f3():
    func = TF3("f3", "sin(x)*sin(y)*sin(z)/(x*y*z)")
    sample = rnp.random_sample(func, 100)
    assert_equal(sample.shape, (100, 3))
Exemple #6
0
x = array.array('d', [-zActiveDipoleLengh / 2, -zActiveDipoleLengh / 2])
y = array.array('d', [0, strength])
n = len(x)
Rz = TPolyLine(n, x, y)
Rz.SetLineColor(ROOT.kBlack)
x = array.array('d', [+zActiveDipoleLengh / 2, +zActiveDipoleLengh / 2])
y = array.array('d', [0, strength])
n = len(x)
Lz = TPolyLine(n, x, y)
Lz.SetLineColor(ROOT.kBlack)

s3func = '[0] * ((2-ROOT::Math::erfc(([1]/2+x)/[2]))/2)*((2-ROOT::Math::erfc(([1]/2-x)/[2]))/2)\
              * ((2-ROOT::Math::erfc(([3]/2+y)/[4]))/2)*((2-ROOT::Math::erfc(([3]/2-y)/[4]))/2)\
              * ((2-ROOT::Math::erfc(([5]/2+z)/[6]))/2)*((2-ROOT::Math::erfc(([5]/2-z)/[6]))/2)'

fieldxyz = TF3("fieldxyz", s3func, -25, +25, -10, +10, -80, +80)

s2func = '[0] * ((2-ROOT::Math::erfc(([1]/2+x)/[2]))/2) * ((2-ROOT::Math::erfc(([1]/2-x)/[2]))/2)\
              * ((2-ROOT::Math::erfc(([3]/2+y)/[4]))/2) * ((2-ROOT::Math::erfc(([3]/2-y)/[4]))/2)'

fieldxy = TF2("fieldxy", s2func, -25, +25, -10, +10)
fieldxz = TF2("fieldxz", s2func, -25, +25, -80, +80)
fieldyz = TF2("fieldyz", s2func, -10, +10, -80, +80)

s1func = '[0] * ((2-ROOT::Math::erfc(([1]/2+x)/[2]))/2) * ((2-ROOT::Math::erfc(([1]/2-x)/[2]))/2)'
fieldx = TF1("fieldx", s1func, -25, +25)
fieldy = TF1("fieldy", s1func, -10, +10)
fieldz = TF1("fieldz", s1func, -80, +80)

fieldxy.SetTitle("B-field x:y")
fieldxz.SetTitle("B-field x:z")