# Applying weights to the branches of a TTree

**URL:** <https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [April 14, 2023, 3:26pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488 "2023-04-14T15:26:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 3:26pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/1 "2023-04-14T15:26:04Z")

</div>

Hi ROOT experts,

I am new to ROOT and I am looking at generating Drell-Yan MC samples. I have been using AnalysisTop to produce a tree where eahch branch contains my relevant variables for each event, which I want to fill into histograms. I want to apply a particular MC generator weight to each event entry when adding the variables into the tree. The trees are filled in the following code:

![image](https://root-forum.cern.ch/uploads/default/original/3X/3/0/30a44aa962952ca24b65ed208f538bc24705c07b.png)

Is there a way to apply the weight “m\_weight\_mc” to all the branches in the tree?

Note that as these weights are different depending on the event, I cannot perform a simple rescaling of the final histogram.

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2023, 3:29pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/2 "2023-04-14T15:29:10Z")

</div>

You can create a new (additional) branch with the weight.

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 3:30pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/3 "2023-04-14T15:30:18Z")

</div>

How exactly would I do this?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2023, 3:32pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/4 "2023-04-14T15:32:04Z")

</div>

You need to find the place where the “m\_tree” is created (i.e., where all its branches are defined).

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 3:37pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/5 "2023-04-14T15:37:01Z")

</div>

The branches of m\_tree is defined as follows:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/d/3d87548c5eae715dd22dc0a509f5e01c89a0d51c.png)

Would I need to multiply &obj by the weight?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2023, 3:42pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/6 "2023-04-14T15:42:36Z")

</div>

You need to find the place where the “makeOutputVarible” is actually used.

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 3:45pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/7 "2023-04-14T15:45:49Z")

</div>

I use MakeOutputVariable here:

![image](https://root-forum.cern.ch/uploads/default/original/3X/6/e/6e23affa83bcbbf90e6ccd95e9882f36f8de08f2.png)

I don’t see how I can apply the weights here?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2023, 3:46pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/8 "2023-04-14T15:46:48Z")

</div>

So, your tree already has a “`weight_mc`” branch (and the corresponding variable is set in the “`CustomTreeManager::fill`” method).  
Just use it when filling histograms, e.g. → [TH1::Fill](https://root.cern/doc/master/classTH1.html#a498de8e0804e75fc75e62dc14a3bb62d) ; [TH2::Fill](https://root.cern/doc/master/classTH2.html#a9d1a326ec5cef292722a357746c5a795) ; [TH3::Fill](https://root.cern/doc/master/classTH3.html#a54dcae0adfee84147c8dd59accb90781)

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 3:47pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/9 "2023-04-14T15:47:13Z")

</div>

Yes, it does

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 4:07pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/10 "2023-04-14T16:07:57Z")

</div>

I am currently filling the histograms using the “project” function:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/c/8c4acf1de270597d4f396324a5b29f6f7bce4f4f.png)

Can I apply the weight using project?

I am not sure how to loop over the branch entries and plot them into the histogram one-by-one, whilst weighting each entry by the corresponding weight.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2023, 4:15pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/11 "2023-04-14T16:15:06Z")

</div>

`tree->Project("h_something", "something", "weight_mc");`

---

<div class="post-metadata">

**Author:** ![jinglis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jinglis](https://root-forum.cern.ch/u/jinglis)\
**Post date:** [April 14, 2023, 4:32pm UTC](https://root-forum.cern.ch/t/applying-weights-to-the-branches-of-a-ttree/54488/12 "2023-04-14T16:32:03Z")

</div>

Thank you so much! I can’t believe it was that simple.
