# How to write two histograms with different bin size in a single histogram

**URL:** <https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014>\
**Category:** Newbie\
**Created:** [May 18, 2022, 2:20pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014 "2022-05-18T14:20:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 18, 2022, 2:20pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/1 "2022-05-18T14:20:59Z")

</div>

Hii All,  
I am trying to add two histograms with different bin size in a single histogram. Finally the single histogram write to a root file instead of plotting. After execution, it gives error for TH1Merger.  
**Error:**  
**root [0] \*\*  
 Processing plot\_hist.cpp…  
 Info in TH1F::Add: Attempt to add histograms with different number of bins - trying to use TH1::Merge  
 Error in TH1Merger::DifferentAxesMerge: Cannot merge histograms - the histograms hs can extend the X axis or have different limits and underflows/overflows are present in the histogram hs.  
 Info in TH1F::Add: Attempt to add histograms with different axis limits - trying to use TH1::Merge  
 Error in : Cannot merge histograms - limits are inconsistent:  
\*\* first: hs (10000, 0.000000, 1000000000.000000), second: hs (10000, 10.000000, 1000000000.000000)**  
**root [1]**

Here I also found a duplicate link for plotting instead of writting.

> [@How to plot 3 histograms with different binning in the same plot?](https://root-forum.cern.ch/t/how-to-plot-3-histograms-with-different-binning-in-the-same-plot/35586):
>
> Hi I am searching for a way to plot 3 histograms with different binning in the same plot unsuccessfully. histogram1: 250 bins from 0.0 to 2.5 GeV/cˆ2 histogram2: 60 bins from 2.5 to 4.0 GeV/cˆ2 histogram3: 80 bins from 4.0 to 8.0 GeV/cˆ2 I have tried many things from manual with no luck. Would you please have some hint ? thanks Luiz Regis ROOT Version: Not Provided Platform: Not Provided Compiler: Not Provided

Would you please have some help regarding this.

Cheers!  
Anil

\_

---

<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 18, 2022, 2:45pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/2 "2022-05-18T14:45:30Z")

</div>

It seems that your two histograms cannot be added because they do not have the same number of bins and cannot be merged because they have incompatible limits (see the error messages). In that case you will need to do an “ad-hoc” merging by:

1. Booking the final histogram as you wish (with the bin numbers and limits you like)
2. Getting the bins contents of the two source histograms
3. Combine the bins contents as you wish
4. Fill the bin you want in the final histogram

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 18, 2022, 3:28pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/3 "2022-05-18T15:28:00Z")

</div>

Thank you @couet.  
I have,

1. 1st histogram: bin\_size- 500, limits- 0. to 1.0E3
2. 2nd histogram: bin\_size- 10000, limits- 1.0E1 to 1.0E9

So the final histogram that I have taken: bin\_size- 10000, limits- 0. to 1.0E9  
Does it fine for the final histogram?

---

<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 18, 2022, 3:28pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/4 "2022-05-18T15:28:52Z")

</div>

> [@Anil\_Kumar](#):
>
> Does it fine for the final histogram?

That up to you to decide, that’s your data.

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 19, 2022, 6:20am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/5 "2022-05-19T06:20:04Z")

</div>

Yes I have used these data to the final histogram. Even if there is error as followes,

> [@Anil\_Kumar](#):
>
> **Error:**  
> **root [0] \*\*  
> Processing plot\_hist.cpp…  
> Info in TH1F::Add: Attempt to add histograms with different number of bins - trying to use TH1::Merge  
> Error in TH1Merger::DifferentAxesMerge: Cannot merge histograms - the histograms hs can extend the X axis or have different limits and underflows/overflows are present in the histogram hs.  
> Info in TH1F::Add: Attempt to add histograms with different axis limits - trying to use TH1::Merge  
> Error in : Cannot merge histograms - limits are inconsistent:  
> \*\* first: hs (10000, 0.000000, 1000000000.000000), second: hs (10000, 10.000000, 1000000000.000000)**  
> **root [1]**

So, what should be the final decided data?

---

<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 19, 2022, 6:21am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/6 "2022-05-19T06:21:40Z")

</div>

Can you provide a macro reproducing the problem ?

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 19, 2022, 6:30am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/7 "2022-05-19T06:30:01Z")

</div>

Here is the macro,  
[write\_hist.cpp](https://root-forum.cern.ch/uploads/short-url/6kGSigAqeeTuFMZtGYiqKqWG75d.cpp) (611 Bytes)

---

<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 19, 2022, 6:32am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/8 "2022-05-19T06:32:16Z")

</div>

Your .root file is missing:

