def getTrees(self): """Opens the ntuple files and loads the tree from each""" if self.isData: filenames = {"Data":self.analysis.filenames["Data"]} else: filenames = {proc:self.analysis.filenames[proc] for proc in self.analysis.filenames if proc != "Data" and proc != "QCD"} files = { proc:rt.TFile.Open(filenames[proc]) for proc in filenames } trees = macro.makeTreeDict(files, "RazorInclusive") return trees
def getTrees(self): """Opens the ntuple files and loads the tree from each""" if self.isData: filenames = {"Data": self.analysis.filenames["Data"]} else: filenames = { proc: self.analysis.filenames[proc] for proc in self.analysis.filenames if proc != "Data" and proc != "QCD" } files = {proc: rt.TFile.Open(filenames[proc]) for proc in filenames} trees = macro.makeTreeDict(files, "RazorInclusive") return trees
def makeControlSampleHists(regionName="TTJetsSingleLepton", filenames={}, samples=[], cutsMC="", cutsData="", bins={}, logX=True, lumiMC=1, lumiData=3000, weightHists={}, sfHists={}, treeName="ControlSampleEvent",dataName="Data", weightOpts=["doPileupWeights", "doLep1Weights", "do1LepTrigWeights"], shapeErrors=[], debugLevel=0): #setup files and trees inputs = filenames files = {name:rt.TFile.Open(inputs[name]) for name in inputs} #get input files for name in inputs: assert files[name] #check input files if debugLevel > 0: print "Opened file",inputs[name] trees = macro.makeTreeDict(files, treeName, debugLevel) #define histograms to fill hists = {name:{} for name in inputs} shapeHists = {name:{} for name in inputs} for name in inputs: #1D histograms for var in bins: if var in titles: title=titles[var] else: title = var hists[name][var] = rt.TH1F(regionName+var+name, title, len(bins[var])-1, array('d',bins[var])) #add up/down histograms for each systematic uncertainty if name in samples: for shape in shapeErrors: if shape+"Down" not in shapeHists[name]: shapeHists[name][shape+"Down"] = {} if shape+"Up" not in shapeHists[name]: shapeHists[name][shape+"Up"] = {} shapeHists[name][shape+"Down"][var] = hists[name][var].Clone(hists[name][var].GetName()+shape+"Down") shapeHists[name][shape+"Up"][var] = hists[name][var].Clone(hists[name][var].GetName()+shape+"Up") #2D MR-Rsq histogram if "MR" in bins and "Rsq" in bins: hists[name][("MR","Rsq")] = rt.TH2F(regionName+"MRRsq"+name, "R^{2} vs M_{R}", len(bins["MR"])-1, array('d',bins["MR"]), len(bins["Rsq"])-1, array('d',bins["Rsq"])) if name in samples: for shape in shapeErrors: shapeHists[name][shape+"Down"][("MR","Rsq")] = hists[name][("MR","Rsq")].Clone(hists[name][("MR","Rsq")].GetName()+shape+"Down") shapeHists[name][shape+"Up"][("MR","Rsq")] = hists[name][("MR","Rsq")].Clone(hists[name][("MR","Rsq")].GetName()+shape+"Up") for var in hists[name]: hists[name][var].Sumw2() hists[name][var].SetDirectory(0) if name in samples: for shape in shapeErrors: shapeHists[name][shape+"Down"][var].Sumw2() shapeHists[name][shape+"Up"][var].Sumw2() shapeHists[name][shape+"Down"][var].SetDirectory(0) shapeHists[name][shape+"Up"][var].SetDirectory(0) listOfVars = hists.itervalues().next().keys() #list of the variable names #fill histograms print("\nData:") macro.loopTree(trees[dataName], weightF=weight_data, cuts=cutsData, hists=hists[dataName], weightHists=weightHists, weightOpts=[], debugLevel=debugLevel) print("\nMC:") macro.loopTrees(trees, weightF=weight_mc, cuts=cutsMC, hists={name:hists[name] for name in samples}, weightHists=weightHists, sfHists=sfHists, scale=lumiData*1.0/lumiMC, weightOpts=weightOpts, debugLevel=debugLevel) #get up/down histogram variations for shape in shapeErrors: print "\n"+shape,"Up:" macro.loopTrees(trees, weightF=weight_mc, cuts=cutsMC, hists={name:shapeHists[name][shape+"Up"] for name in samples}, weightHists=weightHists, sfHists=sfHists, scale=lumiData*1.0/lumiMC, weightOpts=weightOpts, errorOpt=shape+"Up", debugLevel=debugLevel) print "\n"+shape,"Down:" macro.loopTrees(trees, weightF=weight_mc, cuts=cutsMC, hists={name:shapeHists[name][shape+"Down"] for name in samples}, weightHists=weightHists, sfHists=sfHists, scale=lumiData*1.0/lumiMC, weightOpts=weightOpts, errorOpt=shape+"Down", debugLevel=debugLevel) #propagate up/down systematics to central histograms for name in samples: for var in bins: for shape in shapeErrors: if debugLevel > 0: print "Adding",shape,"uncertainty in quadrature with",name,"errors for",var #loop over histogram bins for bx in range(1, hists[name][var].GetNbinsX()+1): #use difference between Up and Down histograms as uncertainty sysErr = abs(shapeHists[name][shape+'Up'][var].GetBinContent(bx) - shapeHists[name][shape+'Down'][var].GetBinContent(bx)) #add in quadrature with existing error oldErr = hists[name][var].GetBinError(bx) hists[name][var].SetBinError(bx, (oldErr**2 + sysErr**2)**(0.5)) if debugLevel > 1: print "Error on bin ",bx,"increases from",oldErr,"to",hists[name][var].GetBinError(bx) #2D case if "MR" in bins and "Rsq" in bins: for shape in shapeErrors: if debugLevel > 0: print "Adding",shape,"uncertainty in quadrature with",name,"errors for razor histogram" #loop over histogram bins for bx in range(1, hists[name][("MR","Rsq")].GetNbinsX()+1): for by in range(1, hists[name][("MR","Rsq")].GetNbinsY()+1): #use difference between Up and Down histograms as uncertainty sysErr = abs(shapeHists[name][shape+'Up'][("MR","Rsq")].GetBinContent(bx,by) - shapeHists[name][shape+'Down'][("MR","Rsq")].GetBinContent(bx,by)) #add in quadrature with existing error oldErr = hists[name][("MR","Rsq")].GetBinError(bx,by) hists[name][("MR","Rsq")].SetBinError(bx,by, (oldErr**2 + sysErr**2)**(0.5)) if debugLevel > 1: print "Error on bin (",bx,by,") increases from",oldErr,"to",hists[name][("MR","Rsq")].GetBinError(bx,by) #print histograms c = rt.TCanvas(regionName+"c", regionName+"c", 800, 600) rt.SetOwnership(c, False) macro.basicPrint(hists, mcNames=samples, varList=listOfVars, c=c, printName=regionName, logx=logX, dataName=dataName, ymin=0.1, lumistr=str(lumiData)+" pb^{-1}") #close files and return for f in files: files[f].Close() return hists
rt.gROOT.SetBatch() if len(sys.argv) > 1: debug = bool(sys.argv[1]) else: debug = False if len(sys.argv) > 2: maxEvents = int(sys.argv[2]) else: maxEvents = -1 #setup print("Initializing") inputs = Filenames() files = {name:rt.TFile.Open(inputs[name]) for name in inputs} #get input files for name in inputs: assert files[name] #check input files hists = {name:{} for name in inputs} for name in inputs: files[name] = rt.TFile.Open(inputs[name]) #load trees trees = macro.makeTreeDict(files, "ControlSampleEvent", debug) #load weight histograms weightHists = macro.loadWeightHists(debug) #define histograms to fill for name in hists: hists[name]["MR"] = rt.TH1F("MR"+name, "M_{R} (GeV)", 20, 300, 4000) hists[name]["Rsq"] = rt.TH1F("Rsq"+name, "R^{2}", 20, 0.15, 1.5) for var in hists[name]: hists[name][var].Sumw2() #fill histograms print("MC:") macro.loopTrees(trees, cuts_TTJetsSingleLepton, weight_mc, weightHists, Fill, {name:hists[name] for name in hists if name != "Data"}, maxEvents=maxEvents, scale=LUMI*1.0/MCLUMI, debug=debug) #MC print("Data:") macro.loopTree(trees["Data"], cuts_TTJetsSingleLepton, weight_data, {}, Fill, hists["Data"], debug=debug) #Data