Skip to content

Commit e23e348

Browse files
guitargeekdpiparo
authored andcommitted
[RF] Properly close file and set histo directories in RooStats tutorial
In absense of the memory regulator, one needs to be more correct with object ownership and closing files. (cherry picked from commit c4b53f8)
1 parent 0627390 commit e23e348

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tutorials/roostats/TwoSidedFrequentistUpperLimitWithBands.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225

226226
# make a histogram of parameter vs. threshold
227227
histOfThresholds = ROOT.TH1F("histOfThresholds", "", parameterScan.numEntries(), firstPOI.getMin(), firstPOI.getMax())
228+
histOfThresholds.SetDirectory(ROOT.nullptr) # so th histogram doesn't get attached to the file with the workspace
228229
histOfThresholds.GetXaxis().SetTitle(firstPOI.GetName())
229230
histOfThresholds.GetYaxis().SetTitle("Threshold")
230231

@@ -271,6 +272,7 @@
271272

272273
# Now we generate background only and find distribution of upper limits
273274
histOfUL = ROOT.TH1F("histOfUL", "", 100, 0, firstPOI.getMax())
275+
histOfUL.SetDirectory(ROOT.nullptr) # make sure the histogram doesn't get attached to the file with the workspace
274276
histOfUL.GetXaxis().SetTitle("Upper Limit (background only)")
275277
histOfUL.GetYaxis().SetTitle("Entries")
276278
for imc in range(nToyMC):
@@ -340,6 +342,10 @@
340342
# for few events, data is often the same, and UL is often the same
341343
# print("thisUL = ", thisUL)
342344

345+
# At this point we can close the input file, since the RooWorkspace is not used
346+
# anymore.
347+
inputFile.Close()
348+
343349
histOfUL.Draw()
344350
c1.SaveAs("two-sided_upper_limit_output.pdf")
345351

@@ -356,7 +362,6 @@
356362

357363
# Now find bands and power constraint
358364
bins = histOfUL.GetIntegral()
359-
cumulative = ROOT.TH1F()
360365
cumulative = histOfUL.Clone("cumulative")
361366
cumulative.SetContent(bins)
362367
band2sigDown = 0

0 commit comments

Comments
 (0)