Problem with Merge and bin labels

Hello everyone,

I have some problems using hadd (and therefore TH1 Merge). I “inherited” some code which should allow me to merge and analyse some TH1-histograms. However, whenever I try to merge SignalSum histograms (see Image) , I get the following:

As far as I understand from http://root.cern.ch/root/html/src/TH1.cxx.html#GUWt6D this means that the histogram should have labeled bins (allHaveLabels is true). However, GetBinLabels returns “” for every bin, so they seem to be empty.

My first guess is that the fact that the histogram is filled with SetBinContent and not Fill has something to do with it. The other histograms which are filled with Fill do not produce this error. Since everything worked at some point, it should be possible the get the merging to work without changing the way the histograms are created.

However, even something as simple as hadd MergedSignal.root SignalSum1.root SignalSum2.root
produces the error, so I do not really have any ideas what to change.
If it its necessary, I can try to get the code used to create the histogram, but it should only be a default histogram filled with SetBinContent, and they are identical in binning/number of data points etc. (most of them are actually absolutely identical)

Edit: I looked around a bit, and found that GetBinLabel returns “” for every bin.
Then I tried to replicate the method to spot labelled bins used in http://root.cern.ch/root/html/src/TH1.cxx.html#GUWt6D
Which is

THashList* hlabels=h->GetXaxis()->GetLabels(); Bool_t haveOneLabel = (hlabels != 0);

However,

THashList* h; h=sig1->GetXaxis()->GetLabels(); if (!(h=0)) std::cout<<"fLabels is not empty"<<std::endl; if (h=0) std::cout<<"flabels is empty"<<std::endl;
shows that fLabels is not empty(allthough std::cout<<h prints out “0”)
Is there a way to delete the labels so Merge() falls back on numerical binning/does not asume there are non-numerical labels in the first place?


I just wrote a macro that opened the histograms, and changed every binlabel to the corresponding binnumber with SetBinLabel. which makes it look rather ugly and does not help me understand why the histogram had empty labels or why the stats are all 0 allthough the entries are clearly visible, but it hadd worked and that is all that really matters :slight_smile:
I will just mark the topic as solved

Hi Aleph (and/or anyone else that can help)

I am having the exact same problem with some histograms that I had to fill using AddBinContent. Could you please post the macro you created to fix the issue by relabeling all the bins? I would very much appreciate it. Or if anyone else has a solution that they could post I would be very grateful.

Thanks!

Hi John,
I am fairly certain you do not want to use the macro I wrote after working with c++ for about a week :smiley:
Additionally it is highly un-portable. What I did was something like this

TString path = "lorem";        // Path to rootfiles
TString histname = "ipsum";   //location and name of histogram in .root file
TFile* inFile = new TFile (path,"update");    //open file
if((hist = (TH1F*) inFile-> Get(histname)))      //try to open histogram ipsum
{
			hist = (TH1F*) inFile-> Get(histname);
}
int nBin = hist->GetNbinsX();
//Since I have no clue how to use type-conversion, and a small, fixed amount of bins for every histogram
const char *labels[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14"}; //I actually had to go up to 80 
for (int b = 0; b < nBin; b++)
		{		
			hist->GetXaxis()->SetBinLabel(b+1,labels[b]); //Set labels to numerical to allow merging to work
		}
hist->Write("",TObject::kOverwrite); //overwrite histogram
inFile->Close();

This is what I put into a loop and changed path and histname accordingly to recreate numerical labels for each affected histogram. There is certainly a less convulted way of relabeling (not using the array of chars would be a start, but I think it should also be possible to get Root to fall back on numerical Labels which would not require this kind of manual relabelling)
Unfortunately I do not have the skills or the knowledge to give you such an elegant solution, but if you just want it to work, SetBinLabel should do the trick
Greetings,
Timo