Plotting different fit on different canvas with RooFit

Hello everyone,

I’ve just begun using RooFit and I have a problem that is quite trivial, I think, but of which I can not find an answer anywhere.
I have to fit in different ways a dataset, I need also to plot these fit in different canvas, or in different sections of the same canvas. To accomplish that I tried to define different RooPlot variables, each from the very same RooRealVar and to draw them, but it does not work because it creates only a canvas.

Here the code ( :red_circle: = the lines where I defined the RooPlot variables):

  RooRealVar x("x", "random variable", 0, 2);

  RooRealVar mean1("mean1","Mean of Gaussian 1",0.5,0,2);
  RooRealVar sigma1("sigma1","Width of Gaussian 1",0.5,0,2) ;
  RooGaussian gauss1("gauss1","gauss(x,mean1,sigma1)",x,mean1,sigma1) ;
  RooRealVar mean2("mean2","Mean of Gaussian 2",1.9,0,2) ;
  RooRealVar sigma2("sigma2","Width of Gaussian 2",0.2,0,2) ;
  RooGaussian gauss2("gauss2","gauss(x,mean2,sigma2)",x,mean2,sigma2) ;
  RooRealVar tau("tau","Tau exponential",0,-5,1);
  RooExponential exponential("exponential","exo(x,-tau)",x,tau) ;

  RooRealVar alpha("alpha", "alpha",0.1, 0, 1);
  RooRealVar beta("beta", "beta",0.3, 0, 1);
  RooRealVar gamma("gamma", "gamma", 0.6, 0, 1);
  RooAddPdf initial_model("model","model", RooArgList(gauss1,gauss2,exponential), RooArgList(alpha,beta));
  RooAddPdf fit_model("fit_model","fit_model", RooArgList(gauss1,gauss2,exponential), RooArgList(alpha,beta, gamma));

// Fit ML
  RooAddPdf model_ml = fit_model;
  model_ml.fitTo(hist, RooFit::Extended());
 :red_circle:  RooPlot *frame_ml = x.frame(RooFit::Name("Fit ML"), RooFit::Title("Fit ML"));

// Fit EML, already extended
  RooAddPdf model_eml = initial_model;
  model_eml.fitTo(hist);//already extended
:red_circle:  RooPlot *frame_eml = x.frame(RooFit::Name("Fit EML"), RooFit::Title("Fit EML"));

Thank you for your attntion

Hi Riccardo,

it’s not a RooFit problem, I guess. You need multiple Canvases (or TPads, to be accurate). If you don’t declare any, ROOT will create one (but only one) for you.

TCanvas canv;
canv.Divide(2, 1); // You can use that to make different partitioning.;

They do it similarly in the very first RooFit tutorial, for example.

Here’s the rest of the tutorials if you need some more inspiration:

1 Like

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