#include #include #include #include "TPad.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "TTree.h" #include "TCut.h" #include "TMath.h" #include "TFile.h" #include "TCanvas.h" #include "RooHist.h" #include "TLegend.h" #include "TH1.h" #include "TVirtualPad.h" #include "TStyle.h" using namespace std; using namespace RooFit; void fit_fraction_ex(){ gStyle->SetOptStat(0); gStyle->SetOptFit(1); TH1F* h00; TH1F* h01; TH1F* h02; string stot[2]={"0.08","0.2"}; TFile *f = new TFile("Data_hist_2bdt_100_2016BF.root"); int ch=1, k=0; TH1F *h0D =(TH1F*)f->Get(Form("Data_osmdr_ch%d_%d",ch,k)); h00 =(TH1F*)f->Get(Form("MC_osmdr40_ch%d_%d",ch,k)); h01 =(TH1F*)f->Get(Form("MC_osmdr41_ch%d_%d",ch,k)); h02 =(TH1F*)f->Get(Form("MC_osmdr42_ch%d_%d",ch,k)); int nentries_ = h0D->GetEntries(); cout << "\n=> total entries in data = " << nentries_ << endl; TObjArray *mc = new TObjArray(3); mc->Add(h00); mc->Add(h01); mc->Add(h02); TFractionFitter* fit = new TFractionFitter(h0D, mc); fit->Constrain(0,0.0,1.0); fit->Constrain(1,0.0,1.0); fit->Constrain(2,0.0,1.0); Int_t status = fit->Fit(); cout << "fit status: " << status <<"\t chi2 "<GetChisquare()<<"\t ndf "<GetNDF()<< endl; h0D->SetMarkerStyle(20); TH1F* resultmc0 = (TH1F*) fit->GetMCPrediction(0); TH1F* resultmc1 = (TH1F*) fit->GetMCPrediction(1); TH1F* resultmc2 = (TH1F*) fit->GetMCPrediction(2); resultmc0->SetLineColor(2); resultmc0->SetMarkerColor(2); resultmc0->SetMarkerStyle(24); resultmc1->SetLineColor(3); resultmc1->SetMarkerColor(3); resultmc1->SetMarkerStyle(24); resultmc2->SetLineColor(4); resultmc2->SetMarkerColor(4); resultmc2->SetMarkerStyle(24); Double_t p0, p1, p2, errP0, errP1, errP2; TLatex l; l.SetTextSize(.02); Char_t texte[200]; TH1F* result = (TH1F*) fit->GetPlot(); fit->GetResult( 0, p0, errP0); printf(" Parameter %d: estim. %.3f +/- %.3f\n", 0, p0, errP0); fit->GetResult( 1, p1, errP1); printf(" Parameter %d: estim. %.3f +/- %.3f\n", 1, p1, errP1); fit->GetResult( 2, p2, errP2); printf(" Parameter %d: estim. %.3f +/- %.3f\n", 2, p2, errP2); h0D->SetTitle("Data distribution with fitted contributions"); TCanvas c(Form("canv_ch%d_bdt_%d",ch,k), "FractionFitter example", 600, 600); h0D->SetTitle("Data distribution with true contributions"); h0D->DrawClone("EP"); resultmc0->SetTitle("MC generated samples with fit predictions"); resultmc0->Draw("same"); resultmc1->Draw("same"); resultmc2->Draw("same"); result->Draw("same"); sprintf( texte, "%d: estimated %.3f +/- %.3f\n", 0, p0, errP0); l.DrawTextNDC( .15, .87, texte); sprintf( texte, "%d: estimated %.3f +/- %.3f\n", 1, p1, errP1); l.DrawTextNDC( .15, .85, texte); sprintf( texte, "%d: estimated %.3f +/- %.3f\n", 2, p2, errP2); l.DrawTextNDC( .15, .83, texte); //sprintf( texte, "Channel_%d _bdt> %s \n", ch,stot[k].c_str()); sprintf( texte, "Channel_0&1_bdt> %s \n", stot[k].c_str()); l.DrawTextNDC( .15, .81, texte); TLegend* leg3 = new TLegend(0.7,0.7,0.89,0.89); leg3->AddEntry(resultmc0,"Gluon fusion","p"); leg3->AddEntry(resultmc1,"Flavor excitation","p"); leg3->AddEntry(resultmc2,"Gluon splitting","p"); leg3->AddEntry(result,"Total MC","f"); leg3->AddEntry(h0D,"Data","p"); leg3->Draw(); c.SaveAs(Form("fraction_100bin_combinechan_de_plotch%d_bdt_%s.pdf",ch,stot[k].c_str())); //*/ }