#include "TCanvas.h" #include "TMath.h" #include "TH1.h" #include "TROOT.h" #include "TSystem.h" #include "TMinuit.h" #include "RooExponential.h" #include "RooAddPdf.h" #include "RooExtendPdf.h" #include "RooPolynomial.h" #include "RooChebychev.h" #include "RooGaussModel.h" #include "RooRealVar.h" #include "RooDataSet.h" #include "RooFitResult.h" #include "RooPlot.h" #include "RooStats/SPlot.h" #include "TMatrixT.h" double massL = 4600.; double massH = 6000.; double mBs0 = 5360.; double sigmaBs = 90.; double mK0 = 497.614; double nSIG_Gen = 3000; double nBKG_Gen = 1100; void makefit(bool doSplot, bool useMyMatrix, bool debug) { TString myfilename = "log_splot.txt"; const int totEntries = nSIG_Gen + nBKG_Gen; std::cout<<"\n ======> Started to execute code. Total entries to be generated: "<Divide(2,2); canb->cd(1); TCanvas *can = new TCanvas("can", "can", 1000, 1200); can->Divide(2,2); can->cd(1); // // **************************************************** // // // // Variables, models etc // // // // **************************************************** // // RooRealVar B_MM ("B_MM","B_MM",massL,massH); RooPlot *p = B_MM.frame(); //#-->model for the invariant mass of Bd: double-side Crystal Ball RooRealVar mu ("mu","mu",mBs0, mBs0-100,mBs0+100); RooRealVar sigma ("sigma","sigma",sigmaBs, 80,110); RooGaussModel modelSignal("modelSignal","modelSignal", B_MM, mu, sigma); // combinatorial RooRealVar tau ("tau","tau",-0.00038,-0.1,0.1); RooExponential modelCombinatorial ("modelCombinatorial","modelCombinatorial",B_MM,tau); RooRealVar aSIGYield ("aSIGYield","aSIGYield", nSIG_Gen, 0,1e6); RooExtendPdf modelSignal_Norm ("modelSignal_Norm","modelSignal_Norm",modelSignal,aSIGYield); RooRealVar bCombYield ("bCombYield","bCombYield", nBKG_Gen, 0,1e6); RooExtendPdf modelCombinatorial_Norm ("modelCombinatorial_Norm","modelCombinatorial_Norm",modelCombinatorial,bCombYield); RooAddPdf model ("model","model", RooArgList(modelSignal_Norm, modelCombinatorial_Norm)); // // **************************************************** // // // // Make and plot toy data // // // // **************************************************** // // RooDataSet* toydata = model.generate(RooArgSet(B_MM),nSIG_Gen + nBKG_Gen); can->cd(1); toydata->plotOn(p); model.plotOn(p, RooFit::LineColor(kBlue)); model.plotOn(p, RooFit::Components("modelSignal_Norm"), RooFit::LineColor(kBlue)); model.plotOn(p, RooFit::Components("modelCombinatorial_Norm"), RooFit::LineColor(kGreen)); p->Draw(); // // **************************************************** // // // // Fit toy data. All floating // // // // **************************************************** // // gSystem->RedirectOutput(myfilename); RooFitResult *res_mll = model.fitTo(*toydata, RooFit::Save(true), RooFit::Extended(kTRUE), RooFit::SumW2Error(kTRUE) ); gSystem->RedirectOutput(0); double sigYieldFitted = aSIGYield.getVal(); double combYieldFitted = bCombYield.getVal(); std::cout<<" \n All parameters floating... the yields are (S) "<RedirectOutput(myfilename); RooFitResult *res_mll_fix = model.fitTo(*toydata, RooFit::Save(true), RooFit::Extended()); gSystem->RedirectOutput(0); sigYieldFitted = aSIGYield.getVal(); combYieldFitted = bCombYield.getVal(); std::cout<<"\n Only yields floating.... the yields are (S) "<cd(2); p->Draw(); // TMinuit *gMinuit = new TMinuit(2); // gMinuit->SetFCN(model); // // **************************************************** // // // // Print fit results // // // // **************************************************** // // std::cout<<"\n ================================================== \n result of initial fit "<Print(); const TMatrixDSym &res_cov = res_mll->covarianceMatrix(); std::cout<<"Printing covariance matrix of first fit .... "<Print(); const TMatrixDSym &res_cov_fix = res_mll_fix->covarianceMatrix(); std::cout<<"Printing covariance matrix of the second fit .... "<getVal() <getVal() <getVal() <getPropagatedError(*res_mll_fix); RooArgSet vars(aSIGYield, bCombYield, mu, sigma, tau); B_MM.setVal(5484.27); std::cout< Matrix was inverted... \n "<cd(1); TH1F *h1 = new TH1F("h1", "h1", 100, massL, massH); h1->Sumw2(); TH1F *h2 = new TH1F("h2", "h2", 100, massL, massH); h2->Sumw2(); for(int i=0 ; iFill(m_tmp, sigwt[i]); h2->Fill(m_tmp, combwt[i]); } h2->SetLineColor(kRed); h2->SetMarkerColor(kRed); h1->DrawCopy(); canb->cd(2); h2->DrawCopy(); // // end of plotting if(doSplot){ std::cout<<"// // **************************************************** // // "<GetYieldFromSWeight("aSIGYield") << std::endl; std::cout << "Yield of BKG is " << bCombYield.getVal() << ". From sWeights it is " << sData->GetYieldFromSWeight("bCombYield") << std::endl; std::cout << std::endl; for(Int_t i=0; i < totEntries; i+=1000) { std::cout << i<<". Bmass = "<< mass_val.at(i) << " sig Weight " << sData->GetSWeight(i,"aSIGYield") << " comb Weight " << sData->GetSWeight(i,"bCombYield") << " Total Weight " << sData->GetSumOfEventSWeight(i) << std::endl; } B_MM.setBins(100); RooDataSet * data_sig = new RooDataSet(toydata->GetName(),toydata->GetTitle(), toydata, *toydata->get(),0,"aSIGYield_sw"); RooPlot *p2[2]; p2[0]= B_MM.frame(); data_sig->plotOn(p2[0]); can->cd(3); p2[0]->Draw(); RooDataSet * data_bkg = new RooDataSet(toydata->GetName(),toydata->GetTitle(), toydata, *toydata->get(),0,"bCombYield_sw"); p2[1]= B_MM.frame(); data_bkg->plotOn(p2[0], RooFit::LineColor(kRed), RooFit::MarkerColor(kRed)); can->cd(4); p2[0]->Draw(); } // if doSplot std::cout<<"\n Fit output saved in file "<