Clone subrange of histogram

Dear root enthusiasts,

is there is simple way how to clone only part of histogram? I need to create from 2D histogram a new one keeping only subranges on individual axis. It can of course be done the hard way, ie. create new histogram with new definition of axis and then copy values, but I would also like to automatically keep the labels,title, and style setting. I have tried something along TH2::SetLimits(); TH2::Clone(), but that does not work. The Clone does not take the axis limits into account. Another way would be to Clone the whole histogram and then truncate (cut off) the axis, but I was not able to do that as well although it seems it is possible to expand the axis if needed. I would assume that such a simple functionality must be available somewhere.

Cheers
Petr

Hi Petr,

I guess you could clone your original histogram, then loop over all its bins, nullifying bin contents outside of your region of interest.

Dear Yus,

thank you for your help. Indeed, that was something I was considering. I can nullify bin content at the beginnings and ends along the axis. However after that I need to “cuf of the axis”, i.e changing number of bins, maximum and minimum. It’s likely my ignorance, but I was not able to find out how to do that.

With best regards,
Petr

You want to change the number of bins in what way exactly? And what do you mean by changing maximum and minimum?

Let’s say my 2D histogram is TH2D old(“”,“”, 100,0.,500.,100,10.,20.). The x bin width is hence 5 and y binwidth is 0.1 . Now I would like to create(select) a new subhistogram with the same bin sizes and bin content, but restrict axis to values between 100<=x<=200 and 12<=y<=15 like TH2D new(“”,“”,20.,100.,200,30,12.,15.).
I would like to do a thing similar to this (which does not work):
old.GetXAxis()->SetLimit(100.,200.)
old.GetYAxis()->SetLimit(12.,15.)
new=old.Clone();

Thanks for your help.

Petr

I don’t see any better way than the following:

for (short xBin = 1; xBin<=old->GetNbinsX(); ++xBin)
    for (short yBin = 1; yBin<=old->GetNbinsY(); ++yBin)
        if (100 <= old->GetXaxis()->GetBinCenter(xBin) <= 200)
            if (12 <= old->GetYaxis()->GetBinCenter(yBin) <=15)
                new->SetBinContent(xBin, yBin, old->GetBinContent(xBin, yBin));

Dear Yus,

thank you very much for your help. Indeed I had to resort to this solution. The problem is that I them must manually copy all other histogram properties. It would be very helpful to have such a “range sensitive” clone.

Best regards,
Petr

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.