# How to use a branch as weights on histogram?

**URL:** <https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325>\
**Category:** ROOT\
**Tags:** tree, python, hist, root\
**Created:** [July 25, 2024, 4:54pm UTC](https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325 "2024-07-25T16:54:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greentree](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/greentree/32/30929_2.png) [@greentree](https://root-forum.cern.ch/u/greentree)\
**Post date:** [July 25, 2024, 4:54pm UTC](https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325/1 "2024-07-25T16:54:49Z")

</div>

I am trying to use weights on my histogram but all bins of the histogram returns to 0. Weights are stored in a friendtree branch. Each entry has a weight.

```auto

  myhistogram = ROOT.TH3F('myhistogram','name', X, axesbinsX.GetArray(), Y, YArray, Z, ZArray)
  cuts = "branchname1 < 10000"

  tree.Draw(f"strings:{particle}>>+myhistogram", f"({cuts}) * cloned_tree.friend_tree.weights")

```

If i use a number instead the cloned\_tree.friend\_tree.weights, it works.

I tried to print the entries of this friend\_tree by:

```auto
  for entry in tree:
    weight = getattr(entry, "friend_tree.weights")

```

and this actually prints the weights. There are non-zero weights but the weights are negative sometimes. Temporarily, I tried to set the weights to 0 when it is negative:

` tree.Draw(f"strings:{particle}>>+myhistogram", f"({cuts}) * (cloned_tree.friend_tree.weights> 0 ? cloned_tree.friend_tree.weights :0 )")`  
Which did not work as well.

---

<div class="post-metadata">

**Author:** ![devajith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/devajith/32/37165_2.png) [@devajith](https://root-forum.cern.ch/u/devajith)\
**Post date:** [July 26, 2024, 7:28am UTC](https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325/2 "2024-07-26T07:28:44Z")

</div>

Hi @greentree, Thanks for your question.

Adding @couet and @linev who might be able to help you.

Thanks,  
Dev

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [July 26, 2024, 7:32am UTC](https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325/3 "2024-07-26T07:32:10Z")

</div>

@devajith

Question is not about graphics but `TTree::Draw` syntax.

Maybe @pcanal can help here.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 9, 2024, 7:32am UTC](https://root-forum.cern.ch/t/how-to-use-a-branch-as-weights-on-histogram/60325/4 "2024-08-09T07:32:39Z")

</div>

Closed on August 9, 2024, 7:32am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
