# Sum weights in TTree

**URL:** <https://root-forum.cern.ch/t/sum-weights-in-ttree/35014>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [July 5, 2019, 4:04pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014 "2019-07-05T16:04:27Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [July 5, 2019, 4:04pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014/1 "2019-07-05T16:04:27Z")

</div>

What is the recommended way to retrieve the weighted number of entries in a `TTree`?

I could, for example, do

```python
nEntEffective = sum(ent.SomeWeight for ent in t if ent.SomeBranch > SomeCutValue)

```

but I wondered if I had overlooked some utility function _a la_ `t.GetEntries('SomeBranch > SomeCutValue')` that would take weights into account.

* * *

_ROOT Version:_ 6.19/01  
_Platform:_ macOS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [July 5, 2019, 4:11pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014/2 "2019-07-05T16:11:57Z")

</div>

Hi,  
maybe

```auto
df.Filter("x > y").Sum("weight")

```

?

---

<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:** [July 5, 2019, 4:45pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014/3 "2019-07-05T16:45:23Z")

</div>

Thank you. From my cursory study, these methods appear comparably fast if one has to make the data frame (i.e., `RDataFrame(t).Filter('SomeBranch > SomeCutValue').Sum('SomeWeight')`), while the data frame method appears much faster if one has already created the filtered data frame (`df.Sum('SomeWeight')`).

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [July 5, 2019, 4:49pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014/4 "2019-07-05T16:49:58Z")

</div>

See [this thread](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/9), the event loop is much faster with RDF, but for small datasets you’ll spend most of the time setting up RDF.

---

<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:** [July 19, 2019, 4:58pm UTC](https://root-forum.cern.ch/t/sum-weights-in-ttree/35014/5 "2019-07-19T16:58:16Z")

</div>

Closed on July 19, 2019, 4:58pm UTC

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