TGraphErrors plot(RooDataSet *dataset, TString alpha, TString constTerm){ TTree *tree = dataset2tree(dataset); TH2F *hist = prof2d(tree, alpha, constTerm, "nll"); RooHistPdf *histPdf = nllToL(hist); TTree *genTree = dataset2tree(histPdf->generate(*histPdf->getVariables(),1000000,kTRUE,kFALSE)); TGraphErrors graph = g(genTree); delete tree; delete hist; delete histPdf; delete genTree; return graph; }
TGraphErrors plot(TFile *f, TString alpha, TString constTerm){ RooStats::MarkovChain *chain = (RooStats::MarkovChain *)f->Get("_markov_chain"); TTree *tree = dataset2tree(chain->GetAsDataSet()); TH2F *hist = prof2d(tree, alpha, constTerm, "nll_MarkovChain_local_"); RooHistPdf *histPdf = nllToL(hist); TTree *genTree = dataset2tree(histPdf->generate(*histPdf->getVariables(),1000000,kTRUE,kFALSE)); TGraphErrors graph = g(genTree); delete tree; delete hist; delete histPdf; delete genTree; return graph; }
void runUPWW() { int higgsMass=125; double intLumi=5.1; int nToys = 10; bool draw=true; using namespace RooFit; gROOT->ProcessLine(".L ~/tdrstyle.C"); setTDRStyle(); gStyle->SetPadLeftMargin(0.16); gROOT->ForceStyle(); gROOT->ProcessLine(".L statsFactory.cc+"); // // set up test kind // double sigRate; double bkgRate; if(higgsMass==125){ sigRate = 7.; bkgRate = 66.; }else{ cout << "HMMMM.... I don't know that mass point...BYE!" << endl; return; } RooRealVar* mll = new RooRealVar("mll","dilepton mass [GeV]", 12, 80.); mll->setBins(17); RooArgSet* obs = new RooArgSet(*mll) ; // read signal hypothesis 1 TChain *tsigHyp1 = new TChain("angles"); tsigHyp1->Add(Form("datafiles/bdtpresel/%i/SMHiggsWW_%i_JHU.root",higgsMass, higgsMass)); RooDataSet *sigHyp1Data = new RooDataSet("sigHyp1Data","sigHyp1Data",tsigHyp1,*obs); RooDataHist *sigHyp1Hist = sigHyp1Data->binnedClone(0); RooHistPdf* sigHyp1Pdf = new RooHistPdf("sigHyp1Pdf", "sigHyp1Pdf", *obs, *sigHyp1Hist); // read background TChain *bkgTree = new TChain("angles"); bkgTree->Add(Form("datafiles/bdtpresel/%i/WW_madgraph_8TeV.root",higgsMass)); RooDataSet *bkgData = new RooDataSet("bkgData","bkgData",bkgTree,*obs); RooDataHist *bkgHist = bkgData->binnedClone(0); RooHistPdf* bkgPdf = new RooHistPdf("bkgPdf", "bkgPdf", *obs, *bkgHist); char statResults[25]; statsFactory *hwwuls; sprintf(statResults,"uls_hww125_%.0ffb.root", intLumi); hwwuls = new statsFactory(obs, sigHyp1Pdf, sigHyp1Pdf, statResults); hwwuls->runUpperLimitWithBackground(sigRate*intLumi, bkgRate*intLumi, bkgPdf, nToys); delete hwwuls; // draw plots if(draw) { RooPlot* plot1 = mll->frame(); TString plot1Name = "mll"; TCanvas* c1 = new TCanvas("c1","c1",400,400); bkgData->plotOn(plot1,MarkerColor(kBlack)); bkgPdf->plotOn(plot1, LineColor(kBlack), LineStyle(kDashed)); sigHyp1Data->plotOn(plot1,MarkerColor(kRed)); sigHyp1Pdf->plotOn(plot1,LineColor(kRed), LineStyle(kDashed)); // draw... plot1->Draw(); c1->SaveAs(Form("plots/ul/epsfiles/%s.eps", plot1Name.Data())); c1->SaveAs(Form("plots/ul/pngfiles/%s.png", plot1Name.Data())); delete c1; } }
void FitterUtils::generate() { //***************Get the PDFs from the workspace TFile fw(workspacename.c_str(), "UPDATE"); RooWorkspace* workspace = (RooWorkspace*)fw.Get("workspace"); RooRealVar *B_plus_M = workspace->var("B_plus_M"); RooRealVar *misPT = workspace->var("misPT"); RooRealVar *T = workspace->var("T"); RooRealVar *n = workspace->var("n"); RooRealVar *expoConst = workspace->var("expoConst"); RooRealVar *trueExp = workspace->var("trueExp"); RooRealVar *fractionalErrorJpsiLeak = workspace->var("fractionalErrorJpsiLeak"); cout<<"VALUE OF T IN GENERATE: "<<T->getVal()<<" +- "<<T->getError()<<endl; cout<<"VALUE OF n IN GENERATE: "<<n->getVal()<<" +- "<<n->getError()<<endl; RooHistPdf *histPdfSignalZeroGamma = (RooHistPdf *) workspace->pdf("histPdfSignalZeroGamma"); RooHistPdf *histPdfSignalOneGamma = (RooHistPdf *) workspace->pdf("histPdfSignalOneGamma"); RooHistPdf *histPdfSignalTwoGamma = (RooHistPdf *) workspace->pdf("histPdfSignalTwoGamma"); RooHistPdf *histPdfPartReco = (RooHistPdf *) workspace->pdf("histPdfPartReco"); RooHistPdf *histPdfJpsiLeak(0); if(nGenJpsiLeak>1) histPdfJpsiLeak = (RooHistPdf *) workspace->pdf("histPdfJpsiLeak"); RooAbsPdf *combPDF; if (fit2D) { combPDF = new RooPTMVis("combPDF", "combPDF", *misPT, *B_plus_M, *T, *n, *expoConst); } else { combPDF = new RooExponential("combPDF", "combPDF", *B_plus_M, *expoConst); } double trueExpConst(trueExp->getValV()); expoConst->setVal(trueExpConst); //***************Prepare generation int nGenSignalZeroGamma(floor(nGenFracZeroGamma*nGenSignal)); int nGenSignalOneGamma(floor(nGenFracOneGamma*nGenSignal)); int nGenSignalTwoGamma(floor(nGenSignal-nGenSignalZeroGamma-nGenSignalOneGamma)); RooArgSet argset2(*B_plus_M); if (fit2D) argset2.add(*misPT); cout<<"Preparing the generation of events 1"; RooRandom::randomGenerator()->SetSeed(); RooAbsPdf::GenSpec* GenSpecSignalZeroGamma = histPdfSignalZeroGamma->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenSignalZeroGamma)); cout<<" 2 "; RooAbsPdf::GenSpec* GenSpecSignalOneGamma = histPdfSignalOneGamma->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenSignalOneGamma)); cout<<" 3 "; RooAbsPdf::GenSpec* GenSpecSignalTwoGamma = histPdfSignalTwoGamma->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenSignalTwoGamma)); cout<<" 4 "; RooAbsPdf::GenSpec* GenSpecPartReco = histPdfPartReco->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenPartReco)); cout<<" 5 "<<endl; RooAbsPdf::GenSpec* GenSpecComb = combPDF->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenComb)); RooAbsPdf::GenSpec* GenSpecJpsiLeak(0); if(nGenJpsiLeak>1) GenSpecJpsiLeak = histPdfJpsiLeak->prepareMultiGen(argset2, RooFit::Extended(1), NumEvents(nGenJpsiLeak)); cout<<"Variable loaded:"<<endl; B_plus_M->Print(); expoConst->Print(); //B_plus_DTFM_M_zero->Print(); if (fit2D) misPT->Print(); //***************Generate some datasets cout<<"Generating signal Zero Photon"<<endl; RooDataSet* dataGenSignalZeroGamma = histPdfSignalZeroGamma->generate(*GenSpecSignalZeroGamma);//(argset, 250, false, true, "", false, true); dataGenSignalZeroGamma->SetName("dataGenSignalZeroGamma"); dataGenSignalZeroGamma->SetTitle("dataGenSignalZeroGamma"); cout<<"Generating signal One Photon"<<endl; RooDataSet* dataGenSignalOneGamma = histPdfSignalOneGamma->generate(*GenSpecSignalOneGamma);//(argset, 250, false, true, "", false, true); dataGenSignalOneGamma->SetName("dataGenSignalOneGamma"); dataGenSignalOneGamma->SetTitle("dataGenSignalOneGamma"); cout<<"Generating signal two Photons"<<endl; RooDataSet* dataGenSignalTwoGamma = histPdfSignalTwoGamma->generate(*GenSpecSignalTwoGamma);//(argset, 250, false, true, "", false, true); dataGenSignalTwoGamma->SetName("dataGenSignalTwoGamma"); dataGenSignalTwoGamma->SetTitle("dataGenSignalTwoGamma"); cout<<"Generating combinatorial"<<endl; RooDataSet* dataGenComb = combPDF->generate(*GenSpecComb);//(argset, 100, false, true, "", false, true); dataGenComb->SetName("dataGenComb"); dataGenComb->SetTitle("dataGenComb"); cout<<"Generating PartReco"<<endl; RooDataSet* dataGenPartReco = histPdfPartReco->generate(*GenSpecPartReco);//argset, 160, false, true, "", false, true); dataGenPartReco->SetName("dataGenPartReco"); dataGenPartReco->SetTitle("dataGenPartReco"); RooDataSet* dataGenJpsiLeak(0); if(nGenJpsiLeak>1) { cout<<"Generating Leaking JPsi"<<endl; dataGenJpsiLeak = histPdfJpsiLeak->generate(*GenSpecJpsiLeak);//argset, 160, false, true, "", false, true); dataGenJpsiLeak->SetName("dataGenJpsiLeak"); dataGenJpsiLeak->SetTitle("dataGenJpsiLeak"); } //*************Saving the generated datasets in a workspace RooWorkspace workspaceGen("workspaceGen", "workspaceGen"); workspaceGen.import(*dataGenSignalZeroGamma); workspaceGen.import(*dataGenSignalOneGamma); workspaceGen.import(*dataGenSignalTwoGamma); workspaceGen.import(*dataGenComb); workspaceGen.import(*dataGenPartReco); if(nGenJpsiLeak>1) workspaceGen.import(*dataGenJpsiLeak); workspaceGen.Write("", TObject::kOverwrite); //delete workspace; fw.Close(); delete dataGenSignalZeroGamma; delete dataGenSignalOneGamma; delete dataGenSignalTwoGamma; delete dataGenComb; delete dataGenPartReco; if(nGenJpsiLeak>1) delete dataGenJpsiLeak; delete GenSpecSignalZeroGamma; delete GenSpecSignalOneGamma; delete GenSpecSignalTwoGamma; delete GenSpecComb; delete GenSpecPartReco; delete combPDF; if(nGenJpsiLeak>1) delete GenSpecJpsiLeak; delete histPdfSignalZeroGamma; delete histPdfSignalOneGamma; delete histPdfSignalTwoGamma; delete histPdfPartReco; if(nGenJpsiLeak>1) delete histPdfJpsiLeak; }
void buildPdf() { Double_t lorange = 100.; Double_t hirange = 140.; // Import data TFile *file = new TFile("/atlas/data18a/yupan/HZZ4l2012/MiniTree/data12.root"); TTree *tree = (TTree*)file->Get("tree_incl_4mu"); // should include all channels, for now just testing... // Define variables Float_t m4l = 0; Float_t m4lerr = 0; // Float_t wgt = 0; // Get number of entries and setbranchaddress Int_t nevents = tree->GetEntries(); tree->SetBranchStatus("*",0); tree->SetBranchStatus("m4l_unconstrained",1); tree->SetBranchStatus("m4lerr_unconstrained",1); //tree->SetBranchStatus("weight",1); tree->SetBranchAddress("m4l_unconstrained",&m4l); tree->SetBranchAddress("m4lerr_unconstrained",&m4lerr); //tree->SetBranchAddress("weight",&wgt); /////////////// // Build pdfs ////////////// RooRealVar* mass = new RooRealVar("m4l","mass",lorange,hirange,"GeV"); RooRealVar merr("m4lerr","mass err",0.1,4.0,"GeV"); //RooRealVar weight("weight","weight",0,10); RooRealVar scale("scale","per-event error scale factor",1.0,0.2,4.0); RooProduct sigmaErr("sigmaErr","sigmaErr",RooArgSet(scale,merr)); /* float totalwgt(0); for (Int_t i=0; i<nevents; i++) { tree->GetEntry(i); totalwgt+=wgt; } std::cout<<"total weight = "<<totalwgt<<std::endl; */ /// Make DataSet RooDataSet signal("signal","signal",RooArgSet(*mass,merr)); std::cout<<"Reading in events from signal minitree"<<std::endl; for (Int_t i=0; i<nevents; i++) { tree->GetEntry(i); mass->setVal(m4l); merr.setVal(m4lerr); //weight.setVal(wgt/totalwgt); signal.add(RooArgSet(*mass,merr)); } // Create 1D kernel estimation for signal mass std::cout<<"Building mass keys"<<std::endl; RooKeysPdf kestsig("kestsig","kestsig",*mass,signal); TH1F* hm = (TH1F*)kestsig.createHistogram("hm",*mass); kestsig.fillHistogram(hm,RooArgList(*mass)); std::cout<<"Building mass pdf"<<std::endl; RooDataHist* dmass = new RooDataHist("dmass","binned dataset",*mass,hm); RooHistPdf* masspdf = new RooHistPdf("masspdf","pdf(dm)",*mass,*dmass,2); // Create 1D kernel estimation for mass err std::cout<<"Building error keys"<<std::endl; RooKeysPdf kestsigerr("kestsigerr","kestsigerr",merr,signal,RooKeysPdf::MirrorBoth,2); TH1F* herr = (TH1F*)kestsigerr.createHistogram("herr",merr); kestsigerr.fillHistogram(herr,RooArgList(merr)); std::cout<<"Integral "<<herr->Integral()<<std::endl; std::cout<<"Building error pdf"<<std::endl; RooDataHist* derr = new RooDataHist("derr","binned dataset",merr,herr); RooHistPdf* errpdf = new RooHistPdf("errpdf","pdf(de)",merr,*derr,2); //Make the crystal ball resolution model with CB sigma = mass error RooRealVar meanCB ("meanCB", "mean CB", hmass, hmass-10., hmass+5.); RooRealVar alphaCB ("alphaCB", "alpha CB", 7, 0., 10.); RooRealVar nnCB ("nnCB", "nn CB", 1.5, 0., 15.); RooCBShape* shapeCB = new RooCBShape("shapeCB", "crystal ball pdf", *mass, meanCB, sigmaErr, alphaCB, nnCB); // Make conditional pdf RooProdPdf* sigmodel = new RooProdPdf("sigmodel","sigmodel", *errpdf, Conditional(*shapeCB, *mass)); // Make a workspace to store the fit model RooWorkspace* pdfWsp = new RooWorkspace("pdfWspCB"); pdfWsp->import(*sigmodel,RecycleConflictNodes()); pdfWsp->import(*masspdf); pdfWsp->import(*errpdf,RecycleConflictNodes()); pdfWsp->import(signal); pdfWsp->Print(); pdfWsp->writeToFile("pdfWspCB.root"); // Make some plots TCanvas *c = new TCanvas("c","c",500,500); c->Divide(2); RooPlot* frame = merr.frame(Title("keys signal error")); signal.plotOn(frame); kestsigerr.plotOn(frame,LineColor(kRed)); errpdf->plotOn(frame); RooPlot* frame2 = mass->frame(Title("keys signal")); signal.plotOn(frame2); kestsig.plotOn(frame2,LineColor(kRed)); masspdf->plotOn(frame2); c->cd(2); frame->Draw(); c->cd(1); grame2->Draw(); c->Print("testPdf.png"); }
h_Mej_allBkg->Add(h_Mej_wjet); h_Mej_allBkg->Add(h_Mej_ww); h_Mej_allBkg->Add(h_Mej_wz); h_Mej_allBkg->Add(h_Mej_zz); h_Mej_allBkg->Add(h_Mej_qcdjet); h_Mej_allBkg->Add(h_Mej_gammajet); //## Mej var RooRealVar Mej("Mej","Mej",0.,1000.,"GeV"); //## Create histograms and pdf in RooFit format RooDataHist RDH_data("data","data",Mej,h_Mej_allBkg); RooHistPdf Pdf_data("dataMej","dataMej" ,RooArgList(Mej),RDH_data); //## Crystall Ball to model the background RooRealVar m0("m0","m0",100,50.,150.); RooRealVar sigma("sigma","sigma",30,0.,60); RooRealVar alpha("alpha","alpha",-0.4,-100,0.); RooRealVar n("n","n",10,0.,+100); RooCBShape Pdf_bkg("bkgMej","bkgMej",Mej,m0,sigma,alpha,n); RooRealVar N_bkg("N_bkg","N_bkg",100,0.,10000); RooAddPdf *modelFit=new RooAddPdf("modelFit","Fit model",RooArgList(Pdf_bkg),RooArgList(N_bkg)); //1) ## Generate a toyMC sample and fit
void MakePlots(TString filename, TString energy="8TeV", TString lumi=""){ TString outDir=filename; outDir.ReplaceAll("fitres","img"); outDir="tmp/"; //std::map<TString, TH2F *> deltaNLL_map; /*------------------------------ Plotto */ TCanvas *c = new TCanvas("c","c"); TFile f_in(filename, "read"); if(f_in.IsZombie()){ std::cerr << "File opening error: " << filename << std::endl; return; } TList *KeyList = f_in.GetListOfKeys(); std::cout << KeyList->GetEntries() << std::endl; for(int i =0; i < KeyList->GetEntries(); i++){ c->Clear(); TKey *key = (TKey *)KeyList->At(i); if(TString(key->GetClassName())!="RooDataSet") continue; RooDataSet *dataset = (RooDataSet *) key->ReadObj(); TString constTermName = dataset->GetName(); TString alphaName=constTermName; alphaName.ReplaceAll("constTerm","alpha"); TTree *tree = dataset2tree(dataset); TGraphErrors bestFit_ = bestFit(tree, alphaName, constTermName); TH2F *hist = prof2d(tree, alphaName, constTermName, "nll", "(12,-0.0005,0.0115,29,-0.0025,0.1425)",true); // // deltaNLL_map.insert(std::pair <TString, TH2F *>(keyName,hist)); hist->SaveAs(outDir+"/deltaNLL-"+constTermName+".root"); hist->Draw("colz"); bestFit_.Draw("P same"); bestFit_.SetMarkerSize(2); Int_t iBinX, iBinY; Double_t x,y; hist->GetBinWithContent2(0,iBinX,iBinY); x= hist->GetXaxis()->GetBinCenter(iBinX); y= hist->GetYaxis()->GetBinCenter(iBinY); TGraph nllBestFit(1,&x,&y); nllBestFit.SetMarkerStyle(3); nllBestFit.SetMarkerColor(kRed); TList* contour68 = contourFromTH2(hist, 0.68); hist->Draw("colz"); hist->GetZaxis()->SetRangeUser(0,50); bestFit_.Draw("P same"); nllBestFit.Draw("P same"); //contour68->Draw("same"); c->SaveAs(outDir+"/deltaNLL-"+constTermName+".png"); hist->SaveAs("tmp/hist-"+constTermName+".root"); nllBestFit.SaveAs("tmp/nllBestFit.root"); contour68->SaveAs("tmp/contour68.root"); delete hist; hist = prof2d(tree, alphaName, constTermName, "nll", "(12,-0.0005,0.0115,29,-0.0025,0.1425)"); RooHistPdf *histPdf = nllToL(hist); delete hist; RooDataSet *gen_dataset=histPdf->generate(*histPdf->getVariables(),1000000,kTRUE,kFALSE); TTree *genTree = dataset2tree(gen_dataset); genTree->SaveAs("tmp/genTree-"+constTermName+".root"); delete gen_dataset; delete histPdf; TGraphErrors toyGraph = g(genTree, constTermName); TGraphErrors bestFitGraph = g(tree,alphaName, constTermName); TGraphErrors bestFitScanGraph = g(y, x); delete genTree; delete tree; toyGraph.SetFillColor(kGreen); toyGraph.SetLineColor(kBlue); toyGraph.SetLineStyle(2); bestFitGraph.SetLineColor(kBlack); bestFitScanGraph.SetLineColor(kRed); bestFitScanGraph.SetLineWidth(2); TMultiGraph g_multi("multigraph",""); g_multi.Add(&toyGraph,"L3"); g_multi.Add(&toyGraph,"L"); g_multi.Add(&bestFitGraph, "L"); g_multi.Add(&bestFitScanGraph, "L"); g_multi.Draw("A"); c->Clear(); g_multi.Draw("A"); c->SaveAs(outDir+"/smearing_vs_energy-"+constTermName+".png"); // TPaveText *pv = new TPaveText(0.7,0.7,1, 0.8); // TLegend *legend = new TLegend(0.7,0.8,0.95,0.92); // legend->SetFillStyle(3001); // legend->SetFillColor(1); // legend->SetTextFont(22); // 132 // legend->SetTextSize(0.04); // l'ho preso mettendo i punti con l'editor e poi ho ricavato il valore con il metodo GetTextSize() // // legend->SetFillColor(0); // colore di riempimento bianco // legend->SetMargin(0.4); // percentuale della larghezza del simbolo // SetLegendStyle(legend); //Plot(c, data,mc,mcSmeared,legend, region, filename, energy, lumi); } f_in.Close(); return; }
void runSigSepWWSingle(int higgsMass, double intLumi, int nToys, const TestType test, int var, int toy, bool draw, const unsigned int seed) { // location of data // for ucsd batch submission const char *dataLocation = "root://xrootd.unl.edu//store/user/yygao/HWWAngular/datafiles/"; // for local tests // const char *dataLocation = "datafiles/"; // // set up test kind // TString testName = getTestName(test); TString varName = getVarName(var); TString toyName = getToyName(toy); std::cout << "Doing " << toyName << " studies on " << testName << " separation based on " << varName << "\n"; double lowMt(0.); double highMt = higgsMass; double sigRate; double bkgRate; if(higgsMass==125){ sigRate = 13.0; bkgRate = 155.; }else{ cout << "HMMMM.... I don't know that mass point...BYE!" << endl; return; } RooRealVar* dphill = new RooRealVar("dphill","#Delta#phi(leptons) [radian]", 0, TMath::Pi()); dphill->setBins(20); RooRealVar* mt = new RooRealVar("mt","transverse higgs mass", 60, 130); mt->setBins(10); RooRealVar* mll = new RooRealVar("mll","dilepton mass [GeV]", 10, 90.); mll->setBins(10); RooArgSet* obs; if ( var == DPHI ) obs = new RooArgSet(*dphill) ; if ( var == MLL ) obs = new RooArgSet(*mll) ; if ( var == MLLMT ) obs = new RooArgSet(*mll, *mt) ; if ( var == DPHIMT ) obs = new RooArgSet(*dphill, *mt) ; // // read signal hypothesis 1 always SMHiggs // TChain *tsigHyp1 = new TChain("angles"); tsigHyp1->Add(Form("%s/%i/SMHiggsWW_%i_JHU.root", dataLocation, higgsMass, higgsMass)); RooDataSet *sigHyp1Data = new RooDataSet("sigHyp1Data","sigHyp1Data",tsigHyp1,*obs); RooDataHist *sigHyp1Hist = sigHyp1Data->binnedClone(0); RooHistPdf* sigHyp1Pdf = new RooHistPdf("sigHyp1Pdf", "sigHyp1Pdf", *obs, *sigHyp1Hist); // read signal hypothesis 2 TChain *tsigHyp2 = new TChain("angles"); TString secondhypName = getSecondHypInputName(test, float(higgsMass)); tsigHyp2->Add(Form("%s/%i/%s",dataLocation, higgsMass, secondhypName.Data())); std::cout << secondhypName << "\n"; RooDataSet *sigHyp2Data = new RooDataSet("sigHyp2Data","sigHyp2Data",tsigHyp2,*obs); RooDataHist *sigHyp2Hist = sigHyp2Data->binnedClone(0); RooHistPdf* sigHyp2Pdf = new RooHistPdf("sigHyp2Pdf", "sigHyp2Pdf", *obs, *sigHyp2Hist); // read background TChain *bkgTree = new TChain("angles"); bkgTree->Add(Form("%s/%i/WW_madgraph_8TeV_0j.root",dataLocation,higgsMass)); RooDataSet *bkgData = new RooDataSet("bkgData","bkgData",bkgTree,*obs); RooDataHist *bkgHist = bkgData->binnedClone(0); RooHistPdf* bkgPdf = new RooHistPdf("bkgPdf", "bkgPdf", *obs, *bkgHist); char statResults[50]; statsFactory *myHypothesisSeparation; sprintf(statResults,"stat_%s_%s_%s_%.0ffb_%u.root",testName.Data(), toyName.Data(), varName.Data(), intLumi, seed); printf(statResults); myHypothesisSeparation = new statsFactory(obs, sigHyp1Pdf, sigHyp2Pdf, seed, statResults); // running pure toys myHypothesisSeparation->hypothesisSeparationWithBackground(sigRate*intLumi,sigRate*intLumi,nToys,bkgPdf,bkgRate*intLumi); delete myHypothesisSeparation; std::cout << "deleted myHypothesisSeparation" << std::endl; // draw plots if(draw) { RooPlot* plot1; TString plot1Name; TCanvas* c1 = new TCanvas("c1","c1",400,400); if ( var == DPHIMT || var == DPHI) { plot1 = dphill->frame(); plot1Name = Form("MELAproj_%s_%s_%s_dphi", testName.Data(), toyName.Data(), varName.Data()); } if ( var == MLL || var == MLLMT) { plot1 = mll->frame(); plot1Name = Form("MELAproj_%s_%s_%s_mll", testName.Data(), toyName.Data(), varName.Data()); } bkgData->plotOn(plot1,MarkerColor(kBlack)); bkgPdf->plotOn(plot1, LineColor(kBlack), LineStyle(kDashed)); sigHyp1Data->plotOn(plot1,MarkerColor(kRed)); sigHyp1Pdf->plotOn(plot1,LineColor(kRed), LineStyle(kDashed)); sigHyp2Data->plotOn(plot1,MarkerColor(kBlue)); sigHyp2Pdf->plotOn(plot1,LineColor(kBlue), LineStyle(kDashed)); // draw... plot1->Draw(); c1->SaveAs(Form("plots/epsfiles/%s.eps", plot1Name.Data())); c1->SaveAs(Form("plots/pngfiles/%s.png", plot1Name.Data())); if ( var == DPHIMT || var == MLLMT ) { RooPlot* plot2 = mt->frame(); TString plot2Name; plot2Name = Form("MELAproj_%s_%s_%s_mt", testName.Data(), toyName.Data(), varName.Data()); bkgData->plotOn(plot2,MarkerColor(kBlack)); bkgPdf->plotOn(plot2, LineColor(kBlack), LineStyle(kDashed)); sigHyp1Data->plotOn(plot2,MarkerColor(kRed)); sigHyp1Pdf->plotOn(plot2,LineColor(kRed), LineStyle(kDashed)); sigHyp2Data->plotOn(plot2,MarkerColor(kBlue)); sigHyp2Pdf->plotOn(plot2,LineColor(kBlue), LineStyle(kDashed)); c1->Clear(); plot2->Draw(); c1->SaveAs(Form("plots/epsfiles/%s.eps", plot2Name.Data())); c1->SaveAs(Form("plots/pngfiles/%s.png", plot2Name.Data())); } delete c1; delete plot1; delete plot2; } // tidy up delete dphill; delete mt; delete mll; delete obs; delete tsigHyp1; delete sigHyp1Data; delete sigHyp1Pdf; delete tsigHyp2; delete sigHyp2Data; delete sigHyp2Pdf; delete bkgTree; delete bkgData; delete bkgPdf; delete myHypothesisSeparation; }
void LHFit() { double lorange = 100.; double hirange = 140.; // Import data TFile *file = new TFile("/atlas/data18a/yupan/HZZ4l2012/MiniTree/MiniTree_2013Moriond/v03/MC12a/JHUPythia8_AU2CTEQ6L1_ggH125_Spin0p_ZZ4lep.root"); TTree *tree = (TTree*)file->Get("tree_incl_4mu"); // Define variables float m4l = 0; float m34 = 0; float m4lerr = 0; float wgt = 0; // Get the number of entries in the tree int nevents = tree->GetEntries(); tree->SetBranchStatus("*",0); tree->SetBranchStatus("m4l_unconstrained",1); tree->SetBranchStatus("mZ2_unconstrained",1); tree->SetBranchStatus("m4lerr_unconstrained",1); tree->SetBranchStatus("weight_corr",1); tree->SetBranchAddress("m4l_unconstrained",&m4l); tree->SetBranchAddress("mZ2_unconstrained",&m34); tree->SetBranchAddress("m4lerr_unconstrained",&m4lerr); tree->SetBranchAddress("weight_corr",&wgt); /////////////// // Build pdfs ////////////// RooRealVar* mass = new RooRealVar("m4l","mass",lorange,hirange,"GeV"); RooRealVar* mZ2 = new RooRealVar("m34","mZ2",10.,60.,"GeV"); RooRealVar merr("m4lerr","mass err",0.1,5.0,"GeV"); RooRealVar weight("weight","weight",0,10); float totalwgt(0); for (Int_t i=0; i<nevents; i++) { tree->GetEntry(i); totalwgt+=wgt; } std::cout<<"total weight = "<<totalwgt<<std::endl; ////////////////// // Create ND dataset ///////////////// RooDataSet signal("signal","signal",RooArgSet(*mass,*mZ2,merr,weight),"weight"); std::cout<<"Reading in events from signal minitree"<<std::endl; for (int i=0; i<nevents; i++) { tree->GetEntry(i); mass->setVal(m4l); mZ2->setVal(m34); merr.setVal(m4lerr); weight.setVal(wgt/totalwgt); signal.add(RooArgSet(*mass,*mZ2,merr),wgt/totalwgt); } // Create 1D kernel estimation for signal mass RooKeysPdf kestmass("kestmass","kestmass",*mass,signal); // Create histogram of 1D kernel estimation pdf for mass TH1* hm = kestmass.createHistogram("hm",*mass); // Create mass pdf from the 1D kernel histogram RooDataHist* dmass = new RooDataHist("dmass","dmass",RooArgSet(*mass),hm); RooHistPdf* masspdf = new RooHistPdf("masspdf","masspdf",*mass,*dmass,2); ////////////////////////////// // Construct plain likelihood ///////////////////////////// // construct unbinned likelihood RooAbsReal *nll = masspdf->createNLL(signal,NumCPU(2)); // Minimize likelihood w.r.t all parameters before making plots RooMinuit(*nll).migrad(); // Plot likelihood scan mass RooPlot* frame1 = mass->frame(Bins(10),Title("LL and profileLL in mass")); // Construct profile likelihood in mass RooAbsReal *pll_mass = nll->createProfile(*mass); // Plot the profile likelihood in mass pll_mass->plotOn(frame1,LineColor(kRed)); TCanvas *c = new TCanvas("c","c",800,600); gPad->SetLeftMargin(0.15); frame1->GetYaxis()->SetTitleOffset(1.4); frame1->Draw(); c->SaveAs("testProLL.png"); }