def rooFit203(): print ">>> setup model..." x = RooRealVar("x", "x", -10, 10) mean = RooRealVar("mean", "mean of gaussian", 0, -10, 10) gauss = RooGaussian("gauss", "gaussian PDF", x, mean, RooConst(1)) # Construct px = 1 (flat in x) px = RooPolynomial("px", "px", x) # Construct model = f*gx + (1-f)px f = RooRealVar("f", "f", 0., 1.) model = RooAddPdf("model", "model", RooArgList(gauss, px), RooArgList(f)) data = model.generate(RooArgSet(x), 10000) # RooDataSet print ">>> fit to full data range..." result_full = model.fitTo(data, Save(kTRUE)) # RooFitResult print "\n>>> fit \"signal\" range..." # Define "signal" range in x as [-3,3] x.setRange("signal", -3, 3) result_sig = model.fitTo(data, Save(kTRUE), Range("signal")) # RooFitResult print "\n>>> plot and print results..." # Make plot frame in x and add data and fitted model frame1 = x.frame(Title("Fitting a sub range")) # RooPlot data.plotOn(frame1, Name("data")) model.plotOn(frame1, Range("Full"), LineColor(kBlue), Name("model")) # Add shape in full ranged dashed model.plotOn(frame1, LineStyle(kDashed), LineColor(kRed), Name("model2")) # By default only fitted range is shown print "\n>>> result of fit on all data:" result_full.Print() print "\n>>> result of fit in in signal region (note increased error on signal fraction):" result_sig.Print() print ">>> draw on canvas..." canvas = TCanvas("canvas", "canvas", 100, 100, 800, 600) legend = TLegend(0.2, 0.85, 0.4, 0.65) legend.SetTextSize(0.032) legend.SetBorderSize(0) legend.SetFillStyle(0) gPad.SetLeftMargin(0.14) gPad.SetRightMargin(0.02) frame1.GetYaxis().SetLabelOffset(0.008) frame1.GetYaxis().SetTitleOffset(1.4) frame1.GetYaxis().SetTitleSize(0.045) frame1.GetXaxis().SetTitleSize(0.045) frame1.Draw() legend.AddEntry("data", "data", 'LEP') legend.AddEntry("model", "fit (full range)", 'L') legend.AddEntry("model2", "fit (signal range)", 'L') legend.Draw() canvas.SaveAs("rooFit203.png")
def rooFit107(): print ">>> setup model..." x = RooRealVar("x","x",-3,3) mean = RooRealVar("mean","mean of gaussian",1,-10,10) sigma = RooRealVar("sigma","width of gaussian",1,0.1,10) gauss = RooGaussian("gauss","gauss",x,mean,sigma) data = gauss.generate(RooArgSet(x),1000) # RooDataSet gauss.fitTo(data) print ">>> plot pdf and data..." frame1 = x.frame(Name("frame1"),Title("Red Curve / SumW2 Histo errors"),Bins(20)) # RooPlot frame2 = x.frame(Name("frame2"),Title("Dashed Curve / No XError bars"),Bins(20)) # RooPlot frame3 = x.frame(Name("frame3"),Title("Filled Curve / Blue Histo"),Bins(20)) # RooPlot frame4 = x.frame(Name("frame4"),Title("Partial Range / Filled Bar chart"),Bins(20)) # RooPlot print ">>> data plotting styles..." data.plotOn(frame1,DataError(RooAbsData.SumW2)) data.plotOn(frame2,XErrorSize(0)) # Remove horizontal error bars data.plotOn(frame3,MarkerColor(kBlue),LineColor(kBlue)) # Blue markers and error bors data.plotOn(frame4,DrawOption("B"),DataError(RooAbsData.None), XErrorSize(0),FillColor(kGray)) # Filled bar chart print ">>> data plotting styles..." gauss.plotOn(frame1,LineColor(kRed)) gauss.plotOn(frame2,LineStyle(kDashed)) # Change line style to dashed gauss.plotOn(frame3,DrawOption("F"),FillColor(kOrange),MoveToBack()) # Filled shapes in green color gauss.plotOn(frame4,Range(-8,3),LineColor(kMagenta)) print ">>> draw pfds and fits on canvas..." canvas = TCanvas("canvas","canvas",100,100,1000,1200) canvas.Divide(2,2) for i, frame in enumerate([frame1,frame2,frame3,frame4],1): canvas.cd(i) gPad.SetLeftMargin(0.15); gPad.SetRightMargin(0.05) frame.GetYaxis().SetTitleOffset(1.6) frame.GetYaxis().SetLabelOffset(0.010) frame.GetYaxis().SetTitleSize(0.045) frame.GetYaxis().SetLabelSize(0.042) frame.GetXaxis().SetTitleSize(0.045) frame.GetXaxis().SetLabelSize(0.042) frame.Draw() canvas.SaveAs("rooFit107.png")
B_1 = RooRealVar ( "B_{1}" , "B_1 " , 0.3 , -20 , 100 ) B_2 = RooRealVar ( "B_{2}" , "B_2" , 0.3 , -20 , 100 ) B_3 = RooRealVar ( "B_{3}" , "B_3" , 0.3 , -20 , 100 ) B_4 = RooRealVar ( "B_{4}" , "B_4" , 0.3 , -20 , 100 ) bkg = RooChebychev("pdfB" , "pdfB" , masskk , RooArgList(aset)) tot = RooAddPdf("tot","g+cheb",RooArgList(signal,bkg),RooArgList(nSig,nBkg)) nfits = 0 mean.setConstant(True) gamma.setConstant(True) sigma_1.setConstant(True) sigma_2.setConstant(True) rPhifit = tot.fitTo(splotData,Range(phimin,phimax),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False)) nfits = nfits + 1 #gamma.setConstant(False) #sigma.setConstant(False) #rPhifit = tot.fitTo(splotData,Range(phimin,phimax),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False)) #nfits = nfits + 1 mean.setConstant(False) gamma.setConstant(False) sigma_1.setConstant(False) sigma_2.setConstant(False) rPhifit = tot.fitTo(splotData,Range(phimin,phimax),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False)) nfits = nfits + 1 c = TCanvas("canvas","canvas",1200,900)
B_1 = RooRealVar("B_{1}", "B_1 ", 0.3, -20, 100) B_2 = RooRealVar("B_{2}", "B_2", 0.3, -20, 100) B_3 = RooRealVar("B_{3}", "B_3", 0.3, -20, 100) B_4 = RooRealVar("B_{4}", "B_4", 0.3, -20, 100) bkg = RooChebychev("pdfB", "pdfB", masskk, RooArgList(aset)) tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) nfits = 0 mean.setConstant(True) gamma.setConstant(True) rPhifit = tot.fitTo(splotData, Range(phimin, phimax), RooFit.NumCPU(args.ncpu), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(True) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(phimin, phimax), RooFit.NumCPU(args.ncpu), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(False) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(phimin, phimax), RooFit.NumCPU(args.ncpu), RooFit.Verbose(False)) nfits = nfits + 1
B_1 = RooRealVar("B_{1}", "B_1 ", 0.3, -20, 100) B_2 = RooRealVar("B_{2}", "B_2", 0.3, -20, 100) B_3 = RooRealVar("B_{3}", "B_3", 0.3, -20, 100) B_4 = RooRealVar("B_{4}", "B_4", 0.3, -20, 100) bkg = RooChebychev("pdfB", "pdfB", psiPrimeMass, RooArgList(aset)) tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) nfits = 0 mean.setConstant(True) gamma.setConstant(True) rPhifit = tot.fitTo(splotData, Range(psimin, psimax), RooFit.NumCPU(20), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(True) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(psimin, psimax), RooFit.NumCPU(20), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(False) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(psimin, psimax), RooFit.NumCPU(20), RooFit.Verbose(False)) nfits = nfits + 1
def rooFit110(): print ">>> setup model..." x = RooRealVar("x","x",-10,10) mean = RooConst(-2) sigma = RooConst(3) gauss = RooGaussian("gauss","gauss",x,mean,sigma) frame1 = x.frame(Title("Gaussian pdf")) # RooPlot gauss.plotOn(frame1) print ">>> retrieve raw & normalized values of RooFit pdfs...\n>>>" # Return 'raw' unnormalized value of gauss print ">>> raw value: gauss.getVal( ) = %.3f" % gauss.getVal() + " depends on x range" print ">>> normalization: gauss.getVal(x) = %.3f" % gauss.getVal(RooArgSet(x)) print ">>> normalization: gauss.getNorm(x) = %.3f" % gauss.getNorm(RooArgSet(x)) print ">>> 1/(sigma*sqrt(2*pi)) = %.3f" % (1/(sigma.getValV()*sqrt(2*pi))) # Create object representing integral over gauss # which is used to calculate gauss_Norm[x] == gauss / gauss_Int[x] igauss = gauss.createIntegral(RooArgSet(x)) # RooAbsReal print ">>> integral: igauss.getVal( ) = %.3f" % igauss.getVal() print ">>> igauss.getVal(x) = %.3f" % igauss.getVal(RooArgSet(x)) print ">>> 1/igauss.getVal(x) = %.3f" % (1/igauss.getVal(RooArgSet(x))) print ">>> gauss.getVal()/igauss.getVal(x) = %.3f" % (gauss.getVal()/igauss.getVal(RooArgSet(x))) # maximum print ">>> maximum: gauss.getMaxVal(x) = %.3f" % gauss.getMaxVal(RooArgSet(x)) print ">>> gauss.getMax(0) = %.3f" % gauss.maxVal(0) print ">>> gauss.getMax(1) = %.3f" % gauss.maxVal(1) print ">>> gauss.asTF(x).GetMaximumX() = %.3f" % gauss.asTF(RooArgList(x)).GetMaximumX() print ">>> gauss.asTF(x).GetMaximum() = %.3f" % gauss.asTF(RooArgList(x)).GetMaximum() xmaxVal = gauss.asTF(RooArgList(x)).GetMaximumX() xmax = RooRealVar("x_max","x_max",xmaxVal) x.setVal(xmaxVal) print ">>> gauss.getVal( ) = %.3f" % gauss.getVal() print ">>> gauss.getVal(xmax) = %.3f" % gauss.getVal(RooArgSet(x)) print ">>> gauss.getVal(xmax) = %.3f" % gauss.getVal(RooArgSet(xmax)) print ">>> plot" frame2 = x.frame(Title("integral of Gaussian pdf")) # RooPlot line1 = RooLinearVar("line1","line1",x,RooConst(0),RooConst(gauss.getVal())) line2 = RooLinearVar("line2","line2",x,RooConst(0),RooConst(gauss.getVal(RooArgSet(x)))) igauss.plotOn(frame2,LineColor(kBlue), Name(igauss.GetName())) line1.plotOn( frame2,LineColor(kRed), Name(line1.GetName())) line2.plotOn( frame2,LineColor(kGreen),Name(line2.GetName())) print ">>>\n>>> integrate normalized pdf over subrange..." # Define a range named "signal" in x from -5,5 x.setRange("signal",-5,5) # Create an integral of gauss_Norm[x] over x in range "signal" # This is the fraction of of p.d.f. gauss_Norm[x] which is in # the range named "signal" igauss_sig = gauss.createIntegral(RooArgSet(x),NormSet(RooArgSet(x)),Range("signal")) # RooAbsReal print ">>> gauss_Int[x|signal]_Norm[x] = %.2f" % igauss_sig.getVal() print ">>> construct cumulative distribution function from pdf..." # Create the cumulative distribution function of # gauss, i.e. calculate Int[-10,x] gauss(x') dx' gauss_cdf = gauss.createCdf(RooArgSet(x)) # RooAbsReal frame3 = x.frame(Title("cdf of Gaussian pdf")) # RooPlot gauss_cdf.plotOn(frame3) print ">>> draw functions on canvas..." canvas = TCanvas("canvas","canvas",100,100,2000,400) legend = TLegend(0.6,0.6,0.8,0.42) legend.SetTextSize(0.032) legend.SetBorderSize(0) legend.SetFillStyle(0) canvas.Divide(3) for i, frame in enumerate([frame1,frame2,frame3],1): canvas.cd(i) gPad.SetLeftMargin(0.14); gPad.SetRightMargin(0.04) frame.GetYaxis().SetTitleOffset(1.6) frame.GetYaxis().SetLabelOffset(0.010) frame.GetYaxis().SetTitleSize(0.045) frame.GetYaxis().SetLabelSize(0.042) frame.GetXaxis().SetTitleSize(0.045) frame.GetXaxis().SetLabelSize(0.042) frame.Draw() if i is 2: legend.AddEntry(line1.GetName(), " gauss.getVal( )",'L') legend.AddEntry(line2.GetName(), " gauss.getVal(x)",'L') legend.AddEntry(igauss.GetName(),"igauss.getVal( )",'L') legend.Draw() canvas.SaveAs("rooFit110.png")
alldata.numEntries() # In[10]: alldata.numEntries() c = TCanvas("canvas","canvas",1200,800) massFrame = mass.frame() alldata.plotOn(massFrame) massFrame.Draw() c.SaveAs("plots/testmass.png") massKKFrame = masskk.frame(Range(phimin,phimax)) alldata.plotOn(massKKFrame,RooLinkedList()) massKKFrame.Draw() c.SaveAs("plots/testmasskk.png") # In[11]: #b0dataNonPromptMass = b0dataNonPrompt.reduce(SelectVars(RooArgSet(mass))) # In[12]:
print "Tree entries %d"%(splotData.numEntries()) 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.)
sigma2 = RooRealVar("sigma2", "width of gaussian2", 0.004, 0.004, 0.01) pdfS1 = RooGaussian("pdfS1", "gaus", mass, mean, sigma1) pdfS2 = RooGaussian("pdfS2", "gaus", mass, mean, sigma2) # pdfB = RooExponential("pdfB" , "pdfB" , mbs , B_c) pdfB = RooBernstein("pdfB", "pdfB", mass, RooArgList(B_c, B_b)) alist1 = RooArgList(pdfS1, pdfS2, pdfB) alist2 = RooArgList(S1, S2, B) tot = RooAddPdf("model", "model", alist1, alist2) # In[ ]: #xDataPromptKK = xdataPrompt.reduce(SelectVars(RooArgSet(masskk))) mean.setConstant(True) rPhifit = tot.fitTo(xdataPrompt, Range(massmin, massmax), RooFit.NumCPU(8)) mean.setConstant(False) rPhifit = tot.fitTo(xdataPrompt, Range(massmin, massmax), RooFit.NumCPU(8)) # In[ ]: c = TCanvas("canvas", "canvas", 1200, 800) phiFrame = masskk.frame(Range(massmin, massmax)) xdataPrompt.plotOn(phiFrame) tot.plotOn(phiFrame) phiFrame.Draw() c.SaveAs("phiMassSPlotPhi.png") c.SaveAs("phiMassSPlotPhi.root") c.Clear()
B_1 = RooRealVar("B_1", "B_1 ", 0.3, -20, 100) B_2 = RooRealVar("B_2", "B_2", 0.3, -20, 100) B_3 = RooRealVar("B_3", "B_3", 0.3, -20, 100) B_4 = RooRealVar("B_4", "B_4", 0.3, -20, 100) bkg = RooBernstein("pdfB", "pdfB", masskk, RooArgList(B_1, B_2, B_3, B_4)) tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) # In[ ]: #xDataPromptKK = xdataPrompt.reduce(SelectVars(RooArgSet(masskk))) mean.setConstant(True) gamma.setConstant(True) rPhifit = tot.fitTo(xdataPrompt, Range(phimin, phimax), RooFit.NumCPU(4)) #rPhifit = tot.fitTo(xdataPrompt,Range(phimin,phimax),RooFit.NumCPU(4)) mean.setConstant(False) gamma.setConstant(False) rPhifit = tot.fitTo(xdataPrompt, Range(phimin, phimax), RooFit.NumCPU(4)) # In[ ]: c = TCanvas("canvas", "canvas", 1200, 800) phiFrame = masskk.frame(Range(phimin, phimax), Normalization((nSig.getValV() + nBkg.getValV()))) xdataPrompt.plotOn(phiFrame) tot.plotOn(phiFrame)
def rooFit108(): print ">>> setup model - a B decay with mixing..." dt = RooRealVar("dt","dt",-20,20) dm = RooRealVar("dm","dm",0.472) tau = RooRealVar("tau","tau",1.547) w = RooRealVar("w","mistag rate",0.1) dw = RooRealVar("dw","delta mistag rate",0.) # Build categories - possible values states # https://root.cern/doc/v610/classRooCategory.html mixState = RooCategory("mixState","B0/B0bar mixing state") mixState.defineType("mixed",-1) mixState.defineType("unmixed",1) tagFlav = RooCategory("tagFlav","Flavour of the tagged B0") tagFlav.defineType("B0",1) tagFlav.defineType("B0bar",-1) # Build a gaussian resolution model dterr = RooRealVar("dterr","dterr",0.1,1.0) bias1 = RooRealVar("bias1","bias1",0) sigma1 = RooRealVar("sigma1","sigma1",0.1) gm1 = RooGaussModel("gm1","gauss model 1",dt,bias1,sigma1) # Construct Bdecay (x) gauss # https://root.cern/doc/v610/classRooBMixDecay.html bmix = RooBMixDecay("bmix","decay",dt,mixState,tagFlav,tau,dm,w,dw,gm1,RooBMixDecay.DoubleSided) print ">>> sample data from data..." data = bmix.generate(RooArgSet(dt,mixState,tagFlav),2000) # RooDataSet print ">>> show dt distribution with custom binning..." # Make plot of dt distribution of data in range (-15,15) with fine binning for dt>0 # and coarse binning for dt<0 tbins = RooBinning(-15,15) # Create binning object with range (-15,15) tbins.addUniform(60,-15,0) # Add 60 bins with uniform spacing in range (-15,0) tbins.addUniform(15,0,15) # Add 15 bins with uniform spacing in range (0,15) dtframe = dt.frame(Range(-15,15),Title("dt distribution with custom binning")) # RooPlot data.plotOn(dtframe,Binning(tbins)) bmix.plotOn(dtframe) # NB: Note that bin density for each bin is adjusted to that of default frame # binning as shown in Y axis label (100 bins --> Events/0.4*Xaxis-dim) so that # all bins represent a consistent density distribution print ">>> plot mixstate asymmetry with custom binning..." # Make plot of dt distribution of data asymmetry in 'mixState' with variable binning abins = RooBinning(-10,10) # Create binning object with range (-10,10) abins.addBoundary(0) # Add boundaries at 0 abins.addBoundaryPair(1) # Add boundaries at (-1,1) abins.addBoundaryPair(2) # Add boundaries at (-2,2) abins.addBoundaryPair(3) # Add boundaries at (-3,3) abins.addBoundaryPair(4) # Add boundaries at (-4,4) abins.addBoundaryPair(6) # Add boundaries at (-6,6) aframe = dt.frame(Range(-10,10),Title("MixState asymmetry distribution with custom binning")) # RooPlot # Plot mixState asymmetry of data with specified customg binning data.plotOn(aframe,Asymmetry(mixState),Binning(abins)) # Plot corresponding property of pdf bmix.plotOn(aframe,Asymmetry(mixState)) # Adjust vertical range of plot to sensible values for an asymmetry aframe.SetMinimum(-1.1) aframe.SetMaximum( 1.1) # NB: For asymmetry distributions no density corrects are needed (and are thus not applied) print "\n>>> draw on canvas..." canvas = TCanvas("canvas","canvas",100,100,1400,600) canvas.Divide(2) canvas.cd(1) gPad.SetLeftMargin(0.15); gPad.SetRightMargin(0.02) dtframe.GetYaxis().SetLabelOffset(0.008) dtframe.GetYaxis().SetTitleOffset(1.6) dtframe.GetYaxis().SetTitleSize(0.045) dtframe.GetXaxis().SetTitleSize(0.045) dtframe.Draw() canvas.cd(2) gPad.SetLeftMargin(0.15); gPad.SetRightMargin(0.02) aframe.GetYaxis().SetLabelOffset(0.008) aframe.GetYaxis().SetTitleOffset(1.6) aframe.GetYaxis().SetTitleSize(0.045) aframe.GetXaxis().SetTitleSize(0.045) aframe.Draw() canvas.SaveAs("rooFit108.png")
# kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 if not debugging: kkMean.setConstant(ROOT.kFALSE) # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 kkGamma.setConstant(ROOT.kFALSE) # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 kkfit = kkTot.fitTo(traKFitData, Range(fitphimin + 0.005, fitphimax - 0.005), RooFit.NumCPU(7), RooFit.Save()) nfit += 1 sigmaside_kk = math.sqrt(kkGamma.getValV()**2 + kkSigma.getValV()**2) kkFrame = tt_mass.frame(Range(fitphimin + 0.005, fitphimax - 0.005)) leftlowside_kk = -6. * sigmaside_kk + kkMean.getValV() leftupside_kk = -4. * sigmaside_kk + kkMean.getValV() rightlowside_kk = +4. * sigmaside_kk + kkMean.getValV() rightupside_kk = +6. * sigmaside_kk + kkMean.getValV() signallow = -3. * sigmaside_kk + kkMean.getValV() signalup = +3. * sigmaside_kk + kkMean.getValV()
event = 2 xTuple.SetBranchAddress("event", AddressOf(milk, 'price5')) newfile = TFile("small.root", "recreate") newtree = xTuple.CloneTree() newtree.CopyEntries(xTuple) newtree.Write() sys.exit() # In[16]: file = TFile("newFile.root", "RECREATE") canvas = TCanvas("canvas", "canvas", 1200, 1000) mass = RooRealVar("xM", "M(#mu#mu#mu#mu)[GeV]", 5.15, 5.55) trigger = RooRealVar("trigger", "trigger", 0.0, 10000) vProb = RooRealVar("vProb", "vProb", -1.0, 1.0) alldata = RooDataSet("alldata", "alldata", xTuple, RooArgSet(mass), RooFormulaVar("vProb", "vProb", "vProb>0.01", RooArgList(vProb))) #,cutFormula) frame = mass.frame(Range(5.15, 5.55)) alldata.plotOn(frame, RooLinkedList()) alldata.Write() frame.Draw() # In[ ]: canvas.SaveAs("testCanvas.eps")
#xb->setRange("alt","x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9") ; b0dataNonPrompt = (( alldata.reduce('xHlt!=8')).reduce('xM>5.2')).reduce("xL>3.0") # In[ ]: b0dataNonPromptMass = b0dataNonPrompt.reduce(SelectVars(RooArgSet(mass))) b0dataNonPrompt.numEntries() # In[ ]: c = TCanvas("canvas", "canvas", 1200, 800) mass.setRange("fitRange", massmin, massmax) mass.setBins(200) massFrame = mass.frame(Range(massmin, massmax)) b0dataNonPrompt.plotOn(massFrame) massFrame.Draw() c.SaveAs("testmass.png") # In[13]: phimean = 1.019 sigma = RooRealVar("sigma", "width of gaussian", 0.0013) gamma = RooRealVar("gamma", "gamma of bw", 0.004253, 0.001, 0.01) mean = RooRealVar("mean", "mean of gaussian", phimean, phimean - 0.005, phimean + 0.005) nSig = RooRealVar("nSig", "nSig", 1E6, 0., 5.0E6) nBkg = RooRealVar("nBkg", "nBkg", 5E5, 0., 5.0E6)
def rooFit111(): print ">>> setup model..." x = RooRealVar("x", "x", -10, 10) mean = RooRealVar("mean", "mean of gaussian", 1, -10, 10) sigma = RooRealVar("sigma", "width of gaussian", 1, 0.1, 10) gauss = RooGaussian("gauss", "gaussian PDF", x, mean, sigma) print ">>> create and plot derivatives wrt x..." dgdx = gauss.derivative(x, 1) # RooAbsReal d2gdx2 = gauss.derivative(x, 2) # RooAbsReal d3gdx3 = gauss.derivative(x, 3) # RooAbsReal print ">>> plot gaussian and its first two derivatives..." frame1 = x.frame(Title("d^{n}(Gauss)/dx^{n}")) # RooPlot norm1 = 10 gauss.plotOn(frame1, LineColor(kBlue), Name(gauss.GetName()), Normalization(norm1)) dgdx.plotOn(frame1, LineColor(kMagenta), Name("dgdx")) d2gdx2.plotOn(frame1, LineColor(kRed), Name("d2gdx2")) d3gdx3.plotOn(frame1, LineColor(kOrange), Name("d3gdx3")) print ">>> create and plot derivatives wrt sigma..." dgds = gauss.derivative(sigma, 1) # RooAbsReal d2gds2 = gauss.derivative(sigma, 2) # RooAbsReal d3gds3 = gauss.derivative(sigma, 3) # RooAbsReal print ">>> plot gaussian and its first two derivatives..." frame2 = sigma.frame(Title("d^{n}(Gauss)/d(sigma)^{n}"), Range(0., 2.)) # RooPlot (norm2, norm21, norm22) = (8000, 15, 5) gauss.plotOn(frame2, LineColor(kBlue), Name(gauss.GetName()), Normalization(norm2)) dgds.plotOn(frame2, LineColor(kMagenta), Name("dgds"), Normalization(norm21)) d2gds2.plotOn(frame2, LineColor(kRed), Name("d2gds2"), Normalization(norm22)) d3gds3.plotOn(frame2, LineColor(kOrange), Name("d3gds3")) print ">>> draw pdfs and fits on canvas..." canvas = TCanvas("canvas", "canvas", 100, 100, 1400, 600) legend1 = TLegend(0.22, 0.85, 0.4, 0.65) legend2 = TLegend(0.60, 0.85, 0.8, 0.65) for legend in [legend1, legend2]: legend.SetTextSize(0.032) legend.SetBorderSize(0) legend.SetFillStyle(0) 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() legend1.AddEntry(gauss.GetName(), "gaussian G (#times%s)" % norm1, 'L') legend1.AddEntry("dgdx", "d(G)/dx", 'L') legend1.AddEntry("d2gdx2", "d^{2}(G)/dx^{2}", 'L') legend1.AddEntry("d3gdx3", "d^{3}(G)/dx^{3}", 'L') legend1.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() legend2.AddEntry(gauss.GetName(), "gaussian G (#times%s)" % norm2, 'L') legend2.AddEntry("dgds", "d(G)/ds (#times%s)" % norm21, 'L') legend2.AddEntry("d2gds2", "d^{2}(G)/ds^{2} (#times%s)" % norm22, 'L') legend2.AddEntry("d3gds3", "d^{3}(G)/ds^{3}", 'L') legend2.Draw() canvas.SaveAs("rooFit111.png")
a3.setConstant(kTRUE) a4.setConstant(kTRUE) a5.setConstant(kTRUE) # a6.setConstant(kTRUE) kkMean.setConstant(kFALSE) # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 kkGamma.setConstant(kFALSE) # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 #kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005), RooFit.NumCPU(7),RooFit.Save()) kkfit = kkTot.fitTo(traKFitData, Range(fitphimin, fitphimax), RooFit.PrintLevel(-1), RooFit.NumCPU(numcpus), RooFit.Save()) nfit += 1 sigmaside_kk = math.sqrt(kkGamma.getValV()**2 + kkSigma.getValV()**2) sigmaside_kk = kkGamma.getValV() if debugging: sigmaside_kk = 0.001 leftlowside = -sidehigh * sigmaside_kk + kkMean.getValV() leftupside = -sidelow * sigmaside_kk + kkMean.getValV() rightlowside = +sidelow * sigmaside_kk + kkMean.getValV() rightupside = +sidehigh * sigmaside_kk + kkMean.getValV() signallow = -signalside * sigmaside_kk + kkMean.getValV()
B_1 = RooRealVar("B_{1}", "B_1 ", 0.3, -20, 100) B_2 = RooRealVar("B_{2}", "B_2", 0.3, -20, 100) B_3 = RooRealVar("B_{3}", "B_3", 0.3, -20, 100) B_4 = RooRealVar("B_{4}", "B_4", 0.3, -20, 100) bkg = RooChebychev("pdfB", "pdfB", mass_ref_c_kkk, RooArgList(aset)) tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) nfits = 0 mean.setConstant(True) gamma.setConstant(True) rPhifit = tot.fitTo(splotData, Range(bumin, bumax), RooFit.NumCPU(8), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(True) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(bumin, bumax), RooFit.NumCPU(8), RooFit.Verbose(False)) nfits = nfits + 1 mean.setConstant(False) gamma.setConstant(False) rPhifit = tot.fitTo(splotData, Range(bumin, bumax), RooFit.NumCPU(8), RooFit.Verbose(False)) nfits = nfits + 1