Import a TH1 of Variable binning into a RooDataHist

Hi expxerts,

I am importing a TH1 with variable binning into a RooDataHist, and the problem is that RooDataHist ‘thinks’ that the original histogram has been normalized for bin size, hence it recalculates the number of entries in each bin based on the bin size (keeping the total normalization constant).
Is there a way/option to tell the RooDataHist that the histogram from which it is being created is a histogram and not a pdf, i.e. not normalized for bin size?

You can run the code below as a macro, the output on the terminal and the plot will hopefully illustrate what I am trying to do and what the problem is.

{

using namespace RooFit;
RooRealVar gl(“gl”,“gl”,0.0, 1);
Double_t gl_bins[9] = {0, 0.25, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
RooBinning glbins(8, gl_bins, “glbin”);
gl.setBinning(glbins);

std::cout<<"Making a histo with non uniform binning BUT setting equal content in each bin: "<<std::endl;

TH1D *hdata = new TH1D(“hdata”, “hdata”, 8, gl_bins);
for(int i=1; i<=hdata->GetNbinsX(); i++) hdata->SetBinContent(i, 10);

TH1D check = (TH1D )hdata->Clone(“check”);
for(int i=1; i<=check->GetNbinsX(); i++)
std::cout<<"Bin “<<i<<” has content "<GetBinContent(i)<<std::endl;

std::cout<<"Now  make a RooDataHist and plot it"<<std::endl;

RooDataHist Data (“Data”,“Data”, gl, hdata);
RooPlot *frameData = gl.frame();
Data.plotOn(frameData, LineColor(kRed), LineStyle(kSolid) );
frameData->Draw();

std::cout<< “Now make a TH1 from the RooDataHist and inspect bin contents”<<std::endl;
TH1 *Data_b = (TH1 *) Data.createHistogram(“Data_b”, gl, Binning(glbins));
for(int i=1; i<=Data_b->GetNbinsX(); i++)
std::cout<<"from Data_b histo, bin “<<i<<” has content "<<Data_b->GetBinContent(i)<<std::endl;

}

Given the absence of a reply to this and also this previous thread by iostrovskiy

I would like to ask the developers to make really clear the subtlety of the way to importing a TH1 into a RooDataHist:
Import (TH1*, kFALSE)
It is not at all made obvious that with uneven binning one should not use the simple constructor with out the ‘import’ option. Moreover, there is not one example in the tutorials with uneven binning.

Secondly, the point of plotting a RooDataHist with uneven binning is also non trivial and has cost me (and I believe many) much time and pain.
The fact that the RooHist object (which actually does the plotting) respects your correctForBinWidth choice depending on your choice of errors is a bug. It does not respect your correctForBinWidth choice if you chose the default errors, only if you specify DataError(RooAbsData::SumW2) while plotting will it respect the correctForBinWidth choice.

Hello,

I took the liberty to revive this topic, since I’m currently dealing with it.

So, what I’m doing is using a variable binning in my histogram (TH2F), then I create a RooDataHist from this histogram and a RooHistPdf from this roodatahist. In both cases, the variable binning is successfully copied, which is good. However, roofit doesn’t seem to know how to properly draw the RooHistData/RooHistPDF ( i guess it’s the former) and it seems that it doesn’t take the bin width into account?

Has there been any development on this? What is the correct procedure? I tried the above suggestion to use Import(histo,kFalse) in the RooDataHist constructor and the DataError in plotting, but it doesn’t change anything.

If I don’t use variable binning, everything is plotted fine.

Furthermore, I use these templates (which are incorrectly drawn) in my fits. The fits seem to perform nice and everything makes sense, so the fitter know how to properly work with variable binning, and the only problems seems to be in drawing. I then proceed to take the yields from the fit and use them to draw plots with uniform binning.

I’m attaching two plots. Both plots show the plotted RooDataHist and RooHistPdf, which was created from the same RooDataHist. One plot is for uniform, the other for variable binning.


Chiming in that it’s still unclear how to import a TH1F with variable bin widths to RooDataHist AND successfully plot it sensibly. Further, the RooDataHist class description itself gives conflicting advice on how to approach this issue (https://root.cern.ch/doc/master/classRooDataHist.html#a779d3fa150c690e6c4e886906f900952 & https://root.cern.ch/doc/master/classRooDataHist.html#ad7d81bc634934f5064eb054f8ee66e4e)

Thanks!