fabstr
October 15, 2021, 1:13pm
1
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();
}
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);
fabstr
October 15, 2021, 3:05pm
4
Contour_data.txt (1.3 KB)
I cut the size of the file because of the file limit
fabstr
October 15, 2021, 3:42pm
5
I implemented the code, but nothing happens.
fabstr
October 16, 2021, 10:09am
6
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”).
fabstr
October 17, 2021, 11:27am
10
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;