def main(): if len(sys.argv) < 2: usage() paths = [sys.argv[1]] analysis = "Hplus2tbAnalysis" hName = "associatedTPt" plotname = analysis + "_" + hName datasetsHiggs = dataset.getDatasetsFromMulticrabDirs( paths, analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTauB_M_") datasetsTT = dataset.getDatasetsFromMulticrabDirs(paths, analysisName=analysis, includeOnlyTasks="TT") datasetsTT.merge("MC", ["TT", "TT_ext"], keepSources=True) style = tdrstyle.TDRStyle() dataset1 = datasetsHiggs.getDataset( "ChargedHiggs_HplusTB_HplusToTauB_M_200").getDatasetRootHisto(hName) dataset2 = datasetsTT.getDataset("MC").getDatasetRootHisto(hName) # dataset1.normalizeToOne() dataset2.normalizeToOne() histo1 = dataset1.getHistogram() histo1.SetMarkerColor(2) histo1.SetMarkerStyle(20) removeNegatives(histo1) histo1.Scale(1. / histo1.Integral()) histo2 = dataset2.getHistogram() histo2.SetMarkerColor(4) histo2.SetMarkerStyle(21) p = plots.ComparisonPlot( histograms.Histo(histo1, "m_{H^{#pm}} = 200 GeV/c^{2}", "p", "P"), histograms.Histo(histo2, "t#bar{t}", "p", "P")) opts = {"ymin": 0, "ymax": 0.2} opts2 = {"ymin": 0.5, "ymax": 1.5} p.createFrame(os.path.join(plotDir, plotname), createRatio=True, opts=opts, opts2=opts2) moveLegend = {"dx": -0.2, "dy": -0.1, "dh": -0.1} p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend)) p.getFrame().GetYaxis().SetTitle("Arbitrary units") p.getFrame().GetXaxis().SetTitle("Top p_{T} (GeV/c)") p.getFrame2().GetYaxis().SetTitle("Ratio") p.getFrame2().GetYaxis().SetTitleOffset(1.6) p.draw() if not os.path.exists(plotDir): os.mkdir(plotDir) p.save(formats)
def plot(self, plotDir): histolist = [] # self.h_data.SetMarkerStyle(21) # self.h_data.SetMarkerSize(2) styles.dataStyle.apply(self.h_data) hhd = histograms.Histo(self.h_data, "Data", legendStyle="PL", drawStyle="E1P") hhd.setIsDataMC(isData=True, isMC=False) histolist.append(hhd) for hname in self.histonames: hhp = histograms.Histo(self.histograms[hname], hname, legendStyle="F", drawStyle="HIST", legendLabel=self.labels[hname]) hhp.setIsDataMC(isData=False, isMC=True) histolist.append(hhp) name = "postFitMt_" + self.name # name = "preFitMt_"+self.name style = tdrstyle.TDRStyle() p = plots.DataMCPlot2(histolist) p.setDefaultStyles() p.stackMCHistograms() p.setLuminosity(lumi) p.addMCUncertainty(postfit=True) # p.addText(0.7, 0.84, "2016", size=20) #FIXME p.setLegendHeader("2016") myParams = {} myParams["xlabel"] = "m_{T} (GeV)" myParams["ylabel"] = "< Events / bin >" myParams["ratio"] = True myParams["ratioYlabel"] = "Data/Bkg. " myParams["logx"] = True myParams["ratioType"] = "errorScale" myParams["ratioErrorOptions"] = { "numeratorStatSyst": False, "denominatorStatSyst": True } myParams["opts"] = self.opts myParams["optsLogx"] = self.optsLogx myParams["opts2"] = self.opts2 myParams["errorBarsX"] = True myParams["xlabelsize"] = 25 myParams["ylabelsize"] = 25 myParams["addMCUncertainty"] = True myParams["addLuminosityText"] = True #FIXME myParams["moveLegend"] = self.moveLegend plots.drawPlot(p, os.path.join(plotDir, name), **myParams) print "Saved plot", os.path.join(plotDir, name)
def makePlot(self, dirname, m, index, luminosity): if self._data == None: return myStackList = [] # expected for i in range(0, len(self._expectedList)): myRHWU = RootHistoWithUncertainties(self._expectedList[i]) myRHWU.addShapeUncertaintyRelative( "syst", th1Plus=self._expectedListSystUp[i], th1Minus=self._expectedListSystDown[i]) myRHWU.makeFlowBinsVisible() if self._expectedLabelList[i] == "QCD": myHisto = histograms.Histo(myRHWU, self._expectedLabelList[i], legendLabel=_legendLabelQCD) elif self._expectedLabelList[i] == "Embedding": myHisto = histograms.Histo(myRHWU, self._expectedLabelList[i], legendLabel=_legendLabelEmbedding) elif self._expectedLabelList[i] == "EWKfakes": myHisto = histograms.Histo(myRHWU, self._expectedLabelList[i], legendLabel=_legendLabelEWKFakes) else: myHisto = histograms.Histo(myRHWU, self._expectedLabelList[i]) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) # data myRHWU = RootHistoWithUncertainties(self._data) myRHWU.makeFlowBinsVisible() myHisto = histograms.Histo(myRHWU, "Data") myHisto.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, myHisto) # Make plot myStackPlot = plots.DataMCPlot2(myStackList) myStackPlot.setLuminosity(luminosity) myStackPlot.setEnergy("%d" % self._config.OptionSqrtS) myStackPlot.setDefaultStyles() myParams = {} myParams["ylabel"] = "Events" myParams["log"] = True myParams["cmsTextPosition"] = "right" myParams["opts"] = {"ymin": 0.9} myParams["opts2"] = {"ymin": 0.3, "ymax": 1.7} #myParams["moveLegend"] = {"dx": -0.08, "dy": -0.12, "dh": 0.1} # for MC EWK+tt #myParams["moveLegend"] = {"dx": -0.15, "dy": -0.12, "dh":0.05} # for data-driven myParams["moveLegend"] = { "dx": -0.53, "dy": -0.52, "dh": 0.05 } # for data-driven myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03} drawPlot( myStackPlot, "%s/DataDrivenCtrlPlot_M%d_%02d_SelectionFlow" % (dirname, m, index), **myParams)
def doSystematicsPlots(self): if self._mySystObject == None: return # Make sure results have been obtained if self._hSystematicsUp == None: self.doSystematicsForMetShapeDifference() # Create output directory if os.path.exists("%s/Systematics_%s"%(self._myDir, self._systName)): shutils.rmtree("%s/Systematics_%s"%(self._myDir, self._systName)) os.mkdir("%s/Systematics_%s"%(self._myDir, self._systName)) # Make plot for systematics (phase-space bins integrated) if self._hFinalShape != None: self._hFinalShape.SetLineColor(ROOT.kBlack) self._hSystematicsUp.SetLineColor(ROOT.kBlue) self._hSystematicsDown.SetLineColor(ROOT.kRed) myYmax = 15 if "2012" in self._moduleInfoString: myYmax = 30 plot = plots.ComparisonManyPlot(histograms.Histo(self._hFinalShape, "Nominal", drawStyle="E"), [histograms.Histo(self._hSystematicsUp, "Up", drawStyle="E"), histograms.Histo(self._hSystematicsDown, "Down", drawStyle="E")]) plot.createFrame("%s/Systematics_%s/QCDInvertedShapeWithMetSyst_final_%s"%(self._myDir, self._systName, self._moduleInfoString), createRatio=True, opts2={"ymin": 0, "ymax": 2}, opts={"addMCUncertainty": True, "ymin": -5, "ymax": myYmax}) plot.frame.GetXaxis().SetTitle(self._histoSpecs["xtitle"]) plot.frame.GetYaxis().SetTitle(self._histoSpecs["ytitle"]) plot.setLegend(histograms.createLegend(0.59, 0.70, 0.87, 0.90)) plot.legend.SetFillColor(0) plot.legend.SetFillStyle(1001) styles.mcStyle(plot.histoMgr.getHisto("Up")) #plot.histoMgr.getHisto("Up").getRootHisto().SetMarkerSize(0) styles.mcStyle2(plot.histoMgr.getHisto("Down")) styles.dataStyle(plot.histoMgr.getHisto("Nominal")) self._drawPlot(plot) # Make plots for systematics (for each phase-space bin separately) nSplitBins = self._myCtrlRegion.getNumberOfPhaseSpaceSplitBins() myCtrlHistograms = self._mySystObject.getHistogramsForCtrlRegion() mySignalHistograms = self._mySystObject.getHistogramsForSignalRegion() myMinIndex = 0 if myCtrlHistograms[0] == None: myMinIndex = 1 # Skip empty first bin for i in range(myMinIndex, nSplitBins): plot = plots.ComparisonPlot(histograms.Histo(mySignalHistograms[i].Clone(), "Signal region", drawStyle="E"), histograms.Histo(myCtrlHistograms[i].Clone(), "Ctrl. region", drawStyle="E")) plot.createFrame("%s/Systematics_%s/QCDInvertedShapeWithMetSyst_%s_%s"%(self._myDir, self._systName, self._myCtrlRegion.getPhaseSpaceBinFileFriendlyTitle(i), self._moduleInfoString), createRatio=True, opts2={"ymin": 0, "ymax": 2}, opts={"addMCUncertainty": True, "ymin": -0.05, "ymax": 0.3}) plot.frame.GetXaxis().SetTitle(self._histoSpecs["xtitle"]) plot.frame.GetYaxis().SetTitle("Arb. units. (Normalised to 1)") styles.mcStyle(plot.histoMgr.getHisto("Ctrl. region")) styles.dataStyle(plot.histoMgr.getHisto("Signal region")) plot.setLegend(histograms.createLegend(0.59, 0.77, 0.87, 0.90)) plot.legend.SetFillColor(0) plot.legend.SetFillStyle(1001) #plot.histoMgr.getHisto("Up").getRootHisto().SetMarkerSize(0) self._drawPlot(plot) print "Saved MET shape systematics plot for bin %s"%self._myCtrlRegion.getPhaseSpaceBinFileFriendlyTitle(i)
def GetHistosForPlotter(datasetsMgr, histoName, **kwargs): ''' ''' HasKeys(["refDataset", "drawStyle", "legStyle"], **kwargs) refHisto = None otherHistos = [] refDataset = kwargs.get("refDataset") drawStyle = kwargs.get("drawStyle") legStyle = kwargs.get("legStyle") normalizeTo = kwargs.get("normalizeTo") histoType = None # For-loop: All dataset objects for d in datasetsMgr.getAllDatasets(): rootHisto = datasetsMgr.getDataset( d.getName()).getDatasetRootHisto(histoName) # Apply Normalization NormalizeRootHisto(datasetsMgr, rootHisto, d.isMC(), normalizeTo) # Get the histogram histo = rootHisto.getHistogram() histoType = type(histo) legName = plots._legendLabels[d.getName()] # Apply Styling styleDict[d.getName()].apply(histo) if d.getName() == refDataset: #histo.SetFillStyle(3001) refHisto = histograms.Histo(histo, legName, legStyle, drawStyle) else: #otherHisto = histograms.Histo(histo, legName, legStyle, drawStyle) #otherHisto = histograms.Histo(histo, legName, "F", "HIST9") otherHisto = histograms.Histo(histo, legName, "LP", "P") # fixme alex otherHistos.append(otherHisto) if refHisto == None: raise Exception("The \"reference\" histogram is None!") if len(otherHistos) < 1: if ("TH2" in str(histoType)): otherHistos.append( "EMPTY") # fixme: temporary fix, otherwise crash for TH2 else: raise Exception("The \"other\" histogram list empty!") return refHisto, otherHistos
def makeIntegratedPlot(self, histoName, outName, histoSpecs, plotOptions, optionPrintPurityByBins=False): h = self.getIntegratedHistogram(histoName, histoSpecs, optionPrintPurityByBins) plot = plots.PlotBase([histograms.Histo(h, "shape", drawStyle="E")]) plot.createFrame("%s/%s_%s"%(self._myDir,outName,self._moduleInfoString), opts=plotOptions) plot.frame.GetXaxis().SetTitle(histoSpecs["xtitle"]) plot.frame.GetYaxis().SetTitle(histoSpecs["ytitle"]) styles.dataStyle(plot.histoMgr.getHisto("shape")) self._drawPlot(plot)
def makeFinalPlot(self, histoName, outName, histoSpecs, plotOptions, optionPrintPurityByBins=False): # Check if plot exists if not self._dsetMgr.getDataset("Data").hasRootHisto(histoName): print "Could not find histogram or directory '%s' in the multicrab root files (perhaps you did not run on Informative level), skipping it ..."%histoName return h = self.getFinalHistogram(histoName, histoSpecs, optionPrintPurityByBins) plot = plots.PlotBase([histograms.Histo(h,"shape", drawStyle="E")]) plot.createFrame("%s/%s_%s"%(self._myDir,outName,self._moduleInfoString), opts=plotOptions) plot.frame.GetXaxis().SetTitle(histoSpecs["xtitle"]) plot.frame.GetYaxis().SetTitle(histoSpecs["ytitle"]) styles.dataStyle(plot.histoMgr.getHisto("shape")) self._drawPlot(plot)
def createPlot(histo, myLumi, legendLabel, sLegStyle, sDrawStyle, **kwargs): style = tdrstyle.TDRStyle() if isinstance(histo, ROOT.TH1): args = {"legendStyle": sLegStyle, "drawStyle": sDrawStyle} args.update(kwargs) histo.GetZaxis().SetTitle("") #p = plots.PlotBase([histograms.Histo(histo, legendLabel, **args)]) p = plots.ComparisonManyPlot(histograms.Histo(histo, legendLabel, **args), []) p.setLuminosity(myLumi) return p else: print "*** ERROR: Histogram \"%s\" is not a valid instance of a ROOT.TH1" % (histo) sys.exit()
def purityGraph(i, datasets, histo): inverted = plots.DataMCPlot(datasets, histo) # inverted.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(5)) inverted.histoMgr.forEachHisto( lambda h: h.setRootHisto(h.getRootHisto().Rebin( len(binning) - 1, h.getRootHisto().GetName(), array.array('d', binning)))) invertedData = inverted.histoMgr.getHisto("Data").getRootHisto().Clone( histo) invertedData.Scale(QCDInvertedNormalization["Inclusive"]) invertedEWK = inverted.histoMgr.getHisto("EWK").getRootHisto().Clone(histo) invertedEWK.Scale(QCDInvertedNormalization["InclusiveEWK"]) numerator = invertedData.Clone() numerator.SetName("numerator") numerator.Add(invertedEWK, -1) denominator = invertedData.Clone() denominator.SetName("denominator") numerator.Divide(denominator) purityGraph = ROOT.TGraphAsymmErrors(numerator) """ purity = ROOT.TEfficiency(numerator,denominator) purity.SetStatisticOption(ROOT.TEfficiency.kFNormal) collection = ROOT.TObjArray() collection.Add(purity) weights = [] weights.append(1) purityGraph = ROOT.TEfficiency.Combine(collection,"",len(weights),array.array("d",weights)) """ purityGraph.SetMarkerStyle(20 + i) purityGraph.SetMarkerColor(2 + i) if i == 3: purityGraph.SetMarkerColor(6) defaults = {"drawStyle": "EP", "legendStyle": "p"} return histograms.Histo(purityGraph, "Purity%s" % i, **defaults)
def doPlot(mass, name, nuisance, thetas): f = ROOT.TFile.Open("lands_histograms_hplushadronic_m%d.root" % mass) h = f.Get(name) hUp = f.Get("%s_%dUp" % (name, nuisance)) hDown = f.Get("%s_%dDown" % (name, nuisance)) shapes = [] ll = {} for i, theta in enumerate(thetas): morphed = doShape(h, hUp, hDown, theta) shapes.append( histograms.Histo(morphed, "Theta%d" % i, drawStyle="HIST", legendStyle="l")) ll["Theta%d" % i] = "#theta=%.2f (%.1f)" % (theta, _integral(morphed)) plot = plots.PlotBase(shapes) plot.histoMgr.forEachHisto(styles.generator()) plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3)) plot.histoMgr.setHistoLegendLabelMany(ll) plot.setLegend(histograms.createLegend()) plot.createFrame("shape_theta_%s_syst%d_m%d" % (name, nuisance, mass)) plot.frame.GetXaxis().SetTitle("m_{T} (GeV)") plot.frame.GetYaxis().SetTitle("Rate") plot.draw() x = 0.6 size = 20 histograms.addText(x, 0.70, "Sample %s" % name, size=size) histograms.addText(x, 0.65, "Nuisance %d" % nuisance, size=size) histograms.addText(x, 0.60, "m_{H^{+}}=%d GeV" % mass, size=size) plot.save() f.Close()
def PlotSignalBackground(datasetsMgr, hG, hF, intLumi): kwargs = {} _kwargs = {} #kwargs = GetHistoKwargs(hG, opts) if opts.normaliseToOne: pG = plots.MCPlot(datasetsMgr, hG, normalizeToOne=True, saveFormats=[], **_kwargs) pF = plots.MCPlot(datasetsMgr, hF, normalizeToOne=True, saveFormats=[], **_kwargs) else: pG = plots.MCPlot(datasetsMgr, hG, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) pF = plots.MCPlot(datasetsMgr, hF, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) # Draw the histograms _cutBox = None _rebinX = 1 _format = "%0.2f" _xlabel = None logY = False _opts = {"ymin": 1e-3, "ymaxfactor": 1.0} if "mass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "M (%s)" % _units if "trijetmass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{top} (%s)" % _units #_cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True} _opts["xmax"] = 805 #1005 if "bjetmass" in hG.lower(): _xlabel = "m_{b-tagged jet} (%s)" % _units _opts["xmax"] = 50 if "bjetldgjet_mass" in hG.lower(): _xlabel = "m_{b, ldg jet} (%s)" % _units if "bjetsubldgjet_mass" in hG.lower(): _xlabel = "m_{b-tagged, subldg jet} (%s)" % _units if "jet_mass" in hG.lower(): _opts["xmax"] = 750 if "mult" in hG.lower(): _format = "%0.0f" _xlabel = "Leading jet mult" if "subldg" in hG.lower(): _xlabel = "Subleading jet mult" if "cvsl" in hG.lower(): _format = "%0.2f" _xlabel = "Leading jet CvsL" if "subldg" in hG.lower(): _xlabel = "Subleading jet CvsL" if "axis2" in hG.lower(): _format = "%0.3f" _xlabel = "Leading jet axis2" if "subldg" in hG.lower(): _xlabel = "Subleading jet axis2" if "dijetmass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{W} (%s)" % _units _opts["xmax"] = 600 if "trijetptdr" in hG.lower(): _opts["xmax"] = 800 _format = "%0.0f" _xlabel = "p_{T}#Delta R_{t}" if "dijetptdr" in hG.lower(): _opts["xmax"] = 800 _format = "%0.0f" _xlabel = "p_{T}#Delta R_{W}" if "dgjetptd" in hG.lower(): _format = "%0.2f" _xlabel = "Leading jet p_{T}D" if "subldg" in hG.lower(): _xlabel = "Subleading jet p_{T}D" else: pass ''' if "bdisc" in hG.lower(): _format = "%0.2f" if "subldg" in hG.lower(): _xlabel = "Subleading jet CSV" elif "ldg" in hG.lower(): _xlabel = "Leading jet CSV" else: _xlabel = "b-tagged jet CSV" ''' if logY: yMaxFactor = 2.0 else: yMaxFactor = 1.2 _opts["ymaxfactor"] = yMaxFactor if opts.normaliseToOne: _opts["ymin"] = 1e-3 else: _opts["ymin"] = 1e0 if "bdisc" in hG.lower(): _format = "%0.2f" if "subldg" in hG.lower(): _xlabel = "Subleading jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.06} elif "ldg" in hG.lower(): _xlabel = "Leading jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.06} else: _xlabel = "b-tagged jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.35} myList = [] # Customise styling pG.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) pF.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) ''' plots.drawPlot(pG, hG, xlabel = _xlabel, ylabel = "Arbitrary Units / %s" % (_format), log = logY, rebinX = _rebinX, cmsExtraText = "Preliminary", createLegend = {"x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.92}, opts = _opts, opts2 = {"ymin": 0.6, "ymax": 1.4}, cutBox = _cutBox, ) plots.drawPlot(pF, hF, xlabel = _xlabel, ylabel = "Arbitrary Units / %s" % (_format), log = logY, rebinX = _rebinX, cmsExtraText = "Preliminary", createLegend = {"x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.92}, opts = _opts, opts2 = {"ymin": 0.6, "ymax": 1.4}, cutBox = _cutBox, ) ''' dataset = datasetsMgr.getDataset("TT") h = dataset.getDatasetRootHisto(hG) h.normalizeToOne() HG = h.getHistogram() h = dataset.getDatasetRootHisto(hF) h.normalizeToOne() HF = h.getHistogram() altSignalBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kAzure + 9, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kAzure + 9, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kAzure + 9) ]) altBackgroundBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kRed - 4, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kRed - 4, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kRed - 4, fillStyle=3001) ]) p = plots.ComparisonPlot( histograms.Histo(HF, "Fake", "p", "P"), histograms.Histo(HG, "Genuine", "pl", "PL"), ) p.histoMgr.setHistoLegendLabelMany({ "Fake": "Unmatched", "Genuine": "Truth-matched" }) p.histoMgr.forHisto("Fake", altBackgroundBDTGStyle) p.histoMgr.setHistoDrawStyle("Fake", "LP") p.histoMgr.setHistoLegendStyle("Fake", "LP") #F p.histoMgr.forHisto("Genuine", altSignalBDTGStyle) p.histoMgr.setHistoDrawStyle("Genuine", "HIST") p.histoMgr.setHistoLegendStyle("Genuine", "LP") #LP #HF = dataset.getDatasetRootHisto(hF).getHistogram() #HG = dataset.getDatasetRootHisto(hG).getHistogram() histoG = histograms.Histo(HG, "TT", "Signal") histoG.setIsDataMC(isData=False, isMC=True) histoF = histograms.Histo(HF, "QCD", "Signal") histoF.setIsDataMC(isData=False, isMC=True) styleG = styles.ttStyle styleF = styles.signalStyleHToTB1000 #hG = histograms.Histo(histoBkg3, legNameBkg3, "F", "HIST9" ) #background3_histo = histograms.Histo(histoBkg3, legNameBkg3, "F", "HIST9" ) styleG.apply(HG) styleF.apply(HF) myList.append(histoG) myList.append(histoF) #myList.append(HF) #myList.append(HG) # p = plots.PlotBase(datasetRootHistos=myList, saveFormats=[]) _kwargs = { "xlabel": _xlabel, "ylabel": "Arbitrary Units / %s" % (_format), "ratioYlabel": "Ratio ", "ratio": False, "ratioInvert": False, "stackMCHistograms": False, "addMCUncertainty": False, "addLuminosityText": False, "addCmsText": True, "cmsExtraText": "Preliminary", "opts": { "ymin": 0.0, "ymaxfactor": 1.1 }, "opts2": { "ymin": 0.6, "ymax": 1.5 }, "log": False, #"createLegend" : {"x1": 0.5, "y1": 0.75, "x2": 0.9, "y2": 0.9}, "createLegend": { "x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.82 }, } ''' plots.drawPlot(p, hG, xlabel = _xlabel, ylabel = "Arbitrary Units / %s" % (_format), log = logY, rebinX = _rebinX, cmsExtraText = "Preliminary", createLegend = {"x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.82}, opts = _opts, opts2 = {"ymin": 0.6, "ymaxfactor": 1.1}, cutBox = _cutBox, ) ''' # Save plot in all formats saveName = hG.split("/")[-1] #plots.drawPlot(p, saveName, **_kwargs) savePath = os.path.join(opts.saveDir, "HplusMasses", hG.split("/")[0], opts.optMode) plots.drawPlot(p, savePath, **_kwargs) SavePlot(p, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf", ".C"]) #SavePlot(p, saveName, savePath) #p = plots.DataMCPlot2( myList, saveFormats=[]) #p.setLuminosity(opts.intLumi) #p.setDefaultStyles() # Draw the plot and save it #hName = hG #plots.drawPlot(p, hName, **_kwargs) #SavePlot(p, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png"]) return
def PlotHistos(d_noSF, d_withSF, num_histoList, den_histoList, opts): # Get the histogram customisations (keyword arguments) _kwargs = GetHistoKwargs(num_histoList[0]) # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CR1", "CR2"] labels = ["Genuine", "Fake", "Inclusive"] #========================================================================================== # Get Dictionaries #========================================================================================== rhDict_den_noSF = GetRootHistos(d_noSF, den_histoList, regions) rhDict_num_noSF = GetRootHistos(d_noSF, num_histoList, regions) rhDict_num_withSF = GetRootHistos(d_withSF, num_histoList, regions) # ========================================================================================= # Normalization Factors (see: getNormalization.py) # ========================================================================================= #f1=0.626877; f2=0.880767; f1 = 0.625454 f2 = 0.836566 #noTopPtRew #f1 = 0.626893; f2 = 0.880846; #noDeltaRqq #f1 = 0.625454; f2 = 0.836566; #noDeltaRqq, noTopPtRew # ========================================================================================= # (A) Apply Normalization Factors (see: getNormalizations.py) # ========================================================================================= # Normalize all histograms (QCD and TT) to normalization factors for re in regions: rhDict_den_noSF["NormQCD-" + re + "-Inclusive"] = rhDict_den_noSF[ "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive") rhDict_den_noSF["NormQCD-" + re + "-Inclusive"].Scale(f1) rhDict_num_noSF["NormQCD-" + re + "-Inclusive"] = rhDict_num_noSF[ "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive") rhDict_num_noSF["NormQCD-" + re + "-Inclusive"].Scale(f1) rhDict_num_withSF["NormQCD-" + re + "-Inclusive"] = rhDict_num_withSF[ "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive") rhDict_num_withSF["NormQCD-" + re + "-Inclusive"].Scale(f1) for la in labels: rhDict_den_noSF["NormTT-" + re + "-" + la] = rhDict_den_noSF["TT-" + re + "-" + la].Clone("NormTT-" + re + "-" + la) rhDict_den_noSF["NormTT-" + re + "-" + la].Scale(f2) rhDict_num_noSF["NormTT-" + re + "-" + la] = rhDict_num_noSF["TT-" + re + "-" + la].Clone("NormTT-" + re + "-" + la) rhDict_num_noSF["NormTT-" + re + "-" + la].Scale(f2) rhDict_num_withSF["NormTT-" + re + "-" + la] = rhDict_num_withSF["TT-" + re + "-" + la].Clone("NormTT-" + re + "-" + la) rhDict_num_withSF["NormTT-" + re + "-" + la].Scale(f2) # ========================================================================================= # (B) Estimate Inclusive TT in SR # ========================================================================================= # (B1) Inclusive TT in Data (Denominator) = Data - F1*QCD - EWK - ST rhDict_den_noSF["TTinData-SR-Inclusive"] = rhDict_den_noSF[ "Data-SR-Inclusive"].Clone("Inclusive t#bar{t} (Data)") rhDict_den_noSF["TTinData-SR-Inclusive"].Add( rhDict_den_noSF["NormQCD-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Inclusive"].Add( rhDict_den_noSF["EWK-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Inclusive"].Add( rhDict_den_noSF["SingleTop-SR-Inclusive"], -1) # (B2) Inclusive TT in Data (Numerator) = Data - (F1*QCD -EWK - ST)*SF rhDict_num_noSF["TTinData-SR-Inclusive"] = rhDict_num_noSF[ "Data-SR-Inclusive"].Clone("Inclusive t#bar{t} (Data)") rhDict_num_noSF["TTinData-SR-Inclusive"].Add( rhDict_num_withSF["NormQCD-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Inclusive"].Add( rhDict_num_withSF["EWK-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Inclusive"].Add( rhDict_num_withSF["SingleTop-SR-Inclusive"], -1) # ========================================================================================== # (C) Plot Inclusive Efficiency (Data Vs MC) # ========================================================================================== _kwargs["opts"] = {"xmax": 800, "ymaxfactor": 2.0} _kwargs["ratioYlabel"] = "Data/MC" _kwargs["ratio"] = True _kwargs["stackMCHistograms"] = False _kwargs["createLegend"] = {"x1": 0.60, "y1": 0.75, "x2": 0.95, "y2": 0.92} _kwargs["ratioInvert"] = True num_data = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone( "t#bar{t}_{SR} (Data)") den_data = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone( "t#bar{t}_{SR} (Data)") num_mc = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)") den_mc = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)") num_data.Rebin(2) num_mc.Rebin(2) den_data.Rebin(2) den_mc.Rebin(2) styles.getABCDStyle("CR4").apply(num_mc) styles.getABCDStyle("CR4").apply(den_mc) # Denominator hName = "InclusiveTT_SR_Denominator" hData_Den = histograms.Histo(den_data, "t#bar{t}_{SR} (Data)", "Data", drawStyle="AP") hData_Den.setIsDataMC(isData=True, isMC=False) hMC_Den = histograms.Histo(den_mc, "t#bar{t}_{SR} (MC)", "MC", drawStyle="HIST") hMC_Den.setIsDataMC(isData=False, isMC=True) pDen = plots.ComparisonManyPlot(hData_Den, [hMC_Den], saveFormats=[]) pDen.setLuminosity(opts.intLumi) pDen.setDefaultStyles() plots.drawPlot(pDen, hName, **_kwargs) SavePlot(pDen, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png"]) # Numerator hName = "InclusiveTT_SR_Numerator" hData_Num = histograms.Histo(num_data, "t#bar{t}_{SR} (Data)", "Data", drawStyle="AP") hData_Num.setIsDataMC(isData=True, isMC=False) hMC_Num = histograms.Histo(num_mc, "t#bar{t}_{SR} (MC)", "MC", drawStyle="HIST") hMC_Num.setIsDataMC(isData=False, isMC=True) pNum = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[]) pNum.setLuminosity(opts.intLumi) pNum.setDefaultStyles() plots.drawPlot(pNum, hName, **_kwargs) SavePlot(pNum, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png"]) # ========================= # Inclusive Efficiency # ========================= _kwargs = { "xlabel": "p_{T} (GeV/c)", "ylabel": "Efficiency", "ratioYlabel": "Data/MC", "ratio": True, "ratioInvert": False, "ratioType": None, "ratioCreateLegend": True, "ratioMoveLegend": { "dx": -0.51, "dy": 0.03, "dh": -0.08 }, "ratioErrorOptions": { "numeratorStatSyst": False, "denominatorStatSyst": False }, "errorBarsX": True, "stackMCHistograms": False, "addMCUncertainty": False, "addLuminosityText": False, "addCmsText": True, "cmsExtraText": "Preliminary", "opts": { "ymin": 0.0, "ymaxfactor": 1.5, "xmax": 600 }, "opts2": { "ymin": 0.6, "ymax": 1.5 }, "log": False, "createLegend": { "x1": 0.64, "y1": 0.80, "x2": 0.95, "y2": 0.92 }, } _kwargs["cutBoxY"] = { "cutValue": 1.10, "fillColor": ROOT.kGray + 1, "fillStyle": 3001, "box": False, "line": True, "greaterThan": True, "mainCanvas": False, "ratioCanvas": True, "mirror": True } bins = [0, 100, 200, 300, 400, 500, 600] xBins = array.array('d', bins) nx = len(xBins) - 1 # Data h0_data_den = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone( "t#bar{t}_{SR} (Data)") h0_data_num = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone( "t#bar{t}_{SR} (Data)") # MC h0_mc_den = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)") h0_mc_num = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)") h0_data_den = h0_data_den.Rebin(nx, "", xBins) h0_data_num = h0_data_num.Rebin(nx, "", xBins) h0_mc_den = h0_mc_den.Rebin(nx, "", xBins) h0_mc_num = h0_mc_num.Rebin(nx, "", xBins) hNumerator_Data, hDenominator_Data = GetHistosForEfficiency( h0_data_num, h0_data_den) hNumerator_MC, hDenominator_MC = GetHistosForEfficiency( h0_mc_num, h0_mc_den) eff_data = ROOT.TEfficiency(hNumerator_Data, hDenominator_Data) eff_mc = ROOT.TEfficiency(hNumerator_MC, hDenominator_MC) eff_data.SetStatisticOption(ROOT.TEfficiency.kFCP) eff_mc.SetStatisticOption(ROOT.TEfficiency.kFCP) geff_data = convert2TGraph(eff_data) geff_mc = convert2TGraph(eff_mc) styles.dataStyle.apply(geff_data) styles.ttStyle.apply(geff_mc) Graph_Data = histograms.HistoGraph(geff_data, "t#bar{t}_{SR} (Data) ", "p", "P") Graph_MC = histograms.HistoGraph(geff_mc, "t#bar{t}_{SR} (MC)", "p", "P") p = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[]) saveName = "Efficiency_InclusiveTT_SR" savePath = os.path.join(opts.saveDir, opts.optMode) plots.drawPlot(p, savePath, **_kwargs) SavePlot(p, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf", ".C"]) # Save results in JSON if (opts.inclusiveEff): name = opts.noSFcrab.split("_")[-4] name = name.replace(opts.analysisName, "") print "name = ", name jsonName = "toptagEff_BDT" + name + "_InclusiveTT_TopMassCut400.json" runRange = "273150-284044" analysis = opts.analysisName label = "2016" plotDir = os.path.join(opts.folder, jsonName) pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi, geff_data) pythonWriter.addMCParameters(label, geff_mc) fileName_json = jsonName pythonWriter.writeJSON(fileName_json) # ========================================================================================== # (D) Estimate Genuine TT in SR # ========================================================================================== # (D1) Genuine TT in Data - Denominator = Data - F1*QCD - F2*FakeTT - EWK - ST rhDict_den_noSF["TTinData-SR-Genuine"] = rhDict_den_noSF[ "Data-SR-Inclusive"].Clone("genuine t#bar{t} (Data)") rhDict_den_noSF["TTinData-SR-Genuine"].Add( rhDict_den_noSF["NormQCD-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Genuine"].Add( rhDict_den_noSF["NormTT-SR-Fake"], -1) rhDict_den_noSF["TTinData-SR-Genuine"].Add( rhDict_den_noSF["EWK-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Genuine"].Add( rhDict_den_noSF["SingleTop-SR-Inclusive"], -1) # (D2) Genuine TT in Data - Numerator = Data - [F1*QCD - F2*FakeTT - ST - EWK] * SF rhDict_num_noSF["TTinData-SR-Genuine"] = rhDict_num_noSF[ "Data-SR-Inclusive"].Clone("genuine t#bar{t} (Data)") rhDict_num_noSF["TTinData-SR-Genuine"].Add( rhDict_num_withSF["NormQCD-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Genuine"].Add( rhDict_num_withSF["NormTT-SR-Fake"], -1) rhDict_num_noSF["TTinData-SR-Genuine"].Add( rhDict_num_withSF["SingleTop-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Genuine"].Add( rhDict_num_withSF["EWK-SR-Inclusive"], -1) # ======================================================================================== # (E) Plot Numerator and Denominator (Data Vs MC) # ======================================================================================== _kwargs = GetHistoKwargs(num_histoList[0]) _kwargs["opts"] = {"xmax": 800, "ymaxfactor": 2.0} _kwargs["ratioYlabel"] = "Data/MC" _kwargs["ratio"] = True _kwargs["stackMCHistograms"] = False _kwargs["createLegend"] = {"x1": 0.60, "y1": 0.75, "x2": 0.95, "y2": 0.92} _kwargs["ratioInvert"] = True num_data.Reset() den_data.Reset() num_mc.Reset() den_mc.Reset() num_data = rhDict_num_noSF["TTinData-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (Data)") den_data = rhDict_den_noSF["TTinData-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (Data)") num_mc = rhDict_num_noSF["TT-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (MC)") den_mc = rhDict_den_noSF["TT-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (MC)") num_data.Rebin(2) num_mc.Rebin(2) den_data.Rebin(2) den_mc.Rebin(2) styles.getABCDStyle("CR4").apply(num_mc) styles.getABCDStyle("CR4").apply(den_mc) # Denominator hName = "GenuineTT_SR_Denominator" hData_Den = histograms.Histo(den_data, "genuine t#bar{t}_{SR} (Data)", "Data", drawStyle="AP") hData_Den.setIsDataMC(isData=True, isMC=False) hMC_Den = histograms.Histo(den_mc, "genuine t#bar{t}_{SR} (MC)", "MC", drawStyle="HIST") hMC_Den.setIsDataMC(isData=False, isMC=True) pDenumerator = plots.ComparisonManyPlot(hData_Den, [hMC_Den], saveFormats=[]) pDenumerator.setLuminosity(opts.intLumi) pDenumerator.setDefaultStyles() plots.drawPlot(pDenumerator, hName, **_kwargs) SavePlot(pDenumerator, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png"]) # Numerator hName = "GenuineTT_SR_Numerator" hData_Num = histograms.Histo(num_data, "genuine t#bar{t}_{SR} (Data)", "Data", drawStyle="AP") hData_Num.setIsDataMC(isData=True, isMC=False) hMC_Num = histograms.Histo(num_mc, "genuine t#bar{t}_{SR} (MC)", "MC", drawStyle="HIST") hMC_Num.setIsDataMC(isData=False, isMC=True) pNumerator = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[]) pNumerator.setLuminosity(opts.intLumi) pNumerator.setDefaultStyles() plots.drawPlot(pNumerator, hName, **_kwargs) SavePlot(pNumerator, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png"]) # ======================================================================================== # (F) Plot Genuine TT Efficiency # ======================================================================================== _kwargs = { "xlabel": "p_{T} (GeV/c)", "ylabel": "Efficiency", "ratioYlabel": "Data/MC", "ratio": True, "ratioInvert": False, "ratioType": None, #"errorScale", "ratioCreateLegend": True, "ratioMoveLegend": { "dx": -0.51, "dy": 0.03, "dh": -0.08 }, "ratioErrorOptions": { "numeratorStatSyst": False, "denominatorStatSyst": False }, "errorBarsX": True, "stackMCHistograms": False, "addMCUncertainty": False, "addLuminosityText": False, "addCmsText": True, "cmsExtraText": "Preliminary", "opts": { "ymin": 0.0, "ymaxfactor": 1.25, "xmax": 600 }, "opts2": { "ymin": 0.55, "ymax": 1.55 }, "log": False, "createLegend": { "x1": 0.54, "y1": 0.80, "x2": 0.95, "y2": 0.92 }, } _kwargs["cutBoxY"] = { "cutValue": 1.10, "fillColor": ROOT.kGray + 1, "fillStyle": 3001, "box": False, "line": True, "greaterThan": True, "mainCanvas": False, "ratioCanvas": True, "mirror": True } bins = [0, 100, 200, 300, 400, 500, 600] xBins = array.array('d', bins) nx = len(xBins) - 1 # Data h1_data_den = rhDict_den_noSF["TTinData-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (Data)") h1_data_num = rhDict_num_noSF["TTinData-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (Data)") # MC h1_mc_den = rhDict_den_noSF["TT-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (MC)") h1_mc_num = rhDict_num_noSF["TT-SR-Genuine"].Clone( "genuine t#bar{t}_{SR} (MC)") h1_data_den = h1_data_den.Rebin(nx, "", xBins) h1_data_num = h1_data_num.Rebin(nx, "", xBins) h1_mc_den = h1_mc_den.Rebin(nx, "", xBins) h1_mc_num = h1_mc_num.Rebin(nx, "", xBins) h_Numerator_Data, h_Denominator_Data = GetHistosForEfficiency( h1_data_num, h1_data_den) h_Numerator_MC, h_Denominator_MC = GetHistosForEfficiency( h1_mc_num, h1_mc_den) effi_data = ROOT.TEfficiency(h_Numerator_Data, h_Denominator_Data) effi_mc = ROOT.TEfficiency(h_Numerator_MC, h_Denominator_MC) effi_data.SetStatisticOption(ROOT.TEfficiency.kFCP) effi_mc.SetStatisticOption(ROOT.TEfficiency.kFCP) geffi_data = convert2TGraph(effi_data) geffi_mc = convert2TGraph(effi_mc) styles.dataStyle.apply(geffi_data) styles.ttStyle.apply(geffi_mc) Graph_Data = histograms.HistoGraph(geffi_data, "genuine t#bar{t}_{SR} (Data) ", "p", "P") Graph_MC = histograms.HistoGraph(geffi_mc, "genuine t#bar{t}_{SR} (MC)", "p", "P") pp = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[]) saveName = "Efficiency_GenuineTT_SR" savePath = os.path.join(opts.saveDir, opts.optMode) plots.drawPlot(pp, savePath, **_kwargs) SavePlot(pp, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf", ".C"]) # Save results in JSON if (not opts.inclusiveEff): name = opts.noSFcrab.split("_")[-4] name = name.replace(opts.analysisName, "") jsonName = "toptagEff_BDT" + name + "_GenuineTT_TopMassCut400.json" runRange = "273150-284044" analysis = opts.analysisName label = "2016" plotDir = os.path.join(opts.folder, jsonName) pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi, geffi_data) pythonWriter.addMCParameters(label, geffi_mc) fileName_json = jsonName pythonWriter.writeJSON(fileName_json) return
def printSummaryInfo(columnNames, myNuisanceInfo, cachedHistos, hObs, m, luminosity, opts): config = aux.load_module(opts.settings) def addOrReplace(dictionary, key, newItem): if not key in dictionary.keys(): dictionary[key] = newItem.Clone() else: dictionary[key].Add(newItem) def getHisto(cachedHistos, name): for h in cachedHistos: if h.GetName() == name: return h raise Exception("Cannot find histogram '%s'!" % name) # Create for each column a root histo with uncertainties myDict = OrderedDict() myTotal = None # Loop over columns (datasets) for c in columnNames: # Bugfix to prevent crashing with blacklisted datasets if c in config.Blacklist: continue hRate = aux.Clone(getHisto(cachedHistos, c)) myRHWU = RootHistoWithUncertainties(hRate) for n in myNuisanceInfo: # Add shape uncertainties if n["name"] != "observation" and n[ "distribution"] == "shape" and n[ c] == "1" and not "statBin" in n["name"]: hUp = aux.Clone( getHisto(cachedHistos, "%s_%sUp" % (c, n["name"]))) hDown = aux.Clone( getHisto(cachedHistos, "%s_%sDown" % (c, n["name"]))) myRHWU.addShapeUncertaintyFromVariation(n["name"], hUp, hDown) # Add constant uncertainties elif n["name"] != "observation" and n["name"] != "rate" and n[ "name"] != "process" and n[c] != "-" and n[ c] != "1" and not "statBin" in n[ "name"] and not "BinByBin" in n["name"]: diffUp = 0.0 diffDown = 0.0 if "/" in n[c]: mySplit = n[c].split("/") diffDown = float(mySplit[0]) - 1.0 diffUp = float(mySplit[1]) - 1.0 else: diffDown = float(n[c]) - 1.0 diffUp = float(n[c]) - 1.0 myRHWU.addNormalizationUncertaintyRelative( n["name"], diffUp, diffDown) # Store column info _myBr = 0.01 myAddToTotalStatus = False if c.startswith("HH") or c.startswith("CMS_Hptntj_HH"): myRHWU.Scale(_myBr**2) addOrReplace(myDict, "Hp", myRHWU) elif c.startswith("HW") or c.startswith("CMS_Hptntj_HW"): myRHWU.Scale(2.0 * _myBr * (1.0 - _myBr)) addOrReplace(myDict, "Hp", myRHWU) elif c.startswith("Hp") or c.startswith("CMS_Hptntj_Hp"): addOrReplace(myDict, "Hp", myRHWU) elif c == "EWK_Tau" or c.startswith("CMS_Hptntj_EWK_Tau"): addOrReplace(myDict, "EWKtau", myRHWU) myAddToTotalStatus = True elif c.endswith("genuinetau"): addOrReplace(myDict, c.replace("CMS_Hptntj_", ""), myRHWU) myAddToTotalStatus = True elif c.endswith("faketau"): addOrReplace(myDict, "EWKfakes", myRHWU) myAddToTotalStatus = True elif c.startswith("QCD") or c.startswith("CMS_Hptntj_QCD"): addOrReplace(myDict, "QCD", myRHWU) myAddToTotalStatus = True else: myDict[c] = myRHWU myAddToTotalStatus = True if myAddToTotalStatus: if myTotal == None: myTotal = myRHWU.Clone() else: myTotal.Add(myRHWU.Clone()) myDict["Totalbkg"] = myTotal # Make table print "\nEvent yields:" myTotal = None for item in myDict.keys(): if myDict[item] != None: myDict[item].makeFlowBinsVisible() rate = myDict[item].getRate() stat = myDict[item].getRateStatUncertainty() (systUp, systDown) = myDict[item].getRateSystUncertainty() #myDict[item].Debug() print "%11s: %.1f +- %.1f (stat.) + %.1f - %.1f (syst.)" % ( item, rate, stat, systUp, systDown) print "Observation: %d\n\n" % hObs.Integral(0, hObs.GetNbinsX() + 2) def setTailFitUncToStat(rhwu): tailfitNames = filter(lambda n: "_TailFit_" in n, rhwu.getShapeUncertaintyNames()) rhwu.setShapeUncertaintiesAsStatistical(tailfitNames) #rhwu.printUncertainties() #print rhwu.getShapeUncertaintiesAsStatistical() return rhwu myLogList = [False, True] for l in myLogList: # Create post fit shape myStackList = [] if "QCD" in myDict.keys(): myHisto = histograms.Histo( setTailFitUncToStat(myDict["QCD"].Clone()), "QCD", legendLabel=ControlPlotMaker._legendLabelQCD) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) if "EWKtau" in myDict.keys(): myHisto = histograms.Histo( setTailFitUncToStat(myDict["EWKtau"].Clone()), "Embedding", legendLabel=ControlPlotMaker._legendLabelEmbedding) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) for c in myDict.keys(): if c.endswith("genuinetau"): histoID = c.replace("CMS_Hptntj_", "").replace("_genuinetau", "") lookupTable = { # Map column name to style in plots.py "tt": "TT", "W": "WJets", "t": "SingleTop", "DY": "DYJetsToLL", "VV": "Diboson", "tt_and_singleTop": "TTandSingleTop", "EWK": "EWK" } histoString = lookupTable[histoID] myHisto = histograms.Histo( setTailFitUncToStat(myDict[c].Clone()), histoString, legendLabel=c.replace("CMS_Hptntj_", "")) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) if "EWKfakes" in myDict.keys() and myDict["EWKfakes"] != None: myHisto = histograms.Histo( myDict["EWKfakes"].Clone(), "EWKfakes", legendLabel=ControlPlotMaker._legendLabelEWKFakes) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) myBlindedStatus = not opts.unblinded myBlindingString = None hObsLocal = aux.Clone(hObs) if myBlindedStatus: myBlindingString = "%d-%d GeV" % (hObs.GetXaxis().GetBinLowEdge(1), hObs.GetXaxis().GetBinUpEdge( hObs.GetNbinsX())) for i in range(0, hObs.GetNbinsX()): hObsLocal.SetBinContent(i, -1.0) hObsLocal.SetBinError(i, 0.0) # Add data myDataHisto = histograms.Histo(hObsLocal, "Data") myDataHisto.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, myDataHisto) # Add signal mySignalLabel = "TTToHplus_M%d" % float(m) if float(m) > 179: mySignalLabel = "HplusTB_M%d" % float(m) myHisto = histograms.Histo(myDict["Hp"].Clone(), mySignalLabel) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(1, myHisto) # Make plot myStackPlot = plots.DataMCPlot2(myStackList) myStackPlot.setLuminosity(luminosity) #myStackPlot.setEnergy("%d"%self._config.OptionSqrtS) myStackPlot.setDefaultStyles() myParams = {} if myBlindedStatus: myParams["blindingRangeString"] = myBlindingString myParams["cmsTextPosition"] = "right" myParams["ratio"] = True myParams["ratioType"] = "errorScale" myParams["ratioYlabel"] = "Data/Bkg. " myParams["stackMCHistograms"] = True myParams["addMCUncertainty"] = True myParams["addLuminosityText"] = True myParams["moveLegend"] = {"dx": -0.14, "dy": -0.10} myParams["ratioErrorOptions"] = {"numeratorStatSyst": False} myParams["ratioCreateLegend"] = True #myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03} myParams["ratioMoveLegend"] = {"dx": -0.06, "dy": -0.1} myParams["opts2"] = {"ymin": 0.0, "ymax": 2.5} myParams["xlabel"] = "m_{T} (GeV)" #if l: # myParams["ylabel"] = "< Events / bin >" #else: myParams["ylabel"] = "Events / 20 GeV" a = hObsLocal.GetXaxis().GetBinWidth(1) b = hObsLocal.GetXaxis().GetBinWidth(hObsLocal.GetNbinsX()) #if abs(a-b) < 0.0001: #myParams["ylabel"] += "%d GeV"%a #else: #myParams["ylabel"] += "%d-%d GeV"%(a,b) #myParams["divideByBinWidth"] = l myParams["log"] = l myPlotName = "PostTailFitShape_M%d" % float(m) if l: # scale ymin by 20 in order to compare the rebinned mT with same y-scale # ymax(factor) takes care of max automatically myParams["opts"] = {"ymin": 20 * 1e-5} else: myParams["opts"] = {"ymin": 0.0} myPlotName += "_Linear" plots.drawPlot(myStackPlot, myPlotName, **myParams)
def __init__(self, opts, config, dirname, luminosity, observation, datasetGroups): plots._legendLabels["MCStatError"] = "Bkg. stat." plots._legendLabels["MCStatSystError"] = "Bkg. stat.#oplussyst." plots._legendLabels["BackgroundStatError"] = "Bkg. stat. unc" plots._legendLabels["BackgroundStatSystError"] = "Bkg. stat.#oplussyst. unc." if config.ControlPlots == None: return myStyle = tdrstyle.TDRStyle() myStyle.setOptStat(False) self._opts = opts self._config = config if config.OptionSqrtS == None: raise Exception(ShellStyles.ErrorLabel()+"Please set the parameter OptionSqrtS = <integer_value_in_TeV> in the config file!"+ShellStyles.NormalStyle()) self._dirname = dirname self._luminosity = luminosity self._observation = observation self._datasetGroups = datasetGroups #myEvaluator = SignalAreaEvaluator() # Make control plots print "\n"+ShellStyles.HighlightStyle()+"Generating control plots"+ShellStyles.NormalStyle() # Loop over mass points massPoints = [] massPoints.extend(self._config.MassPoints) massPoints.append(-1) # for plotting with no signal for m in massPoints: print "... mass = %d GeV"%m # Initialize flow plot selectionFlow = SelectionFlowPlotMaker(self._opts, self._config, m) myBlindedStatus = False for i in range(0,len(self._config.ControlPlots)): if observation.getControlPlotByIndex(i) != None: myCtrlPlot = self._config.ControlPlots[i] print "......", myCtrlPlot.title myMassSuffix = "_M%d"%m # Initialize histograms hSignal = None hQCD = None hQCDdata = None hEmbedded = None hEWKfake = None hData = None # Loop over dataset columns to find histograms myStackList = [] for c in self._datasetGroups: if (m < 0 or c.isActiveForMass(m,self._config)) and not c.typeIsEmptyColumn() and not c.getControlPlotByIndex(i) == None: h = c.getControlPlotByIndex(i)["shape"].Clone() if c.typeIsSignal(): #print "signal:",c.getLabel() # Scale light H+ signal if m < 179: if c.getLabel()[:2] == "HH": h.Scale(self._config.OptionBr**2) elif c.getLabel()[:2] == "HW": h.Scale(2.0*self._config.OptionBr*(1.0-self._config.OptionBr)) if hSignal == None: hSignal = h.Clone() else: hSignal.Add(h) elif c.typeIsQCDinverted(): print "------------" print "ollaanko nyt vaarassa paikassa" if hQCDdata == None: hQCDdata = h.Clone() else: hQCDdata.Add(h) elif c.typeIsQCD(): if hQCD == None: print "hQCD add kai onnistuu" hQCD = h.Clone() else: print "hQCD add onnistuu" hQCD.Add(h) elif c.typeIsEWK(): #print "EWK genuine:",c.getLabel(),h.getRootHisto().Integral(0,h.GetNbinsX()+2) if not self._config.OptionGenuineTauBackgroundSource == "DataDriven": myHisto = histograms.Histo(h,c._datasetMgrColumn) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) else: if hEmbedded == None: hEmbedded = h.Clone() else: hEmbedded.Add(h) elif c.typeIsEWKfake(): #print "EWK fake:",c.getLabel(),h.getRootHisto().Integral(0,h.GetNbinsX()+2) if hEWKfake == None: hEWKfake = h.Clone() else: hEWKfake.Add(h) if len(myStackList) > 0 or self._config.OptionGenuineTauBackgroundSource == "DataDriven": if hQCDdata != None: myHisto = histograms.Histo(hQCDdata,"QCDdata",legendLabel=_legendLabelQCDdata) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(0, myHisto) elif hQCD != None: myHisto = histograms.Histo(hQCD,"QCDdata",legendLabel=_legendLabelQCD) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(0, myHisto) if hEmbedded != None: myHisto = histograms.Histo(hEmbedded,"Embedding",legendLabel=_legendLabelEmbedding) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) if hEWKfake != None: myHisto = histograms.Histo(hEWKfake,"EWKfakes",legendLabel=_legendLabelEWKFakes) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) hData = observation.getControlPlotByIndex(i)["shape"].Clone() hDataUnblinded = hData.Clone() # Apply blinding myBlindingString = None if self._config.BlindAnalysis: if len(myCtrlPlot.blindedRange) > 0: myBlindingString = self._applyBlinding(hData,myCtrlPlot.blindedRange) if self._config.OptionBlindThreshold != None: for k in xrange(1, hData.GetNbinsX()+1): myExpValue = 0.0 for item in myStackList: myExpValue += item.getRootHisto().GetBinContent(k) if hSignal.getRootHisto().GetBinContent(k) >= myExpValue * self._config.OptionBlindThreshold: hData.getRootHisto().SetBinContent(k, -1.0) hData.getRootHisto().SetBinError(k, 0.0) # Data myDataHisto = histograms.Histo(hData,"Data") myDataHisto.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, myDataHisto) # Add signal if m > 0: mySignalLabel = "TTToHplus_M%d"%m if m > 179: mySignalLabel = "HplusTB_M%d"%m myHisto = histograms.Histo(hSignal,mySignalLabel) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(1, myHisto) # Add data to selection flow plot #if myBlindedStatus: # selectionFlow.addColumn(myCtrlPlot.flowPlotCaption,None,myStackList[1:]) #else: selectionFlow.addColumn(myCtrlPlot.flowPlotCaption,hDataUnblinded,myStackList[1:]) if len(myCtrlPlot.blindedRange) > 0: myBlindedStatus = True else: myBlindedStatus = False # Make plot myStackPlot = None myParams = myCtrlPlot.details.copy() #if not isinstance(hData, ROOT.TH2): #for j in range(1,myStackList[0].getRootHisto().GetNbinsY()+1): #for i in range(1,myStackList[0].getRootHisto().GetNbinsX()+1): #mySum = 0.0 #for h in range(2, len(myStackList)): #mySum += myStackList[h].getRootHisto().GetBinContent(i,j) #if mySum > 0.0: #myStackList[0].getRootHisto().SetBinContent(i,j,myStackList[0].getRootHisto().GetBinContent(i,j) / mySum) #else: #myStackList[0].getRootHisto().SetBinContent(i,j,-10.0) #myStackList[0].getRootHisto().SetMinimum(-1.0) #myStackList[0].getRootHisto().SetMaximum(1.0) #myStackList = [myStackList[0]] #myStackPlot = plots.PlotBase(myStackList) #if "ylabelBinInfo" in myParams: #del myParams["ylabelBinInfo"] #del myParams["unit"] #drawPlot2D(myStackPlot, "%s/DataDrivenCtrlPlot_M%d_%02d_%s"%(self._dirname,m,i,myCtrlPlot.title), **myParams) myStackPlot = plots.DataMCPlot2(myStackList) myStackPlot.setLuminosity(self._luminosity) myStackPlot.setEnergy("%d"%self._config.OptionSqrtS) myStackPlot.setDefaultStyles() # Tweak paramaters if not "unit" in myParams.keys(): myParams["unit"] = "" if myParams["unit"] != "": myParams["xlabel"] = "%s (%s)"%(myParams["xlabel"],myParams["unit"]) ylabelBinInfo = True if "ylabelBinInfo" in myParams: ylabelBinInfo = myParams["ylabelBinInfo"] del myParams["ylabelBinInfo"] if ylabelBinInfo: myMinWidth = 10000.0 myMaxWidth = 0.0 for j in range(1,hData.getRootHisto().GetNbinsX()+1): w = hData.getRootHisto().GetBinWidth(j) if w < myMinWidth: myMinWidth = w if w > myMaxWidth: myMaxWidth = w myWidthSuffix = "" myMinWidthString = "%d"%myMinWidth myMaxWidthString = "%d"%myMaxWidth if myMinWidth < 1.0: myFormat = "%%.%df"%(abs(int(log10(myMinWidth)))+1) myMinWidthString = myFormat%myMinWidth if myMaxWidth < 1.0: myFormat = "%%.%df"%(abs(int(log10(myMaxWidth)))+1) myMaxWidthString = myFormat%myMaxWidth myWidthSuffix = "%s-%s"%(myMinWidthString,myMaxWidthString) if abs(myMinWidth-myMaxWidth) < 0.001: myWidthSuffix = "%s"%(myMinWidthString) if not (myParams["unit"] == "" and myWidthSuffix == "1"): myParams["ylabel"] = "%s / %s %s"%(myParams["ylabel"],myWidthSuffix,myParams["unit"]) if myBlindingString != None: if myParams["unit"] != "" and myParams["unit"][0] == "^": myParams["blindingRangeString"] = "%s%s"%(myBlindingString, myParams["unit"]) else: myParams["blindingRangeString"] = "%s %s"%(myBlindingString, myParams["unit"]) if "legendPosition" in myParams.keys(): if myParams["legendPosition"] == "NE": myParams["moveLegend"] = {"dx": -0.10, "dy": -0.02} elif myParams["legendPosition"] == "SE": myParams["moveLegend"] = {"dx": -0.10, "dy": -0.56} elif myParams["legendPosition"] == "SW": myParams["moveLegend"] = {"dx": -0.53, "dy": -0.56} elif myParams["legendPosition"] == "NW": myParams["moveLegend"] = {"dx": -0.53, "dy": -0.02} else: raise Exception("Unknown value for option legendPosition: %s!", myParams["legendPosition"]) del myParams["legendPosition"] elif not "moveLegend" in myParams: myParams["moveLegend"] = {"dx": -0.10, "dy": -0.02} # default: NE if "ratioLegendPosition" in myParams.keys(): if myParams["ratioLegendPosition"] == "left": myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03} elif myParams["ratioLegendPosition"] == "right": myParams["ratioMoveLegend"] = {"dx": -0.08, "dy": 0.03} elif myParams["ratioLegendPosition"] == "SE": myParams["ratioMoveLegend"] = {"dx": -0.08, "dy": -0.33} else: raise Exception("Unknown value for option ratioLegendPosition: %s!", myParams["ratioLegendPosition"]) del myParams["ratioLegendPosition"] else: if not "ratioMoveLegend" in myParams: myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03} # default: left # Remove non-dientified keywords del myParams["unit"] # Ratio axis if not "opts2" in myParams.keys(): myParams["opts2"] = {"ymin": 0.3, "ymax": 1.7} # Do plotting if m > 0: drawPlot(myStackPlot, "%s/DataDrivenCtrlPlot_M%d_%02d_%s"%(self._dirname,m,i,myCtrlPlot.title), **myParams) else: drawPlot(myStackPlot, "%s/DataDrivenCtrlPlot_%02d_%s"%(self._dirname,i,myCtrlPlot.title), **myParams) # Do selection flow plot selectionFlow.makePlot(self._dirname,m,len(self._config.ControlPlots),self._luminosity) #myEvaluator.save(dirname) print "Control plots done"
def PlotHistogram(dsetMgr, histoName, opts): # Get kistogram argumetns kwargs = GetHistoKwargs(histoName, opts) saveName = histoName.replace(opts.folder + "/", "") # Create the plotting object (Data, "FakeB") p1 = plots.DataMCPlot(dsetMgr, histoName, saveFormats=[]) # Copy dataset manager before changing datasets. Keep only EWK (GenuineB) datasets datasetMgr = dsetMgr.deepCopy() datasetMgr.selectAndReorder(aux.GetListOfEwkDatasets()) # Create the MCPlot for the EWKGenuineB histograms if opts.useMC: p2 = plots.MCPlot(datasetMgr, histoName, normalizeToLumi=opts.intLumi, saveFormats=[]) else: histoNameGenuineB = histoName.replace(opts.folder, opts.folder + "EWKGenuineB") p2 = plots.MCPlot(datasetMgr, histoNameGenuineB, normalizeToLumi=opts.intLumi, saveFormats=[]) # Add the datasets to be included in the plot myStackList = [] # Data-driven FakeB background if not opts.useMC: hFakeB = p1.histoMgr.getHisto("FakeB").getRootHisto() hhFakeB = histograms.Histo(hFakeB, "FakeB", legendLabel="Fake-b") hhFakeB.setIsDataMC(isData=False, isMC=True) myStackList.append(hhFakeB) else: hQCD = p1.histoMgr.getHisto("QCD").getRootHisto() hhQCD = histograms.Histo(hQCD, "QCD", legendLabel="QCD") hhQCD.setIsDataMC(isData=False, isMC=True) myStackList.append(hhQCD) # EWK GenuineB background (Replace all EWK histos with GenuineB histos) ewkHistoList = [] # For-loop: All EWK datasets for dataset in aux.GetListOfEwkDatasets(): h = p2.histoMgr.getHisto(dataset).getRootHisto() hh = histograms.Histo(h, dataset, plots._legendLabels[dataset]) hh.setIsDataMC(isData=False, isMC=True) myStackList.append(hh) # Collision data hData = p1.histoMgr.getHisto("Data").getRootHisto() hhData = histograms.Histo(hData, "Data") hhData.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, hhData) # Signal hSignal = p1.histoMgr.getHisto(opts.signal).getRootHisto() hhSignal = histograms.Histo(hSignal, opts.signal, plots._legendLabels[opts.signal]) hhSignal.setIsDataMC(isData=False, isMC=True) myStackList.insert(1, hhSignal) # Create the final plot by passing the histogram list p3 = plots.DataMCPlot2(myStackList, saveFormats=[]) p3.setLuminosity(opts.intLumi) p3.setDefaultStyles() # Apply blinding of data in Signal Region (After creating the plot) if "blindingRangeString" in kwargs: startBlind = float(kwargs["blindingRangeString"].split("-")[1]) endBlind = float(kwargs["blindingRangeString"].split("-")[0]) plots.partiallyBlind(p3, maxShownValue=startBlind, minShownValue=endBlind, invert=True, moveBlindedText=kwargs["moveBlindedText"]) # Draw and save the plot plots.drawPlot(p3, saveName, **kwargs) SavePlot(p3, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".pdf"]) return
def PlotSignalBackground(datasetsMgr, hG, hF, intLumi): kwargs = {} _kwargs = {} if opts.normaliseToOne: pG = plots.MCPlot(datasetsMgr, hG, normalizeToOne=True, saveFormats=[], **_kwargs) pF = plots.MCPlot(datasetsMgr, hF, normalizeToOne=True, saveFormats=[], **_kwargs) else: pG = plots.MCPlot(datasetsMgr, hG, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) pF = plots.MCPlot(datasetsMgr, hF, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) # Draw the histograms _cutBox = None _rebinX = 1 _format = "%0.2f" _xlabel = None logY = False _opts = {"ymin": 0, "ymaxfactor": 1.1} if "mass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "M (%s)" % _units if "trijetmass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{top} (%s)" % _units #_cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True} _opts["xmax"] = 805 #1005 _opts = {"xmin": 0.0, "xmax": 805} if "bjetmass" in hG.lower(): _xlabel = "m_{b-tagged jet} (%s)" % _units _opts["xmax"] = 50 if "bjetldgjet_mass" in hG.lower(): _xlabel = "m_{b, ldg jet} (%s)" % _units _opts["xmax"] = 705 if "bjetsubldgjet_mass" in hG.lower(): _xlabel = "m_{b-tagged, subldg jet} (%s)" % _units _opts["xmax"] = 705 if "jet_mass" in hG.lower(): _opts["xmax"] = 750 if "mult" in hG.lower(): _format = "%0.0f" if "ldg" in hG.lower(): _xlabel = "Leading jet mult" if "subldg" in hG.lower(): _xlabel = "Subleading jet mult" if "avg" in hG.lower(): _xlabel = "avg CvsL" if "cvsl" in hG.lower(): _format = "%0.2f" if "ldg" in hG.lower(): _xlabel = "Leading jet CvsL" if "subldg" in hG.lower(): _xlabel = "Subleading jet CvsL" if "avg" in hG.lower(): _xlabel = "avg CvsL" if "axis2" in hG.lower(): _format = "%0.3f" if "ldg" in hG.lower(): _xlabel = "Leading jet axis2" if "subldg" in hG.lower(): _xlabel = "Subleading jet axis2" if "avg" in hG.lower(): _xlabel = "avg axis2" if "dijetmass" in hG.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{W} (%s)" % _units _opts["xmax"] = 600 _opts = {"xmin": 0.0, "xmax": 605, "ymin": 1e-3, "ymaxfactor": 1.0} if "trijetptdr" in hG.lower(): _opts["xmax"] = 800 _format = "%0.0f" _xlabel = "p_{T}#Delta R_{t}" if "dijetptdr" in hG.lower(): _opts["xmax"] = 800 _format = "%0.0f" _xlabel = "p_{T}#Delta R_{W}" if "dgjetptd" in hG.lower(): _format = "%0.2f" _xlabel = "Leading jet p_{T}D" if "subldg" in hG.lower(): _xlabel = "Subleading jet p_{T}D" if "bdisc" in hG.lower(): _format = "%0.2f" if "subldg" in hG.lower(): _xlabel = "Subleading jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.06} elif "ldg" in hG.lower(): _xlabel = "Leading jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.06} else: _xlabel = "b-tagged jet CSV" _opts = {"ymin": 1e-3, "ymax": 0.35} if "over" in hG.lower(): _format = "%0.2f " _xlabel = "m_{W}/m_{t}" _opts["xmax"] = 1 _opts["xmin"] = 0 if "likelihood" in hG.lower(): _format = "%0.2f" if "ldg" in hG.lower(): _xlabel = "Leading jet QGL" if "subldg" in hG.lower(): _xlabel = "Subleading jet QGL" if "avg" in hG.lower(): _xlabel = "avg QGL" else: pass ''' if "bdisc" in hG.lower(): _format = "%0.2f" if "subldg" in hG.lower(): _xlabel = "Subleading jet CSV" elif "ldg" in hG.lower(): _xlabel = "Leading jet CSV" else: _xlabel = "b-tagged jet CSV" ''' if logY: yMaxFactor = 2.0 else: yMaxFactor = 1.2 _opts["ymaxfactor"] = yMaxFactor if opts.normaliseToOne: _opts["ymin"] = 1e-3 else: _opts["ymin"] = 1e0 myList = [] # Customise styling pG.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) pF.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) #Dataset: ttbar dataset = datasetsMgr.getDataset(opts.dataset) #soti #dataset = datasetsMgr.getDataset("ChargedHiggs_HplusTB_HplusToTB_M_1000") #Get Genuine-top histogram h = dataset.getDatasetRootHisto(hG) h.normalizeToOne() HG = h.getHistogram() #Get Fake-top histogram h = dataset.getDatasetRootHisto(hF) h.normalizeToOne() HF = h.getHistogram() #Define Signal style altSignalBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kAzure + 9, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kAzure + 9, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kAzure + 9) ]) #Define Background style altBackgroundBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kRed - 4, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kRed - 4, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kRed - 4, fillStyle=3001) ]) signalBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kTeal + 2, markerSizes=None, markerStyle=ROOT.kFullTriangleUp), styles.StyleLine(lineColor=ROOT.kTeal + 2, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kTeal + 2) ]) #, fillStyle=3001)]) signalGrayStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kGray + 1, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kGray + 1, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kGray + 1) ]) backgroundGrayStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kGray + 3, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kGray + 3, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kGray + 3, fillStyle=3001) ]) #Comparison Plot p = plots.ComparisonPlot( histograms.Histo(HF, "Fake", "p", "P"), histograms.Histo(HG, "Genuine", "pl", "PL"), ) #Set labels p.histoMgr.setHistoLegendLabelMany({ "Fake": "Unmatched", "Genuine": "Truth-matched" }) #Set Draw style p.histoMgr.forHisto("Fake", altBackgroundBDTGStyle) p.histoMgr.setHistoDrawStyle("Fake", "LP") p.histoMgr.setHistoLegendStyle("Fake", "LP") #F p.histoMgr.forHisto("Genuine", altSignalBDTGStyle) p.histoMgr.setHistoDrawStyle("Genuine", "HIST") p.histoMgr.setHistoLegendStyle("Genuine", "LP") #LP if "avg" in hG.lower() or "likelihood" in hG.lower() or "over" in hG.lower( ): p.histoMgr.forHisto("Genuine", signalBDTGStyle) p.histoMgr.setHistoDrawStyle("Genuine", "HIST") p.histoMgr.setHistoLegendStyle("Genuine", "LP") #LP if "Gray" in opts.mcrab: p.histoMgr.forHisto("Fake", backgroundGrayStyle) p.histoMgr.forHisto("Genuine", signalGrayStyle) histoG = histograms.Histo(HG, "TT", "Signal") histoG.setIsDataMC(isData=False, isMC=True) histoF = histograms.Histo(HF, "QCD", "Signal") histoF.setIsDataMC(isData=False, isMC=True) styleG = styles.ttStyle styleF = styles.signalStyleHToTB1000 styleG.apply(HG) styleF.apply(HF) myList.append(histoG) myList.append(histoF) _kwargs = { "xlabel": _xlabel, "ylabel": "Arbitrary Units / %s" % (_format), "ratioYlabel": "Ratio ", "ratio": False, "ratioInvert": False, "stackMCHistograms": False, "addMCUncertainty": False, "addLuminosityText": False, "addCmsText": True, "cmsExtraText": "Preliminary", #"opts" : {"ymin": 0.0, "ymaxfactor": 1.1}, "opts": _opts, "opts2": { "ymin": 0.6, "ymax": 1.5 }, "log": False, #"createLegend" : {"x1": 0.5, "y1": 0.75, "x2": 0.9, "y2": 0.9}, "createLegend": { "x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.82 }, } # Save plot in all formats saveName = hG.split("/")[-1] #plots.drawPlot(p, saveName, **_kwargs) savePath = os.path.join(opts.saveDir + ANALYSISNAME, "HplusMasses", hG.split("/")[0], opts.optMode) plots.drawPlot(p, savePath, **_kwargs) SavePlot(p, saveName, os.path.join(opts.saveDir + opts.mcrab, opts.optMode), saveFormats=[".png"]) return
def makeVariationPlotDetailed(self, prefix, hNominal, hFit, hFitUncertaintyUp, hFitUncertaintyDown): # Make plot plot = plots.PlotBase() hNominalClone = aux.Clone(hNominal) hNominalClone.SetLineColor(ROOT.kBlack) hNominalClone.SetLineWidth(2) hNominalClone.SetMarkerStyle(22) hNominalClone.SetMarkerSize(1.5) # Remove fit line before drawing myFunctions = hNominalClone.GetListOfFunctions() for i in range(0, myFunctions.GetEntries()): myFunctions.Delete() plot.histoMgr.appendHisto( histograms.Histo(hNominalClone, "nominal", drawStyle="e")) hFitClone = aux.Clone(hFit) hFitClone.SetLineColor(ROOT.kMagenta) hFitClone.SetLineWidth(2) plot.histoMgr.appendHisto(histograms.Histo(hFitClone, "fit")) #for j in range(1, self._hFitFineBinning.GetNbinsX()+1): # print "fit: %d: %f"%(j,self._hFitFineBinning.GetBinContent(j)) myColor = 2 for i in range(0, len(hFitUncertaintyUp)): hUpClone = aux.Clone(hFitUncertaintyUp[i]) hUpClone.SetLineStyle(myColor) hUpClone.SetLineColor(ROOT.kBlue) hUpClone.SetLineWidth(2) myColor += 1 plot.histoMgr.appendHisto( histograms.Histo(hUpClone, "Par%d up" % (i))) myColor = 2 for i in range(0, len(hFitUncertaintyDown)): hDownClone = aux.Clone(hFitUncertaintyDown[i]) hDownClone.SetLineStyle(myColor) hDownClone.SetLineColor(ROOT.kRed) hDownClone.SetLineWidth(2) myColor += 1 plot.histoMgr.appendHisto( histograms.Histo(hDownClone, "Par%d down" % (i))) # ugly hardcoding... fitStart = 180 nominal = "Nominal" if "QCD" in self._label: nominal += " multijets" elif "EWK_Tau" in self._label: nominal += " EWK+t#bar{t} with #tau_{h}" elif "MC_faketau" in self._label: nominal += " EWK+t#bar{t} no #tau_{h}" plot.histoMgr.setHistoLegendLabelMany({ "nominal": nominal, "fit": "With fit for m_{T} > %d GeV" % fitStart, "Par0 up": "+1#sigma uncertainty on par.0", "Par0 down": "-1#sigma uncertainty on par.0", "Par1 up": "+1#sigma uncertainty on par.1", "Par1 down": "-1#sigma uncertainty on par.1" }) if self._luminosity is not None: plot.setLuminosity(self._luminosity) myName = "tailfit_detailed_%s_%s" % (self._label, prefix) #plot.createFrame("tailfit_%s_%s"%(self._label,name), opts={"ymin": 1e-5, "ymaxfactor": 2.}) #plot.getPad().SetLogy(True) #histograms.addStandardTexts(lumi=self.lumi) myParams = {} myParams["ylabel"] = "Events / %.0f GeV" myParams["log"] = True myParams["opts"] = {"ymin": 20 * 1e-5} # compensate for the bin width #myParams["divideByBinWidth"] = True myParams["cmsTextPosition"] = "right" myParams["moveLegend"] = { "dx": -0.215, "dy": -0.1, "dh": -0.1, "dw": -0.05 } myParams["xlabel"] = "m_{T} (GeV)" myDrawer = plots.PlotDrawer() myDrawer(plot, myName, **myParams)
def PlotHistosAndCalculateTF(runRange, datasetsMgr, histoList, binLabels, opts): # Get the histogram customisations (keyword arguments) _kwargs = GetHistoKwargs(histoList[0]) # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CRone", "CRtwo", "CRthree", "CRfour"] rhDict = GetRootHistos(datasetsMgr, histoList, regions, binLabels) #========================================================================================= # Calculate the Transfer Factor (TF) and save to file #========================================================================================= manager = FakeBNormalization.FakeBNormalizationManager(binLabels, opts.mcrab, opts.optMode, verbose=False) if opts.inclusiveOnly: binLabel = "Inclusive" manager.CalculateTransferFactor("Inclusive", rhDict["FakeB-CRone-Inclusive"], rhDict["FakeB-CRtwo-Inclusive"], rhDict["FakeB-CRthree-Inclusive"], rhDict["FakeB-CRfour-Inclusive"]) else: for bin in binLabels: manager.CalculateTransferFactor(bin, rhDict["FakeB-CRone-%s" % bin], rhDict["FakeB-CRtwo-%s" % bin], rhDict["FakeB-CRthree-%s" % bin], rhDict["FakeB-CRfour-%s" % bin]) # Get unique a style for each region for k in rhDict: dataset = k.split("-")[0] region = k.split("-")[1] styles.getABCDStyle(region).apply(rhDict[k]) if "FakeB" in k: styles.getFakeBStyle().apply(rhDict[k]) # sr.apply(rhDict[k]) # ========================================================================================= # Create the final plot object # ========================================================================================= rData_SR = rhDict["Data-SR-Inclusive"] rEWKGenuineB_SR = rhDict["EWK-SR-Inclusive-EWKGenuineB"] rBkgSum_SR = rhDict["FakeB-VR-Inclusive"].Clone("BkgSum-SR-Inclusive") rBkgSum_SR.Reset() if opts.inclusiveOnly: bin = "Inclusive" # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) ) binHisto_VR = rhDict["FakeB-VR-%s" % (bin)] VRtoSR_TF = manager.GetTransferFactor(bin) Verbose( "Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True) binHisto_VR.Scale(VRtoSR_TF) # Add the normalised histogram to the final Inclusive SR (predicted) histo rBkgSum_SR.Add(binHisto_VR, +1) else: # For-loop: All bins for i, bin in enumerate(binLabels, 1): if bin == "Inclusive": continue # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) ) binHisto_VR = rhDict["FakeB-VR-%s" % (bin)] VRtoSR_TF = manager.GetTransferFactor(bin) Verbose( "Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), i == 1) binHisto_VR.Scale(VRtoSR_TF) # Add the normalised histogram to the final Inclusive SR (predicted) histo rBkgSum_SR.Add(binHisto_VR, +1) # Plot histograms if opts.altPlot: # Add the SR EWK Genuine-b to the SR FakeB ( BkgSum = [FakeB] + [GenuineB-MC] = [VR * (CR1/CR2)] + [GenuineB-MC] ) rBkgSum_SR.Add(rEWKGenuineB_SR, +1) # Change style styles.getGenuineBStyle().apply(rBkgSum_SR) # Remove unsupported settings of kwargs _kwargs["stackMCHistograms"] = False _kwargs["addLuminosityText"] = False # Create the plot p = plots.ComparisonManyPlot(rData_SR, [rBkgSum_SR], saveFormats=[]) # Set draw / legend style p.histoMgr.setHistoDrawStyle("Data-SR-Inclusive", "P") p.histoMgr.setHistoLegendStyle("Data-SR-Inclusive", "LP") p.histoMgr.setHistoDrawStyle("BkgSum-SR-Inclusive", "HIST") p.histoMgr.setHistoLegendStyle("BkgSum-SR-Inclusive", "F") # Set legend labels p.histoMgr.setHistoLegendLabelMany({ "Data-SR": "Data", "BkgSum-SR": "Fake-b + Gen-b", }) else: # Create empty histogram stack list myStackList = [] # Add the FakeB data-driven background to the histogram list hFakeB = histograms.Histo(rBkgSum_SR, "FakeB", "Fake-b") hFakeB.setIsDataMC(isData=False, isMC=True) myStackList.append(hFakeB) # Add the EWKGenuineB MC background to the histogram list hGenuineB = histograms.Histo(rEWKGenuineB_SR, "GenuineB", "EWK Genuine-b") hGenuineB.setIsDataMC(isData=False, isMC=True) myStackList.append(hGenuineB) # Add the collision datato the histogram list hData = histograms.Histo(rData_SR, "Data", "Data") hData.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, hData) p = plots.DataMCPlot2(myStackList, saveFormats=[]) p.setLuminosity(opts.intLumi) p.setDefaultStyles() # Draw the plot and save it hName = opts.histoKey plots.drawPlot(p, hName, **_kwargs) SavePlot(p, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png"]) #========================================================================================= # Calculate the Transfer Factor (TF) and save to file #========================================================================================= Verbose("Write the normalisation factors to a python file", True) fileName = os.path.join(opts.mcrab, "FakeBTransferFactors_%s.py" % (runRange)) manager.writeTransferFactorsToFile(fileName, opts) return
def PlotHistos(noSF_datasetsMgr, withCR2SF_datasetsMgr, num_histoList, den_histoList, opts): # Get the histogram customisations (keyword arguments) _kwargs = GetHistoKwargs(num_histoList[0]) # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CR1", "CR2"] labels = ["Genuine", "Fake", "Inclusive"] #========================================================================================== # Get Dictionaries #========================================================================================== rhDict_den_noSF = GetRootHistos(noSF_datasetsMgr, den_histoList, regions) rhDict_num_noSF = GetRootHistos(noSF_datasetsMgr, num_histoList, regions) rhDict_num_withCR2SF = GetRootHistos(withCR2SF_datasetsMgr, num_histoList, regions) # Scale Factors from CR2 are only applied in the Numerator on EWK+QCD+ST #========================================================================================= # Normalization Factors (see: getNormalization.py) #========================================================================================= # Marina f1=0.619886; f2=0.904877; # ========================================================================================= # (A) Apply Normalization Factors (see: getNormalizations.py) # ========================================================================================= # Normalize all histograms (QCD and TT) to normalization factors for re in regions: rhDict_den_noSF["NormQCD-"+re+"-Inclusive"] = rhDict_den_noSF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive") rhDict_den_noSF["NormQCD-"+re+"-Inclusive"].Scale(f1) rhDict_num_noSF["NormQCD-"+re+"-Inclusive"] =rhDict_num_noSF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive") rhDict_num_noSF["NormQCD-"+re+"-Inclusive"].Scale(f1) rhDict_num_withCR2SF["NormQCD-"+re+"-Inclusive"] = rhDict_num_withCR2SF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive") rhDict_num_withCR2SF["NormQCD-"+re+"-Inclusive"].Scale(f1) for la in labels: rhDict_den_noSF["NormTT-"+re+"-"+la] = rhDict_den_noSF["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la) rhDict_den_noSF["NormTT-"+re+"-"+la].Scale(f2) rhDict_num_noSF["NormTT-"+re+"-"+la] = rhDict_num_noSF["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la) rhDict_num_noSF["NormTT-"+re+"-"+la].Scale(f2) # ========================================================================================== # (B) Estimate Inclusive TT in SR # ========================================================================================== # (B1) Inclusive TT in Data - Denominator = Data - F1*QCD - - EWK - ST rhDict_den_noSF["TTinData-SR-Inclusive"] = rhDict_den_noSF["Data-SR-Inclusive"].Clone("t#bar{t} (Data)") rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["NormQCD-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["EWK-SR-Inclusive"], -1) rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["SingleTop-SR-Inclusive"], -1) # (B2) Inclusive TT in Data - Numerator = Data - F1*QCD*SF_{CR2} - ST*SF_{CR2} - EWK*SF_{CR2} rhDict_num_noSF["TTinData-SR-Inclusive"] = rhDict_num_noSF["Data-SR-Inclusive"].Clone("t#bar{t} (Data)") rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["NormQCD-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["SingleTop-SR-Inclusive"], -1) rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["EWK-SR-Inclusive"], -1) # (B3) Inclusive TT in MC - Denominator: rhDict_den_noSF["TT-SR-Inclusive"] # (B4) Inclusive TT in MC - Numerator: rhDict_num_noSF["TT-SR-Inclusive"] # ======================================================================================== # (C) Plot Numerator and Denominator (Data Vs MC) # ======================================================================================== _kwargs["opts"] = {"xmax" : 800, "ymaxfactor" : 2.0} _kwargs["ratioYlabel"] = "Data/MC" _kwargs["ratio"] = True _kwargs["stackMCHistograms"] = False _kwargs["createLegend"] = {"x1": 0.68, "y1": 0.75, "x2": 0.95, "y2": 0.92} _kwargs["ratioInvert"] = True num_data = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)") den_data = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)") num_mc = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)") den_mc = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)") num_data.Rebin(2) num_mc.Rebin(2) den_data.Rebin(2) den_mc.Rebin(2) styles.getABCDStyle("CR4").apply(num_mc) styles.getABCDStyle("CR4").apply(den_mc) # Denominator hName = "InclusiveTT_SR_Denominator_LeadingJet" hData_Den = histograms.Histo( den_data, "t#bar{t} (Data)", "Data", drawStyle="AP"); hData_Den.setIsDataMC(isData=True, isMC=False) hMC_Den = histograms.Histo( den_mc, "t#bar{t} (MC)", "MC", drawStyle="HIST"); hMC_Den.setIsDataMC(isData=False, isMC=True) pDen = plots.ComparisonManyPlot(hData_Den, [hMC_Den], saveFormats=[]) pDen.setLuminosity(opts.intLumi) pDen.setDefaultStyles() plots.drawPlot(pDen, hName, **_kwargs) SavePlot(pDen, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"]) # Numerator hName = "InclusiveTT_SR_Numerator_LeadingJet" hData_Num = histograms.Histo( num_data, "t#bar{t} (Data)", "Data", drawStyle="AP"); hData_Num.setIsDataMC(isData=True, isMC=False) hMC_Num = histograms.Histo( num_mc, "t#bar{t} (MC)", "MC", drawStyle="HIST"); hMC_Num.setIsDataMC(isData=False, isMC=True) pNum = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[]) pNum.setLuminosity(opts.intLumi) pNum.setDefaultStyles() plots.drawPlot(pNum, hName, **_kwargs) SavePlot(pNum, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"]) # ======================================================================================== # (D) Plot Genuine TT Efficiency # ======================================================================================== _kwargs = { "xlabel" : "p_{T} (GeV/c)", "ylabel" : "Efficiency", "ratioYlabel" : "Data/MC", "ratio" : True, "ratioInvert" : False, "ratioType" : "errorScale", "ratioCreateLegend": True, "ratioMoveLegend" : {"dx": -0.51, "dy": 0.03, "dh": -0.08}, "ratioErrorOptions": {"numeratorStatSyst": False, "denominatorStatSyst": False}, "errorBarsX" : True, "stackMCHistograms": False, "addMCUncertainty" : False, "addLuminosityText": False, "addCmsText" : True, "cmsExtraText" : "Preliminary", "opts" : {"ymin": 0.0, "ymaxfactor": 1.28, "xmax" : 600}, "opts2" : {"ymin": 0.72, "ymax": 1.28}, "log" : False, "createLegend" : {"x1": 0.68, "y1": 0.80, "x2": 0.95, "y2": 0.92}, } bins = [0, 100, 200, 300, 400, 500, 600] xBins = array.array('d', bins) nx = len(xBins)-1 # Data h0_data_den = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)") h0_data_num = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)") # MC h0_mc_den = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)") h0_mc_num = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)") h0_data_den = h0_data_den.Rebin(nx, "", xBins) h0_data_num = h0_data_num.Rebin(nx, "", xBins) h0_mc_den = h0_mc_den.Rebin(nx, "", xBins) h0_mc_num = h0_mc_num.Rebin(nx, "", xBins) hNumerator_Data, hDenominator_Data = GetHistosForEfficiency(h0_data_num, h0_data_den) hNumerator_MC, hDenominator_MC = GetHistosForEfficiency(h0_mc_num, h0_mc_den) eff_data = ROOT.TEfficiency(hNumerator_Data, hDenominator_Data) eff_mc = ROOT.TEfficiency(hNumerator_MC, hDenominator_MC) eff_data.SetStatisticOption(ROOT.TEfficiency.kFCP) eff_mc.SetStatisticOption(ROOT.TEfficiency.kFCP) geff_data = convert2TGraph(eff_data) geff_mc = convert2TGraph(eff_mc) styles.dataStyle.apply(geff_data) styles.ttStyle.apply(geff_mc) Graph_Data = histograms.HistoGraph(geff_data, "t#bar{t} (Data) ", "p", "P") Graph_MC = histograms.HistoGraph(geff_mc, "t#bar{t} (MC)", "p", "P") p = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[]) saveName = "Efficiency_InclusiveTT_SR_LeadingJet" savePath = os.path.join(opts.saveDir, opts.optMode) plots.drawPlot(p, savePath, **_kwargs) SavePlot(p, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".pdf", ".C"]) # ============================= # Export JSON file # ============================= #jsonName = "Efficiency_InclusiveTT_SR_MET50_MuIso0p1_InvMET20_InvMuIso0p1.json" #runRange = "273150-284044" #analysis = opts.analysisName #label = "2016" #plotDir = os.path.join(opts.folder, jsonName) #pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi, geff_data) #pythonWriter.addMCParameters(label, geff_mc) #pythonWriter.writeJSON(jsonName) return
def main(opts, signalMass): # Apply TDR style style = tdrstyle.TDRStyle() style.setGridX(False) style.setGridY(False) # Do the topSelection histos folder = "" #=== Define styles: Fixme: Put in function #styles_S = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=928, markerSizes=None, markerStyle=ROOT.kFullDiamond), # styles.StyleLine(lineColor=928, lineStyle=ROOT.kSolid, lineWidth=3), # styles.StyleFill(fillColor=38)]) styles_S = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=928, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=928, lineStyle=ROOT.kSolid, lineWidth=3)]) styles_B = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=2, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=2, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=2, fillStyle=3005)]) styles_SEff = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=ROOT.kBlue, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kBlue, lineStyle=ROOT.kSolid, lineWidth=3),]) styles_BEff = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=ROOT.kRed, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kRed, lineStyle=ROOT.kSolid, lineWidth=3),]) styles_Signif = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=ROOT.kGreen+2, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kGreen+2, lineStyle=ROOT.kSolid, lineWidth=3),]) styles_pur = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=ROOT.kOrange+7, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kOrange+7, lineStyle=ROOT.kSolid, lineWidth=3),]) styles_pureff = styles.StyleCompound([styles.StyleMarker(markerSize=0.4, markerColor=ROOT.kOrange, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kOrange, lineStyle=ROOT.kSolid, lineWidth=3),]) #=== Histo Names ============================================ hN_SignalEff = "sigEffi" hN_BgdEff = "bgdEffi" hN_signif = "significance_BDTG" hN_purS = "purS_BDTG" hN_effpurS = "effpurS_BDTG" hN_BDTs = "MVA_BDTG_S" hN_BDTb = "MVA_BDTG_B" #=== Directories ============================================ inpfile_eff = ROOT.TFile(opts.effFile, "R") inpfile = ROOT.TFile(opts.rootfile, "R") #To be fixed! inpfile_dir = inpfile.Get("Method_BDT/BDTG") #=== Get histograms from directories========================= h_SignalEffi = inpfile_eff.Get(hN_SignalEff) h_BgdEffi = inpfile_eff.Get(hN_BgdEff) h_signif = inpfile_eff.Get(hN_signif) h_purS = inpfile_eff.Get(hN_purS) h_effpurS = inpfile_eff.Get(hN_effpurS) h_BDTs = inpfile_dir.Get(hN_BDTs) h_BDTb = inpfile_dir.Get(hN_BDTb) #=== Scale Significance h_signifScaled = h_signif.Clone("signif") maxSignif = h_signif.GetMaximum() h_signifScaled.Scale(1/maxSignif) histo_signalEff = histograms.Histo(h_SignalEffi, "sEff", legendStyle = "LP", drawStyle="LP") histo_bgdEff = histograms.Histo(h_BgdEffi, "bEff", legendStyle = "LP", drawStyle="LP") histo_signifScaled = histograms.Histo(h_signifScaled, "signif", legendStyle = "LP", drawStyle="LP") histo_purS = histograms.Histo(h_purS, "purity", legendStyle = "LP", drawStyle="LP") histo_effpurS = histograms.Histo(h_effpurS, "pureff", legendStyle = "LP", drawStyle="LP") histo_BDTs = histograms.Histo(h_BDTs, "bdtS", legendStyle = "LP", drawStyle="LP") histo_BDTb = histograms.Histo(h_BDTb, "bdtB", legendStyle = "LP", drawStyle="LP") #=== Efficiency - Purity - Significance================================= histoList_Eff = [histo_bgdEff, histo_signifScaled, histo_purS, histo_effpurS] p_Eff = plots.ComparisonManyPlot(histo_signalEff, histoList_Eff, saveFormats=[]) p_Eff.histoMgr.setHistoLegendLabelMany({"sEff": "Signal efficiency", "bEff": "Bkg efficiency", "signif": "S/#sqrt{S+B}", #"signif": "#frac{S}{#sqrt{S+B}}", "purity": "Signal purity", "pureff": "Signal eff*purity",}) # Define Right Axis (Significance) signifColor = ROOT.kGreen+2 #rightAxis = ROOT.TGaxis(0.5, 0.17, 0.97, 0.34, 0, 1.1*maxSignif, 510, "+L") ''' TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax, Double_t wmin, Double_t wmax, Int_t ndiv, Option_t *chopt, Double_t gridlength) ''' rightAxis = ROOT.TGaxis(1, 0, 1, 1.1, 0, 1.1*maxSignif, 510, "+L") rightAxis.SetWmax(1.1*maxSignif) rightAxis.SetLineColor ( signifColor ) rightAxis.SetLabelColor( signifColor ) rightAxis.SetTitleColor( signifColor ) # rightAxis.SetTitleSize( info->sSig->GetXaxis()->GetTitleSize() ) # rightAxis.SetLabelOffset(0.9) # This will only be be visible if we use a wide canvas (but we don't want that) if 0: style.setWide(True, 0.15) #to make way for significane label rightAxis.SetTitle( "Significance" ) # rightAxis.SetTitleOffset(1.1) #=== BDTG output ======================================================== histo_BDTs = histograms.Histo(h_BDTs, "bdtS", legendStyle = "LP", drawStyle="LP") histo_BDTb = histograms.Histo(h_BDTb, "bdtB", legendStyle = "F", drawStyle="LP") #p_BDT = plots.ComparisonPlot(histo_BDTb, histo_BDTs, saveFormats=[]) p_BDT = plots.ComparisonPlot(histo_BDTs, histo_BDTb, saveFormats=[]) p_BDT.histoMgr.setHistoLegendLabelMany({"bdtS": "Signal", "bdtB": "Background"}) p_Eff.histoMgr.forHisto("sEff", styles_SEff) p_Eff.histoMgr.setHistoDrawStyle("sEff", "L") p_Eff.histoMgr.setHistoLegendStyle("sEff", "L") p_Eff.histoMgr.forHisto("bEff", styles_BEff) p_Eff.histoMgr.setHistoDrawStyle("bEff", "L") p_Eff.histoMgr.setHistoLegendStyle("bEff", "L") p_Eff.histoMgr.forHisto("signif", styles_Signif) p_Eff.histoMgr.setHistoDrawStyle("signif", "L") p_Eff.histoMgr.setHistoLegendStyle("signif", "L") p_Eff.histoMgr.forHisto("purity", styles_pur) p_Eff.histoMgr.setHistoDrawStyle("purity", "L") p_Eff.histoMgr.setHistoLegendStyle("purity", "L") p_Eff.histoMgr.forHisto("pureff", styles_pureff) p_Eff.histoMgr.setHistoDrawStyle("pureff", "L") p_Eff.histoMgr.setHistoLegendStyle("pureff", "L") p_BDT.histoMgr.forHisto("bdtS", styles_S) p_BDT.histoMgr.setHistoDrawStyle("bdtS", "HIST") p_BDT.histoMgr.setHistoLegendStyle("bdtS", "L") p_BDT.histoMgr.forHisto("bdtB", styles_B) p_BDT.histoMgr.setHistoDrawStyle("bdtB", "HIST") p_BDT.histoMgr.setHistoLegendStyle("bdtB", "F") saveName_eff = "mvaeff" saveName = "mva" savePath = os.path.join(opts.saveDir, opts.optMode) _kwargs = { "xlabel" : "BDTG", #"Cut value applied on BDTG output", "ylabel" : "Efficiency (Purity)", "ratioYlabel" : "Ratio ", "ratio" : False, "ratioInvert" : False, "stackMCHistograms": False, "addMCUncertainty" : False, "addLuminosityText": False, "addCmsText" : True, "cmsExtraText" : "Preliminary", "opts" : {"xmin": -1.0, "xmax": 1.01, "ymin": 0.0, "ymax": 1.1}, "opts2" : {"ymin": 0.6, "ymax": 1.5}, "log" : False, "createLegend" : {"x1": 0.45, "y1": 0.45, "x2": 0.70, "y2": 0.70}, } ROOT.gStyle.SetNdivisions(6 + 100*5 + 10000*2, "X") #ROOT.gStyle.SetNdivisions(8, "X") plots.drawPlot(p_Eff, savePath, **_kwargs) rightAxis.Draw() saveDir = opts.rootfile.split("/")[-1] saveDir = saveDir.replace(".root","") SavePlot(p_Eff, saveName_eff, os.path.join(opts.saveDir, saveDir, opts.optMode), saveFormats = [".png", ".pdf", ".C"]) _kwargs["log"] = True _kwargs["opts"]["ymin"] = 3e-2 _kwargs["opts"]["ymax"] = 20 _kwargs["xlabel"] = "BDTG"# response" _kwargs["ylabel"] = "(1/N) dN/dx" _kwargs["createLegend"] = {"x1": 0.25, "y1": 0.75, "x2": 0.50, "y2": 0.9} style.setWide(False, 0.15) ROOT.gStyle.SetNdivisions(6 + 100*5 + 10000*2, "X") plots.drawPlot(p_BDT, savePath, **_kwargs) SavePlot(p_BDT, saveName, os.path.join(opts.saveDir, saveDir, opts.optMode), saveFormats = [".png", ".pdf", ".C"]) return
def plot(self): style = tdrstyle.TDRStyle() ROOT.gStyle.SetErrorX( 0.5 ) #required for x-axis error bars! (must be called AFTER tdrstyle.TDRStyle()) histolist = [] #styles.dataStyle.apply(self.h_data) hhd = histograms.Histo(self.h_data, "Data", legendStyle="PL", drawStyle="E1P") hhd.setIsDataMC(isData=True, isMC=False) histolist.append(hhd) if 0: aux.PrintTH1Info(hhd.getRootHisto()) # For-loop: All signal histo for i, hsignal in enumerate(self.h_signal, 1): mass = hsignal.GetName().replace("Hp", "") Verbose("Customing signal histogram for Mass = \"%s\"" % (mass), i == 1) hhs = histograms.Histo(hsignal, hsignal.GetTitle(), legendStyle="L", drawStyle="HIST") hhs.setIsDataMC(isData=False, isMC=True) signalStyle = styles.getSignalStyleHToTB_M(mass) signalStyle.apply(hhs.getRootHisto()) histolist.append(hhs) # For-loop: All bkg histos for i, hname in enumerate(self.histonames, 1): # Safety net for empty histos if hname in opts.empty: msg = "Skipping %s. Not a histogram!" % (hname) Print( ShellStyles.ErrorStyle() + msg + ShellStyles.NormalStyle(), True) continue else: #print hname pass myLabel = self.labels[hname] myHisto = self.histograms[hname] Verbose( "Creating histogram \"%s\" from ROOT file \"%s\" (Integral = %.1f)" % (hname, myHisto.GetName(), myHisto.Integral()), i == 1) hhp = histograms.Histo(myHisto, hname, legendStyle="F", drawStyle="HIST", legendLabel=myLabel) hhp.setIsDataMC(isData=False, isMC=True) histolist.append(hhp) if 0: aux.PrintTH1Info(hhp.getRootHisto()) # Sanity check for i, h in enumerate(histolist, 1): hName = h.getRootHisto().GetName() Verbose(hName, i == 1) # Do the plot p = plots.DataMCPlot2(histolist) p.setDefaultStyles() p.stackMCHistograms() p.setLuminosity(opts.lumi) if opts.fitUncert: p.addMCUncertainty( postfit=not opts.prefit) # boolean changes only the legend if opts.prefit: p.setLegendHeader("Pre-Fit") else: p.setLegendHeader("Post-Fit") # Customise histogram units = "GeV" #(GeV/c^{2}) myParams = {} myParams["xlabel"] = "m_{jjbb} (%s)" % (units) myParams["ylabel"] = "< Events / " + units + " >" myParams["ratio"] = True myParams["ratioYlabel"] = "Data/Bkg. " myParams["logx"] = self.gOpts.logX myParams["log"] = self.gOpts.logY myParams["ratioType"] = "errorScale" myParams["ratioErrorOptions"] = { "numeratorStatSyst": False, "denominatorStatSyst": True } myParams["opts"] = self.opts myParams["optsLogx"] = self.optsLogx myParams["opts2"] = self.opts2 myParams[ "divideByBinWidth"] = True #not opts.fitUncert # Error when used for "TGraphAsymmErrors" (uncert.) myParams["errorBarsX"] = True myParams["xlabelsize"] = 25 myParams["ylabelsize"] = 25 myParams["addMCUncertainty"] = True myParams["addLuminosityText"] = True myParams["moveLegend"] = self.moveLegend #myParams["saveFormats"] = [] # Draw the plot if not os.path.exists(opts.saveDir): os.makedirs(opts.saveDir) plots.drawPlot(p, os.path.join(opts.saveDir, self.gOpts.saveName), **myParams) # Save the plot (not needed - drawPlot saves the canvas already) SavePlot(p, self.gOpts.saveName, opts.saveDir, saveFormats=[".png", ".pdf", ".C"]) return
def GetScaleFactors(datasetsMgr, num_pathList, den_pathList, intLumi): # Get kwargs _kwargs = GetHistoKwargs(num_pathList[0], opts) _kwargs["stackMCHistograms"] = False _kwargs["opts2"] = {"ymin": 0.50, "ymax": 1.50} _kwargs["ratioType"] = "errorScale" # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CR1", "CR2"] labels = ["Genuine", "Fake", "Inclusive"] # Get Dictionaries rhDict_num = GetRootHistos(datasetsMgr, num_pathList, regions) rhDict_den = GetRootHistos(datasetsMgr, den_pathList, regions) # Marina # Normalization Factors (see: getNormalization.py) f1=0.619886; f2=0.904877; # Marina jsonName = "Efficiency_SystBDT_CR2_MET50_MuIso0p1_InvMET20_InvMuIso0p1_massCut300.json" # ------------------------------------------------------------------------------ # (A) Normalize QCD and TT (MC) in all regions # ------------------------------------------------------------------------------- for re in regions: rhDict_den["NormQCD-"+re+"-Inclusive"] = rhDict_den["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive") rhDict_den["NormQCD-"+re+"-Inclusive"].Scale(f1) rhDict_num["NormQCD-"+re+"-Inclusive"] = rhDict_num["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive") rhDict_num["NormQCD-"+re+"-Inclusive"].Scale(f1) for la in labels: rhDict_den["NormTT-"+re+"-"+la] = rhDict_den["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la) rhDict_den["NormTT-"+re+"-"+la].Scale(f2) rhDict_num["NormTT-"+re+"-"+la] = rhDict_num["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la) rhDict_num["NormTT-"+re+"-"+la].Scale(f2) # ----------------------------------------------------------------------------- # (B) Calculate BKG=QCD+EWK+ST in Data (CR2) # ----------------------------------------------------------------------------- # (B1) Denominator rhDict_den["BKG_Data-CR2-Inclusive"] = rhDict_den["Data-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (Data)") rhDict_den["BKG_Data-CR2-Inclusive"].Add( rhDict_den["NormTT-CR2-Inclusive"], -1) # (B2) Numerator rhDict_num["BKG_Data-CR2-Inclusive"] = rhDict_num["Data-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (Data)") rhDict_num["BKG_Data-CR2-Inclusive"].Add( rhDict_num["NormTT-CR2-Inclusive"], -1) # ----------------------------------------------------------------------------- # (C) Calculate BKG=QCD+EWK+ST in MC (CR2) # ----------------------------------------------------------------------------- # (C1) Denominator rhDict_den["BKG_MC-CR2-Inclusive"] = rhDict_den["NormQCD-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (MC)") rhDict_den["BKG_MC-CR2-Inclusive"].Add( rhDict_den["EWK-CR2-Inclusive"], +1) rhDict_den["BKG_MC-CR2-Inclusive"].Add( rhDict_den["SingleTop-CR2-Inclusive"], +1) # (C2) Numerator rhDict_num["BKG_MC-CR2-Inclusive"] = rhDict_num["NormQCD-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (MC)") rhDict_num["BKG_MC-CR2-Inclusive"].Add( rhDict_num["EWK-CR2-Inclusive"], +1) rhDict_num["BKG_MC-CR2-Inclusive"].Add( rhDict_num["SingleTop-CR2-Inclusive"], +1) # Rebinning bins = [0, 100, 200, 300, 400, 500, 600, 800] xBins = array.array('d', bins) nx = len(xBins)-1 Den_QCD = rhDict_den["NormQCD-CR2-Inclusive"].Clone("QCD") Den_EWK = rhDict_den["EWK-CR2-Inclusive"].Clone("EWK") Den_ST = rhDict_den["SingleTop-CR2-Inclusive"].Clone("ST") Num_QCD = rhDict_num["NormQCD-CR2-Inclusive"].Clone("QCD") Num_EWK = rhDict_num["EWK-CR2-Inclusive"].Clone("EWK") Num_ST = rhDict_num["SingleTop-CR2-Inclusive"].Clone("ST") Num_QCD = Num_QCD.Rebin(nx, "", xBins) Num_EWK = Num_EWK.Rebin(nx, "", xBins) Num_ST = Num_ST.Rebin(nx, "", xBins) Den_QCD = Den_QCD.Rebin(nx, "", xBins) Den_EWK = Den_EWK.Rebin(nx, "", xBins) Den_ST = Den_ST.Rebin(nx, "", xBins) # ------------------------------------------------------------------------------ # (D) Plot denominator & numerator (Data Vs MC) # ------------------------------------------------------------------------------ # (D1) Denominator hName = "QCD_EWK_ST_Denominator_CR2" h0 = rhDict_den["BKG_Data-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (Data)") h1 = rhDict_den["BKG_MC-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (MC)") hData = histograms.Histo( h0, "QCD+EWK (Data)", "Data", drawStyle="AP"); hData.setIsDataMC(isData=True, isMC=False) hMC = histograms.Histo( h1, "QCD+EWK (MC)", "MC", drawStyle="HIST"); hMC.setIsDataMC(isData=False, isMC=True) _kwargs["createLegend"] = {"x1": 0.70, "y1": 0.60, "x2": 0.95, "y2": 0.92} pDen = plots.ComparisonManyPlot(hMC, [hData], saveFormats=[]) pDen.setLuminosity(intLumi) pDen.setDefaultStyles() plots.drawPlot(pDen, hName, **_kwargs) SavePlot(pDen, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"]) # (D2) Numerator hName = "QCD_EWK_ST_Numerator_CR2" h2 = rhDict_num["BKG_Data-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (Data)") h3 = rhDict_num["BKG_MC-CR2-Inclusive"].Clone("QCD+EWK+ST_{CR2} (MC)") hDataNum = histograms.Histo( h2, "QCD+EWK (Data)", "Data", drawStyle="AP"); hDataNum.setIsDataMC(isData=True, isMC=False) hMCNum = histograms.Histo( h3, "QCD+EWK (MC)", "MC", drawStyle="HIST"); hMCNum.setIsDataMC(isData=False, isMC=True) pNum = plots.ComparisonManyPlot(hMCNum, [hDataNum], saveFormats=[]) pNum.setLuminosity(intLumi) pNum.setDefaultStyles() plots.drawPlot(pNum, hName, **_kwargs) SavePlot(pNum, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"]) # ----------------------------------------------------------------------------------------------------- # (E) Plot Mis-Identification rate in CR2 # ----------------------------------------------------------------------------------------------------- hName = "MisID_CR2" _kwargs = { "xlabel" : "p_{T} (GeV/c)", "ylabel" : "Misidentification rate", "ratioYlabel" : "Data/MC", "ratio" : True, "ratioInvert" : False, "ratioType" : "errorScale", "ratioCreateLegend": True, "ratioMoveLegend" : {"dx": -0.51, "dy": 0.03, "dh": -0.08}, "ratioErrorOptions": {"numeratorStatSyst": False, "denominatorStatSyst": False}, "errorBarsX" : True, "stackMCHistograms": False, "addMCUncertainty" : False, "addLuminosityText": False, "addCmsText" : True, "cmsExtraText" : "Preliminary", "opts" : {"ymin": 0.0, "ymaxfactor": 2.0, "xmax" : 600}, "opts2" : {"ymin": 0.30, "ymax": 1.70}, "log" : False, "createLegend" : {"x1": 0.55, "y1": 0.70, "x2": 0.95, "y2": 0.92}, } hNum_Data = rhDict_num["BKG_Data-CR2-Inclusive"].Clone("Numerator (Data)") hDen_Data = rhDict_den["BKG_Data-CR2-Inclusive"].Clone("Denominator (Data)") hNum_MC = rhDict_num["BKG_MC-CR2-Inclusive"].Clone("Numerator (MC)") hDen_MC = rhDict_den["BKG_MC-CR2-Inclusive"].Clone("Denominator (MC)") # Rebinning bins = [0, 100, 200, 300, 400, 500, 600, 800] xBins = array.array('d', bins) nx = len(xBins)-1 hNum_Data = hNum_Data.Rebin(nx, "", xBins) hDen_Data = hDen_Data.Rebin(nx, "", xBins) hNum_MC = hNum_MC.Rebin(nx, "", xBins) hDen_MC = hDen_MC.Rebin(nx, "", xBins) num_data, den_data = GetHistosForEfficiency(hNum_Data, hDen_Data) num_mc, den_mc = GetHistosForEfficiency(hNum_MC, hDen_MC) eff_Data = ROOT.TEfficiency(num_data, den_data) eff_Data.SetStatisticOption(ROOT.TEfficiency.kFCP) geff_Data = convert2TGraph(eff_Data) Graph_Data = histograms.HistoGraph(geff_Data, "QCD+EWK (Data)", "p", "P") eff_MC = ROOT.TEfficiency(num_mc, den_mc) eff_MC.SetStatisticOption(ROOT.TEfficiency.kFCP) geff_MC = convert2TGraph(eff_MC) styles.getABCDStyle("CR1").apply(geff_MC) Graph_MC = histograms.HistoGraph(geff_MC, "QCD+EWK (MC)", "p", "P") # Create efficiency plots p2 = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[]) plots.drawPlot(p2, hName, **_kwargs) SavePlot(p2, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"]) # Dump results in a JSON File runRange = "273150-284044" analysis = opts.analysisName label = "2016" plotDir = os.path.join(opts.folder, jsonName) pythonWriter.addParameters(plotDir, label, runRange, intLumi, geff_Data) pythonWriter.addMCParameters(label, geff_MC) fileName_json = jsonName pythonWriter.writeJSON(fileName_json) return
def PlotHistosAndCalculateTF(datasetsMgr, histoList, binLabels, opts): # Get the histogram customisations (keyword arguments) _kwargs = GetHistoKwargs(histoList[0]) # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CRone", "CRtwo"] rhDict = GetRootHistos(datasetsMgr, histoList, regions, binLabels) #========================================================================================= # Calculate the Transfer Factor (TF) and save to file #========================================================================================= manager = FakeBNormalization.FakeBNormalizationManager(binLabels, opts.mcrab, opts.optMode, verbose=False) if opts.inclusiveOnly: #manager.CalculateTransferFactor(binLabels[0], rhDict["CRone-FakeB"], rhDict["CRtwo-FakeB"]) binLabel = "Inclusive" manager.CalculateTransferFactor("Inclusive", rhDict["FakeB-CRone-Inclusive"], rhDict["FakeB-CRtwo-Inclusive"]) else: for bin in binLabels: manager.CalculateTransferFactor(bin, rhDict["FakeB-CRone-%s" % bin], rhDict["FakeB-CRtwo-%s" % bin]) # Get unique a style for each region for k in rhDict: dataset = k.split("-")[0] region = k.split("-")[1] styles.getABCDStyle(region).apply(rhDict[k]) if "FakeB" in k: styles.getFakeBStyle().apply(rhDict[k]) # sr.apply(rhDict[k]) # ========================================================================================= # Create the final plot object # ========================================================================================= rData_SR = rhDict["Data-SR-Inclusive"] rEWKGenuineB_SR = rhDict["EWK-SR-Inclusive-EWKGenuineB"] rBkgSum_SR = rhDict["FakeB-VR-Inclusive"].Clone("BkgSum-SR-Inclusive") rBkgSum_SR.Reset() if opts.inclusiveOnly: bin = "Inclusive" # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) ) binHisto_VR = rhDict["FakeB-VR-%s" % (bin)] VRtoSR_TF = manager.GetTransferFactor(bin) Print( "Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True) binHisto_VR.Scale(VRtoSR_TF) # Add the normalised histogram to the final Inclusive SR (predicted) histo rBkgSum_SR.Add(binHisto_VR, +1) else: # For-loop: All bins for i, bin in enumerate(binLabels, 1): if bin == "Inclusive": continue # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) ) binHisto_VR = rhDict["FakeB-VR-%s" % (bin)] VRtoSR_TF = manager.GetTransferFactor(bin) Print( "Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), i == 1) binHisto_VR.Scale(VRtoSR_TF) # Add the normalised histogram to the final Inclusive SR (predicted) histo rBkgSum_SR.Add(binHisto_VR, +1) #Print("Got Verification Region (VR) shape %s%s%s" % (ShellStyles.NoteStyle(), rFakeB_VR.GetName(), ShellStyles.NormalStyle()), True) # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) ) #VRtoSR_TF = manager.GetTransferFactor("Inclusive") #Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True) #rBkgSum_SR.Scale(VRtoSR_TF) # Plot histograms if opts.altPlot: # Add the SR EWK Genuine-b to the SR FakeB ( BkgSum = [FakeB] + [GenuineB-MC] = [VR * (CR1/CR2)] + [GenuineB-MC] ) rBkgSum_SR.Add(rEWKGenuineB_SR, +1) # Change style styles.getGenuineBStyle().apply(rBkgSum_SR) # Remove unsupported settings of kwargs _kwargs["stackMCHistograms"] = False _kwargs["addLuminosityText"] = False # Create the plot p = plots.ComparisonManyPlot(rData_SR, [rBkgSum_SR], saveFormats=[]) # Set draw / legend style p.histoMgr.setHistoDrawStyle("Data-SR-Inclusive", "P") p.histoMgr.setHistoLegendStyle("Data-SR-Inclusive", "LP") p.histoMgr.setHistoDrawStyle("BkgSum-SR-Inclusive", "HIST") p.histoMgr.setHistoLegendStyle("BkgSum-SR-Inclusive", "F") # Set legend labels p.histoMgr.setHistoLegendLabelMany({ "Data-SR": "Data", "BkgSum-SR": "Fake-b + Gen-b", }) else: # Create empty histogram stack list myStackList = [] # Signal p2 = plots.DataMCPlot(datasetsMgr, "ForTestQGLR/QGLR_SR/QGLR_SRInclusive", saveFormats=[]) hSignal_800 = p2.histoMgr.getHisto( 'ChargedHiggs_HplusTB_HplusToTB_M_800').getRootHisto() hhSignal_800 = histograms.Histo( hSignal_800, 'ChargedHiggs_HplusTB_HplusToTB_M_800', "H^{+} m_{H^+}=800 GeV") hhSignal_800.setIsDataMC(isData=False, isMC=True) myStackList.append(hhSignal_800) hSignal_250 = p2.histoMgr.getHisto( 'ChargedHiggs_HplusTB_HplusToTB_M_250').getRootHisto() hhSignal_250 = histograms.Histo( hSignal_250, 'ChargedHiggs_HplusTB_HplusToTB_M_250', "H^{+} m_{H^+}=250 GeV" ) #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_250']) hhSignal_250.setIsDataMC(isData=False, isMC=True) #myStackList.append(hhSignal_250) hSignal_500 = p2.histoMgr.getHisto( 'ChargedHiggs_HplusTB_HplusToTB_M_500').getRootHisto() hhSignal_500 = histograms.Histo( hSignal_500, 'ChargedHiggs_HplusTB_HplusToTB_M_500', "H^{+} m_{H^+}=500 GeV" ) #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500']) hhSignal_500.setIsDataMC(isData=False, isMC=True) #myStackList.append(hhSignal_500) hSignal_1000 = p2.histoMgr.getHisto( 'ChargedHiggs_HplusTB_HplusToTB_M_1000').getRootHisto() hhSignal_1000 = histograms.Histo( hSignal_1000, 'ChargedHiggs_HplusTB_HplusToTB_M_1000', "H^{+} m_{H^+}=1000 GeV" ) #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500']) hhSignal_1000.setIsDataMC(isData=False, isMC=True) #myStackList.append(hhSignal_1000) # Add the FakeB data-driven background to the histogram list hFakeB = histograms.Histo(rBkgSum_SR, "FakeB", "Fake-b") hFakeB.setIsDataMC(isData=False, isMC=True) myStackList.append(hFakeB) # Add the EWKGenuineB MC background to the histogram list hGenuineB = histograms.Histo(rEWKGenuineB_SR, "GenuineB", "EWK Genuine-b") hGenuineB.setIsDataMC(isData=False, isMC=True) myStackList.append(hGenuineB) # Add the collision datato the histogram list hData = histograms.Histo(rData_SR, "Data", "Data") hData.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, hData) p = plots.DataMCPlot2(myStackList, saveFormats=[]) p.setLuminosity(opts.intLumi) p.setDefaultStyles() # Draw the plot and save it hName = "test" plots.drawPlot(p, hName, **_kwargs) SavePlot(p, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf"]) #========================================================================================== # Calculate Cut-Flow Efficiency #========================================================================================== kwargs = { "rebinX": 1, "xlabel": "QGLR", "ylabel": "Significance / %.02f ", "opts": { "ymin": 0.0, "ymaxfactor": 1.3 }, "createLegend": { "x1": 0.55, "y1": 0.70, "x2": 0.92, "y2": 0.92 }, # "cutBox" : {"cutValue": 0.0, "fillColor" : 16, "box": False, "line": False, "greaterThan": True}, } efficiencyList = [] xValues = [] yValues_250 = [] yValues_500 = [] yValues_800 = [] yValues_1000 = [] yValues_Bkg = [] nBins = hSignal_250.GetNbinsX() + 1 hBkg = p.histoMgr.getHisto( "ChargedHiggs_HplusTB_HplusToTB_M_800").getRootHisto().Clone("Bkg") hBkg.Reset() # Bkg: FakeB + Genuine B hBkg.Add(hFakeB.getRootHisto(), +1) hBkg.Add(hGenuineB.getRootHisto(), +1) for i in range(0, nBins): # Cut value cut = hSignal_250.GetBinCenter(i) passed_250 = hSignal_250.Integral(i, hSignal_250.GetXaxis().GetNbins()) passed_500 = hSignal_500.Integral(i, hSignal_500.GetXaxis().GetNbins()) passed_800 = hSignal_800.Integral(i, hSignal_800.GetXaxis().GetNbins()) passed_1000 = hSignal_1000.Integral(i, hSignal_1000.GetXaxis().GetNbins()) passed_Bkg = hBkg.Integral(i, hBkg.GetXaxis().GetNbins()) total_250 = hSignal_250.Integral() total_500 = hSignal_500.Integral() total_800 = hSignal_800.Integral() total_1000 = hSignal_1000.Integral() total_Bkg = hBkg.Integral() eff_250 = float(passed_250) / total_250 eff_500 = float(passed_500) / total_500 eff_800 = float(passed_800) / total_800 eff_1000 = float(passed_1000) / total_1000 eff_Bkg = float(passed_Bkg) / total_Bkg xValues.append(cut) yValues_250.append(eff_250) yValues_500.append(eff_500) yValues_800.append(eff_800) yValues_1000.append(eff_1000) yValues_Bkg.append(eff_Bkg) # Create the Efficiency Plot tGraph_250 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_250)) tGraph_500 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_500)) tGraph_800 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_800)) tGraph_1000 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_1000)) tGraph_Bkg = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_Bkg)) styles.getSignalStyleHToTB_M("200").apply(tGraph_250) styles.getSignalStyleHToTB_M("500").apply(tGraph_500) styles.getSignalStyleHToTB_M("800").apply(tGraph_800) styles.getSignalStyleHToTB_M("1000").apply(tGraph_1000) styles.getQCDLineStyle().apply(tGraph_Bkg) drawStyle = "CPE" effGraph_250 = histograms.HistoGraph(tGraph_250, "H^{+} m_{H^{+}} = 250 GeV", "lp", drawStyle) effGraph_500 = histograms.HistoGraph(tGraph_500, "H^{+} m_{H^{+}} = 500 GeV", "lp", drawStyle) effGraph_800 = histograms.HistoGraph(tGraph_800, "H^{+} m_{H^{+}} = 800 GeV", "lp", drawStyle) effGraph_1000 = histograms.HistoGraph(tGraph_1000, "H^{+} m_{H^{+}} = 1000 GeV", "lp", drawStyle) effGraph_Bkg = histograms.HistoGraph(tGraph_Bkg, "Bkg", "lp", drawStyle) efficiencyList.append(effGraph_250) efficiencyList.append(effGraph_500) efficiencyList.append(effGraph_800) efficiencyList.append(effGraph_1000) efficiencyList.append(effGraph_Bkg) # Efficiency plot pE = plots.PlotBase(efficiencyList, saveFormats=["pdf"]) pE.createFrame("QGLR_Efficiency") pE.setEnergy("13") pE.getFrame().GetYaxis().SetLabelSize(18) pE.getFrame().GetXaxis().SetLabelSize(20) pE.getFrame().GetYaxis().SetTitle("Efficiency / 0.01") pE.getFrame().GetXaxis().SetTitle("QGLR Cut") # Add Standard Texts to plot histograms.addStandardTexts() # Customise Legend moveLegend = {"dx": -0.50, "dy": -0.5, "dh": -0.1} pE.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend)) pE.draw() # plots.drawPlot(pE, "QGLR_Efficiency", **kwargs) SavePlot(pE, "QGLR_Efficiency", os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf"]) #========================================================================================== # Calculate Significance #========================================================================================== SignalName = "ChargedHiggs_HplusTB_HplusToTB_M_800" hSignif_250 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone( SignalName) hSignif_500 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone( SignalName) hSignif_800 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone( SignalName) hSignif_1000 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone( SignalName) hSignif_250.Reset() hSignif_500.Reset() hSignif_800.Reset() hSignif_1000.Reset() hBkg = p.histoMgr.getHisto(SignalName).getRootHisto().Clone("Bkg") hBkg.Reset() # Bkg: FakeB + Genuine B hBkg.Add(hFakeB.getRootHisto(), +1) hBkg.Add(hGenuineB.getRootHisto(), +1) nBins = hSignif_250.GetNbinsX() + 1 # For-loop: All histo bins for i in range(1, nBins + 1): sigmaB = ROOT.Double(0) b = hBkg.IntegralAndError(i, nBins, sigmaB) s_250 = hSignal_250.Integral(i, nBins) s_500 = hSignal_500.Integral(i, nBins) s_800 = hSignal_800.Integral(i, nBins) s_1000 = hSignal_1000.Integral(i, nBins) # Calculate significance signif_250 = stat.significance(s_250, b, sigmaB, option="Simple") #Asimov") signif_500 = stat.significance(s_500, b, sigmaB, option="Simple") #Asimov") signif_800 = stat.significance(s_800, b, sigmaB, option="Simple") #Asimov") signif_1000 = stat.significance(s_1000, b, sigmaB, option="Simple") #"Asimov") # Set signif for this bin hSignif_250.SetBinContent(i, signif_250) hSignif_500.SetBinContent(i, signif_500) hSignif_800.SetBinContent(i, signif_800) hSignif_1000.SetBinContent(i, signif_1000) # Apply style s_250 = styles.getSignalStyleHToTB_M("200") s_250.apply(hSignif_250) s_500 = styles.getSignalStyleHToTB_M("500") s_500.apply(hSignif_500) s_800 = styles.getSignalStyleHToTB_M("800") s_800.apply(hSignif_800) s_1000 = styles.getSignalStyleHToTB_M("1000") s_1000.apply(hSignif_1000) hList = [] hList.append(hSignif_250) hList.append(hSignif_500) hList.append(hSignif_800) hList.append(hSignif_1000) hSignif_250.SetName("H^{+} m_{H^{+}} = 250 GeV") hSignif_500.SetName("H^{+} m_{H^{+}} = 500 GeV") hSignif_800.SetName("H^{+} m_{H^{+}} = 800 GeV") hSignif_1000.SetName("H^{+} m_{H^{+}} = 1000 GeV") pS = plots.PlotBase(hList, saveFormats=["png", "pdf"]) pS.setLuminosity(opts.intLumi) # Drawing style pS.histoMgr.setHistoDrawStyleAll("HIST") pS.histoMgr.setHistoLegendStyleAll("L") pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerSize(1.0)) pS.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) pS.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetMarkerStyle(ROOT.kFullCircle)) # Draw the plot name = "QGLR_Signif" + "GE" plots.drawPlot(pS, name, **kwargs) SavePlot(pS, name, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf"]) #========================================================================================= # Calculate the Transfer Factor (TF) and save to file #========================================================================================= Verbose("Write the normalisation factors to a python file", True) fileName = os.path.join( opts.mcrab, "FakeBTransferFactors%s.py" % (getModuleInfoString(opts))) manager.writeNormFactorFile(fileName, opts) return
def main(): if len(sys.argv) < 2 and not os.path.isdir(sys.argv[1]): usage() multicrabdir = sys.argv[1] signalAnalysisDir = "" QCDAnalysisDir = os.path.join(multicrabdir,"pseudoMulticrab_QCDMeasurement") candDirs = execute("ls %s"%multicrabdir) for d in candDirs: if "SignalAnalysis_" in d: signalAnalysisDir = os.path.join(multicrabdir,d) print "Using",signalAnalysisDir print " ",QCDAnalysisDir style = tdrstyle.TDRStyle() ROOT.gROOT.SetBatch(True) datasets = dataset.getDatasetsFromMulticrabDirs([signalAnalysisDir], analysisName=analysis) datasetsQCD = dataset.getDatasetsFromMulticrabDirs([QCDAnalysisDir], analysisName="signalAnalysis") datasets.extend(datasetsQCD) plots.mergeRenameReorderForDataMC(datasets) luminosity = datasets.getDataset("Data").getLuminosity() # Semi-ugly hack for approval homework, remember improve for the next round totalErrorFile = ROOT.TFile("outputLight.root") #totalErrorFile = ROOT.TFile("outputHeavy.root") ## Remove superfluous shape variation uncertainties totalErrorHistoUp = totalErrorFile.Get("total_background") rebin = [0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,600,700,800] import array #totalErrorHistoUp.Sumw2() totalErrorHistoUp = totalErrorHistoUp.Rebin(len(rebin)-1,"foo",array.array("d",rebin)) totalErrorHistoDown = totalErrorHistoUp.Clone("foo2") for i in xrange(1, totalErrorHistoUp.GetNbinsX()+1): #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoUp.GetBinError(i) totalErrorHistoUp.SetBinContent(i, totalErrorHistoUp.GetBinError(i)) totalErrorHistoDown.SetBinContent(i, -totalErrorHistoDown.GetBinError(i)) #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoDown.GetBinContent(i) # semi-ugly hack continues below in QCD myStackList = [] h_data = datasets.getDataset("Data").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram() myRHWU = dataset.RootHistoWithUncertainties(h_data) myRHWU.makeFlowBinsVisible() myHisto = histograms.Histo(myRHWU, "Data") myHisto.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, myHisto) h_FakeTau = datasets.getDataset("QCDMeasurementMT").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram() myRHWU = dataset.RootHistoWithUncertainties(h_FakeTau) # semi-ugly hack continues htemp=myRHWU.getRootHisto() myRHWU.addAbsoluteShapeUncertainty("toterr", totalErrorHistoUp, totalErrorHistoDown) #myRHWU.addShapeUncertaintyFromVariation("toterr", totalErrorHistoUp, totalErrorHistoDown) # semi-ugly hack ends myRHWU.makeFlowBinsVisible() myHisto = histograms.Histo(myRHWU, "QCDdata") myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(1, myHisto) expectedList = [] expectedList.append("TT") expectedList.append("WJets") expectedList.append("SingleTop") expectedList.append("DYJetsToLL") expectedList.append("Diboson") for i in range(0,len(expectedList)): drh = datasets.getDataset(expectedList[i]).getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT") h_bgr = drh.getHistogram() myRHWU = dataset.RootHistoWithUncertainties(h_bgr) # myRHWU.addShapeUncertaintyRelative("syst", th1Plus=self._expectedListSystUp[i], th1Minus=self._expectedListSystDown[i]) myRHWU.makeFlowBinsVisible() myHisto = histograms.Histo(myRHWU, expectedList[i]) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) #myStackList = divideByBinWidth(myStackList) # no, ugly hack continues here histograms.uncertaintyMode.set(histograms.Uncertainty.SystOnly) plots._legendLabels["MCSystError"] = "Bkg. stat.#oplus syst. unc." plots._legendLabels["BackgroundSystError"] = "Bkg. stat.#oplus syst. unc." # and stops again here myStackPlot = plots.DataMCPlot2(myStackList) myStackPlot.setLuminosity(luminosity) myStackPlot.setDefaultStyles() myParams = {} myParams["ylabel"] = "Events / bin" myParams["ratioYlabel"] = "Data/Bkg." myParams["xlabel"] = "m_{T} (GeV)" myParams["log"] = True myParams["ratio"] = True myParams["cmsTextPosition"] = "outframe" myParams["opts"] = {"ymin": 0.0001, "ymax": 3000.0} myParams["opts2"] = {"ymin": 0., "ymax":1.99} myParams["moveLegend"] = {"dx": -0.15, "dy": 0., "dh":0.05} # for data-driven myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03} myParams["stackMCHistograms"] = True myParams["divideByBinWidth"] = True myParams["addMCUncertainty"] = True myParams["ratioType"] = "errorScale" myParams["ratioCreateLegend"] = True myParams["ratioMoveLegend"] = dict(dy=-0.45, dh=-0.1, dx=-0.5) plots.drawPlot(myStackPlot, "TransVerseMassPOSTFIT", **myParams)
def GetScaleFactors(datasetsMgr, num_pathList, den_pathList, intLumi): # Get kwargs _kwargs = GetHistoKwargs(num_pathList[0], opts) _kwargs["stackMCHistograms"] = False _kwargs["opts2"] = {"ymin": 0.50, "ymax": 1.50} _kwargs["ratioType"] = "errorScale" # Get the root histos for all datasets and Control Regions (CRs) regions = ["SR", "VR", "CR1", "CR2"] # Get Dictionaries rhDict_num = GetRootHistos(datasetsMgr, num_pathList, regions) rhDict_den = GetRootHistos(datasetsMgr, den_pathList, regions) # ---------------------------------------------------------------------------- # Get the QCD in Data (Data - EWK - TT - Single Top) and MC in all regions # ---------------------------------------------------------------------------- for re in regions: rhDict_den["QCDinData-" + re + "-Inclusive"] = rhDict_den[ "Data-" + re + "-Inclusive"].Clone("QCDinData-" + re + "-Inclusive") rhDict_den["QCDinData-" + re + "-Inclusive"].Add( rhDict_den["EWK-" + re + "-Inclusive"], -1) rhDict_den["QCDinData-" + re + "-Inclusive"].Add( rhDict_den["TT-" + re + "-Inclusive"], -1) rhDict_den["QCDinData-" + re + "-Inclusive"].Add( rhDict_den["SingleTop-" + re + "-Inclusive"], -1) # ---------------------------------------------------------------------------- # (1) Plot QCD (Data Vs MC) before any normalization in all regions # ---------------------------------------------------------------------------- for re in regions: hName = "QCD_" + re + "_Before_Normalization" h0 = rhDict_den["QCDinData-" + re + "-Inclusive"].Clone("QCD (Data)") h1 = rhDict_den["QCD-" + re + "-Inclusive"].Clone("QCD (MC)") # Get Histos h_QCDinData = histograms.Histo(h0, "QCD (Data)", "Data") h_QCDinData.setIsDataMC(isData=True, isMC=False) h_QCDinMC = histograms.Histo(h1, "QCD (MC)", "MC") h_QCDinMC.setIsDataMC(isData=False, isMC=True) # Create plot pQCD = plots.ComparisonManyPlot(h_QCDinMC, [h_QCDinData], saveFormats=[]) pQCD.setLuminosity(intLumi) pQCD.setDefaultStyles() plots.drawPlot(pQCD, hName, **_kwargs) SavePlot(pQCD, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".C", ".pdf"]) # Reset histograms h0.Reset() h1.Reset() # ------------------------------------------------------------------------------ # (2) Find the normalization factor using CR2 # ------------------------------------------------------------------------------ n_QCDinData = rhDict_den["QCDinData-CR2-Inclusive"].Integral() n_QCDinMC = rhDict_den["QCD-CR2-Inclusive"].Integral() f1 = float(n_QCDinData) / float(n_QCDinMC) Print( "QCD Normalization factor: = %s%0.6f%s in CR2" % (ShellStyles.NoteStyle(), f1, ShellStyles.NormalStyle()), True) # ------------------------------------------------------------------------------ # (3) Normalize QCD (MC) in all regions # ------------------------------------------------------------------------------- for re in regions: rhDict_den["NormQCD-" + re + "-Inclusive"] = rhDict_den["QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive") rhDict_den["NormQCD-" + re + "-Inclusive"].Scale(f1) rhDict_num["NormQCD-" + re + "-Inclusive"] = rhDict_num["QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive") rhDict_num["NormQCD-" + re + "-Inclusive"].Scale(f1) # ------------------------------------------------------------------------------ # (4) Plot all regions after applying the normalization factor # ------------------------------------------------------------------------------ regions2 = ["CR2"] for re in regions2: # Reset histograms h0.Reset() h1.Reset() h0 = rhDict_den["QCDinData-" + re + "-Inclusive"].Clone("QCD (Data)") h1 = rhDict_den["NormQCD-" + re + "-Inclusive"].Clone("F1*QCD (MC)") h_QCDinData = histograms.Histo(h0, "QCD (Data)", "Data") h_QCDinData.setIsDataMC(isData=True, isMC=False) h_QCDinMC_After = histograms.Histo(h1, "QCD (MC)", "QCD") h_QCDinMC_After.setIsDataMC(isData=False, isMC=True) p0 = plots.ComparisonManyPlot(h_QCDinMC_After, [h_QCDinData], saveFormats=[]) p0.setLuminosity(intLumi) p0.setDefaultStyles() # Draw the plot and save it hName = "QCD_" + re + "_After_Normalization" plots.drawPlot(p0, hName, **_kwargs) SavePlot(p0, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".C", ".pdf"]) #======================================================================================== # Get TT in Data (Data - EWK - QCD*f1 - Single Top) in all regions #========================================================================================= for re in regions: rhDict_den["TTinData-" + re + "-Inclusive"] = rhDict_den["Data-" + re + "-Inclusive"].Clone("TTinData-" + re + "-Inclusive") rhDict_den["TTinData-" + re + "-Inclusive"].Add( rhDict_den["EWK-" + re + "-Inclusive"], -1) rhDict_den["TTinData-" + re + "-Inclusive"].Add( rhDict_den["NormQCD-" + re + "-Inclusive"], -1) rhDict_den["TTinData-" + re + "-Inclusive"].Add( rhDict_den["SingleTop-" + re + "-Inclusive"], -1) # ---------------------------------------------------------------------------- # (1) Plot inclusive TT (Data Vs MC) before any normalization in all regions # ---------------------------------------------------------------------------- for re in regions: hName = "TT_" + re + "_Before_Normalization" h0.Reset() h1.Reset() h0 = rhDict_den["TTinData-" + re + "-Inclusive"].Clone("t#bar{t} (Data)") h1 = rhDict_den["TT-SR-Inclusive"].Clone("t#bar{t} (MC)") h_TTinData = histograms.Histo(h0, "t#bar{t} (Data)", "TT") h_TTinData.setIsDataMC(isData=True, isMC=False) h_TTinMC = histograms.Histo(h1, "t#bar{t} (MC)", "TT") h_TTinMC.setIsDataMC(isData=False, isMC=True) pTT = plots.ComparisonManyPlot(h_TTinMC, [h_TTinData], saveFormats=[]) pTT.setLuminosity(intLumi) pTT.setDefaultStyles() plots.drawPlot(pTT, hName, **_kwargs) SavePlot(pTT, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".C", ".pdf"]) # ------------------------------------------------------------------------------ # (2) Find the normalization factor using SR # ------------------------------------------------------------------------------ n_TTinData = rhDict_den["TTinData-SR-Inclusive"].Integral() n_TTinMC = rhDict_den["TT-SR-Inclusive"].Integral() f2 = float(n_TTinData) / float(n_TTinMC) Print( "TT Normalization factor: = %s%0.6f%s in SR" % (ShellStyles.NoteStyle(), f2, ShellStyles.NormalStyle()), True) # ------------------------------------------------------------------------------ # (3) Normalize TT (MC) in all regions # ------------------------------------------------------------------------------- labels = ["Inclusive", "Fake", "Genuine"] for la in labels: for re in regions: rhDict_den["NormTT-" + re + "-" + la] = rhDict_den["TT-" + re + "-" + la].Clone("NormTT-" + re + "-" + la) rhDict_den["NormTT-" + re + "-" + la].Scale(f2) rhDict_num["NormTT-" + re + "-" + la] = rhDict_num["TT-" + re + "-" + la].Clone("NormTT-" + re + "-" + la) rhDict_num["NormTT-" + re + "-" + la].Scale(f2) # ------------------------------------------------------------------------------ # (4) Plot all regions after applying the normalization factor # ------------------------------------------------------------------------------ for re in regions: hName = "TT_" + re + "_After_Normalization" # Reset histograms h0.Reset() h1.Reset() h0 = rhDict_den["TTinData-" + re + "-Inclusive"].Clone("t#bar{t} (Data)") h1 = rhDict_den["NormTT-" + re + "-Inclusive"].Clone("t#bar{t} (MC)") h_TTinData = histograms.Histo(h0, "t#bar{t} (Data)", "TT") h_TTinData.setIsDataMC(isData=True, isMC=False) h_TTinMC_After = histograms.Histo(h1, "t#bar{t} (MC)", "TT") h_TTinMC_After.setIsDataMC(isData=False, isMC=True) p1 = plots.ComparisonManyPlot(h_TTinMC_After, [h_TTinData], saveFormats=[]) p1.setLuminosity(intLumi) p1.setDefaultStyles() plots.drawPlot(p1, hName, **_kwargs) SavePlot(p1, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".C", ".pdf"]) return
def __init__(self, opts, config, dirname, luminosity, observation, datasetGroups, verbose=False): self._validateDatacard(config) self._config = config self._verbose = verbose self._opts = opts self._dirname = dirname self._luminosity = luminosity self._observation = observation self._datasetGroups = datasetGroups # Define label options myStyle = tdrstyle.TDRStyle() myStyle.setOptStat(False) plots._legendLabels["MCStatError"] = "Bkg. stat." plots._legendLabels["MCStatSystError"] = "Bkg. stat.#oplussyst." plots._legendLabels["BackgroundStatError"] = "Bkg. stat. unc" plots._legendLabels[ "BackgroundStatSystError"] = "Bkg. stat.#oplussyst. unc." # Make control plots self.Verbose( ShellStyles.HighlightStyle() + "Generating control plots" + ShellStyles.NormalStyle(), True) # Definitions massPoints = [] massPoints.extend(self._config.MassPoints) if self._config.OptionDoWithoutSignal: massPoints.append(-1) # for plotting with no signal nMasses = len(massPoints) nPlots = len(self._config.ControlPlots) counter = 0 # For-loop: All mass points for m in massPoints: # Initialize flow plot selectionFlow = SelectionFlowPlotMaker(self._opts, self._config, m) # For-loop: All control plots for i in range(0, nPlots): counter += 1 # Skip if control plot does not exist if observation.getControlPlotByIndex(i) == None: continue # Get the control plot myCtrlPlot = self._config.ControlPlots[i] # The case m < 0 is for plotting hitograms without any signal if m > 0: # saveName = "%s/DataDrivenCtrlPlot_M%d_%02d_%s" % (self._dirname, m, i, myCtrlPlot.title) saveName = "%s/DataDrivenCtrlPlot_M%d_%s" % ( self._dirname, m, myCtrlPlot.title) msg = "Control Plot %d/%d (m=%s GeV)" % (counter, nMasses * nPlots, str(m)) else: # saveName = "%s/DataDrivenCtrlPlot_%02d_%s" % (self._dirname, i, myCtrlPlot.title) saveName = "%s/DataDrivenCtrlPlot_%s" % (self._dirname, myCtrlPlot.title) msg = "Control Plot %d/%d (no signal)" % (counter, nMasses * nPlots) # Inform the user of progress self.PrintFlushed( ShellStyles.AltStyle() + msg + ShellStyles.NormalStyle(), counter == 1) if counter == len(massPoints) * nPlots: print # Initialize histograms hData = None hSignal = None hFakeB = None hQCDMC = None myStackList = [] # For-loop: All dataset columns (to find histograms) for c in self._datasetGroups: self.Verbose( "Dataset is %s for plot %s" % (myCtrlPlot.title, c.getLabel()), False) # Skip plot? bDoPlot = (m < 0 or c.isActiveForMass( m, self._config)) and not c.typeIsEmptyColumn( ) and not c.getControlPlotByIndex(i) == None if not bDoPlot: continue # Clone histo h = c.getControlPlotByIndex(i)["shape"].Clone() if c.typeIsSignal(): self.Verbose( "Scaling histogram labelled \"%s\" with BR=%.2f" % (c.getLabel(), self._config.OptionBr), False) h.Scale(self._config.OptionBr) if hSignal == None: hSignal = h.Clone() else: hSignal.Add(h) elif c.typeIsFakeB(): if hFakeB == None: hFakeB = h.Clone() else: hFakeB.Add(h) elif c.typeIsQCDMC(): if hQCD == None: hQCDMC = h.Clone() else: hQCDMC.Add(h) elif c.typeIsEWKMC() or c.typeIsGenuineB(): myHisto = histograms.Histo(h, c._datasetMgrColumn) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.append(myHisto) # FIXME: what's this exactly? if len( myStackList ) < 1 or self._config.OptionFakeBMeasurementSource != "DataDriven": continue # Stack all the histograms if hFakeB != None: myHisto = histograms.Histo(hFakeB, "FakeB", legendLabel=_legendLabelFakeB) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(0, myHisto) elif hQCDMC != None: myHisto = histograms.Histo(hQCDMC, "QCDMC", legendLabel=_legendLabelQCDMC) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(0, myHisto) hData = observation.getControlPlotByIndex(i)["shape"].Clone() hDataUnblinded = hData.Clone() # Apply blinding & Get blinding string myBlindingString = self._applyBlinding(myCtrlPlot, myStackList, hData, hSignal) # Data myDataHisto = histograms.Histo(hData, "Data") myDataHisto.setIsDataMC(isData=True, isMC=False) myStackList.insert(0, myDataHisto) # Add signal if m > 0: #mySignalLabel = "HplusTB_M%d" % m mySignalLabel = "ChargedHiggs_HplusTB_HplusToTB_M_%d" % (m) myHisto = histograms.Histo(hSignal, mySignalLabel) myHisto.setIsDataMC(isData=False, isMC=True) myStackList.insert(1, myHisto) # Add data to selection flow plot selectionFlow.addColumn(myCtrlPlot.flowPlotCaption, hDataUnblinded, myStackList[1:]) # Make plot myStackPlot = None myParams = myCtrlPlot.details.copy() myStackPlot = plots.DataMCPlot2(myStackList) myStackPlot.setLuminosity(self._luminosity) myStackPlot.setEnergy("%d" % self._config.OptionSqrtS) myStackPlot.setDefaultStyles() # Tweak paramaters if not "unit" in myParams.keys(): myParams["unit"] = "" if myParams["unit"] != "": myParams["xlabel"] = "%s (%s)" % (myParams["xlabel"], myParams["unit"]) # Apply various settings to my parameters self._setBlingingString(myBlindingString, myParams) self._setYlabelWidthSuffix(hData, myParams) self._setLegendPosition(myParams) self._setRatioLegendPosition(myParams) # Remove non-dientified keywords del myParams["unit"] # Ratio axis if not "opts2" in myParams.keys(): myParams["opts2"] = {"ymin": 0.3, "ymax": 1.7} # Make sure BR is indicated if anyting else but BR=1.0 if m > 0 and self._config.OptionBr != 1.0: myStackPlot.histoMgr.setHistoLegendLabelMany({ #mySignalLabel: "H^{+} m_{H^{+}}=%d GeV (x %s)" % (m, self._config.OptionBr) mySignalLabel: "m_{H^{+}}=%d GeV (x %s)" % (m, self._config.OptionBr) }) # Do plotting drawPlot(myStackPlot, saveName, **myParams) # Do selection flow plot selectionFlow.makePlot(self._dirname, m, len(self._config.ControlPlots), self._luminosity) return
def Plot_Comparisons(datasetsMgr, datasetsMgrRef, hT, hR, intLumi): #kwargs = {} _kwargs = {} #kwargs = GetHistoKwargs(hG, opts) print if opts.normaliseToOne: pT = plots.MCPlot(datasetsMgr, hT, normalizeToOne=True, saveFormats=[], **_kwargs) pR = plots.MCPlot(datasetsMgrRef, hR, normalizeToOne=True, saveFormats=[], **_kwargs) else: pT = plots.MCPlot(datasetsMgr, hT, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) pR = plots.MCPlot(datasetsMgrRef, hR, normalizeToLumi=intLumi, saveFormats=[], **_kwargs) # Draw the histograms _cutBox = None _rebinX = 1 _format = "%0.2f" _xlabel = None logY = False _opts = {"ymin": 1e-3, "ymaxfactor": 1.0} print "HT", hT xMax = 200 _kwargs = { "xlabel": _xlabel, "ylabel": "Events / %s" % (_format), "ratioYlabel": "Ratio ", "ratio": False, "ratioInvert": False, "stackMCHistograms": False, "addMCUncertainty": False, "addLuminosityText": False, "addCmsText": True, "cmsExtraText": "Preliminary", #"opts" : {"ymin": 0.1, "ymaxfactor": 1.2}, "opts": { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": xMax }, "opts2": { "ymin": 0.6, "ymax": 1.5 }, "log": True, #"createLegend" : {"x1": 0.5, "y1": 0.75, "x2": 0.9, "y2": 0.9}, "createLegend": { "x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.82 }, "rebinX": 1, } if "counters" in hT.lower(): print "HERE" ROOT.gStyle.SetLabelSize(16.0, "X") _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 6 } _kwargs["createLegend"] = { "x1": 0.65, "y1": 0.6, "x2": 0.90, "y2": 0.77 }, if "pt" in hT.lower(): _units = "GeV/c" _format = "%0.0f " + _units _xlabel = "p_{T} (%s)" % _units _kwargs["xmax"] = 800 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 800 } if "mass" in hT.lower(): _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "M (%s)" % _units _kwargs["xmax"] = 800 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 800 } if "tetrajet" in hT.lower(): _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 2000 } _kwargs["rebinX"] = 8 if "trijetmass" in hT.lower(): print "mass" _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{top} (%s)" % _units #_cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True} _kwargs["xmax"] = 400 #1005 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 400 } if "eta" in hT.lower(): _units = "" _format = "%0.1f " + _units _xlabel = "#eta (%s)" % _units _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": -5.0, "xmax": +5.0 } if "mult" in hT.lower(): _format = "%0.0f" if "cleaned" in hT.lower(): _kwargs["xmax"] = 10 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 10 } else: _kwargs["xmax"] = 60 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 60 } if "bdisc" in hT.lower(): _format = "%0.2f" _kwargs["xmax"] = 1 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 1 } if "topbdt" in hT.lower(): _format = "%0.2f" _kwargs["xmax"] = 1 _kwargs["xmin"] = 0.3 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0.3, "xmax": 1.0 } if "dijetmass" in hT.lower(): print "dijet-mass" _units = "GeV/c^{2}" _format = "%0.0f " + _units _xlabel = "m_{W} (%s)" % _units _opts["xmax"] = 600 _kwargs["opts"] = { "ymin": 0.1, "ymaxfactor": 1.2, "xmin": 0, "xmax": 300 } else: pass if logY: yMaxFactor = 2.0 else: yMaxFactor = 1.5 _opts["ymaxfactor"] = yMaxFactor if opts.normaliseToOne: _opts["ymin"] = 1e-3 else: _opts["ymin"] = 1e0 myList = [] # Customise styling pT.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) pR.histoMgr.forEachHisto( lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid)) datasetTarg = datasetsMgr.getDataset( "QCD") #ChargedHiggs_HplusTB_HplusToTB_M_500 datasetRef = datasetsMgrRef.getDataset( "QCD") #ChargedHiggs_HplusTB_HplusToTB_M_500 h = datasetTarg.getDatasetRootHisto(hT) #h.normalizeToOne() h.normalizeToLuminosity(intLumi) HT = h.getHistogram() h = datasetRef.getDatasetRootHisto(hR) #h.normalizeToOne() h.normalizeToLuminosity(intLumi) HR = h.getHistogram() altSignalBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kAzure + 9, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kAzure + 9, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kAzure + 9, fillStyle=3001) ]) altBackgroundBDTGStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kRed - 4, markerSizes=None, markerStyle=ROOT.kFullDiamond), styles.StyleLine(lineColor=ROOT.kRed - 4, lineStyle=ROOT.kSolid, lineWidth=3), #styles.StyleFill(fillColor=ROOT.kRed-4) ]) signalStyle = styles.StyleCompound([ styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kTeal + 2, markerSizes=None, markerStyle=ROOT.kFullTriangleUp), styles.StyleLine(lineColor=ROOT.kTeal + 2, lineStyle=ROOT.kSolid, lineWidth=3), styles.StyleFill(fillColor=ROOT.kTeal + 2, fillStyle=3001) ]) p = plots.ComparisonPlot( histograms.Histo(HT, "Target", "pl", "PL"), histograms.Histo(HR, "Reference", "pl", "PL"), ) p.histoMgr.setHistoLegendLabelMany({ "Target": "bug fixed", "Reference": "bug" }) if ("TT" in datasetTarg.getName()): p.histoMgr.forHisto("Target", altBackgroundBDTGStyle) p.histoMgr.forHisto("Reference", altSignalBDTGStyle) elif ("QCD" in datasetTarg.getName()): p.histoMgr.forHisto( "Target", altBackgroundBDTGStyle) #styles.getABCDStyle("VR")) p.histoMgr.forHisto("Reference", styles.qcdFillStyle) elif ("Charged" in datasetTarg.getName()): p.histoMgr.forHisto("Target", altBackgroundBDTGStyle) p.histoMgr.forHisto("Reference", signalStyle) p.histoMgr.setHistoDrawStyle("Target", "HIST") p.histoMgr.setHistoLegendStyle("Target", "LP") #F p.histoMgr.setHistoDrawStyle("Reference", "HIST") p.histoMgr.setHistoLegendStyle("Reference", "F") #LP ''' histoG = histograms.Histo(HT, "TT", "Signal") histoG.setIsDataMC(isData=False, isMC=True) histoF = histograms.Histo(HR, "QCD", "Signal") histoF.setIsDataMC(isData=False, isMC=True) ''' #styleT = styles.ttStyle #styleR = styles.signalStyleHToTB1000 #styleT.apply(HT) #styleR.apply(HR) ''' myList.append(histoT) myList.append(histoR) ''' # Save plot in all formats #saveName = hT.split("/")[-1] #plots.drawPlot(p, saveName, **_kwargs) #savePath = os.path.join(opts.saveDir, "HplusMasses", hT.split("/")[0], opts.optMode) #plots.drawPlot(p, savePath, **_kwargs) #SavePlot(p, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".pdf", ".C"]) dName = datasetTarg.getName() dName = dName.replace("ChargedHiggs_HplusTB_HplusToTB_", "") saveName = hT.split("/")[-1] + "_" + dName #print saveName, hT, opts.saveDir savePath = os.path.join(opts.saveDir, hT.split("/")[0], opts.optMode) print "TYPE", type(hT), type(savePath), type(p) plots.drawPlot(p, savePath, **_kwargs) #leg = ROOT.TLegend(0.2, 0.8, 0.81, 0.87) leg = ROOT.TLegend(0.2, 0.8, 0.51, 0.87) leg.SetFillStyle(0) leg.SetFillColor(0) leg.SetBorderSize(0) #{"dx": -0.55, "dy": -0.55, "dh": -0.08} datasetName = datasetTarg.getName() datasetName = datasetName.replace("TT", "t#bar{t}") if "ChargedHiggs" in datasetName: datasetName = datasetName.replace("ChargedHiggs_HplusTB_HplusToTB_M_", "m_{H+} = ") datasetName = datasetName + "GeV" #leg.SetHeader("t#bar{t}") leg.SetHeader(datasetName) leg.Draw() #print savePath #savePath = os.path.join(opts.saveDir, opts.optMode) SavePlot(p, saveName, savePath) return
def doPlot(self, opts, myAllShapeNuisances, f, mass, luminosity, signalTable, rebinList=None): def rebin(h, rebinList): if rebinList == None or h == None: return h myArray = array.array("d",rebinList) hnew = h.Rebin(len(rebinList)-1,h.GetTitle(),myArray) h.Delete() return hnew print "Doing plots for:",self._name hNominal = f.Get(self._cardReader.getHistoNameForColumn(self._name)) hNominal = rebin(hNominal, rebinList) hNominal.SetFillStyle(0) hNominalFine = f.Get(self._cardReader.getHistoNameForColumn(self._name)+"_fineBinning") hNominalFine = rebin(hNominalFine, rebinList) hNominalHisto = histograms.Histo(hNominal, self._name, drawStyle="HIST") # Determine label mySignalLabels = ["HH","HW","Hp"] mySignalStatus = False for l in mySignalLabels: if l in self._name: mySignalStatus = True myMCLabels = ["HH","HW","Hp","MC"] myMCStatus = False for mclab in myMCLabels: if mclab in self._name: myMCStatus = True histograms.cmsTextMode = histograms.CMSMode.PRELIMINARY if myMCStatus: histograms.cmsTextMode = histograms.CMSMode.SIMULATION_PRELIMINARY x = 0.6 size = 20 myRatioContainer = RatioPlotContainer(self._label) for uncName in self._uncertaintyShapes: myShortName = uncName print "... uncertainty:",myShortName myLongName = self._cardReader.getHistoNameForNuisance(self._name, uncName) hup = f.Get("%sUp"%(myLongName)) hup = rebin(hup, rebinList) hup.SetFillStyle(0) up = dataset.RootHistoWithUncertainties(hup) upFine = f.Get("%sUp_fineBinning"%(myLongName)) upFine = rebin(upFine, rebinList) nom = dataset.RootHistoWithUncertainties(hNominal.Clone()) nom.makeFlowBinsVisible() hdown = f.Get("%sDown"%(myLongName)) hdown = rebin(hdown, rebinList) hdown.SetFillStyle(0) down = dataset.RootHistoWithUncertainties(hdown) downFine = f.Get("%sDown_fineBinning"%(myLongName)) downFine = rebin(downFine, rebinList) up.getRootHisto().SetLineColor(ROOT.kRed) nom.getRootHisto().SetLineColor(ROOT.kBlack) down.getRootHisto().SetLineColor(ROOT.kBlue) upHisto = histograms.Histo(up, "Up %.1f"%_integral(up.getRootHisto()), drawStyle="HIST", legendStyle="l") downHisto = histograms.Histo(down, "Down %.1f"%_integral(down.getRootHisto()), drawStyle="HIST", legendStyle="l") nomHisto = histograms.Histo(nom, "Nominal %.1f"%_integral(nom.getRootHisto()), drawStyle="HIST", legendStyle="l") # Add fit uncert. as stat uncert. for i in range(0,9): tailFitUp = f.Get("%s_TailFit_par%dUp"%(myLongName,i)) tailFitDown = f.Get("%s_TailFit_par%dDown"%(myLongName,i)) if tailFitUp != None and tailFitDown != None: nom.addShapeUncertaintyFromVariation("%s_TailFit_par%d"%(self._name,i),tailFitUp,tailFitDown) tailFitUp.Delete() tailFitDown.Delete() tailfitNames = filter(lambda n: "_TailFit_" in n, nom.getShapeUncertaintyNames()) nom.setShapeUncertaintiesAsStatistical(tailfitNames) # Do plot plot = plots.ComparisonManyPlot(nomHisto, [upHisto, downHisto]) plot.setLuminosity(luminosity) plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3)) myPlotName = "%s/shapeSyst_%s_syst%s" % (_dirname, self._label, uncName.replace("Up","")) myParams = {} myParams["ylabel"] = "Events" myParams["log"] = False #myParams["opts2"] = {"ymin": 0.0, "ymax":2.0} myParams["opts2"] = {"ymin": 0.7, "ymax":1.3} myParams["opts"] = {"ymin": 0.0} myParams["ratio"] = True myParams["ratioType"] = "errorScale" myParams["ratioYlabel"] = "Var./Nom." myParams["addLuminosityText"] = True myParams["customizeBeforeDraw"] = customizeMarker #myParams["ratioErrorOptions"] = {"numeratorStatSyst": False} myParams["addMCUncertainty"] = True plots.drawPlot(plot, myPlotName, **myParams) myRatioContainer.addRatioPlot(plot, myShortName) # Analyse up and down variation if mySignalStatus and upFine != None and downFine != None: a = abs(upFine.Integral()/hNominalFine.Integral() - 1.0) b = abs(1.0 - downFine.Integral()/hNominalFine.Integral()) r = a if b > a: r = b if uncName in signalTable.keys(): if r < signalTable[uncName]["min"]: signalTable[uncName]["min"] = r if r > signalTable[uncName]["max"]: signalTable[uncName]["max"] = r else: signalTable[uncName] = {} signalTable[uncName]["min"] = r signalTable[uncName]["max"] = r # Create plots with only the ratio plot if opts.individual: myRatioContainer.drawIndividually() else: myRatioContainer.drawAllInOne(myAllShapeNuisances, luminosity)
def doSinglePlot(hbase, hinv, myDir, histoName, luminosity): def rebin(h, name): myBinning = [] if name.startswith("MT"): myBinning = systematics.getBinningForPlot("shapeTransverseMass") elif name.startswith("INVMASS"): myBinning = systematics.getBinningForPlot("shapeInvariantMass") else: raise Exception("Unknown binning information") myArray = array.array("d", myBinning) # Rebin and move under/overflow bins to visible bins h = h.Rebin(len(myBinning) - 1, "", myArray) h.SetBinContent(1, h.GetBinContent(0) + h.GetBinContent(1)) h.SetBinError(1, math.sqrt(h.GetBinContent(0)**2 + h.GetBinContent(1)**2)) h.SetBinContent( h.GetNbinsX() + 1, h.GetBinContent(h.GetNbinsX() + 1) + h.GetBinContent(h.GetNbinsX() + 2)) h.SetBinError( h.GetNbinsX() + 1, math.sqrt( h.GetBinError(h.GetNbinsX() + 1)**2 + h.GetBinError(h.GetNbinsX() + 2)**2)) h.SetBinContent(0, 0.0) h.SetBinError(0, 0.0) h.SetBinContent(h.GetNbinsX() + 2, 0.0) h.SetBinError(h.GetNbinsX() + 2, 0.0) return h hbase.SetLineColor(ROOT.kBlack) hinv.SetLineColor(ROOT.kRed) # Rebin hbase = rebin(hbase, histoName) hinv = rebin(hinv, histoName) # Normalize print "baseline: %.1f events, inverted %.1f events" % (hbase.Integral(), hinv.Integral()) #hbase.Scale(1.0 / hbase.Integral()) #hinv.Scale(1.0 / hinv.Integral()) # Plot baseHisto = histograms.Histo(hbase, "Isolated", drawStyle="HIST", legendStyle="l") invHisto = histograms.Histo(hinv, "Anti-isolated", drawStyle="HIST", legendStyle="l") plot = plots.ComparisonPlot(baseHisto, invHisto) plot.setLuminosity(luminosity) plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3)) myPlotName = "%s/QCDInv_ClosureTest_%s" % (myDir, histoName) myParams = {} myParams["ylabel"] = "Events/#Deltam_{T}, normalized to 1" myParams["log"] = False myParams["opts2"] = {"ymin": 0.0, "ymax": 2.0} myParams["opts"] = {"ymin": 0.0} myParams["ratio"] = True myParams["ratioType"] = "errorScale" myParams["ratioYlabel"] = "Var./Nom." myParams["cmsText"] = myCMSText myParams["addLuminosityText"] = True myParams["divideByBinWidth"] = True plots.drawPlot(plot, myPlotName, **myParams)
def main(argv): dirs = [] if len(sys.argv) < 2: usage() dirs.append(sys.argv[1]) QCDInvertedNormalization = QCDInvertedNormalizationFactors.QCDInvertedNormalization QCDInvertedNormalizationFilteredEWKFakeTaus = QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization analysis = "signalAnalysisInvertedTau" optModes = [] #optModes.append("OptQCDTailKillerZeroPlus") optModes.append("OptQCDTailKillerLoosePlus") optModes.append("OptQCDTailKillerMediumPlus") optModes.append("OptQCDTailKillerTightPlus") #optModes.append("OptQCDTailKillerVeryTightPlus") #optModes.append("OnlyGenuineMCTausFalse") #optModes.append("OnlyGenuineMCTausTrue") #Optimal: 0.8, 0.82, 0.9 #w1_list = [0.8, 0.82, 0.84, 0.87] #w1_list = [0.8, 0.82, 0.9, 1] w1_list = [0.9] defaultBinning = systematics.getBinningForPlot("shapeTransverseMass") diff_opt = [] for optMode in optModes: diff_list = [] for w1 in w1_list: var_values = [] nom_values = [] w2 = 1 - w1 color = 1 #signal dirs_signal = ["../../SignalAnalysis_140605_143702/"] datasets_signal = dataset.getDatasetsFromMulticrabDirs( dirs_signal, dataEra=dataEra, searchMode=searchMode, analysisName=analysis.replace("InvertedTau", ""), optimizationMode=optMode) datasets_signal.updateNAllEventsToPUWeighted() datasets_signal.loadLuminosities() datasets_signal.remove( filter(lambda name: "TTToHplus" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "HplusTB" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "Hplus_taunu_t-channel" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "TTJets_SemiLept" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "TTJets_FullLept" in name, datasets_signal.getAllDatasetNames())) datasets_signal.remove( filter(lambda name: "TTJets_Hadronic" in name, datasets_signal.getAllDatasetNames())) plots.mergeRenameReorderForDataMC(datasets_signal) datasets_signal.merge( "EWK", ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"]) mtplot_signalfaketaus = plots.DataMCPlot( datasets_signal, "shapeEWKFakeTausTransverseMass") mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto( "EWK").getRootHisto().Clone("shapeEWKFakeTausTransverseMass") mt_signalfaketaus.SetName("BaselineFakeTaus") myBinning = [0, 20, 40, 60, 80, 100, 120, 140, 160, 200, 400] myArray = array.array("d", myBinning) fitBinning = [] for i in range(0, 45): fitBinning.append(i * 10) fitArray = array.array("d", fitBinning) mt_baseline = mt_signalfaketaus #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin() #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax() #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4) #theFit.SetParLimits(0,0.5,10000) #theFit.SetParLimits(1,90,10000) #theFit.SetParLimits(2,30,10000) #theFit.SetParLimits(3,0.001,10000) #mt_signalfaketaus.Fit(theFit,"R") #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax()) #theFit.SetLineStyle(2) #theFit.SetLineColor(4) #theFit.SetLineWidth(3) #theFit.Draw() #mt_corr = theFit.GetHistogram() #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray) #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum()) for HISTONAME in histoNameList: var = False if HISTONAME == "shapeEWKGenuineTausTransverseMass": var = True datasets = dataset.getDatasetsFromMulticrabDirs( dirs, dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode) datasets.updateNAllEventsToPUWeighted() datasets.loadLuminosities() plots.mergeRenameReorderForDataMC(datasets) datasets.merge( "EWK", ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"]) histonames = datasets.getDataset("Data").getDirectoryContent( HISTONAME) bins = [] for histoname in histonames: binname = histoname.replace(HISTONAME, "") if not binname == "Inclusive": bins.append(binname) for i, bin in enumerate(bins): mtplot = plots.DataMCPlot( datasets, HISTONAME + "/" + HISTONAME + bin) if i == 0: mt = mtplot.histoMgr.getHisto( "Data").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) mt_ewk = mtplot.histoMgr.getHisto( "EWK").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) mtn = mtplot.histoMgr.getHisto( "Data").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) mtn_ewk = mtplot.histoMgr.getHisto( "EWK").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) if var: legendName = "QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)" else: legendName = "QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)" legendName = legendName.replace("Plus", "") mt.SetName(legendName) mt.SetLineColor(color) mt.Add(mt_ewk, -1) mtn.Add(mtn_ewk, -1) mtn.Scale(QCDInvertedNormalization[str(i)]) if var: scale = w1 * QCDInvertedNormalizationFilteredEWKFakeTaus[ str( i )] + w2 * QCDInvertedNormalizationFilteredEWKFakeTaus[ str(i) + "EWK_FakeTaus"] mt.Scale(scale) else: mt.Scale(QCDInvertedNormalization[str(i)]) color += 1 if color == 5: color += 1 else: h = mtplot.histoMgr.getHisto( "Data").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) mt_ewk = mtplot.histoMgr.getHisto( "EWK").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) hn = mtplot.histoMgr.getHisto( "Data").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) mtn_ewk = mtplot.histoMgr.getHisto( "EWK").getRootHisto().Clone(HISTONAME + "/" + HISTONAME + bin) h.Add(mt_ewk, -1) hn.Add(mtn_ewk, -1) hn.Scale(QCDInvertedNormalization[str(i)]) if var: scale = w1 * QCDInvertedNormalizationFilteredEWKFakeTaus[ str( i )] + w2 * QCDInvertedNormalizationFilteredEWKFakeTaus[ str(i) + "EWK_FakeTaus"] h.Scale(scale) else: h.Scale(QCDInvertedNormalization[str(i)]) mt.Add(h) mtn.Add(hn) #mt = mt.Rebin(len(myBinning)-1,"",myArray) #mt_corr = mt_corr.Rebin(len(myBinning)-1,"",myArray) if not var: mt.Add(mt_baseline) #mt.Add(mt_corr) #myBinning = [] #for i in range(0,11): # myBinning.append(20*i) #myBinning.append(400) #myArray = array.array("d",defaultBinning) mt = mt.Rebin(len(myBinning) - 1, "", myArray) for i in range(0, mt.GetSize()): if var: var_values.append(mt.GetBinContent(i)) else: nom_values.append(mt.GetBinContent(i)) if var: #mt.SetLineStyle(2) var_hist = mt else: #mt.SetLineStyle(2) nom_hist = mt style = tdrstyle.TDRStyle() #gStyle.SetOptStat(1101) #mt_data.SetStats(1) #gPad.Update() bins = [0, 390, 400] arr = array.array("d", bins) mtn = mtn.Rebin(len(bins) - 1, "", arr) plot_data = plots.PlotBase() plot_data.histoMgr.appendHisto(histograms.Histo(mtn, "Data")) plot_data.createFrame("Data_" + HISTONAME + "_" + optMode + "_" + str(w1)) plot_data.draw() plot_data.save() plot = plots.ComparisonPlot(nom_hist, var_hist) plot.createFrame(optMode.replace( "Opt", "Mt_" + "w1=" + str(w1) + "_w2=" + str(w2) + "_DataDrivenVsMC_"), createRatio=True) moveLegend = {"dx": -0.295, "dy": 0.05} plot.setLegend( histograms.moveLegend(histograms.createLegend(), **moveLegend)) histograms.addText( 0.65, 0.20, optMode.replace("OptQCDTailKiller", "R_{BB} ").replace("Plus", ""), 25) histograms.addCmsPreliminaryText() histograms.addEnergyText() lumi = datasets.getDataset("Data").getLuminosity() histograms.addLuminosityText(x=None, y=None, lumi=lumi) plot.draw() plot.save() num = 0 denom = 0 #print var_values for i in range(0, len(nom_values)): num += var_values[i] * (var_values[i] - nom_values[i])**2 denom += var_values[i] diff = num / denom diff_list.append(diff) diff_opt.append(diff_list) print w1_list, '\n' for i in range(0, len(diff_opt)): print diff_opt[i] print w1_list[diff_opt[i].index(min(diff_opt[i]))] mt_baseline = mt_baseline.Rebin(len(bins) - 1, "", arr) plot_bft = plots.PlotBase() plot_bft.histoMgr.appendHisto(histograms.Histo(mt_baseline, "baseline")) #mt_corr.Scale(2) #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test")) #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin() #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax() #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4) #theFit.SetParLimits(0,0.5,10000) #theFit.SetParLimits(1,90,10000) #theFit.SetParLimits(2,30,10000) #theFit.SetParLimits(3,0.001,10000) #mt_signalfaketaus.Fit(theFit,"R") #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax()) #theFit.SetLineStyle(2) #theFit.SetLineColor(4) #theFit.SetLineWidth(3) #theFit.Draw() #mt_corr = theFit.GetHistogram() #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray) #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum()) #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test")) #plot_bft.histoMgr.appendHisto(histograms.Histo(theFit,"theFit")) plot_bft.createFrame('BaselineFakeTaus') plot_bft.draw() plot_bft.save()