TH2F from RooDataSet

I cannot seem to get the syntax right for creating a TH2F from my RooDataSet, or from my RooNDKeysPdf. I am doing

  file = new TFile(fileName.Data());
  tree = (TTree*)file->Get("data");
  X = new RooRealVar("X","X",0,75);
  Y = new RooRealVar("Y","Y",0,75);

  // Import observables (X,Y)
  // to the dataset
  ds = new RooDataSet("ds","ds",
  RooNDKeysPdf kde("kde","kde",
                   *ds,"am") ;
  TH2F *sig1 = kde.createHistogram("hh_pdf",
                                   YVar(*Y,Binning(25))) ;

  TH2F *h2_pdf = ds->createHistogram("h2_pdf",*X,Binning(20),YVar(*Y,Binning(20))) ;

at compile time I get the error:

error: invalid conversion from ‘TH1*’ to ‘TH2F*’

I have tried numerous other ways of creating these TH2 histos but nothing works. Can an expert show me how to create these histograms from my RooDataSet and RooNDKeysPdf?

Thank you very much!

That’s because you are trying to convert into a TH2F, but that’s not necessarily possible. The function may return a TH1F or even a TH3F, and therefore, it’s returning the most general histogram class: TH1. Therefore, you have to convert the TH1* that’s being returned and test if the conversion worked.

Try dynamic_cast<TH2F*> on the pointer that is being returned. If it’s not nullptr after that, you have a valid histogram.

You can find more info in type casting in C++ here:

That got it! Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.