How to make variable bins in RooDataHist

HI, roofiters :slight_smile:

Could you share your wisdom to make RooDataHist with variable bins?

I mean, I’m working with a simple 2 dim. histogram as below,

RooRealVar x(“x”,“x”, 0.0, 10);
RooRealVar y(“y”,“y”, 0.0, 10);

x.setBins(5) ;
y.setBins(5) ;

RooDataHist hdata(“hdata”,"", RooArgSet(x,y), histogram);
RooHistPdf hpdf(“hpdf”,"", RooArgSet(x,y), hdata);

but, actually, x and y in TH2 ‘histogram’ have variable bins like,

x = { 0, 2, 3, 5, 6, 10 }
y = { 0, 1, 4, 5, 8, 10 }

, so could you let me some hints how to define the roorealvariables?

Thanks in advance,

dear rooters,

actually I found basical RooBinning class which makes variable binning easily. Here what I did is,

RooBinning x5bins(0);
x5bins.addBoundary(3); like that, also for variable y5bins…

and I could use it clearly. Thanks! :slight_smile:


Even better: If you have a THx with variable bins, those boundaries will be automatically copied when you construct a RooDataHist from it.


Dear Wouter,

Thanks for comments!

First, I can’t use RooMCStudy like things directly due to my fitter. I’m using TFractionFitter now. But I would like to borrow toy generation function implanted in roofit.

Also I know that the variable bins are copied into RooDataHist correctly, but,
really what I need to make a toy sample using this pdf, then make it as a histogram again.

I mean, in sequence as

RooDataHist -> RooHistPdf -> RooDataSet, generate -> createHistogram

and in generate step, the variable bins seems destroyed…

And in createHisotgram, there is no way as far as I understand, to use variable bins yet.
(only fixed uniform binning is possible, like, YVar(yy, Binning(16)).

So, I tried to use binnedClone() with those RooBinning first. Then I make histogram again. But in that case, still the normalization is not exact, in my experience…

Could you let me have some hints to use variable bins in createHistogram?

Thanks in advance!


You should also be able to pass a RooAbsBinning reference through a Binning()
argument (see snippet from documentation of createHistogram below). That allows to specify again a non-uniform binning in createHistogram.


// Create and fill a ROOT histogram TH1,TH2 or TH3 with the values of this function.
// This function accepts the following arguments
// name – Name of the ROOT histogram
// xvar – Observable to be mapped on x axis of ROOT histogram
// Binning(const char* name) – Apply binning with given name to x axis of histogram
// Binning(RooAbsBinning& binning) – Apply specified binning to x axis of histogram
// Binning(int nbins, [double lo, double hi]) – Apply specified binning to x axis of histogram
// ConditionalObservables(const RooArgSet& set) – Do not normalized PDF over following observables when projecting PDF into histogram
// Scaling(Bool_t) – Apply density-correction scaling (multiply by bin volume), default is kTRUE

Dear Wouter,

Thanks for advices.

Exactly it works!

(I hesitated to use Abs classes due to my poor understandings to c++ and roofit features, but it is getting better from much help of rooters. :slight_smile: )


Hi there,
Like Aprilii (first posts), I’d like to import a TH1 created with a predefined binning (equidistant or not).
Unfortunately, I get stuck at the RooDataHist stage.

Double_t xAxis[3] = {-5.,0.,7.} ; TH1D *histo = new TH1D ("histo","histo",2, xAxis) ; histo->SetBinContent(1,2.3) ; histo->SetBinContent(2,5.7) ; RooBinning xbins (histo->GetNbinsX(),((histo->GetXaxis())->GetXbins())->GetArray()) ; xbins.Print() ; RooRealVar t ("t","t", histo->GetXaxis()->GetBinLowEdge(1), histo->GetXaxis()->GetBinUpEdge(histo->GetNbinsX())) ; t.Print() ; t.setBinning(xbins) ; t.Print() ; //RooDataHist data ("data","data",RooArgSet(t),histo) ; /// seg. fault Any clue?


which version are you using ? I did not get a seg fault with your code when using a version >= 5.26


The problem occurs with ROOT 5.27/02 and ROOT 5.26/00b (binaries or source) + ROOFIT 3.12 on Cygwin.
No prob with the Cygwin binaries of ROOT 5.24/00 + ROOFIT 3.00 !
No prob also with ROOT 5.26/00b on CentOS 5.5


ROOT 5.27/04 will work fine (due this week). I’ve fixed a bug in the handling
of binning in RooDataHists.


Thanks Wouter, the problem still lies unfortunately :frowning:
Has your fix been integrated to the last release?
Have you tried the same piece of code?


The fix went into the release and I used your macro to confirm this…
Let me look into this and get back to you.