# Definition of vector in python to call TMVA functions

**URL:** <https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961>\
**Category:** TMVA\
**Tags:** python\
**Created:** [February 28, 2019, 12:24pm UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961 "2019-02-28T12:24:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delegido](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@Delegido](https://root-forum.cern.ch/u/Delegido)\
**Post date:** [February 28, 2019, 12:24pm UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/1 "2019-02-28T12:24:50Z")

</div>

Hello, I’m trying to run TMVA on my python code but I’m having some problems when sending arguments to TMVA functions because of the definition of the variables. When I write dataloader.AddSignalTestEvent(tvars,event.weight), I do not know how to define tvars in order to make the line work. The following error constantly appears

> TypeError: void TMVA::DataLoader::AddSignalTestEvent(const vector& event, double weight = 1.) =\>  
> could not convert argument 1

I have tried to send a list of floats, a numpy array and a string, but I don’t know what else can I try.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [March 1, 2019, 9:14am UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/2 "2019-03-01T09:14:43Z")

</div>

Hi @Delegido

Unfortunately those conversions you tried (python list, numpy array) are not yet available for vectors. So you need to pass exactly what the function is expecting, which is an std::vector of doubles:

[AddSignalTestEvent](https://root.cern.ch/doc/v612/classTMVA_1_1DataLoader.html#a353f737f5e76411298d76051d998e534) (const std::vector\< [Double\_t](https://root.cern.ch/doc/v612/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) \> &event, [Double\_t](https://root.cern.ch/doc/v612/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) weight=1.0)

You can create an std::vector from python like this:

```auto
v = ROOT.std.vector('double')()

```

and then you can use vector methods to add new elements.

---

<div class="post-metadata">

**Author:** ![Delegido](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@Delegido](https://root-forum.cern.ch/u/Delegido)\
**Post date:** [March 1, 2019, 6:49pm UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/4 "2019-03-01T18:49:41Z")

</div>

Hi @etejedor

Thank you for the answer. That was helpful because now the previous error does not appear any more. Unfortunately, the code now crashes. The code have used

```auto
eventlist = get_event(event, sig_mass)
v = ROOT.std.vector('double')()
for i in range(len(eventlist)):
        v.push_back(eventlist[i])

gRandom = ROOT.TRandom3()
gRandom.SetSeed(event.eventNumber) 
n_entry = gRandom.Rndm()

if (n_entry%2):
        dataloader.AddSignalTestEvent(v,1.0)

```

The error that appears in the crash is the following

```auto
SystemError: void TMVA::DataLoader::AddSignalTestEvent(const vector<double>& event, double weight = 1.) =>
    problem in C++; program state has been reset

```

Do you know what might be the problem? I have checked the input of the double vector and it does not have any strange thing. It it only a double vector.

Thank you for the help

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [March 4, 2019, 9:51am UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/5 "2019-03-04T09:51:12Z")

</div>

Hi @Delegido

I think that issue is related to TMVA, if I run this in C++:

```cpp
TMVA::DataLoader d("dataset");
std::vector<double> v;
v.push_back(1.);
v.push_back(2.);
d.AddSignalTrainingEvent(v, 1.0);

```

I get the same error.

Perhaps @kialbert , @swunsch, @moneta can comment?

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [March 4, 2019, 12:29pm UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/6 "2019-03-04T12:29:30Z")

</div>

Hi,

Try calling `d.AddVariable(”x”);` first so that memory will be allocated for the event first.

(This should give an appropriate error message instead of crashing, thanks for reporting. I will add a JIRA ticket.)

(Finally, please, for c++, use non-managed heap allocated memory for the dataloader since tmva, through the factory, implicitly takes ownership of it. I.e. use `new DataLoader{}`)

Sorry for my brevity. Cheers,  
Kim

Edit:

For reference, here is a working setup (albeit with a _very_ simple dataset) in python:

```auto
import ROOT

d = ROOT.TMVA.DataLoader("dataset")
d.AddVariable("x")
d.AddVariable("y")

for i in range(10):
    # vector has 2 entries, first entry corresponding to variable x,
    # second to variable y.
    v = ROOT.std.vector('double')()
    v.push_back(i*1.0)
    v.push_back(i*2.0)
    d.AddSignalTrainingEvent(v)
    d.AddSignalTestEvent(v)
    
    v2 = ROOT.std.vector('double')()
    v2.push_back(-i*1.0)
    v2.push_back(-i*2.0)
    d.AddBackgroundTrainingEvent(v2)
    d.AddBackgroundTestEvent(v2)

d.PrepareTrainingAndTestTree(ROOT.TCut(""), "")

```

---

<div class="post-metadata">

**Author:** ![Delegido](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@Delegido](https://root-forum.cern.ch/u/Delegido)\
**Post date:** [March 6, 2019, 1:05pm UTC](https://root-forum.cern.ch/t/definition-of-vector-in-python-to-call-tmva-functions/32961/7 "2019-03-06T13:05:44Z")

</div>

Thank you!! Now it is working
