Skip to content

Commit

Permalink
add all examples up to 111, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
clelange committed May 13, 2016
1 parent 287aeb9 commit 5a618dd
Show file tree
Hide file tree
Showing 9 changed files with 658 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CtoPy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ sed -i '.bak' 's|::|.|g' ${FILE}
# this screws up formulae
# sed -i '.bak' 's|*||g;' ${FILE}
sed -i '.bak' 's|(Title|(RooFit.Title|g' ${FILE}
sed -i '.bak' 's|,Title|,RooFit.Title|g' ${FILE}
sed -i '.bak' 's|(Name|(RooFit.Name|g' ${FILE}
sed -i '.bak' 's|(Range|(RooFit.Range|g' ${FILE}
sed -i '.bak' 's|,Layout|,RooFit.Layout|g' ${FILE}
sed -i '.bak' 's|,DataError|,RooFit.DataError|g' ${FILE}
sed -i '.bak' 's|,RooConst|,RooFit.RooConst|g' ${FILE}
sed -i '.bak' 's|,Bins|,RooFit.Bins|g' ${FILE}
sed -i '.bak' 's|,Save()|,RooFit.Save()|g' ${FILE}
sed -i '.bak' 's|,.RooConst(|,RooFit.RooConst(|g' ${FILE}
sed -i '.bak' 's/;*$//g' ${FILE}
sed -i '.bak' 's/generate(x/generate(RooArgSet(x)/g' ${FILE}
sed -i '.bak' 's/createIntegral(x/createIntegral(RooArgSet(x)/g' ${FILE}
sed -i '.bak' 's/using namespace RooFit/from ROOT import */g' ${FILE}

