Get pixel coordinates interactively

Hi,

When a TCanvas is opened and an image is displayed, how to get the coordinates(x, y values) of my cursor’s position interactively?

I know that we could use ToggleEventStatus to display the coordinates with a toolbar, but how to output the coordinates of my current cursor’s position into a file/stdout?

Sorry if it is a silly question, I am a new user in root, so I am appreciate your help.

Vicky

See examples in tutorials like
-triangles.C
-DynamicSlice.C
-exec1.C

Rene

Hi Rene,

Thanks for your reply. I got the tutorials and I have tried the tutorials suggested method. But I still can’t get the coordinates printing on stdout. Actually, it can’t select the object.

Here is part of my code:

  TObject *select = gPad->GetSelected();
  if(!select) {cout << "can't select" << endl ; return;}
  if(!select->InheritsFrom("TH2")) {gPad->SetUniqueID(0); return;}
  TH2 *h = (TH2*)select;
  gPad->GetCanvas()->FeedbackMode(kTRUE);

  int px = gPad->GetEventX();
  int py = gPad->GetEventY();
  cout << px << endl;

I got “can’t select” when I right-click the mouse on the image.

Thanks in advance,
Vicky

Use the left button.
When clicking in a pad,
-the middle button is reserved to set the current pad
-the right button is reserved for the context menu

Rene

Hi Vicky,
check also root.cern.ch/root/rqex/rqfiller.C

Regards. Valeriy

Hi all,

I tried to use the left button but it doesn’t work either. The situation is that I got the message “can’t select” when I moved my mouse away from the canvas. Nothink happen when I click the mouse left, middle, or right button.

I only have one Pad, i.e. no pad is defined, only TCanvas. Is it a problem?

Thanks a million for your help.
Vicky

Please send more info of what you do, including a concrete piece of code and the way you invoke it

Rene

Hi Rene,

I am using astro-root’s AstroImage to display a fits file on a Canvas and want to get the coordinates (x,y values) of my mouse position when I click the mouse.

Here is my code:

int display()
{
gROOT->Reset();
// read an FITS image using fitsfile format for iraf images
  using namespace std;

   // The image we want to display
   fitsfile *fptr;
   int status = 0;
   if (ffopen(&fptr, "1es1959.fits", READONLY, &status))
      {
      ffrprt(stderr, status);
      return;
      }

   // open a window and open the status bar to display the 
   // coordinates and value of the image pixel at the mouse position
   TCanvas * win = new TCanvas("image", "fits_display", 100, 100, 550, 524);
   win->ToggleEventStatus();

   // create the image display for the just read image
   AstroImage * disp = new AstroImage(fptr);

   // read a color palette, it was written to the file via the
   // color editor (Save button)
   TFile *fpal = new TFile("fits2.pal.root", "READ");
   TImagePalette *palette = (TImagePalette*)fpal->Get("TImagePalette");
   delete fpal;

   // use the palette for this image
   disp->SetPalette(palette);
   delete palette;

   // draw the image
   disp->Draw();

  //Add a TExec object to the canvas
  win->AddExec("ex", "DynamicCoordinates()");

  return 0;
}

void DynamicCoordinates()
{

  TObject *select = gPad->GetSelected();
  if(!select) {cout << "can't select" << endl ; return;}
  if(!select->InheritsFrom("TH2")) {gPad->SetUniqueID(0); return;}
  TH2 *h = (TH2*)select;
  gPad->GetCanvas()->FeedbackMode(kTRUE);

  int px = gPad->GetEventX();
  int py = gPad->GetEventY();
  cout << px << "   " << py << endl;

}

I got “can’t select” when I moved my mouse out of the canvas. When the mouse was inside the canvas, then I clicked it, but nothing happened to me. Where did I do wrong?

Thanks for your help.

Regards,
Vicky

It looks like your AstroImage class does not have a DistancetoPrimitive function implemented, so gPad->GetSelected returns 0.
Could you move the lines
int px = gPad->GetEventX();
int py = gPad->GetEventY();
cout << px << " " << py << endl;

at the start of DynamicCoordinates() ?

see also: root.cern.ch/root/HowtoPick.html

Rene

Hi Rene,

Actually, I tried to just use

int px = gPad->GetEventX();
int py = gPad->GetEventY();
cout << px << " " << py << endl;

for function “DynamicCoordinates”, this time, I got printout on the screen whenever I moved my mouse, not only I clicked it. As a result, I got many useless data points. Is there any way I could do to printout data only when I click the mouse?

As for the AstroImage class, I have no idea on it. When I used the ToggleEventStatus(), I got the coordinates displayed on the status bar, could I get this information printout to screen when I click the mouse?

Thanks again.

Regards,
Vicky

Hi all.

Thanks very much for all your help. Finally, I solve the problem.

I used GetEvent() instead of GetSelected() to activate the printout to screen.

int event = gPad->GetEvent();
if(event !=1 ) return;
cout << px << " " << py << endl;

“event = 1” is the action when we left-click the mouse.

Thanks again.

Vicky :astonished: