# Using a RooBinnedCategory for a simultaneous fit

**URL:** <https://root-forum.cern.ch/t/using-a-roobinnedcategory-for-a-simultaneous-fit/58379>\
**Category:** Roofit and RooStats\
**Tags:** roofit\
**Created:** [March 4, 2024, 12:07pm UTC](https://root-forum.cern.ch/t/using-a-roobinnedcategory-for-a-simultaneous-fit/58379 "2024-03-04T12:07:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmassa](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/258eb7/32.png) [@lmassa](https://root-forum.cern.ch/u/lmassa)\
**Post date:** [March 4, 2024, 12:07pm UTC](https://root-forum.cern.ch/t/using-a-roobinnedcategory-for-a-simultaneous-fit/58379/1 "2024-03-04T12:07:59Z")

</div>

Dear experts,  
I am trying to perform a simultaneous fit (on the variable ΔM) of a sample that I split into bins (of a different variable, cosθ\*). To bin the sample in cosθ\* I used the RooBinnedCategory class.

```python
DM = ROOT.RooRealVar("DM", "DeltaM", 140, 160)
cos_theta_real = ROOT.RooRealVar("cos_theta_real", "cos_theta_real", -1, 1)
cos_theta_real.setBins(8, "cos_theta_bins")
data = ROOT.RooDataSet("data", "data", {DM, cos_theta_real}, ImportFromFile=("input.root", "tree"))
cos_theta_bin = ROOT.RooBinningCategory(
    "cos_theta_bin", "cos#theta* bin", cos_theta_real, "cos_theta_bins", "bin")
data.addColumn(cos_theta_bin)
cos_theta_table = data.table(cos_theta_bin)
cos_theta_table.Print("v")

```

So far so good, the output I get makes sense

```auto
[#0] WARNING:DataHandling -- RooDataSetHelper::Finalize(baseData) Ignored 2245 out-of-range events

  Table cos_theta_bin : data
  +------+-------+
  | bin0 | 15238 |
  | bin1 | 36386 |
  | bin2 | 36376 |
  | bin3 | 35657 |
  | bin4 | 38457 |
  | bin5 | 41304 |
  | bin6 | 40966 |
  | bin7 | 20241 |
  +------+-------+

```

However, when I try to create a RooSimultaneous like this

```python
mu = ROOT.RooRealVar("mu", "#mu", 145.4258, 145, 146, "MeV/c^{2}")
pdfs = {} # {bin_name: RooAbsPdf}
for i in range(8): # Loop over cosθ* bins
    suffix = f"_{i}"
    fT = ROOT.RooRealVar("fT" + suffix, f"f_{{J,{i}}}", 0.5, 0.05, 0.95)
    lam = ROOT.RooRealVar("lam" + suffix, f"#lambda_{{{i}}}", 0.3, 0.01, 10, "MeV/c^{2}")
    gamma = ROOT.RooRealVar("gamma" + suffix, f"#gamma_{{{i}}}", 0, -0.4, 0.4)
    delta = ROOT.RooRealVar("delta" + suffix, f"#delta_{{{i}}}", 1.0, 0.5, 3)
    sigmaG = ROOT.RooRealVar("sigmaG" + suffix, f"#sigma_{{G,{i}}}", 0.2, 0.01, 1, "MeV/c^{2}")
    n = ROOT.RooRealVar("n" + suffix, f"n_{{{i}}}", 5000, 1000, 30000)
    # PDF for the bin
    sigTail = ROOT.RooJohnson("sigTail" + suffix, "sigTail" + suffix, DM, mu, lam, gamma, delta)
    sigPeak = ROOT.RooGaussian("sigPeak" + suffix, "sigPeak" + suffix, DM, mu, sigmaG)
    sig = ROOT.RooAddPdf("sig", "sig", [sigTail, sigPeak], fT)
    sigCount = ROOT.RooExtendPdf(f"sigCount_{i}", f"sigCount_{i}", sig, n)
    bin_name = f"bin{i}"
    pdfs[bin_name] = sigCount
    # Prevent objects from being deleted when the variable is reused
    ROOT.SetOwnership(fT, False)
    ROOT.SetOwnership(lam, False)
    ROOT.SetOwnership(gamma, False)
    ROOT.SetOwnership(delta, False)
    ROOT.SetOwnership(sigmaG, False)
    ROOT.SetOwnership(n, False)
    ROOT.SetOwnership(sigTail, False)
    ROOT.SetOwnership(sigPeak, False)
    ROOT.SetOwnership(sig, False)

simPdf = ROOT.RooSimultaneous("simPdf", "simPdf", pdfs, cos_theta_bin)

```

I get the following error

```auto
Traceback (most recent call last):
  File "/home/lmassa/Documents/b2/d02pipipi0_tests/offline/./fit_simultaneous.py", line 121, in <module>
    simPdf = ROOT.RooSimultaneous("simPdf", "simPdf", pdfs, cos_theta_bin)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ROOT/_pythonization/_roofit/_roosimultaneous.py", line 41, in __init__
    self._init(*args)
TypeError: none of the 5 overloaded methods succeeded. Full details:
  RooSimultaneous::RooSimultaneous(const char* name, const char* title, const RooArgList& pdfList, RooAbsCategoryLValue& indexCat) =>
    TypeError: could not convert argument 3
  RooSimultaneous::RooSimultaneous(const char* name, const char* title, RooAbsCategoryLValue& indexCat) =>
    TypeError: takes at most 3 arguments (4 given)
  RooSimultaneous::RooSimultaneous(const char* name, const char* title, map<string,RooAbsPdf*> pdfMap, RooAbsCategoryLValue& inIndexCat) =>
    TypeError: could not convert argument 4
  RooSimultaneous::RooSimultaneous(const RooSimultaneous& other, const char* name = nullptr) =>
    TypeError: takes at most 2 arguments (4 given)
  RooSimultaneous::RooSimultaneous() =>
    TypeError: takes at most 0 arguments (4 given)

```

I believe this is due to the fact that RooBinningCategory does not inherit from RooAbsCategoryLValue, but I do not know how to proceed in order to use this binning for my simultaneous fit.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [March 4, 2024, 7:23pm UTC](https://root-forum.cern.ch/t/using-a-roobinnedcategory-for-a-simultaneous-fit/58379/2 "2024-03-04T19:23:57Z")

</div>

Dear Ludovico,

Thanks for the post and welcome to the ROOT community!

I would defer to @moneta and @jonas , our RooFit experts.

Best,  
Danilo

---

<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:** [March 18, 2024, 7:24pm UTC](https://root-forum.cern.ch/t/using-a-roobinnedcategory-for-a-simultaneous-fit/58379/3 "2024-03-18T19:24:42Z")

</div>

Closed on March 18, 2024, 7:24pm UTC

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