TH3 and Project3D

Dear rooters,

I am filling a TH3 Histogram and want to have a 2D Projection of the y and z axis for every x axis bin. Somehow just to do:

TH2D *hProjection = h3->Project3D(“yz”)->Draw(“Col”);

is not the solution which I want.

Somehow the problem can not be solved like in the case when of a TH2 when projecting the y-axis for every x-axis bin:

TFile *file = new TFile(“File.root”,“READ”);
TH2D *h = (TH2D *)file->Get(“h”);

TCanvas *c1 = new TCanvas(“c1”,"",50,50,800,800);
c1->Divide(2,2);
c1->Draw();

Int_t nbinsX = h->GetNbinsX();
TH1D *hProjection;
Char_t HistName[256];
for(Int_t i = 0; i < nbinsX; i++)
{
sprintf(HistName,“hProjection_%i”,i+1);
hProjection = h->ProjectionX(HistName,i,i+1);
c1->cd(i+1);
hProjection->Draw();
c1->Update();
}

Is there a similar way to solve it as described above as for the TH2 example above?

Thanks a lot in advance for the help.

Cheers,

Michael

Hi,
you can use SetRange on the x axis:

h3.GetXaxis()->SetRange(xbin,xbin);
TH2D *hProjection = (TH2D*) h3->Project3D("yz");

Regards

Lorenzo

Dear Lorenzo,

Thanks a lot for your answer. That is what I was looking for :smiley: .

Best regards,

Michael

Dear Lorenzo and Michael,

Could you please to advise how to use the Project3D with the release of 2D histos? From the documentation I learned that the 3d Project function returns a pointer TH1. So, when I try to compile your example as expected, it fails on the conversion from “TH1*” to " TH2*»

Thank You,
Andrey

Hi,

The returned type is a TH1* but the actual real type is a TH2D* .
Now, when using Cling and not Cint, you need to explicitly cast the returned pointer.
I have edit and fixed the code

Best

Lorenzo