-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
85 changed files
with
9,415 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #101 | ||
// | ||
// Fitting, plotting, toy data generation on one-dimensional p.d.f | ||
// | ||
// pdf = gauss(x,m,s) | ||
// | ||
// | ||
// 07/2008 - Wouter Verkerke | ||
// | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef __CINT__ | ||
#include "RooGlobalFunc.h" | ||
#endif | ||
#include "RooRealVar.h" | ||
#include "RooDataSet.h" | ||
#include "RooGaussian.h" | ||
#include "TCanvas.h" | ||
#include "RooPlot.h" | ||
#include "TAxis.h" | ||
using namespace RooFit ; | ||
|
||
|
||
void rf101_basics() | ||
{ | ||
// S e t u p m o d e l | ||
// --------------------- | ||
|
||
// Declare variables x,mean,sigma with associated name, title, initial value and allowed range | ||
RooRealVar x("x","x",-10,10) ; | ||
RooRealVar mean("mean","mean of gaussian",1,-10,10) ; | ||
RooRealVar sigma("sigma","width of gaussian",1,0.1,10) ; | ||
|
||
// Build gaussian p.d.f in terms of x,mean and sigma | ||
RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ; | ||
|
||
// Construct plot frame in 'x' | ||
RooPlot* xframe = x.frame(Title("Gaussian p.d.f.")) ; | ||
|
||
|
||
// P l o t m o d e l a n d c h a n g e p a r a m e t e r v a l u e s | ||
// --------------------------------------------------------------------------- | ||
|
||
// Plot gauss in frame (i.e. in x) | ||
gauss.plotOn(xframe) ; | ||
|
||
// Change the value of sigma to 3 | ||
sigma.setVal(3) ; | ||
|
||
// Plot gauss in frame (i.e. in x) and draw frame on canvas | ||
gauss.plotOn(xframe,LineColor(kRed)) ; | ||
|
||
|
||
// G e n e r a t e e v e n t s | ||
// ----------------------------- | ||
|
||
// Generate a dataset of 1000 events in x from gauss | ||
RooDataSet* data = gauss.generate(x,10000) ; | ||
|
||
// Make a second plot frame in x and draw both the | ||
// data and the p.d.f in the frame | ||
RooPlot* xframe2 = x.frame(Title("Gaussian p.d.f. with data")) ; | ||
data->plotOn(xframe2) ; | ||
gauss.plotOn(xframe2) ; | ||
|
||
|
||
// F i t m o d e l t o d a t a | ||
// ----------------------------- | ||
|
||
// Fit pdf to data | ||
gauss.fitTo(*data) ; | ||
|
||
// Print values of mean and sigma (that now reflect fitted values and errors) | ||
mean.Print() ; | ||
sigma.Print() ; | ||
|
||
// Draw all frames on a canvas | ||
TCanvas* c = new TCanvas("rf101_basics","rf101_basics",800,400) ; | ||
c->Divide(2) ; | ||
c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ; | ||
c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.6) ; xframe2->Draw() ; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #102 | ||
// | ||
// Importing data from ROOT TTrees and THx histograms | ||
// | ||
// | ||
// | ||
// 07/2008 - Wouter Verkerke | ||
// | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef __CINT__ | ||
#include "RooGlobalFunc.h" | ||
#endif | ||
#include "RooRealVar.h" | ||
#include "RooDataSet.h" | ||
#include "RooDataHist.h" | ||
#include "RooGaussian.h" | ||
#include "TCanvas.h" | ||
#include "RooPlot.h" | ||
#include "TTree.h" | ||
#include "TH1D.h" | ||
#include "TRandom.h" | ||
using namespace RooFit ; | ||
|
||
TH1* makeTH1() ; | ||
TTree* makeTTree() ; | ||
|
||
|
||
void rf102_dataimport() | ||
{ | ||
//////////////////////////////////////////////////////// | ||
// I m p o r t i n g R O O T h i s t o g r a m s // | ||
//////////////////////////////////////////////////////// | ||
|
||
// I m p o r t T H 1 i n t o a R o o D a t a H i s t | ||
// --------------------------------------------------------- | ||
|
||
// Create a ROOT TH1 histogram | ||
TH1* hh = makeTH1() ; | ||
|
||
// Declare observable x | ||
RooRealVar x("x","x",-10,10) ; | ||
|
||
// Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x' | ||
RooDataHist dh("dh","dh",x,Import(*hh)) ; | ||
|
||
|
||
// P l o t a n d f i t a R o o D a t a H i s t | ||
// --------------------------------------------------- | ||
|
||
// Make plot of binned dataset showing Poisson error bars (RooFit default) | ||
RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ; | ||
dh.plotOn(frame) ; | ||
|
||
// Fit a Gaussian p.d.f to the data | ||
RooRealVar mean("mean","mean",0,-10,10) ; | ||
RooRealVar sigma("sigma","sigma",3,0.1,10) ; | ||
RooGaussian gauss("gauss","gauss",x,mean,sigma) ; | ||
gauss.fitTo(dh) ; | ||
gauss.plotOn(frame) ; | ||
|
||
// P l o t a n d f i t a R o o D a t a H i s t w i t h i n t e r n a l e r r o r s | ||
// --------------------------------------------------------------------------------------------- | ||
|
||
// If histogram has custom error (i.e. its contents is does not originate from a Poisson process | ||
// but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead | ||
// (same error bars as shown by ROOT) | ||
RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ; | ||
dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ; | ||
gauss.plotOn(frame2) ; | ||
|
||
// Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used | ||
// in a maximum likelihood fit | ||
// | ||
// A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition | ||
// of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly | ||
// fitted with a chi^2 fit (see rf602_chi2fit.C) | ||
|
||
|
||
//////////////////////////////////////////////// | ||
// I m p o r t i n g R O O T T T r e e s // | ||
//////////////////////////////////////////////// | ||
|
||
|
||
// I m p o r t T T r e e i n t o a R o o D a t a S e t | ||
// ----------------------------------------------------------- | ||
|
||
TTree* tree = makeTTree() ; | ||
|
||
// Define 2nd observable y | ||
RooRealVar y("y","y",-10,10) ; | ||
|
||
// Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars | ||
// is done by name of the branch/RRV | ||
// | ||
// Note that ONLY entries for which x,y have values within their allowed ranges as defined in | ||
// RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15] | ||
// and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree' | ||
|
||
RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ; | ||
|
||
|
||
// P l o t d a t a s e t w i t h m u l t i p l e b i n n i n g c h o i c e s | ||
// ------------------------------------------------------------------------------------ | ||
|
||
// Print number of events in dataset | ||
ds.Print() ; | ||
|
||
// Print unbinned dataset with default frame binning (100 bins) | ||
RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ; | ||
ds.plotOn(frame3) ; | ||
|
||
// Print unbinned dataset with custom binning choice (20 bins) | ||
RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ; | ||
ds.plotOn(frame4,Binning(20)) ; | ||
|
||
// Draw all frames on a canvas | ||
TCanvas* c = new TCanvas("rf102_dataimport","rf102_dataimport",800,800) ; | ||
c->Divide(2,2) ; | ||
c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ; | ||
c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ; | ||
c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ; | ||
c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
TH1* makeTH1() | ||
{ | ||
// Create ROOT TH1 filled with a Gaussian distribution | ||
|
||
TH1D* hh = new TH1D("hh","hh",25,-10,10) ; | ||
for (int i=0 ; i<100 ; i++) { | ||
hh->Fill(gRandom->Gaus(0,3)) ; | ||
} | ||
return hh ; | ||
} | ||
|
||
|
||
TTree* makeTTree() | ||
{ | ||
// Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y | ||
|
||
TTree* tree = new TTree("tree","tree") ; | ||
Double_t* px = new Double_t ; | ||
Double_t* py = new Double_t ; | ||
tree->Branch("x",px,"x/D") ; | ||
tree->Branch("y",py,"y/D") ; | ||
for (int i=0 ; i<100 ; i++) { | ||
*px = gRandom->Gaus(0,3) ; | ||
*py = gRandom->Uniform()*30 - 15 ; | ||
tree->Fill() ; | ||
} | ||
return tree ; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #103 | ||
// | ||
// Interpreted functions and p.d.f.s | ||
// | ||
// | ||
// | ||
// 07/2008 - Wouter Verkerke | ||
// | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef __CINT__ | ||
#include "RooGlobalFunc.h" | ||
#endif | ||
#include "RooRealVar.h" | ||
#include "RooDataSet.h" | ||
#include "RooGaussian.h" | ||
#include "TCanvas.h" | ||
#include "TAxis.h" | ||
#include "RooPlot.h" | ||
#include "RooFitResult.h" | ||
#include "RooGenericPdf.h" | ||
#include "RooConstVar.h" | ||
|
||
using namespace RooFit ; | ||
|
||
|
||
void rf103_interprfuncs() | ||
{ | ||
///////////////////////////////////////////////////////// | ||
// G e n e r i c i n t e r p r e t e d p . d . f . // | ||
///////////////////////////////////////////////////////// | ||
|
||
// Declare observable x | ||
RooRealVar x("x","x",-20,20) ; | ||
|
||
// C o n s t r u c t g e n e r i c p d f f r o m i n t e r p r e t e d e x p r e s s i o n | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
// 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] | ||
// | ||
RooRealVar alpha("alpha","alpha",5,0.1,10) ; | ||
RooGenericPdf genpdf("genpdf","genpdf","(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))",RooArgSet(x,alpha)) ; | ||
|
||
|
||
// S a m p l e , f i t a n d p l o t g e n e r i c p d f | ||
// --------------------------------------------------------------- | ||
|
||
// Generate a toy dataset from the interpreted p.d.f | ||
RooDataSet* data = genpdf.generate(x,10000) ; | ||
|
||
// Fit the interpreted p.d.f to the generated data | ||
genpdf.fitTo(*data) ; | ||
|
||
// Make a plot of the data and the p.d.f overlaid | ||
RooPlot* xframe = x.frame(Title("Interpreted expression pdf")) ; | ||
data->plotOn(xframe) ; | ||
genpdf.plotOn(xframe) ; | ||
|
||
|
||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
// S t a n d a r d p . d . f a d j u s t w i t h i n t e r p r e t e d h e l p e r f u n c t i o n // | ||
// // | ||
// Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian // | ||
// // | ||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
// C o n s t r u c t s t a n d a r d p d f w i t h f o r m u l a r e p l a c i n g p a r a m e t e r | ||
// ------------------------------------------------------------------------------------------------------------ | ||
|
||
// Construct parameter mean2 and sigma | ||
RooRealVar mean2("mean2","mean^2",10,0,200) ; | ||
RooRealVar sigma("sigma","sigma",3,0.1,10) ; | ||
|
||
// Construct interpreted function mean = sqrt(mean^2) | ||
RooFormulaVar mean("mean","mean","sqrt(mean2)",mean2) ; | ||
|
||
// Construct a gaussian g2(x,sqrt(mean2),sigma) ; | ||
RooGaussian g2("g2","h2",x,mean,sigma) ; | ||
|
||
|
||
// G e n e r a t e t o y d a t a | ||
// --------------------------------- | ||
|
||
// Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3 | ||
RooGaussian g1("g1","g1",x,RooConst(10),RooConst(3)) ; | ||
RooDataSet* data2 = g1.generate(x,1000) ; | ||
|
||
|
||
// F i t a n d p l o t t a i l o r e d s t a n d a r d p d f | ||
// ------------------------------------------------------------------- | ||
|
||
// Fit g2 to data from g1 | ||
RooFitResult* r = g2.fitTo(*data2,Save()) ; | ||
r->Print() ; | ||
|
||
// Plot data on frame and overlay projection of g2 | ||
RooPlot* xframe2 = x.frame(Title("Tailored Gaussian pdf")) ; | ||
data2->plotOn(xframe2) ; | ||
g2.plotOn(xframe2) ; | ||
|
||
|
||
// Draw all frames on a canvas | ||
TCanvas* c = new TCanvas("rf103_interprfuncs","rf103_interprfuncs",800,400) ; | ||
c->Divide(2) ; | ||
c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ; | ||
c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.4) ; xframe2->Draw() ; | ||
|
||
|
||
} |
Oops, something went wrong.