Addition and convolution: fitting and plotting in sub ranges

**Author:** Clemens Lange, Wouter Verkerke (C++ version)

*This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, July 05, 2020 at 08:19 AM.*

In [ ]:

```
from __future__ import print_function
import ROOT
```

Construct observables x

In [ ]:

```
x = ROOT.RooRealVar("x", "x", -10, 10)
```

Construct gaussx(x,mx,1)

In [ ]:

```
mx = ROOT.RooRealVar("mx", "mx", 0, -10, 10)
gx = ROOT.RooGaussian("gx", "gx", x, mx, ROOT.RooFit.RooConst(1))
```

px = 1 (flat in x)

In [ ]:

```
px = ROOT.RooPolynomial("px", "px", x)
```

model = f*gx + (1-f)px

In [ ]:

```
f = ROOT.RooRealVar("f", "f", 0., 1.)
model = ROOT.RooAddPdf(
"model", "model", ROOT.RooArgList(gx, px), ROOT.RooArgList(f))
```

Generated 10000 events in (x,y) from p.d.f. model

In [ ]:

```
modelData = model.generate(ROOT.RooArgSet(x), 10000)
```

Fit p.d.f to all data

In [ ]:

```
r_full = model.fitTo(modelData, ROOT.RooFit.Save(ROOT.kTRUE))
```

Define "signal" range in x as [-3,3]

In [ ]:

```
x.setRange("signal", -3, 3)
```

Fit p.d.f only to data in "signal" range

In [ ]:

```
r_sig = model.fitTo(modelData, ROOT.RooFit.Save(
ROOT.kTRUE), ROOT.RooFit.Range("signal"))
```

Make plot frame in x and add data and fitted model

In [ ]:

```
frame = x.frame(ROOT.RooFit.Title("Fitting a sub range"))
modelData.plotOn(frame)
model.plotOn(
frame, ROOT.RooFit.Range("Full"), ROOT.RooFit.LineStyle(
ROOT.kDashed), ROOT.RooFit.LineColor(
ROOT.kRed)) # Add shape in full ranged dashed
model.plotOn(frame) # By default only fitted range is shown
```

Print fit results

In [ ]:

```
print("result of fit on all data ")
r_full.Print()
print("result of fit in in signal region (note increased error on signal fraction)")
r_sig.Print()
```

Draw frame on canvas

In [ ]:

```
c = ROOT.TCanvas("rf203_ranges", "rf203_ranges", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()
c.SaveAs("rf203_ranges.png")
```

Draw all canvases

In [ ]:

```
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()
```