# Difference: SWGuideRooFitExamples (6 vs. 7)

#### Revision 72009-05-27 - LucaLista

Line: 1 to 1

 META TOPICPARENT name="SWGuideRooFit"
`<!-- PDFSTART -->`

# RooFit Examples in CMSSW

Line: 18 to 18
addpkg PhysicsTools/CandExamples V02-00-00 cd PhysicsTools/CandExamples scram b
Changed:
<
<
\$CMSSW_BASE/test/slc4_ia32_gcc345/<name of the application<
>
>
\$CMSSW_BASE/test/slc4_ia32_gcc345/

## Building RooFit applications in CMSSW

Line: 34 to 34

### Code fragments

Define the variable (`x`) and the parameters of interest (`mu`, `sigma`, `lambda`) with their validity range:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooRealVar x("x", "x", -10, 10);

RooRealVar mu("mu", "average", 0, -1, 1); RooRealVar sigma("sigma", "sigma", 1, 0, 5); RooGaussian gauss("gauss","gaussian PDF", x, mu, sigma); RooRealVar lambda("lambda", "slope", -0.1, -5., 0.);

Changed:
<
<
>
>
%ENDSYNTAX%
Define a Gaussian and an Exponential PDF according to the above parameters:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooGaussian gauss("gauss","gaussian PDF", x, mu, sigma); RooExponential expo("expo", "exponential PDF", x, lambda);
Changed:
<
<
>
>
%ENDSYNTAX%
Define a linar combination of the two PDFs with yields `s` (signal) and `b` (background):
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooRealVar s("s", "signal yield", 100, 0, 10000); RooRealVar b("b", "background yield", 100, 0, 10000);

RooAddPdf sum("sum", "gaussian plus exponential PDF", RooArgList(gauss, expo), RooArgList(s, b));

Changed:
<
<
>
>
%ENDSYNTAX%
Generate a mixed signal plus background sample with 2000 entries:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooDataSet * data = sum.generate(x, 2000);
Changed:
<
<
>
>
%ENDSYNTAX%
Fit the sample according to the PDF model using Extended Unbinned Maximum Likelihood method:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
sum.fitTo(*data, RooFit::Extended());
Changed:
<
<
>
>
%ENDSYNTAX%
Plot the fit result and save the output as EPS file:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooPlot * xFrame = x.frame() ; data->plotOn(xFrame) ; sum.plotOn(xFrame) ;
Line: 78 to 78
TCanvas c; xFrame->Draw(); c.SaveAs("extendedLikelihoodFit.eps");
Changed:
<
<
>
>
%ENDSYNTAX%

### Result

Below the plot resulting from running the above example
Line: 95 to 95
In order to defined the binned histogram, one has to use the `RooDataHist` class, setting the proper binning for the `x` variable:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
x.setBins(50); RooDataHist hist("hist", "hist", RooArgSet(x), *data);
Changed:
<
<
>
>
%ENDSYNTAX%
Then, the chi-squared fit should be used instead of the likelihood fit. Notice, that if the yields should be fitted, an extra parameter (similar to the "extended" option in the likelihood fit) should be set to true in `RooChi2Var`:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooChi2Var chi2("chi2","chi2", sum, hist, true); RooMinuit minuit(chi2); minuit.migrad(); minuit.hesse();
Changed:
<
<
>
>
%ENDSYNTAX%
All the rest of the code remains unchanged.
Line: 131 to 131
sets to a single dimension using `RooPlot`, or as two-dimensional standard ROOT contour plots:
Changed:
<
<
>
>
%SYNTAX{ syntax="cpp"}%
RooRealVar x("x", "x", -10, 10); RooRealVar y("y", "y", -10, 10); RooRealVar mux("mux", "average-x'", 0, -1, 1);
Line: 155 to 154
RooArgList(gaussxy, expoxy), RooArgList(s, b)); RooDataSet * data = sum.generate(RooArgSet(x,y), 250); RooFitResult* result = sum.fitTo(*data, RooFit::Extended(), RooFit::Minos(), RooFit::Save());
Changed:
<
<
assert(result = 0);
>
>
result->Print();

RooPlot * xFrame = x.frame() ;

Line: 181 to 180
TH2 * surfData = data->createHistogram(x, y, 100, 100, "", "surfData"); surfData->Draw("contz"); c.SaveAs("twoDimensionalFit-xyDataContour.eps");
Changed:
<
<
>
>
%ENDSYNTAX%

### Complete example

• `PhysicsTools/CandExamples/test/twoDimensionalFit.cpp`
Line: 199 to 197

 META FILEATTACHMENT attachment="binnedChi2Fit.eps" attr="h" comment="" date="1243412330" name="binnedChi2Fit.eps" path="binnedChi2Fit.eps" size="20490" stream="binnedChi2Fit.eps" user="Main.LucaLista" version="1" attachment="binnedChi2Fit.gif" attr="h" comment="" date="1243412343" name="binnedChi2Fit.gif" path="binnedChi2Fit.gif" size="12837" stream="binnedChi2Fit.gif" user="Main.LucaLista" version="1" attachment="style.css" attr="h" comment="" date="1243429050" name="style.css" path="style.css" size="257" stream="style.css" user="Main.LucaLista" version="1"