# Histogram subsets

Dear ROOTers,

is there a fast (maybe a method which I missed) way to get a “subset” of the channels of a histogram and making a new histogram with that?

I mean, e.g., from a TH2 with 100 bins on both axis I’d like to have a new TH2 made by the 17th to 47th X bin and by the 39th to the 99th bin (without changing the bin specifications like bin width and edges).

If yes, it is also calculating the underflow/overflow bins?

Thanks,
Matteo

May be a macro like that (I have not checked all details):

``````TH2F * TH2Subset(TH2F *h,Int_t binx1, Int_t binx2, Int_t biny1, Int_t biny2)
{
Double_t x1 = h->GetXaxis()->GetBinLowEdge(binx1);
Double_t x2 = h->GetXaxis()->GetBinLowEdge(binx2)+h->GetXaxis()->GetBinWidth(binx2);
Double_t y1 = h->GetYaxis()->GetBinLowEdge(biny1);
Double_t y2 = h->GetYaxis()->GetBinLowEdge(biny2)+h->GetYaxis()->GetBinWidth(biny2);
Int_t    n1 = binx1+binx2+1;
Int_t    n2 = biny1+biny2+1;

Int_t nbinsx = h->GetNbinsX();
Int_t nbinsy = h->GetNbinsY();

TH2F *hs = new TH2F(h->GetName(),
h->GetTitle(),
n1, x1, x2,
n2, y1, y2);

Double_t content, x, y;

for (Int_t i=1; i<=nbinsx; i++) {
for (Int_t j=1; j<=nbinsx; j++) {
content = h->GetBinContent(i,j);
x = h->GetXaxis()->GetBinCenter(i);
y = h->GetYaxis()->GetBinCenter(j);
hs->Fill(x, y, content);
}
}

return hs;
}``````

You can use it that way:

``````root [0] .L TH2Subset.C
root [1] TH2F *h = hsimple->Get("hpxpy")
root [2] TH2F *h2 = new TH2F()
root [3] h2 = TH2Subset(h,10,20,5,15)``````

Hi Couet,

I was writing something that and I wondered if it was already existing as TH2 method…

BTW, it would be worth to have this routine as TH2 method, wouldn’t be?

It seems also a quite standard routine not depended by the “taste” of each user (I do not see any other meaning for asking a TH2 subset…), is it possible to add a method like this as a feature for next versions?

I suggest you contact Lorenzo Moneta (@cern.ch) who is responsible of the histograming package.