Roofit plot not showing with open TFile

Hello experts,

I wonder why the plot will not show unless I uncomment the line with TFile. The plot does not depend on the TFile anyway. I am using ROOT 6.20/04 installed using conda.

Thanks

#include <iostream>
#include <vector>
#include <string>
#include <memory>

#include "TCanvas.h"
#include "TFile.h"
#include "TH2D.h"
#include "TFractionFitter.h"
#include "TObjArray.h"

#include "RooRealVar.h"
#include "RooDataHist.h"
#include "RooDataSet.h"
#include "RooHistPdf.h"
#include "RooHistFunc.h"
#include "RooAddPdf.h"
#include "RooRealSumPdf.h"
#include "RooPlot.h"
#include "RooGaussian.h"

using namespace RooFit;

void demo_plot()
{
    TH1::AddDirectory(kFALSE);

    RooRealVar s12("s12", "s12", 0.075, 0.12);
    RooRealVar s13("s13", "s13", 5.85, 6.2);

    // TFile amps("amps.root");

    TCanvas * canTest = new TCanvas("canTest", "canTest", 400, 400);
    
    RooRealVar x("x", "x", -10, 10);
    RooRealVar mean("mean", "mean", 1);
    RooRealVar sigma("sigma", "sigma", 1);
    RooGaussian pdf("gaus", "gaus", x, mean, sigma);

    RooPlot * xframe = x.frame();
    pdf.plotOn(xframe);
    xframe->Draw();
}

Hi,

which operating system is this? For me, it shows in both variants.

Does it show when you add
canTest->Draw()?

Thanks for the reply! Did you uncomment the line with TFile? I don’t understand why there is only an empty plot shown when I uncomment the TFile line. The file amps.root contains some histograms. canTest->Draw() does not help.

I am using ubuntu 18.04LTS.

Another strange point is that if I put everything into {...} rather than define a named function demo_plot, the plot will show.

Try:

// ...
#include "TROOT.h"
// ...
TFile amps("amps.root");
gROOT->cd();
// ...

Thanks, that worked!

So, there is a bug somewhere in RooSomething, which simply disrespects:
TH1::AddDirectory(kFALSE);

It seems that one needs to use:
RooPlot::setAddDirectoryStatus(0);

Yes, I did. But what I forgot/oversaw is that it complains that the file doesn’t exist!

After creating one, I can reproduce. Will investigate!

Update: RooPlot will soon react to TH1::AddDirectory().

To explain why you don’t see the plot:
It gets attached to the file if a file is open. (That’s apparently happening so the plot automatically ends up in the file if its writable, ancient ROOT behaviour that we cannot change without breaking user code.)
When the function exits, the file gets closed, and the plot gets deleted. Therefore, it’s not visible, any more. If you make the file a pointer and let it leak, the plot also survives …

Actually, I am not really sure that RooPlot should react to TH1::AddDirectory at all.
It has a dedicated RooPlot::setAddDirectoryStatus method.
Maybe the right thing to do would be to make sure that, when the file is closed, the whole RooPlot is deleted, not just its contents (then it would be clear what happened).

The whole RooPlot is actually deleted. What remains after closing the file is just the empty canvas.

It looks like I misinterpreted

I will think about it.

Yes, you are right. I forgot about the created “canTest”. So, maybe there is no bug at all.

It’s just that the user must make sure that the created RooPlot is not “owned” by some opened file.

On the other hand, this behavior is counterintuitive. Standard ROOT canvases / pads / frames are not “owned” by files, so TH1::AddDirectory(kFALSE); should be sufficient to get their contents (with newly created histograms) intact.

Even more confusing is the fact that there is no “SetDirectory” method in RooPlot (so, e.g. one cannot change the “owner”).
Also the name “setAddDirectoryStatus” is much different from the default usual “AddDirectory”.

Ok, here’s the plan:
RooPlot receives functions with exactly the same names and behaviour as TH1.

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