Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speeding up histogram postprocessing in doHadd #284

Open
wants to merge 1 commit into
base: UL_production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ShapeAnalysis/python/ShapeFactoryMulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ def postprocess_nuisance_variations(nuisance, samples, cuts, variables, outFile)
histoNameVar = 'histo_' + outputFormat.format(sample=sampleName, subsample=slabel, nuisance=('_%sV%dVar' % (nuisance['name'], ivar)))
hTotalVar = outDir.Get(histoNameVar)
variations[ivar, :] = rnp.hist2array(hTotalVar, copy=True).flat
hTotalVar.Delete()
if hasattr(userConfig, 'shapeFactoryDeleteVariations') and userConfig.shapeFactoryDeleteVariations:
outDir.Delete(histoNameVar + ';*')

Expand All @@ -1511,11 +1512,15 @@ def postprocess_nuisance_variations(nuisance, samples, cuts, variables, outFile)
rnp.array2hist(arrup, outputsHistoUp)
if 'suppressNegativeNuisances' in sample and (cutName in sample['suppressNegativeNuisances'] or 'all' in sample['suppressNegativeNuisances']) : ShapeFactory._fixNegativeBin(outputsHistoUp, nominal)
outputsHistoUp.Write()
outputsHistoUp.Delete()
outputsHistoDown = nominal.Clone(histoNameDown)
if twosided:
rnp.array2hist(arrdown, outputsHistoDown)
if 'suppressNegativeNuisances' in sample and (cutName in sample['suppressNegativeNuisances'] or 'all' in sample['suppressNegativeNuisances']) : ShapeFactory._fixNegativeBin(outputsHistoDown, nominal)
outputsHistoDown.Write()
outputsHistoDown.Delete()
nominal.Delete()
ROOT.TObject.Delete(outDir)

@staticmethod
def postprocess_NegativeBinAndError(nuisances, sampleName, sample, cuts, variables, outFile):
Expand Down Expand Up @@ -1554,6 +1559,8 @@ def postprocess_NegativeBinAndError(nuisances, sampleName, sample, cuts, variabl
nominal = outDir.Get(histoName)

if ShapeFactory._fixNegativeBinAndError(nominal): nominal.Write()
nominal.Delete()
ROOT.TObject.Delete(outDir)

if 'suppressNegativeNuisances' in sample:
for nuisance in nuisances.itervalues():
Expand Down Expand Up @@ -1606,3 +1613,7 @@ def postprocess_NegativeBinAndError(nuisances, sampleName, sample, cuts, variabl
if twosided:
if ShapeFactory._fixNegativeBin(outputsHistoDown, nominal): outputsHistoDown.Write()

nominal.Delete()
outputsHistoUp.Delete()
outputsHistoDown.Delete()
ROOT.TObject.Delete(outDir)
10 changes: 6 additions & 4 deletions ShapeAnalysis/scripts/mkShapesMulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import subprocess
import threading, Queue
from LatinoAnalysis.ShapeAnalysis.ShapeFactoryMulti import ShapeFactory
import time

# Common Tools & batch
from LatinoAnalysis.Tools.commonTools import *
Expand Down Expand Up @@ -534,19 +535,20 @@ def makeTargetList(options, samples):
if 'exists' in output.lower(): stoppedForExistingRootfile = True
print(output.strip())

start = time.time()
outFile = ROOT.TFile.Open(finalpath, 'update')
# Speed up closure of output file in case of many histograms - see https://root-forum.cern.ch/t/tfile-close-slow/24179
ROOT.gROOT.GetListOfFiles().Remove(outFile)
for nuisance in nuisances.itervalues():
if 'kind' in nuisance and (nuisance['kind'].endswith('_envelope') or nuisance['kind'].endswith('_rms')):
ShapeFactory.postprocess_nuisance_variations(nuisance, samples, cuts, variables, outFile)
outFile.Close()

if opt.FixNegativeAfterHadd:
for sampleName, sample in samples.iteritems():
if 'suppressNegative' in sample or 'suppressNegativeNuisances' in sample:
outFile = ROOT.TFile.Open(finalpath, 'update')
ShapeFactory.postprocess_NegativeBinAndError(nuisances, sampleName, sample, cuts, variables, outFile)
outFile.Close()

outFile.Close()
print 'Time to postprocess hadded shapes: {:.1f}s'.format(time.time()-start)
### Modded to avoid cleaning all partial shapes if hadd quits because the target rootfile already exists
if not opt.doNotCleanup and not stoppedForExistingRootfile:
for fname in fileList:
Expand Down