Extract histograms from HistFactory as TH3

Hi,

I’m tying to extract fitted histograms from HistFactory workspace, and save them as TH3. I’m doing this because I’m also building fitted histograms based on input templates and shape variations with some external script, and I’d like to check that I build it correctly.

I looked at this question, and it seems that there the suggestions is to use plotOn, which is not what I’m looking for.

Naively I’d like to use createHistogram method of, say, PiecewiseInterpolation, and here’s the gist:

    auto obs =
        static_cast<RooArgSet *>(mModelHf->getObservables(*mData)->selectByName(
            TString("*") + keyword + skim + "*"));
    auto x = reinterpret_cast<RooAbsRealLValue *>(
        obs->find("obs_x_" + prefix + skim));
    auto y = reinterpret_cast<RooCmdArg *>(obs->find("obs_y_" + prefix + skim));
    auto z = reinterpret_cast<RooCmdArg *>(obs->find("obs_z_" + prefix + skim));

    if (pdf == nullptr) {
      auto pdfHist = static_cast<RooHistFunc *>(
          mWorkspace->function(pdfName + "_nominal"));
      if (pdfHist == nullptr) {
        cout << pdfName << " does not exist, continuing" << endl;
        continue;
      }

      histo = reinterpret_cast<TH3F *>(pdfHist->createHistogram(
          outputName.c_str(), *x, IntrinsicBinning(), *y, IntrinsicBinning(),
          *z, IntrinsicBinning()));
    } else {
      histo                = reinterpret_cast<TH3F *>(
          pdf->createHistogram(outputName.c_str(), *x, IntrinsicBinning(), *y,
    }

But I get segmentation faults:

 *** Break *** segmentation violation
 Generating stack trace...
 0x00007f7b2730585b in RooArgList::RooArgList(RooAbsArg const&, char const*) + 0x3b from /nix/store/z7xj2hix4nfzagwz2fp4gg3rv6kgbc61-root-6.16.00/lib/libRooFitCore.so
 0x00007f7b272d926e in RooAbsReal::createHistogram(char const*, RooAbsRealLValue const&, RooLinkedList&) const + 0x21e from /nix/store/z7xj2hix4nfzagwz2fp4gg3rv6kgbc61-root-6.16.00/lib/libRooFitCore.so
 0x00007f7b272da62e in RooAbsReal::createHistogram(char const*, RooAbsRealLValue const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&, RooCmdArg const&) const + 0x11e from /nix/store/z7xj2hix4nfzagwz2fp4gg3rv6kgbc61-root-6.16.00/lib/libRooFitCore.so
 0x0000000000421bb1 in ParamExtract::getHistos(TString const&, std::vector<RooStats::HistFactory::Sample, std::allocator<RooStats::HistFactory::Sample> >&, TString const&) + 0xd91 from ./bin/extract_fit_params.exe
 0x00000000004223ee in ParamExtract::writeHistos(std::vector<TString, std::allocator<TString> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0x30e from ./bin/extract_fit_params.exe
 0x000000000041bd7e in main + 0x11ae from ./bin/extract_fit_params.exe
 0x00007f7b26428ded in __libc_start_main + 0xed from /nix/store/adxc893j47gxx3xjw403zdf0liiddvw2-glibc-2.32-48/lib/libc.so.6
 0x000000000041c18a in _start at /build/glibc-2.32/csu/../sysdeps/x86_64/start.S:122 from ./bin/extract_fit_params.exe

The full source code is available at here

Hi @yipengsun,

To me, after a quick look to your gist, it seems that any of the pointers that you are passing to createHistogram(), e.g. obs, x, y, etc., might be null or otherwise pointing to unmapped memory.

Other than that, I will invite @moneta and @jonas to this topic as they are our RooFit experts.

Cheers,
J.

Hi,

Indeed, I think the y and z are probably not casted properly. I mis-read the documentation, so I thought I need to cast them into RooCmdArg. Turns out the following “works” (again, just some sketch):

    auto obs =
        static_cast<RooArgSet *>(mModelHf->getObservables(*mData)->selectByName(
            TString("*") + keyword + skim + "*"));
    auto x = reinterpret_cast<RooAbsRealLValue *>(
        obs->find("obs_x_" + keyword + skim));
    auto y = reinterpret_cast<RooAbsRealLValue *>(
        obs->find("obs_y_" + keyword + skim));
    auto z = reinterpret_cast<RooAbsRealLValue *>(
        obs->find("obs_z_" + keyword + skim));

    auto   pdf         = static_cast<PiecewiseInterpolation *>(
        mWorkspace->function(pdfName + "_Hist_alpha"));

      histo                = reinterpret_cast<TH3F *>(pdf->createHistogram(
          outputName.c_str(), *x, IntrinsicBinning(), YVar(*y), ZVar(*z)));

This produces a TH3F* with expected binning.

The remaining problem is: The Integral() of these histograms look very different that what I’m expecting. I’m reading the docs again to see how to solve it.

Cheers.

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