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; }
/* * 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 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.