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")
#background_norm_olderr = background_norm.getError() #background_norm_newerr = background_norm_olderr * LUMI / lumi_in #background_norm.setVal(background_norm_newval) #background_norm.setError(background_norm_newerr) # ----------------------------------------- # plot background canBname = 'can_Mjj_Data' canB = TCanvas(canBname, canBname, 900, 600) gPad.SetLogy() canB.cd(1).SetBottomMargin(0.4) frame1 = x.frame() frame2 = x.frame() roohistBkg.plotOn(frame1, RooFit.Binning(NBINS)) background.plotOn(frame1) hpull = frame1.pullHist() frame2.addPlotable(hpull, 'p') frame1.SetMinimum(0.5) frame1.GetXaxis().SetTitle('') frame1.GetXaxis().SetLabelSize(0.0) frame1.GetYaxis().SetTickLength(0.06) frame1.Draw() pad = TPad('pad', 'pad', 0., 0., 1., 1.) pad.SetTopMargin(0.6) pad.SetFillColor(0) pad.SetFillStyle(0) pad.Draw() pad.cd(0)
def pdf_logPt2_prelim(): #PDF fit to log_10(pT^2) for preliminary figure #tree_in = tree_incoh tree_in = tree #ptbin = 0.04 ptbin = 0.12 ptmin = -5. ptmax = 1. mmin = 2.8 mmax = 3.2 #fitran = [-5., 1.] fitran = [-0.9, 0.1] binned = False #gamma-gamma 131 evt for pT<0.18 #input data pT = RooRealVar("jRecPt", "pT", 0, 10) m = RooRealVar("jRecM", "mass", 0, 10) dataIN = RooDataSet("data", "data", tree_in, RooArgSet(pT, m)) strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax) data = dataIN.reduce(strsel) #x is RooRealVar for log(Pt2) draw = "TMath::Log10(jRecPt*jRecPt)" draw_func = RooFormulaVar( "x", "Dielectron log_{10}( #it{p}_{T}^{2} ) ((GeV/c)^{2})", draw, RooArgList(pT)) x = data.addColumn(draw_func) x.setRange("fitran", fitran[0], fitran[1]) #binned data nbins, ptmax = ut.get_nbins(ptbin, ptmin, ptmax) hPt = TH1D("hPt", "hPt", nbins, ptmin, ptmax) hPtCoh = ut.prepare_TH1D("hPtCoh", ptbin, ptmin, ptmax) hPtCoh.SetLineWidth(2) #fill in binned data tree_in.Draw(draw + " >> hPt", strsel) tree_coh.Draw(draw + " >> hPtCoh", strsel) dataH = RooDataHist("dataH", "dataH", RooArgList(x), hPt) #range for plot x.setMin(ptmin) x.setMax(ptmax) x.setRange("plotran", ptmin, ptmax) #create the pdf b = RooRealVar("b", "b", 5., 0., 10.) pdf_func = "log(10.)*pow(10.,x)*exp(-b*pow(10.,x))" pdf_logPt2 = RooGenericPdf("pdf_logPt2", pdf_func, RooArgList(x, b)) #make the fit if binned == True: r1 = pdf_logPt2.fitTo(dataH, rf.Range("fitran"), rf.Save()) else: r1 = pdf_logPt2.fitTo(data, rf.Range("fitran"), rf.Save()) #calculate norm to number of events xset = RooArgSet(x) ipdf = pdf_logPt2.createIntegral(xset, rf.NormSet(xset), rf.Range("fitran")) print "PDF integral:", ipdf.getVal() if binned == True: nevt = tree_incoh.Draw( "", strsel + " && " + draw + ">{0:.3f}".format(fitran[0]) + " && " + draw + "<{1:.3f}".format(fitran[0], fitran[1])) else: nevt = data.sumEntries("x", "fitran") print "nevt:", nevt pdf_logPt2.setNormRange("fitran") print "PDF norm:", pdf_logPt2.getNorm(RooArgSet(x)) #a = nevt/ipdf.getVal() a = nevt / pdf_logPt2.getNorm(RooArgSet(x)) print "a =", a #gamma-gamma contribution hPtGG = ut.prepare_TH1D("hPtGG", ptbin, ptmin, ptmax) tree_gg.Draw(draw + " >> hPtGG", strsel) #ut.norm_to_data(hPtGG, hPt, rt.kGreen, -5., -2.9) ut.norm_to_num(hPtGG, 131., rt.kGreen + 1) print "Int GG:", hPtGG.Integral() #sum of all contributions hSum = ut.prepare_TH1D("hSum", ptbin, ptmin, ptmax) hSum.SetLineWidth(3) #add ggel to the sum hSum.Add(hPtGG) #add incoherent contribution func_logPt2 = TF1("pdf_logPt2", "[0]*log(10.)*pow(10.,x)*exp(-[1]*pow(10.,x))", -10., 10.) func_logPt2.SetParameters(a, b.getVal()) hInc = ut.prepare_TH1D("hInc", ptbin, ptmin, ptmax) ut.fill_h1_tf(hInc, func_logPt2) hSum.Add(hInc) #add coherent contribution ut.norm_to_data(hPtCoh, hPt, rt.kBlue, -5., -2.2) # norm for coh hSum.Add(hPtCoh) #set to draw as a lines ut.line_h1(hSum, rt.kBlack) #create canvas frame can = ut.box_canvas() ut.set_margin_lbtr(gPad, 0.11, 0.1, 0.01, 0.01) frame = x.frame(rf.Bins(nbins), rf.Title("")) frame.SetTitle("") frame.SetYTitle("J/#psi candidates / ({0:.3f}".format(ptbin) + " (GeV/c)^{2})") frame.GetXaxis().SetTitleOffset(1.2) frame.GetYaxis().SetTitleOffset(1.6) print "Int data:", hPt.Integral() #plot the data if binned == True: dataH.plotOn(frame, rf.Name("data")) else: data.plotOn(frame, rf.Name("data")) pdf_logPt2.plotOn(frame, rf.Range("fitran"), rf.LineColor(rt.kRed), rf.Name("pdf_logPt2")) pdf_logPt2.plotOn(frame, rf.Range("plotran"), rf.LineColor(rt.kRed), rf.Name("pdf_logPt2_full"), rf.LineStyle(rt.kDashed)) frame.Draw() leg = ut.prepare_leg(0.61, 0.77, 0.16, 0.19, 0.03) #ut.add_leg_mass(leg, mmin, mmax) hx = ut.prepare_TH1D("hx", 1, 0, 1) hx.Draw("same") ln = ut.col_lin(rt.kRed, 2) leg.AddEntry(hx, "Data", "p") leg.AddEntry(hSum, "Sum", "l") leg.AddEntry(hPtCoh, "Coherent J/#psi", "l") leg.AddEntry(ln, "Incoherent parametrization", "l") leg.AddEntry(hPtGG, "#gamma#gamma#rightarrow e^{+}e^{-}", "l") #leg.AddEntry(ln, "ln(10)*#it{A}*10^{log_{10}#it{p}_{T}^{2}}exp(-#it{b}10^{log_{10}#it{p}_{T}^{2}})", "l") leg.Draw("same") l0 = ut.cut_line(fitran[0], 0.9, frame) l1 = ut.cut_line(fitran[1], 0.9, frame) #l0.Draw() #l1.Draw() pleg = ut.prepare_leg(0.12, 0.75, 0.14, 0.22, 0.03) pleg.AddEntry(None, "#bf{|#kern[0.3]{#it{y}}| < 1}", "") ut.add_leg_mass(pleg, mmin, mmax) pleg.AddEntry(None, "STAR Preliminary", "") pleg.AddEntry(None, "AuAu@200 GeV", "") pleg.AddEntry(None, "UPC sample", "") pleg.Draw("same") desc = pdesc(frame, 0.14, 0.9, 0.057) desc.set_text_size(0.03) desc.itemD("#chi^{2}/ndf", frame.chiSquare("pdf_logPt2", "data", 2), -1, rt.kRed) desc.itemD("#it{A}", a, -1, rt.kRed) desc.itemR("#it{b}", b, rt.kRed) #desc.draw() #put the sum hSum.Draw("same") frame.Draw("same") #put gamma-gamma and coherent J/psi hPtGG.Draw("same") hPtCoh.Draw("same") #ut.invert_col(rt.gPad) can.SaveAs("01fig.pdf")
#pdf = RooNumConvPdf("pdf", 'convolution', x, p1, p2) #x.setBins(10000,"fft") ; #pdf = RooFFTConvPdf("pdf", 'convolution', x, p1, p2) #pdf.setBufferFraction(50.0) #pdf = RooGenericPdf("pdf","abs(x)>m0?exp(-abs(x)/tau)+exp(-m0/tau2)-exp(-m0/tau):exp(-abs(x)/tau2)",RooArgList(x,m0,tau,tau2)) ; #pdf = RooGenericPdf("pdf","abs(x)>m0?exp(-abs(x)/tau2):exp(-abs(x)/tau)+exp(-m0/tau2)-exp(-m0/tau)",RooArgList(x,m0,tau,tau2)) ; pdf = RooGenericPdf("pdf","exp(-abs(x)/tau2)/tau2>exp(-abs(x)/tau)/tau?exp(-abs(x)/tau2)/tau2:exp(-abs(x)/tau)/tau",RooArgList(x,tau,tau2)) ; #pdf = RooGenericPdf("pdf","exp(-abs(x)/tau)+1000",RooArgList(x,m0,tau,tau2)) ; # Plot PDF canvas = TCanvas("c1","",1200,480); canvas.Divide(3,1); canvas.cd(1) xframe = x.frame() p1.plotOn( xframe ) xframe.Draw() gPad.SetLogy() canvas.cd(2) xframe2 = x.frame() p2.plotOn( xframe2 ) xframe2.Draw() gPad.SetLogy() canvas.cd(3) xframe3 = x.frame() pdf.plotOn( xframe3 ) xframe3.Draw() gPad.SetLogy()
def pdf_logPt2_incoh(): #PDF fit to log_10(pT^2) #tree_in = tree_incoh tree_in = tree #ptbin = 0.04 ptbin = 0.12 ptmin = -5. ptmax = 1. mmin = 2.8 mmax = 3.2 #fitran = [-5., 1.] fitran = [-0.9, 0.1] binned = False #gamma-gamma 131 evt for pT<0.18 #output log file out = open("out.txt", "w") ut.log_results( out, "in " + infile + " in_coh " + infile_coh + " in_gg " + infile_gg) loglist = [(x, eval(x)) for x in ["ptbin", "ptmin", "ptmax", "mmin", "mmax", "fitran", "binned"]] strlog = ut.make_log_string(loglist) ut.log_results(out, strlog + "\n") #input data pT = RooRealVar("jRecPt", "pT", 0, 10) m = RooRealVar("jRecM", "mass", 0, 10) dataIN = RooDataSet("data", "data", tree_in, RooArgSet(pT, m)) strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax) data = dataIN.reduce(strsel) #x is RooRealVar for log(Pt2) draw = "TMath::Log10(jRecPt*jRecPt)" draw_func = RooFormulaVar("x", "log_{10}( #it{p}_{T}^{2} ) (GeV^{2})", draw, RooArgList(pT)) x = data.addColumn(draw_func) x.setRange("fitran", fitran[0], fitran[1]) #binned data nbins, ptmax = ut.get_nbins(ptbin, ptmin, ptmax) hPt = TH1D("hPt", "hPt", nbins, ptmin, ptmax) tree_in.Draw(draw + " >> hPt", strsel) dataH = RooDataHist("dataH", "dataH", RooArgList(x), hPt) #range for plot x.setMin(ptmin) x.setMax(ptmax) x.setRange("plotran", ptmin, ptmax) #create the pdf b = RooRealVar("b", "b", 5., 0., 10.) pdf_func = "log(10.)*pow(10.,x)*exp(-b*pow(10.,x))" pdf_logPt2 = RooGenericPdf("pdf_logPt2", pdf_func, RooArgList(x, b)) #make the fit if binned == True: r1 = pdf_logPt2.fitTo(dataH, rf.Range("fitran"), rf.Save()) else: r1 = pdf_logPt2.fitTo(data, rf.Range("fitran"), rf.Save()) ut.log_results(out, ut.log_fit_result(r1)) #calculate norm to number of events xset = RooArgSet(x) ipdf = pdf_logPt2.createIntegral(xset, rf.NormSet(xset), rf.Range("fitran")) print "PDF integral:", ipdf.getVal() if binned == True: nevt = tree_incoh.Draw( "", strsel + " && " + draw + ">{0:.3f}".format(fitran[0]) + " && " + draw + "<{1:.3f}".format(fitran[0], fitran[1])) else: nevt = data.sumEntries("x", "fitran") print "nevt:", nevt pdf_logPt2.setNormRange("fitran") print "PDF norm:", pdf_logPt2.getNorm(RooArgSet(x)) #a = nevt/ipdf.getVal() a = nevt / pdf_logPt2.getNorm(RooArgSet(x)) ut.log_results(out, "log_10(pT^2) parametrization:") ut.log_results(out, "A = {0:.2f}".format(a)) ut.log_results(out, ut.log_fit_parameters(r1, 0, 2)) print "a =", a #Coherent contribution hPtCoh = ut.prepare_TH1D("hPtCoh", ptbin, ptmin, ptmax) hPtCoh.Sumw2() #tree_coh.Draw(draw + " >> hPtCoh", strsel) tree_coh.Draw("TMath::Log10(jGenPt*jGenPt) >> hPtCoh", strsel) ut.norm_to_data(hPtCoh, hPt, rt.kBlue, -5., -2.2) # norm for coh #ut.norm_to_data(hPtCoh, hPt, rt.kBlue, -5, -2.1) #ut.norm_to_num(hPtCoh, 405, rt.kBlue) print "Coherent integral:", hPtCoh.Integral() #TMath::Log10(jRecPt*jRecPt) #Sartre generated coherent shape sartre = TFile.Open( "/home/jaroslav/sim/sartre_tx/sartre_AuAu_200GeV_Jpsi_coh_2p7Mevt.root" ) sartre_tree = sartre.Get("sartre_tree") hSartre = ut.prepare_TH1D("hSartre", ptbin, ptmin, ptmax) sartre_tree.Draw("TMath::Log10(pT*pT) >> hSartre", "rapidity>-1 && rapidity<1") ut.norm_to_data(hSartre, hPt, rt.kViolet, -5, -2) # norm for Sartre #gamma-gamma contribution hPtGG = ut.prepare_TH1D("hPtGG", ptbin, ptmin, ptmax) tree_gg.Draw(draw + " >> hPtGG", strsel) #ut.norm_to_data(hPtGG, hPt, rt.kGreen, -5., -2.9) ut.norm_to_num(hPtGG, 131., rt.kGreen) print "Int GG:", hPtGG.Integral() #psi' contribution psiP = TFile.Open(basedir_mc + "/ana_slight14e4x1_s6_sel5z.root") psiP_tree = psiP.Get("jRecTree") hPtPsiP = ut.prepare_TH1D("hPtPsiP", ptbin, ptmin, ptmax) psiP_tree.Draw(draw + " >> hPtPsiP", strsel) ut.norm_to_num(hPtPsiP, 12, rt.kViolet) #sum of all contributions hSum = ut.prepare_TH1D("hSum", ptbin, ptmin, ptmax) hSum.SetLineWidth(3) #add ggel to the sum hSum.Add(hPtGG) #add incoherent contribution func_logPt2 = TF1("pdf_logPt2", "[0]*log(10.)*pow(10.,x)*exp(-[1]*pow(10.,x))", -10., 10.) func_logPt2.SetParameters(a, b.getVal()) hInc = ut.prepare_TH1D("hInc", ptbin, ptmin, ptmax) ut.fill_h1_tf(hInc, func_logPt2) hSum.Add(hInc) #add coherent contribution hSum.Add(hPtCoh) #add psi(2S) contribution #hSum.Add(hPtPsiP) #set to draw as a lines ut.line_h1(hSum, rt.kBlack) #create canvas frame can = ut.box_canvas() ut.set_margin_lbtr(gPad, 0.11, 0.09, 0.01, 0.01) frame = x.frame(rf.Bins(nbins), rf.Title("")) frame.SetTitle("") frame.SetMaximum(75) frame.SetYTitle("Events / ({0:.3f}".format(ptbin) + " GeV^{2})") print "Int data:", hPt.Integral() #plot the data if binned == True: dataH.plotOn(frame, rf.Name("data")) else: data.plotOn(frame, rf.Name("data")) pdf_logPt2.plotOn(frame, rf.Range("fitran"), rf.LineColor(rt.kRed), rf.Name("pdf_logPt2")) pdf_logPt2.plotOn(frame, rf.Range("plotran"), rf.LineColor(rt.kRed), rf.Name("pdf_logPt2_full"), rf.LineStyle(rt.kDashed)) frame.Draw() amin = TMath.Power(10, ptmin) amax = TMath.Power(10, ptmax) - 1 print amin, amax pt2func = TF1("f1", "TMath::Power(10, x)", amin, amax) #TMath::Power(x, 10) aPt2 = TGaxis(-5, 75, 1, 75, "f1", 510, "-") ut.set_axis(aPt2) aPt2.SetTitle("pt2") #aPt2.Draw(); leg = ut.prepare_leg(0.57, 0.78, 0.14, 0.19, 0.03) ut.add_leg_mass(leg, mmin, mmax) hx = ut.prepare_TH1D("hx", 1, 0, 1) hx.Draw("same") ln = ut.col_lin(rt.kRed) leg.AddEntry(hx, "Data") leg.AddEntry(hPtCoh, "Sartre MC", "l") leg.AddEntry(hPtGG, "#gamma#gamma#rightarrow e^{+}e^{-} MC", "l") #leg.AddEntry(ln, "ln(10)*#it{A}*10^{log_{10}#it{p}_{T}^{2}}exp(-#it{b}10^{log_{10}#it{p}_{T}^{2}})", "l") #leg.AddEntry(ln, "Incoherent fit", "l") leg.Draw("same") l0 = ut.cut_line(fitran[0], 0.9, frame) l1 = ut.cut_line(fitran[1], 0.9, frame) #l0.Draw() #l1.Draw() desc = pdesc(frame, 0.14, 0.8, 0.054) desc.set_text_size(0.03) desc.itemD("#chi^{2}/ndf", frame.chiSquare("pdf_logPt2", "data", 2), -1, rt.kRed) desc.itemD("#it{A}", a, -1, rt.kRed) desc.itemR("#it{b}", b, rt.kRed) desc.draw() #put the sum #hSum.Draw("same") #gPad.SetLogy() frame.Draw("same") #put gamma-gamma hPtGG.Draw("same") #put coherent J/psi hPtCoh.Draw("same") #put Sartre generated coherent shape #hSartre.Draw("same") #put psi(2S) contribution #hPtPsiP.Draw("same") leg2 = ut.prepare_leg(0.14, 0.9, 0.14, 0.08, 0.03) leg2.AddEntry( ln, "ln(10)*#it{A}*10^{log_{10}#it{p}_{T}^{2}}exp(-#it{b}10^{log_{10}#it{p}_{T}^{2}})", "l") #leg2.AddEntry(hPtCoh, "Sartre MC reconstructed", "l") #leg2.AddEntry(hSartre, "Sartre MC generated", "l") leg2.Draw("same") ut.invert_col(rt.gPad) can.SaveAs("01fig.pdf")
#res = background.fitTo(roohistBkg, RooFit.Save(ROOT.kTRUE)) #res = ebkg.fitTo(roohistBkg, RooFit.Save(ROOT.kTRUE)) res = model.fitTo(roohistBkg, RooFit.Save(ROOT.kTRUE)) res.Print() # ----------------------------------------- # plot background canBname = 'can_Mjj_Data' canB = TCanvas(canBname,canBname,900,600) gPad.SetLogy() canB.cd(1).SetBottomMargin(0.4) frame1 = x.frame() frame2 = x.frame() roohistBkg.plotOn(frame1,RooFit.Binning(NBINS)) background.plotOn(frame1) hpull = frame1.pullHist() frame2.addPlotable(hpull,'p') frame1.SetMinimum(0.5) frame1.GetXaxis().SetTitle('') frame1.GetXaxis().SetLabelSize(0.0) frame1.GetYaxis().SetTickLength(0.06) frame1.Draw() pad = TPad('pad','pad',0.,0.,1.,1.) pad.SetTopMargin(0.6) pad.SetFillColor(0) pad.SetFillStyle(0) pad.Draw() pad.cd(0)
roohistBkg = RooDataHist('roohist','roohist',RooArgList(x),hDat) res = background.fitTo(roohistBkg) # ----------------------------------------- # plot background canBname = 'can_Mjj_Data' if useSub: canBname = 'can_Sub_Mjj_Data' canB = TCanvas(canBname,canBname,900,600) gPad.SetLogy() canB.cd(1).SetBottomMargin(0.4) frame1 = x.frame() frame2 = x.frame() roohistBkg.plotOn(frame1,RooFit.Binning(NBINS)) background.plotOn(frame1) hpull = frame1.pullHist() frame2.addPlotable(hpull,'p') frame1.SetMinimum(0.5) frame1.GetXaxis().SetTitle('') if useSub: frame1.GetXaxis().SetRangeUser(900,2500) frame1.GetXaxis().SetLabelSize(0.0) frame1.GetYaxis().SetTickLength(0.06) frame1.Draw() pad = TPad('pad','pad',0.,0.,1.,1.) pad.SetTopMargin(0.6) pad.SetFillColor(0) pad.SetFillStyle(0)
p2 = RooRealVar('p2','p2',5,0,20) p3 = RooRealVar('p3','p3',0.1,0,1) model = RooGenericPdf('model','pow(1-@0/8000,@1)/pow(@0/8000,@2+@3*log(@0/8000))',RooArgList(x,p1,p2,p3)) roohist = RooDataHist('roohist','roohist',RooArgList(x),h) res = model.fitTo(roohist) can = TCanvas('can_Mjj_Data','can_Mjj_Data',900,600) gPad.SetLogy() can.cd(1).SetBottomMargin(0.4); frame1 = x.frame() frame2 = x.frame(); roohist.plotOn(frame1,RooFit.Binning(NBINS)) model.plotOn(frame1) hpull = frame1.pullHist(); frame2.addPlotable(hpull,'p'); frame1.SetMinimum(0.5) frame1.GetXaxis().SetTitle('') frame1.GetXaxis().SetLabelSize(0.0) frame1.GetYaxis().SetTickLength(0.06) frame1.Draw() pad = TPad('pad','pad',0.,0.,1.,1.); pad.SetTopMargin(0.6); pad.SetFillColor(0); pad.SetFillStyle(0); pad.Draw(); pad.cd(0);
def fit(): #fit to log_10(pT^2) with components and plot of plain pT^2 #range in log_10(pT^2) ptbin = 0.12 ptmin = -5. ptmax = 0.99 # 1.01 #range in pT^2 ptsq_bin = 0.03 ptsq_min = 1e-5 ptsq_max = 1 mmin = 2.8 mmax = 3.2 #range for incoherent fit fitran = [-0.9, 0.1] #number of gamma-gamma events ngg = 131 #number of psi' events npsiP = 20 #input data pT = RooRealVar("jRecPt", "pT", 0, 10) m = RooRealVar("jRecM", "mass", 0, 10) data_all = RooDataSet("data", "data", tree, RooArgSet(pT, m)) #select for mass range strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax) data = data_all.reduce(strsel) #create log(pT^2) from pT ptsq_draw = "jRecPt*jRecPt" # will be used for pT^2 logPtSq_draw = "TMath::Log10(" + ptsq_draw + ")" logPtSq_form = RooFormulaVar("logPtSq", "logPtSq", logPtSq_draw, RooArgList(pT)) logPtSq = data.addColumn(logPtSq_form) logPtSq.setRange("fitran", fitran[0], fitran[1]) #bins and range for the plot nbins, ptmax = ut.get_nbins(ptbin, ptmin, ptmax) logPtSq.setMin(ptmin) logPtSq.setMax(ptmax) logPtSq.setRange("plotran", ptmin, ptmax) #range for pT^2 ptsq_nbins, ptsq_max = ut.get_nbins(ptsq_bin, ptsq_min, ptsq_max) #incoherent parametrization bval = RooRealVar("bval", "bval", 3.3, 0, 10) inc_form = "log(10.)*pow(10.,logPtSq)*exp(-bval*pow(10.,logPtSq))" incpdf = RooGenericPdf("incpdf", inc_form, RooArgList(logPtSq, bval)) #make the incoherent fit res = incpdf.fitTo(data, rf.Range("fitran"), rf.Save()) #get incoherent norm to the number of events lset = RooArgSet(logPtSq) iinc = incpdf.createIntegral(lset, rf.NormSet(lset), rf.Range("fitran")) inc_nevt = data.sumEntries("logPtSq", "fitran") incpdf.setNormRange("fitran") aval = RooRealVar("aval", "aval", inc_nevt / incpdf.getNorm(lset)) #print "A =", aval.getVal() #print "b =", bval.getVal() #incoherent distribution from log_10(pT^2) function for the sum with gamma-gamma hIncPdf = ut.prepare_TH1D_n("hGG", nbins, ptmin, ptmax) func_incoh_logPt2 = TF1("func_incoh_logPt2", "[0]*log(10.)*pow(10.,x)*exp(-[1]*pow(10.,x))", -10., 10.) func_incoh_logPt2.SetNpx(1000) func_incoh_logPt2.SetLineColor(rt.kMagenta) func_incoh_logPt2.SetParameters( aval.getVal(), bval.getVal()) # 4.9 from incoherent mc, 3.3 from data fit ut.fill_h1_tf(hIncPdf, func_incoh_logPt2, rt.kMagenta) #gamma-gamma contribution hGG = ut.prepare_TH1D_n("hGG", nbins, ptmin, ptmax) tree_gg.Draw(logPtSq_draw + " >> hGG", strsel) ut.norm_to_num(hGG, ngg, rt.kGreen + 1) #sum of incoherent distribution and gamma-gamma hSumIncGG = ut.prepare_TH1D_n("hSumIncGG", nbins, ptmin, ptmax) hSumIncGG.Add(hIncPdf) hSumIncGG.Add(hGG) ut.line_h1(hSumIncGG, rt.kMagenta) #gamma-gamma in pT^2 hGG_ptsq = ut.prepare_TH1D_n("hGG_ptsq", ptsq_nbins, ptsq_min, ptsq_max) tree_gg.Draw(ptsq_draw + " >> hGG_ptsq", strsel) ut.norm_to_num(hGG_ptsq, ngg, rt.kGreen + 1) #psi' contribution psiP_file = TFile.Open(basedir_mc + "/ana_slight14e4x1_s6_sel5z.root") psiP_tree = psiP_file.Get("jRecTree") hPsiP = ut.prepare_TH1D_n("hPsiP", nbins, ptmin, ptmax) psiP_tree.Draw(logPtSq_draw + " >> hPsiP", strsel) ut.norm_to_num(hPsiP, npsiP, rt.kViolet) #psi' in pT^2 hPsiP_ptsq = ut.prepare_TH1D_n("hPsiP_ptsq", ptsq_nbins, ptsq_min, ptsq_max) psiP_tree.Draw(ptsq_draw + " >> hPsiP_ptsq", strsel) ut.norm_to_num(hPsiP_ptsq, npsiP, rt.kViolet) #create canvas frame gStyle.SetPadTickY(1) can = ut.box_canvas(1086, 543) # square area is still 768^2 can.SetMargin(0, 0, 0, 0) can.Divide(2, 1, 0, 0) gStyle.SetLineWidth(1) can.cd(1) ut.set_margin_lbtr(gPad, 0.11, 0.1, 0.01, 0) frame = logPtSq.frame(rf.Bins(nbins)) frame.SetTitle("") frame.SetMaximum(80) frame.SetYTitle("Events / ({0:.3f}".format(ptbin) + " GeV^{2})") frame.SetXTitle("log_{10}( #it{p}_{T}^{2} ) (GeV^{2})") frame.GetXaxis().SetTitleOffset(1.2) frame.GetYaxis().SetTitleOffset(1.6) #plot the data data.plotOn(frame, rf.Name("data"), rf.LineWidth(2)) #incoherent parametrization incpdf.plotOn(frame, rf.Range("fitran"), rf.LineColor(rt.kRed), rf.Name("incpdf"), rf.LineWidth(2)) incpdf.plotOn(frame, rf.Range("plotran"), rf.LineColor(rt.kRed), rf.Name("incpdf_full"), rf.LineStyle(rt.kDashed), rf.LineWidth(2)) frame.Draw() #add gamma-gamma contribution hGG.Draw("same") #sum of incoherent distribution and gamma-gamma #hSumIncGG.Draw("same") #add psi' #hPsiP.Draw("same") #legend for log_10(pT^2) leg = ut.prepare_leg(0.15, 0.77, 0.28, 0.19, 0.035) hxl = ut.prepare_TH1D("hxl", 1, 0, 1) hxl.Draw("same") ilin = ut.col_lin(rt.kRed, 2) ilin2 = ut.col_lin(rt.kRed, 2) ilin2.SetLineStyle(rt.kDashed) leg.AddEntry(ilin, "Incoherent parametrization, fit region", "l") leg.AddEntry(ilin2, "Incoherent parametrization, extrapolation region", "l") leg.AddEntry(hGG, "#gamma#gamma#rightarrow e^{+}e^{-}", "l") #leg.AddEntry(hxl, "Data", "lp") leg.AddEntry(hxl, "Data, log_{10}( #it{p}_{T}^{2} )", "lp") leg.Draw("same") #----- plot pT^2 on the right ----- #pT^2 variable from pT ptsq_form = RooFormulaVar("ptsq", "ptsq", ptsq_draw, RooArgList(pT)) ptsq = data.addColumn(ptsq_form) #range for pT^2 plot ptsq.setMin(ptsq_min) ptsq.setMax(ptsq_max) #make the pT^2 plot can.cd(2) gPad.SetLogy() #gPad.SetLineWidth(3) #gPad.SetFrameLineWidth(1) ut.set_margin_lbtr(gPad, 0, 0.1, 0.01, 0.15) ptsq_frame = ptsq.frame(rf.Bins(ptsq_nbins), rf.Title("")) #print type(ptsq_frame), type(ptsq) ptsq_frame.SetTitle("") ptsq_frame.SetXTitle("#it{p}_{T}^{2} (GeV^{2})") ptsq_frame.GetXaxis().SetTitleOffset(1.2) data.plotOn(ptsq_frame, rf.Name("data"), rf.LineWidth(2)) ptsq_frame.SetMaximum(9e2) ptsq_frame.SetMinimum(0.8) # 0.101 ptsq_frame.Draw() #incoherent parametrization in pT^2 over the fit region, scaled to the plot inc_ptsq = TF1("inc_ptsq", "[0]*exp(-[1]*x)", 10**fitran[0], 10**fitran[1]) inc_ptsq.SetParameters(aval.getVal() * ptsq_bin, bval.getVal()) #incoherent parametrization in the extrapolation region, below and above the fit region inc_ptsq_ext1 = TF1("inc_ptsq_ext1", "[0]*exp(-[1]*x)", 0., 10**fitran[0]) inc_ptsq_ext2 = TF1("inc_ptsq_ext2", "[0]*exp(-[1]*x)", 10**fitran[1], 10) inc_ptsq_ext1.SetParameters(aval.getVal() * ptsq_bin, bval.getVal()) inc_ptsq_ext1.SetLineStyle(rt.kDashed) inc_ptsq_ext2.SetParameters(aval.getVal() * ptsq_bin, bval.getVal()) inc_ptsq_ext2.SetLineStyle(rt.kDashed) inc_ptsq.Draw("same") inc_ptsq_ext1.Draw("same") inc_ptsq_ext2.Draw("same") #add gamma-gamma in pT^2 hGG_ptsq.Draw("same") #add psi' in pT^2 #hPsiP_ptsq.Draw("same") #redraw the frame #ptsq_frame.Draw("same") ptsq_frame.GetXaxis().SetLimits(-9e-3, ptsq_frame.GetXaxis().GetXmax()) #vertical axis for pT^2 plot xpos = ptsq_frame.GetXaxis().GetXmax() ypos = ptsq_frame.GetMaximum() ymin = ptsq_frame.GetMinimum() ptsq_axis = TGaxis(xpos, 0, xpos, ypos, ymin, ypos, 510, "+GL") ut.set_axis(ptsq_axis) ptsq_axis.SetMoreLogLabels() ptsq_axis.SetTitle("Events / ({0:.3f}".format(ptsq_bin) + " GeV^{2})") ptsq_axis.SetTitleOffset(2.2) ptsq_axis.Draw() #legend for input data #dleg = ut.prepare_leg(0.4, 0.77, 0.14, 0.18, 0.035) dleg = ut.prepare_leg(0.4, 0.71, 0.16, 0.24, 0.035) dleg.AddEntry(None, "#bf{|#kern[0.3]{#it{y}}| < 1}", "") ut.add_leg_mass(dleg, mmin, mmax) dleg.AddEntry(None, "AuAu@200 GeV", "") dleg.AddEntry(None, "UPC sample", "") dleg.AddEntry(hxl, "Data, #it{p}_{T}^{2}", "lp") dleg.Draw("same") #ut.invert_col_can(can) can.SaveAs("01fig.pdf")
cLep = ROOT.TCanvas("DCanvas3Fe", "Fit", 750, 750) pad1 = ROOT.TPad("pad1", "The pad 70% of the height", 0.0, 0.25, 1.0, 1.0) pad2 = ROOT.TPad("pad2", "The pad 30% of the height", 0.0, 0.0, 1.0, 0.25) pad1.SetBottomMargin(0.065) pad1.SetBorderMode(0) pad2.SetTopMargin(0.00001) pad2.SetBottomMargin(0.2999) pad2.SetBorderMode(0) pad2.Draw() pad1.Draw() pad1.cd() frameLep = cosTheta_lepton.frame() data_selected.plotOn(frameLep, RooFit.Binning(25)) Legendre_Lep.plotOn(frameLep) frameLep.GetYaxis().SetTitle('A.U') frameLep.GetXaxis().SetTitle('cos #theta_{l}') frameLep.Draw() pad2.cd() hresid = frameLep.pullHist() frameLep_res = cosTheta_lepton.frame() frameLep_res.addPlotable(hresid, "P") frameLep_res.GetXaxis().SetTitle('cos #theta_{l}') frameLep_res.Draw() cLep.SaveAs( "plots/cosThetaLepton_acceptance_pulls_{}.png".format(name_of_bin)) cLep_final = ROOT.TCanvas("DCanvas3Fe_final", "Fit", 400, 400) cLep_final.cd() frameLep.Draw()
RooArgList(turnon, time, offset)) else: print 'Unknown acceptance type. Aborting' assert(False) # Dataset to fit to datahist = RooDataHist('datahist', 'Dataset from a histogram', RooArgList(time), RooFit.Import(timehist2, False)) # Debug datahist.Print('v') # Fit PDF.fitTo(datahist, RooFit.SumW2Error(True), RooFit.NumCPU(1), RooFit.Range(epsilon, 0.005), RooFit.Optimize(False), RooFit.Verbose(True), RooFit.Strategy(2)) # Plot tframe1 = time.frame(RooFit.Name('ptime'), RooFit.Title('Lifetime acceptance fitted to %s' % accfn)) datahist.plotOn(tframe1, RooFit.MarkerStyle(kFullTriangleUp)) PDF.plotOn(tframe1, RooFit.LineColor(kGreen)) # canvas2 = TCanvas('canvas2', 'Acceptance', 800, 600) tframe1.Draw() canvas1.Print(fname) canvas1.Print(fname + ']') # timestamp = get_timestamp() # canvas1.Print('plots/simple-distrib-%s.pdf' % timestamp) # canvas2.Print('plots/simple-fit-%s-%s.pdf' % (accfn, timestamp))
print "PHSP fit" BkgTotalMPdf = RooGenericPdf("BkgPdf","BkgPdf","sqrt( pow(dimuonditrk_m_rf_c,4) + pow(3.0967,4) + pow(1.01946,4) - 2*pow(dimuonditrk_m_rf_c,2)*pow(3.0967,2) - 2*pow(3.0967,2)*pow(1.01946,2) - 2*pow(dimuonditrk_m_rf_c,2)*pow(1.01946,2) ) * sqrt( pow(5.279,4) + pow(dimuonditrk_m_rf_c,4) + pow(0.493677,4) - 2*pow(5.279,2)*pow(dimuonditrk_m_rf_c,2) - 2*pow(5.279,2)*pow(0.493677,2) - 2*pow(dimuonditrk_m_rf_c,2)*pow(0.493677,2) ) / (dimuonditrk_m_rf_c)", RooArgList(dimuonditrk_m_rf_c)); dimuonditrk_m_rf_c.setBins(80) dimuonditrk_m_rf_c.setRange("baserange",4.0,5.0) s = BkgTotalMPdf.createIntegral(RooArgSet(dimuonditrk_m_rf_c),"baserange").getVal() #bkgFit = BkgTotalMPdf.fitTo(splotBkgData,Range(4.0,5.0),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False)) cb = TCanvas("canvas_b","canvas_b",1200,800) print s mumukkFrame = dimuonditrk_m_rf_c.frame(Title("Phase Space Fit"),Range(4.0,5.0),Normalization(1.0)) splotData.plotOn(mumukkFrame) BkgTotalMPdf.plotOn(mumukkFrame,Normalization(1.65)) mumukkFrame.Draw() if args.phsps: cb.SaveAs(args.path[:-5] + '_bu_phsp_plot.root') cb.SaveAs(args.path[:-5] + '_bu_phsp_plot.png') sys.exit() print "SPLOT FIT" a0 = RooRealVar("a0","a0",0.001,-10.,10.) a1 = RooRealVar("a1","a1",0.001,-5.0,5.0) a2 = RooRealVar("a2","a2",-0.00001,-2.,2.) a3 = RooRealVar("a3","a3",0.0,-0.5,0.5)