# Adding treess to dataloader with weights does not work (for me)

**URL:** <https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038>\
**Category:** TMVA\
**Created:** [November 24, 2017, 4:51pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038 "2017-11-24T16:51:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![a.bragagnolo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@a.bragagnolo](https://root-forum.cern.ch/u/a.bragagnolo)\
**Post date:** [November 24, 2017, 4:51pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/1 "2017-11-24T16:51:43Z")

</div>

Hi,  
my problem is very simple when I add a tree to the dataloader the weight parameters does nothing.

In other words this:

```auto
TTree * tree = (TTree*)file ->Get("tree");

dataloader->AddSignalTree( tree1 );
dataloader->AddBackgroundTree( tree1 );

TCut mycuts = "some_signal_cuts" ;
TCut mycutb = "some_background_cuts";
dataloader->PrepareTrainingAndTestTree( mycuts, mycutb, "SplitMode=Random:V");

```

is completely identical to this:

```auto
TTree * tree = (TTree*)file ->Get("tree");

dataloader->AddSignalTree( tree1, some_number );
dataloader->AddBackgroundTree( tree1, some_other_number );
TCut mycuts = "some_signal_cuts" ;
TCut mycutb = "some_background_cuts";
dataloader->PrepareTrainingAndTestTree( mycuts, mycutb, "SplitMode=Random:V");

```

This is how I define factory and dataloader.

```auto

std::string factoryOptions( "!V:!Silent:Color:DrawProgressBar:AnalysisType=Classification" );
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile, factoryOptions );

TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");

```

This force me to manually select the number of signal events and background events with nTrain\_Signal and the other similar options, or with SigToBkgFraction (available only for BDTs). This is very tedious for various reasons.

My feeling is that the problem is that I load the same events both in the background tree and in the signal one, and then later I define signal and background with cuts.

Any help is welcome,

Alberto

---

<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:** [November 28, 2017, 9:59pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/2 "2017-11-28T21:59:34Z")

</div>

This sounds weird to me. There should be a variable `weight` in your Training and Test trees in the generated root file. These _should_ have differing values if your tree weights are different.

I’ll look into it as soon as I have some time.

A tedious workaround would be to generate and prepare the data set externally with pre-multiplied weights.

Cheers,  
Kim

---

<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:** [November 29, 2017, 3:00pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/3 "2017-11-29T15:00:49Z")

</div>

I cannot reproduce your problem. The appended file should mimic your situation, please let me know if this is not the case.

Running with

```auto
root -l -q MinimalClassification.C

```

and

```auto
rootbrowse out.root

```

and checking both the Train and Test trees, these have the correct weight distribution. Please do note that the weights need not be the same for both trees due to renormalisation of the training data.

This leads me to believe the issue lies outside of TMVA. Sorry I cannot give you a more conclusive answer.

Cheers,  
Kim

[MinimalClassification.C](https://root-forum.cern.ch/uploads/default/original/2X/7/7c163aa254c1c3524c8e152ed47044e50da1803a.C) (1.3 KB)

---

<div class="post-metadata">

**Author:** ![a.bragagnolo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@a.bragagnolo](https://root-forum.cern.ch/u/a.bragagnolo)\
**Post date:** [December 5, 2017, 10:59am UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/4 "2017-12-05T10:59:34Z")

</div>

Hi Kim,  
I checked the Train and Test trees. The test tree has the correct weights, but the train tree has always the background weight set such has the number of effective bkg events is equal to the sgn events.

In other words the train tree always behave as if something `SigToBkgFraction` is set to one. In this way the weights with which I add the trees at the dataloader are completely meaningless and the training is not influenced by them.  
The strange things is that (to my knowledge) `SigToBkgFraction` is an option for the BDT method, but I’m seeing this behavior in other methods (for example MLP).

[Here](https://www.dropbox.com/s/85jasd59ohfdnq1/TMVA_example_weights.root?dl=0) you can find an output file of a trained MLP with signal waight equal to 1 and background weight equal to 5.  
You can clearly see that the bkg has a weight around 2.5:

 ![ROOT Object Browser_145](https://root-forum.cern.ch/uploads/default/original/2X/b/b099e631f974d299ce24b59f23a4eae0aee35746.png)

Cheers,  
Alberto

---

<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:** [December 5, 2017, 12:02pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/5 "2017-12-05T12:02:38Z")

</div>

Hi,

Try adding the following option to your dataloader options. This removes the initial normalisation step when the data set is created.

```auto
dataloader->PrepareTrainingAndTestTree("...:NormMode=None");

```

The option `NormMode` controls pre-method normalisation. That is, normalisation of data done before any methods and transformamtions.

There are 3 choices for this option `None`, `NumEvents`, `EqualNumEvents` with the last being the default. `NumEvents` ensures that the average weight for each class, independently, is `1`. `EqualNumEvents` ensures that, for all classes taken together is `1`.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![a.bragagnolo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@a.bragagnolo](https://root-forum.cern.ch/u/a.bragagnolo)\
**Post date:** [December 5, 2017, 1:51pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/6 "2017-12-05T13:51:57Z")

</div>

Thank you Kim, that works.

Cheers,  
Alberto

---

<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:** [December 5, 2017, 2:50pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/7 "2017-12-05T14:50:15Z")

</div>

Please mark the post as the solution to help others with similar problems 🙂

---

<div class="post-metadata">

**Author:** ![a.bragagnolo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@a.bragagnolo](https://root-forum.cern.ch/u/a.bragagnolo)\
**Post date:** [December 5, 2017, 3:02pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/8 "2017-12-05T15:02:37Z")

</div>

How can I do that? I cannot found any button or option to set a topic as solved

Alberto

---

<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:** [December 5, 2017, 3:34pm UTC](https://root-forum.cern.ch/t/adding-treess-to-dataloader-with-weights-does-not-work-for-me/27038/9 "2017-12-05T15:34:25Z")

</div>

There should be a button to the left of the reply button (of the solution post) that says “Solution” when hovered over.
