Adding TH2D in a loop

Dear All,

I hope this question fits in the Newbie section. I have several root files which contain a TH2D histogram with exactly the same number of bins (basically it is the same histogram but filled with different data in each root file). I want to produce a single plot out of all of them. I am initializing an empty histogram and then adding the others to it. For obvious reasons (different binning) my code does not run but I want to avoid to change specific histogram and then loop over the others. Is there any workaround this? I am using PyROOT for this. My code is this one:

    for folder in folders:
        h_ph_2D_sum = TH2D()
        # Looping over all the root files in subdirectory
        for root_file in rootfile_dict[folder]:
            tmp_file = TFile(folder + root_file)  # Getting root file

the error code is then:

Info in <TH2D::Add>: Attempt to add histograms with different number of bins - trying to use TH1::Merge
Error in <Merge>: Cannot merge histograms - limits are inconsistent:
 first:  (1, 0.000000, 1.000000), second: h_2D (128, 0.500000, 1280.500000)

I understand the error but I do not know any other solution apart from initializing the histogram to the correct number of bins.

Thanks in advance,

Yes that what you should do.
You can simply get this information from the stored histograms.

Thanks for the reply.
So, there is not other workaround this?

I do not see what is the penalty doing that ?

You’re right I just have to initialize with one histogram and the run my loop over the others.
Thank you