Polar Axis For Histogram

Hello,

I am trying to draw a TH2D as a polar plot and I am not able to have axes overlaid on it.
I used this trick:

It works, but as soon as I change the canvas’ margin to properly show the palette, the TGraphPolarGram is not rescaled… see below

any idea how to scale it properly or display the same axes in a better way?

Thanks in advance for your help

You should keep the Pad square

With a rectangular canvas it works see attached. The problem arises when I do cv.SetRightMargin(0.15).
I added attached also a macro to reproduce the problem.
Test.C (3.8 KB)

I made a simpler version of your macro to ease the debugging:

void mperrint() {

   double p0 = 25;
   const char* fileOut = "bla.pdf";

   gStyle->SetLabelSize(0.06,"xyz");
   gStyle->SetLabelOffset(0.012,"xy");
   gStyle->SetTitleOffset(1.05,"x");
   gStyle->SetTitleOffset(1.15,"y");
   gStyle->SetTitleSize(0.065,"xy");
   gStyle->SetTitleBorderSize(0);
   gStyle->SetOptStat("");

   TLorentzVector* p4Pi = new TLorentzVector();
   double mPi = 0.13957;
   p4Pi->SetXYZM(0,0,p0,mPi);

   double binW = 200/2595.;
   double thMax =1.5;
   auto hPol = new TH2D("hPol", "",40,-TMath::Pi(),TMath::Pi(),90,0,thMax);
   auto hTh  = new TH1D("hTh"  , ";#theta_{#nu#pi} [#circ]; evts per sr",90,0,thMax);

   hPol->GetZaxis()->SetMaxDigits(3);
   hTh->GetYaxis()->SetMaxDigits(3);

   double bW = hTh->GetBinWidth(2)*TMath::DegToRad();
   double bWPhi = 2*TMath::Pi()/40;

   auto cv = new TCanvas();
   double mMu = 0.10565837;
   double massDecay[2] = {mMu,0.};
   TGenPhaseSpace generator;

   int nP=100;

   for (int ip(0); ip < nP; ip++) {
      int nPh = int(gRandom->Poisson(1000.));
      p4Pi->SetXYZM(0,0,p0,mPi);
      generator.SetDecay(*p4Pi, 2, massDecay);

      for (int i(0); i<nPh ; i++) {
         // generate
         Double_t weight = generator.Generate();

         // true
         TLorentzVector* p4Mu = generator.GetDecay(0);
         TLorentzVector* p4Nu = generator.GetDecay(1);
         double th  = p4Nu->Vect().Theta();
         double phi = p4Nu->Vect().Phi();

         hPol->Fill(phi,th*TMath::RadToDeg(),weight/nP / bW / bWPhi);
         //hPol.Fill(phi,th*TMath::RadToDeg(),weight/nP);
         hTh->Fill(th*TMath::RadToDeg()     ,weight/nP / bW / (2*TMath::Pi()));
      }
   }

  hPol->Draw("lego2 polz");

  hPol->SetLineColorAlpha(kBlack, 0.0);
  cv->SetGrid(0,0);
  cv->SetRightMargin(0.15);
  cv->SetTheta(90.);
  cv->SetPhi(0.);
  gPad->Update();

  auto gp = new TGraphPolargram("g",0,thMax, -180., 180.);
  gp->SetToDegree ();
  gp->SetNdivPolar(8);
  gp->SetNdivRadial(4);
  gp->SetLineColor(1);
  gp->Draw();
}

This Marco super-impose a 3D pol and a 2D polargram. Superimposing is not straightforward in that case. I am looking for a solution with an extra pad…

I found a way…

void mperrint() {

   double p0 = 25;
   const char* fileOut = "bla.pdf";

   gStyle->SetLabelSize(0.06,"xyz");
   gStyle->SetLabelOffset(0.012,"xy");
   gStyle->SetTitleOffset(1.05,"x");
   gStyle->SetTitleOffset(1.15,"y");
   gStyle->SetTitleSize(0.065,"xy");
   gStyle->SetTitleBorderSize(0);
   gStyle->SetOptStat("");

   TLorentzVector* p4Pi = new TLorentzVector();
   double mPi = 0.13957;
   p4Pi->SetXYZM(0,0,p0,mPi);

   double binW = 200/2595.;
   double thMax =1.5;
   auto hPol = new TH2D("hPol", "",40,-TMath::Pi(),TMath::Pi(),90,0,thMax);
   auto hTh  = new TH1D("hTh"  , ";#theta_{#nu#pi} [#circ]; evts per sr",90,0,thMax);

   hPol->GetZaxis()->SetMaxDigits(3);
   hTh->GetYaxis()->SetMaxDigits(3);

   double bW = hTh->GetBinWidth(2)*TMath::DegToRad();
   double bWPhi = 2*TMath::Pi()/40;

   auto cv = new TCanvas();
   double mMu = 0.10565837;
   double massDecay[2] = {mMu,0.};
   TGenPhaseSpace generator;

   int nP=100;

   for (int ip(0); ip < nP; ip++) {
      int nPh = int(gRandom->Poisson(1000.));
      p4Pi->SetXYZM(0,0,p0,mPi);
      generator.SetDecay(*p4Pi, 2, massDecay);

      for (int i(0); i<nPh ; i++) {
         // generate
         Double_t weight = generator.Generate();

         // true
         TLorentzVector* p4Mu = generator.GetDecay(0);
         TLorentzVector* p4Nu = generator.GetDecay(1);
         double th  = p4Nu->Vect().Theta();
         double phi = p4Nu->Vect().Phi();

         hPol->Fill(phi,th*TMath::RadToDeg(),weight/nP / bW / bWPhi);
         //hPol.Fill(phi,th*TMath::RadToDeg(),weight/nP);
         hTh->Fill(th*TMath::RadToDeg()     ,weight/nP / bW / (2*TMath::Pi()));
      }
   }

   hPol->Draw("lego2 polz");

   hPol->SetLineColorAlpha(kBlack, 0.0);
   cv->SetRightMargin(0.15);
   cv->SetTheta(90.);
   cv->SetPhi(0.);
   gPad->Update();

   auto PPad = new TPad("p","p",0.01,0.,.94,1.);
   PPad->SetFillStyle(4000);
   PPad->Draw();
   PPad->cd();
   auto gp = new TGraphPolargram("g",0,thMax, -180., 180.);
   gp->SetToDegree ();
   gp->SetNdivPolar(8);
   gp->SetNdivRadial(4);
   gp->SetLineColor(1);
   gp->Draw();
}

1 Like

Thanks a lot! It works :slight_smile:

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