void TwoCategorySimZFitter( TH1& h_TT, TH1& h_TF, double intLumi, int removeEE ) { // The fit variable - lepton invariant mass rooMass_ = new RooRealVar("Mass","m_{ee}",60.0, 120.0, "GeV/c^{2}"); rooMass_->setBins(20.0); RooRealVar Mass = *rooMass_; // Make the category variable that defines the two fits, // namely whether the probe passes or fails the eff criteria. RooCategory sample("sample","") ; sample.defineType("TT", 1) ; sample.defineType("TF", 2) ; gROOT->cd(); ///////// convert Histograms into RooDataHists RooDataHist* data_TT = new RooDataHist("data_TT","data_TT", RooArgList(Mass), &h_TT); RooDataHist* data_TF = new RooDataHist("data_TF","data_TF", RooArgList(Mass), &h_TF); RooDataHist* data = new RooDataHist( "fitData","fitData", RooArgList(Mass),Index(sample), Import("TT",*data_TT), Import("TF",*data_TF) ); data->get()->Print(); cout << "Made datahist" << endl; // ********** Construct signal & bkg shape PDF ********** // makeSignalPdf(); cout << "Made signal pdf" << endl; makeBkgPdf(); cout << "Made bkg pdf" << endl; // Now supply integrated luminosity in inverse picobarn // --> we get this number from the CMS lumi group // https://twiki.cern.ch/twiki/bin/view/CMS/LumiWiki2010Data RooRealVar lumi("lumi","lumi", intLumi); // Now define Z production cross section variable (in pb) RooRealVar xsec("xsec","xsec", 1300., 400.0, 2000.0); // Define efficiency variables RooRealVar eff("eff","eff", 0.9, 0.5, 1.0); // Now define acceptance variables --> we get these numbers from MC // RooRealVar acc_BB("acc_BB","acc_BB", 0.2253); // RooRealVar acc_EB("acc_EB","acc_EB", 0.1625); // RooRealVar acc_EE("acc_EE","acc_EE", 0.0479); double ACCEPTANCE=0.4357; if(removeEE==1) ACCEPTANCE= 0.3878; if(removeEE==2) ACCEPTANCE= 0.2253; RooRealVar acc("acc","acc", ACCEPTANCE); // Define background yield variables: they are not related to each other RooRealVar nBkgTF("nBkgTF","nBkgTF",10.,-10.,100000.); //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// // Define signal yield variables. // They are linked together by the total cross section: e.g. // Nbb = sigma*L*Abb*effB char* formula; RooArgList* args; formula = "lumi*xsec*acc*eff*eff"; args = new RooArgList(lumi,xsec,acc,eff); RooFormulaVar nSigTT("nSigTT", formula, *args); delete args; formula = "lumi*xsec*acc*eff*(1.0-eff)"; args = new RooArgList(lumi,xsec,acc,eff); RooFormulaVar nSigTF("nSigTF",formula, *args); delete args; ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// RooArgList componentsTF(*signalShapePdfTF_,*bkgShapePdfTF_); RooArgList yieldsTF(nSigTF, nBkgTF ); RooExtendPdf pdfTT("pdfTT","extended sum pdf", *signalShapePdfTT_, nSigTT); RooAddPdf pdfTF("pdfTF","extended sum pdf",componentsTF, yieldsTF); // The total simultaneous fit ... RooSimultaneous totalPdf("totalPdf","totalPdf", sample); totalPdf.addPdf(pdfTT,"TT"); totalPdf.Print(); totalPdf.addPdf(pdfTF,"TF"); totalPdf.Print(); // ********* Do the Actual Fit ********** // RooFitResult *fitResult = totalPdf.fitTo(*data, Save(true), Extended(), Minos(), PrintEvalErrors(-1),Warnings(false) ); fitResult->Print("v"); // ********** Make and save Canvas for the plots ********** // gROOT->ProcessLine(".L ~/tdrstyle.C"); setTDRStyle(); tdrStyle->SetErrorX(0.5); tdrStyle->SetPadLeftMargin(0.19); tdrStyle->SetPadRightMargin(0.10); tdrStyle->SetPadBottomMargin(0.15); tdrStyle->SetLegendBorderSize(0); tdrStyle->SetTitleYOffset(1.5); RooAbsData::ErrorType errorType = RooAbsData::SumW2; TString cname = Form("Zmass_TT_%dnb", (int)(1000*intLumi) ); c = new TCanvas(cname,cname,500,500); RooPlot* frame1 = Mass.frame(); data_TT->plotOn(frame1,RooFit::DataError(errorType)); pdfTT.plotOn(frame1,ProjWData(*data_TT)); frame1->SetMinimum(0); frame1->Draw("e0"); TPaveText *plotlabel = new TPaveText(0.23,0.87,0.43,0.92,"NDC"); plotlabel->SetTextColor(kBlack); plotlabel->SetFillColor(kWhite); plotlabel->SetBorderSize(0); plotlabel->SetTextAlign(12); plotlabel->SetTextSize(0.03); plotlabel->AddText("CMS Preliminary 2010"); TPaveText *plotlabel2 = new TPaveText(0.23,0.82,0.43,0.87,"NDC"); plotlabel2->SetTextColor(kBlack); plotlabel2->SetFillColor(kWhite); plotlabel2->SetBorderSize(0); plotlabel2->SetTextAlign(12); plotlabel2->SetTextSize(0.03); plotlabel2->AddText("#sqrt{s} = 7 TeV"); TPaveText *plotlabel3 = new TPaveText(0.23,0.75,0.43,0.80,"NDC"); plotlabel3->SetTextColor(kBlack); plotlabel3->SetFillColor(kWhite); plotlabel3->SetBorderSize(0); plotlabel3->SetTextAlign(12); plotlabel3->SetTextSize(0.03); char temp[100]; sprintf(temp, "%.1f", intLumi); plotlabel3->AddText((string("#int#font[12]{L}dt = ") + temp + string(" pb^{ -1}")).c_str()); TPaveText *plotlabel4 = new TPaveText(0.6,0.87,0.8,0.92,"NDC"); plotlabel4->SetTextColor(kBlack); plotlabel4->SetFillColor(kWhite); plotlabel4->SetBorderSize(0); plotlabel4->SetTextAlign(12); plotlabel4->SetTextSize(0.03); double nsig = eff.getVal()*xsec.getVal()*acc.getVal()*lumi.getVal(); double xsecFrErr = xsec.getError()/xsec.getVal(); double effFrErr = eff.getError()/eff.getVal(); double effxsecCorr = fitResult->correlation(eff, xsec); double nsigerr = sqrt(effFrErr**2 +xsecFrErr**2 + 2.0*effxsecCorr*xsecFrErr*effFrErr)*nsig; sprintf(temp, "Signal = %.2f #pm %.2f", nsig, nsigerr); plotlabel4->AddText(temp); TPaveText *plotlabel5 = new TPaveText(0.6,0.82,0.8,0.87,"NDC"); plotlabel5->SetTextColor(kBlack); plotlabel5->SetFillColor(kWhite); plotlabel5->SetBorderSize(0); plotlabel5->SetTextAlign(12); plotlabel5->SetTextSize(0.03); sprintf(temp, "#epsilon = %.4f #pm %.4f", eff.getVal(), eff.getError()); plotlabel5->AddText(temp); TPaveText *plotlabel6 = new TPaveText(0.6,0.77,0.8,0.82,"NDC"); plotlabel6->SetTextColor(kBlack); plotlabel6->SetFillColor(kWhite); plotlabel6->SetBorderSize(0); plotlabel6->SetTextAlign(12); plotlabel6->SetTextSize(0.03); sprintf(temp, "#sigma = %.1f #pm %.1f pb", xsec.getVal(), xsec.getError()); plotlabel6->AddText(temp); plotlabel->Draw(); plotlabel2->Draw(); plotlabel3->Draw(); plotlabel4->Draw(); plotlabel5->Draw(); plotlabel6->Draw(); c->SaveAs( cname + TString(".eps")); c->SaveAs( cname + TString(".gif")); c->SaveAs( cname + TString(".root")); c->SaveAs( cname + TString(".png")); c->SaveAs( cname + TString(".C")); TString cname = Form("Zmass_TF_%dnb", (int)(1000*intLumi) ); c = new TCanvas(cname,cname,500,500); RooPlot* frame2 = Mass.frame(); data_TF->plotOn(frame2,RooFit::DataError(errorType)); pdfTF.plotOn(frame2,ProjWData(*data_TF), Components(*bkgShapePdfTF_),LineColor(kRed)); pdfTF.plotOn(frame2,ProjWData(*data_TF)); frame2->SetMinimum(0); frame2->Draw("e0"); frame2->GetYaxis()->SetNdivisions(505); TPaveText *plotlabel = new TPaveText(0.23,0.87,0.43,0.92,"NDC"); plotlabel->SetTextColor(kBlack); plotlabel->SetFillColor(kWhite); plotlabel->SetBorderSize(0); plotlabel->SetTextAlign(12); plotlabel->SetTextSize(0.03); plotlabel->AddText("CMS Preliminary 2010"); TPaveText *plotlabel2 = new TPaveText(0.23,0.82,0.43,0.87,"NDC"); plotlabel2->SetTextColor(kBlack); plotlabel2->SetFillColor(kWhite); plotlabel2->SetBorderSize(0); plotlabel2->SetTextAlign(12); plotlabel2->SetTextSize(0.03); plotlabel2->AddText("#sqrt{s} = 7 TeV"); TPaveText *plotlabel3 = new TPaveText(0.23,0.75,0.43,0.80,"NDC"); plotlabel3->SetTextColor(kBlack); plotlabel3->SetFillColor(kWhite); plotlabel3->SetBorderSize(0); plotlabel3->SetTextAlign(12); plotlabel3->SetTextSize(0.03); char temp[100]; sprintf(temp, "%.1f", intLumi); plotlabel3->AddText((string("#int#font[12]{L}dt = ") + temp + string(" pb^{ -1}")).c_str()); TPaveText *plotlabel4 = new TPaveText(0.6,0.87,0.8,0.92,"NDC"); plotlabel4->SetTextColor(kBlack); plotlabel4->SetFillColor(kWhite); plotlabel4->SetBorderSize(0); plotlabel4->SetTextAlign(12); plotlabel4->SetTextSize(0.03); double nsig = (1.0-eff.getVal())*xsec.getVal()*acc.getVal()*lumi.getVal(); double xsecFrErr = xsec.getError()/xsec.getVal(); double effFrErr = eff.getError()/(1.0-eff.getVal()); double effxsecCorr = fitResult->correlation(eff, xsec); double nsigerr = sqrt(effFrErr**2 +xsecFrErr**2 + 2.0*effxsecCorr*xsecFrErr*effFrErr)*nsig; sprintf(temp, "Signal = %.2f #pm %.2f", nsig, nsigerr); plotlabel4->AddText(temp); TPaveText *plotlabel5 = new TPaveText(0.6,0.82,0.8,0.87,"NDC"); plotlabel5->SetTextColor(kBlack); plotlabel5->SetFillColor(kWhite); plotlabel5->SetBorderSize(0); plotlabel5->SetTextAlign(12); plotlabel5->SetTextSize(0.03); sprintf(temp, "Bkg = %.2f #pm %.2f", nBkgTF.getVal(), nBkgTF.getError()); plotlabel5->AddText(temp); TPaveText *plotlabel6 = new TPaveText(0.6,0.77,0.8,0.82,"NDC"); plotlabel6->SetTextColor(kBlack); plotlabel6->SetFillColor(kWhite); plotlabel6->SetBorderSize(0); plotlabel6->SetTextAlign(12); plotlabel6->SetTextSize(0.03); sprintf(temp, "#epsilon = %.4f #pm %.4f", eff.getVal(), eff.getError()); plotlabel6->AddText(temp); plotlabel->Draw(); plotlabel2->Draw(); plotlabel3->Draw(); plotlabel4->Draw(); plotlabel5->Draw(); plotlabel6->Draw(); c->SaveAs( cname + TString(".eps")); c->SaveAs( cname + TString(".gif")); c->SaveAs( cname + TString(".root")); c->SaveAs( cname + TString(".png")); c->SaveAs( cname + TString(".C")); /* RooMCStudy toymc( totalPdf, RooArgSet(Mass, sample), Binned(kTRUE), FitModel(totalPdf), Extended(),FitOptions(Extended(), Minos())); int numEventsToGenerate = (int)(h_TT.Integral()+h_TF.Integral()); toymc.generateAndFit(1000,numEventsToGenerate); RooPlot* plot1 = toymc.plotPull( xsec, -4., 4., 24); plot1->SetTitle(""); plot1->GetXaxis()->SetTitle("cross section pull"); TString cname = Form("pull_crosssection_%dnb", (int)(1000*intLumi)); TCanvas *c2 = new TCanvas(cname,"Pull distribution of cross section",500,500); plot1->Draw(); c2->SaveAs( cname + TString(".eps")); c2->SaveAs( cname + TString(".gif")); c2->SaveAs( cname + TString(".root")); RooPlot* plot2 = toymc.plotError( eff, 0., 0.2, 20); plot2->SetTitle(""); plot2->GetXaxis()->SetTitle("Uncertainty in efficiency"); TString cname = Form("error_efficiency_%dnb", (int)(1000*intLumi)); TCanvas *c3 = new TCanvas(cname,"Uncertainty in efficiency",500,500); plot2->Draw(); c3->SaveAs( cname + TString(".eps")); c3->SaveAs( cname + TString(".gif")); c3->SaveAs( cname + TString(".root")); RooPlot* plot3 = toymc.plotError( xsec, 0., 650., 65); plot3->SetTitle(""); plot3->GetXaxis()->SetTitle("Uncertainty in cross section (pb)"); TString cname = Form("error_crosssection_%dnb", (int)(1000*intLumi)); TCanvas *c4 = new TCanvas(cname,"Uncertainty in cross section",500,500); plot3->Draw(); c4->SaveAs( cname + TString(".eps")); c4->SaveAs( cname + TString(".gif")); c4->SaveAs( cname + TString(".root")); */ // if(data) delete data; // if(c) delete c; }
int main(int argc, char *argv[]){ OptionParser(argc,argv); RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR); RooMsgService::instance().setSilentMode(true); system(Form("mkdir -p %s",outdir_.c_str())); vector<string> procs; split(infilenames_,infilenamesStr_,boost::is_any_of(",")); TPython::Exec("import os,imp,re"); const char * env = gSystem->Getenv("CMSSW_BASE") ; std::string globeRt = env; TPython::Exec(Form("buildSMHiggsSignalXSBR = imp.load_source('*', '%s/src/flashggFinalFit/Signal/python/buildSMHiggsSignalXSBR.py')",globeRt.c_str())); TPython::Eval(Form("buildSMHiggsSignalXSBR.Init%dTeV()", 13)); for (unsigned int i =0 ; i<infilenames_.size() ; i++){ int mH =(int) TPython::Eval(Form("int(re.search('_M(.+?)_','%s').group(1))",infilenames_[i].c_str())); double WH_XS = (double)TPython::Eval(Form("buildSMHiggsSignalXSBR.getXS(%d,'%s')",mH,"WH")); double ZH_XS = (double)TPython::Eval(Form("buildSMHiggsSignalXSBR.getXS(%d,'%s')",mH,"ZH")); float tot_XS = WH_XS + ZH_XS; float wFrac= WH_XS /tot_XS ; float zFrac= ZH_XS /tot_XS ; std::cout << "mass "<< mH << " wh fraction "<< WH_XS /tot_XS << ", zh fraction "<< ZH_XS /tot_XS <<std::endl; TFile *infile = TFile::Open(infilenames_[i].c_str()); string outname =(string) TPython::Eval(Form("'%s'.split(\"/\")[-1].replace(\"VH\",\"WH_VH\")",infilenames_[i].c_str())); TFile *outfile = TFile::Open(outname.c_str(),"RECREATE") ; TDirectory* saveDir = outfile->mkdir("tagsDumper"); saveDir->cd(); RooWorkspace *inWS = (RooWorkspace*) infile->Get("tagsDumper/cms_hgg_13TeV"); RooRealVar *intLumi = (RooRealVar*)inWS->var("IntLumi"); RooWorkspace *outWS = new RooWorkspace("cms_hgg_13TeV"); outWS->import(*intLumi); std::list<RooAbsData*> data = (inWS->allData()) ; std::cout <<" [INFO] Reading WS dataset contents: "<< std::endl; for (std::list<RooAbsData*>::const_iterator iterator = data.begin(), end = data.end(); iterator != end; ++iterator ) { RooDataSet *dataset = dynamic_cast<RooDataSet *>( *iterator ); if (dataset) { string zhname =(string) TPython::Eval(Form("'%s'.replace(\"wzh\",\"zh\")",dataset->GetName())); string whname =(string) TPython::Eval(Form("'%s'.replace(\"wzh\",\"wh\")",dataset->GetName())); RooDataSet *datasetZH = (RooDataSet*) dataset->emptyClone(zhname.c_str(),zhname.c_str()); RooDataSet *datasetWH = (RooDataSet*) dataset->emptyClone(whname.c_str(),whname.c_str()); TRandom3 r; r.Rndm(); double x[dataset->numEntries()]; r.RndmArray(dataset->numEntries(),x); for (int j =0; j < dataset->numEntries() ; j++){ if( x[j] < wFrac){ dataset->get(j); datasetWH->add(*(dataset->get(j)),dataset->weight()); } else{ dataset->get(j); datasetZH->add(*(dataset->get(j)),dataset->weight()); } } float w =datasetWH->sumEntries(); float z =datasetZH->sumEntries(); if(verbose_){ std::cout << "Original dataset " << *dataset <<std::endl; std::cout << "WH dataset " << *datasetWH <<std::endl; std::cout << "ZH dataset " << *datasetZH <<std::endl; std::cout << "********************************************" <<std::endl; std::cout << "WH fraction (obs) : WH " << w/(w+z) <<", ZH "<< z/(w+z) << std::endl; std::cout << "WH fraction (exp) : WH " << wFrac <<", ZH "<< zFrac << std::endl; std::cout << "********************************************" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "********************************************" <<std::endl; } outWS->import(*datasetWH); outWS->import(*datasetZH); } RooDataHist *datahist = dynamic_cast<RooDataHist *>( *iterator ); if (datahist) { string zhname =(string) TPython::Eval(Form("'%s'.replace(\"wzh\",\"zh\")",datahist->GetName())); string whname =(string) TPython::Eval(Form("'%s'.replace(\"wzh\",\"wh\")",datahist->GetName())); RooDataHist *datahistZH = (RooDataHist*) datahist->emptyClone(zhname.c_str(),zhname.c_str()); RooDataHist *datahistWH = (RooDataHist*) datahist->emptyClone(whname.c_str(),whname.c_str()); TRandom3 r; r.Rndm(); double x[datahist->numEntries()]; r.RndmArray(datahist->numEntries(),x); for (int j =0; j < datahist->numEntries() ; j++){ datahistWH->add(*(datahist->get(j)),datahist->weight()*wFrac); datahistZH->add(*(datahist->get(j)),datahist->weight()*zFrac); } float w =datahistWH->sumEntries(); float z =datahistZH->sumEntries(); if(verbose_){ std::cout << "Original datahist " << *datahist <<std::endl; std::cout << "WH datahist " << *datahistWH <<std::endl; std::cout << "ZH datahist " << *datahistZH <<std::endl; std::cout << "********************************************" <<std::endl; std::cout << "WH fraction (obs) : WH " << w/(w+z) <<", ZH "<< z/(w+z) << std::endl; std::cout << "WH fraction (exp) : WH " << wFrac <<", ZH "<< zFrac << std::endl; std::cout << "********************************************" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "********************************************" <<std::endl; } outWS->import(*datahistWH); outWS->import(*datahistZH); } } saveDir->cd(); outWS->Write(); outfile->Close(); infile->Close(); } }
int main(int argc, char *argv[]){ OptionParser(argc,argv); RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR); RooMsgService::instance().setSilentMode(true); system(Form("mkdir -p %s",outdir_.c_str())); vector<string> procs; split(infilenames_,infilenamesStr_,boost::is_any_of(",")); TPython::Exec("import os,imp,re"); for (unsigned int i =0 ; i<infilenames_.size() ; i++){ TFile *infile = TFile::Open(infilenames_[i].c_str()); string outname =(string) TPython::Eval(Form("'%s'.split(\"/\")[-1].replace('root','_reduced.root')",infilenames_[i].c_str())); TFile *outfile = TFile::Open(outname.c_str(),"RECREATE") ; TDirectory* saveDir = outfile->mkdir("tagsDumper"); saveDir->cd(); RooWorkspace *inWS = (RooWorkspace*) infile->Get("tagsDumper/cms_hgg_13TeV"); RooRealVar *intLumi = (RooRealVar*)inWS->var("IntLumi"); RooWorkspace *outWS = new RooWorkspace("cms_hgg_13TeV"); outWS->import(*intLumi); std::list<RooAbsData*> data = (inWS->allData()) ; std::cout <<" [INFO] Reading WS dataset contents: "<< std::endl; for (std::list<RooAbsData*>::const_iterator iterator = data.begin(), end = data.end(); iterator != end; ++iterator ) { RooDataSet *dataset = dynamic_cast<RooDataSet *>( *iterator ); if (dataset) { RooDataSet *datasetReduced = (RooDataSet*) dataset->emptyClone(dataset->GetName(),dataset->GetName()); TRandom3 r; r.Rndm(); double x[dataset->numEntries()]; r.RndmArray(dataset->numEntries(),x); int desiredEntries = floor(0.5+ dataset->numEntries()*fraction_); int modFraction = floor(0.5+ 1/fraction_); int finalEventCount=0; for (int j =0; j < dataset->numEntries() ; j++){ if( j%modFraction==0){ finalEventCount++; } } float average_weight= dataset->sumEntries()/finalEventCount; for (int j =0; j < dataset->numEntries() ; j++){ if( j%modFraction==0){ dataset->get(j); datasetReduced->add(*(dataset->get(j)),average_weight); } } float entriesIN =dataset->sumEntries(); float entriesOUT =datasetReduced->sumEntries(); if(verbose_){ std::cout << "Original dataset " << *dataset <<std::endl; std::cout << "Reduced dataset " << *datasetReduced <<std::endl; std::cout << "********************************************" <<std::endl; std::cout << "fraction (obs) : " << entriesOUT/entriesIN << std::endl; std::cout << "fraction (exp) : " << fraction_ << std::endl; std::cout << "********************************************" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "********************************************" <<std::endl; } outWS->import(*datasetReduced); } RooDataHist *datahist = dynamic_cast<RooDataHist *>( *iterator ); if (datahist) { RooDataHist *datahistOUT = (RooDataHist*) datahist->emptyClone(datahist->GetName(),datahist->GetName()); TRandom3 r; r.Rndm(); for (int j =0; j < datahist->numEntries() ; j++){ datahistOUT->add(*(datahist->get(j)),datahist->weight()); } float w =datahistOUT->sumEntries(); float z =datahist->sumEntries(); if(verbose_){ std::cout << "Original datahist " << *datahist <<std::endl; std::cout << "Reduced datahist " << *datahistOUT<<std::endl; std::cout << "********************************************" <<std::endl; std::cout << "WH fraction (obs) : " << w/(z) <<std::endl; std::cout << "WH fraction (exp) : " << fraction_ << std::endl; std::cout << "********************************************" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "" <<std::endl; std::cout << "********************************************" <<std::endl; } outWS->import(*datahistOUT); } } saveDir->cd(); outWS->Write(); outfile->Close(); infile->Close(); } }