TH2 x y projection interactive viewer

2D histograms are very useful for visualizing data, but are limited when it comes to quantify gradients and structures based on the color map, or when it comes to compare two different 2D histograms. Normally, one tends to project on the x or y axis, or to draw 1D profiles along a given direction.

Currently, the option SetShowProjection method is a helpful tool to do that analysis interactively. In this app, I extend this idea with a class that

  • does simultaneously x/y projections
  • allows interactive changing of the window width (instead of closing and regenerating the projection) and position
  • compares several 2D histograms (or rather their 1D projections) with corresponding checkboxes
  • it is not sensitive to the mouse position, which has always made SetShowProjection a tricky feature to use.

Hope it helps you!

Run as:

root -l
TH2* h2 = (TH2*)(TFile::Open("graphs.root")->Get("h2"));
TH2* h3 = (TH2*)(TFile::Open("graphs.root")->Get("h3"));
#include "TH2Viewer.cpp"
TH2Viewer* h2v = new TH2Viewer({h2,h3});

graphs.root (9.4 KB)
TH2Viewer.h (7.3 KB)
TH2Viewer.cpp (17.2 KB)


Attached version 2 with autozoom option.
TH2Viewer.h (7.9 KB)
TH2Viewer.cpp (21.9 KB)

If you face compilation issues with newer ROOT or compiler versions, add the following:

.c_str() after std::to_string(...) on lines 54,56,327,379

Thanks for this contribution.

Highlight mode in ROOT offers exactly this option simultaneously x/y projections in tutorial hlHisto2.C

Highlight mode for histogram
Highlight mode for graph

Thanks for the reply otnikuda and the info, I didn’t know about this Highlight tool. However it does not do exactly the same:

  • My tool allows several 2D histograms to be compared / drawn at the same time. Only one is shown in the 2D color plot, but the 1D projections are shown for all of them
  • It allows a custom ‘window’, not just a projection over one bin
  • It lets you fix the window without having to depend on the mouse position, which can be quite annoying in general if you want to make a fit on the 1D profile, or even take a screenshot.

In any case, it would be great if either this Highlight Mode and/or the TH2Viewer tool I created were automatically accessible from the ContextMenu of the TCanvas, not just using custom scripts outside.