利用Jupyter完成RooFit
时间:2021-4-24 19:11 作者:JourinTown 分类: 计算机技术
我們先把一些必要的包引用進來,事實上第二步不用那麼複雜,這裡不過是把常用的包引用進來了,你之後總還是可以用 ROOT.xxx來使用對應的函數
import ROOT from ROOT import RooRealVar, RooFormulaVar, RooVoigtian, RooChebychev, RooArgList, \ RooArgSet, RooAddPdf, RooDataSet, RooCategory, RooSimultaneous, \ RooBreitWigner, RooCBShape, RooFFTConvPdf, RooGaussian
然後我們就可以Fit了,這裡是一個案例
tree = ROOT.TChain("mphi") tree.Add(args.path) x_min = 0.98 x_max = 1.06 n_bins = 100 mphi=RooRealVar("mphi","",x_min,x_max) mean1=RooRealVar("mean1", "", 1.020, 1, 1.03) sigma1=RooRealVar("sigma1", "", 1.94489e-03, 1.5e-03,0.01) mean2=RooRealVar("mean2", "", 1.019, 1, 1.020) sigma2=RooRealVar("sigma2", "", 1.94489e-03, 1.5e-03,3.0) gaus1=RooGaussian("gaus1", "gaus1", mphi, mean1, sigma1) gaus2=RooGaussian("gaus2", "gaus2", mphi, mean2, sigma2) c2=RooRealVar("c2","coeffiicient #2",-0.1,-30000,30000) bkg=RooChebychev("bkg","background p.d.f",mphi,RooArgList(c2)) Nsig1=RooRealVar("Nsig1", "", 1000, 0, 20000) Nsig2=RooRealVar("Nsig2", "", 1000, 0, 20000) Nbg=RooRealVar("Nbg", "", 200,0,2000) model=RooAddPdf("model", "model", RooArgList(gaus1, gaus2, bkg), RooArgList(Nsig1,Nsig2, Nbg)) dataset_data = RooDataSet("dataset_data", "data",tree, mphi) fit_res_all = model.fitTo(dataset_data, ROOT.RooFit.Extended(ROOT.kTRUE)) canvas = ROOT.TCanvas() frame = mphi.frame(ROOT.RooFit.Bins(n_bins),ROOT.RooFit.Title("unbin maximum likehood Fit")) dataset_data.plotOn(frame, ROOT.RooFit.Binning(n_bins), ROOT.RooFit.LineColor(1), ROOT.RooFit.Name("data")); model.plotOn(frame, ROOT.RooFit.LineColor(9), ROOT.RooFit.Name("model")); model.plotOn(frame, ROOT.RooFit.Components("gaus1"), ROOT.RooFit.LineStyle(2),ROOT.RooFit.LineColor(2)); model.plotOn(frame, ROOT.RooFit.Components("gaus2"), ROOT.RooFit.LineStyle(2),ROOT.RooFit.LineColor(3)); model.plotOn(frame, ROOT.RooFit.Components("bkg"), ROOT.RooFit.LineStyle(2),ROOT.RooFit.LineColor(1)); frame.GetXaxis().SetTitle("M_{#phi}(GeV/c^{2})"); frame.GetYaxis().SetTitle("Event"); frame.GetXaxis().CenterTitle() frame.GetYaxis().CenterTitle() frame.Draw() lt=ROOT.TLatex() lt.SetNDC() lt.SetTextAngle(0) lt.SetTextSize(0.04) NSig1text="Nsig1="+str(int(Nsig1.getVal()))+" #pm "+str(int(Nsig1.getError())) NSig2text="Nsig2="+str(int(Nsig2.getVal()))+" #pm "+str(int(Nsig2.getError())) Nbkgtext="Nbkg="+str(int(Nbg.getVal()))+" #pm "+str(int(Nbg.getError())) lt.DrawLatex(0.6, 0.70, "#phi #rightarrow K^{+} K^{-}") lt.DrawLatex(0.6, 0.65, NSig1text) lt.DrawLatex(0.6, 0.60, NSig2text) lt.DrawLatex(0.6, 0.55, Nbkgtext) canvas.Draw() canvas.Print(args.o)
你也來試試吧!
具體也可以查看這一處的案例https://cc.benhaotang.cn/share/b9146c1b60c7c3ce9cd51b06d426785e1843d556/Fit.ipynb?viewer=share
评论: