# 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 ( = 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);

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

:red_circle:  RooPlot *frame_eml = x.frame(RooFit::Name("Fit EML"), RooFit::Title("Fit EML"));
hist.plotOn(frame_eml);
model_eml.plotOn(frame_eml);
frame_eml->Draw();
``````

Riccardo

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.
canv.cd(1);
firstPlot->Draw();

canv.cd(2);
secondPlot->Draw();
``````

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:
https://root.cern/doc/master/group__tutorial__roofit.html

1 Like

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