Making slices for TH2F


I have a TH2F, and I want to make Y projections for the different X bins. I can do this by issuing commands like,

h2->ProjectionY(“bin2”,Xbin2,Xbin2), etc.

but is there a method which I can invoke at the command line and make all the projection in one shot. We have FitSlicesY, which fits the Y slices, but no method which just makes them.


1 Like

If you know how to make an individual projection, couldn’t you just make a rootscript which loops over all the bins and fits all the projections?


The main problem is not a function returning all the slices, but where?
It is quite trivial to achieve what you want to do with something like:

TH1 *hbins[100] ;//or whatever you need, ie nbins for (int i=0;i<nbins;i++) { hbins[i] = h2->ProjectionY(Form("bin%d",i+1),i+1,i+2); }

Hi Rene,

Well, you could return them to the same place where you return the results of FitSlicesY. For instance, if I fit a histogram h with a Gaus, I get h_0, h_1, h_2, and h_chi2.

In the same way, you could return h2_1, h2_2, …, as the result of something like h->SlicesY()


p.s. thanks for the tip about how to do it in a loop. Didn’t know that you could do that.

Hi Vivek,

check the most complex THStack constructor - it does what you want. You can query the histograms via THStack::GetHists() afterwards.

Cheers, Axel.

1 Like

HI Axel,

Maybe I am not understanding something, but I do the following,

THStack hs(“tag_IPinfo_weight2D_trkPt_u”,“Y”,“YProj”,“Y Projections for X Slices”,1,50,"","") (I also tried it without the trailing ,"","")

where I want to get Y projections for the TH2F tag_IPinfo_weight2D_trkPt_u

I get an error message that no definition of the constructor matches my arguments.

Error: Can’t call THStack::THStack(“tag_IPinfo_weight2D_trkPt_u”,“Y”,“YProj”,“Y Projections for X Slices”,1,50,"","") in current scope (tmpfile):1:
Possible candidates are…
filename line:size busy function type and name (in THStack)

(compiled) 0:0 0 public: THStack THStack(const TH1* hist,Option_t* axis=x,const char* name=0,const char* title=0,Int_t firstbin=-1,Int_t lastbin=-1,Int_t firstbin2=-1,Int_t lastbin2=-1,Option_t* proj_option=,Option_t* draw_option=);

However, at … ck:THStack

I see - hist: the histogram used for the projections. Can be an object deriving from TH2 or TH3.


you cannot skip parameters “in between” by passing nothing surrounded by commas. That works for some languages (I forgot which one - perl? but definitely not C++.

Also you will need to pass the histogram to the THStack constructor, not a string - even if that string is the histogram’s name.

THStack* hs = new THStack(tag_IPinfo_weight2D_trkPt_u,"Y")

should do it (if your TH2 is in a variable called tag_IPinfo_weight2D_trkPt_u).

Cheers, Axel.

1 Like

thanks a lot. axel.

this is exactly what I wanted…


Hello Axel,

When I do this now, I get the following,

THStack* hs = new THStack(“TopMassCut3”,“Y”)

root [2] hs->GetHists()
(const class TList*)0x0

And I don’t get slices. I am using Root version 5.18.00d