Cast histogram

Dear experts,

this line bellow has no effect, actually I still have an TH1D object:

TH1F htestfit=(TH1F)fit->GetHistogram();

htestfit->ClassName(): TH1D

Do you know how I should cast to have a TH1F object?

Regards

Yes I see the same:

root [0] TH1D *hd = new TH1D()
(TH1D *) 0x7f99ab768420
root [1] TH1F *hf = new TH1F()
(TH1F *) 0x7f99ad2f0750
root [2] hf->ClassName()
(const char *) "TH1F"
root [3] hf = (TH1F*)hd;
root [4] hf->ClassName()
(const char *) "TH1D"

Dear Couet,

thank you for your answer. Do you know how to solve this?

Regards

Hi,

You cannot cast from a TH1D to a TH1F. They are different objects. What you can do is to use TH1::Copy to copy the histogram from a TH1D to a TH1F if you need to reduce your memory space occupied by the histogram

Cheers

Lorenzo

Dear experts,

ok, thank you or your help. I’m a bit surprise actually in C++ we can cast a “double object” in an “int object” which are to 2 different objects right? So for me it is weird that we can’t do that with root objects.

Regards

In C / C++ you can cast anything to anything else. You just need to know that it makes sense.
Casting a “THxD*” to a “THxF*” does not.

Dear experts,

ok, thank you for your answer.

Regards