def _obtainQCDNormalizationSystHistograms(self, shapeHisto, dsetMgr, plotName, luminosity, normDataSrc, normEWKSrc): print ShellStyles.HighlightStyle( ) + "...Obtaining region transition systematics" + ShellStyles.NormalStyle( ) myPlotSignalRegionShape = dataDrivenQCDCount.DataDrivenQCDShape( dsetMgr=dsetMgr, dsetLabelData="Data", dsetLabelEwk="EWK", histoName=plotName, dataPath=normDataSrc + "", #"QCDNormalizationSignal", ewkPath=normEWKSrc + "", #"QCDNormalizationSignal", luminosity=luminosity) myPlotControlRegionShape = dataDrivenQCDCount.DataDrivenQCDShape( dsetMgr=dsetMgr, dsetLabelData="Data", dsetLabelEwk="EWK", histoName=plotName, dataPath=normDataSrc + "", #"QCDNormalizationControl", ewkPath=normEWKSrc + "", #"QCDNormalizationControl", luminosity=luminosity) myPlotRegionTransitionSyst = metSyst.SystematicsForMetShapeDifference( myPlotSignalRegionShape, myPlotControlRegionShape, shapeHisto, moduleInfoString=self._moduleInfoString, quietMode=True) myPlotSignalRegionShape.delete() myPlotControlRegionShape.delete() # Store up and down variations #hUp = aux.Clone(myPlotRegionTransitionSyst.getUpHistogram(), "QCDfactMgrSystQCDSystUp%d"%i) #hUp.SetTitle(plotName+"systQCDUp") #self._QCDNormalizationSystPlots.append(hUp) #self._QCDNormalizationSystPlotLabels.append(hUp.GetTitle()) #hDown = aux.Clone(myPlotRegionTransitionSyst.getDownHistogram(), "QCDfactMgrSystQCDSystDown%d"%i) #hDown.SetTitle(plotName+"systQCDDown") #self._QCDNormalizationSystPlots.append(hDown) #self._QCDNormalizationSystPlotLabels.append(hDown.GetTitle()) # Store source histograms hNum = aux.Clone( myPlotRegionTransitionSyst.getCombinedSignalRegionHistogram(), "QCDfactMgrSystQCDSystNumerator") hNum.SetTitle(plotName + "systQCDNumerator") self._QCDNormalizationSystPlots.append(hNum) self._QCDNormalizationSystPlotLabels.append(hNum.GetTitle()) hDenom = aux.Clone( myPlotRegionTransitionSyst.getCombinedCtrlRegionHistogram(), "QCDfactMgrSystQCDSystDenominator") hDenom.SetTitle(plotName + "systQCDDenominator") self._QCDNormalizationSystPlots.append(hDenom) self._QCDNormalizationSystPlotLabels.append(hDenom.GetTitle()) # Free memory myPlotRegionTransitionSyst.delete()
def _printSelection(self): # Define style here myNotSelectedStr = " " mySelectedStr = " %s--> " % (ShellStyles.HighlightStyle()) mySelectedSuffix = " <--%s" % (ShellStyles.NormalStyle()) print "\nSelected data eras:" for i in range(0, len(self._availableEras)): myStr = myNotSelectedStr mySuffix = "" if self._availableEras[i] in self._selectedEras: myStr = mySelectedStr mySuffix = mySelectedSuffix print "%s%2d: %s%s" % (myStr, i, self._availableEras[i].replace( "Run", ""), mySuffix) print "\nSelected search modes:" for i in range(0, len(self._availableSearchModes)): myStr = myNotSelectedStr mySuffix = "" if self._availableSearchModes[i] in self._selectedSearchModes: myStr = mySelectedStr mySuffix = mySelectedSuffix print "%s%2d: %s%s" % (myStr, i, self._availableSearchModes[i], mySuffix) print "\nSelected optimization modes:" for i in range(0, len(self._availableOptimizationModes)): myStr = myNotSelectedStr mySuffix = "" if self._availableOptimizationModes[ i] in self._selectedOptimizationModes: myStr = mySelectedStr mySuffix = mySelectedSuffix if self._availableOptimizationModes[i] == "": print "%s%2d: (nominal)%s" % (myStr, i, mySuffix) else: print "%s%2d: %s%s" % ( myStr, i, self._availableOptimizationModes[i].replace( "Opt", ""), mySuffix) if not self._disableSystematicsList: print "\nSelected systematic variations:" for i, systVar in enumerate(self._availableSystematicVariations): myStr = myNotSelectedStr mySuffix = "" if systVar in self._selectedSystematicVariations: myStr = mySelectedStr mySuffix = mySelectedSuffix if systVar == "": print "%s%2d: (nominal)%s" % (myStr, i, mySuffix) else: print "%s%2d: %s%s" % ( myStr, i, systVar.replace("SystVar", ""), mySuffix) print ""
def doPlot(name, dset, errorlevel, optimizationMode, lumi): print shellStyles.HighlightStyle()+"Generating plots for dataset '%s'"%name+shellStyles.NormalStyle() s = optimizationMode.split("BjetDiscrWorkingPoint") discrName = s[0].replace("OptBjetDiscr","") discrWP = s[1] myPartons = ["B", "C", "G", "Light"] myPartonLabels = ["b#rightarrowb", "c#rightarrowb", "g#rightarrowb", "uds#rightarrowb"] histoObjects = [] results = [] for i in range(len(myPartons)): n = "All%sjets"%myPartons[i] dsetHisto = dset.getDatasetRootHisto(n) dsetHisto.normalizeToLuminosity(lumi) hAll = dsetHisto.getHistogram() #(hAll, hAllName) = dset.getRootHisto(n) if hAll == None: raise Exception("Error: could not find histogram '%s'!"%n) treatNegativeBins(hAll) n = "Selected%sjets"%myPartons[i] dsetHisto = dset.getDatasetRootHisto(n) dsetHisto.normalizeToLuminosity(lumi) hPassed = dsetHisto.getHistogram() #(hPassed, hPassedName) = dset.getRootHisto(n) if hPassed == None: raise Exception("Error: could not find histogram '%s'!"%n) treatNegativeBins(hPassed) # Find proper binning myBinEdges = [] for k in range(1, hPassed.GetNbinsX()+1): if len(myBinEdges) > 0 or hPassed.GetBinContent(k) > 0: myBinEdges.append(hPassed.GetXaxis().GetBinLowEdge(k)) myBinEdges.append(hPassed.GetXaxis().GetBinUpEdge(hPassed.GetNbinsX())) myArray = array.array("d", myBinEdges) hAllNew = hAll.Rebin(len(myArray)-1, "", myArray) hPassedNew = hPassed.Rebin(len(myArray)-1, "", myArray) (hPassed, hAll) = findProperBinning(hPassedNew, hAllNew, myBinEdges, errorlevel) #print myBinEdges # Treat fluctuations for k in range(hPassed.GetNbinsX()+2): if hPassed.GetBinContent(k) > hAll.GetBinContent(k): hPassed.SetBinContent(k, hAll.GetBinContent(k)) # Construct efficiency plot eff = ROOT.TEfficiency(hPassed, hAll) eff.SetStatisticOption(ROOT.TEfficiency.kFNormal) for k in range(hPassed.GetNbinsX()): resultObject = {} resultObject["flavor"] = myPartons[i] resultObject["ptMin"] = hPassed.GetXaxis().GetBinLowEdge(k+1) resultObject["ptMax"] = hPassed.GetXaxis().GetBinUpEdge(k+1) resultObject["eff"] = eff.GetEfficiency(k+1) resultObject["effUp"] = eff.GetEfficiencyErrorUp(k+1) resultObject["effDown"] = eff.GetEfficiencyErrorLow(k+1) resultObject["discr"] = discrName resultObject["workingPoint"] = discrWP results.append(resultObject) #gEff = eff.CreateGraph() styles.styles[i].apply(eff) hobj = histograms.HistoEfficiency(eff, myPartonLabels[i], legendStyle="P", drawStyle="") #hobj = histograms.HistoGraph(gEff, myPartonLabels[i], legendStyle="P", drawStyle="") hobj.setIsDataMC(False, True) histoObjects.append(hobj) myPlot = plots.PlotBase(histoObjects) #myPlot.setLuminosity(-1) # Do not set myPlot.setEnergy("13") #myPlot.setDefaultStyles() myParams = {} myParams["xlabel"] = "Jet p_{T}, GeV" myParams["ylabel"] = "Probability for passing b tagging" myParams["log"] = True myParams["cmsExtraText"] = "Simulation" myParams["cmsTextPosition"] = "outframe" # options: left, right, outframe myParams["opts"] = {"ymin": 1e-3, "ymax": 1.0} #myParams["opts2"] = {"ymin": 0.5, "ymax":1.5} #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.0, "dy": -0.46} # for data-driven drawPlot = plots.PlotDrawer(ratio=False, #stackMCHistograms=False, addMCUncertainty=True, addLuminosityText=False, cmsTextPosition="outframe") drawPlot(myPlot, "%s_%s"%(dset.name, name), **myParams) return results
if __name__ == "__main__": # Check parameters if len(sys.argv) < 2: usage() # Find out the era/search mode/optimization mode combinations and run each of them myModuleSelector = analysisModuleSelector.AnalysisModuleSelector() dsetMgrCreator = dataset.readFromMulticrabCfg(directory=sys.argv[1]) myModuleSelector.setPrimarySource("analysis", dsetMgrCreator) myModuleSelector.doSelect(None) #myModuleSelector.printSelectedCombinationCount() for era in myModuleSelector.getSelectedEras(): for searchMode in myModuleSelector.getSelectedSearchModes(): for optimizationMode in myModuleSelector.getSelectedOptimizationModes( ): print ShellStyles.HighlightStyle( ) + "\nCalculating normalization for module %s/%s/%s%s" % ( era, searchMode, optimizationMode, ShellStyles.NormalStyle()) # Construct info string for the module moduleInfoString = "%s_%s" % (era, searchMode) if len(optimizationMode) > 0: moduleInfoString += "_%s" % (optimizationMode) # Create dataset manager dsetMgr = dsetMgrCreator.createDatasetManager( dataEra=era, searchMode=searchMode, optimizationMode=optimizationMode) main(sys.argv, dsetMgr, moduleInfoString) dsetMgrCreator.close()
ROOT.PyConfig.IgnoreCommandLineOptions = True import HiggsAnalysis.NtupleAnalysis.tools.histograms as histograms import HiggsAnalysis.NtupleAnalysis.tools.tdrstyle as tdrstyle import HiggsAnalysis.NtupleAnalysis.tools.plots as plots import HiggsAnalysis.NtupleAnalysis.tools.styles as styles import HiggsAnalysis.Keras_ANN.results as _results import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles import HiggsAnalysis.NtupleAnalysis.tools.aux as aux #================================================================================================ # Shell Types #================================================================================================ sh_e = ShellStyles.ErrorStyle() sh_s = ShellStyles.SuccessStyle() sh_h = ShellStyles.HighlightStyle() sh_a = ShellStyles.HighlightAltStyle() sh_t = ShellStyles.NoteStyle() sh_n = ShellStyles.NormalStyle() sh_w = ShellStyles.WarningStyle() #================================================================================================ # Function definition #================================================================================================ def Verbose(msg, printHeader=False): ''' Calls Print() only if verbose options is set to true. ''' if not opts.verbose: return
# Create pseudo-multicrab creator myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator( _generalOptions["analysisName"], myMulticrabDir) n = 0 myGlobalStartTime = time.time() for shapeType in opts.shape: # Determine normalization sources #_generalOptions["normalizationDataSource"] = "%s%s%s"%(_generalOptions["normalizationSourcePrefix"], shapeType, _generalOptions["normalizationPoint"]) #obsolete _generalOptions["normalizationDataSource"] = "ForDataDrivenCtrlPlots" # prefix = _generalOptions["EWKsource"].replace(_generalOptions["dataSource"],"") # _generalOptions["normalizationEWKSource"] = _generalOptions["normalizationSourcePrefix"].replace("/","%s/"%prefix) # _generalOptions["normalizationEWKSource"] += "%s%s"%(shapeType, _generalOptions["normalizationPoint"]) _generalOptions["normalizationEWKSource"] = "ForDataDrivenCtrlPlots" # Initialize myOutputCreator.initialize(shapeType) print ShellStyles.HighlightStyle( ) + "Creating dataset for shape: %s%s" % (shapeType, ShellStyles.NormalStyle()) # Loop over era, searchMode, and optimizationMode for era in myModuleSelector.getSelectedEras(): for searchMode in myModuleSelector.getSelectedSearchModes(): for optimizationMode in myModuleSelector.getSelectedOptimizationModes( ): #===== Obtain normalization factors myNormFactors = importNormFactors(era, searchMode, optimizationMode, opts.multicrabDir) #===== Nominal module myModuleInfoString = "%s_%s" % (era, searchMode) if len(optimizationMode) > 0: myModuleInfoString += "_%s" % optimizationMode n += 1
def validateUnfoldedHistogramReader(): def check(a, b): if abs(a - b) < 0.00001: return TestPassedStyle() + "PASSED" + ShellStyles.NormalStyle() else: print ErrorStyle() + "FAILED (%f != %f)" % ( a, b) + ShellStyles.NormalStyle() raise Exception("Error: validation test failed!") print ShellStyles.HighlightStyle( ) + "validate: UnfoldedHistogramReader\n" + ShellStyles.NormalStyle() print "Creating dummy histogram for testing..." # Create histogram with factorisation dimensions 4 x 3 x 4 h = ROOT.TH2F("testHisto", "AxisA:4:AxisB:3:AxisC:4:testHisto", 10, 0., 10., 48, 0., 48.) h.Sumw2() # Fill known information to the histogram myValue = 1 # Loop over bins for k in range(0, 4): for j in range(0, 3): for i in range(0, 4): # Loop over shape idx = (i) + (j) * 4 + (k) * 4 * 3 for b in range(0, 10): for a in range(0, myValue * (b + 1)): h.Fill(b, idx) #print myValue,i,j,k,idx,"test",h.GetBinContent(1,idx+1) myValue += 1 # Then make some tests r = UnfoldedHistogramReader(debugStatus=True) # Test binning specs r._initialize(h) print "validate: UnfoldedHistogramReader::_initialize():", check( len(r.getNbinsList()), 3) # Test bin unfolding print "validate: UnfoldedHistogramReader::_convertBinIndexListToUnfoldedIndex():", check( r._convertBinIndexListToUnfoldedIndex([1, 2, 3]), 45) print "validate: UnfoldedHistogramReader::decomposeUnfoldedbin1(): ", check( r.decomposeUnfoldedbin(45)[0], 1) print "validate: UnfoldedHistogramReader::decomposeUnfoldedbin2(): ", check( r.decomposeUnfoldedbin(45)[1], 2) print "validate: UnfoldedHistogramReader::decomposeUnfoldedbin3(): ", check( r.decomposeUnfoldedbin(45)[2], 3) # Test event counts print "validate: UnfoldedHistogramReader::getEventCountForBin(): ", check( r.getEventCountForBin([1, 2, 3], h), 46) print "validate: UnfoldedHistogramReader::getEventCountUncertaintyForBin(): ", check( r.getEventCountUncertaintyForBin([1, 2, 3], h), sqrt(46)) # Test shape counts print "validate: UnfoldedHistogramReader::getShapeForBin(): ", check( r.getShapeForBin([3, 1, 2], h)[2], 32 * 3) print "validate: UnfoldedHistogramReader::getShapeUncertaintyForBin(): ", check( r.getShapeUncertaintyForBin([3, 1, 2], h)[2], sqrt(32 * 3)) # Test contracted event counts print "validate: UnfoldedHistogramReader::getContractedEventCountForBin(): ", check( r.getContractedEventCountForBin(0, 0, h), 276) print "validate: UnfoldedHistogramReader::getContractedEventCountUncertaintyForBin(): ", check( r.getContractedEventCountUncertaintyForBin(0, 0, h), sqrt(276)) print "validate: UnfoldedHistogramReader::getContractedShapeCountForBin() test1: ", check( r.getContractedShapeForBin(2, 1, h)[0], 222) print "validate: UnfoldedHistogramReader::getContractedShapeCountUncertaintForBin() test1: ", check( r.getContractedShapeUncertaintyForBin(2, 1, h)[0], sqrt(222)) print "validate: UnfoldedHistogramReader::getContractedShapeCountForBin() test2: ", check( r.getContractedShapeForBin(2, 1, h)[3], 888) print "validate: UnfoldedHistogramReader::getContractedShapeCountUncertaintForBin() test2: ", check( r.getContractedShapeUncertaintyForBin(2, 1, h)[3], sqrt(888))
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 __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 main(): # Object for selecting data eras, search modes, and optimization modes myModuleSelector = analysisModuleSelector.AnalysisModuleSelector() # Obtain multicrab directory myMulticrabDir = "." if opts.mcrab != None: myMulticrabDir = opts.mcrab if not os.path.exists("%s/multicrab.cfg" % myMulticrabDir): msg = "No multicrab directory found at path '%s'! Please check path or specify it with --mcrab!" % ( myMulticrabDir) raise Exception(ShellStyles.ErrorLabel() + msg + ShellStyles.NormalStyle()) if len(opts.shape) == 0: raise Exception( ShellStyles.ErrorLabel() + "Provide a shape identifierwith --shape (for example MT)!" + ShellStyles.NormalStyle()) # Obtain dsetMgrCreator and register it to module selector dsetMgrCreator = dataset.readFromMulticrabCfg(directory=myMulticrabDir) # Obtain systematics names mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources() mySystematicsNames = [] for item in mySystematicsNamesRaw: mySystematicsNames.append("%sPlus" % item) mySystematicsNames.append("%sMinus" % item) if opts.test: mySystematicsNames = [] #[mySystematicsNames[0]] #FIXME # Set the primary source myModuleSelector.setPrimarySource(label=opts.analysisName, dsetMgrCreator=dsetMgrCreator) # Select modules myModuleSelector.doSelect(opts=None) #FIXME: (opts=opts) # Loop over era/searchMode/optimizationMode combos myDisplayStatus = True myTotalModules = myModuleSelector.getSelectedCombinationCount() * ( len(mySystematicsNames) + 1) * len(opts.shape) Verbose("Found %s modules in total" % (myTotalModules), True) count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually( ) if nSysVars > 0: msg = "Will run over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % ( count, nEras, nSearchModes, nOptModes, nSysVars) else: msg = "Will run over %d modules (%d eras x %d searchModes x %d optimizationModes)" % ( count, nEras, nSearchModes, nOptModes) Print(msg, True) # Create pseudo-multicrab creator myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator( opts.analysisName, myMulticrabDir) # Make time stamp for start time myGlobalStartTime = time.time() iModule = 0 # For-loop: All Shapes for shapeType in opts.shape: # Initialize myOutputCreator.initialize(shapeType, prefix="") msg = "Creating dataset for shape \"%s\"%s" % ( shapeType, ShellStyles.NormalStyle()) Verbose(ShellStyles.HighlightStyle() + msg, True) # Get lists of settings erasList = myModuleSelector.getSelectedEras() modesList = myModuleSelector.getSelectedSearchModes() optList = myModuleSelector.getSelectedOptimizationModes() optList.append("") #append the default opt mode! # For-Loop over era, searchMode, and optimizationMode options for era in erasList: for searchMode in modesList: for optimizationMode in optList: Verbose( "era = %s, searchMode = %s, optMode = %s" % (era, searchMode, optimizationMode), True) # If an optimization mode is defined in options skip the rest if opts.optMode != None: if optimizationMode != opts.optMode: continue # Obtain normalization factors myNormFactors = importNormFactors(era, searchMode, optimizationMode, opts.mcrab) # Nominal module myModuleInfoString = getModuleInfoString( era, searchMode, optimizationMode) iModule += 1 # Inform user of what is being processes msg = "Module %d/%d:%s %s/%s" % ( iModule, myTotalModules, ShellStyles.NormalStyle(), myModuleInfoString, shapeType) Print(ShellStyles.CaptionStyle() + msg, True) # Keep time myStartTime = time.time() Verbose("Create dataset manager with given settings", True) nominalModule = ModuleBuilder(opts, myOutputCreator) nominalModule.createDsetMgr(myMulticrabDir, era, searchMode, optimizationMode) if (iModule == 1): if opts.verbose: nominalModule.debug() doQCDNormalizationSyst = False #FIXME if not doQCDNormalizationSyst: msg = "Disabling systematics" Print(ShellStyles.WarningLabel() + msg, True) nominalModule.buildModule(opts.dataSrc, opts.ewkSrc, myNormFactors["nominal"], doQCDNormalizationSyst, opts.normDataSrc, opts.normEwkSrc) if len(mySystematicsNames) > 0: Print( "Adding QCD normalization systematics (iff also other systematics present) ", True) nominalModule.buildQCDNormalizationSystModule( opts.dataSrc, opts.ewkSrc) # FIXME: add quark gluon weighting systematics! if 0: Print("Adding Quark/Gluon weighting systematics", True) nominalModule.buildQCDQuarkGluonWeightingSystModule( opts.dataSrc, opts.ewkSrc, myNormFactors["FakeWeightingUp"], myNormFactors["FakeWeightingDown"], False, opts.normDataSrc, opts.normEwkSrc) Verbose("Deleting nominal module", True) nominalModule.delete() Verbose("Printing time estimate", True) printTimeEstimate(myGlobalStartTime, myStartTime, iModule, myTotalModules) Verbose("Now do the rest of systematics variations", True) for syst in mySystematicsNames: iModule += 1 msg = "Analyzing systematics variations %d/%d: %s/%s/%s" % ( iModule, myTotalModules, myModuleInfoString, syst, shapeType) Print( ShellStyles.CaptionStyle() + msg + ShellStyles.NormalStyle(), True) myStartTime = time.time() systModule = ModuleBuilder(opts, myOutputCreator) # Create dataset manager with given settings systModule.createDsetMgr(myMulticrabDir, era, searchMode, optimizationMode, systematicVariation=syst) # Build asystematics module systModule.buildModule(opts.dataSrc, opts.ewkSrc, myNormFactors["nominal"], False, opts.normDataSrc, opts.normEwkSrc) printTimeEstimate(myGlobalStartTime, myStartTime, iModule, myTotalModules) systModule.delete() Verbose("Pseudo-multicrab ready for %s" % shapeType, True) # Create rest of pseudo multicrab directory myOutputCreator.silentFinalize() # Print some timing statistics Print( "Average processing time per module was %.1f s" % getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True) Print( "Total elapsed time was %.1f s" % getTotalElapsedTime(myGlobalStartTime), False) msg = "Created pseudo-multicrab %s for shape type \"%s\"" % ( myOutputCreator.getDirName(), shapeType) Print(ShellStyles.SuccessLabel() + msg, True) return
def __init__(self, dataPath, ewkPath, dsetMgr, luminosity, moduleInfoString, normFactors, optionCalculateQCDNormalizationSyst=True, normDataSrc = None, normEWKSrc = None, optionUseInclusiveNorm=False, verbose=False): self._shapePlots = [] self._shapePlotLabels = [] self._QCDNormalizationSystPlots = [] self._QCDNormalizationSystPlotLabels = [] self._moduleInfoString = moduleInfoString self._useInclusiveNorm = optionUseInclusiveNorm if len(normFactors.keys()) == 1 and normFactors.keys()[0] == "Inclusive": self._useInclusiveNorm = True self._verbose = verbose msg = "Obtaining final shape from data path \"%s\"" % (dataPath) Verbose(ShellStyles.HighlightStyle() + msg + ShellStyles.NormalStyle(), True) # Determine list of plots to consider myObjects = dsetMgr.getDataset("Data").getDirectoryContent(dataPath) # Ignore unwanted histograms and those designed for HToTauNu keywordList = ["JetEtaPhi"] ignoreList = [] for k in keywordList: ignoreList.extend(filter(lambda name: k in name, myObjects)) msg = "Ignoring a total of %s histograms:" % (len(ignoreList)) Print(ShellStyles.WarningLabel() + msg, True) for hName in ignoreList: print "\t", os.path.join(dataPath, hName) # Update myObjects list with filtered results myObjects = list(x for x in myObjects if x not in ignoreList) # For-Loop: All plots to consider for i, plotName in enumerate(myObjects, 1): # For testing #if "LdgTrijetMass_AfterAllSelections" not in plotName: # continue msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(myObjects)), os.path.join(dataPath, plotName) ) Print(ShellStyles.HighlightAltStyle() + msg + ShellStyles.NormalStyle(), i==1) # Ensure that histograms exist dataOk = self._sanityChecks(dsetMgr, dataPath, plotName) ewkOk = self._sanityChecks(dsetMgr, ewkPath, plotName) Verbose("Obtaining shape plots (the returned object is not owned)", True) myShapeHisto = self._obtainShapeHistograms(i, dataPath, ewkPath, dsetMgr, plotName, luminosity, normFactors) # Obtain plots for systematics coming from met shape difference for control plots #FIXME-Systematics if optionCalculateQCDNormalizationSyst: if isinstance(myShapeHisto, ROOT.TH2): msg = "Skipping met shape uncertainty because histogram has more than 1 dimensions!" Print(ShellStyles.WarningLabel() + msg, True) else: self._obtainQCDNormalizationSystHistograms(myShapeHisto, dsetMgr, plotName, luminosity, normDataSrc, normEWKSrc) return
def _doCalculate(self, shape, moduleInfoString, normFactors, optionPrintPurityByBins, optionDoNQCDByBinHistograms): ''' Calculates the result ''' self.Verbose("Calculate final shape in signal region (shape * w_QCD) & initialize result containers", True) nSplitBins = shape.getNumberOfPhaseSpaceSplitBins() self.Verbose("The phase-space of shape %s is split into %i bin(s)" % (shape.getDataDrivenQCDHistoForSplittedBin(0).GetName(), nSplitBins), True) self.Verbose("Create Data-Driven Shape", True) self._resultShape = aux.Clone(shape.getDataDrivenQCDHistoForSplittedBin(0)) self._resultShape.Reset() self._resultShape.SetTitle("FakeB_Total_%s"%moduleInfoString) self._resultShape.SetName("FakeB_Total_%s"%moduleInfoString) self.Verbose("Create EWK shape", True) self._resultShapeEWK = aux.Clone(shape.getDataDrivenQCDHistoForSplittedBin(0)) self._resultShapeEWK.Reset() self._resultShapeEWK.SetTitle("FakeB_EWK_%s"%moduleInfoString) self._resultShapeEWK.SetName("FakeB_EWK_%s"%moduleInfoString) self.Verbose("Create Purity shape", True) self._resultShapePurity = aux.Clone(shape.getDataDrivenQCDHistoForSplittedBin(0)) self._resultShapePurity.Reset() self._resultShapePurity.SetTitle("FakeB_Purity_%s"%moduleInfoString) self._resultShapePurity.SetName("FakeB_Purity_%s"%moduleInfoString) self._histogramsList = [] myUncertaintyLabels = ["statData", "statEWK"] self._resultCountObject = extendedCount.ExtendedCount(0.0, [0.0, 0.0], myUncertaintyLabels) if optionDoNQCDByBinHistograms: for i in range(0, nSplitBins): hBin = aux.Clone(self._resultShape) hBin.SetTitle("FakeB_%s_%s"%(shape.getPhaseSpaceBinFileFriendlyTitle(i).replace(" ",""), moduleInfoString)) hBin.SetName("FakeB_%s_%s"%(shape.getPhaseSpaceBinFileFriendlyTitle(i).replace(" ",""), moduleInfoString)) self._histogramsList.append(hBin) if isinstance(self._resultShape, ROOT.TH2): self.Verbose("Skippings TH2 histogram with name '%s'. The 2d function needs validation first!" % (self._resultShape.GetName()), False) # self._doCalculate2D(nSplitBins, shape, normFactors, optionPrintPurityByBins, optionDoNQCDByBinHistograms, myUncertaintyLabels) return # Intialize counters for purity calculation in final shape binning myShapeDataSum = [] myShapeDataSumUncert = [] myShapeEwkSum = [] myShapeEwkSumUncert = [] # For-loop: All histogram bins for j in range(1, self._resultShape.GetNbinsX()+1): myShapeDataSum.append(0.0) myShapeDataSumUncert.append(0.0) myShapeEwkSum.append(0.0) myShapeEwkSumUncert.append(0.0) # FIXME: New addition to fix "optionUseInclusiveNorm" functionality (24-Mar-2018). Should not affect binned result! if self._optionUseInclusiveNorm: nSplitBins = 1 self.Verbose("Calculate results separately for each phase-space bin and then combine", True) # For-loop: All measurement bins (e.g. tetrajetBejt eta and/or pT bins for h2tb) for i in range(0, nSplitBins): # The zeroth bin (i==0) is the "Inclusive" bin msg = "{:<10} {:>3} {:<1} {:<3} {:<30}".format("Splitted-Bin", "%i" % i, "/", "%s" % (nSplitBins), "") self.Verbose(ShellStyles.HighlightStyle() + msg + ShellStyles.NormalStyle(), False) hName = shape.getDataDrivenQCDHistoForSplittedBin(i).GetName() self.Verbose("Get data-driven Fake-b, data, and EWK MC shape histogram %s for the phase-space bin #%i" % (hName, i), i==0) h = shape.getDataDrivenQCDHistoForSplittedBin(i) hData = shape.getDataHistoForSplittedBin(i) hEwk = shape.getEwkHistoForSplittedBin(i) self.Verbose("Get normalization factor", True) wQCDLabel = "%s" % i #shape.getPhaseSpaceBinFileFriendlyTitle(i) if self._optionUseInclusiveNorm: wQCDLabel = "Inclusive" wQCD = 0.0 if not wQCDLabel in normFactors.keys(): msg = "No normalization factors available for bin '%s' when accessing histogram %s! Ignoring this bin..." % (wQCDLabel, shape.getHistoName()) self.Print(ShellStyles.WarningLabel() + msg, True) else: wQCD = normFactors[wQCDLabel] msg = "Weighting bin \"%i\" (label=\"%s\") with normFactor \"%s\"" % (i, wQCDLabel, wQCD) self.Verbose(ShellStyles.NoteLabel() + msg, True) # Construct info table (debugging) table = [] align = "{:>6} {:^10} {:^15} {:>10} {:>10} {:>10} {:^3} {:^12} {:^3} {:^12}" header = align.format("Bin", "Width", "Range", "Content", "NormFactor", "FakeB", "+/-", "Stat. Data", "+/-", "Stat. EWK") hLine = "="*100 table.append("{:^100}".format(shape.getHistoName())) table.append(hLine) table.append(header) table.append(hLine) binSum = 0.0 nBins = h.GetNbinsX() binWidth = hData.GetBinWidth(0) xMin = hData.GetXaxis().GetBinCenter(0) xMax = hData.GetXaxis().GetBinCenter(nBins+1) # For-Loop (nested): All bins in the shape histogram for j in range(1, nBins+1): # Initialise values myResult = 0.0 myStatDataUncert = 0.0 myStatEwkUncert = 0.0 # Ignore zero bins if abs(h.GetBinContent(j)) > 0.00001: # self.Verbose("Bin %i: Calculating the result" % (j) , j==0) binContent = h.GetBinContent(j) binRange = "%.1f -> %.1f" % (h.GetXaxis().GetBinLowEdge(j), h.GetXaxis().GetBinUpEdge(j) ) binWidth = GetTH1BinWidthString(h, j) binSum += binContent myResult = binContent * wQCD #apply normalisation factor (transfer from CR to SR) self.Verbose("Bin %i: BinContent = %.3f x %.3f = %.3f" % (j, binContent, wQCD, myResult), False) # self.Verbose("Calculate abs. stat. uncert. for data and for MC EWK (Do not calculate here MC EWK syst.)", True) myStatDataUncert = hData.GetBinError(j) * wQCD myStatEwkUncert = hEwk.GetBinError(j) * wQCD table.append(align.format(j, binWidth, binRange, "%0.1f" % binContent, wQCD, "%.1f" % myResult, "+/-", "%.1f" % myStatDataUncert, "+/-", "%.1f" % myStatEwkUncert)) # Get count object myCountObject = extendedCount.ExtendedCount(myResult, [myStatDataUncert, myStatEwkUncert], myUncertaintyLabels) self._resultCountObject.add(myCountObject) if optionDoNQCDByBinHistograms: self.Verbose("Setting bin content \"%i\"" % (j), True) self._histogramsList[i].SetBinContent(j, myCountObject.value()) self._histogramsList[i].SetBinError(j, myCountObject.statUncertainty()) binContent = self._resultShape.GetBinContent(j) + myCountObject.value() binError = self._resultShape.GetBinError(j) + myCountObject.statUncertainty()**2 self.Verbose("Setting bin %i to content %0.1f +/- %0.1f" % (j, binContent, binError), j==0) self._resultShape.SetBinContent(j, binContent) self._resultShape.SetBinError(j, binError) # Sum squared (take sqrt outside loop on final squared sum) self.Verbose("Sum items for purity calculation", True) myShapeDataSum[j-1] += hData.GetBinContent(j)*wQCD myShapeDataSumUncert[j-1] += (hData.GetBinError(j)*wQCD)**2 myShapeEwkSum[j-1] += hEwk.GetBinContent(j)*wQCD myShapeEwkSumUncert[j-1] += (hEwk.GetBinError(j)*wQCD)**2 # Delete the shape histograms h.Delete() hData.Delete() hEwk.Delete() # For-loop: All histogram bins for j in range(1,self._resultShape.GetNbinsX()+1): # Take square root of uncertainties self._resultShape.SetBinError(j, math.sqrt(self._resultShape.GetBinError(j))) # Print detailed results in a formatted table qcdResults = self._resultCountObject.getResultAndStatErrorsDict() bins = "%0.f-%.0f" % (1, nBins) binRange = "%.1f -> %.1f" % (xMin, xMax) binSum = "%.1f" % binSum nQCD = "%.1f" % qcdResults["value"] dataStat = "%.1f" % qcdResults["statData"] ewkStat = "%.1f" % qcdResults["statEWK"] table.append(align.format(bins, binWidth, binRange, binSum, wQCD, nQCD, "+/-", dataStat, "+/-", ewkStat)) table.append(hLine) # For-loop: All lines in table for i, line in enumerate(table): if i == len(table)-2: self.Verbose(ShellStyles.TestPassedStyle()+line+ShellStyles.NormalStyle(), i==1) else: self.Verbose(line, i==0) # Calculate the Purity histograms self.CalculatePurity(nBins, shape, myShapeDataSum, myShapeDataSumUncert, myShapeEwkSum, myShapeEwkSumUncert, verbose=optionPrintPurityByBins) return
import sys import datetime import string from optparse import OptionParser import HiggsAnalysis.NtupleAnalysis.tools.aux as aux import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles #================================================================================================ # Variable definition #================================================================================================ ss = ShellStyles.SuccessStyle() ns = ShellStyles.NormalStyle() ts = ShellStyles.NoteStyle() hs = ShellStyles.HighlightAltStyle() ls = ShellStyles.HighlightStyle() es = ShellStyles.ErrorStyle() cs = ShellStyles.CaptionStyle() #================================================================================================ # Function definition #================================================================================================ def Verbose(msg, printHeader=False): ''' Calls Print() only if verbose options is set to true. ''' if not opts.verbose: return Print(msg, printHeader) return
# Do calculation manager.calculateCombinedNormalizationCoefficient(qcdMt, ewkFakeTausMt) #===== Save normalization outFileName = "QCDNormalizationFactors_%s_%s.py"%(HISTONAME, moduleInfoString) outFileFullName = os.path.join(argv[1],outFileName) manager.writeScaleFactorFile(outFileFullName, moduleInfoString) if __name__ == "__main__": # Check parameters if len(sys.argv) < 2: usage() # Find out the era/search mode/optimization mode combinations and run each of them myModuleSelector = analysisModuleSelector.AnalysisModuleSelector() dsetMgrCreator = dataset.readFromMulticrabCfg(directory=sys.argv[1]) myModuleSelector.setPrimarySource("analysis", dsetMgrCreator) myModuleSelector.doSelect(None) #myModuleSelector.printSelectedCombinationCount() for era in myModuleSelector.getSelectedEras(): for searchMode in myModuleSelector.getSelectedSearchModes(): for optimizationMode in myModuleSelector.getSelectedOptimizationModes(): print ShellStyles.HighlightStyle()+"\nCalculating normalization for module %s/%s/%s%s"%(era, searchMode, optimizationMode, ShellStyles.NormalStyle()) # Construct info string for the module moduleInfoString = "%s_%s"%(era, searchMode) if len(optimizationMode) > 0: moduleInfoString += "_%s"%(optimizationMode) # Create dataset manager dsetMgr = dsetMgrCreator.createDatasetManager(dataEra=era,searchMode=searchMode,optimizationMode=optimizationMode) main(sys.argv, dsetMgr, moduleInfoString) dsetMgrCreator.close()