Removing the area outside a circle


Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.22/00
Platform: Ubuntu 20.04.1 LTS (WSL2)
Compiler: GNU or GCC


Hello,
I have created a contour plot of the magnetic flux density of an octupole magnet.
I want to remove everything outside the area beyond the radius of 0.49 meters.
How do I do that?


#include  <iostream

using namespace std;

void Contour_Plot(){
   const Int_t NCont = 10000;
   gStyle->SetNumberContours(NCont);
   auto g = new TGraph2D("data.txt","%lg %lg %lg");
   g->SetNpx(200);
   g->SetNpy(200);
   g->SetTitle("The contour plot");
   g->GetHistogram()->GetXaxis()->SetTitle("x (m)");
   g->GetHistogram()->GetYaxis()->SetTitle("y (m)");
   g->SetLineWidth(1);
   g->SetLineStyle(1);
//   g->Draw("colz cont6 ");
  TCanvas *c = new TCanvas("c", "c");
  g->Draw("COLZ");
  g->GetHistogram()->GetYaxis()->SetRangeUser(-0.10, 0.10);
  g->GetHistogram()->GetXaxis()->SetRangeUser(-0.10, 0.10);
  c->SetRealAspectRatio();
  c->Modified(); c->Update();

  TEllipse *e = new TEllipse(0., 0., 0.050); e->SetFillStyle(0); e->Draw();
  gPad->Modified(); gPad->Update();

}

can you post data.txt ?

Double_t r2 = std::pow(0.050, 2.); // the radius squared
for (Int_t i = g->GetN() - 1; i >= 0; i--)
  if (std::pow(g->GetX()[i], 2.) + std::pow(g->GetY()[i], 2.) > r2) g->RemovePoint(i);

Contour_data.txt (1.3 KB)
I cut the size of the file because of the file limit

I implemented the code, but nothing happens.

I made following modifications:
But nothing happens.

#include “TFile.h”
#include “TTree.h”
#include “TCanvas.h”
#include “TH1F.h”
#include <iostream

using namespace std;

void Contour_Plot(){
const Int_t NCont = 20000;
gStyle->SetNumberContours(NCont);
auto g = new TGraph2D(“data.txt”,“%lg %lg %lg”);
g->SetNpx(200);
g->SetNpy(200);
g->SetTitle(“The contour plot”);
g->GetHistogram()->GetXaxis()->SetTitle(“x (m)”);
g->GetHistogram()->GetYaxis()->SetTitle(“y (m)”);
g->SetLineWidth(1);
g->SetLineStyle(1);

// g->Draw("colz cont6 ");
TCanvas *c = new TCanvas(“c”, “c”);
g->Draw(“COLZ”);
//g->GetHistogram()->GetYaxis()->SetRangeUser(-0.10, 0.10);
//g->GetHistogram()->GetXaxis()->SetRangeUser(-0.10, 0.10);
c->SetRealAspectRatio();
c->Modified(); c->Update();

Double_t r2 = std::pow(0.050, 2.); // radius squared
for (Int_t i = g->GetN() - 1; i >= 0; i–)
if (std::pow(g->GetX()[i], 2.) + std::pow(g->GetY()[i], 2.) > std::pow(0.050, 2.)) g->RemovePoint(i);

TEllipse *e = new TEllipse(0., 0., 0.050); e->SetFillStyle(0); e->Draw();
gPad->Modified(); gPad->Update();

}

You should execute “RemovePoint” before “Draw” (and probably before “GetHistogram”).

Tried to move the removepoint before Draw and GetHistogram, but the script was stuck in loading.
Have I forgot something in my code?

using namespace std;
void Contour_Plot(){
const Int_t NCont = 2000;
gStyle->SetNumberContours(NCont);
auto g = new TGraph2D(“Contour_data.txt”,“%lg %lg %lg”);
g->SetNpx(200);
g->SetNpy(200);
g->SetTitle(“Contour plot of the magnetic flux density (T) in the x-y plane”);
Double_t r2 = std::pow(0.050, 2.); // radius squared
for (Int_t i = g->GetN() - 1; i >= 0; i–)
if (std::pow(g->GetX()[i], 2.) + std::pow(g->GetY()[i], 2.) > std::pow(0.050, 2.)) g->RemovePoint(i);
g->GetHistogram()->GetXaxis()->SetTitle(“x (m)”);
g->GetHistogram()->GetYaxis()->SetTitle(“y (m)”);
g->SetLineWidth(1);
g->SetLineStyle(1);
// g->Draw("colz cont6 ");
TCanvas *c = new TCanvas(“c”, “c”);
g->Draw(“COLZ”);
//g->GetHistogram()->GetYaxis()->SetRangeUser(-0.10, 0.10);
//g->GetHistogram()->GetXaxis()->SetRangeUser(-0.10, 0.10);
c->SetRealAspectRatio();
c->Modified(); c->Update();
TEllipse *e = new TEllipse(0., 0., 0.050); e->SetFillStyle(0); e->Draw();
gPad->Modified(); gPad->Update();
}

Try with a smaller number of contours, e.g.: const Int_t NCont = 20;

Thanks it worked