# How to Pick Objects?

**URL:** <https://root-forum.cern.ch/t/how-to-pick-objects/28251>\
**Category:** Howto\
**Tags:** graphics\
**Created:** [March 9, 2018, 11:58am UTC](https://root-forum.cern.ch/t/how-to-pick-objects/28251 "2018-03-09T11:58:02Z")\
**Posts on this page:** 1\
**Page:** 1

<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:** [March 9, 2018, 11:58am UTC](https://root-forum.cern.ch/t/how-to-pick-objects/28251/1 "2018-03-09T11:58:02Z")

</div>

When the mouse is moved or a button pressed/released, the [TCanvas::HandleInput](https://root-forum.cern.ch/doc/master/classTCanvas.html#TCanvas:HandleInput) function scans the list of objects in all its pads and for each object invokes **object-\>DistancetoPrimitive(px, py)**. This function computes a “distance” to an object from the moose position at the pixel position px,py and return this distance in pixel units. The selected object will be the one with the shortest computed distance. To see how this work, select the **Event Status** item in the canvas **Options** menu. ROOT will display one status line showing the picked object. If the picked object is, for example an histogram, the status line indicates the name of the histogram, the position x,y in histogram coordinates, the channel number and the channel content.

If you click on the left moose button, the **object-\>ExecuteEvent(event, px, py)** function is called.

If you click with the right moose button, a context menu (see [TContextMenu](https://root-forum.cern.ch/doc/master/classTContextMenu.html)) with the list of possible actions for this object is shown. This context menu is automatically built by ROOT from the class dictionary by selecting the member functions having the string **MENU** in the comment field. Remember that the class dictionary is built by parsing your class header files.

The middle button (or left+right on a 2-buttons moose) can be used to change the current pad to the pointed pad. The current pad is always highlighted. Its frame is drawn with a special color (see [TCanvas:SetHighLightColor](https://root-forum.cern.ch/doc/master/classTCanvas.html#TCancas:SetHighLightColor)).

Various examples of **DistancetoPrimitive** or **ExecuteEvent** functions can be seen in the ROOT source code. To users who want to write such functions, we strongly recommend to look at existing implementations in the ROOT source code. In particular, have a look at [TLine::ExecuteEvent](https://root-forum.cern.ch/doc/master/classTLine.html#TLine:ExecuteEvent) and [TBox::ExecuteEvent](https://root-forum.cern.ch/doc/master/classTBox.html#TBox:ExecuteEvent). Note that these functions may, in turn, invoke other objects such functions in case an object is drawn using other objects. You can also exploit at best the virtues of inheritance. See for example, how the class [TArrow](https://root-forum.cern.ch/doc/master/classTArrow.html) (derived from Tline) or [TPaveText](https://root-forum.cern.ch/doc/master/classTPaveText.html) (derived from [TPave](https://root-forum.cern.ch/doc/master/classTPave.html), itself derived from [TBox](https://root-forum.cern.ch/doc/master/classTBox.html)) use or redefine the picking functions in their base classes.

For example in the case of an event display program, the **Event::DistancetoPrimitive** may loop on the list of detectors and tracks and for each detector/track object invokes its **DistancetoPrimitive**. See, for example TGenerator::DistancetoPrimitive.

The moose position is always given in pixel units in these two functions. px=0 and py=0 correspond to the top-left corner of the canvas. Here we have followed the standard convention in windowing systems. Note that user coordinates in a pad have the origin at the bottom-left part of the pad. One can use transformation functions such as [TPad::PixeltoX](https://root-forum.cern.ch/doc/master/classTPad.html#TPad:PixeltoX), [TPad::XtoPixel](https://root-forum.cern.ch/doc/master/classTPad.html#TPad:XtoPixel),etc. to convert to/from pad coordinates.

If a named object of a class **MyClass** (derived from) is inserted in a pad, you can get a pointer to this object with:

```auto
auto obj = (Myclass*) pad->GetPrimitive(objectname);

```
