# Facing error when using RDF to deal with TMVA .xml file via PyROOT

**URL:** <https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298>\
**Category:** TMVA\
**Tags:** python, rdataframe\
**Created:** [July 23, 2024, 10:21am UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298 "2024-07-23T10:21:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [July 23, 2024, 10:21am UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/1 "2024-07-23T10:21:03Z")

</div>

Dear experts,  
When I try to use RDF in PyROOT to deal with TMVA .xml file, I face a strange error that it can not find computeModel from ROOT. I find other answer in the forum using almost the same script as mine do not face the same error. I am really confused of it. Here are the error message and my script.

In addition, I am sure BDT.xml file could be read successfully because `variables` can be defined correctly.

` raise AttributeError("Failed to get attribute {} from ROOT".format(name)) AttributeError: Failed to get attribute computeModel from ROOT`

```python
import ROOT 
ROOT.gInterpreter.ProcessLine('''
TMVA::Experimental::RReader model("BDT.xml");
auto computeModel = TMVA::Experimental::Compute<21, float>(model);
''')
variables = ROOT.model.GetVariableNames()
rdf = ROOT.RDataFrame('DecayTree', 'data.root')
c1 = ROOT.TCanvas()
df = rdf.Define('y', ROOT.computeModel, variables)

```

Thank you so much for considering my problem!

Best regards,  
Linnuo

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 23, 2024, 12:05pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/2 "2024-07-23T12:05:26Z")

</div>

Hi @Linnuo!

Is there a reason why you use the `gInterpreter`? Why not use PyROOT directly for everything? Maybe that works. Let us know!

```python
import ROOT 

model = ROOT.TMVA.Experimental.RReader("BDT.xml")
variables = ROOT.model.GetVariableNames()
rdf = ROOT.RDataFrame('DecayTree', 'data.root')
df = rdf.Define('y',
                ROOT.TMVA.Experimental.Compute[21, "float"](model),
                variables)

```

---

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [July 23, 2024, 4:37pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/3 "2024-07-23T16:37:12Z")

</div>

Dear @jonas ,  
I use` gInterpreter` because I find someting wrong when directly use PyROOT, and I find instruction on the following topics “~rdataframe-and-tmva-in-pyroot/38930” “~/t/evaluating-mva-within-root-data-frame/49238/4”  
Sorry for not putting the whole links because I cannot post my reply with them.

I try your advice, but still face the same problem.

> Traceback (most recent call last):  
> File “TMVA\_python.py”, line 12, in   
> variables = ROOT.model.GetVariableNames()  
> ^^^^^^^^^^  
> File “/cvmfs/lhcbdev.cern.ch/conda/envs/default/2024-07-10\_13-01/linux-64/lib/python3.12/site-packages/ROOT/\_facade.py”, line 236, in \_getattr  
> return getattr(self, name)  
> ^^^^^^^^^^^^^^^^^^^  
> File “/cvmfs/lhcbdev.cern.ch/conda/envs/default/2024-07-10\_13-01/linux-64/lib/python3.12/site-packages/ROOT/\_facade.py”, line 164, in \_fallback\_getattr  
> raise AttributeError(“Failed to get attribute {} from ROOT”.format(name))  
> AttributeError: Failed to get attribute model from ROOT. Did you mean: ‘module’?

```auto
import ROOT 
model = ROOT.TMVA.Experimental.RReader("BDT.xml")
variables = ROOT.model.GetVariableNames()
rdf = ROOT.RDataFrame('DecayTree', 'data.root')
df = rdf.Define('y',
                ROOT.TMVA.Experimental.Compute[21, "float"](model),
                variables)

```

Best,  
Linnuo

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 23, 2024, 5:57pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/4 "2024-07-23T17:57:56Z")

</div>

At this point, `model` is a Python variable. Did you try:

```python
variables = model.GetVariableNames()

```

?

---

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [July 24, 2024, 12:45pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/5 "2024-07-24T12:45:53Z")

</div>

Dear @jonas ,  
Thank you so much! It works now.  
However, I meet a new problem that if the variables contains formula like “log(IPCHI2)”, it do not work.(IPCHI2 works).

> variables = [“log(IPCHI2)”, “IPCHI2”]

The error message is

> ```
> runtime_error: Unknown column: "log(IPCHI2)"
> 
> ```

log(IPCHI2) cannot be recognized. How can I include formula into variables name?

Best,  
Linnuo

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 28, 2024, 1:01pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/6 "2024-07-28T13:01:08Z")

</div>

Hello! Where did you read that using a formula in the variable name is supported? I think it’s not supported actually.

---

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [July 30, 2024, 10:19am UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/7 "2024-07-30T10:19:22Z")

</div>

Dear @jonas ,  
I think it is supported in ROOT using C++. But this is not really important. I define new columns in my script.  
However, I meet a strange error when deal with MLP model. I am really confused of it. It works when I use .xml file from BDT model, but now, it failed.

> model = ROOT.TMVA.Experimental.RReader(“MLP.xml”)  
> variables =[“…”]  
> df = rdf.Define(‘MLP\_value’, ROOT.TMVA.Experimental.Compute20, “float”,variables)  
> h = df.Histo1D(‘MLP\_value’)  
> h.Draw()

And the error message is

> RDataFrame::Run: event loop was interrupted  
> Traceback (most recent call last):  
> File"TMVA.py", line 46, in   
> h.Draw()  
> ^^^^^^  
> cppyy.gbl.std.runtime\_error: TH1D& ROOT::RDF::RResultPtr::operator\*() =\>  
> runtime\_error: Size of input vector is not equal to number of variables.

Best,  
Linnuo

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [August 2, 2024, 6:55am UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/8 "2024-08-02T06:55:57Z")

</div>

Hi! I can help you with the Python-related problems, but I know too little about TMVA to answer this questions.

Maybe our TMVA expert, @moneta, has some advice?

---

<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:** [August 7, 2024, 12:57pm UTC](https://root-forum.cern.ch/t/facing-error-when-using-rdf-to-deal-with-tmva-xml-file-via-pyroot/60298/9 "2024-08-07T12:57:03Z")

</div>

Hi,  
It looks like the number of variables you are passsing in the ‘Compute’ functon (20) does not match the number of variables you pass in the Define function.  
If this is not the case, can you please post your running code, including the input data.

Best,

Lorenzo
