void upper_limit_Bayesian_MCMC(Model* model,double confidence,int Niters){ cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; cout<<"Calculating upper limit with the MCMC method"<<endl; cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; RooWorkspace* wspace = new RooWorkspace("wspace"); ModelConfig* modelConfig = new ModelConfig("bayes"); modelConfig->SetWorkspace(*wspace); modelConfig->SetPdf(*model->get_complete_likelihood()); modelConfig->SetPriorPdf(*model->get_POI_prior()); modelConfig->SetParametersOfInterest(*model->get_POI_set()); // modelConfig->SetNuisanceParameters(); modelConfig->SetNuisanceParameters(*model->get_nuisance_set()); //configure the calculator //model->Print(); cout<<" POI size "<<model->get_POI_set()->getSize()<<endl; RooRealVar* firstPOI = (RooRealVar*) modelConfig->GetParametersOfInterest()->first(); MCMCCalculator mcmccalc(*model->get_data(), *modelConfig ); mcmccalc.SetTestSize(1-confidence); mcmccalc.SetLeftSideTailFraction(0); mcmccalc.SetNumIters(Niters); MCMCInterval* interval = mcmccalc.GetInterval(); double ul = interval->UpperLimit(*firstPOI); cout<<"UpperLimit: "<<ul<<endl; }
double upper_limit_FC(Model* model,double confidence){ cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; cout<<"Calculating upper limit with the FC method"<<endl; cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; RooWorkspace* wspace = new RooWorkspace("wspace"); ModelConfig* modelConfig = new ModelConfig("FC"); modelConfig->SetWorkspace(*wspace); modelConfig->SetPdf(*model->get_complete_likelihood()); modelConfig->SetPriorPdf(*model->get_POI_prior()); modelConfig->SetParametersOfInterest(*model->get_POI_set()); //modelConfig->SetParametersOfInterest(*wspace->set("poi")); //modelConfig->SetNuisanceParameters(*wspace->set("nuis")); // modelConfig->SetNuisanceParameters(); modelConfig->SetNuisanceParameters(*model->get_nuisance_set()); RooDataSet* data = model->get_data(); RooArgSet* poi= model->get_POI_set(); //configure the calculator //model->Print(); cout<<" POI size "<<model->get_POI_set()->getSize()<<endl; // use FeldmaCousins (takes ~20 min) FeldmanCousins fc(*data, *modelConfig); fc.SetConfidenceLevel(0.95); //0.9 central limit=0.95 upper limit //fc.SetTestSize(.1); // set size of test //number counting: dataset always has 1 entry with N events observed fc.FluctuateNumDataEntries(false); fc.UseAdaptiveSampling(true); fc.SetNBins(200); PointSetInterval* fcInt = NULL; //ConfInterval* interval = 0; RooRealVar* firstPOI = (RooRealVar*) modelConfig->GetParametersOfInterest()->first(); // if(doFeldmanCousins){ // takes 7 minutes fcInt = (PointSetInterval*) fc.GetInterval(); // fix cast //xs} //interval = (PointSetInterval*) fc.GetInterval(); cout<<" ["<<fcInt->LowerLimit( *firstPOI ) << ", " << fcInt->UpperLimit( *firstPOI ) << "]" << endl; cout<<" ["<<fcInt->LowerLimit( *firstPOI ) << ", " << fcInt->UpperLimit( *firstPOI ) << "]" << endl; double ul=fcInt->UpperLimit( *firstPOI ); //double ul=interval->UpperLimit( *firstPOI ); return ul; }
void upper_limit_Bayesian(Model* model,double confidence){ cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; cout<<"Calculating upper limit with the Bayesian method"<<endl; cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl; RooWorkspace* wspace = new RooWorkspace("wspace"); ModelConfig* modelConfig = new ModelConfig("bayes"); modelConfig->SetWorkspace(*wspace); modelConfig->SetPdf(*model->get_complete_likelihood()); modelConfig->SetPriorPdf(*model->get_POI_prior()); modelConfig->SetParametersOfInterest(*model->get_POI_set()); //modelConfig->SetNuisanceParameters(*model->get_nuisance_set()); //configure the calculator //model->Print(); cout<<" POI size "<<model->get_POI_set()->getSize()<<endl; BayesianCalculator bcalc(*model->get_data(), *modelConfig); //BayesianCalculator bcalc(*model->get_data(),*model->get_complete_likelihood(),*model->get_POI_set(),*model->get_POI_prior(),model->get_nuisance_set()); //BayesianCalculator bcalc(*model->get_data(),*model->get_complete_likelihood(),*model->get_POI_set(),*model->get_POI_prior(),0); bcalc.SetLeftSideTailFraction(0); //for upper limit //get the interval bcalc.SetConfidenceLevel(confidence); cout<<"Calculating"<<endl; SimpleInterval* interval = bcalc.GetInterval(); double ul=interval->UpperLimit(); std::cout <<confidence <<"% CL upper limit: "<< ul<<endl; TCanvas *c1=new TCanvas; bcalc.SetScanOfPosterior(100); RooPlot * plot = bcalc.GetPosteriorPlot(); plot->Draw(); c1->SaveAs("bayesian_PosteriorPlot.png"); }
void StandardBayesianNumericalDemo(const char* infile = "", const char* workspaceName = "combined", const char* modelConfigName = "ModelConfig", const char* dataName = "obsData") { // option definitions double confLevel = optBayes.confLevel; TString integrationType = optBayes.integrationType; int nToys = optBayes.nToys; bool scanPosterior = optBayes.scanPosterior; int nScanPoints = optBayes.nScanPoints; int intervalType = optBayes.intervalType; int maxPOI = optBayes.maxPOI; double nSigmaNuisance = optBayes.nSigmaNuisance; ///////////////////////////////////////////////////////////// // First part is just to access a user-defined file // or create the standard example file if it doesn't exist //////////////////////////////////////////////////////////// const char* filename = ""; if (!strcmp(infile,"")) { filename = "results/example_combined_GaussExample_model.root"; bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code // if file does not exists generate with histfactory if (!fileExist) { #ifdef _WIN32 cout << "HistFactory file cannot be generated on Windows - exit" << endl; return; #endif // Normally this would be run on the command line cout <<"will run standard hist2workspace example"<<endl; gROOT->ProcessLine(".! prepareHistFactory ."); gROOT->ProcessLine(".! hist2workspace config/example.xml"); cout <<"\n\n---------------------"<<endl; cout <<"Done creating example input"<<endl; cout <<"---------------------\n\n"<<endl; } } else filename = infile; // Try to open the file TFile *file = TFile::Open(filename); // if input file was specified byt not found, quit if(!file ){ cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl; return; } ///////////////////////////////////////////////////////////// // Tutorial starts here //////////////////////////////////////////////////////////// // get the workspace out of the file RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName); if(!w){ cout <<"workspace not found" << endl; return; } // get the modelConfig out of the file ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName); // get the modelConfig out of the file RooAbsData* data = w->data(dataName); // make sure ingredients are found if(!data || !mc){ w->Print(); cout << "data or ModelConfig was not found" <<endl; return; } ///////////////////////////////////////////// // create and use the BayesianCalculator // to find and plot the 95% credible interval // on the parameter of interest as specified // in the model config // before we do that, we must specify our prior // it belongs in the model config, but it may not have // been specified RooUniform prior("prior","",*mc->GetParametersOfInterest()); w->import(prior); mc->SetPriorPdf(*w->pdf("prior")); // do without systematics //mc->SetNuisanceParameters(RooArgSet() ); if (nSigmaNuisance > 0) { RooAbsPdf * pdf = mc->GetPdf(); assert(pdf); RooFitResult * res = pdf->fitTo(*data, Save(true), Minimizer(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str()), Hesse(true), PrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1) ); res->Print(); RooArgList nuisPar(*mc->GetNuisanceParameters()); for (int i = 0; i < nuisPar.getSize(); ++i) { RooRealVar * v = dynamic_cast<RooRealVar*> (&nuisPar[i] ); assert( v); v->setMin( TMath::Max( v->getMin(), v->getVal() - nSigmaNuisance * v->getError() ) ); v->setMax( TMath::Min( v->getMax(), v->getVal() + nSigmaNuisance * v->getError() ) ); std::cout << "setting interval for nuisance " << v->GetName() << " : [ " << v->getMin() << " , " << v->getMax() << " ]" << std::endl; } } BayesianCalculator bayesianCalc(*data,*mc); bayesianCalc.SetConfidenceLevel(confLevel); // 95% interval // default of the calculator is central interval. here use shortest , central or upper limit depending on input // doing a shortest interval might require a longer time since it requires a scan of the posterior function if (intervalType == 0) bayesianCalc.SetShortestInterval(); // for shortest interval if (intervalType == 1) bayesianCalc.SetLeftSideTailFraction(0.5); // for central interval if (intervalType == 2) bayesianCalc.SetLeftSideTailFraction(0.); // for upper limit if (!integrationType.IsNull() ) { bayesianCalc.SetIntegrationType(integrationType); // set integrationType bayesianCalc.SetNumIters(nToys); // set number of ietrations (i.e. number of toys for MC integrations) } // in case of toyMC make a nnuisance pdf if (integrationType.Contains("TOYMC") ) { RooAbsPdf * nuisPdf = RooStats::MakeNuisancePdf(*mc, "nuisance_pdf"); cout << "using TOYMC integration: make nuisance pdf from the model " << std::endl; nuisPdf->Print(); bayesianCalc.ForceNuisancePdf(*nuisPdf); scanPosterior = true; // for ToyMC the posterior is scanned anyway so used given points } // compute interval by scanning the posterior function if (scanPosterior) bayesianCalc.SetScanOfPosterior(nScanPoints); RooRealVar* poi = (RooRealVar*) mc->GetParametersOfInterest()->first(); if (maxPOI != -999 && maxPOI > poi->getMin()) poi->setMax(maxPOI); SimpleInterval* interval = bayesianCalc.GetInterval(); // print out the iterval on the first Parameter of Interest cout << "\n>>>> RESULT : " << confLevel*100 << "% interval on " << poi->GetName()<<" is : ["<< interval->LowerLimit() << ", "<< interval->UpperLimit() <<"] "<<endl; // make a plot // since plotting may take a long time (it requires evaluating // the posterior in many points) this command will speed up // by reducing the number of points to plot - do 50 // ignore errors of PDF if is zero RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::Ignore) ; cout << "\nDrawing plot of posterior function....." << endl; // always plot using numer of scan points bayesianCalc.SetScanOfPosterior(nScanPoints); RooPlot * plot = bayesianCalc.GetPosteriorPlot(); plot->Draw(); }
/* * Prepares the workspace to be used by the hypothesis test calculator */ void workspace_preparer(char *signal_file_name, char *signal_hist_name_in_file, char *background_file_name, char *background_hist_name_in_file, char *data_file_name, char *data_hist_name_in_file, char *config_file) { // Include the config_reader class. TString path = gSystem->GetIncludePath(); path.Append(" -I/home/max/cern/cls/mario"); gSystem->SetIncludePath(path); gROOT->LoadMacro("config_reader.cxx"); // RooWorkspace used to store values. RooWorkspace * pWs = new RooWorkspace("ws"); // Create a config_reader (see source for details) to read the config // file. config_reader reader(config_file, pWs); // Read MR and RR bounds from the config file. double MR_lower = reader.find_double("MR_lower"); double MR_upper = reader.find_double("MR_upper"); double RR_lower = reader.find_double("RR_lower"); double RR_upper = reader.find_double("RR_upper"); double MR_initial = (MR_lower + MR_upper)/2; double RR_initial = (RR_lower + RR_upper)/2; // Define the Razor Variables RooRealVar MR = RooRealVar("MR", "MR", MR_initial, MR_lower, MR_upper); RooRealVar RR = RooRealVar("RSQ", "RSQ", RR_initial, RR_lower, RR_upper); // Argument lists RooArgList pdf_arg_list(MR, RR, "input_args_list"); RooArgSet pdf_arg_set(MR, RR, "input_pdf_args_set"); /***********************************************************************/ /* PART 1: IMPORTING SIGNAL AND BACKGROUND HISTOGRAMS */ /***********************************************************************/ /* * Get the signal's unextended pdf by converting the TH2D in the file * into a RooHistPdf */ TFile *signal_file = new TFile(signal_file_name); TH2D *signal_hist = (TH2D *)signal_file->Get(signal_hist_name_in_file); RooDataHist *signal_RooDataHist = new RooDataHist("signal_roodatahist", "signal_roodatahist", pdf_arg_list, signal_hist); RooHistPdf *unextended_sig_pdf = new RooHistPdf("unextended_sig_pdf", "unextended_sig_pdf", pdf_arg_set, *signal_RooDataHist); /* * Repeat this process for the background. */ TFile *background_file = new TFile(background_file_name); TH2D *background_hist = (TH2D *)background_file->Get(background_hist_name_in_file); RooDataHist *background_RooDataHist = new RooDataHist("background_roodatahist", "background_roodatahist", pdf_arg_list, background_hist); RooHistPdf *unextended_bkg_pdf = new RooHistPdf("unextended_bkg_pdf", "unextended_bkg_pdf", pdf_arg_set, *background_RooDataHist); /* * Now, we want to create the bprime variable, which represents the * integral over the background-only sample. We will perform the * integral automatically (that's why this is the only nuisance * parameter declared in this file - its value can be determined from * the input histograms). */ ostringstream bprime_string; ostringstream bprime_pdf_string; bprime_string << "bprime[" << background_hist->Integral() << ", 0, 999999999]"; bprime_pdf_string << "Poisson::bprime_pdf(bprime, " << background_hist->Integral() << ")"; pWs->factory(bprime_string.str().c_str()); pWs->factory(bprime_pdf_string.str().c_str()); /* * This simple command will create all values from the config file * with 'make:' at the beginning and a delimiter at the end (see config * _reader if you don't know what a delimiter is). In other * words, the luminosity, efficiency, transfer factors, and their pdfs * are created from this command. The declarations are contained in the * config file to be changed easily without having to modify this code. */ reader.factory_all(); /* * Now, we want to create the extended pdfs from the unextended pdfs, as * well as from the S and B values we manufactured in the config file. * S and B are the values by which the signal and background pdfs, * respectively, are extended. Recall that they were put in the * workspace in the reader.facotry_all() command. */ RooAbsReal *S = pWs->function("S"); RooAbsReal *B = pWs->function("B"); RooExtendPdf *signalpart = new RooExtendPdf("signalpart", "signalpart", *unextended_sig_pdf, *S); RooExtendPdf *backgroundpart = new RooExtendPdf("backgroundpart", "backgroundpart", *unextended_bkg_pdf, *B); RooArgList *pdf_list = new RooArgList(*signalpart, *backgroundpart, "list"); // Add the signal and background pdfs to make a TotalPdf RooAddPdf *TotalPdf = new RooAddPdf("TotalPdf", "TotalPdf", *pdf_list); RooArgList *pdf_prod_list = new RooArgList(*TotalPdf, *pWs->pdf("lumi_pdf"), *pWs->pdf("eff_pdf"), *pWs->pdf("rho_pdf"), *pWs->pdf("bprime_pdf")); // This creates the final model pdf. RooProdPdf *model = new RooProdPdf("model", "model", *pdf_prod_list); /* * Up until now, we have been using the workspace pWs to contain all of * our values. Now, all of our values that we require are in use in the * RooProdPdf called "model". So, we need to import "model" into a * RooWorkspace. To avoid recopying values into the rooworkspace, when * the values may already be present (which can cause problems), we will * simply create a new RooWorkspace to avoid confusion and problems. The * new RooWorkspace is created here. */ RooWorkspace *newworkspace = new RooWorkspace("newws"); newworkspace->import(*model); // Immediately delete pWs, so we don't accidentally use it again. delete pWs; // Show off the newworkspace newworkspace->Print(); // observables RooArgSet obs(*newworkspace->var("MR"), *newworkspace->var("RSQ"), "obs"); // global observables RooArgSet globalObs(*newworkspace->var("nom_lumi"), *newworkspace->var("nom_eff"), *newworkspace->var("nom_rho")); //fix global observables to their nominal values newworkspace->var("nom_lumi")->setConstant(); newworkspace->var("nom_eff")->setConstant(); newworkspace->var("nom_rho")->setConstant(); //Set Parameters of interest RooArgSet poi(*newworkspace->var("sigma"), "poi"); //Set Nuisnaces RooArgSet nuis(*newworkspace->var("prime_lumi"), *newworkspace->var("prime_eff"), *newworkspace->var("prime_rho"), *newworkspace->var("bprime")); // priors (for Bayesian calculation) newworkspace->factory("Uniform::prior_signal(sigma)"); // for parameter of interest newworkspace->factory("Uniform::prior_bg_b(bprime)"); // for data driven nuisance parameter newworkspace->factory("PROD::prior(prior_signal,prior_bg_b)"); // total prior //Observed data is pulled from histogram. //TFile *data_file = new TFile(data_file_name); TFile *data_file = new TFile(data_file_name); TH2D *data_hist = (TH2D *)data_file->Get(data_hist_name_in_file); RooDataHist *pData = new RooDataHist("data", "data", obs, data_hist); newworkspace->import(*pData); // Now, we will draw our data from a RooDataHist. /*TFile *data_file = new TFile(data_file_name); TTree *data_tree = (TTree *) data_file->Get(data_hist_name_in_file); RooDataSet *pData = new RooDataSet("data", "data", data_tree, obs); newworkspace->import(*pData);*/ // Craft the signal+background model ModelConfig * pSbModel = new ModelConfig("SbModel"); pSbModel->SetWorkspace(*newworkspace); pSbModel->SetPdf(*newworkspace->pdf("model")); pSbModel->SetPriorPdf(*newworkspace->pdf("prior")); pSbModel->SetParametersOfInterest(poi); pSbModel->SetNuisanceParameters(nuis); pSbModel->SetObservables(obs); pSbModel->SetGlobalObservables(globalObs); // set all but obs, poi and nuisance to const SetConstants(newworkspace, pSbModel); newworkspace->import(*pSbModel); // background-only model // use the same PDF as s+b, with sig=0 // POI value under the background hypothesis // (We will set the value to 0 later) Double_t poiValueForBModel = 0.0; ModelConfig* pBModel = new ModelConfig(*(RooStats::ModelConfig *)newworkspace->obj("SbModel")); pBModel->SetName("BModel"); pBModel->SetWorkspace(*newworkspace); newworkspace->import(*pBModel); // find global maximum with the signal+background model // with conditional MLEs for nuisance parameters // and save the parameter point snapshot in the Workspace // - safer to keep a default name because some RooStats calculators // will anticipate it RooAbsReal * pNll = pSbModel->GetPdf()->createNLL(*pData); RooAbsReal * pProfile = pNll->createProfile(RooArgSet()); pProfile->getVal(); // this will do fit and set POI and nuisance parameters to fitted values RooArgSet * pPoiAndNuisance = new RooArgSet(); if(pSbModel->GetNuisanceParameters()) pPoiAndNuisance->add(*pSbModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pSbModel->GetParametersOfInterest()); cout << "\nWill save these parameter points that correspond to the fit to data" << endl; pPoiAndNuisance->Print("v"); pSbModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; // Find a parameter point for generating pseudo-data // with the background-only data. // Save the parameter point snapshot in the Workspace pNll = pBModel->GetPdf()->createNLL(*pData); pProfile = pNll->createProfile(poi); ((RooRealVar *)poi.first())->setVal(poiValueForBModel); pProfile->getVal(); // this will do fit and set nuisance parameters to profiled values pPoiAndNuisance = new RooArgSet(); if(pBModel->GetNuisanceParameters()) pPoiAndNuisance->add(*pBModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pBModel->GetParametersOfInterest()); cout << "\nShould use these parameter points to generate pseudo data for bkg only" << endl; pPoiAndNuisance->Print("v"); pBModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; // save workspace to file newworkspace->writeToFile("ws_twobin.root"); // clean up delete newworkspace; delete pData; delete pSbModel; delete pBModel; } // ----- end of tutorial ----------------------------------------
// implementation void TwoBinInstructional( void ){ // let's time this example TStopwatch t; t.Start(); // set RooFit random seed for reproducible results RooRandom::randomGenerator()->SetSeed(4357); // make model RooWorkspace * pWs = new RooWorkspace("ws"); // derived from data pWs->factory("xsec[0.2,0,2]"); // POI pWs->factory("bg_b[10,0,50]"); // data driven nuisance // predefined nuisances pWs->factory("lumi[100,0,1000]"); pWs->factory("eff_a[0.2,0,1]"); pWs->factory("eff_b[0.05,0,1]"); pWs->factory("tau[0,1]"); pWs->factory("xsec_bg_a[0.05]"); // constant pWs->var("xsec_bg_a")->setConstant(1); // channel a (signal): lumi*xsec*eff_a + lumi*bg_a + tau*bg_b pWs->factory("prod::sig_a(lumi,xsec,eff_a)"); pWs->factory("prod::bg_a(lumi,xsec_bg_a)"); pWs->factory("prod::tau_bg_b(tau, bg_b)"); pWs->factory("Poisson::pdf_a(na[14,0,100],sum::mu_a(sig_a,bg_a,tau_bg_b))"); // channel b (control): lumi*xsec*eff_b + bg_b pWs->factory("prod::sig_b(lumi,xsec,eff_b)"); pWs->factory("Poisson::pdf_b(nb[11,0,100],sum::mu_b(sig_b,bg_b))"); // nuisance constraint terms (systematics) pWs->factory("Lognormal::l_lumi(lumi,nom_lumi[100,0,1000],sum::kappa_lumi(1,d_lumi[0.1]))"); pWs->factory("Lognormal::l_eff_a(eff_a,nom_eff_a[0.20,0,1],sum::kappa_eff_a(1,d_eff_a[0.05]))"); pWs->factory("Lognormal::l_eff_b(eff_b,nom_eff_b[0.05,0,1],sum::kappa_eff_b(1,d_eff_b[0.05]))"); pWs->factory("Lognormal::l_tau(tau,nom_tau[0.50,0,1],sum::kappa_tau(1,d_tau[0.05]))"); //pWs->factory("Lognormal::l_bg_a(bg_a,nom_bg_a[0.05,0,1],sum::kappa_bg_a(1,d_bg_a[0.10]))"); // complete model PDF pWs->factory("PROD::model(pdf_a,pdf_b,l_lumi,l_eff_a,l_eff_b,l_tau)"); // Now create sets of variables. Note that we could use the factory to // create sets but in that case many of the sets would be duplicated // when the ModelConfig objects are imported into the workspace. So, // we create the sets outside the workspace, and only the needed ones // will be automatically imported by ModelConfigs // observables RooArgSet obs(*pWs->var("na"), *pWs->var("nb"), "obs"); // global observables RooArgSet globalObs(*pWs->var("nom_lumi"), *pWs->var("nom_eff_a"), *pWs->var("nom_eff_b"), *pWs->var("nom_tau"), "global_obs"); // parameters of interest RooArgSet poi(*pWs->var("xsec"), "poi"); // nuisance parameters RooArgSet nuis(*pWs->var("lumi"), *pWs->var("eff_a"), *pWs->var("eff_b"), *pWs->var("tau"), "nuis"); // priors (for Bayesian calculation) pWs->factory("Uniform::prior_xsec(xsec)"); // for parameter of interest pWs->factory("Uniform::prior_bg_b(bg_b)"); // for data driven nuisance parameter pWs->factory("PROD::prior(prior_xsec,prior_bg_b)"); // total prior // create data pWs->var("na")->setVal(14); pWs->var("nb")->setVal(11); RooDataSet * pData = new RooDataSet("data","",obs); pData->add(obs); pWs->import(*pData); //pData->Print(); // signal+background model ModelConfig * pSbModel = new ModelConfig("SbModel"); pSbModel->SetWorkspace(*pWs); pSbModel->SetPdf(*pWs->pdf("model")); pSbModel->SetPriorPdf(*pWs->pdf("prior")); pSbModel->SetParametersOfInterest(poi); pSbModel->SetNuisanceParameters(nuis); pSbModel->SetObservables(obs); pSbModel->SetGlobalObservables(globalObs); // set all but obs, poi and nuisance to const SetConstants(pWs, pSbModel); pWs->import(*pSbModel); // background-only model // use the same PDF as s+b, with xsec=0 // POI value under the background hypothesis Double_t poiValueForBModel = 0.0; ModelConfig* pBModel = new ModelConfig(*(RooStats::ModelConfig *)pWs->obj("SbModel")); pBModel->SetName("BModel"); pBModel->SetWorkspace(*pWs); pWs->import(*pBModel); // find global maximum with the signal+background model // with conditional MLEs for nuisance parameters // and save the parameter point snapshot in the Workspace // - safer to keep a default name because some RooStats calculators // will anticipate it RooAbsReal * pNll = pSbModel->GetPdf()->createNLL(*pData); RooAbsReal * pProfile = pNll->createProfile(RooArgSet()); pProfile->getVal(); // this will do fit and set POI and nuisance parameters to fitted values RooArgSet * pPoiAndNuisance = new RooArgSet(); if(pSbModel->GetNuisanceParameters()) pPoiAndNuisance->add(*pSbModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pSbModel->GetParametersOfInterest()); cout << "\nWill save these parameter points that correspond to the fit to data" << endl; pPoiAndNuisance->Print("v"); pSbModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; // Find a parameter point for generating pseudo-data // with the background-only data. // Save the parameter point snapshot in the Workspace pNll = pBModel->GetPdf()->createNLL(*pData); pProfile = pNll->createProfile(poi); ((RooRealVar *)poi.first())->setVal(poiValueForBModel); pProfile->getVal(); // this will do fit and set nuisance parameters to profiled values pPoiAndNuisance = new RooArgSet(); if(pBModel->GetNuisanceParameters()) pPoiAndNuisance->add(*pBModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pBModel->GetParametersOfInterest()); cout << "\nShould use these parameter points to generate pseudo data for bkg only" << endl; pPoiAndNuisance->Print("v"); pBModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; // inspect workspace pWs->Print(); // save workspace to file pWs->writeToFile("ws_twobin.root"); // clean up delete pWs; delete pData; delete pSbModel; delete pBModel; } // ----- end of tutorial ----------------------------------------
void IntervalExamples() { // Time this macro TStopwatch t; t.Start(); // set RooFit random seed for reproducible results RooRandom::randomGenerator()->SetSeed(3001); // make a simple model via the workspace factory RooWorkspace* wspace = new RooWorkspace(); wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])"); wspace->defineSet("poi","mu"); wspace->defineSet("obs","x"); // specify components of model for statistical tools ModelConfig* modelConfig = new ModelConfig("Example G(x|mu,1)"); modelConfig->SetWorkspace(*wspace); modelConfig->SetPdf( *wspace->pdf("normal") ); modelConfig->SetParametersOfInterest( *wspace->set("poi") ); modelConfig->SetObservables( *wspace->set("obs") ); // create a toy dataset RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100); data->Print(); // for convenience later on RooRealVar* x = wspace->var("x"); RooRealVar* mu = wspace->var("mu"); // set confidence level double confidenceLevel = 0.95; // example use profile likelihood calculator ProfileLikelihoodCalculator plc(*data, *modelConfig); plc.SetConfidenceLevel( confidenceLevel); LikelihoodInterval* plInt = plc.GetInterval(); // example use of Feldman-Cousins FeldmanCousins fc(*data, *modelConfig); fc.SetConfidenceLevel( confidenceLevel); fc.SetNBins(100); // number of points to test per parameter fc.UseAdaptiveSampling(true); // make it go faster // Here, we consider only ensembles with 100 events // The PDF could be extended and this could be removed fc.FluctuateNumDataEntries(false); // Proof // ProofConfig pc(*wspace, 4, "workers=4", kFALSE); // proof-lite //ProofConfig pc(w, 8, "localhost"); // proof cluster at "localhost" // ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler(); // toymcsampler->SetProofConfig(&pc); // enable proof PointSetInterval* interval = (PointSetInterval*) fc.GetInterval(); // example use of BayesianCalculator // now we also need to specify a prior in the ModelConfig wspace->factory("Uniform::prior(mu)"); modelConfig->SetPriorPdf(*wspace->pdf("prior")); // example usage of BayesianCalculator BayesianCalculator bc(*data, *modelConfig); bc.SetConfidenceLevel( confidenceLevel); SimpleInterval* bcInt = bc.GetInterval(); // example use of MCMCInterval MCMCCalculator mc(*data, *modelConfig); mc.SetConfidenceLevel( confidenceLevel); // special options mc.SetNumBins(200); // bins used internally for representing posterior mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in mc.SetNumIters(100000); // how long to run chain mc.SetLeftSideTailFraction(0.5); // for central interval MCMCInterval* mcInt = mc.GetInterval(); // for this example we know the expected intervals double expectedLL = data->mean(*x) + ROOT::Math::normal_quantile( (1-confidenceLevel)/2,1) / sqrt(data->numEntries()); double expectedUL = data->mean(*x) + ROOT::Math::normal_quantile_c((1-confidenceLevel)/2,1) / sqrt(data->numEntries()) ; // Use the intervals std::cout << "expected interval is [" << expectedLL << ", " << expectedUL << "]" << endl; cout << "plc interval is [" << plInt->LowerLimit(*mu) << ", " << plInt->UpperLimit(*mu) << "]" << endl; std::cout << "fc interval is ["<< interval->LowerLimit(*mu) << " , " << interval->UpperLimit(*mu) << "]" << endl; cout << "bc interval is [" << bcInt->LowerLimit() << ", " << bcInt->UpperLimit() << "]" << endl; cout << "mc interval is [" << mcInt->LowerLimit(*mu) << ", " << mcInt->UpperLimit(*mu) << "]" << endl; mu->setVal(0); cout << "is mu=0 in the interval? " << plInt->IsInInterval(RooArgSet(*mu)) << endl; // make a reasonable style gStyle->SetCanvasColor(0); gStyle->SetCanvasBorderMode(0); gStyle->SetPadBorderMode(0); gStyle->SetPadColor(0); gStyle->SetCanvasColor(0); gStyle->SetTitleFillColor(0); gStyle->SetFillColor(0); gStyle->SetFrameFillColor(0); gStyle->SetStatColor(0); // some plots TCanvas* canvas = new TCanvas("canvas"); canvas->Divide(2,2); // plot the data canvas->cd(1); RooPlot* frame = x->frame(); data->plotOn(frame); data->statOn(frame); frame->Draw(); // plot the profile likelihood canvas->cd(2); LikelihoodIntervalPlot plot(plInt); plot.Draw(); // plot the MCMC interval canvas->cd(3); MCMCIntervalPlot* mcPlot = new MCMCIntervalPlot(*mcInt); mcPlot->SetLineColor(kGreen); mcPlot->SetLineWidth(2); mcPlot->Draw(); canvas->cd(4); RooPlot * bcPlot = bc.GetPosteriorPlot(); bcPlot->Draw(); canvas->Update(); t.Stop(); t.Print(); }
void StandardBayesianNumericalDemo(const char* infile = "", const char* workspaceName = "combined", const char* modelConfigName = "ModelConfig", const char* dataName = "obsData"){ ///////////////////////////////////////////////////////////// // First part is just to access a user-defined file // or create the standard example file if it doesn't exist //////////////////////////////////////////////////////////// TString filename = infile; if (filename.IsNull()) { filename = "results/example_combined_GaussExample_model.root"; std::cout << "Use standard file generated with HistFactory : " << filename << std::endl; } // Check if example input file exists TFile *file = TFile::Open(filename); // if input file was specified but not found, quit if(!file && !TString(infile).IsNull()){ cout <<"file " << filename << " not found" << endl; return; } // if default file not found, try to create it if(!file ){ // Normally this would be run on the command line cout <<"will run standard hist2workspace example"<<endl; gROOT->ProcessLine(".! prepareHistFactory ."); gROOT->ProcessLine(".! hist2workspace config/example.xml"); cout <<"\n\n---------------------"<<endl; cout <<"Done creating example input"<<endl; cout <<"---------------------\n\n"<<endl; // now try to access the file again file = TFile::Open(filename); } if(!file){ // if it is still not there, then we can't continue cout << "Not able to run hist2workspace to create example input" <<endl; return; } ///////////////////////////////////////////////////////////// // Tutorial starts here //////////////////////////////////////////////////////////// // get the workspace out of the file RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName); if(!w){ cout <<"workspace not found" << endl; return; } // get the modelConfig out of the file ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName); // get the modelConfig out of the file RooAbsData* data = w->data(dataName); // make sure ingredients are found if(!data || !mc){ w->Print(); cout << "data or ModelConfig was not found" <<endl; return; } ///////////////////////////////////////////// // create and use the BayesianCalculator // to find and plot the 95% credible interval // on the parameter of interest as specified // in the model config // before we do that, we must specify our prior // it belongs in the model config, but it may not have // been specified RooUniform prior("prior","",*mc->GetParametersOfInterest()); w->import(prior); mc->SetPriorPdf(*w->pdf("prior")); // do without systematics //mc->SetNuisanceParameters(RooArgSet() ); BayesianCalculator bayesianCalc(*data,*mc); bayesianCalc.SetConfidenceLevel(0.95); // 95% interval // default of the calculator is central interval. here use shortest , central or upper limit depending on input // doing a shortest interval might require a longer time since it requires a scan of the posterior function if (intervalType == 0) bayesianCalc.SetShortestInterval(); // for shortest interval if (intervalType == 1) bayesianCalc.SetLeftSideTailFraction(0.5); // for central interval if (intervalType == 2) bayesianCalc.SetLeftSideTailFraction(0.); // for upper limit if (!integrationType.IsNull() ) { bayesianCalc.SetIntegrationType(integrationType); // set integrationType bayesianCalc.SetNumIters(nToys); // set number of ietrations (i.e. number of toys for MC integrations) } // compute interval by scanning the posterior function if (scanPosterior) bayesianCalc.SetScanOfPosterior(nScanPoints); SimpleInterval* interval = bayesianCalc.GetInterval(); // print out the iterval on the first Parameter of Interest RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first(); cout << "\n95% interval on " <<firstPOI->GetName()<<" is : ["<< interval->LowerLimit() << ", "<< interval->UpperLimit() <<"] "<<endl; // make a plot // since plotting may take a long time (it requires evaluating // the posterior in many points) this command will speed up // by reducing the number of points to plot - do 50 cout << "\nDrawing plot of posterior function....." << endl; bayesianCalc.SetScanOfPosterior(nScanPoints); RooPlot * plot = bayesianCalc.GetPosteriorPlot(); plot->Draw(); }
void new_RA4(){ // let's time this challenging example TStopwatch t; t.Start(); // set RooFit random seed for reproducible results RooRandom::randomGenerator()->SetSeed(4357); // make model RooWorkspace* wspace = new RooWorkspace("wspace"); wspace->factory("Gaussian::sigCons(prime_SigEff[0,-5,5], nom_SigEff[0,-5,5], 1)"); wspace->factory("expr::SigEff('1.0*pow(1.20,@0)',prime_SigEff)"); // // 1+-20%, 1.20=exp(20%) wspace->factory("Poisson::on(non[0,50], sum::splusb(prod::SigUnc(s[0,0,50],SigEff),mainb[8.8,0,50],dilep[0.9,0,20],tau[2.3,0,20],QCD[0.,0,10],MC[0.1,0,4]))"); wspace->factory("Gaussian::mcCons(prime_rho[0,-5,5], nom_rho[0,-5,5], 1)"); wspace->factory("expr::rho('1.0*pow(1.39,@0)',prime_rho)"); // // 1+-39% wspace->factory("Poisson::off(noff[0,200], prod::rhob(mainb,rho,mu_plus_e[0.74,0.01,10],1.08))"); wspace->factory("Gaussian::mcCons2(mu_plus_enom[0.74,0.01,4], mu_plus_e, sigmatwo[.05])"); wspace->factory("Gaussian::dilep_pred(dilep_nom[0.9,0,20], dilep, sigma3[2.2])"); wspace->factory("Gaussian::tau_pred(tau_nom[2.3,0,20], tau, sigma4[0.5])"); wspace->factory("Gaussian::QCD_pred(QCD_nom[0.0,0,10], QCD, sigma5[1.0])"); wspace->factory("Gaussian::MC_pred(MC_nom[0.1,0.01,4], MC, sigma7[0.14])"); wspace->factory("PROD::model(on,off,mcCons,mcCons2,sigCons,dilep_pred,tau_pred,QCD_pred,MC_pred)"); RooArgSet obs(*wspace->var("non"), *wspace->var("noff"), *wspace->var("mu_plus_enom"), *wspace->var("dilep_nom"), *wspace->var("tau_nom"), "obs"); obs.add(*wspace->var("QCD_nom")); obs.add(*wspace->var("MC_nom")); RooArgSet globalObs(*wspace->var("nom_SigEff"), *wspace->var("nom_rho"), "global_obs"); // fix global observables to their nominal values wspace->var("nom_SigEff")->setConstant(); wspace->var("nom_rho")->setConstant(); RooArgSet poi(*wspace->var("s"), "poi"); RooArgSet nuis(*wspace->var("mainb"), *wspace->var("prime_rho"), *wspace->var("prime_SigEff"), *wspace->var("mu_plus_e"), *wspace->var("dilep"), *wspace->var("tau"), "nuis"); nuis.add(*wspace->var("QCD")); nuis.add(*wspace->var("MC")); wspace->factory("Uniform::prior_poi({s})"); wspace->factory("Uniform::prior_nuis({mainb,mu_plus_e,dilep,tau,QCD,MC})"); wspace->factory("PROD::prior(prior_poi,prior_nuis)"); wspace->var("non")->setVal(8); //observed //wspace->var("non")->setVal(12); //expected observation wspace->var("noff")->setVal(7); //observed events in control region wspace->var("mu_plus_enom")->setVal(0.74); wspace->var("dilep_nom")->setVal(0.9); wspace->var("tau_nom")->setVal(2.3); wspace->var("QCD")->setVal(0.0); wspace->var("MC")->setVal(0.1); RooDataSet * data = new RooDataSet("data","",obs); data->add(obs); wspace->import(*data); ///////////////////////////////////////////////////// // Now the statistical tests // model config ModelConfig* pSbModel = new ModelConfig("SbModel"); pSbModel->SetWorkspace(*wspace); pSbModel->SetPdf(*wspace->pdf("model")); pSbModel->SetPriorPdf(*wspace->pdf("prior")); pSbModel->SetParametersOfInterest(poi); pSbModel->SetNuisanceParameters(nuis); pSbModel->SetObservables(obs); pSbModel->SetGlobalObservables(globalObs); wspace->import(*pSbModel); // set all but obs, poi and nuisance to const SetConstants(wspace, pSbModel); wspace->import(*pSbModel); Double_t poiValueForBModel = 0.0; ModelConfig* pBModel = new ModelConfig(*(RooStats::ModelConfig *)wspace->obj("SbModel")); pBModel->SetName("BModel"); pBModel->SetWorkspace(*wspace); wspace->import(*pBModel); RooAbsReal * pNll = pSbModel->GetPdf()->createNLL(*data); RooAbsReal * pProfile = pNll->createProfile(RooArgSet()); pProfile->getVal(); // this will do fit and set POI and nuisance parameters to fitted values RooArgSet * pPoiAndNuisance = new RooArgSet(); //if(pSbModel->GetNuisanceParameters()) // pPoiAndNuisance->add(*pSbModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pSbModel->GetParametersOfInterest()); cout << "\nWill save these parameter points that correspond to the fit to data" << endl; pPoiAndNuisance->Print("v"); pSbModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; pNll = pBModel->GetPdf()->createNLL(*data); pProfile = pNll->createProfile(poi); ((RooRealVar *)poi.first())->setVal(poiValueForBModel); pProfile->getVal(); // this will do fit and set nuisance parameters to profiled values pPoiAndNuisance = new RooArgSet(); //if(pBModel->GetNuisanceParameters()) // pPoiAndNuisance->add(*pBModel->GetNuisanceParameters()); pPoiAndNuisance->add(*pBModel->GetParametersOfInterest()); cout << "\nShould use these parameter points to generate pseudo data for bkg only" << endl; pPoiAndNuisance->Print("v"); pBModel->SetSnapshot(*pPoiAndNuisance); delete pProfile; delete pNll; delete pPoiAndNuisance; // inspect workspace wspace->Print(); // save workspace to file wspace->writeToFile("tight.root"); //wspace->writeToFile("tight_median.root"); // clean up delete wspace; delete data; delete pSbModel; delete pBModel; }
void build_hbb_workspace1( const char* infile = "outputfiles/input-file.txt", const char* outfile = "outputfiles/ws.root" ) { //------------------------------------------------------------------------- //-- Create workspace and other RooStats things. printf("\n\n Creating workspace.\n\n") ; RooWorkspace workspace("ws") ; workspace.autoImportClassCode(true) ; globalObservables = new RooArgSet("globalObservables"); allNuisances = new RooArgSet("allNuisances"); allNuisancePdfs = new RooArgSet("allNuisancePdfs"); RooArgSet* observedParametersList = new RooArgSet("observables") ; //------------------------------------------------------------------------- printf("\n\n Reading input file: %s\n\n", infile ) ; float fileVal ; char pname[1000] ; char formula[1000] ; sprintf( pname, "bins_of_met" ) ; if ( !getFileValue( infile, pname, fileVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } int bins_of_met = TMath::Nint( fileVal ) ; //-- save bins_of_met in the workspace for convenience. RooRealVar bom( "bins_of_met", "bins_of_met", bins_of_met, 0., 1000. ) ; bom.setConstant(kTRUE) ; workspace.import(bom) ; //-- save bins_of_nb in the workspace for convenience. RooRealVar bonb( "bins_of_nb", "bins_of_nb", bins_of_nb, 0., 1000. ) ; bonb.setConstant(kTRUE) ; workspace.import(bonb) ; RooRealVar* rv_N_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooRealVar* rv_N_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooRealVar* rv_smc_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooRealVar* rv_smc_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_Rsigsb_corr[bins_of_nb][max_bins_of_met] ; for ( int nbi=0; nbi<bins_of_nb; nbi++ ) { for ( int mbi=0; mbi<bins_of_met; mbi++ ) { sprintf( pname, "N_%db_msig_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, fileVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } rv_N_msig[nbi][mbi] = new RooRealVar( pname, pname, 0., 1.e6 ) ; rv_N_msig[nbi][mbi] -> setVal( TMath::Nint(fileVal) ) ; rv_N_msig[nbi][mbi] -> setConstant( kTRUE ) ; observedParametersList -> add( *rv_N_msig[nbi][mbi] ) ; sprintf( pname, "N_%db_msb_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, fileVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } rv_N_msb[nbi][mbi] = new RooRealVar( pname, pname, 0., 1.e6 ) ; rv_N_msb[nbi][mbi] -> setVal( TMath::Nint(fileVal) ) ; rv_N_msb[nbi][mbi] -> setConstant( kTRUE ) ; observedParametersList -> add( *rv_N_msb[nbi][mbi] ) ; sprintf( pname, "smc_%db_msig_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, fileVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } rv_smc_msig[nbi][mbi] = new RooRealVar( pname, pname, 0., 1.e6 ) ; rv_smc_msig[nbi][mbi] -> setVal( TMath::Nint(fileVal) ) ; rv_smc_msig[nbi][mbi] -> setConstant( kTRUE ) ; sprintf( pname, "smc_%db_msb_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, fileVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } rv_smc_msb[nbi][mbi] = new RooRealVar( pname, pname, 0., 1.e6 ) ; rv_smc_msb[nbi][mbi] -> setVal( TMath::Nint(fileVal) ) ; rv_smc_msb[nbi][mbi] -> setConstant( kTRUE ) ; float corrVal, corrSyst ; sprintf( pname, "Rsigsb_syst_%db_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, corrSyst ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } sprintf( pname, "Rsigsb_corr_%db_met%d", nbi+2, mbi+1 ) ; if ( !getFileValue( infile, pname, corrVal ) ) { printf("\n\n *** Error. Can't find %s\n\n", pname ) ; return ; } rv_Rsigsb_corr[nbi][mbi] = makeLognormalConstraint( pname, corrVal, corrSyst ) ; } // mbi. } // nbi. //-- Finished reading input from file. //------------------------------------------------------------------------- printf("\n\n Creating and importing dataset into workspace.\n\n") ; RooDataSet* dsObserved = new RooDataSet("hbb_observed_rds", "hbb observed data values", *observedParametersList ) ; dsObserved -> add( *observedParametersList ) ; workspace.import( *dsObserved ) ; //------------------------------------------------------------------------- //-- Define all floats. printf("\n\n Defining all unconstrained floats (Ratios, signal strength).\n\n") ; double R_msigmsb_initialval(0.15) ; RooRealVar* rv_R_msigmsb[50] ; for ( int mbi=0; mbi<bins_of_met; mbi++ ) { sprintf( pname, "R_msigmsb_met%d", mbi+1 ) ; printf( " %s\n", pname ) ; rv_R_msigmsb[mbi] = new RooRealVar( pname, pname, R_msigmsb_initialval, 0., 3. ) ; rv_R_msigmsb[mbi] -> setConstant( kFALSE ) ; rv_R_msigmsb[mbi] -> Print() ; } // mbi. printf("\n") ; sprintf( pname, "sig_strength" ) ; RooRealVar* rv_sig_strength = new RooRealVar( pname, pname, 1.0, 0., 10. ) ; rv_sig_strength -> setConstant(kFALSE) ; rv_sig_strength -> Print() ; printf(" %s\n\n", pname ) ; //------------------------------------------------------------------------- //-- Define all mu parameters. printf("\n\n Defining mu parameters.\n\n") ; RooAbsReal* rv_mu_bg_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_mu_bg_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_mu_sig_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_mu_sig_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. for ( int nbi=0; nbi<bins_of_nb; nbi++ ) { for ( int mbi=0; mbi<bins_of_met; mbi++ ) { sprintf( pname, "mu_bg_%db_msb_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_mu_bg_msb[nbi][mbi] = new RooRealVar( pname, pname, rv_N_msb[nbi][mbi] -> getVal(), 0., 1.e6 ) ; rv_mu_bg_msb[nbi][mbi] -> Print() ; sprintf( formula, "@0 * @1 * @2" ) ; sprintf( pname, "mu_bg_%db_msig_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_mu_bg_msig[nbi][mbi] = new RooFormulaVar( pname, formula, RooArgSet( *rv_Rsigsb_corr[nbi][mbi], *rv_R_msigmsb[mbi], *rv_mu_bg_msb[nbi][mbi] ) ) ; rv_mu_bg_msig[nbi][mbi] -> Print() ; sprintf( formula, "@0 * @1" ) ; sprintf( pname, "mu_sig_%db_msig_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_mu_sig_msig[nbi][mbi] = new RooFormulaVar( pname, formula, RooArgSet( *rv_sig_strength, *rv_smc_msig[nbi][mbi] ) ) ; rv_mu_sig_msig[nbi][mbi] -> Print() ; sprintf( formula, "@0 * @1" ) ; sprintf( pname, "mu_sig_%db_msb_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_mu_sig_msb[nbi][mbi] = new RooFormulaVar( pname, formula, RooArgSet( *rv_sig_strength, *rv_smc_msb[nbi][mbi] ) ) ; rv_mu_sig_msb[nbi][mbi] -> Print() ; } // mbi. } // nbi. //-- Finished defining mu parameters. //------------------------------------------------------------------------- //-- Defining small n's printf("\n\n Defining small n's.\n\n") ; RooAbsReal* rv_n_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_n_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. for ( int nbi=0; nbi<bins_of_nb; nbi++ ) { for ( int mbi=0; mbi<bins_of_met; mbi++ ) { sprintf( formula, "@0 + @1" ) ; sprintf( pname, "n_%db_msig_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_n_msig[nbi][mbi] = new RooFormulaVar( pname, formula, RooArgSet( *rv_mu_sig_msig[nbi][mbi], *rv_mu_bg_msig[nbi][mbi] ) ) ; rv_n_msig[nbi][mbi] -> Print() ; workspace.import( *rv_n_msig[nbi][mbi] ) ; sprintf( pname, "n_%db_msb_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_n_msb[nbi][mbi] = new RooFormulaVar( pname, formula, RooArgSet( *rv_mu_sig_msb[nbi][mbi], *rv_mu_bg_msb[nbi][mbi] ) ) ; rv_n_msb[nbi][mbi] -> Print() ; workspace.import( *rv_n_msb[nbi][mbi] ) ; } // mbi. } // nbi. //------------------------------------------------------------------------- //-- Define the Poisson pdfs for the observables. printf("\n\n Defining Poisson pdfs for the observables.\n\n") ; RooAbsReal* rv_pdf_msig[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooAbsReal* rv_pdf_msb[bins_of_nb][max_bins_of_met] ; // first index is number of btags, second is met bin. RooArgSet pdflist ; for ( int nbi=0; nbi<bins_of_nb; nbi++ ) { for ( int mbi=0; mbi<bins_of_met; mbi++ ) { sprintf( pname, "pdf_%db_msig_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_pdf_msig[nbi][mbi] = new RooPoisson( pname, pname, *rv_N_msig[nbi][mbi], *rv_n_msig[nbi][mbi] ) ; rv_pdf_msig[nbi][mbi] -> Print() ; pdflist.add( *rv_pdf_msig[nbi][mbi] ) ; sprintf( pname, "pdf_%db_msb_met%d", nbi+2, mbi+1 ) ; printf( " %s\n", pname ) ; rv_pdf_msb[nbi][mbi] = new RooPoisson( pname, pname, *rv_N_msb[nbi][mbi], *rv_n_msb[nbi][mbi] ) ; rv_pdf_msb[nbi][mbi] -> Print() ; pdflist.add( *rv_pdf_msb[nbi][mbi] ) ; } // mbi. } // nbi. //------------------------------------------------------------------------- //-- Build the likelihood. printf("\n\n Building the likelihood.\n\n") ; pdflist.add( *allNuisancePdfs ) ; pdflist.Print() ; printf("\n") ; RooProdPdf* likelihood = new RooProdPdf( "likelihood", "hbb likelihood", pdflist ) ; likelihood->Print() ; //------------------------------------------------------------------------- // printf("\n\n Running a test fit.\n\n") ; // dsObserved -> Print() ; // dsObserved -> printMultiline(cout, 1, kTRUE, "") ; // printf("\n\n =============================================\n\n") ; // likelihood -> fitTo( *dsObserved, PrintLevel(3), Hesse(0), Minos(0) ) ; // printf("\n\n =============================================\n\n") ; //-- Set up RooStats models. printf("\n\n Setting up S+B model.\n\n") ; RooArgSet poi( *rv_sig_strength, "poi" ) ; RooUniform signal_prior( "signal_prior", "signal_prior", *rv_sig_strength ) ; ModelConfig sbModel ("SbModel"); sbModel.SetWorkspace( workspace ) ; sbModel.SetPdf( *likelihood ) ; sbModel.SetParametersOfInterest( poi ); sbModel.SetPriorPdf(signal_prior); sbModel.SetObservables( *observedParametersList ); sbModel.SetNuisanceParameters( *allNuisances ); sbModel.SetGlobalObservables( *globalObservables ); workspace.Print() ; printf("\n\n Doing fit for S+B model.\n" ) ; fflush(stdout) ; RooAbsReal* pNll = sbModel.GetPdf()->createNLL(*dsObserved); RooAbsReal* pProfile = pNll->createProfile(RooArgSet()); pProfile->getVal(); RooArgSet* pPoiAndNuisance = new RooArgSet(); pPoiAndNuisance->add(*sbModel.GetParametersOfInterest()); if(sbModel.GetNuisanceParameters()) pPoiAndNuisance->add(*sbModel.GetNuisanceParameters()); printf("\n\n Will save these parameter points that correspond to the fit to data.\n\n") ; fflush(stdout) ; pPoiAndNuisance->Print("v"); sbModel.SetSnapshot(*pPoiAndNuisance); workspace.import (sbModel); delete pProfile ; delete pNll ; delete pPoiAndNuisance ; printf("\n\n Setting up BG-only model.\n\n") ; ModelConfig bModel (*(RooStats::ModelConfig *)workspace.obj("SbModel")); bModel.SetName("BModel"); bModel.SetWorkspace(workspace); printf("\n\n Doing fit for BG-only model.\n" ) ; fflush(stdout) ; pNll = bModel.GetPdf()->createNLL(*dsObserved); pProfile = pNll->createProfile(*bModel.GetParametersOfInterest()); ((RooRealVar *)(bModel.GetParametersOfInterest()->first()))->setVal(0.); pProfile->getVal(); pPoiAndNuisance = new RooArgSet(); pPoiAndNuisance->add(*bModel.GetParametersOfInterest()); if(bModel.GetNuisanceParameters()) pPoiAndNuisance->add(*bModel.GetNuisanceParameters()); printf("\n\n Should use these parameter points to generate pseudo data for bkg only.\n\n") ; fflush(stdout) ; pPoiAndNuisance->Print("v"); bModel.SetSnapshot(*pPoiAndNuisance); workspace.import (bModel); delete pProfile ; delete pNll ; delete pPoiAndNuisance ; workspace.Print() ; printf("\n\n Saving workspace in : %s\n\n", outfile ) ; gSystem->Exec(" mkdir -p outputfiles " ) ; workspace.writeToFile( outfile ) ; } // build_hbb_workspace1.
void exercise_3() { //Open the rootfile and get the workspace from the exercise_0 TFile fIn("exercise_0.root"); fIn.cd(); RooWorkspace *w = (RooWorkspace*)fIn.Get("w"); //You can set constant parameters that are known //If you leave them floating, the fit procedure will determine their uncertainty w->var("mean")->setConstant(kFALSE); //don't fix the mean, it's what we want to know the interval for! w->var("sigma")->setConstant(kTRUE); w->var("tau")->setConstant(kTRUE); w->var("Nsig")->setConstant(kTRUE); w->var("Nbkg")->setConstant(kTRUE); //Set the RooModelConfig and let it know what the content of the workspace is about ModelConfig model; model.SetWorkspace(*w); model.SetPdf("PDFtot"); //Let the model know what is the parameter of interest RooRealVar* mean = w->var("mean"); mean->setRange(120., 130.); //this is mostly for plotting reasons RooArgSet poi(*mean); // set confidence level double confidenceLevel = 0.68; //Build the profile likelihood calculator ProfileLikelihoodCalculator plc; plc.SetData(*(w->data("PDFtotData"))); plc.SetModel(model); plc.SetParameters(poi); plc.SetConfidenceLevel(confidenceLevel); //Get the interval LikelihoodInterval* plInt = plc.GetInterval(); //Now let's do the same for the Bayesian Calculator //Now we also need to specify a prior in the ModelConfig //To be quicker, we'll use the PDF factory facility of RooWorkspace //NB!! For simplicity, we are using a flat prior, but this doesn't mean it's the best choice! w->factory("Uniform::prior(mean)"); model.SetPriorPdf(*w->pdf("prior")); //Construct the bayesian calculator BayesianCalculator bc(*(w->data("PDFtotData")), model); bc.SetConfidenceLevel(confidenceLevel); bc.SetParameters(poi); SimpleInterval* bcInt = bc.GetInterval(); // Let's make a plot TCanvas dataCanvas("dataCanvas"); dataCanvas.Divide(2,1); dataCanvas.cd(1); LikelihoodIntervalPlot plotInt((LikelihoodInterval*)plInt); plotInt.SetTitle("Profile Likelihood Ratio and Posterior for mH"); plotInt.SetMaximum(3.); plotInt.Draw(); dataCanvas.cd(2); RooPlot *bcPlot = bc.GetPosteriorPlot(); bcPlot->Draw(); dataCanvas.SaveAs("exercise_3.gif"); //Now print the interval for mH for the two methods cout << "PLC interval is [" << plInt->LowerLimit(*mean) << ", " << plInt->UpperLimit(*mean) << "]" << endl; cout << "Bayesian interval is [" << bcInt->LowerLimit() << ", " << bcInt->UpperLimit() << "]" << endl; }