Get histogram coordinates with mouse

Hi,

I would like to get the coordinates from a histogram when clicking with the mouse.
For now, I’ve something like that :

void click(){
	TFile *f = new TFile("........","READ");
	TCanvas *c1 = new TCanvas("c1","c1",200,10,800,600);
	char histname[30];
	int i = 1; sprintf(histname, "SiPM_tot;%d", i); 
	TH1F *h = (TH1F*)f->Get(histname);
	h->Draw();

	c1->AddExec("ex","Clicked()");
}

void Clicked() {
	int event = gPad->GetEvent();
	if (event != 11) return;
	TObject *select = gPad->GetSelected();
	if (!select) return;
	if (select->InheritsFrom("TH1F")) {
	TH1F *h = (TH1F*)select;
	int px = gPad->GetEventX();
	int py = gPad->GetEventY();
	cout << px << endl;
	}
}

It doesn’t work, and I think that the problem comes from select->InheritsFrom(“TH1F”) because I select a point on the histogram. How to solve it ?

Moreover, what mean if (event != 11) return;. I didn’t find something about the value which is returned by gPad->GetEvent();.

Thanks a lot,
Nicolas

It works, but know, I want to click only a few time and save the position in a variable. I try to make only one click with the following code but the histogram isn’t drawn before the line x = Clicked(); and it’s return -1.


int Clicked() {
	float x=0;
	float y=0;
   	int event = gPad->GetEvent();
   	if (event != 11) {event = gPad->GetEvent();};
	int px = gPad->GetEventX();
	int py = gPad->GetEventY();
   	TObject *select = gPad->GetSelected();
   	if (!select) return -1;
      	Float_t xx = gPad->AbsPixeltoX(px);
     	Float_t yy = gPad->AbsPixeltoY(py);
	x  = gPad->PadtoX(xx);
     	y  = gPad->PadtoY(yy);
	return x;
}

void click(){
	TFile *f = new TFile(".....","READ");
	TCanvas *c1 = new TCanvas("c1","c1",200,10,800,600);
	char histname[30];
	float x=0;
	float y=0;
	int i = 1;
	sprintf(histname, "SiPM_tot;%d", i); 
	TH1F *h = (TH1F*)f->Get(histname);
	h->Draw();
	
	x = Clicked();
	cout << x << endl;
}

Nicolas

May be using TExec as shown here can be the solution:

root.cern.ch/doc/master/mandelbrot_8C.html

Thanks,

I took a look at this code, but it seems to be the same problem.
The function myexec only print on screen without writing any variable in main function.

Moreover, it’s called every time an event is produced but I would like to do it only a few time.

In this example the points are cumulated in a TGraph

TGraph *g;
Int_t i = 0;

void myexec()
{
   // get event information
   int event = gPad->GetEvent();
   int px    = gPad->GetEventX();
   int py    = gPad->GetEventY();

   // some magic to get the coordinates...
   double xd = gPad->AbsPixeltoX(px);
   double yd = gPad->AbsPixeltoY(py);
   float x = gPad->PadtoX(xd);
   float y = gPad->PadtoY(yd);
   
   if (event==1) { // left mouse button click
      g->SetPoint(i,x,y);
      if (i==0) g->Draw("L");
      i++;
      gPad->Update();
      return;
   }
}


void click()
{
   // draw hpx from hsimple.root
   hpx->Draw();

   g = new TGraph();

   // add exec
   gPad->AddExec("myexec","myexec()");
}

Thanks, I see.

Now, I would like to store these data in an array and to stop looking for events in the pad. How to do that since the function myexec doesn’t return anything ?

I try to use DeleteExec to stop it but it doesn’t go back to the program.

g is global you can access it directly

Yes, and how can I stop it after N points ?

I would like to do it for several histogram :

  • open histo ;
  • save some points ;
  • stop exec and do some processes ;
  • continue with an other histo.
  1. you Draw an histo
  2. you execute the macro which add a TExec to the Pad
  3. you click until you have enough point (it can be a fix loop in TExec or a special character you hit)

I wrote the following code, It works (a little bit …). But after few points, the time before the result is print become long and then the program gets blocked. Any idea ? Maybe a memory problem ?

float x = 0, y = 0;

void trig(TCanvas *c){
	c->AddExec("ex","Clicked()");
	gPad->WaitPrimitive();
	c->DeleteExec("Clicked()");
	
}
void click(){
	TFile *f = new TFile("...","READ");
	TCanvas *c1 = new TCanvas("c1","c1",200,10,800,600);
	char histname[30];

	for(int i=1;i<=6;i++){;
	sprintf(histname, "hist%d", i); 
	cout << histname << endl;
	TH1F *h = (TH1F*)f->Get(histname);
	h->Draw();	
	gPad->Update();
	
	trig(c1); cout << " x = " << x << " ; y = " << y << endl;	
	trig(c1); cout << " x = " << x << " ; y = " << y << endl;	

	delete h;
	}
}

void Clicked() {
 	int event = gPad->GetEvent();
	int px = gPad->GetEventX();
	int py = gPad->GetEventY();	

   	if (event==1) { // left mouse button click
   		if (!gPad->GetSelected()) return;
      		Float_t xx = gPad->AbsPixeltoX(px);
     		Float_t yy = gPad->AbsPixeltoY(py);
		x  = gPad->PadtoX(xx);
     		y  = gPad->PadtoY(yy);
		return;
	}
}