Plotting TH2 histogram from RooProdPdf?


Using RooFit v2.20 Im finding it difficult to generate a TH2 plot from a RooProdPdf as instructed in the v2.07 RooFit Manual on page 37.

My question is if the following lines of code on pg 37 are still valid:

1) TH2 *hd = data->createHistogram("hd",x,Binning(20),YVar(y,Binning(20)));
2) TH2* hf = gaussxy.createHistogram("hf",x,Binning(40),YVar(y,Binning(40))); 

I see doing something like,
helps with (1). But I cannot see how you can generate a TH2 from a RooProdPdf, gaussxy.

Many Thanks,


Hi Colin,

Sorry for the late reply, I was mostly offline last week.

To answer your question: both lines of code should work. In case

  1. a (binned or unbinned) RooFit dataset fills a ROOT two-dimensional
    histogram. In case 2) a two-dimensional RooFit p.d.f. is sampled in
    a two-dimensional grid and the results are stored in a two-dimensional
    ROOT histogram.


I see the same problem:

TH2D *hRes = model.createHistogram("hRes",dm,Binning(hfit->GetNbinsX()),YVar(m4h,Binning(hfit->GetNbinsY())));

I compile my program and the error is:

I am using ROOT 5.22 and RooFit 2.95.

If I type from ROOT:

and press TAB, I get:

TH1* createHistogram(const char* varNameList, Int_t xbins = 0, Int_t ybins = 0, Int_t zbins = 0) const TH1* createHistogram(const char* name, const RooAbsRealLValue& xvar, RooLinkedList& argList) const TH1* createHistogram(const char* name, const RooAbsRealLValue& xvar, const RooCmdArg& arg1 = RooCmdArg::none(), const RooCmdArg& arg2 = RooCmdArg::none(), const RooCmdArg& arg3 = RooCmdArg::none(), const RooCmdArg& arg4 = RooCmdArg::none(), const RooCmdArg& arg5 = RooCmdArg::none(), const RooCmdArg& arg6 = RooCmdArg::none(), const RooCmdArg& arg7 = RooCmdArg::none(), const RooCmdArg& arg8 = RooCmdArg::none()) const

Could be the TH2* createHistogram function missing?


you need to downcast the TH1 pointer to a TH2. Just do

TH2 *hRes = dynamic_cast<TH2 *> ( model.createHistogram("hRes",dm,Binning(hfit->GetNbinsX()),YVar(m4h,Binning(hfit->GetNbinsY()))) );


Thank you, now it works.