Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
StandardProfileInspectorDemo.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook -js
4 /// Standard demo of the ProfileInspector class
5 /// StandardProfileInspectorDemo
6 ///
7 /// This is a standard demo that can be used with any ROOT file
8 /// prepared in the standard way. You specify:
9 /// - name for input ROOT file
10 /// - name of workspace inside ROOT file that holds model and data
11 /// - name of ModelConfig that specifies details for calculator tools
12 /// - name of dataset
13 ///
14 /// With default parameters the macro will attempt to run the
15 /// standard hist2workspace example and read the ROOT file
16 /// that it produces.
17 ///
18 /// The actual heart of the demo is only about 10 lines long.
19 ///
20 /// The ProfileInspector plots the conditional maximum likelihood estimate
21 /// of each nuisance parameter in the model vs. the parameter of interest.
22 /// (aka. profiled value of nuisance parameter vs. parameter of interest)
23 /// (aka. best fit nuisance parameter with p.o.i fixed vs. parameter of interest)
24 ///
25 /// \macro_image
26 /// \macro_output
27 /// \macro_code
28 ///
29 /// \author Kyle Cranmer
30 
31 #include "TFile.h"
32 #include "TROOT.h"
33 #include "TCanvas.h"
34 #include "TList.h"
35 #include "TMath.h"
36 #include "TSystem.h"
37 #include "RooWorkspace.h"
38 #include "RooAbsData.h"
39 
40 #include "RooStats/ModelConfig.h"
42 
43 using namespace RooFit;
44 using namespace RooStats;
45 
46 void StandardProfileInspectorDemo(const char *infile = "", const char *workspaceName = "combined",
47  const char *modelConfigName = "ModelConfig", const char *dataName = "obsData")
48 {
49 
50  // -------------------------------------------------------
51  // First part is just to access a user-defined file
52  // or create the standard example file if it doesn't exist
53 
54  const char *filename = "";
55  if (!strcmp(infile, "")) {
56  filename = "results/example_combined_GaussExample_model.root";
57  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
58  // if file does not exists generate with histfactory
59  if (!fileExist) {
60 #ifdef _WIN32
61  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
62  return;
63 #endif
64  // Normally this would be run on the command line
65  cout << "will run standard hist2workspace example" << endl;
66  gROOT->ProcessLine(".! prepareHistFactory .");
67  gROOT->ProcessLine(".! hist2workspace config/example.xml");
68  cout << "\n\n---------------------" << endl;
69  cout << "Done creating example input" << endl;
70  cout << "---------------------\n\n" << endl;
71  }
72 
73  } else
74  filename = infile;
75 
76  // Try to open the file
77  TFile *file = TFile::Open(filename);
78 
79  // if input file was specified byt not found, quit
80  if (!file) {
81  cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
82  return;
83  }
84 
85  // -------------------------------------------------------
86  // Tutorial starts here
87  // -------------------------------------------------------
88 
89  // get the workspace out of the file
90  RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
91  if (!w) {
92  cout << "workspace not found" << endl;
93  return;
94  }
95 
96  // get the modelConfig out of the file
97  ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
98 
99  // get the modelConfig out of the file
100  RooAbsData *data = w->data(dataName);
101 
102  // make sure ingredients are found
103  if (!data || !mc) {
104  w->Print();
105  cout << "data or ModelConfig was not found" << endl;
106  return;
107  }
108 
109  // -----------------------------
110  // now use the profile inspector
111  ProfileInspector p;
112  TList *list = p.GetListOfProfilePlots(*data, mc);
113 
114  // now make plots
115  TCanvas *c1 = new TCanvas("c1", "ProfileInspectorDemo", 800, 200);
116  if (list->GetSize() > 4) {
117  double n = list->GetSize();
118  int nx = (int)sqrt(n);
119  int ny = TMath::CeilNint(n / nx);
120  nx = TMath::CeilNint(sqrt(n));
121  c1->Divide(ny, nx);
122  } else
123  c1->Divide(list->GetSize());
124  for (int i = 0; i < list->GetSize(); ++i) {
125  c1->cd(i + 1);
126  list->At(i)->Draw("al");
127  }
128 
129  cout << endl;
130 }