Troubles with data in RooFit

Hello everyone,

I’m using RooFit in order to fit a distribution and find a parameter.
The data are from a TTree called “data” and it has a branch called “costheta”.
I wrote this code

using namespace std;

using namespace RooFit;

void fit()
 TFile *input = new TFile("cosine.root");
 TTree *cos = (TTree*)input->Get("data");

 RooRealVar x("x","x",-1.,1.);

 RooRealVar c("c","c",0.375); 
 RooRealVar na("na","na",1500,500,4000);
 RooGenericPdf std("std","c*(1+x**2)",RooArgList(c,x));
 RooRealVar a("a","a",0,-1.,1.);
 RooRealVar nb("nb","nb",1500,500,4000);
 RooGenericPdf asy("asy","a*x",RooArgList(a,x));

 RooAddPdf p("p","p",RooArgList(std,asy),RooArgList(na,nb));
 RooDataSet xdata("xdata","dataset in costheta", cos,x);

 RooFitResult *result = p.fitTo(xdata);

 RooPlot* xframe = x.frame();
 xdata.plotOn(xframe) ;

when i plot the RooDataSet xdata i get something completely wrong, i can’t understand why…obviously, the fit is not correct…i thought about a syntax error but it seems it is not the case…

Thanks in advance for help


what do you mean by “completely wrong”? Could you share the input file in order to make the behaviour reproducible?


Hi, thanks for the reply
The root file is this:
cosine.root (380 KB)
I hope it works, it is the first time i share i file here…

For “completely wrong” i mean this: the distribution is like a Dirac-delta function, all points are zero except one.

Thank you again


thanks, that was the missing piece.
You are constructing your RooDataset in the wrong way. Here you have the correct standalone snippet:

using namespace RooFit;
TFile *input = new TFile("cosine.root");
TTree *cos1 = (TTree*)input->Get("data");

RooRealVar x("costheta","costheta",-1.,1.); // <- NOTICE THE NEW NAME HERE!!!!!
RooDataSet xdata("data for RooFit","dataset in costheta", cos1,x);

RooPlot* xframe = x.frame();
xdata.plotOn(xframe) ;



It worked perfectly, thank you very much for patience and time!