#include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "TCanvas.h" #include "RooPlot.h" #include "TAxis.h" using namespace RooFit; void test_shiftRooKey() { // S e t u p m o d e l // --------------------- // Declare variables x,mean,sigma with associated name, title, initial value and allowed range RooRealVar x("x", "x", 0, -10, 10); RooRealVar shift( "shift", "shift" , 0, -10,10); RooRealVar mean("mean", "mean of gaussian", 0, -10, 10); RooRealVar sigma("sigma", "width of gaussian", 3, 0.1, 10); RooRealVar shiftPar("shift", "shift", 0, -20, 20); RooFormulaVar shiftMean( "shiftMean", "@0-@1", RooArgList(x, shiftPar)); RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma); RooPlot *xframe = x.frame(Title("Gaussian pdf.")); // P l o t m o d e l used to generate // --------------------------------------------------------------------------- gauss.plotOn(xframe); gauss.plotOn(xframe, LineColor(kRed)); RooDataSet *data = gauss.generate(x, 10000); //make a RooKeyPdf on the x, and passing the shiftedX par RooKeysPdf rookeyNotShift("myRooKey", "myRooKey", shiftMean, x, *data); //plot the RooKeyPdf with "no-shift" in place obtained from generated centered gaussian rookeyNotShift.plotOn(xframe,LineColor(kGray)); //shift the gaussian by 3 units, to fit the RooKeyAfterwards mean.setVal(3); RooDataSet *data_shifted = gauss.generate(x, 10000); //Make the second plot RooPlot *xframe2 = x.frame(Title("RooKey with the shift applied fit")); data_shifted->plotOn(xframe2,LineColor(kRed)); rookeyNotShift.fitTo( *data_shifted); rookeyNotShift.plotOn( xframe2); // Print values of mean and sigma (that now reflect fitted values and errors) mean.Print(); shiftPar.Print(); sigma.Print(); // Draw all frames on a canvas TCanvas *c = new TCanvas("rf101_basics", "rf101_basics", 800, 400); c->Divide(2); c->cd(1); gPad->SetLeftMargin(0.15); xframe->GetYaxis()->SetTitleOffset(1.6); xframe->Draw(); c->cd(2); gPad->SetLeftMargin(0.15); xframe2->GetYaxis()->SetTitleOffset(1.6); xframe2->Draw(); }