Translating Between "Histogram Coordinates" and Locations on the Screen in NDC or Pixel Coords

Hi, I am trying to implement a system that, when drawing a histogram with a TLegend, will essentially change the Y axis range to a large enough size that my histograms will not be covered up by the TLegend.

At the moment, I already have a system that draws some histograms and the TLegend to the screen in a place where there’s normally not an issue. However, I’d like to tack on a little line of code at the end that:

  1. Checks if the Legend is covering anything, if not continue, otherwise:
  2. Scale the Y axis max by some number, redraw everything and go back to (1)

In order to implement the “collision detection,” I need to have some basic information about the location of bin contents on my screen to compare to the area covered by the legend. However, I am having a hard time translating between the NDC coordinates I have for the Legend and the “max Y” position taken up by my histograms.

The algorithm above was crafted after I played on the command line for a while and found that the combination gPad->PixeltoX(gPad->UtoPixel(ndcX)) would allow me to translate from an NDC coordinate to an x value in some histogram, but apparently this was some sort of fluke is not always true, and so I am not able to know which bin is “under” my legend.

Does anyone know how I can translate between NDC coordinates and information about, for instance, which bin from a drawn histogram is at that X coordinate? Again, the basic idea is to the find the bin under the X_min location of my TLegend, find the maximum bin content in the hist I’ve drawn from that bin forward (which requires I know the x value at my NDC coord), and then translate that max bin content into an NDC Y coordinate and rescale as necessary.

More generally, this does not seem like the best algorithm to do this job anyway. But unfortunately, it seems to be nearly impossible to have any sort of “collision detection” inside a TPad. I’m also interested in hearing if anyone has a better way to approach this…

Thanks for your time,
-Bobak

I think we are working on the same thing right now. I have produced a small prototype doing that and now I am a bit further: I would like to define the legend size automatically from the font text size . Now it is the opposite. If you are interested I can send you the macro I have. That’s only a prototype. At the end the collision detection code will be included in a ROOT Class… TPad seems the more accurate.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.