Problem with workspace storage/retrieval of RooDecay instance

Hi,
in the following example I can store and retrieve a simple RooExponential (and generate toys based on it), but the same crashes when using RooDecay (uncomment #define USEROODECAY at the top of the file). Am I doing something wrong? Help would be greatly appreciated!
cheers,
Alex

//#define USEROODECAY

#include "RooDataSet.h"
#include "RooRealVar.h"
#include "RooGaussModel.h"
#include "RooDecay.h"
#include "RooExponential.h"
#include "RooAbsPdf.h"
#include "RooFit.h"
#include "RooWorkspace.h"
#include "RooConstVar.h"
#include <iomanip>

void MakeModel(RooWorkspace *ws);
void CreateToy(RooWorkspace *ws);

void WorkspaceTest()
{
  RooWorkspace *wspace = new RooWorkspace("myWS");
  MakeModel(wspace);
  CreateToy(wspace);
  
}

void MakeModel(RooWorkspace *ws)
{
  Double_t Tau=1.0,Smear=1.0;
  Double_t lowProperTimeRange=1.0, highProperTimeRange=15.0;

  RooRealVar properTime("properTime", "c#tau", lowProperTimeRange, highProperTimeRange, "ps");

  RooConstVar DecayConst("eDecayConst", "Decay Constant", -1.0/Tau);
#ifdef USEROODECAY
  RooRealVar GaussianSmearBias("GaussianSmearBias","Gaussian Smear Bias",0.0);
  GaussianSmearBias.setConstant();
  RooRealVar GaussianSmearSigma("GaussianSmearSigma","Gaussian Smear Sigma",Smear);
  RooGaussModel SmearModel("CombinatorialLifeTimeSmear","Lifetime Smear",properTime,GaussianSmearBias,GaussianSmearSigma);
  RooDecay LifeTimeModel("CombinatorialLifeTimeModel", "Lifetime model",properTime, DecayConst,SmearModel,RooDecay::DoubleSided);
#else
  RooExponential LifeTimeModel("LifeTimeModel","Expo model",properTime,DecayConst);
#endif
  ws->import(LifeTimeModel);
}

void CreateToy(RooWorkspace *ws)
{
  std::cout << "Dataset creation" << std::endl;
  Int_t nEvents=100;
  RooAbsPdf *model = ws->pdf("LifeTimeModel");
  RooRealVar *properTime = ws->var("properTime");
  RooDataSet *data = model->generate(RooArgSet(*properTime), nEvents);
 std::cout << "import to workspace" << std::endl;
   ws->import(*data);
}

Hi,

In the case of USEROODECAY you are using a different name for the pdf. It is normal then that a null pointer is returned when you retrieve the pdf from the workspace to generate the events.
Here are the correct lines of code in CreateToy():

void CreateToy(RooWorkspace *ws)
{
  std::cout << "Dataset creation" << std::endl;
  Int_t nEvents=100;
#ifdef USEROODECAY
  RooAbsPdf *model = ws->pdf("CombinatorialLifeTimeModel");
#else
  RooAbsPdf *model = ws->pdf("LifeTimeModel");
#endif
  RooRealVar *properTime = ws->var("properTime");
  RooDataSet *data = model->generate(RooArgSet(*properTime), nEvents);
 std::cout << "import to workspace" << std::endl;
   ws->import(*data);
}

It’s true that the name is different, but there’s also a bug when copying a RooDecay. There’s an object that’s not used anywhere (as far as I can see), and its pointer is being copied around.
When you delete the original (as might happen when you import it into a workspace), you copy around a dangling pointer.

I’ll remove this object, as it’s not used anywhere.

Track progress here:
https://sft.its.cern.ch/jira/browse/ROOT-10764

Sorry Lorenzo: unfortunately I left the name mismatch when simplifying to the example from my application. After the fix you propose I still get the error I was originally facing.

Hopefully StephanH is onto something related to this?

Thanks to both for following up!

Here the cleaned-up code, just for reference:

#define USEROODECAY

#include “RooDataSet.h”
#include “RooRealVar.h”
#include “RooGaussModel.h”
#include “RooDecay.h”
#include “RooExponential.h”
#include “RooAbsPdf.h”
#include “RooFit.h”
#include “RooWorkspace.h”
#include “RooConstVar.h”
#include

void MakeModel(RooWorkspace *ws);
void CreateToy(RooWorkspace *ws);

void WorkspaceTest()
{
RooWorkspace *wspace = new RooWorkspace(“myWS”);
MakeModel(wspace);
CreateToy(wspace);

}

void MakeModel(RooWorkspace *ws)
{
Double_t Tau=1.0,Smear=1.0;
Double_t lowProperTimeRange=1.0, highProperTimeRange=15.0;

RooRealVar properTime(“properTime”, “c#tau”, lowProperTimeRange, highProperTimeRange, “ps”);

RooConstVar DecayConst(“eDecayConst”, “Decay Constant”, -1.0/Tau);
#ifdef USEROODECAY
RooRealVar GaussianSmearBias(“GaussianSmearBias”,“Gaussian Smear Bias”,0.0);
GaussianSmearBias.setConstant();
RooRealVar GaussianSmearSigma(“GaussianSmearSigma”,“Gaussian Smear Sigma”,Smear);
RooGaussModel SmearModel(“SmearModel”,“Lifetime Smear”,properTime,GaussianSmearBias,GaussianSmearSigma);
RooDecay LifeTimeModel(“LifeTimeModel”, “Lifetime model”,properTime, DecayConst,SmearModel,RooDecay::DoubleSided);
#else
RooExponential LifeTimeModel(“LifeTimeModel”,“Expo model”,properTime,DecayConst);
#endif
ws->import(LifeTimeModel);
}

void CreateToy(RooWorkspace *ws)
{
std::cout << “Dataset creation” << std::endl;
Int_t nEvents=100;
RooAbsPdf *model = ws->pdf(“LifeTimeModel”);
RooRealVar *properTime = ws->var(“properTime”);
RooDataSet *data = model->generate(RooArgSet(*properTime), nEvents);
std::cout << “import to workspace” << std::endl;
ws->import(*data);
}

It’s fixed! I just need to merge it before the next ROOT version comes out :-).

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