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 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; }
//## 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 RooDataSet *data = Pdf_data.generate(Mej,10000) ; RooFitResult* fitresult = Pdf_bkg.fitTo(*data,Extended(),Minos(kFALSE),Save(kTRUE)) ; // or //2) ## Fit the bkg histogram with the fit model //RooFitResult* fitresult = Pdf_bkg.fitTo(RDH_data,Extended(),Minos(kFALSE),Save(kTRUE)) ; //## Plot histo data and fit result together RooPlot* mesframe = Mej.frame(Name("Mejframe"),Title("Only bkg")) ; data->plotOn(Mejframe) ; //RDH_data.plotOn(Mejframe) ; modelFit.plotOn(Mejframe) ;
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; }