Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StandardHistFactoryPlotsWithCategories.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook -js
4/// StandardHistFactoryPlotsWithCategories
5///
6/// This is a standard demo that can be used with any ROOT file
7/// prepared in the standard way. You specify:
8/// - name for input ROOT file
9/// - name of workspace inside ROOT file that holds model and data
10/// - name of ModelConfig that specifies details for calculator tools
11/// - name of dataset
12///
13/// With default parameters the macro will attempt to run the
14/// standard hist2workspace example and read the ROOT file
15/// that it produces.
16///
17/// The macro will scan through all the categories in a simPdf find the corresponding
18/// observable. For each category, it will loop through each of the nuisance parameters
19/// and plot
20/// - the data
21/// - the nominal model (blue)
22/// - the +Nsigma (red)
23/// - the -Nsigma (green)
24///
25/// You can specify how many sigma to vary by changing nSigmaToVary.
26/// You can also change the signal rate by changing muVal.
27///
28/// The script produces a lot plots, you can merge them by doing:
29/// ~~~{.cpp}
30/// gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf `ls *pdf`
31/// ~~~
32///
33/// \macro_image
34/// \macro_output
35/// \macro_code
36///
37/// \author Kyle Cranmer
38
39#include "TFile.h"
40#include "TROOT.h"
41#include "TCanvas.h"
42#include "TList.h"
43#include "TMath.h"
44#include "TSystem.h"
45#include "RooWorkspace.h"
46#include "RooAbsData.h"
47#include "RooRealVar.h"
48#include "RooPlot.h"
49#include "RooSimultaneous.h"
50#include "RooCategory.h"
51
54
55using namespace RooFit;
56using namespace RooStats;
57using namespace std;
58
59void StandardHistFactoryPlotsWithCategories(const char *infile = "", const char *workspaceName = "combined",
60 const char *modelConfigName = "ModelConfig",
61 const char *dataName = "obsData")
62{
63
64 double nSigmaToVary = 5.;
65 double muVal = 0;
66 bool doFit = false;
67
68 // -------------------------------------------------------
69 // First part is just to access a user-defined file
70 // or create the standard example file if it doesn't exist
71 const char *filename = "";
72 if (!strcmp(infile, "")) {
73 filename = "results/example_combined_GaussExample_model.root";
74 bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
75 // if file does not exists generate with histfactory
76 if (!fileExist) {
77#ifdef _WIN32
78 cout << "HistFactory file cannot be generated on Windows - exit" << endl;
79 return;
80#endif
81 // Normally this would be run on the command line
82 cout << "will run standard hist2workspace example" << endl;
83 gROOT->ProcessLine(".! prepareHistFactory .");
84 gROOT->ProcessLine(".! hist2workspace config/example.xml");
85 cout << "\n\n---------------------" << endl;
86 cout << "Done creating example input" << endl;
87 cout << "---------------------\n\n" << endl;
88 }
89
90 } else
91 filename = infile;
92
93 // Try to open the file
95
96 // if input file was specified byt not found, quit
97 if (!file) {
98 cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
99 return;
100 }
101
102 // -------------------------------------------------------
103 // Tutorial starts here
104 // -------------------------------------------------------
105
106 // get the workspace out of the file
107 RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
108 if (!w) {
109 cout << "workspace not found" << endl;
110 return;
111 }
112
113 // get the modelConfig out of the file
114 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
115
116 // get the modelConfig out of the file
117 RooAbsData *data = w->data(dataName);
118
119 // make sure ingredients are found
120 if (!data || !mc) {
121 w->Print();
122 cout << "data or ModelConfig was not found" << endl;
123 return;
124 }
125
126 // -------------------------------------------------------
127 // now use the profile inspector
128
129 RooRealVar *obs = (RooRealVar *)mc->GetObservables()->first();
130 TList *list = new TList();
131
132 RooRealVar *firstPOI = dynamic_cast<RooRealVar *>(mc->GetParametersOfInterest()->first());
133
134 firstPOI->setVal(muVal);
135 // firstPOI->setConstant();
136 if (doFit) {
137 mc->GetPdf()->fitTo(*data);
138 }
139
140 // -------------------------------------------------------
141
142 mc->GetNuisanceParameters()->Print("v");
143 int nPlotsMax = 1000;
144 cout << " check expectedData by category" << endl;
145 RooDataSet *simData = NULL;
146 RooSimultaneous *simPdf = NULL;
147 if (strcmp(mc->GetPdf()->ClassName(), "RooSimultaneous") == 0) {
148 cout << "Is a simultaneous PDF" << endl;
149 simPdf = (RooSimultaneous *)(mc->GetPdf());
150 } else {
151 cout << "Is not a simultaneous PDF" << endl;
152 }
153
154 if (doFit) {
155 RooCategory *channelCat = (RooCategory *)(&simPdf->indexCat());
156 auto const& catName = channelCat->begin()->first;
157 RooAbsPdf *pdftmp = ((RooSimultaneous *)mc->GetPdf())->getPdf(catName.c_str());
158 std::unique_ptr<RooArgSet> obstmp{pdftmp->getObservables(*mc->GetObservables())};
159 obs = ((RooRealVar *)obstmp->first());
160 RooPlot *frame = obs->frame();
161 cout << Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str()) << endl;
162 cout << catName << " " << channelCat->getLabel() << endl;
163 data->plotOn(frame, MarkerSize(1),
164 Cut(Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str())),
166
167 Double_t normCount =
168 data->sumEntries(Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str()));
169
170 pdftmp->plotOn(frame, LineWidth(2.), Normalization(normCount, RooAbsReal::NumEvent));
171 frame->Draw();
172 cout << "expected events = " << mc->GetPdf()->expectedEvents(*data->get()) << endl;
173 return;
174 }
175
176 int nPlots = 0;
177 if (!simPdf) {
178
180 RooRealVar *var = NULL;
181 while ((var = (RooRealVar *)it.Next()) != NULL) {
182 RooPlot *frame = obs->frame();
183 frame->SetYTitle(var->GetName());
184 data->plotOn(frame, MarkerSize(1));
185 var->setVal(0);
186 mc->GetPdf()->plotOn(frame, LineWidth(1.));
187 var->setVal(1);
188 mc->GetPdf()->plotOn(frame, LineColor(kRed), LineStyle(kDashed), LineWidth(1));
189 var->setVal(-1);
191 list->Add(frame);
192 var->setVal(0);
193 }
194
195 } else {
196 RooCategory *channelCat = (RooCategory *)(&simPdf->indexCat());
197 for (auto const& tt : *channelCat) {
198
199 if (nPlots == nPlotsMax) {
200 break;
201 }
202
203 auto const& catName = tt.first;
204
205 cout << "on type " << catName << " " << endl;
206 // Get pdf associated with state from simpdf
207 RooAbsPdf *pdftmp = simPdf->getPdf(catName.c_str());
208
209 // Generate observables defined by the pdf associated with this state
210 std::unique_ptr<RooArgSet> obstmp{pdftmp->getObservables(*mc->GetObservables())};
211 // obstmp->Print();
212
213 obs = ((RooRealVar *)obstmp->first());
214
216 RooRealVar *var = NULL;
217 while (nPlots < nPlotsMax && (var = (RooRealVar *)it.Next())) {
218 TCanvas *c2 = new TCanvas("c2");
219 RooPlot *frame = obs->frame();
220 frame->SetName(Form("frame%d", nPlots));
221 frame->SetYTitle(var->GetName());
222
223 cout << Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str()) << endl;
224 cout << catName << " " << channelCat->getLabel() << endl;
225 data->plotOn(frame, MarkerSize(1),
226 Cut(Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str())),
228
229 Double_t normCount =
230 data->sumEntries(Form("%s==%s::%s", channelCat->GetName(), channelCat->GetName(), catName.c_str()));
231
232 if (strcmp(var->GetName(), "Lumi") == 0) {
233 cout << "working on lumi" << endl;
234 var->setVal(w->var("nominalLumi")->getVal());
235 var->Print();
236 } else {
237 var->setVal(0);
238 }
239 // w->allVars().Print("v");
240 // mc->GetNuisanceParameters()->Print("v");
241 // pdftmp->plotOn(frame,LineWidth(2.));
242 // mc->GetPdf()->plotOn(frame,LineWidth(2.),Slice(*channelCat,catName.c_str()),ProjWData(*data));
243 // pdftmp->plotOn(frame,LineWidth(2.),Slice(*channelCat,catName.c_str()),ProjWData(*data));
244 normCount = pdftmp->expectedEvents(*obs);
245 pdftmp->plotOn(frame, LineWidth(2.), Normalization(normCount, RooAbsReal::NumEvent));
246
247 if (strcmp(var->GetName(), "Lumi") == 0) {
248 cout << "working on lumi" << endl;
249 var->setVal(w->var("nominalLumi")->getVal() + 0.05);
250 var->Print();
251 } else {
252 var->setVal(nSigmaToVary);
253 }
254 // pdftmp->plotOn(frame,LineColor(kRed),LineStyle(kDashed),LineWidth(2));
255 // mc->GetPdf()->plotOn(frame,LineColor(kRed),LineStyle(kDashed),LineWidth(2.),Slice(*channelCat,catName.c_str()),ProjWData(*data));
256 // pdftmp->plotOn(frame,LineColor(kRed),LineStyle(kDashed),LineWidth(2.),Slice(*channelCat,catName.c_str()),ProjWData(*data));
257 normCount = pdftmp->expectedEvents(*obs);
258 pdftmp->plotOn(frame, LineWidth(2.), LineColor(kRed), LineStyle(kDashed),
260
261 if (strcmp(var->GetName(), "Lumi") == 0) {
262 cout << "working on lumi" << endl;
263 var->setVal(w->var("nominalLumi")->getVal() - 0.05);
264 var->Print();
265 } else {
266 var->setVal(-nSigmaToVary);
267 }
268 // pdftmp->plotOn(frame,LineColor(kGreen),LineStyle(kDashed),LineWidth(2));
269 // mc->GetPdf()->plotOn(frame,LineColor(kGreen),LineStyle(kDashed),LineWidth(2),Slice(*channelCat,catName.c_str()),ProjWData(*data));
270 // pdftmp->plotOn(frame,LineColor(kGreen),LineStyle(kDashed),LineWidth(2),Slice(*channelCat,catName.c_str()),ProjWData(*data));
271 normCount = pdftmp->expectedEvents(*obs);
272 pdftmp->plotOn(frame, LineWidth(2.), LineColor(kGreen), LineStyle(kDashed),
274
275 // set them back to normal
276 if (strcmp(var->GetName(), "Lumi") == 0) {
277 cout << "working on lumi" << endl;
278 var->setVal(w->var("nominalLumi")->getVal());
279 var->Print();
280 } else {
281 var->setVal(0);
282 }
283
284 list->Add(frame);
285
286 // quit making plots
287 ++nPlots;
288
289 frame->Draw();
290 c2->SaveAs(Form("%s_%s_%s.pdf", catName.c_str(), obs->GetName(), var->GetName()));
291 delete c2;
292 }
293 }
294 }
295
296 // -------------------------------------------------------
297
298 // now make plots
299 TCanvas *c1 = new TCanvas("c1", "ProfileInspectorDemo", 800, 200);
300 if (list->GetSize() > 4) {
301 double n = list->GetSize();
302 int nx = (int)sqrt(n);
303 int ny = TMath::CeilNint(n / nx);
304 nx = TMath::CeilNint(sqrt(n));
305 c1->Divide(ny, nx);
306 } else
307 c1->Divide(list->GetSize());
308 for (int i = 0; i < list->GetSize(); ++i) {
309 c1->cd(i + 1);
310 list->At(i)->Draw();
311 }
312}
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gROOT
Definition TROOT.h:405
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
void Print(Option_t *options=nullptr) const override
Print the object to the defaultPrintStream().
Definition RooAbsArg.h:318
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
const char * getLabel() const
Retrieve current label. Use getCurrentLabel() for more clarity.
std::map< std::string, value_type >::const_iterator begin() const
Iterator for category state names. Points to pairs of index and name.
RooAbsArg * first() const
TIterator * createIterator(bool dir=kIterForward) const
TIterator-style iteration over contained elements.
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
virtual double expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
virtual RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, const RooLinkedList &cmdList={})
Fit PDF to given dataset.
RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none(), const RooCmdArg &arg9=RooCmdArg::none(), const RooCmdArg &arg10=RooCmdArg::none()) const override
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition RooAbsPdf.h:126
RooPlot * frame(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Create a new RooPlot on the heap with a drawing frame initialized for this object,...
RooCategory is an object to represent discrete states.
Definition RooCategory.h:28
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:57
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
void SetName(const char *name) override
Set the name of the RooPlot to 'name'.
Definition RooPlot.cxx:1233
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
void SetYTitle(const char *title)
Definition RooPlot.cxx:1348
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
void setVal(double value) override
Set value of variable to 'value'.
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsPdf * getPdf(RooStringView catName) const
Return the p.d.f associated with the given index category name.
const RooAbsCategoryLValue & indexCat() const
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
The RooWorkspace is a persistable container for RooFit projects.
The Canvas class.
Definition TCanvas.h:23
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:51
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4053
TObject * Next()
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1299
RooCmdArg DataError(Int_t)
RooCmdArg LineWidth(Width_t width)
RooCmdArg Normalization(double scaleFactor)
RooCmdArg MarkerSize(Size_t size)
RooCmdArg Cut(const char *cutSpec)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
return c1
Definition legend1.C:41
const Int_t n
Definition legend1.C:16
return c2
Definition legend2.C:14
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18
Namespace for the RooStats classes.
Definition Asimov.h:19
Int_t CeilNint(Double_t x)
Returns the nearest integer of TMath::Ceil(x).
Definition TMath.h:672
Definition file.py:1
auto * tt
Definition textangle.C:16