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

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.

Would you please have some help regarding this.

Cheers!
Anil

_

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

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?

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

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

So, what should be the final decided data?

Can you provide a macro reproducing the problem ?

Here is the macro,
write_hist.cpp (611 Bytes)

Your .root file is missing:

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");

Here is the .root files
meson_energy.root (4.3 KB)
muon_energy.root (5.0 KB)

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 ?

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.

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 …

Thank you @couet for the helping.

Hi Anil,

As far as I remember my solution was:

//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

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