Insert points at the mouse position in a TGraph


_ROOT Version: 6.14/02
_Platform:Ubuntu18.04
Compiler: Not Provided


Hi,

I got a problem on TGraph class. In order to insert a point in the position where my mouse is, I write a function like this:

#include “TCanvas.h”
#include “TPad.h”
bool InsertPoint2(TPad *c, TGraph *g)
{
c->cd();

 Int_t px = c->GetEventX();
 Int_t py = c->GetEventY();
 cout<<" px= "<<setw(8)<<px<<"; py= "<<setw(8)<<py<<endl;

 double ax=c->AbsPixeltoX(px);
 double ay=c->AbsPixeltoY(py);
 
 cout<<" ax= "<<setw(8)<<ax<<"; ay= "<<setw(8)<<ay<<endl;

 double xmin,xmax,ymin,ymax;

 cout<<" Axis range is:\n";
 c->GetRangeAxis(xmin,ymin,xmax,ymax);
 cout<<" xmin="<<setw(8)<<xmin<<"; xmax="<<setw(8)<<xmax<<"; ymin="<<setw(8)<<ymin<<"; ymax="<<setw(8)<<ymax<<endl;
 if(ax>=xmin&&ax<=xmax&&ay>=ymin&&ay<=ymax)
 {
   g->SetPoint(g->GetN(),ax,ay);
   c->Update();
   return true;
 }
 else
 {
   cout<<"   Out of range!"<<endl;
   return false;
 }

}

Any how, it works in the command by calling it like this:

bool insertstate=InsertPoint2(c1,g1)

But when I call this function in a script, the mouse position can’t be updated, it’s always in the same position.
Here is the code:

 cin.sync();
 char c;
cip->cd();
cout<<"	Input [Y] when mouse is in the correct place!(Input 'N' to quit):";
while(cin>>c)
 {
 	cout<<"	Your input is:"<<int(c)<<endl;
 	if(c=='Y'||c=='y')
 	{
 						
	insertstate=InsertPoint2(cip,g3c[editline-1]);
 		if(insertstate)
 		cout<<"	Insert point to line NO."<<setw(3)<<editline<<" Succeed!\n";
 		else
 		cout<<"	Insert point to line NO."<<setw(3)<<editline<<" Failed!\n";
 	}
 	else if(c=='N'||c=='n')
 		{
 			cout<<" Line NO."<<setw(2)<<editline<<" has done!\n";
 			break;
 		}
	else
	{
 
	cout<<"	Invalid input!\n";
	cout<<"	Input [Y] when mouse is in the correct place!(Input 'N' to quit):";
	}
	cin.sync();

}

Why this happened?

Why don’t you use the “InsertPoint” menu item when you right click on a TGraph ?

InsertPoint need you to move the inserted point to the correct place since the new point’s initial position is on the line of two adjacent points. Anyhow it could be an option if this problem can’t be solved. Thank you for your suggestion.

you 2nd macro is not really runable a as it is . can you provide a self contained macro reproducing the problem ?

This script may not be exactly reproduced my problem since the TGraph can’t be show on the canvas when the function InsertPoint2() was called(By the way, do you know why?:joy: Actually this is also a problem for my work.)

#include “insertpoint2.cpp”
int main()
{
{
TCanvas *c1=new TCanvas(“c1”,“c1”,1000,800);
c1->Draw();
TGraph g1=new TGraph();
g1->SetPoint(0,0,0);
g1->SetPoint(1,100,100);
g1->Draw("ap
");
c1->Update();
char c;
bool insertstate=false;
cout<<" Input [Y] when mouse is in the correct place!(Input ‘N’ to quit):";
while(cin>>c)
{
cout<<" Your input is:"<<int©<<endl;
if(c==‘Y’||c==‘y’)
{

  		insertstate=InsertPoint2(c1,g1);
  		if(insertstate)
  			cout<<"	Insert point to line Succeed!\n";
  		else
  			cout<<"	Insert point to line Failed!\n";
  	}
  	else if(c=='N'||c=='n')
  	{
  		cout<<" Line has done!\n";
  		break;
  	}
  	else
  	{

  		cout<<"	Invalid input!\n";
  		cout<<"	Input [Y] when mouse is in the correct place!(Input 'N' to quit):";
  	}
  	cin.sync();
  }

}
return 0;
}

insertpoint.C (658 Bytes)

:star_struck::star_struck::star_struck:Thank you very much! Perfect code!

1 Like

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