How to detect a mouse click on an object

Dear all,
I am trying to do something when cliking a TMarker drawn on a canvas.
I wrote the following code based on

void MyClass::Draw()
  if (! cm) cm = new TCanvas("cm", "MyClass", 600, 600);
  cm->DrawFrame(-150, -150, 150, 150);
  g->Draw("contzsame"); // g is a pointer to a TGraph2D object

  for (int i = 0; i < g->GetN(); ++i) {
    TMarker *m = new TMarker(g->GetX()[i], g->GetY()[i], 20);

  cm->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "MyClass", this,

void MyClass::DrawProfile(Int_t event, Int_t x, Int_t y, TObject *selected)
  TCanvas *c = (TCanvas *) gTQSender;
  if (event == kButton1Down) {
    TString objName = selected->IsA()->GetName();
    if (objName == "TMarker") {
      TMarker *m = (TMarker *) selected;
      int iDet = m->GetUniqueID(); // iDet
      std::cout << "Marker #: " << iDet << std::endl;
      // do something

But when another object such as a TLine is also drawn on the canvas overlapping with the markers,
the ProcessedEvent() always recognize a cllicked object as a line and not as a marker.
TMarker::Pop() did not fix this problem.
Is there any way to detect a click on a specific kind of object overlapping with other objects?

Thank you.

Can you send a small macro (I can run) reproducing the problem ? It is true that if the TLine is on top of the TMarker it will be seen first. But “popping” the marker should work (or send the TLine to the back).

thank you for your reply.

I figured out the solution.
I was trying to pop only overlapping markers with a line.
I don’t know why, but this was not enough.
Somehow, only after executing Pop() for all markers on the canvas, they are actually poped and respond to a click.
Thank you for your advice.