# TMVA training with multiple backgrounds and prepared training/test samples

**URL:** <https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746>\
**Category:** TMVA\
**Created:** [September 8, 2021, 8:46pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746 "2021-09-08T20:46:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgoncerz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@mgoncerz](https://root-forum.cern.ch/u/mgoncerz)\
**Post date:** [September 8, 2021, 8:46pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/1 "2021-09-08T20:46:38Z")

</div>

Hello!

I’m trying to setup a training with signal and multiple weighted backgrounds, but I’m really confused with how the PrepareTrainingAndTestTree method should be called. I already have them split in testing and training ntuples.

Let’s assume for simplicity that they are signal\_train, signal\_test, background1\_train, background1\_test, background2\_train and background2\_test. I load them using:

```
dataloader->AddSignalTree(signal_train, 1, TMVA::Types::kTraining);
dataloader->AddSignalTree(signal_test, 1, TMVA::Types::kTesting);
dataloader->AddBackgroundTree(background1_train, 0.5, TMVA::Types::kTraining);
dataloader->AddBackgroundTree(background1_test, 0.5, TMVA::Types::kTesting);
dataloader->AddBackgroundTree(background2_train, 0.5, TMVA::Types::kTraining);
dataloader->AddBackgroundTree(background2_test, 0.5, TMVA::Types::kTesting);

```

How do I make sure that all of the samples are fully used and properly assigned to training/testing (without additional implicit splitting)? Would it simply be:

```
TCut mycuts = "";
TCut mycutb = "";  
dataloader->PrepareTrainingAndTestTree(mycuts, mycutb, "NormMode=NumEvents:!V");

```

?

Thanks!

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [September 9, 2021, 3:05pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/2 "2021-09-09T15:05:48Z")

</div>

Hi, @mgoncerz,

If you want to discriminate multiple sources of background, you might consider a multiclass approach, where instead of training “Signal” and “Background” samples, you train a number of arbitrarily named samples. See [this tutorial](https://root.cern/doc/v624/TMVAMulticlass_8C.html) for training and [this one](https://root.cern/doc/v624/TMVAMulticlassApplication_8C.html) for application. You can also consider what is done in [this other tutorial](https://root.cern/doc/v624/TMVAMultipleBackgroundExample_8C.html) for training multiple backgrounds.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![mgoncerz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@mgoncerz](https://root-forum.cern.ch/u/mgoncerz)\
**Post date:** [September 9, 2021, 8:58pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/3 "2021-09-09T20:58:50Z")

</div>

Dear @mwilkins, thank you for suggestions. I think that the multiclass approach is a bit of an overkill for my case. In the end I am only interested in a simple BDT trained to discriminate signal against a combined background from multiple sources.

I can, of course, prepare such a combined background sample manually, but I’ve assumed that TMVA may have some way of handling that built in.

I would still like to prepare the training and testing sub-samples myself though, so that I can do the actual analysis outside of TMVA GUI. Unfortunately, I can’t really find any clear example of it being done.

What I’m mostly confused by is whether I should call PrepareTrainingAndTestTree() after I have added appropriate trees using TMVA::Types::kTraining and TMVA::Types::kTesting, as it seems to be doing the splitting as well.

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [September 9, 2021, 9:55pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/4 "2021-09-09T21:55:47Z")

</div>

Hi, @mgoncerz,

Okay, I think I understand your question now. I’m actually not sure you need to call `PrepareTrainingAndTestTree` at all… Glancing at the source code, it looks like calling `AddSignalTree` and `AddBackgroundTree`, as you’ve done, already accomplish what you need, but I should say I am not an expert here. Perhaps try running your code without the `PrepareTrainingAndTestTree` line and see if it behaves as you expect.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [September 9, 2021, 11:29pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/5 "2021-09-09T23:29:53Z")

</div>

Hi @mgoncerz,

> [@mgoncerz](#):
>
> What I’m mostly confused by is whether I should call PrepareTrainingAndTestTree() after I have added appropriate trees using TMVA::Types::kTraining and TMVA::Types::kTesting, as it seems to be doing the splitting as well.

What I suggest is to invite @moneta to this topic; he might provide some hints regarding the issue.

Cheers,  
J.

---

<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:** [September 28, 2021, 3:38pm UTC](https://root-forum.cern.ch/t/tmva-training-with-multiple-backgrounds-and-prepared-training-test-samples/46746/6 "2021-09-28T15:38:48Z")

</div>

Hi,

I think if you are doing yourself the splitting by adding the trees with the right training/testing flags you should not need to call `PrepareTrainingAndTestTree`. I have not tested this workflow, so in case it does not work, please let me know

Cheers

Lorenzo
