Plot polar TH2

Hi,
I am trying to draw a TH2D plot in a polar coordinate. I have tried:

tree->Draw(“phi:theta>>h2”, “”, “pollego2z”);
(its top view is the 1st plot attached)

and
tree->Draw(“phi:theta>>h2”, “”, “polcolz”);
(the 2nd plot attached)
.

But both of them are not satisfied. Plot 1 needs to add axis and y-axis of plot 2 has a problem. I am wondering if I can plot a TH2D plot in a polar coordinate like :

root.cern.ch/root/html/TGraphPolar.html

.

Thank you,
Zhiyi.




1 Like

I will investigate. Surely some improved can be made.

I do not understand how you get the first plot. In my case I get 2 axis when I use POLCOLZ. See the attached picture. I am using the latest ROOT version.


Thanks for your testing.

However, in your plot, x and y axises are not meaningful for a polar coordinate.

Yes the angle is missing. That’s known . It has to be implemented.
I was just pointing out the fact that there is some axis.

I do not know if it has been implemented yet. I worked out a solution for this long time ago and would post it here, may be helpful for other guys.

TCanvas * plot_histogram_in_polar(TH2D * h2d) {

  h2d->SetStats(false);
  TCanvas * can = new TCanvas("can", "", 600, 600);
  can->SetTheta(90);
  can->SetPhi(180);
  
  h2d->Draw("pollego2z");
  double ymax = h2d->GetYaxis()->GetXmax();
  double ymin = h2d->GetYaxis()->GetXmin();

  TGraphPolargram * gp = new TGraphPolargram("g",
                                             ymin, ymax,
                                             0, 2*TMath::Pi());
  gp->SetNdivRadial(4);
  gp->SetLineColor(17);
  gp->Draw();
  can->Update();
  
  TPaletteAxis *palette = (TPaletteAxis*)h2d->GetListOfFunctions()->FindObject("palette");
  palette->SetX1NDC(0.89);
  palette->SetX2NDC(0.92);
  palette->SetY1NDC(0.6);
  palette->SetY2NDC(0.97);
  palette->SetLabelSize(0.03);
  can->Modified();
  
  return can; 
}

Thanks for the input.

Hi,

Sorry for reopening this thread, but I am unable to get a 2D polar plot going. Basically I can get:

Data->Draw("lead_E:atan(lead_px/lead_py)","","SURF7 POL")


Naively I thought I should only do

Data->Draw("lead_E:atan(lead_px/lead_py)","","COL POL")


Which looks nothing near to what the SURF managed to do.
In short, how can I just get the 2D polar plot from a TH2?

Thanks,

Andre

ps - I am on

ROOT 5.34/05 (tags/v5-34-05@48582, Feb 14 2013, 23:27:57 on macosx64)

I works for me with 5.34 on mac

root [1] ntuple->Draw("px:py","","colz pol")


Hello @zhiyiliu,

Can you just explain how your TH2D has to be build before your function ?
Is it :

  • TH2D* th2 = new TH2D(“th2”," name",binTheta, thetaMin, thetaMax, binR, rMin, rMax);
    or
  • TH2D* th2 = new TH2D(“th2”," name",binX, xMin, xMax, binY, yMin, yMax);
    ?

Because I would like to use the first one, but I don’t manage to do so with “Draw(pol colz);” or your function. Also do you now if theta has to be in degree or radian ? The documentation don’t say.

Thank you for your help.

I had some success trawling through the source code to understand what’s going on. It seems that the axis ranges are messed up if when drawing a TH2 with Draw(“COLZ POL”). I managed to get it working by drawing a dummy histogram beforehand.

[code]void PolarTH2()
{
gStyle->SetOptStat(0);

Double_t rmax(1.);

TH2D* pol_his = new TH2D("polarHist", "polarHist", 20, 0., 2.*TMath::Pi(), 20, 0., rmax); // the polar data. X maps to theta, Y maps to R

Double_t theta, r;
//fill the histogram with something nice.
for(Int_t i=1; i<=pol_his->GetNbinsX(); i++)
{
	theta = pol_his->GetXaxis()->GetBinCenter(i);
	for(Int_t j=1; j<=pol_his->GetNbinsY(); j++)
	{
		r = pol_his->GetYaxis()->GetBinCenter(j);
		pol_his->SetBinContent(i, j, r*cos(2.*theta) );
	}
}

//make a dummy histogram to set the axis range from -rmax to rmax in both directions
TH2D* dummy_his = new TH2D("dummy", "histo title", 100, -rmax, rmax, 100, -rmax, rmax);

TCanvas* c1 = new TCanvas("theCanvas", "theCanvas", 600, 600);
dummy_his->Draw("COL"); // draw the dummy histogram first
pol_his->Draw("COL POL SAME"); // now draw the data histogram. If it has "SAME" it will use the first histogram ranges

}[/code]


PolarTH2.cpp (940 Bytes)

Any status on improving the POL draw commands? It is very awkward to have to make a blank histograms just to create the proper histogram axis.

Thanks.

May be you ca post a bug report on JIRA. It is easier to follow.