StandardTestStatDistributionDemo.C: flag to control whether to use Proof
/*
StandardTestStatDistributionDemo.C
author Kyle Cranmer
date: summer solstice, 2011
This simple script plots the sampling distribution of the profile likelihood
ratio test statistic based on the input Model File. To do this one needs to
specify the value of the parameter of interest that will be used for evaluating
the test statistic and the value of the parameters used for generating the toy data.
In this case, it uses the upper-limit estimated from the ProfileLikleihoodCalculator,
which assumes the asymptotic chi-square distribution for -2 log profile likleihood ratio.
Thus, the script is handy for checking to see if the asymptotic approximations are valid.
To aid, that comparison, the script overlays a chi-square distribution as well.
The most common parameter of interest is a parameter proportional to the signal rate,
and often that has a lower-limit of 0, which breaks the standard chi-square distribution.
Thus the script allows the parameter to be negative so that the overlay chi-square is
the correct asymptotic distribution.
*/
#include "TFile.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TSystem.h"
#include "TF1.h"
#include "TSystem.h"
#include "RooWorkspace.h"
#include "RooAbsData.h"
#include "RooStats/ModelConfig.h"
#include "RooStats/FeldmanCousins.h"
#include "RooStats/ToyMCSampler.h"
#include "RooStats/PointSetInterval.h"
#include "RooStats/ConfidenceBelt.h"
#include "RooStats/ProfileLikelihoodCalculator.h"
#include "RooStats/LikelihoodInterval.h"
#include "RooStats/ProfileLikelihoodTestStat.h"
#include "RooStats/SamplingDistribution.h"
#include "RooStats/SamplingDistPlot.h"
using namespace RooFit;
using namespace RooStats;
bool useProof = false; // flag to control whether to use Proof
int nworkers = 0; // number of workers (default use all available cores)
/////////////////////////////////////////////////////////////////////////
// The actual macro
void StandardTestStatDistributionDemo(const char* infile = "",
const char* workspaceName = "combined",
const char* modelConfigName = "ModelConfig",
const char* dataName = "obsData"){
// the number of toy MC used to generate the distribution
int nToyMC = 1000;
// The parameter below is needed for asymptotic distribution to be chi-square,
// but set to false if your model is not numerically stable if mu<0
bool allowNegativeMu=true;
/////////////////////////////////////////////////////////////
// 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;
}
/////////////////////////////////////////////////////////////
// Now get the data and workspace
////////////////////////////////////////////////////////////
// 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;
}
mc->Print();
/////////////////////////////////////////////////////////////
// Now find the upper limit based on the asymptotic results
////////////////////////////////////////////////////////////
RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
ProfileLikelihoodCalculator plc(*data,*mc);
LikelihoodInterval* interval = plc.GetInterval();
double plcUpperLimit = interval->UpperLimit(*firstPOI);
delete interval;
cout << "\n\n--------------------------------------"<<endl;
cout <<"Will generate sampling distribution at " << firstPOI->GetName() << " = " << plcUpperLimit <<endl;
int nPOI = mc->GetParametersOfInterest()->getSize();
if(nPOI>1){
cout <<"not sure what to do with other parameters of interest, but here are their values"<<endl;
mc->GetParametersOfInterest()->Print("v");
}
/////////////////////////////////////////////
// create thte test stat sampler
ProfileLikelihoodTestStat ts(*mc->GetPdf());
// to avoid effects from boundary and simplify asymptotic comparison, set min=-max
if(allowNegativeMu)
firstPOI->setMin(-1*firstPOI->getMax());
// temporary RooArgSet
RooArgSet poi;
poi.add(*mc->GetParametersOfInterest());
// create and configure the ToyMCSampler
ToyMCSampler sampler(ts,nToyMC);
sampler.SetPdf(*mc->GetPdf());
sampler.SetObservables(*mc->GetObservables());
sampler.SetGlobalObservables(*mc->GetGlobalObservables());
if(!mc->GetPdf()->canBeExtended() && (data->numEntries()==1)){
cout << "tell it to use 1 event"<<endl;
sampler.SetNEventsPerToy(1);
}
firstPOI->setVal(plcUpperLimit); // set POI value for generation
sampler.SetParametersForTestStat(*mc->GetParametersOfInterest()); // set POI value for evaluation
if (useProof) {
ProofConfig pc(*w, nworkers, "",false);
sampler.SetProofConfig(&pc); // enable proof
}
firstPOI->setVal(plcUpperLimit);
RooArgSet allParameters;
allParameters.add(*mc->GetParametersOfInterest());
allParameters.add(*mc->GetNuisanceParameters());
allParameters.Print("v");
SamplingDistribution* sampDist = sampler.GetSamplingDistribution(allParameters);
SamplingDistPlot plot;
plot.AddSamplingDistribution(sampDist);
plot.GetTH1F(sampDist)->GetYaxis()->SetTitle(Form("f(-log #lambda(#mu=%.2f) | #mu=%.2f)",plcUpperLimit,plcUpperLimit));
plot.SetAxisTitle(Form("-log #lambda(#mu=%.2f)",plcUpperLimit));
TCanvas* c1 = new TCanvas("c1");
c1->SetLogy();
plot.Draw();
double min = plot.GetTH1F(sampDist)->GetXaxis()->GetXmin();
double max = plot.GetTH1F(sampDist)->GetXaxis()->GetXmax();
TF1* f = new TF1("f",Form("2*ROOT::Math::chisquared_pdf(2*x,%d,0)",nPOI),min,max);
f->Draw("same");
c1->SaveAs("standard_test_stat_distribution.pdf");
}
StandardTestStatDistributionDemo.C:1 StandardTestStatDistributionDemo.C:2 StandardTestStatDistributionDemo.C:3 StandardTestStatDistributionDemo.C:4 StandardTestStatDistributionDemo.C:5 StandardTestStatDistributionDemo.C:6 StandardTestStatDistributionDemo.C:7 StandardTestStatDistributionDemo.C:8 StandardTestStatDistributionDemo.C:9 StandardTestStatDistributionDemo.C:10 StandardTestStatDistributionDemo.C:11 StandardTestStatDistributionDemo.C:12 StandardTestStatDistributionDemo.C:13 StandardTestStatDistributionDemo.C:14 StandardTestStatDistributionDemo.C:15 StandardTestStatDistributionDemo.C:16 StandardTestStatDistributionDemo.C:17 StandardTestStatDistributionDemo.C:18 StandardTestStatDistributionDemo.C:19 StandardTestStatDistributionDemo.C:20 StandardTestStatDistributionDemo.C:21 StandardTestStatDistributionDemo.C:22 StandardTestStatDistributionDemo.C:23 StandardTestStatDistributionDemo.C:24 StandardTestStatDistributionDemo.C:25 StandardTestStatDistributionDemo.C:26 StandardTestStatDistributionDemo.C:27 StandardTestStatDistributionDemo.C:28 StandardTestStatDistributionDemo.C:29 StandardTestStatDistributionDemo.C:30 StandardTestStatDistributionDemo.C:31 StandardTestStatDistributionDemo.C:32 StandardTestStatDistributionDemo.C:33 StandardTestStatDistributionDemo.C:34 StandardTestStatDistributionDemo.C:35 StandardTestStatDistributionDemo.C:36 StandardTestStatDistributionDemo.C:37 StandardTestStatDistributionDemo.C:38 StandardTestStatDistributionDemo.C:39 StandardTestStatDistributionDemo.C:40 StandardTestStatDistributionDemo.C:41 StandardTestStatDistributionDemo.C:42 StandardTestStatDistributionDemo.C:43 StandardTestStatDistributionDemo.C:44 StandardTestStatDistributionDemo.C:45 StandardTestStatDistributionDemo.C:46 StandardTestStatDistributionDemo.C:47 StandardTestStatDistributionDemo.C:48 StandardTestStatDistributionDemo.C:49 StandardTestStatDistributionDemo.C:50 StandardTestStatDistributionDemo.C:51 StandardTestStatDistributionDemo.C:52 StandardTestStatDistributionDemo.C:53 StandardTestStatDistributionDemo.C:54 StandardTestStatDistributionDemo.C:55 StandardTestStatDistributionDemo.C:56 StandardTestStatDistributionDemo.C:57 StandardTestStatDistributionDemo.C:58 StandardTestStatDistributionDemo.C:59 StandardTestStatDistributionDemo.C:60 StandardTestStatDistributionDemo.C:61 StandardTestStatDistributionDemo.C:62 StandardTestStatDistributionDemo.C:63 StandardTestStatDistributionDemo.C:64 StandardTestStatDistributionDemo.C:65 StandardTestStatDistributionDemo.C:66 StandardTestStatDistributionDemo.C:67 StandardTestStatDistributionDemo.C:68 StandardTestStatDistributionDemo.C:69 StandardTestStatDistributionDemo.C:70 StandardTestStatDistributionDemo.C:71 StandardTestStatDistributionDemo.C:72 StandardTestStatDistributionDemo.C:73 StandardTestStatDistributionDemo.C:74 StandardTestStatDistributionDemo.C:75 StandardTestStatDistributionDemo.C:76 StandardTestStatDistributionDemo.C:77 StandardTestStatDistributionDemo.C:78 StandardTestStatDistributionDemo.C:79 StandardTestStatDistributionDemo.C:80 StandardTestStatDistributionDemo.C:81 StandardTestStatDistributionDemo.C:82 StandardTestStatDistributionDemo.C:83 StandardTestStatDistributionDemo.C:84 StandardTestStatDistributionDemo.C:85 StandardTestStatDistributionDemo.C:86 StandardTestStatDistributionDemo.C:87 StandardTestStatDistributionDemo.C:88 StandardTestStatDistributionDemo.C:89 StandardTestStatDistributionDemo.C:90 StandardTestStatDistributionDemo.C:91 StandardTestStatDistributionDemo.C:92 StandardTestStatDistributionDemo.C:93 StandardTestStatDistributionDemo.C:94 StandardTestStatDistributionDemo.C:95 StandardTestStatDistributionDemo.C:96 StandardTestStatDistributionDemo.C:97 StandardTestStatDistributionDemo.C:98 StandardTestStatDistributionDemo.C:99 StandardTestStatDistributionDemo.C:100 StandardTestStatDistributionDemo.C:101 StandardTestStatDistributionDemo.C:102 StandardTestStatDistributionDemo.C:103 StandardTestStatDistributionDemo.C:104 StandardTestStatDistributionDemo.C:105 StandardTestStatDistributionDemo.C:106 StandardTestStatDistributionDemo.C:107 StandardTestStatDistributionDemo.C:108 StandardTestStatDistributionDemo.C:109 StandardTestStatDistributionDemo.C:110 StandardTestStatDistributionDemo.C:111 StandardTestStatDistributionDemo.C:112 StandardTestStatDistributionDemo.C:113 StandardTestStatDistributionDemo.C:114 StandardTestStatDistributionDemo.C:115 StandardTestStatDistributionDemo.C:116 StandardTestStatDistributionDemo.C:117 StandardTestStatDistributionDemo.C:118 StandardTestStatDistributionDemo.C:119 StandardTestStatDistributionDemo.C:120 StandardTestStatDistributionDemo.C:121 StandardTestStatDistributionDemo.C:122 StandardTestStatDistributionDemo.C:123 StandardTestStatDistributionDemo.C:124 StandardTestStatDistributionDemo.C:125 StandardTestStatDistributionDemo.C:126 StandardTestStatDistributionDemo.C:127 StandardTestStatDistributionDemo.C:128 StandardTestStatDistributionDemo.C:129 StandardTestStatDistributionDemo.C:130 StandardTestStatDistributionDemo.C:131 StandardTestStatDistributionDemo.C:132 StandardTestStatDistributionDemo.C:133 StandardTestStatDistributionDemo.C:134 StandardTestStatDistributionDemo.C:135 StandardTestStatDistributionDemo.C:136 StandardTestStatDistributionDemo.C:137 StandardTestStatDistributionDemo.C:138 StandardTestStatDistributionDemo.C:139 StandardTestStatDistributionDemo.C:140 StandardTestStatDistributionDemo.C:141 StandardTestStatDistributionDemo.C:142 StandardTestStatDistributionDemo.C:143 StandardTestStatDistributionDemo.C:144 StandardTestStatDistributionDemo.C:145 StandardTestStatDistributionDemo.C:146 StandardTestStatDistributionDemo.C:147 StandardTestStatDistributionDemo.C:148 StandardTestStatDistributionDemo.C:149 StandardTestStatDistributionDemo.C:150 StandardTestStatDistributionDemo.C:151 StandardTestStatDistributionDemo.C:152 StandardTestStatDistributionDemo.C:153 StandardTestStatDistributionDemo.C:154 StandardTestStatDistributionDemo.C:155 StandardTestStatDistributionDemo.C:156 StandardTestStatDistributionDemo.C:157 StandardTestStatDistributionDemo.C:158 StandardTestStatDistributionDemo.C:159 StandardTestStatDistributionDemo.C:160 StandardTestStatDistributionDemo.C:161 StandardTestStatDistributionDemo.C:162 StandardTestStatDistributionDemo.C:163 StandardTestStatDistributionDemo.C:164 StandardTestStatDistributionDemo.C:165 StandardTestStatDistributionDemo.C:166 StandardTestStatDistributionDemo.C:167 StandardTestStatDistributionDemo.C:168 StandardTestStatDistributionDemo.C:169 StandardTestStatDistributionDemo.C:170 StandardTestStatDistributionDemo.C:171 StandardTestStatDistributionDemo.C:172 StandardTestStatDistributionDemo.C:173 StandardTestStatDistributionDemo.C:174 StandardTestStatDistributionDemo.C:175 StandardTestStatDistributionDemo.C:176 StandardTestStatDistributionDemo.C:177 StandardTestStatDistributionDemo.C:178 StandardTestStatDistributionDemo.C:179 StandardTestStatDistributionDemo.C:180 StandardTestStatDistributionDemo.C:181 StandardTestStatDistributionDemo.C:182 StandardTestStatDistributionDemo.C:183 StandardTestStatDistributionDemo.C:184 StandardTestStatDistributionDemo.C:185 StandardTestStatDistributionDemo.C:186 StandardTestStatDistributionDemo.C:187 StandardTestStatDistributionDemo.C:188 StandardTestStatDistributionDemo.C:189 StandardTestStatDistributionDemo.C:190 StandardTestStatDistributionDemo.C:191 StandardTestStatDistributionDemo.C:192 StandardTestStatDistributionDemo.C:193 StandardTestStatDistributionDemo.C:194 StandardTestStatDistributionDemo.C:195 StandardTestStatDistributionDemo.C:196