rm ${FILE}.bak
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ Currently available:
1. [rf101_basics.py](rf101_basics.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #101
2. [rf102_dataimport.py](rf102_dataimport.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
3. [rf103_interprfuncs.py](rf103_interprfuncs.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #103
4. [rf104_classfactory.py](rf104_classfactory.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #104
4. [rf104_classfactory.py](rf104_classfactory.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #104 - currently crashes on Mac OS X, works on Linux
5. ~~[rf105_funcbinding.py](rf105_funcbinding.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #105~~ - not working
6. [rf106_plotdecoration.py](rf106_plotdecoration.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #106
7. [rf107_plotstyles.py](rf107_plotstyles.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #107
8. [rf108_plotbinning.py](rf108_plotbinning.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #108
9. [rf109_chi2residpull.py](rf109_chi2residpull.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #109
10. [rf110_normintegration.py](rf110_normintegration.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #110
11. [rf111_derivatives.py](rf111_derivatives.py) 'BASIC FUNCTIONALITY' RooFit tutorial macro #111
84 changes: 84 additions & 0 deletions rf105_funcbinding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#####################################
#
# 'BASIC FUNCTIONALITY' RooFit tutorial macro #105
#
# Demonstration of binding ROOT Math functions as RooFit functions
# and pdfs
#
# 07/2008 - Wouter Verkerke
#
####################################/

from ROOT import *

def rf105_funcbinding():

# B i n d T M a t h : : E r f C f u n c t i o n
# ---------------------------------------------------

# Bind one-dimensional TMath.Erf function as RooAbsReal function
x = RooRealVar("x","x",-3,3)
erf = RooFit.bindFunction("erf",TMath.Erf,x)

# Print erf definition
erf.Print()

# Plot erf on frame
frame1 = x.frame(RooFit.Title("TMath.Erf bound as RooFit function"))
erf.plotOn(frame1)


# B i n d R O O T : : M a t h : : b e t a _ p d f C f u n c t i o n
# -----------------------------------------------------------------------

# Bind pdf ROOT.Math.Beta with three variables as RooAbsPdf function
x2 = RooRealVar("x2","x2",0,0.999)
a = RooRealVar("a","a",5,0,10)
b = RooRealVar("b","b",2,0,10)
beta = RooFit.bindPdf("beta",ROOT.Math.beta_pdf,x2,a,b)

# Perf beta definition
beta.Print()

# Generate some events and fit
data = beta.generate(RooArgSet(x2),10000)
beta.fitTo(*data)

# Plot data and pdf on frame
frame2 = x2.frame(RooFit.Title("ROOT.Math.Beta bound as RooFit pdf"))
data.plotOn(frame2)
beta.plotOn(frame2)



# B i n d R O O T T F 1 a s R o o F i t f u n c t i o n
# ---------------------------------------------------------------

# Create a ROOT TF1 function
fa1 = TF1("fa1","sin(x)/x",0,10)

# Create an observable
x3 = RooRealVar("x3","x3",0.01,20)

# Create binding of TF1 object to above observable
rfa1 = bindFunction(fa1,x3)

# Print rfa1 definition
rfa1.Print()

# Make plot frame in observable, TF1 binding function
frame3 = x3.frame(RooFit.Title("TF1 bound as RooFit function"))
rfa1.plotOn(frame3)



c = TCanvas("rf105_funcbinding","rf105_funcbinding",1200,400)
c.Divide(3)
c.cd(1) ; gPad.SetLeftMargin(0.15) ; frame1.GetYaxis().SetTitleOffset(1.6) ; frame1.Draw()
c.cd(2) ; gPad.SetLeftMargin(0.15) ; frame2.GetYaxis().SetTitleOffset(1.6) ; frame2.Draw()
c.cd(3) ; gPad.SetLeftMargin(0.15) ; frame3.GetYaxis().SetTitleOffset(1.6) ; frame3.Draw()

c.SaveAs("rf105_funcbinding.png")

if __name__ == "__main__":
rf105_funcbinding()
95 changes: 95 additions & 0 deletions rf106_plotdecoration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#####################################
#
# 'BASIC FUNCTIONALITY' RooFit tutorial macro #106
#
# Adding boxes with parameters, to RooPlots.
# Decorating RooPlots with arrows, etc...
#
#
# 07/2008 - Wouter Verkerke
#
####################################/


from ROOT import *


def rf106_plotdecoration():

# S e t u p m o d e l
# ---------------------

# Create observables
x = RooRealVar("x","x",-10,10)

# Create Gaussian
sigma = RooRealVar("sigma","sigma",1,0.1,10)
mean = RooRealVar("mean","mean",-3,-10,10)
gauss = RooGaussian("gauss","gauss",x,mean,sigma)

# Generate a sample of 1000 events with sigma=3
data = gauss.generate(RooArgSet(x),1000)

# Fit pdf to data
gauss.fitTo(data)


# P l o t p . d . f a n d d a t a
# -------------------------------------

# Overlay projection of gauss on data
frame = x.frame(RooFit.Name("xframe"),RooFit.Title("RooPlot with decorations"),RooFit.Bins(40))
data.plotOn(frame)
gauss.plotOn(frame)


# A d d b o x w i t h p d f p a r a m e t e r s
# -----------------------------------------------------

# Left edge of box starts at 55% of Xaxis)
gauss.paramOn(frame,RooFit.Layout(0.55))


# A d d b o x w i t h d a t a s t a t i s t i c s
# -------------------------------------------------------

# X size of box is from 55% to 99% of Xaxis range, of box is at 80% of Yaxis range)
data.statOn(frame,RooFit.Layout(0.55,0.99,0.8))


# A d d t e x t a n d a r r o w
# -----------------------------------

# Add text to frame
txt = TText(2,100,"Signal")
txt.SetTextSize(0.04)
txt.SetTextColor(kRed)
frame.addObject(txt)

# Add arrow to frame
arrow = TArrow(2,100,-1,50,0.01,"|>")
arrow.SetLineColor(kRed)
arrow.SetFillColor(kRed)
arrow.SetLineWidth(3)
frame.addObject(arrow)


# P e r s i s t f r a m e w i t h a l l d e c o r a t i o n s i n R O O T f i l e
# ---------------------------------------------------------------------------------------------

f = TFile("rf106_plotdecoration.root","RECREATE")
frame.Write()
f.Close()

# To read back and plot frame with all decorations in clean root session do
# root> TFile f("rf106_plotdecoration.root")
# root> xframe.Draw()

c = TCanvas("rf106_plotdecoration","rf106_plotdecoration",600,600)
gPad.SetLeftMargin(0.15) ; frame.GetYaxis().SetTitleOffset(1.6) ; frame.Draw()

c.SaveAs("rf106_plotdecoration.png")

if __name__ == "__main__":
rf106_plotdecoration()

94 changes: 94 additions & 0 deletions rf107_plotstyles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#####################################
#
# 'BASIC FUNCTIONALITY' RooFit tutorial macro #107
#
# Demonstration of various plotting styles of data, functions
# in a RooPlot
#
# 07/2008 - Wouter Verkerke
#
####################################/

from ROOT import *


def rf107_plotstyles():

# S e t u p m o d e l
# ---------------------

# Create observables
x = RooRealVar("x","x",-10,10)

# Create Gaussian
sigma = RooRealVar("sigma","sigma",3,0.1,10)
mean = RooRealVar("mean","mean",-3,-10,10)
gauss = RooGaussian("gauss","gauss",x,mean,sigma)

# Generate a sample of 100 events with sigma=3
data = gauss.generate(RooArgSet(x),100)

# Fit pdf to data
gauss.fitTo(data)



# M a k e p l o t f r a m e s
# -------------------------------

# Make four plot frames to demonstrate various plotting features
frame1 = x.frame(RooFit.Name("xframe"),RooFit.Title("Red Curve / SumW2 Histo errors"),RooFit.Bins(20))
frame2 = x.frame(RooFit.Name("xframe"),RooFit.Title("Dashed Curve / No XError bars"),RooFit.Bins(20))
frame3 = x.frame(RooFit.Name("xframe"),RooFit.Title("Filled Curve / Blue Histo"),RooFit.Bins(20))
frame4 = x.frame(RooFit.Name("xframe"),RooFit.Title("Partial Range / Filled Bar chart"),RooFit.Bins(20))



# D a t a p l o t t i n g s t y l e s
# ---------------------------------------

# Use sqrt(sum(weights^2)) error instead of Poisson errors
data.plotOn(frame1,RooFit.DataError(RooAbsData.SumW2))

# Remove horizontal error bars
data.plotOn(frame2,RooFit.XErrorSize(0))

# Blue markers and error bors
data.plotOn(frame3,RooFit.MarkerColor(kBlue),RooFit.LineColor(kBlue))

# Filled bar chart
data.plotOn(frame4,RooFit.DrawOption("B"),RooFit.DataError(RooAbsData.None),RooFit.XErrorSize(0),RooFit.FillColor(kGray))



# F u n c t i o n p l o t t i n g s t y l e s
# -----------------------------------------------

# Change line color to red
gauss.plotOn(frame1,RooFit.LineColor(kRed))

# Change line style to dashed
gauss.plotOn(frame2,RooFit.LineStyle(kDashed))

# Filled shapes in green color
gauss.plotOn(frame3,RooFit.DrawOption("F"),RooFit.FillColor(kOrange),RooFit.MoveToBack())

#
gauss.plotOn(frame4,RooFit.Range(-8,3),RooFit.LineColor(kMagenta))



c = TCanvas("rf107_plotstyles","rf107_plotstyles",800,800)
c.Divide(2,2)
c.cd(1) ; gPad.SetLeftMargin(0.15) ; frame1.GetYaxis().SetTitleOffset(1.6) ; frame1.Draw()
c.cd(2) ; gPad.SetLeftMargin(0.15) ; frame2.GetYaxis().SetTitleOffset(1.6) ; frame2.Draw()
c.cd(3) ; gPad.SetLeftMargin(0.15) ; frame3.GetYaxis().SetTitleOffset(1.6) ; frame3.Draw()
c.cd(4) ; gPad.SetLeftMargin(0.15) ; frame4.GetYaxis().SetTitleOffset(1.6) ; frame4.Draw()

c.SaveAs("rf107_plotstyles.png")

if __name__ == "__main__":
rf107_plotstyles()



Loading

0 comments on commit 5a618dd

Please sign in to comment.