# Can I add buttons on a TGraph?

**URL:** <https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 20, 2024, 2:50pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707 "2024-06-20T14:50:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 20, 2024, 2:50pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/1 "2024-06-20T14:50:52Z")

</div>

Dear experts

I wonder if one can add buttons on a TGraph, to be more specific, I have a event display of drift chamber like this:

 ![run2060entry4InitialTrack](https://root-forum.cern.ch/uploads/default/original/3X/e/3/e3b7c4c13f4a3265f57ce1bb5b628fdfa46a7443.jpeg)  
And dots represents the hits (wire positions) in the chamber. I want to make all hits (red dots) to buttons that when I click that button the waveform of that channel will be showed in a new window.

Any suggestion will be helpful! Even just telling me “It’s impossible”

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2024, 6:18pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/2 "2024-06-20T18:18:52Z")

</div>

Hi Siyuan,

Very interesting use case!

I do not think it’s something ROOT supports out of the box. I am adding our experts in the loop @linev @bellenot .

Best,  
D

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 20, 2024, 6:30pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/3 "2024-06-20T18:30:10Z")

</div>

Hi @Crisps,  
maybe this part on the TGraphPainter can be useful.  
And maybe you could have a look also a these tutorials  
[Tutorial 1](https://root.cern.ch/doc/master/hlGraph1_8C.html)  
[Tutorial2](https://root.cern.ch/doc/master/hlGraph2_8C.html)

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 1:32am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/4 "2024-06-21T01:32:40Z")

</div>

Thank you for the reply! Which part you mean “this part”? maybe you forgot to add link on it…?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [June 21, 2024, 5:29am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/5 "2024-06-21T05:29:10Z")

</div>

Sorry I meant to put [this link](https://root.cern.ch/doc/master/classTGraphPainter.html#GrP8)

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 5:30am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/6 "2024-06-21T05:30:06Z")

</div>

I have read through these two tutorials, and I got what you mean “this part”, now I have one question related to the function in `TCanvas::HighlightConnect()` : `UserFunction(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)`  
In my understanding this function will be called when the mouse is pointing to the ith point on the TGraph (or histogram). But what I need is:  
I have a map of `TGraph`s which is channel to waveform (not all channels have waveform), so I use `TGraph::SetPoint(channel, x, y)` to set the point on the TGraph. The problem is the point that I didn’t set (let’s say I set 2,4,6th point) will be created automatically and result on the TGraph, is there a way to disable auto creation?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 6:57am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/7 "2024-06-21T06:57:21Z")

</div>

I solved the problem above by moving unused points out of window (99999, 99999). If you have better (or decent) solution please teach me!

Now I got another question:  
I want to make the `HighlightGraph(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)` as a member function in the same class, but when I connected it to canvas:

```auto
c->HighlightConnect("HighlightGraph(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)");

```

it is never called at runtime even I can see the circle highlight on the TGraph.

then I was thinking maybe class member function is different from function pointer, so I made it a function pointer:

```auto
std::function<void(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)> func = [this](TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y){
                this->HighlightGraph(pad, obj, ihp, y);
};
c->HighlightConnect("func(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)");

```

but it still didn’t work…any help?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 7:14am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/8 "2024-06-21T07:14:55Z")

</div>

And I also tried this:

```auto
c->Connect("Highlighted(TVirtualPad*, TObject*, Int_t, Int_t)", "EventDisplay", this, "HighlightGraph(TVirtualPad*, TObject*, Int_t, Int_t)");
g->SetHighlight();

```

but didn’t work ☹

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 21, 2024, 7:24am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/9 "2024-06-21T07:24:11Z")

</div>

Hi,

`TCanvas::HighlightConnect()` method is simplified version which only allows to connect global functions, but not objects methods. See [docu](https://root.cern/doc/master/classTHistPainter.html#HP30). You can use TQObject::Connect():

```auto
   auto receiver = new ReceiverClass("receiver");

   auto Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 500);

   Canvas->Connect("Highlighted(TVirtualPad*,TObject*,Int_t,Int_t)",
                   "ReceiverClass", receiver, "HighlightHisto(TVirtualPad*,TObject*,Int_t,Int_t)");

```

I attach modified version of hlGraph1.C tutorial macro:

[hlGraph1.C](https://root-forum.cern.ch/uploads/short-url/d2ID0JKWAwGcio16LrTuc3IRLgX.C) (1.8 KB)

Do you have dictionary for your class?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 7:45am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/10 "2024-06-21T07:45:04Z")

</div>

Thank you for the reply, I have tried this method (my last post) but it didn’t work and give this:

```auto
Error in <TQObject::CheckConnectArgs>: slot HighlightGraph(TVirtualPad*,TObject*,int,int) does not exist

```

But as you pointed my class `EventDisplay` is no derived from ROOT classes, so there is no dictionary of it. Should I derive it from `TNamed` and make a dictionary?

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 21, 2024, 7:49am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/11 "2024-06-21T07:49:30Z")

