Problem while projecting a TH3F histogram

Hi,

I have a TH3F *histo and when trying to make a 2d projection on TH2D *projection, like, projection = histo->Project3D(“xy”)
I get the following error:

Error: Incorrect assignment to projection, wrong type “TH1*”

Which is the correct way to do this projection?

Thanks in advance,
Hermes

Hi,

we need the minimal amount of running, self-contained code that shows this problem. Given your info so far I don’t see any issue: root [0] TH3F *histo = new TH3F("h3","h3",10,0.,1.,11,0.,1.,12,0.,1.) root [1] TH2D* projection = histo->Project3D("xy")
Cheers, Axel.

Hi,

Thanks for the quick answer. I was cleaning the code before uploading and decided to give another try changing one line and now it works, before I had:

for a TH3F *histo,

TH2D *projection = new TH2D(“projection”, “projectiontest”, 10, 1, 10, 10, 1, 10)
projection = histo->Project3D(“xy”);

and now changed it as your example:

TH2D *projection = histo->Project3D(“xy”);

and it works. Then, should I not use the contructor for the histogram where I want to project?

Many thanks,
Hermes

Hi,

when a function returns “A*” it creates A internally, you receive it by storing its address in a pointer variable. No need to create your own object.

Cheers, Axel.

Hello all
I’m stuck with a similar problem but I can’t work out how to solve it from this thread and I would be very grateful for some help. I am trying to project a TH3F onto a TH2.

In COMPILED code I have (3dhisto is a previously declared TH3F):
TH2 proj2=3dhisto->Project3D(“xz”);
This doesn’t compile. It returns error: invalid conversion from TH1*' toTH2

So I do
TH1 *proj1=3dhisto->Project3D(“xz”);
This compiles, but gives segmentation violation when I try to run it.

Projecting onto a 1D histo compiles and runs fine. E.g:
TH1 *proj1=3dhisto->Project3D(“x”);

So I can see there’s a problem with the pointer that I’m returning but I’m not sure what to do… cast the TH2* as a TH1*?
I would be very grateful for any help!
Thanks very much in advance
Sarah

do:

TH2D *proj2= (TH2D*)3dhisto->Project3D("xz");
Rene

1 Like

Thank you Rene that command compiled and ran fine once I put it in a separate macro to do the plotting. For some reason it wouldn’t run when the command was in the .C file that I use for my analysis, but it probably shouldn’t be in there anyway!
Sarah