#include "RooRealVar.h" #include "RooDataSet.h" #include "RooDataHist.h" #include "RooGaussian.h" #include "RooCategory.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "TFile.h" using namespace RooFit; TRandom3 rrand(0); void test(){ //Define the interval for the data observable x, and the number of bins const double xmin=0; const double xmax=100; const int xbins=400; //Create a histogram for this observable TH1D *hx=new TH1D("hx","hx",xbins,xmin,xmax); //Define the interval for the MC prediction y, and the number of bins const double ymin=-100; const double ymax=200; const int ybins=1200; //Create a histogram for this prediction TH1D *hy=new TH1D("hy","hy",ybins,ymin,ymax); //Fill the histogram data with pseudo-events const double xmean=20; const double xsigma=1.; for (int ii=0;ii<100000;ii++){ hx->Fill(rrand.Gaus(xmean,xsigma)); } //define the true scaling value alpha (y=x*alpha) const double alpha=1.1; //Fill the MC pdf with ymean=xmean*alpha and sigma=xsigma*alpha const double ymean=xmean*alpha; const double ysigma=xsigma*alpha; for (int ii=0;ii<2000000;ii++){ hy->Fill(rrand.Gaus(ymean,ysigma)); } //Create RooRealVar x and the dataset RooRealVar x("x","x",xmin,xmax); RooDataHist dx("dx","dx",RooArgList(x),hx); //Create RooRealVar y and the dataset RooRealVar y("y","y",ymin,ymax); RooDataHist dy("dy","dy",RooArgList(y),hy); //Create RooRealVar alpha RooRealVar valpha("valpha","valpha",1.,0.8,1.2); //Create RooFormula var RooFormulaVar vx("vx","x*valpha",RooArgList(x,valpha)); //Create the PDF for x RooHistPdf pdf("pdf","pdf",vx,y,dy,2); //Create a sum with a constant (usually helps to avoid PDF being exactly zero) auto r=RooPolynomial("pol0","pol0",x,RooArgList()); auto f=RooRealVar("f","f",0.9,0.8,1.0); auto s=RooAddPdf("model","model",RooArgList(pdf,r),RooArgList(f)); //Fit s.fitTo(dx); auto frame=x.frame(); dx.plotOn(frame); pdf.plotOn(frame); frame->Draw(); }