Code works interactively, doesn't work as macro

When I copy and paste this code (the part within void{}), it works exactly as expected. When I doroot fit_masscopy.C or root .L fit_masscopy.C fit_masscopy() or root .x fit_masscopy.C, it just leaves me with a blank canvas, although it works otherwise. Help? Code below.

[code]void fit_masscopy(){
using namespace RooFit;
//open file and histograms, assign variables
TFile f(“histos.root”);
TH1F hmass = (TH1F)f.Get(“cutmassLb7”);
TH1F hmassLL = (TH1F)f.Get(“cutmassLb8”);
TH1F hmassDD = (TH1F)f.Get(“cutmassLb9”);
RooRealVar mass(“mass”,“m(#Lambda_{b}^{0}) with Liming’s cuts and KS cut”,4100,6100);
RooDataHist data(“data”,“mass data”,mass,hmass);

//Construct PDF model
RooRealVar mean(“mean”,“mean of gaussian”,5600,4100,6100);
RooRealVar sigma(“sigma”,“width of gaussian”,1,0,10);
RooGaussian gauss(“gauss”,“gaussian PDF”,mass,mean,sigma);

//perform fits
gauss.fitTo(data, Range(5600,5620));

//plot PDF and data
RooPlot *massframe = mass.frame();
gauss.plotOn(massframe, Range(5600,5620));

//Print final values of parameters

The “f” file must not be closed after “fit_masscopy” finishes …
TFile *f = TFile::Open(“histos.root”, “READ”);

TFile *f = TFile::Open(“histos.root”, “read”);

that did it. Why did that do it?

Hi mwilkins,

creates the object on stack so it disappears when fit_masscopy()

creates it on the heap where it survives

Btw: which root version are you using?



Thanks much for the explanation.