# Increase accuracy of leaves when using RDataFrame.Mean

**URL:** <https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130>\
**Category:** ROOT\
**Tags:** rdataframe, tree\
**Created:** [September 8, 2025, 3:28pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130 "2025-09-08T15:28:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [September 8, 2025, 3:28pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/1 "2025-09-08T15:28:14Z")

</div>

Dear ROOT-ers,

I’m reading a ROOT tree with _Float\_t_ leaves. I use RDataFrame for that:

`dr_th_no_cut = df.Mean["double"]("displacement.dr_proj_th_cm")`

My idea is that I don’t need large precision for each leaf, thus saving space. But to calculate the mean, I would like to increase the precision of the result to avoid numerical errors. Unfortunately I get an error with that code (see the first line):

> Error in TTreeReaderValueBase::CreateProxy(): Leaf of type Float\_t cannot be read by TTreeReaderValue.  
> Traceback (most recent call last):  
> File “…/read\_phys\_file.py”, line 46, in  
> print(“no cut, cuts:”, dr\_th\_no\_cut.GetValue(), dr\_th\_wcuts.GetValue())  
> ~~~~~~~~~~~~~~~~~~~~~ ^^  
> cppyy.gbl.std.runtime\_error: const double& ROOT::RDF::RResultPtr::GetValue() =\>  
> runtime\_error: An error was encountered while processing the data. TTreeReader status code is: 6

Can RDataFrame support increasing accuracy for such aggregation operations? Is there a good workaround to solve that?

I’m using ROOT 6.30 at the moment. Thank you.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 8, 2025, 5:42pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/2 "2025-09-08T17:42:07Z")

</div>

Maybe with Redefine (or Define, for new extra columns), but the result seems to be the same, so maybe it doesn’t matter if it’s float originally, ROOT will use double precision (or it’s not really converting to double? an RDataFrame expert may clarify)?

With this:

```auto
import ROOT

d = ROOT.RDataFrame("ntuple","hsimple.root")
print(d.Describe())
print('Mean px:',d.Mean("px").GetValue())
print('Mean py:',d.Mean("py").GetValue())

d2 = d.Redefine("px","(double)px").Redefine("py","(double)py")
print(d2.Describe())
print('Mean px:',d2.Mean("px").GetValue())
print('Mean py:',d2.Mean("py").GetValue())

```

I get

```auto
...
Column Type Origin
------ ---- ------
i Float_t Dataset
px Float_t Dataset
py Float_t Dataset
pz Float_t Dataset
random Float_t Dataset

Mean px: -0.0038264499006807457
Mean py: -0.0032243128226821954

...
Column Type Origin
------ ---- ------
i Float_t Dataset
px double Define
py double Define
pz Float_t Dataset
random Float_t Dataset

Mean px: -0.0038264499006807457
Mean py: -0.0032243128226821954

```

---

<div class="post-metadata">

**Author:** ![ajomy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ajomy/32/37349_2.png) [@ajomy](https://root-forum.cern.ch/u/ajomy)\
**Post date:** [September 12, 2025, 10:15am UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/4 "2025-09-12T10:15:33Z")

</div>

Perhaps @vpadulan can help here

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [September 15, 2025, 3:15pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/5 "2025-09-15T15:15:26Z")

</div>

@ynikitenko I think I have good news for you:

Although the column being read might be float, the internal accumulators used to calculate the mean are always `double`. Does this solve this topic?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [September 15, 2025, 3:17pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/6 "2025-09-15T15:17:19Z")

</div>

Even better, in 2022, RDF was updated to use Kahan sums to compute the running sums.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [September 15, 2025, 3:23pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/7 "2025-09-15T15:23:39Z")

</div>

I also added a small comment to the documentation, so the knowledge can be found by users:

> <https://github.com/root-project/root/pull/19891>
>
> Reply to https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rda…taframe-mean/64130/5

---

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [September 15, 2025, 5:06pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/8 "2025-09-15T17:06:01Z")

</div>

Many thanks! Yes, it is even better than I imagined 🙂

Looking forward for the information to appear in docs for [Mean](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a685e954c0a81643a18dd17f831fb730f).

I suppose, this also relates to Sum, Stats,… and probably many other methods. I peeked into the source code for Mean, and Kahan summation is not evident at all (though _double_ is more visible).

Could you also update that knowledge please? Maybe there could a common section on precision?

---

<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:** [September 29, 2025, 5:06pm UTC](https://root-forum.cern.ch/t/increase-accuracy-of-leaves-when-using-rdataframe-mean/64130/9 "2025-09-29T17:06:03Z")

</div>

Closed on September 29, 2025, 5:06pm UTC

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