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;
data_file.GetObject("Kpi",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);
model.fitTo(*data);
RooPlot *frame = invMass.frame(Title("Invariant Mass fit to Gaussian"));
data->plotOn(frame);
model.plotOn(frame);
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);
frame->Draw();
cout << data->numEntries() << endl;
}
Edit:
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.