# Graphing the average of multiple rows

**URL:** <https://root-forum.cern.ch/t/graphing-the-average-of-multiple-rows/53202>\
**Category:** Newbie\
**Tags:** tree, graphics, hist, root\
**Created:** [January 21, 2023, 6:43pm UTC](https://root-forum.cern.ch/t/graphing-the-average-of-multiple-rows/53202 "2023-01-21T18:43:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacetomato1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spacetomato1/32/32132_2.png) [@spacetomato1](https://root-forum.cern.ch/u/spacetomato1)\
**Post date:** [January 21, 2023, 6:43pm UTC](https://root-forum.cern.ch/t/graphing-the-average-of-multiple-rows/53202/1 "2023-01-21T18:43:42Z")

</div>

Hello!  
I’m new to root and have recently installed it to work with the data that I’m obtaining from a geant4 particle simulation (Never worked with c++ either so forgive me if I sound like an idiot). My current file structure involves a single tree with 1e+6 rows/entries (1 for each particle). Then for each row there iterations/instances for each “step” of that particle (point where they are created, change direction, , hit an atom, etc). What I want to be able to do is to be able to interpret the average final momentum (through a 2d histogram or a graph) for the starting point of a particle. At the moment I can use the command in the root terminal

t-\>Draw(“pz:Min$(z)”,“vlm==3”)

pz is the momentum of a particle (in Z axis) at a given instance/iteration, and vlm = 3 means the particle makes it to the detector. Min$(z) gives the lowest z value of all the iterations for the row that a pz’s iteration/instance belongs too. So basically, this command graphs for me the final momentum of a particle vs its starting position inside the source.

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

**Vertical Axis = particle momentum**  
**Horizontal Axis = position of particle creation (min$(z)**

What I need to do is find the average final (hitting vlm 3) momentum of all the particles for a given min$(z), not graph the momentum of every single particle against its min$(z). I tried to use the sum$ and length$ commands to find the mean, but these only work for all the iterations in a row, not all the rows (it gives me the average pz of all the steps of a certain particle vs starting position, not the average final step pz of all the particles). I have spent the last few days working on this but cannot find a solution and am really struggling so I can hear. Could someone please give me some help?

Thanks,  
Rio

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [January 22, 2023, 11:18pm UTC](https://root-forum.cern.ch/t/graphing-the-average-of-multiple-rows/53202/2 "2023-01-22T23:18:47Z")

</div>

Hi @spacetomato1,

To me, this seems like a task that should be easily accomplished using RDataFrame. Please, check the documentation at [Dataframes - ROOT](https://root.cern/manual/data_frame/) and the tutorials at [ROOT: Dataframe tutorials](https://root.cern/doc/master/group __tutorial__ dataframe.html). The full reference guide can be found at [https://root.cern/doc/master/classROOT\_1\_1RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame).

Cheers,  
J.
