// show the histogram in first slot, try a Gaussian fit with given parameters void PRadHistCanvas::UpdateHist(int index, TObject *tob, int range_min, int range_max) { --index; if(index < 0 || index >= canvases.size()) return; canvases[index]->cd(); canvases[index]->SetGrid(); gPad->SetLogy(); TH1 *hist = (TH1*)tob; hist->GetXaxis()->SetRangeUser(hist->FindFirstBinAbove(0,1) - 10, hist->FindLastBinAbove(0,1) + 10); hist->GetXaxis()->SetLabelSize(HIST_LABEL_SIZE); hist->GetYaxis()->SetLabelSize(HIST_LABEL_SIZE); // try to fit gaussian in certain range if(range_max > range_min && hist->Integral(range_min, range_max + 1) > 0) { TF1 *fit = new TF1("", "gaus", range_min, range_max); fit->SetLineColor(kRed); fit->SetLineWidth(2); hist->Fit(fit,"qlR"); } hist->SetFillColor(fillColors[index]); hist->Draw(); canvases[index]->Refresh(); }
void Fit(TString SIGNAL,TString HISTO,double scaleSGN) { gROOT->ForceStyle(); TFile *fDat = TFile::Open("Histo_flatTree_data_tmva"+SIGNAL+".root"); TFile *fBkg = TFile::Open("Histo_flatTree_qcd_weights_tmva"+SIGNAL+".root"); TFile *fSgn = TFile::Open("Histo_flatTree_"+SIGNAL+"_weights_tmva"+SIGNAL+".root"); TH1 *hDat = (TH1*)fDat->Get(HISTO); TH1 *hBkgRaw = (TH1*)fBkg->Get(HISTO); TH1 *hSgn = (TH1*)fSgn->Get(HISTO); TH1 *hDat_JESlo = (TH1*)fDat->Get(HISTO+"_JESlo"); TH1 *hBkgRaw_JESlo = (TH1*)fBkg->Get(HISTO+"_JESlo"); TH1 *hSgn_JESlo = (TH1*)fSgn->Get(HISTO+"_JESlo"); TH1 *hDat_JESup = (TH1*)fDat->Get(HISTO+"_JESup"); TH1 *hBkgRaw_JESup = (TH1*)fBkg->Get(HISTO+"_JESup"); TH1 *hSgn_JESup = (TH1*)fSgn->Get(HISTO+"_JESup"); TH1F *hBkg = (TH1F*)hBkgRaw->Clone("Bkg"); TH1F *hBkg_JESlo = (TH1F*)hBkgRaw_JESlo->Clone("Bkg_JESlo"); TH1F *hBkg_JESup = (TH1F*)hBkgRaw_JESup->Clone("Bkg_JESup"); hBkg->Smooth(2); hBkg_JESlo->Smooth(2); hBkg_JESup->Smooth(2); hSgn->Smooth(2); hSgn_JESlo->Smooth(2); hSgn_JESup->Smooth(2); double lumi = 4967; hBkg->Scale(lumi); hBkg_JESlo->Scale(lumi); hBkg_JESup->Scale(lumi); double k_factor = hDat->Integral()/hBkg->Integral(); double k_factor_JESlo = hDat->Integral()/hBkg_JESlo->Integral(); double k_factor_JESup = hDat->Integral()/hBkg_JESup->Integral(); hBkg->Scale(k_factor); cout<<"Signal entries = "<<hSgn->GetEntries()<<endl; hSgn->Scale(lumi/scaleSGN); hBkg_JESlo->Scale(k_factor_JESlo); hSgn_JESlo->Scale(lumi/scaleSGN); hBkg_JESup->Scale(k_factor_JESup); hSgn_JESup->Scale(lumi/scaleSGN); hSgn_JESlo->Scale(hSgn->Integral()/hSgn_JESlo->Integral()); hSgn_JESup->Scale(hSgn->Integral()/hSgn_JESup->Integral()); TH1 *hBkg_STATlo = (TH1*)hBkg->Clone(HISTO+"_STATlo"); TH1 *hSgn_STATlo = (TH1*)hSgn->Clone(HISTO+"_STATlo"); TH1 *hBkg_STATup = (TH1*)hBkg->Clone(HISTO+"_STATup"); TH1 *hSgn_STATup = (TH1*)hSgn->Clone(HISTO+"_STATup"); float y1,e1; for(int i=0;i<hBkg->GetNbinsX();i++) { y1 = hBkg->GetBinContent(i+1); e1 = hBkg->GetBinError(i+1); hBkg_STATlo->SetBinContent(i+1,y1-e1); hBkg_STATup->SetBinContent(i+1,y1+e1); y1 = hSgn->GetBinContent(i+1); e1 = hSgn->GetBinError(i+1); hSgn_STATlo->SetBinContent(i+1,y1-e1); hSgn_STATup->SetBinContent(i+1,y1+e1); } hBkg_STATlo->Scale(hBkg->Integral()/hBkg_STATlo->Integral()); hBkg_STATup->Scale(hBkg->Integral()/hBkg_STATup->Integral()); hSgn_STATlo->Scale(hSgn->Integral()/hSgn_STATlo->Integral()); hSgn_STATup->Scale(hSgn->Integral()/hSgn_STATup->Integral()); double xMIN = hBkg->GetBinLowEdge(1); double xMAX = hBkg->GetBinLowEdge(hBkg->GetNbinsX()+1); double xMIN2 = hDat->GetBinLowEdge(hDat->FindFirstBinAbove(0.5)); double xMAX2 = hDat->GetBinLowEdge(hDat->FindLastBinAbove(0.5)+1); RooRealVar x("x","x",xMIN2,xMAX2); RooDataHist data("data","dataset with x",x,hDat); RooDataHist bkg("qcd","bkg with x",x,hBkg); RooDataHist sgn("signal","sgn with x",x,hSgn); RooHistPdf bkgPDF("bkgPDF","bkgPDF",x,bkg,0); RooHistPdf sgnPDF("sgnPDF","sgnPDF",x,sgn,0); RooRealVar f("f","f",0,0.,1.); RooAddPdf model("model","model",RooArgList(sgnPDF,bkgPDF),RooArgList(f)); RooFitResult* r = model.fitTo(data,Save()); r->Print("v"); double N = hDat->Integral(); double B = hBkg->Integral(); double S = hSgn->Integral(); double m = f.getVal(); double e = f.getError(); cout<<"k-factor = "<<k_factor<<endl; cout<<N<<" "<<B<<" "<<S<<endl; cout<<"Total cross section = "<<N/lumi<<" pb"<<endl; cout<<"Model cross section = "<<S/lumi<<" pb"<<endl; cout<<"Fitted signal strength = "<<m*N/S<<endl; cout<<"Fitted signal error = "<<e*N/S<<endl; double p = 0.95; double xup = (N/S)*(m+sqrt(2.)*e*TMath::ErfInverse((1-p)*TMath::Erf(m/e)+p)); cout<<"Bayesian Upper limit = "<<xup<<endl; RooPlot* frame1 = x.frame(); data.plotOn(frame1); model.plotOn(frame1,Components("sgnPDF*"),LineStyle(1),LineWidth(2),LineColor(kGreen+1)); model.plotOn(frame1,Components("bkgPDF*"),LineStyle(1),LineWidth(2),LineColor(kRed)); model.plotOn(frame1,LineStyle(1),LineWidth(2),LineColor(kBlue)); //cout<<frame1->chiSquare()<<endl; RooHist* hresid = frame1->residHist(); RooHist* hpull = frame1->pullHist(); RooPlot* frame2 = x.frame(Title("Residual Distribution")); frame2->addPlotable(hresid,"P") ; // Create a new frame to draw the pull distribution and add the distribution to the frame RooPlot* frame3 = x.frame(Title("Pull Distribution")); frame3->addPlotable(hpull,"P"); TCanvas* cFit = new TCanvas("fitANN_"+SIGNAL,"fitANN_"+SIGNAL,900,600); gPad->SetLogy(); frame1->SetMaximum(1e+4); frame1->SetMinimum(0.5); frame1->GetXaxis()->SetTitle("ANN Output"); frame1->GetYaxis()->SetTitle("Events"); frame1->Draw(); cout<<frame1->nameOf(3)<<endl; TLegend *leg = new TLegend(0.7,0.65,0.9,0.9); leg->SetHeader(SIGNAL); leg->AddEntry(frame1->findObject("h_data"),"data","P"); leg->AddEntry(frame1->findObject("model_Norm[x]"),"QCD+Signal","L"); leg->AddEntry(frame1->findObject("model_Norm[x]_Comp[bkgPDF*]"),"QCD","L"); leg->AddEntry(frame1->findObject("model_Norm[x]_Comp[sgnPDF*]"),"Signal","L"); leg->SetFillColor(0); leg->SetBorderSize(0); leg->SetTextFont(42); leg->SetTextSize(0.04); leg->Draw(); TCanvas* cPull = new TCanvas("pullANN_"+SIGNAL,"pullANN_"+SIGNAL,900,400); frame3->GetXaxis()->SetTitle("ANN Output"); frame3->GetYaxis()->SetTitle("Pull"); frame3->Draw(); cout<<"Creating datacard"<<endl; ofstream datacard; datacard.open("datacard_"+SIGNAL+"_"+HISTO+".txt"); datacard.setf(ios::right); datacard<<"imax 1"<<"\n"; datacard<<"jmax 1"<<"\n"; datacard<<"kmax *"<<"\n"; datacard<<"----------------"<<"\n"; datacard<<"shapes * * "<<SIGNAL+"_"+HISTO+"_input.root $PROCESS $PROCESS_$SYSTEMATIC"<<"\n"; datacard<<"----------------"<<"\n"; datacard<<"bin 1"<<"\n"; datacard<<"observation "<<N<<"\n"; datacard<<"----------------"<<"\n"; datacard<<"bin 1 1"<<"\n"; datacard<<"process signal background "<<"\n"; datacard<<"process 0 1"<<"\n"; datacard<<"rate "<<S<<" "<<B<<"\n"; datacard<<"----------------"<<"\n"; datacard<<"lumi lnN 1.022 1.022"<<"\n"; datacard<<"jes shape 1 1"<<"\n"; datacard<<"mcstat shape 1 1"<<"\n"; datacard<<"jer shape 0 0"<<"\n"; datacard.close(); TFile *out = new TFile(SIGNAL+"_"+HISTO+"_input.root","RECREATE"); out->cd(); hDat->Write("data_obs"); hBkg->Write("background"); hSgn->Write("signal"); hDat_JESlo->Write("data_obs_jesDown"); hBkg_JESlo->Write("background_jesDown"); hBkg_STATlo->Write("background_mcstatDown"); hSgn_STATlo->Write("signal_mcstatDown"); hSgn_JESlo->Write("signal_jesDown"); hDat_JESup->Write("data_obs_jesUp"); hBkg_JESup->Write("background_jesUp"); hBkg_STATup->Write("background_mcstatUp"); hSgn_JESup->Write("signal_jesUp"); hSgn_STATup->Write("signal_mcstatUp"); //----- JER placeholder ---------------- hBkg_JESlo->Write("background_jerDown"); hSgn_JESlo->Write("signal_jerDown"); hBkg_JESup->Write("background_jerUp"); hSgn_JESup->Write("signal_jerUp"); }
histoBook* histoBook::set( string opt, vector<string> params ){ //cout << "Setting : " << opt << endl; //for ( int i = 0; i < params.size(); i++ ){ // cout << params[ i ] << " "; //} //cout << endl; // force the param name to lowercase transform(opt.begin(), opt.end(), opt.begin(), ::tolower); TH1* h = get( styling ); if ( h ){ if ( "title" == opt ){ h->SetTitle( cParam(params, 0) ); } else if ( "x" == opt ){ h->GetXaxis()->SetTitle( cParam(params, 0) ); } else if ( "y" == opt ){ h->GetYaxis()->SetTitle( cParam(params, 0) ); } else if ( "legend" == opt ){ legend->AddEntry( h, cParam(params, 0), cParam(params, 1, "lpf") ); legend->Draw(); } else if ( "draw" == opt ){ drawOption = cParam(params, 0); } else if ( "linecolor" == opt ){ int c = color( cParam( params, 0) ); if ( c < 0 ) c = (int) dParam( params, 0); h->SetLineColor( c ); } else if ( "fillcolor" == opt ){ int c = color( cParam( params, 0) ); if ( c < 0 ) c = (int) dParam( params, 0); h->SetFillColor( c ); } else if ( "linewidth" == opt ){ h->SetLineWidth( dParam( params, 0) ); } else if ( "domain" == opt ){ double min = dParam( params, 0); double max = dParam( params, 1); h->GetXaxis()->SetRangeUser( min, max ); } else if ( "dynamicdomain" == opt ){ double thresh = dParam( params, 0); int min = (int)dParam( params, 1); int max = (int)dParam( params, 2); int axis = (int)dParam( params, 3); // 1 = x, 2 = y if ( 1 != axis && 2 != axis ) axis = 1; if ( thresh >= 0) { if ( -1 >= min ) min = h->FindFirstBinAbove( thresh, axis ); if ( -1 >= max ) max = h->FindLastBinAbove( thresh, axis ); } if ( 1 == axis ) h->GetXaxis()->SetRange( min, max ); else if ( 2 == axis ) h->GetYaxis()->SetRange( min, max ); } else if ( "range" == opt ){ double min = dParam( params, 0); double max = dParam( params, 1); h->GetYaxis()->SetRangeUser( min, max ); } else if ( "markercolor" == opt ) { int c = color( cParam( params, 0) ); if ( c < 0 ) c = (int) dParam( params, 0); h->SetMarkerColor( c ); } else if ( "markerstyle" == opt ) { h->SetMarkerStyle( (int)dParam( params, 0) ); } else if ( "legend" == opt ){ // p1 - alignmentX // p2 - alignmentY // p3 - width // p4 - height // make sure option is valid double p1 = dParam( params, 0); double p2 = dParam( params, 1); if ( !(legendAlignment::center == p1 || legendAlignment::left == p1 || legendAlignment::right == p1) ) p1 = legendAlignment::best; if ( !(legendAlignment::center == p2 || legendAlignment::top == p2 || legendAlignment::bottom == p2) ) p2 = legendAlignment::best; placeLegend( p1, p2, dParam( params, 3), dParam( params, 3) ); } else if ( "numberofticks" == opt ){ // p1 - # of primary divisions // p2 - # of secondary divisions // p3 - axis : 0 or 1 = x, 2 = y double p1 = dParam( params, 0); double p2 = dParam( params, 1); double p3 = dParam( params, 2); if ( p2 == -1 ) p2 = 0; if ( 2 == (int)p3 ) h->GetYaxis()->SetNdivisions( (int) p1, (int) p2, 0, true ); else h->GetXaxis()->SetNdivisions( (int) p1, (int) p2, 0, true ); } else if ( "logy" == opt ){ gPad->SetLogy( (int)dParam( params, 0 ) ); } else if ( "logx" == opt ){ gPad->SetLogx( (int)dParam( params, 0 ) ); } else if ( "logz" == opt ){ gPad->SetLogz( (int)dParam( params, 0 ) ); } } return this; }
histoBook* histoBook::set( string param, double p1, double p2, double p3, double p4 ){ transform(param.begin(), param.end(), param.begin(), ::tolower); TH1* h = get( styling ); if ( h ){ if ( "linecolor" == param ){ h->SetLineColor( (int) p1 ); } else if ( "domain" == param ){ double min = p1; double max = p2; h->GetXaxis()->SetRangeUser( min, max ); } else if ( "dynamicdomain" == param ){ double thresh = p1; int min = (int)p2; int max = (int)p3; int axis = (int)p4; // 1 = x, 2 = y if ( 1 != axis && 2 != axis ) axis = 1; if ( thresh >= 0) { if ( -1 >= min ) min = h->FindFirstBinAbove( thresh, axis ); if ( -1 >= max ) max = h->FindLastBinAbove( thresh, axis ); } if ( 1 == axis ) h->GetXaxis()->SetRange( min, max ); else if ( 2 == axis ) h->GetYaxis()->SetRange( min, max ); } else if ( "range" == param ){ double min = p1; double max = p2; h->GetYaxis()->SetRangeUser( min, max ); } else if ( "markercolor" == param ) { h->SetMarkerColor( (int)p1 ); } else if ( "markerstyle" == param ) { h->SetMarkerStyle( (int)p1 ); } else if ( "legend" == param ){ // p1 - alignmentX // p2 - alignmentY // p3 - width // p4 - height // make sure option is valid if ( !(legendAlignment::center == p1 || legendAlignment::left == p1 || legendAlignment::right == p1) ) p1 = legendAlignment::best; if ( !(legendAlignment::center == p2 || legendAlignment::top == p2 || legendAlignment::bottom == p2) ) p2 = legendAlignment::best; placeLegend( p1, p2, p3, p4 ); } else if ( "numberofticks" == param ){ // p1 - # of primary divisions // p2 - # of secondary divisions // p3 - axis : 0 or 1 = x, 2 = y if ( p2 == -1 ) p2 = 0; if ( 2 == (int)p3 ) h->GetYaxis()->SetNdivisions( (int) p1, (int) p2, 0, true ); else h->GetXaxis()->SetNdivisions( (int) p1, (int) p2, 0, true ); } } return this; }