# Get normalization factor of the plotted PDF

**URL:** <https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822>\
**Category:** Roofit and RooStats\
**Tags:** roofit, root\
**Created:** [January 12, 2024, 8:50am UTC](https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822 "2024-01-12T08:50:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iaosiiuv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/82dd89/32.png) [@iaosiiuv](https://root-forum.cern.ch/u/iaosiiuv)\
**Post date:** [January 12, 2024, 8:50am UTC](https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822/1 "2024-01-12T08:50:25Z")

</div>

Dear experts,

I would like to know how many events Roofit used to renormalize the PDF when plotted, or I want to get directly the normalization factor. I don’t know how to get this information, and I explain below the case I am dealing with, which is not trivial.

I have a simple TDCPV example, using a `RooBCPGenDecay` to model the signal, and a simple gaussian for the background. I combined them using `RooAddPdf`:

```auto
# Background PDF
mgBkg = r.RooRealVar("mgBkgB", "mg", 0)
sgBkg = r.RooRealVar("sgBkg1", "sg2", 4.3)
bkgPDF = r.RooGaussian("bkgPDF", "gauss", dt, mgBkg, sgBkg)

# BCPGenDecay model
BCPGenDecayPDF = r.RooBCPGenDecay("BCPGenDecayPDF", "BCPGenDecayPDF", dt, q, tau, dm, w, Agen, Sgen, dw, effR, signalRes, r.RooBCPGenDecay.DoubleSided)

# Combined PDF
combinedPDF = r.RooAddPdf("combinedPDF", "combinedPDF", r.RooArgList(BCPGenDecayPDF, bkgPDF), r.RooArgList(fsig))

```

Now, I generate from this combination 100000 events, which depend on the flavor `q` (a category, `B0` or `B0bar`):

```auto
nevents = 100000
data_set = combinedPDF.generate(r.RooArgSet(dt, q), nevents)

```

and then I only plot the `B0` category:

```auto
frame = dt.frame(nbins)
data_set.plotOn(frame, Cut="q==q::B0", MarkerColor=r.kBlue)
combinedPDF.plotOn(frame, Slice=(q, "B0"), Components="BCPGenDecayPDF", LineStyle=r.kDashed, LineColor=r.kGreen)
combinedPDF.plotOn(frame, Slice=(q, "B0"), Components="bkgPDF", LineStyle=r.kDashed, LineColor=r.kRed)

```

Now, the renormalization used by the `Slice` method of `plotOn` is trivial for the gaussian background component, but not for the CP asymmetry part.

For the background component, which doesn’t depend on the flavor, I get the correct normalization value with:

```auto
q.setLabel("B0")
xlim = (dt.getMin(), dt.getMax())
x = np.linspace(*xlim, 10000)
for i in range(len(x)):
     var.setVal(x[i])
     bkgPDF.getVal(var)*norm

```

with

```auto
norm = n_expected_events_for_bkgPDF * bin_width / bkgPDF_integral
     = (n_B0_events * (1 - f_sig)) * bin_width / 1
     = (nevents/2 * (1 - f_sig)) * bin_width / 1

```

This was tested and works wonderfully.

For `BCPGenDecayPDF`, this is not trivial, as we don’t have

```auto
n_expected_events_for_BCPGenDecayPDF = nevents/2 * f_sig

```

The dependencies on the miss-tagging `w`, the asymmetry parameters `A` and `S` or the misstag rate `dw` makes `n_expected_events_for_BCPGenDecayPDF` go a bit higher or lower. All those parameters are fixed before using the same method to get the correct values for the gaussian background, and somehow it is always a bit off by a factor between 0.8 and 1.1 from my tests.

I tried to get information on normalization using `combinedPDF.getComponents()`, or I tried to get information of the `RooPlot` output when I plot `BCPGenDecayPDF` (`plot.getHist(plot.nameOf(1))` returns a null pointer, or `plot.getFitRangeNEvt()` returns 10000) without any success.

Any suggestions on how to get this correct normalization factor?

Best regards

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 12, 2024, 4:50pm UTC](https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822/2 "2024-01-12T16:50:46Z")

</div>

@jonas can probably help you on this

---

<div class="post-metadata">

**Author:** ![iaosiiuv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/82dd89/32.png) [@iaosiiuv](https://root-forum.cern.ch/u/iaosiiuv)\
**Post date:** [January 16, 2024, 5:47am UTC](https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822/3 "2024-01-16T05:47:16Z")

</div>

Small update: I managed to correctly get the correct number for `n_expected_events_for_BCPGenDecayPDF`, which is:

```auto
n_expected_events_for_BCPGenDecayPDF = n_true_B0_events - n_expected_events_for_bkgPDF

```

with `n_true_B0_events` equal to the number of `B0` events actually generated in the `data_set`. It differs from the `n_B0_events` defined in my first post, which is the theoretical number of `B0` generated (without CP asymmetry) equal to `nevents/2`.

This solution gives in the end the correct normalization factor. It works because the background component doesn’t depend on the CP asymmetry.

For more complex examples, I still don’t know how I can get the normalization (or just the integral) of the plotted function by `plotOn`.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 30, 2024, 5:47am UTC](https://root-forum.cern.ch/t/get-normalization-factor-of-the-plotted-pdf/57822/4 "2024-01-30T05:47:23Z")

</div>

Closed on January 30, 2024, 5:47am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