```auto
Processing write_hist.cpp...
Error in <TFile::TFile>: file /Users/couet/Downloads/muon_energy.root does not exist
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
In file included from input_line_9:1:
/Users/couet/Downloads/write_hist.cpp:10:21: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
  TH1F *h1 = (TH1F*)f1->Get("hs");

```

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 19, 2022, 6:46am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/9 "2022-05-19T06:46:39Z")

</div>

Here is the .root files  
[meson\_energy.root](https://root-forum.cern.ch/uploads/short-url/pm6VceelPgx5dBWA3IhytAib9ZD.root) (4.3 KB)  
[muon\_energy.root](https://root-forum.cern.ch/uploads/short-url/m8pWF2MF112SCNrB9fZc8RJ9tpZ.root) (5.0 KB)

---

<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 19, 2022, 6:55am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/10 "2022-05-19T06:55:35Z")

</div>

The two histograms are completely incompatible. They do not have the same limits on the X axis, they to not have the same number of bins. Even the limits overlap… What do you except at the end ?

 ![Screenshot 2022-05-19 at 08.51.42](https://root-forum.cern.ch/uploads/default/original/3X/d/e/dead732e72bf7afcda0ca0e3e490540ca547f024.png)

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 19, 2022, 7:06am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/11 "2022-05-19T07:06:01Z")

</div>

Yeah, individual histogram is fine with different canvas.  
I want to add these histograms in a single canvas and write these to a single histogram and finally save in energy\_hist.root as shown in the macro.

The single histogram inside the energy\_hist.root has no entries.

 ![Screenshot from 2022-05-19 12-33-27](https://root-forum.cern.ch/uploads/default/original/3X/9/4/9462f204e0c9f818e18be6850f2c7b2964324371.png)

---

<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 19, 2022, 7:14am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/12 "2022-05-19T07:14:15Z")

</div>

I understand what you want to do. But the problem is “how do you want to add these two histograms” ? They are very incompatible. The more realistic way I would see is to first draw a frame covering the full range (along x and y) of the two histograms and then plot the two histograms in that frame using the option SAME. Any other kind of combination should be done very carefully. In particular do the bins matches correctly in the overlapping zone ? I bet not …

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 19, 2022, 8:08am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/13 "2022-05-19T08:08:53Z")

</div>

Thank you @couet for the helping.

---

<div class="post-metadata">

**Author:** ![OtaviusDecius](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otaviusdecius/32/31051_2.png) [@OtaviusDecius](https://root-forum.cern.ch/u/OtaviusDecius)\
**Post date:** [May 19, 2022, 7:29pm UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/14 "2022-05-19T19:29:17Z")

</div>

Hi Anil,

As far as I remember my solution was:

[//0-2.5](https://0-2.5) (125bins), 2.5-4(60bins), 4-8(80bins)  
double xmin1 = 0.;  
double xmax1 = 2.5;  
int nbins1 = 125;

double xmin2 = 2.5;  
double xmax2 = 4.;  
int nbins2 = 60;

double xmin3 = 4.;  
double xmax3 = 8.;  
int nbins3 = 80;

double bwidth1 = (xmax1 - xmin1)/nbins1;  
double bwidth2 = (xmax2 - xmin2)/nbins2;  
double bwidth3 = (xmax3 - xmin3)/nbins3;

int nbinstot = nbins1 + nbins2 + nbins3;  
//…Luiz  
double edges[nbinstot+1] ;

//nbinstot++;

int nbins=0;

for( int i=0; i\<nbins1; i++){ edges[nbins] = xmin1 + bwidth1 \* i; nbins++;}  
for( int i=0; i\<nbins2; i++){ edges[nbins] = xmin2 + bwidth2 \* i; nbins++;}  
//…Luiz  
for( int i=0; i\<=nbins3; i++){ edges[nbins] = xmin3 + bwidth3 \* i; nbins++;}

histosTH1F[“hm2rec2OS\_ttbb2varbin”] = new TH1F(“hm2rec2OS\_ttbb2varbin”,“TTBB variable bins”,nbinsto  
t,edges);  
histosTH1F[“hm2rec2OS\_diag2varbin”] = new TH1F(“hm2rec2OS\_diag2varbin”,“DIAG variable bins”,nbinsto

t,edges);

Hope this helps.

Cheers,  
Luiz Regis

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [May 20, 2022, 7:50am UTC](https://root-forum.cern.ch/t/how-to-write-two-histograms-with-different-bin-size-in-a-single-histogram/50014/15 "2022-05-20T07:50:45Z")

</div>

Hi Luiz Regis,  
Thank you very much for the reply.
