# Create a 2D histogram from two histograms

**URL:** <https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445>\
**Category:** Newbie\
**Created:** [February 3, 2023, 3:00pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445 "2023-02-03T15:00:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [February 3, 2023, 3:00pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/1 "2023-02-03T15:00:10Z")

</div>

Hi,  
I am wondering if there is way to create a 2D histogram from two other histograms which are stored in the root file. Mainly, I have the `Muon_eta` and `Muon_phi` variables, which I want to plot as a function of each other. F.g. on y axis `Muon_eta` and on x axis `Muon_phi`. How could I do this?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 3, 2023, 3:04pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/2 "2023-02-03T15:04:03Z")

</div>

That’s not possible,  
You need to fill the 2D histogram from the original tree variables.

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [February 3, 2023, 3:06pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/3 "2023-02-03T15:06:49Z")

</div>

Oh, that’s sad to know… But let’s say if I want to fill it from the original tree variables `Muon_eta` and `Muon_phi`, then how do I fill them?

---

<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:** [February 3, 2023, 3:13pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/4 "2023-02-03T15:13:02Z")

</div>

```auto
tree->Draw("Muon_eta:Muon_phi");

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [February 3, 2023, 3:16pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/5 "2023-02-03T15:16:24Z")

</div>

Thank you…  
In my case, given that I already have these variables stored in the root file, isn’t there a way around like, for example, extract X and Y values from each histogram and plot them in 2D hist? Then from `Muon_eta` I would have X1 and Y1 values, let’s say array of them, and from `Muon_phi` X2 and Y2 values… and then fill a 2D hist with these…

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [February 3, 2023, 3:33pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/6 "2023-02-03T15:33:01Z")

</div>

Otherwise, could you please expand your answer to how could I apply this to analysis code with RDataFrame, where I need to explicitly create and fill a 2D histogram in one go, and then draw it with `hist.emplace` since there is no option of simply doing it like you said…  
Just an example of how I am doing it for 1D histograms:  
`auto muon_pt = df.Histo1D( {"Muon_pt","",50,0,200}, "Muon_pt", weight); hist.emplace("BasicDistributions", muon_pt);`

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 3, 2023, 3:35pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/7 "2023-02-03T15:35:47Z")

</div>

[Histo2D](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a5408b465ad0870f34d8a57d5d9924f76)

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [February 3, 2023, 3:38pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/8 "2023-02-03T15:38:15Z")

</div>

Alright, thank you, I will rerun the whole analysis then if there is no other option… But just to make it clear, it’s not possible to do it the way I described above?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 3, 2023, 3:41pm UTC](https://root-forum.cern.ch/t/create-a-2d-histogram-from-two-histograms/53445/9 "2023-02-03T15:41:06Z")

</div>

Only 2D → 2 x 1D is possible.

Search for “profile” and “projection” in the [TH2](https://root.cern/doc/master/classTH2.html) class reference.
