Home | News | Documentation | Download

Error with TH1::GetEntries()

Hi everyone.
I want to return the entries of my histogram, but I see that it return not correct result.

#include <iostream>
#include <TH1.h>
using namespace std;

void anh()
{
    delete gROOT->FindObject("h");  //delete old object
    TH1D *h = new TH1D("h", "his", 10, 0, 10);
    for (int i = 1; i <= h->GetNbinsX(); i++)
    {
        h->SetBinContent(i, i);
    }
    cout << "Entries: " << h->GetEntries() << endl;
    h->Draw();
}

Here is an output.

root [9] .x anh.cpp
Entries: 10

I think correct entries are 55 (1+2+…+10 = 55).

Hi,

actually no, everything is correct. Number of entries is the number of times you called TH1::Fill(...) or TH1::SetBinContent(...). You are calling the latter in the loop over bins, and you have 10 bins. So you should end up with 10 entries.

The 1+2+3+…+10=55 you are thinking of is the integral.

Hi, thank you. It corrected my code and it was fine.