RooStats::SPlot, extended PDFs and RooWorkspace


I’m sorry if this is covered in the documentation somewhere, but if it is I can’t find it. I’m using SPlot to background subtract a set of interest variables. The code is roughly structured like this:

void jpsi_fit(TTree* tree, RooRealVar* mass, RooRealVar* tau, const double lumi, RooWorkspace& wkspc){ RooDataSet data("data","data",RooArgSet(*mass,*tau),RooFit::Import(*tree)); RooAbsPdf* model = build_model(mass,tau,data.numEntries()); RooFitResult* result = Fit(model,data); model->SetName("model"); //model->Write(); result->SetName("result"); result->Print(); //result->Write(); wkspc.import(data); wkspc.import(*model); wkspc.import(*result); print_plot(mass,&data,model,"mass",";J/#psi Mass [GeV]",lumi); print_plot(tau,&data,model,"tau",";J/#psi Proper Decay Time [ps]",lumi); }
Then, later on I construct the sWeights:

TH1* print_splot_stack(TTree* tree, TH1* base_hist, const char* suffix,
		       const double lumi,RooWorkspace* wkspc){
  const std::string plot_name(base_hist->GetName());
  RooAbsPdf& model = *wkspc->pdf("model");
  RooAbsPdf& Signal = *wkspc->pdf("ext_sig");
  RooAbsPdf& Background = *wkspc->pdf("ext_bkg");
  RooRealVar& mass = *wkspc->var("jpsi_m");
  RooRealVar& tau  = *wkspc->var("jpsi_tau");
  RooRealVar& nsig = *wkspc->var("nsig");
  RooRealVar& nbkg = *wkspc->var("nbkg");
  RooRealVar interest_var(base_hist->GetName(),base_hist->GetName(),
  RooDataSet* data=new RooDataSet("data","data",RooArgSet(mass,tau,interest_var),RooFit::Import(*tree));//dynamic_cast<RooDataSet*>(wkspc->data("data"));
  RooFIter iter = model.getVariables()->fwdIterator();
  RooRealVar* var = NULL;
  RooStats::SPlot sData("sData","SPlot Dataset ", *data, &model,RooArgList(nsig,nbkg));
  TCanvas canv("canv","SPlot diagnostic Canvas",600,600);
  RooDataSet data_signal(data->GetName(),data->GetTitle(),data,*data->get(),0,"nsig_sw");
  // more printing code
  return sig_final;

When I run this code from one binary, everything works (ie I run jpsi_fit, and then I call print_plot_stack). I would like to decouple the jpsi_fit from the print_plot_stack by writing the workspace to a file and then reading it in. This is to make debugging and validating the procedure faster since I won’t have to re-fit the distribution all the time when I want to tweak what the output plots look like.

I was doing this before, but with SPlot, I get a segfault. If I try to call the fit again, the fitting procedure prints a bunch of errors stating that the PDF cannot be fit in extended mode. (Even though its constructed as an extended pdf and works when called the first time).

Am I doing anything obviously wrong? I will try to post the stack trace later.

sorry for the noise, my code works after decoupling.