pair<double,double> bkgEvPerGeV(RooWorkspace *work, int m_hyp, int cat, int spin=false){ RooRealVar *mass = (RooRealVar*)work->var("CMS_hgg_mass"); if (spin) mass = (RooRealVar*)work->var("mass"); mass->setRange(100,180); RooAbsPdf *pdf = (RooAbsPdf*)work->pdf(Form("pdf_data_pol_model_8TeV_cat%d",cat)); RooAbsData *data = (RooDataSet*)work->data(Form("data_mass_cat%d",cat)); RooPlot *tempFrame = mass->frame(); data->plotOn(tempFrame,Binning(80)); pdf->plotOn(tempFrame); RooCurve *curve = (RooCurve*)tempFrame->getObject(tempFrame->numItems()-1); double nombkg = curve->Eval(double(m_hyp)); RooRealVar *nlim = new RooRealVar(Form("nlim%d",cat),"",0.,0.,1.e5); //double lowedge = tempFrame->GetXaxis()->GetBinLowEdge(FindBin(double(m_hyp))); //double upedge = tempFrame->GetXaxis()->GetBinUpEdge(FindBin(double(m_hyp))); //double center = tempFrame->GetXaxis()->GetBinUpCenter(FindBin(double(m_hyp))); nlim->setVal(nombkg); mass->setRange("errRange",m_hyp-0.5,m_hyp+0.5); RooAbsPdf *epdf = 0; epdf = new RooExtendPdf("epdf","",*pdf,*nlim,"errRange"); RooAbsReal *nll = epdf->createNLL(*data,Extended(),NumCPU(4)); RooMinimizer minim(*nll); minim.setStrategy(0); minim.setPrintLevel(-1); minim.migrad(); minim.minos(*nlim); double error = (nlim->getErrorLo(),nlim->getErrorHi())/2.; data->Print(); return pair<double,double>(nombkg,error); }
double getMyNLL(RooRealVar *var, RooAbsPdf *pdf, RooDataHist *data){ RooPlot *plot = var->frame(); data->plotOn(plot); pdf->plotOn(plot); RooCurve *pdfCurve = (RooCurve*)plot->getObject(plot->numItems()-1); double sum=0.; for (int i=0; i<data->numEntries(); i++){ double binCenter = data->get(i)->getRealValue("CMS_hgg_mass"); double weight = data->weight(); sum+=TMath::Log(TMath::Poisson(100.*weight,100.*pdfCurve->Eval(binCenter))); } return -1.*sum; }
void draw_data_mgg(TString folderName,bool blind=true,float min=103,float max=160) { TFile inputFile(folderName+"/data.root"); const int nCat = 5; TString cats[5] = {"HighPt","Hbb","Zbb","HighRes","LowRes"}; TCanvas cv; for(int iCat=0; iCat < nCat; iCat++) { RooWorkspace *ws = (RooWorkspace*)inputFile.Get(cats[iCat]+"_mgg_workspace"); RooFitResult* res = (RooFitResult*)ws->obj("fitresult_pdf_data"); RooRealVar * mass = ws->var("mgg"); mass->setRange("all",min,max); mass->setRange("blind",121,130); mass->setRange("low",106,121); mass->setRange("high",130,160); mass->setUnit("GeV"); mass->SetTitle("m_{#gamma#gamma}"); RooAbsPdf * pdf = ws->pdf("pdf"); RooPlot *plot = mass->frame(min,max,max-min); plot->SetTitle(""); RooAbsData* data = ws->data("data")->reduce(Form("mgg > %f && mgg < %f",min,max)); double nTot = data->sumEntries(); if(blind) data = data->reduce("mgg < 121 || mgg>130"); double nBlind = data->sumEntries(); double norm = nTot/nBlind; //normalization for the plot data->plotOn(plot); pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::Range("Full"),RooFit::LineWidth(0.1) ); plot->Print(); //add the fix error band RooCurve* c = plot->getCurve("pdf_Norm[mgg]_Range[Full]_NormRange[Full]"); const int Nc = c->GetN(); //TGraphErrors errfix(Nc); //TGraphErrors errfix2(Nc); TGraphAsymmErrors errfix(Nc); TGraphAsymmErrors errfix2(Nc); Double_t *x = c->GetX(); Double_t *y = c->GetY(); double NtotalFit = ws->var("Nbkg1")->getVal()*ws->var("Nbkg1")->getVal() + ws->var("Nbkg2")->getVal()*ws->var("Nbkg2")->getVal(); for( int i = 0; i < Nc; i++ ) { errfix.SetPoint(i,x[i],y[i]); errfix2.SetPoint(i,x[i],y[i]); mass->setVal(x[i]); double shapeErr = pdf->getPropagatedError(*res)*NtotalFit; //double totalErr = TMath::Sqrt( shapeErr*shapeErr + y[i] ); //total normalization error double totalErr = TMath::Sqrt( shapeErr*shapeErr + y[i]*y[i]/NtotalFit ); if ( y[i] - totalErr > .0 ) { errfix.SetPointError(i, 0, 0, totalErr, totalErr ); } else { errfix.SetPointError(i, 0, 0, y[i] - 0.01, totalErr ); } //2sigma if ( y[i] - 2.*totalErr > .0 ) { errfix2.SetPointError(i, 0, 0, 2.*totalErr, 2.*totalErr ); } else { errfix2.SetPointError(i, 0, 0, y[i] - 0.01, 2.*totalErr ); } /* std::cout << x[i] << " " << y[i] << " " << " ,pdf get Val: " << pdf->getVal() << " ,pdf get Prop Err: " << pdf->getPropagatedError(*res)*NtotalFit << " stat uncertainty: " << TMath::Sqrt(y[i]) << " Ntot: " << NtotalFit << std::endl; */ } errfix.SetFillColor(kYellow); errfix2.SetFillColor(kGreen); //pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kGreen),RooFit::Range("Full"), RooFit::VisualizeError(*res,2.0,kFALSE)); //pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kYellow),RooFit::Range("Full"), RooFit::VisualizeError(*res,1.0,kFALSE)); //pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kGreen),RooFit::Range("Full"), RooFit::VisualizeError(*res,2.0,kTRUE)); //pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kYellow),RooFit::Range("Full"), RooFit::VisualizeError(*res,1.0,kTRUE)); plot->addObject(&errfix,"4"); plot->addObject(&errfix2,"4"); plot->addObject(&errfix,"4"); data->plotOn(plot); TBox blindBox(121,plot->GetMinimum()-(plot->GetMaximum()-plot->GetMinimum())*0.015,130,plot->GetMaximum()); blindBox.SetFillColor(kGray); if(blind) { plot->addObject(&blindBox); pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kGreen),RooFit::Range("Full"), RooFit::VisualizeError(*res,2.0,kTRUE)); pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::FillColor(kYellow),RooFit::Range("Full"), RooFit::VisualizeError(*res,1.0,kTRUE)); } //plot->addObject(&errfix,"4"); //data->plotOn(plot); //pdf->plotOn(plot,RooFit::Normalization( norm ) ); //pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::Range("Full"),RooFit::LineWidth(1.5) ); pdf->plotOn(plot,RooFit::NormRange( "low,high" ),RooFit::Range("Full"), RooFit::LineWidth(1)); data->plotOn(plot); /* pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::Range("all"),RooFit::LineWidth(0.8) ); //pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::FillColor(kGreen),RooFit::Range("all"), RooFit::VisualizeError(*res,2.0,kFALSE)); //pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::FillColor(kYellow),RooFit::Range("all"), RooFit::VisualizeError(*res,1.0,kFALSE)); pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::FillColor(kGreen),RooFit::Range("all"), RooFit::VisualizeError(*res,2.0,kTRUE)); pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::FillColor(kYellow),RooFit::Range("all"), RooFit::VisualizeError(*res,1.0,kTRUE)); data->plotOn(plot); pdf->plotOn(plot,RooFit::Normalization(norm),RooFit::Range("all"),RooFit::LineWidth(0.8) ); */ TLatex lbl0(0.1,0.96,"CMS Preliminary"); lbl0.SetNDC(); lbl0.SetTextSize(0.042); plot->addObject(&lbl0); TLatex lbl(0.4,0.96,Form("%s Box",cats[iCat].Data())); lbl.SetNDC(); lbl.SetTextSize(0.042); plot->addObject(&lbl); TLatex lbl2(0.6,0.96,"#sqrt{s}=8 TeV L = 19.78 fb^{-1}"); lbl2.SetNDC(); lbl2.SetTextSize(0.042); plot->addObject(&lbl2); int iObj=-1; TNamed *obj; while( (obj = (TNamed*)plot->getObject(++iObj)) ) { obj->SetName(Form("Object_%d",iObj)); } plot->Draw(); TString tag = (blind ? "_BLIND" : ""); cv.SaveAs(folderName+"/figs/mgg_data_"+cats[iCat]+tag+TString(Form("_%0.0f_%0.0f",min,max))+".png"); cv.SaveAs(folderName+"/figs/mgg_data_"+cats[iCat]+tag+TString(Form("_%0.0f_%0.0f",min,max))+".pdf"); cv.SaveAs(folderName+"/figs/mgg_data_"+cats[iCat]+tag+TString(Form("_%0.0f_%0.0f",min,max))+".C"); } }
void checkGoodness(TString scenario, TH1D* dummyThis,TString varType, TString shapeComb, TString INPUTDIR_PREFIX, TString SCEN_TRIG) { cout << "Inside the checkGoodness we have " << scenario << " histo " << dummyThis->GetName() << " " << varType << " " << shapeComb << " " << INPUTDIR_PREFIX << " " << SCEN_TRIG << endl; //for each bin, for each type get 3 histos TH1D* hpulls_sig_pass = (TH1D*)dummyThis->Clone(); hpulls_sig_pass->SetName("hpulls_sig_pass"); TH1D* hpulls_sig_fail = (TH1D*)dummyThis->Clone(); hpulls_sig_fail->SetName("hpulls_sig_fail"); //TH1D* hpulls_bkg_pass = (TH1D*)dummyThis->Clone(); //hpulls_bkg_pass->SetName("hpulls_bkg_pass"); //TH1D* hpulls_bkg_fail = (TH1D*)dummyThis->Clone(); //hpulls_bkg_fail->SetName("hpulls_bkg_fail"); for (int ibin = 0; ibin < getNbin(varType)-1; ibin++) { //int to string std::ostringstream pprint; pprint.str(""); pprint<<ibin; string bin = pprint.str(); //for each bin for each type cout << "get data from canvas" << endl; RooHist* thisDataPass = getFitHist(INPUTDIR_PREFIX, scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb, "1",TString(bin), "data"); RooCurve* thisSignalPass = getFitCurve(INPUTDIR_PREFIX,scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb, "1",TString(bin), "sig"); RooCurve* thisBackgroundPass = getFitCurve(INPUTDIR_PREFIX,scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb,"1",TString(bin), "bkg"); RooHist* thisDataFail = getFitHist(INPUTDIR_PREFIX, scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb,"2",TString(bin), "data"); RooCurve* thisSignalFail = getFitCurve(INPUTDIR_PREFIX,scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb,"2",TString(bin), "sig"); RooCurve* thisBackgroundFail = getFitCurve(INPUTDIR_PREFIX,scenario, "datalike_mc", varType, SCEN_TRIG,shapeComb,"2",TString(bin), "bkg"); // RooCurve* thisBackgroundFail = getFitCurve(INPUTDIR_PREFIX,scenario, "datalike_mc", varType, SCEN_TRIG,getShapeUtility(shapeComb, ibin,"datalike_mc"),"2",TString(bin), "bkg"); //Plot and save //RooPlot* ctmp_pass = getFitPlot(INPUTDIR_PREFIX, scenario, "datalike_mc", varType, SCEN_TRIG,getShapeUtility(shapeComb, ibin,"datalike_mc"),"1",TString(bin), "data"); //new RooPlot(); TCanvas* ctmp_pass = new TCanvas("PASS"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc"),"PASS"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc")); ctmp_pass->cd(); //ctmp_pass->Draw(); thisDataPass->Draw("ap"); thisSignalPass->Draw("same"); thisBackgroundPass->Draw("same"); ctmp_pass->SaveAs(INPUTDIR_PREFIX+"/PASS"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc")+".png"); TCanvas* ctmp_fail = new TCanvas("FAIL"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc"),"FAIL"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc")); //RooPlot* ctmp_fail = getFitPlot(INPUTDIR_PREFIX, scenario, "datalike_mc", varType, SCEN_TRIG,getShapeUtility(shapeComb, ibin,"datalike_mc"),"1",TString(bin), "data"); ctmp_fail->cd(); thisDataFail->Draw("ap"); thisSignalFail->Draw("same"); thisBackgroundFail->Draw("same"); ctmp_fail->SaveAs(INPUTDIR_PREFIX+"/FAIL"+TString(bin)+"_view_"+getShapeUtility(shapeComb, ibin,"datalike_mc")+".png"); //calculate and draw pulls //PASS TCanvas* cpull_pass_sig_tmp = new TCanvas("pulls_"+TString(bin)+"_PASS_SIG_"+getShapeUtility(shapeComb, ibin,"datalike_mc"),"pulls_"+TString(bin)+"_PASS_SIG_"+getShapeUtility(shapeComb, ibin,"datalike_mc")); cpull_pass_sig_tmp->cd(); thisDataPass->makeResidHist(*thisSignalPass,kTRUE)->Draw("ap"); cpull_pass_sig_tmp->SaveAs(INPUTDIR_PREFIX+"/SIG"+TString(bin)+"_PULL_PASS_"+getShapeUtility(shapeComb, ibin,"datalike_mc")+".png"); //get mean pulls for a given canvas hpulls_sig_pass->SetBinContent(ibin+1,fabs(getRooMean(thisDataPass->makeResidHist(*thisSignalPass,kTRUE)))); hpulls_sig_pass->SetBinError(ibin+1,0.000001); /* TCanvas* cpull_pass_bkg_tmp = new TCanvas("pulls_"+TString(bin)+"_PASS_BKG_"+getShapeUtility(shapeComb, ibin,"datalike_mc","pulls_"+TString(bin)+"_PASS_BKG_"+getShapeUtility(shapeComb, ibin,"datalike_mc"); cpull_pass_bkg_tmp->cd(); thisDataPass->makeResidHist(*thisBackgroundPass,kTRUE)->Draw("ap"); cpull_pass_bkg_tmp->SaveAs("BKG"+TString(bin)+"_PULL_PASS_"+getShapeUtility(shapeComb, ibin,"datalike_mc"+".png"); //get mean pulls for a given canvas hpulls_bkg_pass->SetBinContent(ibin+1,getRooMean(thisDataPass->makeResidHist(*thisBackgroundPass,kTRUE))); hpulls_bkg_pass->SetBinError(ibin+1,0.000001); */ //FAIL TCanvas* cpull_fail_sig_tmp = new TCanvas("pulls_"+TString(bin)+"_FAIL_SIG_"+getShapeUtility(shapeComb, ibin,"datalike_mc"),"pulls_"+TString(bin)+"_FAIL_SIG_"+getShapeUtility(shapeComb, ibin,"datalike_mc")); cpull_fail_sig_tmp->cd(); thisDataFail->makeResidHist(*thisSignalFail,kTRUE)->Draw("ap"); cpull_fail_sig_tmp->SaveAs( INPUTDIR_PREFIX+"/SIG"+TString(bin)+"_PULL_FAIL_"+getShapeUtility(shapeComb, ibin,"datalike_mc")+".png"); cout << "get mean pulls for a given canvas" << endl; hpulls_sig_fail->SetBinContent(ibin+1,fabs(getRooMean(thisDataFail->makeResidHist(*thisSignalFail,kTRUE)))); hpulls_sig_fail->SetBinError(ibin+1,0.000001); /* TCanvas* cpull_fail_bkg_tmp = new TCanvas("pulls_"+TString(bin)+"_FAIL_BKG_"+getShapeUtility(shapeComb, ibin,"datalike_mc","pulls_"+TString(bin)+"_FAIL_BKG_"+getShapeUtility(shapeComb, ibin)); cpull_fail_bkg_tmp->cd(); thisDataFail->makeResidHist(*thisBackgroundFail,kTRUE)->Draw("ap"); cpull_fail_bkg_tmp->SaveAs("BKG"+TString(bin)+"_PULL_FAIL_"+getShapeUtility(shapeComb, ibin,"datalike_mc"+".png"); cout << "get mean pulls for a given canvas" << endl; hpulls_bkg_fail->SetBinContent(ibin+1,getRooMean(thisDataFail->makeResidHist(*thisBackgroundFail,kTRUE))); hpulls_bkg_fail->SetBinError(ibin+1,0.000001); */ } cout << "OUT PULLS FILL LOOP XXX" << endl; //PLOT pull means distributions TCanvas* cpullf_sig = new TCanvas("SIG"+scenario+varType,"SIG"+scenario+varType); cpullf_sig->cd(); cpullf_sig->SetLogx(); cpullf_sig->SetLogy(); hpulls_sig_pass->SetMarkerStyle(22); hpulls_sig_pass->SetMarkerSize(1.1); hpulls_sig_pass->SetMarkerColor(kBlack); hpulls_sig_pass->Draw("P"); hpulls_sig_fail->SetMarkerStyle(24); hpulls_sig_fail->SetMarkerSize(1.1); hpulls_sig_fail->SetMarkerColor(kRed); hpulls_sig_fail->Draw("Psame"); cpullf_sig->SaveAs(INPUTDIR_PREFIX+"/SIG"+scenario+varType+".png"); /* TCanvas* cpullf_bkg = new TCanvas("BKG"+scenario+varType,"BKG"+scenario+varType); cpullf_bkg->cd(); hpulls_bkg_pass->SetMarkerStyle(22); hpulls_bkg_pass->SetMarkerSize(1.1); hpulls_bkg_pass->SetMarkerColor(kBlack); hpulls_bkg_pass->Draw("P"); hpulls_bkg_fail->SetMarkerStyle(24); hpulls_bkg_fail->SetMarkerSize(1.1); hpulls_bkg_fail->SetMarkerColor(kRed); hpulls_bkg_fail->Draw("Psame"); cpullf_bkg->SaveAs("BKG"+scenario+varType+".png"); */ }
void fitbkgdataCard(TString configCard="template.config", bool dobands = true, // create baerror bands for BG models bool dosignal = false, // plot the signal model (needs to be present) bool blinded = true, // blind the data in the plots? bool verbose = true ) { gROOT->Macro("MitStyle.C"); gStyle->SetErrorX(0); gStyle->SetOptStat(0); gROOT->ForceStyle(); TString projectDir; std::vector<TString> catdesc; std::vector<TString> catnames; std::vector<int> polorder; double massmin = -1.; double massmax = -1.; double theCMenergy = -1.; bool readStatus = readFromConfigCard( configCard, projectDir, catnames, catdesc, polorder, massmin, massmax, theCMenergy ); if( !readStatus ) { std::cerr<<" ERROR: Could not read from card > "<<configCard.Data()<<" <."<<std::endl; return; } TFile *fdata = new TFile(TString::Format("%s/CMS-HGG-data.root",projectDir.Data()),"READ"); if( !fdata ) { std::cerr<<" ERROR: Could not open file "<<projectDir.Data()<<"/CMS-HGG-data.root."<<std::endl; return; } if( !gSystem->cd(TString::Format("%s/databkg/",projectDir.Data())) ) { std::cerr<<" ERROR: Could not change directory to "<<TString::Format("%s/databkg/",projectDir.Data()).Data()<<"."<<std::endl; return; } // ---------------------------------------------------------------------- // load the input workspace.... RooWorkspace* win = (RooWorkspace*)fdata->Get("cms_hgg_workspace_data"); if( !win ) { std::cerr<<" ERROR: Could not load workspace > cms_hgg_workspace_data < from file > "<<TString::Format("%s/CMS-HGG-data.root",projectDir.Data()).Data()<<" <."<<std::endl; return; } RooRealVar *intLumi = win->var("IntLumi"); RooRealVar *hmass = win->var("CMS_hgg_mass"); if( !intLumi || !hmass ) { std::cerr<<" ERROR: Could not load needed variables > IntLumi < or > CMS_hgg_mass < forom input workspace."<<std::endl; return; } //win->Print(); hmass->setRange(massmin,massmax); hmass->setBins(4*(int)(massmax-massmin)); hmass->SetTitle("m_{#gamma#gamma}"); hmass->setUnit("GeV"); hmass->setRange("fitrange",massmin,massmax); hmass->setRange("blind1",100.,110.); hmass->setRange("blind2",150.,180.); // ---------------------------------------------------------------------- // some auxiliray vectro (don't know the meaning of all of them ... yet... std::vector<RooAbsData*> data_vec; std::vector<RooAbsPdf*> pdfShape_vec; // vector to store the NOT-EXTENDED PDFs (aka pdfshape) std::vector<RooAbsPdf*> pdf_vec; // vector to store the EXTENDED PDFs std::vector<RooAbsReal*> normu_vec; // this holds the normalization vars for each Cat (needed in bands for combined cat) RooArgList normList; // list of range-limityed normalizations (needed for error bands on combined category) //std::vector<RooRealVar*> coeffv; //std::vector<RooAbsReal*> normu_vecv; // ??? // ---------------------------------------------------------------------- // define output works RooWorkspace *wOut = new RooWorkspace("wbkg","wbkg") ; // util;ities for the combined fit RooCategory finalcat ("finalcat", "finalcat") ; RooSimultaneous fullbkgpdf("fullbkgpdf","fullbkgpdf",finalcat); RooDataSet datacomb ("datacomb", "datacomb", RooArgList(*hmass,finalcat)) ; RooDataSet *datacombcat = new RooDataSet("data_combcat","",RooArgList(*hmass)) ; // add the 'combcat' to the list...if more than one cat if( catnames.size() > 1 ) { catnames.push_back("combcat"); catdesc.push_back("Combined"); } for (UInt_t icat=0; icat<catnames.size(); ++icat) { TString catname = catnames.at(icat); finalcat.defineType(catname); // check if we're in a sub-cat or the comb-cat RooDataSet *data = NULL; RooDataSet *inData = NULL; if( icat < (catnames.size() - 1) || catnames.size() == 1) { // this is NOT the last cat (which is by construction the combination) inData = (RooDataSet*)win->data(TString("data_mass_")+catname); if( !inData ) { std::cerr<<" ERROR: Could not find dataset > data_mass_"<<catname.Data()<<" < in input workspace."<<std::endl; return; } data = new RooDataSet(TString("data_")+catname,"",*hmass,Import(*inData)); // copy the dataset (why?) // append the data to the combined data... RooDataSet *datacat = new RooDataSet(TString("datacat")+catname,"",*hmass,Index(finalcat),Import(catname,*data)) ; datacomb.append(*datacat); datacombcat->append(*data); // normalization for this category RooRealVar *nbkg = new RooRealVar(TString::Format("CMS_hgg_%s_bkgshape_norm",catname.Data()),"",800.0,0.0,25e3); // we keep track of the normalizario vars only for N-1 cats, naming convetnions hystoric... if( catnames.size() > 2 && icat < (catnames.size() - 2) ) { RooRealVar* cbkg = new RooRealVar(TString::Format("cbkg%s",catname.Data()),"",0.0,0.0,1e3); cbkg->removeRange(); normu_vec.push_back(cbkg); normList.add(*cbkg); } /// generate the Bernstrin polynomial (FIX-ME: add possibility ro create other models...) fstBernModel* theBGmodel = new fstBernModel(hmass, polorder[icat], icat, catname); // using my dedicated class... std::cout<<" model name is "<<theBGmodel->getPdf()->GetName()<<std::endl; RooAbsPdf* bkgshape = theBGmodel->getPdf(); // the BG shape RooAbsPdf* bkgpdf = new RooExtendPdf(TString("bkgpdf")+catname,"",*bkgshape,*nbkg); // the extended PDF // add the extedned PDF to the RooSimultaneous holding all models... fullbkgpdf.addPdf(*bkgpdf,catname); // store the NON-EXTENDED PDF for usgae to compute the error bands later.. pdfShape_vec.push_back(bkgshape); pdf_vec .push_back(bkgpdf); data_vec .push_back(data); } else { data = datacombcat; // we're looking at the last cat (by construction the combination) data_vec.push_back(data); // sum up all the cts PDFs for combined PDF RooArgList subpdfs; for (int ipdf=0; ipdf<pdf_vec.size(); ++ipdf) { subpdfs.add(*pdf_vec.at(ipdf)); } RooAddPdf* bkgpdf = new RooAddPdf(TString("bkgpdf")+catname,"",subpdfs); pdfShape_vec.push_back(bkgpdf); pdf_vec .push_back(bkgpdf); // I don't think this is really needed though.... } // generate the binned dataset (to be put into the workspace... just in case...) RooDataHist *databinned = new RooDataHist(TString("databinned_")+catname,"",*hmass,*data); wOut->import(*data); wOut->import(*databinned); } std::cout<<" ***************** "<<std::endl; // fit the RooSimultaneous to the combined dataset -> (we could also fit each cat separately) fullbkgpdf.fitTo(datacomb,Strategy(1),Minos(kFALSE),Save(kTRUE)); RooFitResult *fullbkgfitres = fullbkgpdf.fitTo(datacomb,Strategy(2),Minos(kFALSE),Save(kTRUE)); // in principle we're done now, so store the results in the output workspace wOut->import(datacomb); wOut->import(fullbkgpdf); wOut->import(*fullbkgfitres); std::cout<<" ***************** "<<std::endl; if( verbose ) wOut->Print(); std::cout<<" ***************** "<<std::endl; wOut->writeToFile("bkgdatawithfit.root") ; if( verbose ) { printf("IntLumi = %5f\n",intLumi->getVal()); printf("ndata:\n"); for (UInt_t icat=0; icat<catnames.size(); ++icat) { printf("%i ",data_vec.at(icat)->numEntries()); } printf("\n"); } // -------------------------------------------------------------------------------------------- // Now comesd the plotting // chage the Statistics style... gStyle->SetOptStat(1110); // we want to plot in 1GeV bins (apparently...) UInt_t nbins = (UInt_t) (massmax-massmin); // here we'll store the curves for the bands... std::vector<RooCurve*> fitcurves; // loop again over the cats TCanvas **canbkg = new TCanvas*[catnames.size()]; RooPlot** plot = new RooPlot*[catnames.size()]; TLatex** lat = new TLatex*[catnames.size()]; TLatex** lat2 = new TLatex*[catnames.size()]; std::cout<<" beofre plotting..."<<std::endl; for (UInt_t icat=0; icat<catnames.size(); ++icat) { TString catname = catnames.at(icat); std::cout<<" trying to plot #"<<icat<<std::endl; // plot the data and the fit canbkg[icat] = new TCanvas; plot [icat] = hmass->frame(Bins(nbins),Range("fitrange")); std::cout<<" trying to plot #"<<icat<<std::endl; // first plot the data invisibly... and put the fitted BG model on top... data_vec .at(icat)->plotOn(plot[icat],RooFit::LineColor(kWhite),MarkerColor(kWhite),Invisible()); pdfShape_vec.at(icat)->plotOn(plot[icat],RooFit::LineColor(kRed),Range("fitrange"),NormRange("fitrange")); std::cout<<" trying to plot #"<<icat<<std::endl; // if toggled on, plot also the Data visibly if( !blinded ) { data_vec.at(icat)->plotOn(plot[icat]); } std::cout<<" trying to plot #"<<icat<<std::endl; // some cosmetics... plot[icat]->SetTitle(""); plot[icat]->SetMinimum(0.0); plot[icat]->SetMaximum(1.40*plot[icat]->GetMaximum()); plot[icat]->GetXaxis()->SetTitle("m_{#gamma#gamma} (GeV/c^{2})"); plot[icat]->Draw(); std::cout<<" trying to plot #"<<icat<<std::endl; // legend.... TLegend *legmc = new TLegend(0.68,0.70,0.97,0.90); legmc->AddEntry(plot[icat]->getObject(2),"Data","LPE"); legmc->AddEntry(plot[icat]->getObject(1),"Bkg Model","L"); // this part computes the 1/2-sigma bands. TGraphAsymmErrors *onesigma = NULL; TGraphAsymmErrors *twosigma = NULL; std::cout<<" trying *** to plot #"<<icat<<std::endl; RooAddition* sumcatsnm1 = NULL; if ( dobands ) { //&& icat == (catnames.size() - 1) ) { onesigma = new TGraphAsymmErrors(); twosigma = new TGraphAsymmErrors(); // get the PDF for this cat from the vector RooAbsPdf *thisPdf = pdfShape_vec.at(icat); // get the nominal fir curve RooCurve *nomcurve = dynamic_cast<RooCurve*>(plot[icat]->getObject(1)); fitcurves.push_back(nomcurve); bool iscombcat = ( icat == (catnames.size() - 1) && catnames.size() > 1); RooAbsData *datanorm = ( iscombcat ? &datacomb : data_vec.at(icat) ); // this si the nornmalization in the 'sliding-window' (i.e. per 'test-bin') RooRealVar *nlim = new RooRealVar(TString::Format("nlim%s",catnames.at(icat).Data()),"",0.0,0.0,10.0); nlim->removeRange(); if( iscombcat ) { // ----------- HISTORIC NAMING ---------------------------------------- sumcatsnm1 = new RooAddition("sumcatsnm1","",normList); // summing all normalizations epect the last Cat // this is the normlization of the last Cat RooFormulaVar *nlast = new RooFormulaVar("nlast","","TMath::Max(0.1,@0-@1)",RooArgList(*nlim,*sumcatsnm1)); // ... and adding it ot the list of norms normu_vec.push_back(nlast); } //if (icat == 1 && catnames.size() == 2) continue; // only 1 cat, so don't need combination for (int i=1; i<(plot[icat]->GetXaxis()->GetNbins()+1); ++i) { // this defines the 'binning' we use for the error bands double lowedge = plot[icat]->GetXaxis()->GetBinLowEdge(i); double upedge = plot[icat]->GetXaxis()->GetBinUpEdge(i); double center = plot[icat]->GetXaxis()->GetBinCenter(i); // get the nominal value at the center of the bin double nombkg = nomcurve->interpolate(center); nlim->setVal(nombkg); hmass->setRange("errRange",lowedge,upedge); // this is the new extended PDF whith the normalization restricted to the bin-area RooAbsPdf *extLimPdf = NULL; if( iscombcat ) { extLimPdf = new RooSimultaneous("epdf","",finalcat); // loop over the cats and generate temporary extended PDFs for (int jcat=0; jcat<(catnames.size()-1); ++jcat) { RooRealVar *rvar = dynamic_cast<RooRealVar*>(normu_vec.at(jcat)); if (rvar) rvar->setVal(fitcurves.at(jcat)->interpolate(center)); RooExtendPdf *ecpdf = new RooExtendPdf(TString::Format("ecpdf%s",catnames.at(jcat).Data()),"",*pdfShape_vec.at(jcat),*normu_vec.at(jcat),"errRange"); static_cast<RooSimultaneous*>(extLimPdf)->addPdf(*ecpdf,catnames.at(jcat)); } } else extLimPdf = new RooExtendPdf("extLimPdf","",*thisPdf,*nlim,"errRange"); RooAbsReal *nll = extLimPdf->createNLL(*datanorm,Extended(),NumCPU(1)); RooMinimizer minim(*nll); minim.setStrategy(0); double clone = 1.0 - 2.0*RooStats::SignificanceToPValue(1.0); double cltwo = 1.0 - 2.0*RooStats::SignificanceToPValue(2.0); if (iscombcat) minim.setStrategy(2); minim.migrad(); if (!iscombcat) { minim.minos(*nlim); } else { minim.hesse(); nlim->removeAsymError(); } if( verbose ) printf("errlo = %5f, errhi = %5f\n",nlim->getErrorLo(),nlim->getErrorHi()); onesigma->SetPoint(i-1,center,nombkg); onesigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi()); // to get the 2-sigma bands... minim.setErrorLevel(0.5*pow(ROOT::Math::normal_quantile(1-0.5*(1-cltwo),1.0), 2)); // the 0.5 is because qmu is -2*NLL // eventually if cl = 0.95 this is the usual 1.92! if (!iscombcat) { minim.migrad(); minim.minos(*nlim); } else { nlim->setError(2.0*nlim->getError()); nlim->removeAsymError(); } twosigma->SetPoint(i-1,center,nombkg); twosigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi()); // for memory clean-up delete nll; delete extLimPdf; } hmass->setRange("errRange",massmin,massmax); if( verbose ) onesigma->Print("V"); // plot[icat] the error bands twosigma->SetLineColor(kGreen); twosigma->SetFillColor(kGreen); twosigma->SetMarkerColor(kGreen); twosigma->Draw("L3 SAME"); onesigma->SetLineColor(kYellow); onesigma->SetFillColor(kYellow); onesigma->SetMarkerColor(kYellow); onesigma->Draw("L3 SAME"); plot[icat]->Draw("SAME"); // and add the error bands to the legend legmc->AddEntry(onesigma,"#pm1 #sigma","F"); legmc->AddEntry(twosigma,"#pm2 #sigma","F"); } std::cout<<" trying ***2 to plot #"<<icat<<std::endl; // rest of the legend .... legmc->SetBorderSize(0); legmc->SetFillStyle(0); legmc->Draw(); lat[icat] = new TLatex(103.0,0.9*plot[icat]->GetMaximum(),TString::Format("#scale[0.7]{#splitline{CMS preliminary}{#sqrt{s} = %.1f TeV L = %.2f fb^{-1}}}",theCMenergy,intLumi->getVal())); lat2[icat] = new TLatex(103.0,0.75*plot[icat]->GetMaximum(),catdesc.at(icat)); lat[icat] ->Draw(); lat2[icat]->Draw(); // ------------------------------------------------------- // save canvas in different formats canbkg[icat]->SaveAs(TString("databkg") + catname + TString(".pdf")); canbkg[icat]->SaveAs(TString("databkg") + catname + TString(".eps")); canbkg[icat]->SaveAs(TString("databkg") + catname + TString(".root")); } return; }
void grabDataSubtractedHistograms(int nJet, int massRange) { TFile f("Histograms_data_and_template.root", "update"); // Figure out the plot directory name first TString dir2jet = "./plots_10172011_2jetsample"; TString dir3jet = "./plots_10172011_3jetsample"; TString dirName = ""; if(nJet==2) dirName = dir2jet; if(nJet==3) dirName = dir3jet; TString massStr = ""; if(nJet==2 && massRange==1) massStr = "150-230"; else if(nJet==2 && massRange==2) massStr = "200-400"; else if(nJet==2 && massRange==3) massStr = "360-500"; else if(nJet==2 && massRange==4) massStr = "450-800"; else if(nJet==3 && massRange==1) massStr = "150-230"; else if(nJet==3 && massRange==2) massStr = "200-400"; else if(nJet==3 && massRange==3) massStr = "360-800"; TString ConnectorStr = ""; if( !(massStr=="") ) ConnectorStr = "-"; TString fitFileName = TString("mLnuJJ-") + massStr + ConnectorStr + TString("combined-fit"); TFile* fitFile = new TFile( dirName + TString("/") + fitFileName+".root", "read"); TCanvas* fitCan = (TCanvas*) fitFile->Get( fitFileName ); RooHist* data = (RooHist*) fitCan->FindObject( "h_data" ); RooCurve* fit = (RooCurve*) fitCan->FindObject( "h_total" ); RooCurve* fit_wjj = (RooCurve*) fitCan->FindObject( "h_Wjets" ); RooCurve* fit_diboson = (RooCurve*) fitCan->FindObject( "h_diboson" ); RooCurve* fit_Top = (RooCurve*) fitCan->FindObject( "h_Top" ); RooCurve* fit_QCD = (RooCurve*) fitCan->FindObject( "h_QCD" ); RooCurve* fit_Zjets = (RooCurve*) fitCan->FindObject( "h_Zjets" ); TFile* systFileUp = new TFile( dirName + TString("SystUp/") + fitFileName+".root", "read"); TCanvas* systFileUpCan = (TCanvas*) systFileUp->Get( fitFileName ); RooCurve* fit_wjj_systUp = (RooCurve*) systFileUpCan->FindObject( "h_Wjets" ); RooCurve* fit_systUp = (RooCurve*) systFileUpCan->FindObject( "h_total" ); TFile* systFileDown = new TFile( dirName + TString("SystDown/") + fitFileName+".root", "read"); TCanvas* systFileDownCan = (TCanvas*) systFileDown->Get( fitFileName ); RooCurve* fit_wjj_systDown = (RooCurve*) systFileDownCan->FindObject( "h_Wjets" ); RooCurve* fit_systDown = (RooCurve*) systFileDownCan->FindObject("h_total"); ///// Now save everything /////////////// TString outPrefix = TString("2jet_MassRange_") + massStr + TString("_"); if(nJet==3) outPrefix = TString("3jet_MassRange_") + massStr + TString("_"); data->SetName( outPrefix+"hist_data" ); fit->SetName( outPrefix+"curve_fitTotal" ); fit_wjj->SetName( outPrefix+"curve_WJets" ); fit_diboson->SetName( outPrefix+"curve_diboson" ); fit_Top->SetName( outPrefix+"curve_Top" ); fit_QCD->SetName( outPrefix+"curve_QCD" ); fit_Zjets->SetName( outPrefix+"curve_Zjets" ); fit_wjj_systUp->SetName( outPrefix+"curve_WJets_SystUp" ); fit_wjj_systDown->SetName( outPrefix+"curve_WJets_SystDown" ); fit_systUp->SetName( outPrefix+"curve_fitTotal_SystUp" ); fit_systDown->SetName( outPrefix+"curve_fitTotal_SystDown" ); f.cd(); data->Write(); fit->Write(); fit_wjj->Write(); fit_diboson->Write(); fit_Top->Write(); fit_QCD->Write(); fit_Zjets->Write(); fit_wjj_systUp->Write(); fit_wjj_systDown->Write(); fit_systUp->Write(); fit_systDown->Write(); f.Close(); delete fitFile; delete systFileUp; delete systFileDown; }
int main() { float min_logL1 = 5986.94; float min_logL0 = 5987.16; string filepath="FINAL_RESULT_AB.root_RESULT__RESULT"; filepath="/shome/buchmann/KillerKoala/CBAF/Development/exchange/RooFit__WorkSpace__Exchange_201417_175622__RNSG_46692.4.root__RESULT__RESULT"; // final MCwS filepath="/shome/buchmann/KillerKoala/CBAF/Development/exchange/RooFit__WorkSpace__Exchange_201417_175622__RNSG_46692.4.root__RESULT__RESULT"; filepath="/shome/buchmann/KillerKoala/CBAF/Development/exchange/RooFit__WorkSpace__Exchange_201417_141126__RNSG_97048.1.root__RESULT__RESULT"; // ************************************************************************************* setlumi(PlottingSetup::luminosity); setessentialcut(PlottingSetup::essential); // this sets the essential cut; this one is used in the draw command so it is AUTOMATICALLY applied everywhere. IMPORTANT: Do NOT store weights here! stringstream resultsummary; // write_analysis_type(PlottingSetup::RestrictToMassPeak,PlottingSetup::DoBTag); do_png(true); do_pdf(true); do_eps(false); do_C(true); do_root(false); PlottingSetup::directoryname = "pValuePlot"; gROOT->SetStyle("Plain"); bool do_fat_line = false; // if you want to have HistLineWidth=1 and FuncWidth=1 as it was before instead of 2 setTDRStyle(do_fat_line); gStyle->SetTextFont(42); bool showList = true; set_directory(PlottingSetup::directoryname); // Indicate the directory name where you'd like to save the output files in Setup.C set_treename("events"); // you can set the treename here to be used; options are "events" (for reco) for "PFevents" (for particle flow) TFile *f = new TFile(filepath.c_str()); if(f->IsZombie()) { cout << "Seems to be a zombie. goodbye." << endl; return -1; } RooWorkspace *wa = (RooWorkspace*)f->Get("transferSpace"); RooPlot *plot = (RooPlot*) wa->obj("frame_mlledge_109fde50"); // cout << plot << endl; wa->Print("v"); TCanvas *can = new TCanvas("can","can"); cout << "Address of plot : " << plot << endl; // plot->Draw(); float pVal_mllmin=35; float pVal_mllmax=90; int is_data=PlottingSetup::data; vector < std::pair < float, float> > loglikelihoods; string function=""; for(int i=0; i< plot->numItems();i++){ string name = plot->getObject(i)->GetName(); if (plot->getObject(i)->IsA()->InheritsFrom( "RooCurve" ))function=name; } RooCurve* curve = (RooCurve*) plot->findObject(function.c_str(),RooCurve::Class()) ; if (!curve) { dout << "RooPlot::residHist(" << plot->GetName() << ") cannot find curve" << endl ; return 0 ; } int iMinimum=0; float min=1e7; for(int i=0;i<curve->GetN();i++) { double x,y; curve->GetPoint(i,x,y); if(y<min & y>=0) { min=y; iMinimum=i; } } double x,y; curve->GetPoint(iMinimum,x,y); cout << "Minimum is at " << x << " : " << y << endl; loglikelihoods.push_back(make_pair(x,y+min_logL1)); //move right starting from the minimum for(int i=iMinimum+1;i<curve->GetN();i++) { float yold=y; curve->GetPoint(i,x,y); //if(abs((y-yold)/yold)>0.5) continue; loglikelihoods.push_back(make_pair(x,y+min_logL1)); } /* for(int i=0;i<curve->GetN();i++) { double x,y; curve->GetPoint(i,x,y); loglikelihoods.push_back(make_pair(x,y+min_logL1)); }*/ cout << "The whole thing contains " << loglikelihoods.size() << " points " << endl; ProduceSignificancePlots(min_logL0, loglikelihoods, pVal_mllmin, pVal_mllmax, is_data, "", ""); can->SaveAs("Crap.png"); delete can; delete plot; delete wa; f->Close(); return 0; }
void grabDataSubtractedHistograms() { TFile f("Histograms_Mjj_data_and_template.root", "update"); TFile* fitFile = new TFile( plots_dir + "/mJJ-combined-fit.root", "read"); TCanvas* fitCan = (TCanvas*) fitFile->Get( "mJJ-combined-fit" ); RooHist* data = (RooHist*) fitCan->FindObject( "h_data" ); RooCurve* fit_total = (RooCurve*) fitCan->FindObject( "h_total" ); RooCurve* fit_diboson = (RooCurve*) fitCan->FindObject( "h_diboson" ); RooCurve* fit_Wjets = (RooCurve*) fitCan->FindObject( "h_Wjets" ); RooCurve* fit_Top = (RooCurve*) fitCan->FindObject( "h_Top" ); RooCurve* fit_QCD = (RooCurve*) fitCan->FindObject( "h_QCD" ); RooCurve* fit_Zjets = (RooCurve*) fitCan->FindObject( "h_Zjets" ); // RooCurve* fit_Ztautau = (RooCurve*) fitCan->FindObject( "h_Ztautau" ); TFile* subtrFile = new TFile( plots_dir + "/mJJ-combined-fit-subtracted.root", "read"); TCanvas* subtrCan = (TCanvas*) subtrFile->Get( "mJJ-combined-fit-subtracted" ); RooHist* subtrHist = (RooHist*) subtrCan->FindObject( "resid_h_data_h_Background" ); RooCurve* Diboson = (RooCurve*) subtrCan->FindObject( "h_diboson" ); // TFile* fitFile1 = new TFile( plots_dir + "/mJJ-mu-fit.root", "read"); // TCanvas* fitCan1 = (TCanvas*) fitFile1->Get( "mJJ-mu-fit" ); // RooHist* data1 = (RooHist*) fitCan1->FindObject( "h_data" ); // RooCurve* fit1 = (RooCurve*) fitCan1->FindObject( "h_total" ); // TFile* subtrFile1 = new TFile( plots_dir + "/mJJ-mu-fit-subtracted.root", "read"); // TCanvas* subtrCan1 = (TCanvas*) subtrFile1->Get( "mJJ-mu-fit-subtracted" ); // RooHist* subtrHist1 = (RooHist*) subtrCan1->FindObject( "resid_h_data_h_Background" ); // RooCurve* Diboson1 = (RooCurve*) subtrCan1->FindObject( "h_diboson" ); // TFile* fitFile2 = new TFile( plots_dir + "/mJJ-ele-fit.root", "read"); // TCanvas* fitCan2 = (TCanvas*) fitFile2->Get( "mJJ-ele-fit" ); // RooHist* data2 = (RooHist*) fitCan2->FindObject( "h_data" ); // RooCurve* fit2 = (RooCurve*) fitCan2->FindObject( "h_total" ); // TFile* subtrFile2 = new TFile( plots_dir + "/mJJ-ele-fit-subtracted.root", "read"); // TCanvas* subtrCan2 = (TCanvas*) subtrFile2->Get( "mJJ-ele-fit-subtracted" ); // RooHist* subtrHist2 = (RooHist*) subtrCan2->FindObject( "resid_h_data_h_Background" ); // RooCurve* Diboson2 = (RooCurve*) subtrCan2->FindObject( "h_diboson" ); // --------- Now save everything in the output file ------- f.cd(); data->Write("hist_data"); fit_total->Write("fit_total"); fit_diboson->Write("fit_diboson"); fit_Wjets->Write("fit_Wjets"); fit_Top->Write("fit_Top"); fit_QCD->Write("fit_QCD"); fit_Zjets->Write("fit_Zjets"); // fit_Ztautau->Write("fit_Ztautau"); subtrHist->Write("hist_data_subtracted"); Diboson->Write("curve_diboson"); // data1->Write("hist_data_muon"); // fit1->Write("curve_fit_muon"); // subtrHist1->Write("hist_data_subtracted_muon"); // Diboson1->Write("curve_diboson_muon"); // data2->Write("hist_data_electron"); // fit2->Write("curve_fit_electron"); // subtrHist2->Write("hist_data_subtracted_electron"); // Diboson2->Write("curve_diboson_electron"); f.Close(); }