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.
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.
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?
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?
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;
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?