Rotate TEllipse

Relevant Information:
ROOT Version: 6.04.02
Platform: 3.16.0-4-amd64 GNU/Linux
Compiler: gcc version 4.9.2 (Debian 4.9.2-10+deb8u1)

I am trying to draw a rotated ellipse on a TH2. I would expect this to be straight forward, but I guess I do not understand something very basic. Here is a minimal example showing my problem. Using the attached ROOT file and this script:

void minimal() {

  TFile* fIn = new TFile("minimal.root","READ");
  TH2F* hist = (TH2F*) fIn->Get("zBands");

  TCanvas* canvas = new TCanvas("c1","c1");
  gPad->SetLogz();
  hist->Draw("colz");

  TEllipse* ellipse = new TEllipse(232, 7036, 10, 50, 0, 360);
  //TEllipse* ellipse = new TEllipse(232, 7036, 10, 50, 0, 360, -45);
  ellipse->SetLineColor(1);
  ellipse->SetLineWidth(3);
  ellipse->SetFillColor(0);
  ellipse->SetFillStyle(4000);
  ellipse->Draw("same");

}

produces this plot:

But I want the ellipse to be rotated by -45 degrees (the “-” sign means clockwise) in order to outline the distribution on the plot. So I added a rotate argument (the line that is commented out in the above script). This produces:

Which is absolutely not what I expected based on the example from https://root.cern.ch/doc/v606/classTEllipse.html. I’ve tried a lot of other things since then and have had no luck. All I want is to rotate the ellipse in the first plot by -45 degrees. I must be missing something something very basic. Any help is appreciated.

The ROOT file that contains the TH2 is here:
minimal.root (186.3 KB)

Indeed it works. Try the following:

void RotateEllipse()
{
   TCanvas *C = new TCanvas("C", "C",0,0,700,700);
   for (double angle=0; angle<360; angle = angle+5.) {
      TEllipse *ellipse = new TEllipse(0.5, 0.5, 0.4, 0.05, 0, 360, angle);
      ellipse->SetFillStyle(0);
      ellipse->Draw();
   }
}

I’m afraid your example doesn’t explain why the original code fails.

Oh yes it might be because of the range … let me improve…

Hi couet,

Thanks for the reply. Unfortunately, this example does not solve the problem in my original post. I am also confused since your response does not address my minimal example. To repeat the main points:

  1. I have a TH2, and want to draw a rotated ellipse on it.
  2. I can draw an ellipse (not rotated), which produces the first plot in my original post
  3. I add the rotate argument, which produces a nonsense result in the second plot

Again, all I want to do is draw a rotated ellipse on my TH2. It can’t be this hard, right? Any help is appreciated. Thanks.

BTW. Do not miss: ROOT -> Forum -> Search -> color palette Logz

void DrawEllipse(double x1, double y1, double r1, double r2, double angle)
{

   double px1, py1, px2, py2;
   gPad->Update();
   gPad->GetRange (px1, py1, px2, py2);
   auto pE = new TPad();
   pE->Draw();
   pE->SetFillStyle(0);
   pE->cd();
   double x1n = (x1-px1)/(px2-px1);
   double y1n = (y1-py1)/(py2-py1);
   double r1n = r1/(px2-px1);
   double r2n = r2/(py2-py1);;
   TEllipse* ellipse = new TEllipse(x1n, y1n, r1n, r2n, 0., 360., angle);
   ellipse->SetLineColor(1);
   ellipse->SetLineWidth(3);
   ellipse->SetFillStyle(0);
   ellipse->Draw();
}

void minimal() {
   TFile* fIn = new TFile("minimal.root","READ");
   TH2F* hist = (TH2F*) fIn->Get("zBands");

   TCanvas* canvas = new TCanvas("c1","c1");
   gPad->SetLogz();
   hist->Draw("colz");
   DrawEllipse(232, 7036, 10, 70, 155.);
}

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