 TWiki> CMSPublic Web>SWGuide>SWGuidePhysicsTools>SWGuideRooFit>SWGuideRooFitExamples (revision 3)EditAttachPDF

# RooFit Examples in CMSSW

Complete: ## Introduction

This page is intended to provide examples with RooFit that are usable within CMSSW.

## Building RooFit applications in CMSSW

Before using the following examples, you should first understand how to build a RooFit application under CMSSW. This part is described in the following page:

## Simple Maximum Likelihood fit with Toy Monte Carlo generation

Generate a Toy Monte Carlo sample according to a PDF modeled as Exponential plus a Gaussian, and perform an Extended Maximum Likelihood fit according to the same model.

### Code fragments

Define the variable (`x`) and the parameters of interest (`mu`, `sigma`, `lambda`) with their validity range:
```  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.);
```

Define a Gaussian and an Exponential PDF according to the above parameters:

```  RooGaussian gauss("gauss","gaussian PDF", x, mu, sigma);
RooExponential expo("expo", "exponential PDF", x, lambda);
```

Define a linar combination of the two PDFs with yields `s` (signal) and `b` (background):

```  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));
```

Generate a mixed signal plus background sample with 2000 entries:

```  RooDataSet * data = sum.generate(x, 2000);
```

Fit the sample according to the PDF model using Extended Unbinned Maximum Likelihood method:

```  sum.fitTo(*data, RooFit::Extended());
```

Plot the fit result and save the output as EPS file:

```  RooPlot * xFrame = x.frame() ;
data->plotOn(xFrame) ;
sum.plotOn(xFrame) ;
sum.plotOn(xFrame, RooFit::Components(expo), RooFit::LineStyle(kDashed)) ;
TCanvas c;
xFrame->Draw();
c.SaveAs("extendedLikelihoodFit.eps");
```

### Result

Below the plot resulting from running the above example

### Complete example

• `PhysicsTools/CandExamples/test/extendedLikelihoodFit.cpp `

## Simple Chi-squared fit

Using the same PDF defined in the previous example, we can also adopt a binned chi-squared fit.

In order to defined the binned histogram, one has to use the `RooDataHist` class, setting the proper binning for the `x` variable:

```  x.setBins(50);
RooDataHist hist("hist", "hist", RooArgSet(x), *data);
```

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`:

```  RooChi2Var chi2("chi2","chi2", sum, hist, true);
RooMinuit minuit(chi2);
minuit.hesse();
```

All the rest of the code remains unchanged.

### Results

Below the plot resulting from running the above example

### Complete example

• `PhysicsTools/CandExamples/test/binnedChi2Fit.cpp `

## More examples

You can find more examples in the following pages:

Edit | Attach | Watch | Print version |  | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r3 - 2009-05-27 - LucaLista Log In

Create a LeftBar  Cern Search TWiki Search Google Search CMSPublic All webs Copyright &© 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback