New histogram filled with only number of bins from old histogram

Hi all,

if I have a histogram with 10 000 bins and I need a new histogram containing only first 1000 bins from the old one - Is there an easy way to do this?


I don’t think there is any direct method to do it but maybe you could live with the original histogram. Many histogram methods respect the set “axis range”, e.g:
h10k->SetAxisRange(h10k->GetBinCenter(1), h10k->GetBinCenter(1000), "X");

1 Like

To make a partial copy would need to make a a macro looping on the 1000 bins you want to copy and fill a new histogram with them. I guess. @moneta can confirm.

1 Like

If you want to avoid to loop and be fast you can use directly the histogram array of the bin content.
So suppose h1 is your original histogram (type TH1D or TH1F) you can do something like:

auto h2 = new TH1D("h2","Histogram built with first 1000 bins of h1",1000, h1->GetXaxis()->GetBinLowEdge(1), h1->GetXaxis()->GetBinUpEdge(1000) );
// note that first array element is underflow bin 
std::copy( h1->GetArray()+1, h1->GetArray() + 1001, h2->GetArray()+1 ); 
// copy also error array if existing
if (h1->GetSumw2N() > 0) { 
  std::copy( h1->GetSumw2()->GetArray()+1, h1->GetSumw2()->GetArray()+1001 , h2->GetSumw2()->GetArray()+1 ); 


1 Like

Thank you all for your help!

@moneta I also have a similar problem but in my case, the histogram has variable binning. What is the best way to treat that?