</div>

Hi,

When code runs as macro ROOT automatically creates dictionary for all used classes.  
If you build and run your application differently - you need to create dictionary for your `EventDisplay` class. It is not necessary to derive it from `TObject`.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 7:55am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/12 "2024-06-21T07:55:49Z")

</div>

I tried to add this line in my code:

```auto
gInterpreter->GenerateDictionary("EventDisplay", "EventDisplay.hxx");

```

but got this:  
fatal error: ‘EventDisplay.hxx’ file not found

#include “EventDisplay.hxx”

then I modified to:

```auto
gInterpreter->GenerateDictionary("EventDisplay", "/Users/siyuan/Physics/comet/crt/tracking/src/EventDisplay.hxx");

```

but still got the same error…

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 21, 2024, 8:00am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/13 "2024-06-21T08:00:58Z")

</div>

No, you need to add `ROOT_GENERATE_DICTIONARY` to your `CMakeLists.txt` file.

See it in ROOT examples: [root/test/periodic/CMakeLists.txt at master · root-project/root · GitHub](https://github.com/root-project/root/blob/master/test/periodic/CMakeLists.txt#L19)

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 8:11am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/14 "2024-06-21T08:11:41Z")

</div>

Thank you, in that `CMakeLists.txt`, what is `NdbLinkDef.h`?  
should I write `#pragma link C++ class EventDisplay+;` in that header file?

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 21, 2024, 8:14am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/15 "2024-06-21T08:14:59Z")

</div>

Yes, you need to create new `LinkDef.h` file with minimal content:

```auto
#ifdef __CLING__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class EventDisplay+;

#endif

```

You can include your other classes - if it is required.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 8:28am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/16 "2024-06-21T08:28:49Z")

</div>

I tried to make a similar version `CMakeLists.txt` but got totally lost…  
What does these variables refer…?

```auto
file(GLOB DBsources RELATIVE ${CMAKE_SOURCE_DIR} Ndb*.cxx)
file(GLOB XSsources RELATIVE ${CMAKE_SOURCE_DIR} XS*.cxx)
file(GLOB headers RELATIVE ${CMAKE_SOURCE_DIR} *.h)
list(REMOVE_ITEM headers NdbLinkDef.h)

```

for example I have `EventDisplay.hxx`, `EventDisplay.cxx` and `EventDisplay_LinkDef.h`, which one should fill in where?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 21, 2024, 8:29am UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/17 "2024-06-21T08:29:01Z")

</div>

Surely the approach you are following now is possible one. However, let me propose another one. I modified an example I have (graphtext3.C) to retrieve which point has been clicked in a TGraph. Once you have it, you are then free to draw whatever you want. In this example, I redraw the graph (as I mentioned, this is an example I already had).

```auto
const int Npoints = 3; // Number of points in the graph
const double x[Npoints] = {1.0, 2.0, 5.0};
const double y[Npoints] = {0.5, 1.0, 2.0};
const char *c[Npoints] = {"a","b","c"};

void graphtext3() {
   TGraph *gr = new TGraph(Npoints, x, y);
   gr->SetMarkerStyle(kCircle);
   TExec *ex = new TExec("ex","DrawText();");
   gr->GetListOfFunctions()->Add(ex);

   gr->Draw("ALP");
   gr->GetXaxis()->SetLabelSize(0);
   gr->GetXaxis()->SetTickLength(0);
}

void DrawText()
{
   Int_t i,n;
   Double_t x,y;
   TLatex *t;
   TLine *l;
   double eps = 0.01;

   TGraph *g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");

   // Get event information
   int event = gPad->GetEvent();
   int px = gPad->GetEventX();
   int py = gPad->GetEventY();

   // Get the coordinates
   double xd = gPad->AbsPixeltoX(px);
   double yd = gPad->AbsPixeltoY(py);
   float xp = gPad->PadtoX(xd);
   float yp = gPad->PadtoY(yd);
   n = g->GetN();
   for (i=0; i<n; i++) {
      g->GetPoint(i,x,y);
      if (TMath::Abs(x-xp)<eps && TMath::Abs(y-yp)<eps) printf("Point %d has been clicked\n",i);
   }

   double ymin = g->GetHistogram()->GetMinimum();
   double ymax = g->GetHistogram()->GetMaximum();
   double dy = (ymax-ymin);
   for (i=0; i<n; i++) {
      g->GetPoint(i,x,y);
      t = new TLatex(x, ymin-0.03*dy, Form("%s (%4.2f,%4.2f)",c[i],x,y));
      t->SetTextSize(0.025);
      t->SetTextFont(42);
      t->SetTextAlign(21);
      t->Paint();
      l = new TLine(x,ymin,x,ymin+0.03*dy);
      l->Paint();
   }
}

```

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 1:01pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/18 "2024-06-21T13:01:50Z")

