//_________________________________________________ void TestJeffreysGaussSigma(){ // this one is VERY sensitive // if the Gaussian is narrow ~ range(x)/nbins(x) then the peak isn't resolved // and you get really bizzare shapes // if the Gaussian is too wide range(x) ~ sigma then PDF gets renormalized // and the PDF falls off too fast at high sigma RooWorkspace w("w"); w.factory("Gaussian::g(x[0,-20,20],mu[0,-5,5],sigma[1,1,5])"); w.factory("n[100,.1,2000]"); w.factory("ExtendPdf::p(g,n)"); // w.var("sigma")->setConstant(); w.var("mu")->setConstant(); w.var("n")->setConstant(); w.var("x")->setBins(301); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; // w.defineSet("poi","mu,sigma"); //w.defineSet("poi","mu,sigma,n"); w.defineSet("poi","sigma"); w.defineSet("obs","x"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",3); const RooArgSet* temp = w.set("poi"); pi.getParameters(*temp)->Print(); // return; // return; RooGenericPdf* test = new RooGenericPdf("test","test","sqrt(2.)/sigma",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("sigma")->frame(); pi.plotOn(plot); test->plotOn(plot,LineColor(kRed),LineStyle(kDotted)); plot->Draw(); }
void JeffreysPriorDemo(){ RooWorkspace w("w"); w.factory("Uniform::u(x[0,1])"); w.factory("mu[100,1,200]"); w.factory("ExtendPdf::p(u,mu)"); // w.factory("Poisson::pois(n[0,inf],mu)"); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); // RooDataHist* asimov2 = w.pdf("pois")->generateBinned(*w.var("n"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; w.defineSet("poi","mu"); w.defineSet("obs","x"); // w.defineSet("obs2","n"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; // pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",10); // JeffreysPrior pi2("jeffreys2","jeffreys",*w.pdf("pois"),*w.set("poi"),*w.set("obs2")); // return; RooGenericPdf* test = new RooGenericPdf("test","test","1./sqrt(mu)",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("mu")->frame(); // pi.plotOn(plot, Normalization(1,RooAbsReal::Raw),Precision(.1)); pi.plotOn(plot); // pi2.plotOn(plot,LineColor(kGreen),LineStyle(kDotted)); test->plotOn(plot,LineColor(kRed)); plot->Draw(); }
//_________________________________________________ void TestJeffreysGaussMean(){ RooWorkspace w("w"); w.factory("Gaussian::g(x[0,-20,20],mu[0,-5,5],sigma[1,0,10])"); w.factory("n[10,.1,200]"); w.factory("ExtendPdf::p(g,n)"); w.var("sigma")->setConstant(); w.var("n")->setConstant(); RooDataHist* asimov = w.pdf("p")->generateBinned(*w.var("x"),ExpectedData()); RooFitResult* res = w.pdf("p")->fitTo(*asimov,Save(),SumW2Error(kTRUE)); asimov->Print(); res->Print(); TMatrixDSym cov = res->covarianceMatrix(); cout << "variance = " << (cov.Determinant()) << endl; cout << "stdev = " << sqrt(cov.Determinant()) << endl; cov.Invert(); cout << "jeffreys = " << sqrt(cov.Determinant()) << endl; // w.defineSet("poi","mu,sigma"); w.defineSet("poi","mu"); w.defineSet("obs","x"); RooJeffreysPrior pi("jeffreys","jeffreys",*w.pdf("p"),*w.set("poi"),*w.set("obs")); // pi.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ; // pi.specialIntegratorConfig(kTRUE)->getConfigSection("RooIntegrator1D").setRealValue("maxSteps",3); const RooArgSet* temp = w.set("poi"); pi.getParameters(*temp)->Print(); // return; RooGenericPdf* test = new RooGenericPdf("test","test","1",*w.set("poi")); TCanvas* c1 = new TCanvas; RooPlot* plot = w.var("mu")->frame(); pi.plotOn(plot); test->plotOn(plot,LineColor(kRed),LineStyle(kDotted)); plot->Draw(); }
void rf403_weightedevts() { // C r e a t e o b s e r v a b l e a n d u n w e i g h t e d d a t a s e t // ------------------------------------------------------------------------------- // Declare observable RooRealVar x("x","x",-10,10) ; x.setBins(40) ; // Construction a uniform pdf RooPolynomial p0("px","px",x) ; // Sample 1000 events from pdf RooDataSet* data = p0.generate(x,1000) ; // C a l c u l a t e w e i g h t a n d m a k e d a t a s e t w e i g h t e d // ----------------------------------------------------------------------------------- // Construct formula to calculate (fake) weight for events RooFormulaVar wFunc("w","event weight","(x*x+10)",x) ; // Add column with variable w to previously generated dataset RooRealVar* w = (RooRealVar*) data->addColumn(wFunc) ; // Dataset d is now a dataset with two observable (x,w) with 1000 entries data->Print() ; // Instruct dataset wdata in interpret w as event weight rather than as observable RooDataSet wdata(data->GetName(),data->GetTitle(),data,*data->get(),0,w->GetName()) ; // Dataset d is now a dataset with one observable (x) with 1000 entries and a sum of weights of ~430K wdata.Print() ; // U n b i n n e d M L f i t t o w e i g h t e d d a t a // --------------------------------------------------------------- // Construction quadratic polynomial pdf for fitting RooRealVar a0("a0","a0",1) ; RooRealVar a1("a1","a1",0,-1,1) ; RooRealVar a2("a2","a2",1,0,10) ; RooPolynomial p2("p2","p2",x,RooArgList(a0,a1,a2),0) ; // Fit quadratic polynomial to weighted data // NOTE: A plain Maximum likelihood fit to weighted data does in general // NOT result in correct error estimates, unless individual // event weights represent Poisson statistics themselves. // // Fit with 'wrong' errors RooFitResult* r_ml_wgt = p2.fitTo(wdata,Save()) ; // A first order correction to estimated parameter errors in an // (unbinned) ML fit can be obtained by calculating the // covariance matrix as // // V' = V C-1 V // // where V is the covariance matrix calculated from a fit // to -logL = - sum [ w_i log f(x_i) ] and C is the covariance // matrix calculated from -logL' = -sum [ w_i^2 log f(x_i) ] // (i.e. the weights are applied squared) // // A fit in this mode can be performed as follows: RooFitResult* r_ml_wgt_corr = p2.fitTo(wdata,Save(),SumW2Error(kTRUE)) ; // P l o t w e i g h e d d a t a a n d f i t r e s u l t // --------------------------------------------------------------- // Construct plot frame RooPlot* frame = x.frame(Title("Unbinned ML fit, binned chi^2 fit to weighted data")) ; // Plot data using sum-of-weights-squared error rather than Poisson errors wdata.plotOn(frame,DataError(RooAbsData::SumW2)) ; // Overlay result of 2nd order polynomial fit to weighted data p2.plotOn(frame) ; // M L F i t o f p d f t o e q u i v a l e n t u n w e i g h t e d d a t a s e t // ----------------------------------------------------------------------------------------- // Construct a pdf with the same shape as p0 after weighting RooGenericPdf genPdf("genPdf","x*x+10",x) ; // Sample a dataset with the same number of events as data RooDataSet* data2 = genPdf.generate(x,1000) ; // Sample a dataset with the same number of weights as data RooDataSet* data3 = genPdf.generate(x,43000) ; // Fit the 2nd order polynomial to both unweighted datasets and save the results for comparison RooFitResult* r_ml_unw10 = p2.fitTo(*data2,Save()) ; RooFitResult* r_ml_unw43 = p2.fitTo(*data3,Save()) ; // C h i 2 f i t o f p d f t o b i n n e d w e i g h t e d d a t a s e t // ------------------------------------------------------------------------------------ // Construct binned clone of unbinned weighted dataset RooDataHist* binnedData = wdata.binnedClone() ; binnedData->Print("v") ; // Perform chi2 fit to binned weighted dataset using sum-of-weights errors // // NB: Within the usual approximations of a chi2 fit, a chi2 fit to weighted // data using sum-of-weights-squared errors does give correct error // estimates RooChi2Var chi2("chi2","chi2",p2,*binnedData,DataError(RooAbsData::SumW2)) ; RooMinuit m(chi2) ; m.migrad() ; m.hesse() ; // Plot chi^2 fit result on frame as well RooFitResult* r_chi2_wgt = m.save() ; p2.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ; // C o m p a r e f i t r e s u l t s o f c h i 2 , M L f i t s t o ( u n ) w e i g h t e d d a t a // --------------------------------------------------------------------------------------------------------------- // Note that ML fit on 1Kevt of weighted data is closer to result of ML fit on 43Kevt of unweighted data // than to 1Kevt of unweighted data, whereas the reference chi^2 fit with SumW2 error gives a result closer to // that of an unbinned ML fit to 1Kevt of unweighted data. cout << "==> ML Fit results on 1K unweighted events" << endl ; r_ml_unw10->Print() ; cout << "==> ML Fit results on 43K unweighted events" << endl ; r_ml_unw43->Print() ; cout << "==> ML Fit results on 1K weighted events with a summed weight of 43K" << endl ; r_ml_wgt->Print() ; cout << "==> Corrected ML Fit results on 1K weighted events with a summed weight of 43K" << endl ; r_ml_wgt_corr->Print() ; cout << "==> Chi2 Fit results on 1K weighted events with a summed weight of 43K" << endl ; r_chi2_wgt->Print() ; new TCanvas("rf403_weightedevts","rf403_weightedevts",600,600) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.8) ; frame->Draw() ; }
void rf401_importttreethx() { // I m p o r t m u l t i p l e T H 1 i n t o a R o o D a t a H i s t // -------------------------------------------------------------------------- // Create thee ROOT TH1 histograms TH1* hh_1 = makeTH1("hh1",0,3) ; TH1* hh_2 = makeTH1("hh2",-3,1) ; TH1* hh_3 = makeTH1("hh3",+3,4) ; // Declare observable x RooRealVar x("x","x",-10,10) ; // Create category observable c that serves as index for the ROOT histograms RooCategory c("c","c") ; c.defineType("SampleA") ; c.defineType("SampleB") ; c.defineType("SampleC") ; // Create a binned dataset that imports contents of all TH1 mapped by index category c RooDataHist* dh = new RooDataHist("dh","dh",x,Index(c),Import("SampleA",*hh_1),Import("SampleB",*hh_2),Import("SampleC",*hh_3)) ; dh->Print() ; // Alternative constructor form for importing multiple histograms map<string,TH1*> hmap ; hmap["SampleA"] = hh_1 ; hmap["SampleB"] = hh_2 ; hmap["SampleC"] = hh_3 ; RooDataHist* dh2 = new RooDataHist("dh","dh",x,c,hmap) ; dh2->Print() ; // I m p o r t i n g a T T r e e i n t o a R o o D a t a S e t w i t h c u t s // ----------------------------------------------------------------------------------------- TTree* tree = makeTTree() ; // Define observables y,z RooRealVar y("y","y",-10,10) ; RooRealVar z("z","z",-10,10) ; // Import only observables (y,z) RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ; ds.Print() ; // Import observables (x,y,z) but only event for which (y+z<0) is true RooDataSet ds2("ds2","ds2",RooArgSet(x,y,z),Import(*tree),Cut("y+z<0")) ; ds2.Print() ; // I m p o r t i n g i n t e g e r T T r e e b r a n c h e s // --------------------------------------------------------------- // Import integer tree branch as RooRealVar RooRealVar i("i","i",0,5) ; RooDataSet ds3("ds3","ds3",RooArgSet(i,x),Import(*tree)) ; ds3.Print() ; // Define category i RooCategory icat("i","i") ; icat.defineType("State0",0) ; icat.defineType("State1",1) ; // Import integer tree branch as RooCategory (only events with i==0 and i==1 // will be imported as those are the only defined states) RooDataSet ds4("ds4","ds4",RooArgSet(icat,x),Import(*tree)) ; ds4.Print() ; // I m p o r t m u l t i p l e R o o D a t a S e t s i n t o a R o o D a t a S e t // ---------------------------------------------------------------------------------------- // Create three RooDataSets in (y,z) RooDataSet* dsA = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z<-5") ; RooDataSet* dsB = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"abs(z)<5") ; RooDataSet* dsC = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z>5") ; // Create a dataset that imports contents of all the above datasets mapped by index category c RooDataSet* dsABC = new RooDataSet("dsABC","dsABC",RooArgSet(x,y),Index(c),Import("SampleA",*dsA),Import("SampleB",*dsB),Import("SampleC",*dsC)) ; dsABC->Print() ; }
void Back_2dFit(void){ TFile *ifile = TFile::Open("/home/vitaly/B0toDh0/TMVA/fil_b2dh_gen.root"); TTree *tree = (TTree*)ifile->Get("TEvent"); RooCategory b0f("b0f","b0f"); b0f.defineType("comb",-1); RooArgSet argset; const double mbcMin = 5.20; const double mbcMax = 5.29; const double deMin = -0.15; const double deMax = 0.3; RooRealVar mbc("mbc","M_{bc}",mbcMin,mbcMax,"GeV"); argset.add(mbc); mbc.setRange("Signal",mbc_min,mbc_max); mbc.setRange("mbcSignal",mbc_min,mbc_max); mbc.setRange("deSignal",mbcMin,mbcMax); RooRealVar de("de","#DeltaE",deMin,deMax,"GeV"); argset.add(de); de.setRange("Signal",de_min,de_max); de.setRange("mbcSignal",deMin,deMax); de.setRange("deSignal",de_min,de_max); RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV"); argset.add(md); RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV"); argset.add(mk); RooRealVar mpi0("mpi0","mpi0",Pi0Mass-mpi0_cut,Pi0Mass+mpi0_cut,"GeV"); argset.add(mpi0); RooRealVar bdtgs("bdtgs","bdtgs",bdtgs_cut,1.); argset.add(bdtgs); RooRealVar atckpi_max("atckpi_max","atckpi_max",0.,atckpi_cut); argset.add(atckpi_max); argset.add(b0f); RooDataSet ds("ds","ds",tree,argset); RooDataSet* ds0 = ds.reduce(RooArgSet(de,mbc)); RooDataHist* dh = ds0->binnedClone(); ds.Print(); dh->Print(); //////////// // de pdf // //////////// // RooRealVar c1("c1","c1",mc_c1,-1.,0.); if(cComb) c1.setConstant(kTRUE); // RooRealVar c2("c2","c2",mc_c2,0.,0.1); if(cComb) c2.setConstant(kTRUE); RooRealVar c3("c3","c3",mc_c1,-1.,0.); if(cComb) c3.setConstant(kTRUE); RooRealVar c4("c4","c4",mc_c2,0.,0.1); if(cComb) c4.setConstant(kTRUE); RooChebychev pdf_de("pdf_de","pdf_de",de,RooArgSet(c3,c4)); ///////////// // mbc pdf // ///////////// RooRealVar argpar("argpar","argus shape parameter",mc_argpar,-100.,-1.); if(cComb) argpar.setConstant(kTRUE); RooRealVar argedge("argedge","argedge",mc_argedge,5.285,5.292); if(cComb) argedge.setConstant(kTRUE); RooArgusBG pdf_mbc("pdf_mbc","Argus PDF",mbc,argedge,argpar); ///////// // pdf // ///////// RooProdPdf pdf("pdf","pdf",RooArgList(pdf_de,pdf_mbc)); pdf.fitTo(*dh,Verbose(),Timer(true)); ///////////// // Plots // ///////////// // de // RooPlot* deFrame = de.frame(); ds.plotOn(deFrame,DataError(RooAbsData::SumW2),MarkerSize(1),MarkerColor(kGreen)); pdf.plotOn(deFrame,LineWidth(2),LineColor(kGreen)); ds.plotOn(deFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("mbcSignal")); pdf.plotOn(deFrame,LineWidth(2),ProjectionRange("mbcSignal")); RooHist* hdepull = deFrame->pullHist(); RooPlot* dePull = de.frame(Title("#Delta E pull distribution")); dePull->addPlotable(hdepull,"P"); dePull->GetYaxis()->SetRangeUser(-5,5); TCanvas* cm = new TCanvas("#Delta E, Signal","#Delta E, Signal",600,700); cm->cd(); TPad *pad3 = new TPad("pad3","pad3",0.01,0.20,0.99,0.99); TPad *pad4 = new TPad("pad4","pad4",0.01,0.01,0.99,0.20); pad3->Draw(); pad4->Draw(); pad3->cd(); pad3->SetLeftMargin(0.15); pad3->SetFillColor(0); deFrame->GetXaxis()->SetTitleSize(0.05); deFrame->GetXaxis()->SetTitleOffset(0.85); deFrame->GetXaxis()->SetLabelSize(0.04); deFrame->GetYaxis()->SetTitleOffset(1.6); deFrame->Draw(); stringstream out; out.str(""); out << "#chi^{2}/n.d.f = " << deFrame->chiSquare(); TPaveText *pt = new TPaveText(0.6,0.8,0.98,0.9,"brNDC"); pt->SetFillColor(0); pt->SetTextAlign(12); pt->AddText(out.str().c_str()); pt->Draw(); TLine *de_line_RIGHT = new TLine(de_max,0,de_max,200); de_line_RIGHT->SetLineColor(kRed); de_line_RIGHT->SetLineStyle(1); de_line_RIGHT->SetLineWidth((Width_t)2.); de_line_RIGHT->Draw(); TLine *de_line_LEFT = new TLine(de_min,0,de_min,200); de_line_LEFT->SetLineColor(kRed); de_line_LEFT->SetLineStyle(1); de_line_LEFT->SetLineWidth((Width_t)2.); de_line_LEFT->Draw(); pad4->cd(); pad4->SetLeftMargin(0.15); pad4->SetFillColor(0); dePull->SetMarkerSize(0.05); dePull->Draw(); TLine *de_lineUP = new TLine(deMin,3,deMax,3); de_lineUP->SetLineColor(kBlue); de_lineUP->SetLineStyle(2); de_lineUP->Draw(); TLine *de_line = new TLine(deMin,0,deMax,0); de_line->SetLineColor(kBlue); de_line->SetLineStyle(1); de_line->SetLineWidth((Width_t)2.); de_line->Draw(); TLine *de_lineDOWN = new TLine(deMin,-3,deMax,-3); de_lineDOWN->SetLineColor(kBlue); de_lineDOWN->SetLineStyle(2); de_lineDOWN->Draw(); cm->Update(); // mbc // RooPlot* mbcFrame = mbc.frame(); ds.plotOn(mbcFrame,DataError(RooAbsData::SumW2),MarkerSize(1),MarkerColor(kGreen)); pdf.plotOn(mbcFrame,LineWidth(2),LineColor(kGreen)); ds.plotOn(mbcFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("deSignal")); pdf.plotOn(mbcFrame,LineWidth(2),ProjectionRange("deSignal")); RooHist* hmbcpull = mbcFrame->pullHist(); RooPlot* mbcPull = mbc.frame(Title("#Delta E pull distribution")); mbcPull->addPlotable(hmbcpull,"P"); mbcPull->GetYaxis()->SetRangeUser(-5,5); TCanvas* cmmbc = new TCanvas("M_{bc}, Signal","M_{bc}, Signal",600,700); cmmbc->cd(); TPad *pad1 = new TPad("pad1","pad1",0.01,0.20,0.99,0.99); TPad *pad2 = new TPad("pad2","pad2",0.01,0.01,0.99,0.20); pad1->Draw(); pad2->Draw(); pad1->cd(); pad1->SetLeftMargin(0.15); pad1->SetFillColor(0); mbcFrame->GetXaxis()->SetTitleSize(0.05); mbcFrame->GetXaxis()->SetTitleOffset(0.85); mbcFrame->GetXaxis()->SetLabelSize(0.04); mbcFrame->GetYaxis()->SetTitleOffset(1.6); mbcFrame->Draw(); out.str(""); out << "#chi^{2}/n.d.f = " << mbcFrame->chiSquare(); TPaveText *ptmbc = new TPaveText(0.6,0.8,0.98,0.9,"brNDC"); ptmbc->SetFillColor(0); ptmbc->SetTextAlign(12); ptmbc->AddText(out.str().c_str()); ptmbc->Draw(); TLine *mbc_line_RIGHT = new TLine(mbc_max,0,mbc_max,150); mbc_line_RIGHT->SetLineColor(kRed); mbc_line_RIGHT->SetLineStyle(1); mbc_line_RIGHT->SetLineWidth((Width_t)2.); mbc_line_RIGHT->Draw(); TLine *mbc_line_LEFT = new TLine(mbc_min,0,mbc_min,150); mbc_line_LEFT->SetLineColor(kRed); mbc_line_LEFT->SetLineStyle(1); mbc_line_LEFT->SetLineWidth((Width_t)2.); mbc_line_LEFT->Draw(); pad2->cd(); pad2->SetLeftMargin(0.15); pad2->SetFillColor(0); mbcPull->SetMarkerSize(0.05); mbcPull->Draw(); TLine *mbc_lineUP = new TLine(mbcMin,3,mbcMax,3); mbc_lineUP->SetLineColor(kBlue); mbc_lineUP->SetLineStyle(2); mbc_lineUP->Draw(); TLine *mbc_line = new TLine(mbcMin,0,mbcMax,0); mbc_line->SetLineColor(kBlue); mbc_line->SetLineStyle(1); mbc_line->SetLineWidth((Width_t)2.); mbc_line->Draw(); TLine *mbc_lineDOWN = new TLine(mbcMin,-3,mbcMax,-3); mbc_lineDOWN->SetLineColor(kBlue); mbc_lineDOWN->SetLineStyle(2); mbc_lineDOWN->Draw(); TH2D* hh_pdf = pdf.createHistogram("hh_data",de,Binning(50,-0.15,0.1),YVar(mbc,Binning(50,5.26,5.30))); hh_pdf->SetLineColor(kBlue); TCanvas* hhc = new TCanvas("hhc","hhc",600,600); hhc->cd(); hh_pdf->Draw("SURF"); cmmbc->Update(); }
void makeModel(RooWorkspace& w) { TFile *_file0 = TFile::Open("plots/htotal_root_ZprimeRecomass.root"); TH1F *Histo = (TH1F*)_file0->Get("htotaldata"); RooRealVar invm("invm","invm",200.,4000.); RooDataHist* data = new RooDataHist("data","data",invm,Import(*Histo)) ; // TTree* tree = new TTree("simple","data from ascii file"); // Long64_t nlines = tree->ReadFile("list_mll_200_2016.txt","x1:x2:x3:invm:x5:x6"); // Long64_t nlines = tree->ReadFile("a.txt","x1:x2:x3:invm:x5:x6"); // printf(" found %lld pointsn",nlines); // tree->Write(); // tree->GetEntries(); RooRealVar mass("mass","mass", 300., 200., 1600.); RooRealVar nsig("nsig","Number of signal events", 0., 5000.); RooRealVar nbkg("nbkg","Number of background events", 0., 300000.); w.import(mass); w.import(nsig); w.import(nbkg); // RooRealVar invm("invm","Invariant mass", 200., 4000.); // RooDataSet* data = new RooDataSet("data", "Data", invm, RooFit::Import(*tree)); data->Print("v"); w.import(invm); w.import(*data); w.factory("expr::sigma('invm*(0.01292 + 0.00001835 * invm - 0.0000000002733 * invm*invm)',invm)"); w.factory("expr::width('0.03*invm',invm)"); w.factory("CEXPR::bkgpdf('exp(24.9327 - 2.39287e-03*invm + 3.19926e-07*invm*invm - 3.38799e-11*invm*invm*invm)*pow(invm,-3.3634)',invm)"); w.factory("Voigtian::sigpdf(invm,mass,width,sigma)"); w.factory("SUM::model(nbkg*bkgpdf, nsig*sigpdf)"); RooAbsPdf* sigpdf = w.pdf("sigpdf"); RooAbsPdf* bkgpdf = w.pdf("bkgpdf"); RooAbsPdf* model = w.pdf("model"); RooStats::ModelConfig* mc = new ModelConfig("mc",&w); mc->SetPdf(*w.pdf("model")); mc->SetParametersOfInterest(*w.var("nsig")); mc->SetObservables(*w.var("invm")); w.defineSet("nuisParams","nbkg"); mc->SetNuisanceParameters(*w.set("nuisParams")); w.var("mass")->setConstant(true); w.import(*mc); w.Print("tree"); w.writeToFile("MyModel_workspace.root"); TCanvas* c1 = new TCanvas("c1","Control Plots", 900, 700); RooPlot* plot = w.var("invm")->frame(); w.data("data")->plotOn(plot); w.pdf("model")->plotOn(plot); w.pdf("model")->plotOn(plot, Components("bkgpdf"),LineStyle(kDashed)); w.pdf("model")->plotOn(plot, Components("sigpdf"),LineColor(kRed)); plot->Draw(); return; }