Exemplo n.º 1
0
def rooFit103():

    print ">>> construct generic pdf from interpreted expression..."
    # To construct a proper p.d.f, the formula expression is explicitly normalized internally
    # by dividing  it by a numeric integral of the expresssion over x in the range [-20,20]
    x = RooRealVar("x", "x", -20, 20)
    alpha = RooRealVar("alpha", "alpha", 5, 0.1, 10)
    genpdf = RooGenericPdf("genpdf", "genpdf",
                           "(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))",
                           RooArgList(x, alpha))

    print ">>> generate and fit toy data...\n"
    data = genpdf.generate(RooArgSet(x), 10000)  # RooDataSet
    genpdf.fitTo(data)
    frame1 = x.frame(Title("Interpreted expression pdf"))  # RooPlot
    data.plotOn(frame1, Binning(40))
    genpdf.plotOn(frame1)

    print "\n>>> construct standard pdf with formula replacing parameter..."
    mean2 = RooRealVar("mean2", "mean^2", 10, 0, 200)
    sigma = RooRealVar("sigma", "sigma", 3, 0.1, 10)
    mean = RooFormulaVar("mean", "mean", "sqrt(mean2)", RooArgList(mean2))
    gaus2 = RooGaussian("gaus2", "gaus2", x, mean, sigma)

    print ">>> generate and fit toy data...\n"
    gaus1 = RooGaussian("gaus1", "gaus1", x, RooConst(10), RooConst(3))
    data2 = gaus1.generate(RooArgSet(x), 1000)  # RooDataSet
    result = gaus2.fitTo(data2, Save())  # RooFitResult
    result.Print()
    frame2 = x.frame(Title("Tailored Gaussian pdf"))  # RooPlot
    data2.plotOn(frame2, Binning(40))
    gaus2.plotOn(frame2)

    print "\n>>> draw pfds and fits on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 1400, 600)
    canvas.Divide(2)
    canvas.cd(1)
    gPad.SetLeftMargin(0.15)
    gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.6)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    canvas.cd(2)
    gPad.SetLeftMargin(0.15)
    gPad.SetRightMargin(0.02)
    frame2.GetYaxis().SetLabelOffset(0.008)
    frame2.GetYaxis().SetTitleOffset(1.6)
    frame2.GetYaxis().SetTitleSize(0.045)
    frame2.GetXaxis().SetTitleSize(0.045)
    frame2.Draw()
    canvas.SaveAs("rooFit103.png")
Exemplo n.º 2
0
# Second Gaussian PDF
mean2 = RooRealVar("mean2", "Mean of Gaussian 2", 1.02052, 1.0, 1.1)
sigma2 = RooRealVar("sigma2", "Width of Gaussian 2", 0.0072, 0.001, 1)

gauss2 = RooGaussian("gauss2", "gauss1(Mkk,mean2,sigma2)", Mkk, mean2, sigma2)
r2 = RooRealVar("r2", "ratio of width of gaussian 2", 3.2, 0, 10)

customGaussR = RooGenericPdf(
    "customGaussR", "custom PDF build",
    "1/(sigma1*r2) * exp(- (Mkk - mean2)**2 / (2 * (sigma1*r2) ** 2))",
    RooArgList(Mkk, mean2, sigma1, r2))

# faster method:
r_sigma2 = RooFormulaVar("r_sigma2", "sigma1 * r2", RooArgList(sigma1, r2))
gauss2R = RooGaussian("gauss2R", "gauss1(Mkk,mean2,sigma2)", Mkk, mean2,
                      r_sigma2)

toyDataCustom = customGaussR.generate(RooArgSet(Mkk), 100000)
toyData = gauss2.generate(RooArgSet(Mkk), 100000)
toyDataR = gauss2R.generate(RooArgSet(Mkk), 100000)

c = TCanvas("c", "c", 800, 800)
frame1 = Mkk.frame(RooFit.Bins(50), RooFit.Title("Comparison of PDFs"))

# Plot all data tagged as passed sample
toyData.plotOn(frame1)
toyDataR.plotOn(frame1)
toyDataCustom.plotOn(frame1)
#totalPdf.paramOn(frame1, RooFit.Layout(0.55, 0.9, 0.9), RooFit.Format("NEU", RooFit.AutoPrecision(1)))
frame1.Draw()