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.
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:
Booking the final histogram as you wish (with the bin numbers and limits you like)
Getting the bins contents of the two source histograms
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");
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 …