</div>

Thank you @couet and @linev, I tried to make dictionary by adding this command in my `CMakeLists.txt`:

```auto
ROOT_GENERATE_DICTIONARY(G__EventDisplay LINKDEF ${CMAKE_CURRENT_SOURCE_DIR}/src/EventDisplay_LinkDef.h)

```

and then

```auto
add_library(lib other_cxx_files ${CMAKE_CURRENT_SOURCE_DIR}/build/G__EventDisplay.cxx)

```

it compiled successfully but I got runtime error:

```auto
Error in <TQObject::CheckConnectArgs>: slot HighlightGraph(TVirtualPad*,TObject*,int,int) does not exist

```

where I confirmed many time that I have no typo of this function name or parameter.  
I also attach part of my EventDisplay.cxx:

```auto
void EventDisplay::HighlightGraph(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y){
        c_waveform = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c_waveform");
        std::cout<<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"<<std::endl;
        if(!c_waveform){
                std::cout<<"Make new Canvas for ADC waveform"<<std::endl;
                c_waveform = new TCanvas("c_waveform", "ADC waveform", 505, 0, 600, 400);
        }

        if(ihp == -1){
                delete c_waveform;
                return;
        }

        if(waveforms.size() != 0 && waveforms.find(ihp) != waveforms.end()){
                c_waveform->cd();
                waveforms.at(ihp)->Draw("AP");
                gPad->Update();
        }
}

void EventDisplay::DrawHits(CDCHitContainer* hits, int event){
        TCanvas* c = new TCanvas();
        fCanvases.push_back(c);
// c->HighlightConnect("HighlightGraph(TVirtualPad* pad, TObject* obj, Int_t ihp, Int_t y)");
        c->Connect("Highlighted(TVirtualPad*, TObject*, Int_t, Int_t)", "EventDisplay", this, "HighlightGraph(TVirtualPad*, TObject*, Int_t, Int_t)");
...
        g->SetHighlight();
}

```

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 21, 2024, 1:52pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/19 "2024-06-21T13:52:27Z")

</div>

It would be easy if you can provide access to your source code.

May be method is not public?  
Or cmake file has other problems

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 21, 2024, 2:19pm UTC](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707/20 "2024-06-21T14:19:55Z")

</div>

I finally solved it and now it runs fine…Thank you all so much!!!

It’s not fatal but I got seg fault when the Highlight function is called:

```auto
 ***Break*** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/siyuan/Physics/root/install/lib/libHistPainter.so] THistPainter::DistancetoPrimitive(int, int) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGpad.so] TPad::Pick(int, int, TObjLink*&) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGpad.so] TPad::Pick(int, int, TObjLink*&) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGpad.so] TCanvas::Pick(int, int, TObject*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGpad.so] TCanvas::HandleInput(EEventType, int, int) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TRootContainer::HandleMotion(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGFrame::HandleEvent(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGClient::HandleEvent(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGClient::ProcessOneEvent() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGInputHandler::Notify() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TSystem::InnerLoop() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TSystem::Run() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TApplication::Run(bool) (no debug info)
[/Users/siyuan/Physics/comet/crt/tracking/build/CRTTracking] main (no debug info)
[/usr/lib/dyld] start (no debug info)

```

but it still works when I moving my mouse to next point, do you know why this seg fault happens…?

[Next page](https://root-forum.cern.ch/t/can-i-add-buttons-on-a-tgraph/59707.md?page=2)
