From 2305cd0815d79737648da615dcf5735ddb6feb88 Mon Sep 17 00:00:00 2001 From: Susan Dittmer Date: Thu, 17 Mar 2022 18:36:26 +0100 Subject: [PATCH 1/2] Adding option to rebin histogram when running mkPlot.py --- ShapeAnalysis/python/PlotFactory.py | 11 +++++++++++ ShapeAnalysis/scripts/mkPlot.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/ShapeAnalysis/python/PlotFactory.py b/ShapeAnalysis/python/PlotFactory.py index 26803a288..2c16c74a6 100644 --- a/ShapeAnalysis/python/PlotFactory.py +++ b/ShapeAnalysis/python/PlotFactory.py @@ -144,6 +144,8 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu for variableName, variable in self._variables.iteritems(): if 'cuts' in variable and cutName not in variable['cuts']: continue + if 'tree' in variable: + continue if type(fileIn) is not dict and not fileIn.GetDirectory(cutName+"/"+variableName): continue @@ -259,6 +261,8 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu print ' --> ', histo print 'new_histo_' + sampleName + '_' + cutName + '_' + variableName histos[sampleName] = histo.Clone('new_histo_' + sampleName + '_' + cutName + '_' + variableName) + if self._rebin != 1: + histos[sampleName].Rebin(self._rebin) #print " -> sampleName = ", sampleName, " --> ", histos[sampleName].GetTitle(), " --> ", histos[sampleName].GetName(), " --> ", histos[sampleName].GetNbinsX() #for iBinAmassiro in range(1, histos[sampleName].GetNbinsX()+1): @@ -475,6 +479,8 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu histoVar = fileIn.Get(shapeNameVar) if histoVar != None : + if self._rebin != 1: + histoVar.Rebin(self._rebin) nuisanceHistos[ivar][nuisanceName] = histoVar elif not self._SkipMissingNuisance : print " This is bad, the nuisance ", nuisanceName, " is missing! You need to add it, maybe some jobs crashed?" @@ -659,6 +665,8 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu if self._postFit == 'b': tgrDataOverPF = tgrData.Clone("tgrDataOverPF") # use this for ratio with Post-Fit MC histoPF = fileIn.Get(cutName+"/"+variableName+'/histo_total_postfit_b') + if self._rebin != 1: + histoPF.Rebin(self._rebin) # at this stage "thsBackground" and then "last" includes ALSO the signal last = thsBackground.GetStack().Last() @@ -724,6 +732,9 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu else: histo_total = fileIn.Get(special_shapeName) + if self._rebin != 1: + histo_total.Rebin(self._rebin) + if variable['divideByBinWidth'] == 1 and histo_total != None: histo_total.Scale(1,"width") print '--> histo_total = ', histo_total diff --git a/ShapeAnalysis/scripts/mkPlot.py b/ShapeAnalysis/scripts/mkPlot.py index 40abc7e25..6a061f852 100755 --- a/ShapeAnalysis/scripts/mkPlot.py +++ b/ShapeAnalysis/scripts/mkPlot.py @@ -85,6 +85,8 @@ parser.add_option('--parallelPlotting', dest='parallelPlotting', help='Plot each cut in parallel' , action='store_true', default=False) + parser.add_option('--rebin', dest='rebin', help='Rebin histograms before plotting', default=1, type=int); + # read default parsing options as well hwwtools.addOptions(parser) hwwtools.loadOptDefaults(parser) @@ -118,6 +120,7 @@ print " NoPreliminary =", opt.NoPreliminary print " RemoveAllMC =", opt.RemoveAllMC print " parallelPlotting =", opt.parallelPlotting + print " rebin =", opt.rebin print "" opt.scaleToPlot = float(opt.scaleToPlot) @@ -256,6 +259,8 @@ def launch_plot(inputFile, outputDirPlots, variables, cuts, samples, plot, nuisa factory._preliminary = not opt.NoPreliminary factory._removeAllMC = opt.RemoveAllMC + factory._rebin = opt.rebin + factory.makePlot(inputFile ,outputDirPlots, variables, cuts, samples, plot, nuisances, legend, groupPlot) From 5eaf5e988382fb877758b40c3843d7ac5991b025 Mon Sep 17 00:00:00 2001 From: Susan Dittmer Date: Tue, 22 Mar 2022 15:52:18 +0100 Subject: [PATCH 2/2] PlotFactory rebinning bugfix --- ShapeAnalysis/python/PlotFactory.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ShapeAnalysis/python/PlotFactory.py b/ShapeAnalysis/python/PlotFactory.py index 2c16c74a6..238fb107e 100644 --- a/ShapeAnalysis/python/PlotFactory.py +++ b/ShapeAnalysis/python/PlotFactory.py @@ -260,9 +260,9 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu if not histo: continue print ' --> ', histo print 'new_histo_' + sampleName + '_' + cutName + '_' + variableName - histos[sampleName] = histo.Clone('new_histo_' + sampleName + '_' + cutName + '_' + variableName) if self._rebin != 1: - histos[sampleName].Rebin(self._rebin) + histo.Rebin(self._rebin) + histos[sampleName] = histo.Clone('new_histo_' + sampleName + '_' + cutName + '_' + variableName) #print " -> sampleName = ", sampleName, " --> ", histos[sampleName].GetTitle(), " --> ", histos[sampleName].GetName(), " --> ", histos[sampleName].GetNbinsX() #for iBinAmassiro in range(1, histos[sampleName].GetNbinsX()+1): @@ -665,7 +665,7 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu if self._postFit == 'b': tgrDataOverPF = tgrData.Clone("tgrDataOverPF") # use this for ratio with Post-Fit MC histoPF = fileIn.Get(cutName+"/"+variableName+'/histo_total_postfit_b') - if self._rebin != 1: + if (self._postFit == 'p' or self._postFit == 's' or self._postFit == 'b') and self._rebin != 1: histoPF.Rebin(self._rebin) # at this stage "thsBackground" and then "last" includes ALSO the signal @@ -732,7 +732,7 @@ def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nu else: histo_total = fileIn.Get(special_shapeName) - if self._rebin != 1: + if histo_total and self._rebin != 1: histo_total.Rebin(self._rebin) if variable['divideByBinWidth'] == 1 and histo_total != None: