Skip to content

Commit c6c1599

Browse files
committed
Some improvements to RooStats tutorials, in particular Python versions
* Get rid of checks if files or objects in the file were found. It's ROOTs responsability to error out nicely if something is not found, not the responsability of the tutorials * Use f-formatting in Python to be more concise * Use Python iteration patters * Use pythonic `len()` instead of `GetSize()` * Use native Python string instead of `TString` * In Python, one can retrieve objects in the `RooWorkspace with `workspace[obj_name]` independent of the type, which is preferred because it's more Pythonic
1 parent bad90a5 commit c6c1599

4 files changed

+17
-94
lines changed

tutorials/roostats/StandardProfileInspectorDemo.C

-18
Original file line numberDiff line numberDiff line change
@@ -69,39 +69,21 @@ void StandardProfileInspectorDemo(const char *infile = "", const char *workspace
6969
} else
7070
filename = infile;
7171

72-
// Try to open the file
7372
TFile *file = TFile::Open(filename);
7473

75-
// if input file was specified but not found, quit
76-
if (!file) {
77-
cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
78-
return;
79-
}
80-
8174
// -------------------------------------------------------
8275
// Tutorial starts here
8376
// -------------------------------------------------------
8477

8578
// get the workspace out of the file
8679
RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
87-
if (!w) {
88-
cout << "workspace not found" << endl;
89-
return;
90-
}
9180

9281
// get the modelConfig out of the file
9382
ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
9483

9584
// get the modelConfig out of the file
9685
RooAbsData *data = w->data(dataName);
9786

98-
// make sure ingredients are found
99-
if (!data || !mc) {
100-
w->Print();
101-
cout << "data or ModelConfig was not found" << endl;
102-
return;
103-
}
104-
10587
// -----------------------------
10688
// now use the profile inspector
10789
ProfileInspector p;

tutorials/roostats/StandardProfileInspectorDemo.py

+9-26
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
## \authors Akeem Hart, Kyle Cranmer (C++ Version)
3030

3131
import ROOT
32-
import sys
33-
import math
3432

3533
# -------------------------------------------------------
3634
# First part is just to access a user-defined file
@@ -50,13 +48,7 @@
5048
print("Done creating example input")
5149
print("---------------------\n\n")
5250

53-
# Try to open the file
54-
try:
55-
file = ROOT.TFile.Open(filename)
56-
except:
57-
# if input file was specified but not found, quit
58-
print("StandardRooStatsDemoMacro: Input file %s is not found" % filename)
59-
sys.exit()
51+
file = ROOT.TFile.Open(filename)
6052

6153
# -------------------------------------------------------
6254
# Tutorial starts here
@@ -65,39 +57,30 @@
6557
# get the workspace out of the file
6658

6759
w = file.Get(workspaceName)
68-
if not w:
69-
print("Workspace not found")
70-
sys.exit()
7160

7261
# get the modelConfig out of the file
73-
mc = w.obj(modelConfigName)
62+
mc = w[modelConfigName]
7463

7564
# get the modelConfig out of the file
76-
data = w.data(dataName)
77-
78-
# make sure ingredients are found
79-
if not data or not mc:
80-
w.Print()
81-
print("data or ModelConfig was not found")
82-
sys.exit()
65+
data = w[dataName]
8366

8467
# -----------------------------
8568
# now use the profile inspector
8669
p = ROOT.RooStats.ProfileInspector()
87-
list = p.GetListOfProfilePlots(data, mc)
70+
profile_plots = p.GetListOfProfilePlots(data, mc)
8871

8972
# now make plots
9073
c1 = ROOT.TCanvas("c1", "ProfileInspectorDemo", 800, 200)
91-
n = list.GetSize()
74+
n = len(profile_plots)
9275
if n > 4:
93-
nx = int(math.sqrt(n))
76+
nx = int(n**0.5)
9477
ny = ROOT.TMath.CeilNint(n / nx)
95-
nx = ROOT.TMath.CeilNint(math.sqrt(n))
78+
nx = ROOT.TMath.CeilNint(n**0.5)
9679
c1.Divide(ny, nx)
9780
else:
9881
c1.Divide(n)
99-
for i in range(n):
82+
for i, plot in enumerate(profile_plots):
10083
c1.cd(i + 1)
101-
list.At(i).Draw("al")
84+
plot.Draw("al")
10285

10386
c1.Update()

tutorials/roostats/StandardProfileLikelihoodDemo.C

-17
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,19 @@ void StandardProfileLikelihoodDemo(const char *infile = "", const char *workspac
9191
// Try to open the file
9292
TFile *file = TFile::Open(filename);
9393

94-
// if input file was specified byt not found, quit
95-
if (!file) {
96-
cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
97-
return;
98-
}
99-
10094
// -------------------------------------------------------
10195
// Tutorial starts here
10296
// -------------------------------------------------------
10397

10498
// get the workspace out of the file
10599
RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
106-
if (!w) {
107-
cout << "workspace not found" << endl;
108-
return;
109-
}
110100

111101
// get the modelConfig out of the file
112102
ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
113103

114104
// get the modelConfig out of the file
115105
RooAbsData *data = w->data(dataName);
116106

117-
// make sure ingredients are found
118-
if (!data || !mc) {
119-
w->Print();
120-
cout << "data or ModelConfig was not found" << endl;
121-
return;
122-
}
123-
124107
// ---------------------------------------------
125108
// create and use the ProfileLikelihoodCalculator
126109
// to find and plot the 95% confidence interval

tutorials/roostats/StandardProfileLikelihoodDemo.py

+8-33
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
## \authors Akeem Hart, Kyle Cranmer (C++ Version)
2828

2929
import ROOT
30-
import sys
3130

3231
workspaceName = "combined"
3332
modelConfigName = "ModelConfig"
@@ -50,13 +49,7 @@
5049
print("Done creating example input")
5150
print("---------------------\n\n")
5251

53-
# Try to open the file
54-
try:
55-
file = ROOT.TFile.Open(filename)
56-
except:
57-
# if input file was specified but not found, quit
58-
print("StandardRooStatsDemoMacro: Input file %s is not found" % filename)
59-
sys.exit()
52+
file = ROOT.TFile.Open(filename)
6053

6154
# -------------------------------------------------------
6255
# Tutorial starts here
@@ -65,21 +58,12 @@
6558
# get the workspace out of the file
6659

6760
w = file.Get(workspaceName)
68-
if not w:
69-
print("Workspace not found")
70-
sys.exit()
7161

7262
# get the modelConfig out of the file
73-
mc = w.obj(modelConfigName)
63+
mc = w[modelConfigName]
7464

7565
# get the modelConfig out of the file
76-
data = w.data(dataName)
77-
78-
# make sure ingredients are found
79-
if not data or not mc:
80-
w.Print()
81-
print("data or ModelConfig was not found")
82-
sys.exit()
66+
data = w[dataName]
8367

8468
# ---------------------------------------------
8569
# create and use the ProfileLikelihoodCalculator
@@ -93,17 +77,8 @@
9377

9478
# print out the interval on the first Parameter of Interest
9579
firstPOI = mc.GetParametersOfInterest().first()
96-
print(
97-
"\n>>>> RESULT : "
98-
+ str(confLevel * 100)
99-
+ "% interval on "
100-
+ firstPOI.GetName()
101-
+ " is : ["
102-
+ str(interval.LowerLimit(firstPOI))
103-
+ ", "
104-
+ str(interval.UpperLimit(firstPOI))
105-
+ "]\n\n"
106-
)
80+
limit_lower, limit_upper = interval.LowerLimit(firstPOI), interval.UpperLimit(firstPOI)
81+
print(f"\n>>>> RESULT : {confLevel * 100}% interval on {firstPOI.GetName()} is : [{limit_lower}, {limit_upper}]\n")
10782

10883
# make a plot
10984

@@ -115,10 +90,10 @@
11590
plot.SetNPoints(nScanPoints) # do not use too many points, it could become very slow for some models
11691
if poiXMin < poiXMax:
11792
plot.SetRange(poiXMin, poiXMax)
118-
opt = ROOT.TString("")
93+
opt = ""
11994
if plotAsTF1:
120-
opt += ROOT.TString("tf1")
121-
plot.Draw(opt.Data()) # use option TF1 if too slow (plot.Draw("tf1")
95+
opt += "tf1"
96+
plot.Draw(opt) # use option TF1 if too slow (plot.Draw("tf1")
12297

12398
# if requested perform also an hypothesis test for the significance
12499
if doHypoTest:

0 commit comments

Comments
 (0)