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;
}
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()");
}
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.
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;
}
}