# TH2 x y projection interactive viewer

**URL:** <https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [September 26, 2017, 8:57pm UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335 "2017-09-26T20:57:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 26, 2017, 8:57pm UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/1 "2017-09-26T20:57:20Z")

</div>

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](https://root-forum.cern.ch/t/ineractive-x-y-projection-of-th2fs/3122/6) 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:

```auto
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](https://root-forum.cern.ch/uploads/default/original/2X/7/7eeb113eadcb81e4e1de54d3d888ccd337c760d7.root) (9.4 KB)  
[TH2Viewer.h](https://root-forum.cern.ch/uploads/default/original/2X/7/774b58606ca6090a375ab990d3366782d40c5d70.h) (7.3 KB)  
[TH2Viewer.cpp](https://root-forum.cern.ch/uploads/default/original/2X/d/d2a65bd53afed87352622b6d828139da8caaef6d.cpp) (17.2 KB)

 ![screenshot](https://root-forum.cern.ch/uploads/default/original/2X/e/e70f96e2e0063e7e91734dbd34c7c7baae48b99e.png)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 28, 2017, 6:23pm UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/2 "2017-09-28T18:23:45Z")

</div>

Attached version 2 with autozoom option.  
[TH2Viewer.h](https://root-forum.cern.ch/uploads/default/original/2X/0/03d28d648de9ea878cc2f65d488022e6864a9885.h) (7.9 KB)  
[TH2Viewer.cpp](https://root-forum.cern.ch/uploads/default/original/2X/4/4963c98c34d96fb81eec452f0c66a0c3a6c3e743.cpp) (21.9 KB)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 27, 2022, 11:08am UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/3 "2022-05-27T11:08:02Z")

</div>

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

---

<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:** [May 30, 2022, 11:38am UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/4 "2022-05-30T11:38:52Z")

</div>

Thanks for this contribution.

---

<div class="post-metadata">

**Author:** ![otnikuda](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/9fc348/32.png) [@otnikuda](https://root-forum.cern.ch/u/otnikuda)\
**Post date:** [June 1, 2022, 12:40pm UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/5 "2022-06-01T12:40:28Z")

</div>

Highlight mode in ROOT offers exactly this option [simultaneously x/y projections in tutorial hlHisto2.C](https://root.cern.ch/doc/master/hlHisto2_8C.html)

[Highlight mode for histogram](https://root.cern.ch/doc/master/classTHistPainter.html#HP30)  
[Highlight mode for graph](https://root.cern.ch/doc/master/classTGraphPainter.html#GrP8)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [June 1, 2022, 1:05pm UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/6 "2022-06-01T13:05:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [October 26, 2023, 8:50am UTC](https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335/7 "2023-10-26T08:50:10Z")

</div>

This tool is now part of ROOT master. Just right click on histogram, and in the context menu, you can select the ProjectionXY option.

> <https://github.com/root-project/root/issues/10660>
>
> \### Is your feature request related to a problem? Please describe.
> When working… with TH2 histograms, it is very common to right-click on the canvas and use SetShowProjectionX or SetShowProjectionY.
> 
> There are cases where one needs to see both of these at the same time.
> 
> \### Describe the solution you'd like
> A function which plots the two projections at the same time, once a rectangular window has been defined.
> Ideally, one should be able to add more (gdirectory-open) TH2s to the 'buffer' via a Combo, as it is very useful to compare several 2D images by looking at their 1D projections.
> one of the projections could be rotated by 90deg, if needed, to make it fit with 2D axis orientation.
> 
> \### Describe alternatives you've considered
> I programmed back then this tool: https://root-forum.cern.ch/t/th2-x-y-projection-interactive-viewer/26335
> but it would be nice if it were adopted by ROOT, and be accesible by the context menu of the TCanvas.
> 
> \### Additional context
> https://stackoverflow.com/questions/40641895/plot-aligned-x-y-1d-histograms-from-projected-2d-histogram
> https://www.astrobetter.com/blog/2014/02/10/visualization-fun-with-python-2d-histogram-with-1d-histograms-on-axes/

> <https://github.com/root-project/root/pull/12566>
>
> \# This Pull request:
> 
> \## Changes or fixes:
> Adds XY projection capabilities si…multaneously for a standard TCanvas with a TH2. (This is needed for users in some applications where they do not use the new TWebCanvas, which was kindly implemented by @linev, but the traditional one).
> 
> !\[image\](https://user-images.githubusercontent.com/10653970/228327012-83f2f1a0-b07b-46c1-a71c-4ba602ec3d6d.png)
> 
> 
> \## Checklist:
> 
> \- \[x\] tested changes locally
> \- \[x\] updated the docs (if necessary)
> 
> This PR fixes https://github.com/root-project/root/issues/10660
