StandardBayesianNumericalDemo.C

Go to the documentation of this file.
00001 // Standard demo of the numerical Bayesian calculator
00002 
00003 /*
00004 
00005 
00006 Author: Kyle Cranmer
00007 date: Dec. 2010
00008 
00009 This is a standard demo that can be used with any ROOT file 
00010 prepared in the standard way.  You specify:
00011  - name for input ROOT file
00012  - name of workspace inside ROOT file that holds model and data
00013  - name of ModelConfig that specifies details for calculator tools
00014  - name of dataset 
00015 
00016 With default parameters the macro will attempt to run the 
00017 standard hist2workspace example and read the ROOT file
00018 that it produces.
00019 
00020 The actual heart of the demo is only about 10 lines long.
00021 
00022 The BayesianCalculator is based on Bayes's theorem
00023 and performs the integration using ROOT's numeric integration utilities
00024 */
00025 
00026 #include "TFile.h"
00027 #include "TROOT.h"
00028 #include "RooWorkspace.h"
00029 #include "RooAbsData.h"
00030 #include "RooRealVar.h"
00031 
00032 #include "RooUniform.h"
00033 #include "RooStats/ModelConfig.h"
00034 #include "RooStats/BayesianCalculator.h"
00035 #include "RooStats/SimpleInterval.h"
00036 #include "RooPlot.h"
00037 
00038 using namespace RooFit;
00039 using namespace RooStats;
00040 
00041 void StandardBayesianNumericalDemo(const char* infile = "",
00042                       const char* workspaceName = "proto",
00043                       const char* modelConfigName = "ModelConfig",
00044                       const char* dataName = "expData"){
00045 
00046   /////////////////////////////////////////////////////////////
00047   // First part is just to access a user-defined file 
00048   // or create the standard example file if it doesn't exist
00049   ////////////////////////////////////////////////////////////
00050 
00051   // note, use a different default file that BayesianCalculator can 
00052   // deal with.  The direct numeric integration is limited to a few dimensions
00053   const char* filename = "";
00054   if (!strcmp(infile,"")) 
00055     filename = "results/example_channel1_ConstExample_model.root";
00056   else
00057     filename = infile;
00058   // Check if example input file exists
00059   TFile *file = TFile::Open(filename);
00060 
00061   // if input file was specified byt not found, quit
00062   if(!file && strcmp(infile,"")){
00063     cout <<"file not found" << endl;
00064     return;
00065   } 
00066 
00067   // if default file not found, try to create it
00068   if(!file ){
00069     // Normally this would be run on the command line
00070     cout <<"will run standard hist2workspace example"<<endl;
00071     gROOT->ProcessLine(".! prepareHistFactory .");
00072     gROOT->ProcessLine(".! hist2workspace config/example.xml");
00073     cout <<"\n\n---------------------"<<endl;
00074     cout <<"Done creating example input"<<endl;
00075     cout <<"---------------------\n\n"<<endl;
00076   }
00077 
00078   // now try to access the file again
00079   file = TFile::Open(filename);
00080   if(!file){
00081     // if it is still not there, then we can't continue
00082     cout << "Not able to run hist2workspace to create example input" <<endl;
00083     return;
00084   }
00085 
00086   
00087   /////////////////////////////////////////////////////////////
00088   // Tutorial starts here
00089   ////////////////////////////////////////////////////////////
00090 
00091   // get the workspace out of the file
00092   RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
00093   if(!w){
00094     cout <<"workspace not found" << endl;
00095     return;
00096   }
00097 
00098   // get the modelConfig out of the file
00099   ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
00100 
00101   // get the modelConfig out of the file
00102   RooAbsData* data = w->data(dataName);
00103 
00104   // make sure ingredients are found
00105   if(!data || !mc){
00106     w->Print();
00107     cout << "data or ModelConfig was not found" <<endl;
00108     return;
00109   }
00110 
00111   /////////////////////////////////////////////
00112   // create and use the BayesianCalculator
00113   // to find and plot the 95% credible interval
00114   // on the parameter of interest as specified
00115   // in the model config
00116   
00117   // before we do that, we must specify our prior
00118   // it belongs in the model config, but it may not have
00119   // been specified
00120   RooUniform prior("prior","",*mc->GetParametersOfInterest());
00121   w->import(prior);
00122   mc->SetPriorPdf(*w->pdf("prior"));
00123 
00124   
00125   BayesianCalculator bayesianCalc(*data,*mc);
00126   bayesianCalc.SetConfidenceLevel(0.95); // 95% interval
00127 
00128   // default is the shortest interval.  here use central
00129   bayesianCalc.SetLeftSideTailFraction(0.5); // for central interval
00130 
00131   SimpleInterval* interval = bayesianCalc.GetInterval();
00132 
00133   // make a plot (slightly inconsistent interface)
00134   // since plotting may take a long time (it requires evaluating 
00135   // the posterior in many points) this command will speed up 
00136   // by reducing the number of points to plot - do 40
00137   bayesianCalc.SetScanOfPosterior(40);
00138   RooPlot * plot = bayesianCalc.GetPosteriorPlot();
00139   plot->Draw();
00140   
00141   // print out the iterval on the first Parameter of Interest
00142   RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
00143   cout << "\n95% interval on " <<firstPOI->GetName()<<" is : ["<<
00144     interval->LowerLimit() << ", "<<
00145     interval->UpperLimit() <<"] "<<endl;
00146 
00147 }

Generated on Tue Jul 5 15:45:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1