# Summing TBranch data

**URL:** <https://root-forum.cern.ch/t/summing-tbranch-data/56463>\
**Category:** Newbie\
**Created:** [September 24, 2023, 10:54pm UTC](https://root-forum.cern.ch/t/summing-tbranch-data/56463 "2023-09-24T22:54:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [September 24, 2023, 10:54pm UTC](https://root-forum.cern.ch/t/summing-tbranch-data/56463/1 "2023-09-24T22:54:41Z")

</div>

I am trying to sum over a column of 1s and 0s in a TTree, and this seems like a trivial task, but I have no clue what function lets me pull these values. I am attempting to do this in a .C file, where I am able to pull statistics from other TTrees from the same TFile Here is what I have so far:

```auto
TFile *input3 = new TFile("TestCAL_Cs137_d5z6_MoreStats.root", "read");
TTree *tree3 = (TTree*)input3->Get("Incident");
int entries3 = tree3->GetEntries();
TH1F *hist3 = new TH1F("hist3", "", 2, 0 ,2);
double IncGamIter = 0;
double TotGamIter = 0;
double Exceed1Vals = 0;
for(int i = 0; i < entries3; i++)
{
        IncGamIter += tree3->GetVal(i);
        TotGamIter += 1;
}

```

My main problem is using GetVal(), as I do not think this gets the values I need.  
Thank you for your help!

_ROOT Version:_ 6.26  
_Platform:_ Linux

---

<div class="post-metadata">

**Author:** ![Ian\_L](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ian_l/32/33898_2.png) [@Ian\_L](https://root-forum.cern.ch/u/Ian_L)\
**Post date:** [September 24, 2023, 11:01pm UTC](https://root-forum.cern.ch/t/summing-tbranch-data/56463/2 "2023-09-24T23:01:09Z")

</div>

Here is the first 10 entries of what the TTree contains:

```auto
************************
* Row * Incident_ *
************************
* 0 * 0 *
* 1 * 0 *
* 2 * 0 *
* 3 * 0 *
* 4 * 1 *
* 5 * 0 *
* 6 * 0 *
* 7 * 0 *
* 8 * 0 *
* 9 * 1 *
* 10 * 0 *

```

I can also provide the actual file, but it is too large for me to upload to here.

Thanks again!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [September 25, 2023, 6:27am UTC](https://root-forum.cern.ch/t/summing-tbranch-data/56463/3 "2023-09-25T06:27:32Z")

</div>

Hello Ian,

Thanks for posting. I propose to solve your enquiry with [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html), the modern way to deal with columnar (and not only) datasets. Below you find a complete example. I had to create a fake dataset in the first function to delivere a copy & paste recipe. Let us know how it goes!

Cheers,  
D

```auto
void produceFakeInput()
{
    // This function is there just to produce some fake data
    auto df = ROOT::RDataFrame(128).Define("YourBranch", []{return (int) gRandom->Gaus() > 0 ? 1 : 0;});
    df.Snapshot("Incident", "TestCAL_Cs137_d5z6_MoreStats.root");
}

void example()
{
    produceFakeInput();

    // Open a Data Frame built on your columnar dataset
    auto df = ROOT::RDataFrame("Incident", "TestCAL_Cs137_d5z6_MoreStats.root");
    auto sumRes = df.Sum("YourBranch");
    auto statsRes = df.Stats("YourBranch");

    std::cout << "The sum is " << sumRes.GetValue() << std::endl;

    std::cout << "A more complete statistics: " << std::endl;
    statsRes->Print();

}

```
