使用RooFit的原因:pdf;拟合;Toy MC;可视化
1.基本的数据结构
变量:x | RooRealVar |
函数:f(x) | RooAbsReal |
PDF:f(x) | RooAbsPdf |
数据: | RooArgSet |
积分: | RooRealIntegral |
数据集 | RooAbsData |
为了理解,下面给出一个例子,创建了一个Gaussian的pdf,并使用RooFit把它画出来。
using namespace RooFit;
RooRealVar x("x","x",-10,10) ;
RooRealVar mean("mean","mean of gaussian",0,-10,10) ;
RooRealVar sigma("sigma","width of gaussian",1) ;
RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
RooPlot* xframe = x.frame() ;
gauss.plotOn(xframe) ;
xframe->Draw() ;
c1->Draw();
上面的例子中用了三种不同的RooRealVar定义方式:
RooRealVar::RooRealVar ( const char * name,
const char * title,
Double_t minValue,
Double_t maxValue,
const char * unit = ""
)
RooRealVar::RooRealVar ( const char * name,
const char * title,
Double_t value,
Double_t minValue,
Double_t maxValue,
const char * unit = ""
)
RooRealVar::RooRealVar ( const char * name,
const char * title,
Double_t value,
const char * unit = ""
)
2.定义自己的pdf
使用定义好的pdf肯定是简单的,RooFit中已经给出很多常用的函数。但在一些情况下,我们需要把一个已知函数转化为pdf,RooFit使得这个过程十分简单。
using namespace RooFit;
RooRealVar x("x","x",-1,1) ;
RooRealVar a("a","a",3.0) ;
RooRealVar b("b","b",-2.0) ;
RooGenericPdf gp("gp","Generic PDF","exp(x*x+a)-b*x",RooArgSet(x,a,b)) ;
RooPlot* xframe = x.frame() ;
gp.plotOn(xframe) ;
xframe->Draw() ;
c1->Draw();
很明显,RooFit给我们返回了一个归一化的pdf。
3.对任意pdf进行抽样(toy MC)
using namespace RooFit;
RooRealVar x("x","x",-1,1) ;
RooRealVar a("a","a",3.0) ;
RooRealVar b("b","b",-2.0) ;
RooGenericPdf gp("gp","Generic PDF","exp(x*x+a)-b*x",RooArgSet(x,a,b)) ;
RooDataSet* data = gp.generate(x,10000) ;
RooPlot* xframe = x.frame() ;
data->plotOn(xframe) ;
gp.plotOn(xframe);
xframe->Draw() ;
c1->Draw();
- 生成的数据是不分Bin的,只是在画图时以分Bin的形式画出;
- pdf自动归一化到数据集。
4.用两个pdf的和拟合数据并给出拟合参数
有一丢丢复杂,参考官方例子:rf204a_extendedLikelihood.C
using namespace RooFit;
auto c1 = new TCanvas("Canvas", "Canvas", 800, 600);
RooRealVar x("x", "x", 0, 10);
// Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
RooRealVar mean("mean", "mean of gaussians", 5);
RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
RooRealVar sigma2("sigma2", "width of gaussians", 1);
RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
// Build Chebychev polynomial pdf
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", 0.2, 0., 1.);
RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
// Sum the signal components into a composite signal pdf
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
// Define signal range in which events counts are to be defined
x.setRange("signalRange",4.,6.) ;
// Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange"
RooRealVar nsig("nsig","number of signal events in signalRange",5000,0.,10000) ;
RooRealVar nbkg("nbkg","number of background events in signalRange",5000,0.,10000) ;
//Use Extend Pdf to Count the events
RooExtendPdf esig("esig","signal pdf",sig,nsig,"signalRange");
RooExtendPdf ebkg("ebkg","background pdf",bkg,nbkg,"signalRange");
RooAddPdf model("model","(g1+g2)+a", RooArgList(esig,ebkg)) ;
RooDataSet *data = model.generate(x,10000) ;
RooFitResult* r = model.fitTo(*data,Extended(kTRUE),Save()) ;
// Extended(Bool_t flag) Add extended likelihood term, off by default
//Save(Bool_t flag) Flag controls if RooFitResult object is produced and returned, off by default
r->Print();
RooPlot * frame = x.frame(Title("full range fitted"));
data->plotOn(frame);
model.plotOn(frame, VisualizeError(*r));
//VisualizeError(const RooFitResult&) Visualize the uncertainty on the parameters, as given in fitres, at 'Z' sigma.
model.plotOn(frame);
model.plotOn(frame,Components(sig),LineStyle(2),LineColor(2));
model.plotOn(frame,Components(bkg),LineStyle(5),LineColor(3));
model.paramOn(frame,Layout(0.1, 0.45, 0.9));
frame->Draw();
c1->Draw();
上述代码实现了全区间的拟合,在(4,6)区间的计数。