Canvas is Blank after plotting RooFit

Hello, I’m having a problem that seems like it should have a simple solution, but I can’t seem to find it.
I’m using a saved root file to create a dataset from a TTree, then fitting it and plotting it. The fitting seems to go fine, the dataset does not seem to be empty, but the canvas is blank after plotting. What am I doing wrong?

Here is my code:

using namespace RooFit;

void d0_gauss_fit(const char *file)
        TFile data_file("data/normal_output.root");
        TTree* data_tree;
        RooRealVar invMass("daughterInvariantMass__bo0__bc","daughterInvariantMass__bo0__bc",0,5);

        RooDataSet *data = new RooDataSet("data","data",data_tree,RooArgSet(invMass));

        RooRealVar mean("mean", "mean of gaussians", 0,5);
        RooRealVar sigma1("sigma1", "width of gaussians", 0,5);

        RooGaussian sig("sig", "Invariant Mass Signal", invMass, mean, sigma1);

        RooRealVar a0("a0", "a0", 0.5, 0., 1.);
        RooRealVar a1("a1", "a1", 0.2, 0., 1.);
        RooChebychev bkg("bkg", "Background", invMass, RooArgSet(a0, a1));

        RooRealVar sigFrac("sigFrac","Percent signal",0,1);
        RooAddPdf model("model","sig + bkg",RooArgSet(sig,bkg),sigFrac);


        RooPlot *frame = invMass.frame(Title("Invariant Mass fit to Gaussian"));
        model.plotOn(frame, Components(sig));

        // Overlay the background component of model with a dashed line
        model.plotOn(frame, Components(bkg), LineColor(kRed), LineStyle(kDashed));

        new TCanvas("d0_gauss_fit", "d0_gauss_fit", 800, 800);

        cout << data->numEntries() << endl;


I figured out the error: for whatever reason, opening a TFile causes the canvas to appear blank. I created two scripts, one that translates the root tree file to a roodataset, and one that takes the roodataset and does the plotting.

Hi @jlawless ,
and welcome to the ROOT forum! What was probably happening is that the thing you were drawing got destroyed at the end of the function (either because data_file is destroyed and frame was associated to it, or because invMass is destroyed and frame was owned by invMass, I’m not sure).


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