GetBinEntries within a range

Dear experts,

Is there a way to get the number of entries in a range of a histogram? Suppose I fill my histogram as:
TH1D * hist = new TH1D(“hist”,“hist”,100,0,10)

hist->Fill(x,weight).

I want to know the number of real entries between say x=1.5 and x=10? I am only aware of hist->GetEntries(), which returns the number of entries for the whole histogram.

Thank You,

Kuhan

Hello,
I’m not sure about this answer, but I can’t check it out atm:

(...) TH1D * hist = new TH1D("hist","hist",100,0,10); [filling procedure] double xmin=1.5,xmax=10.; hist->GetXaxis()->SetUserRange(xmin,xmax); int ent=hist->GetEntries(); (...)

Let me know if that works. The only other option is to sum the entries of the bins from the first to the last in the selected range:

[code]
(…)
TH1D * hist = new TH1D(“hist”,“hist”,100,0,10);
TH1D * hist2 = new TH1D(“hist”,“hist”,100,0,10);

[here yo fill with data hist and with an increment of 1 hist2, same bin in the two TH1D, just because if you fill with a weight>1 hist you loose the entry-number-to-bin-content correlation]

double xmin=1.5,xmax=10.;
int ent=0;
int minbin=(int)(xmin/hist2->GetXaxis()->GetBinWidth());
int maxbin=(int)(xmax/hist2->GetXaxis()->GetBinWidth());
for(int i=minbin;i<=maxbin;i++){
ent+=hist2->GetBinContent(x);
}
(…)[/code]

Have fun

Gabriele