# Plot final signal/background distributions, weight problem

**URL:** <https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475>\
**Category:** TMVA\
**Created:** [August 29, 2018, 8:36am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475 "2018-08-29T08:36:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [August 29, 2018, 8:36am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/1 "2018-08-29T08:36:03Z")

</div>

Dear Sir,

In my TMVA study, I have ~10 background, all background with different weighting (cross section size).

After TMVA, I wish to plot the signal and background distributions for the input variables, to see the TMVA CUT effect on the variables. I followed this : [TMVA: Macro to plot final signal/background distributions?](https://root-forum.cern.ch/t/tmva-macro-to-plot-final-signal-background-distributions/9970)

My question is :  
for all the background is added together, it seems the final distribution shown with i.e. " TestTree-\>Draw(“myvar1”,“LD\>0.3”,"");" is plotted with the un-weighted # of events.

Because each contributed BG have different shape of distribution and weight (cross section),  
I wish to know, how can I plot with background distribution with correct contribution from each background?

Thanks!

Best,  
Jung

---

<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:** [August 29, 2018, 10:35am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/2 "2018-08-29T10:35:28Z")

</div>

Hi,

See section 12.20.1 of the root users guide 🙂

[https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#simple-analysis-using-ttreedraw](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#simple-analysis-using-ttreedraw)

Basically `TestTree->Draw(“myvar1”,“weight*(LD>0.3)”);` should work if the event weight is stored in a branch/leaf name `weight`.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [August 29, 2018, 10:55am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/3 "2018-08-29T10:55:45Z")

</div>

Hi Kim,

Thanks for the reply. I had tried this. But the weight branch gave by TMVA is strange, i.e. I have totally 9000 events, I want total weight as 20.0, but in the weight branch, it shown as x axis is 22, with y axis 9000.

If I used TestTree-\>Draw(“myvar1”,“weight\*(LD\>0.3)”); it gave each event times 9000.

My main problem is, it used # of events for each background, not the weight I input.

I have more than 10 background. Each have different weights, if it used # of events for each background, it will give a wrong BG distribution. And because all BG are added in a BG branch. I cannot re-weight it again.

So I wish to know how to add weight to each BG, which gave a correct total BG distribution.

Thanks,

Jung

---

<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:** [August 29, 2018, 1:11pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/4 "2018-08-29T13:11:12Z")

</div>

Hi,

Ok, thanks for the clarification. If all events have the same weight, then I suspect they are not propagated correctly from your input to TMVA.

Could you explain how you do it?

If you have a branch containing the weights for each event regardless of class using

```auto
dataloader->SetWeightExpression("branch_name_in_input_tree");

```

_should_ do the trick, and the correct weight should end up in the final output file.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [August 29, 2018, 1:40pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/5 "2018-08-29T13:40:27Z")

</div>

Hi Kim,

Thanks for the reply.

This is part of my BG and its weight :

Double\_t bbjaWeight = 367000.0;

Double\_t bbaaWeight = 140.0;

Double\_t bbjjWeight = 434000000.0;

Double\_t bbhWeight = 1.26;

Double\_t tthWeight = 1.37;

Double\_t ccjaWeight = 1050000.0;

```
dataloader->AddBackgroundTree( bbja_BG, bbjaWeight*3000.0*5.0/10000.0 *73273.0/3000000.0);

dataloader->AddBackgroundTree( bbaa_BG, bbaaWeight*3000.0*60253.0/3000000.0 );

dataloader->AddBackgroundTree( bbjj_BG, bbjjWeight*3000.0*56126.0/5000000.0*5.0/10000.0*5.0/10000.0);

dataloader->AddBackgroundTree( bbh_BG, bbhWeight*3000.0*666.0/1000000.0);

dataloader->AddBackgroundTree( tth_BG, tthWeight*3000.0*38478.0/1000000.0);

dataloader->AddBackgroundTree( ccja_BG, ccjaWeight *3000.0*10173.0/1000000.0*5.0/10000.0*1/8.0*1/8.0);

```

I’m thinking is it related to TMVA setting : NormMode, previously I set it as NumEvents. Maybe I should use NormMode=None?

(Actually I had tried with None, seems the output is no difference. I will delete previous output.root file, and try again.)

Thanks!

Best,

Jung

---

<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:** [August 29, 2018, 2:17pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/6 "2018-08-29T14:17:14Z")

</div>

Hi,

That is how you should set the weights for the trees (and I verified that it works in the TMVAClassification example irrespective of the setting of `NormMode`).

I think some deeper debugging is needed to resolve your issue. Would it be possible for you to reduce your script to a minimal reproducer? (I forsee this will either help you find a bug in your code, or provide us with a concrete test case to debug!)

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [August 30, 2018, 1:48am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/7 "2018-08-30T01:48:04Z")

</div>

Hi Kim,

Thanks for the mail. I just used the default TMVAClassification file provided in ROOT/tutorials/tmva, and add my tree and weighting.

I attached the file. Thanks!

Best,

Jung

[TMVAClassification.C](https://root-forum.cern.ch/uploads/default/original/3X/6/1/6166a62a97aed54d696380252114c8ab4249852c.C) (15.7 KB)

---

<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:** [August 30, 2018, 1:26pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/8 "2018-08-30T13:26:57Z")

</div>

Hi,

I can’t run your script since I lack access to the input file but it seems fine from a TMVA perspective. Could you post a plot of the weight branch in the output file?

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [August 31, 2018, 6:05am UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/9 "2018-08-31T06:05:57Z")

</div>

Hi Kim,

Thanks for the reply. I had already add two new reply, but it did not shown here.

Sorry I found I made a very stupid mistake. I thought the weight given at " dataloader-\>AddBackgroundTree( BG, Weight);", the Weight is the total weight. When I read the manual more carefully, I found its the weight of each event.

I modified the weight to each events, now everything is ok.

I have one naive question, if I want to use the weight I set to do the TMVA study, all I need is set NormMode=None?

And if NormMode= NumEvents, it means it will rescale BG and signal to Ns and Nb I set in “nTrain\_Signal=0:nTrain\_Background=0”, if I set it as 0, then the signal and BG will be weighted as 1/2 of total Ns and Nb.

If I set NormMode= EqualNumEvents, it means the signal and BG will be weighted equally.

Is my understanding of these NormMode setting correct?

And another question is can I have TMVA variables given in different root file?  
i.e.  
variable A from input1.root  
variable B from input2.root

Sorry and Thanks!!

Best,  
Jung

---

<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:** [August 31, 2018, 4:57pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/10 "2018-08-31T16:57:12Z")

</div>

Hi,

Glad you figured it out! For you questions:

- Your understanding of the different event normalisations is correct.
- Yes you can add variables from different files but it requires that your files are “friends”, that is linked. Friend trees is the canonical way to split a tree over multiple files. (See: [https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#example-3-adding-friends-to-trees](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#example-3-adding-friends-to-trees))
  - If the dataset is small you could also use RDataFrame to combine the two branches into a new tree and save the result in a new file. (See: [https://root.cern.ch/doc/v614/classROOT\_1\_1RDataFrame.html](https://root.cern.ch/doc/v614/classROOT_1_1RDataFrame.html))

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![JungChang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JungChang](https://root-forum.cern.ch/u/JungChang)\
**Post date:** [September 1, 2018, 2:18pm UTC](https://root-forum.cern.ch/t/plot-final-signal-background-distributions-weight-problem/30475/11 "2018-09-01T14:18:47Z")

</div>

Hi Kim,

Thanks a lot for the reply! I got it!

Best,

Jung
