Summing TBranch data

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:

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

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

************************
*    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!

Hello Ian,

Thanks for posting. I propose to solve your enquiry with RDataFrame, 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

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();


}