Home | News | Documentation | Download

Correct usage of histogram constructor without parameters

_ROOT Version: 6.18

Could you, please, explain, how to use TH2 constructor without arguments correctly.
Let’s say, I create an object:
“TH2I *hist = new TH2I();”

Then I set some name and then I should set some axis parameters. I do it using:
“hist->GetXaxis()->Set(nbinsx, xleft, xright)” and the same for y axis.

But what else i should do for correct working with this histogram, because that not enough: the histogram isn’t filled when only previous settings were done (that’s what i see from the results of my testing).

Best,
Nazar

You also need to Fill the histogram (or SetBinContent). Check out the available methods for TH2 (and those inherited from TH1) in the documentation, to see what you can do. Also you can see examples in the manual and tutorials here, .e.g. (https://root.cern/doc/master/group__tutorial__hist.html).

Hi Nazar,

It is not recommended way to use histogram classes - default constructor normally used for I/O.
If you want to understand, how to initialize histogram members correctly, check constructor of correspondent classes - TH2I and TH2. I am not sure, that all necessary histogram methods are public, therefore try to use normal constructors.

Regards,
Sergey

Of course i know that I should put something in histogram for filling). But no Fill(), no SetBinContent() doesn’t work. Though the axes are binned after TAxis::Set() as I see from both GetNbinsX() and GetNbinsY(), but neighter Fill() nor SetBinContent() doesn’t put anything in the histogram.

But if I create histogram using constructor with parameters, I can use Fill() or SetBinContent() as always, so I decided that I should do something else after TAxis::Set() if I use constructor without parameters.

Hi Sergiy,

So, If I want create histogram before I know axes parameters, it’s possible that I don’t have normal instrument for it? That’s pity. But I will try after careful study of the constructors).

Best,
Nazar

Hi Nazar,

It is possible to change axis min/max values (using TAxis::Set() method),
and then you should call TH2I::SetBinsLength(-1) method to reallocate histogram bins container.
Hope it will work for you.

Regards,
Sergey

1 Like

It works! Thank you for help! There are assigning some value to a fNcells after TAxis::Set() as I see in the TH2I constructor, but I don’t see who allocate the memory, maybe that doesn’t matter for me, if i’m not developer of root)

Best,
Nazar

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