From $ROOTSYS/tutorials/roostats/StandardProfileLikelihoodDemo.C

// Standard demo of the Profile Likelihood calculcator
/*
StandardProfileLikelihoodDemo

Author: Kyle Cranmer
date: Dec. 2010

This is a standard demo that can be used with any ROOT file
prepared in the standard way.  You specify:
 - name for input ROOT file
 - name of workspace inside ROOT file that holds model and data
 - name of ModelConfig that specifies details for calculator tools
 - name of dataset

With default parameters the macro will attempt to run the
standard hist2workspace example and read the ROOT file
that it produces.

The actual heart of the demo is only about 10 lines long.

The ProfileLikelihoodCalculator is based on Wilks's theorem
and the asymptotic properties of the profile likeihood ratio
(eg. that it is chi-square distributed for the true value).
*/

#include "TFile.h"
#include "TROOT.h"
#include "TSystem.h"
#include "RooWorkspace.h"
#include "RooAbsData.h"
#include "RooRealVar.h"

#include "RooStats/ModelConfig.h"
#include "RooStats/ProfileLikelihoodCalculator.h"
#include "RooStats/LikelihoodInterval.h"
#include "RooStats/LikelihoodIntervalPlot.h"

using namespace RooFit;
using namespace RooStats;

void StandardProfileLikelihoodDemo(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
  ////////////////////////////////////////////////////////////
   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 ProfileLikelihoodCalculator
  // to find and plot the 95% confidence interval
  // on the parameter of interest as specified
  // in the model config
  ProfileLikelihoodCalculator pl(*data,*mc);
  pl.SetConfidenceLevel(0.95); // 95% interval
  LikelihoodInterval* interval = pl.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(*firstPOI) << ", "<<
    interval->UpperLimit(*firstPOI) <<"] "<<endl;

  // make a plot

  cout << "making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the TF1 drawing option)\n";
  LikelihoodIntervalPlot plot(interval);
  plot.SetNPoints(50);  // do not use too many points, it could become very slow for some models
  plot.Draw("");  // use option TF1 if too slow (plot.Draw("tf1")


}
 StandardProfileLikelihoodDemo.C:1
 StandardProfileLikelihoodDemo.C:2
 StandardProfileLikelihoodDemo.C:3
 StandardProfileLikelihoodDemo.C:4
 StandardProfileLikelihoodDemo.C:5
 StandardProfileLikelihoodDemo.C:6
 StandardProfileLikelihoodDemo.C:7
 StandardProfileLikelihoodDemo.C:8
 StandardProfileLikelihoodDemo.C:9
 StandardProfileLikelihoodDemo.C:10
 StandardProfileLikelihoodDemo.C:11
 StandardProfileLikelihoodDemo.C:12
 StandardProfileLikelihoodDemo.C:13
 StandardProfileLikelihoodDemo.C:14
 StandardProfileLikelihoodDemo.C:15
 StandardProfileLikelihoodDemo.C:16
 StandardProfileLikelihoodDemo.C:17
 StandardProfileLikelihoodDemo.C:18
 StandardProfileLikelihoodDemo.C:19
 StandardProfileLikelihoodDemo.C:20
 StandardProfileLikelihoodDemo.C:21
 StandardProfileLikelihoodDemo.C:22
 StandardProfileLikelihoodDemo.C:23
 StandardProfileLikelihoodDemo.C:24
 StandardProfileLikelihoodDemo.C:25
 StandardProfileLikelihoodDemo.C:26
 StandardProfileLikelihoodDemo.C:27
 StandardProfileLikelihoodDemo.C:28
 StandardProfileLikelihoodDemo.C:29
 StandardProfileLikelihoodDemo.C:30
 StandardProfileLikelihoodDemo.C:31
 StandardProfileLikelihoodDemo.C:32
 StandardProfileLikelihoodDemo.C:33
 StandardProfileLikelihoodDemo.C:34
 StandardProfileLikelihoodDemo.C:35
 StandardProfileLikelihoodDemo.C:36
 StandardProfileLikelihoodDemo.C:37
 StandardProfileLikelihoodDemo.C:38
 StandardProfileLikelihoodDemo.C:39
 StandardProfileLikelihoodDemo.C:40
 StandardProfileLikelihoodDemo.C:41
 StandardProfileLikelihoodDemo.C:42
 StandardProfileLikelihoodDemo.C:43
 StandardProfileLikelihoodDemo.C:44
 StandardProfileLikelihoodDemo.C:45
 StandardProfileLikelihoodDemo.C:46
 StandardProfileLikelihoodDemo.C:47
 StandardProfileLikelihoodDemo.C:48
 StandardProfileLikelihoodDemo.C:49
 StandardProfileLikelihoodDemo.C:50
 StandardProfileLikelihoodDemo.C:51
 StandardProfileLikelihoodDemo.C:52
 StandardProfileLikelihoodDemo.C:53
 StandardProfileLikelihoodDemo.C:54
 StandardProfileLikelihoodDemo.C:55
 StandardProfileLikelihoodDemo.C:56
 StandardProfileLikelihoodDemo.C:57
 StandardProfileLikelihoodDemo.C:58
 StandardProfileLikelihoodDemo.C:59
 StandardProfileLikelihoodDemo.C:60
 StandardProfileLikelihoodDemo.C:61
 StandardProfileLikelihoodDemo.C:62
 StandardProfileLikelihoodDemo.C:63
 StandardProfileLikelihoodDemo.C:64
 StandardProfileLikelihoodDemo.C:65
 StandardProfileLikelihoodDemo.C:66
 StandardProfileLikelihoodDemo.C:67
 StandardProfileLikelihoodDemo.C:68
 StandardProfileLikelihoodDemo.C:69
 StandardProfileLikelihoodDemo.C:70
 StandardProfileLikelihoodDemo.C:71
 StandardProfileLikelihoodDemo.C:72
 StandardProfileLikelihoodDemo.C:73
 StandardProfileLikelihoodDemo.C:74
 StandardProfileLikelihoodDemo.C:75
 StandardProfileLikelihoodDemo.C:76
 StandardProfileLikelihoodDemo.C:77
 StandardProfileLikelihoodDemo.C:78
 StandardProfileLikelihoodDemo.C:79
 StandardProfileLikelihoodDemo.C:80
 StandardProfileLikelihoodDemo.C:81
 StandardProfileLikelihoodDemo.C:82
 StandardProfileLikelihoodDemo.C:83
 StandardProfileLikelihoodDemo.C:84
 StandardProfileLikelihoodDemo.C:85
 StandardProfileLikelihoodDemo.C:86
 StandardProfileLikelihoodDemo.C:87
 StandardProfileLikelihoodDemo.C:88
 StandardProfileLikelihoodDemo.C:89
 StandardProfileLikelihoodDemo.C:90
 StandardProfileLikelihoodDemo.C:91
 StandardProfileLikelihoodDemo.C:92
 StandardProfileLikelihoodDemo.C:93
 StandardProfileLikelihoodDemo.C:94
 StandardProfileLikelihoodDemo.C:95
 StandardProfileLikelihoodDemo.C:96
 StandardProfileLikelihoodDemo.C:97
 StandardProfileLikelihoodDemo.C:98
 StandardProfileLikelihoodDemo.C:99
 StandardProfileLikelihoodDemo.C:100
 StandardProfileLikelihoodDemo.C:101
 StandardProfileLikelihoodDemo.C:102
 StandardProfileLikelihoodDemo.C:103
 StandardProfileLikelihoodDemo.C:104
 StandardProfileLikelihoodDemo.C:105
 StandardProfileLikelihoodDemo.C:106
 StandardProfileLikelihoodDemo.C:107
 StandardProfileLikelihoodDemo.C:108
 StandardProfileLikelihoodDemo.C:109
 StandardProfileLikelihoodDemo.C:110
 StandardProfileLikelihoodDemo.C:111
 StandardProfileLikelihoodDemo.C:112
 StandardProfileLikelihoodDemo.C:113
 StandardProfileLikelihoodDemo.C:114
 StandardProfileLikelihoodDemo.C:115
 StandardProfileLikelihoodDemo.C:116
 StandardProfileLikelihoodDemo.C:117
 StandardProfileLikelihoodDemo.C:118
 StandardProfileLikelihoodDemo.C:119
 StandardProfileLikelihoodDemo.C:120
 StandardProfileLikelihoodDemo.C:121
 StandardProfileLikelihoodDemo.C:122
 StandardProfileLikelihoodDemo.C:123
 StandardProfileLikelihoodDemo.C:124
 StandardProfileLikelihoodDemo.C:125
 StandardProfileLikelihoodDemo.C:126
 StandardProfileLikelihoodDemo.C:127
 StandardProfileLikelihoodDemo.C:128
 StandardProfileLikelihoodDemo.C:129
 StandardProfileLikelihoodDemo.C:130