def plotRatio(var,name,hist1,hist2): """Plot minimal ratio of two histograms.""" canvas = TCanvas('canvas','canvas',100,100,800,800) canvas.Divide(2) canvas.cd(1) gPad.SetPad('pad1','pad1',0,0.42,1,1,0,-1,0) gPad.SetTopMargin( 0.10 ); gPad.SetBottomMargin( 0.01 ) hist1.Draw() hist2.Draw('SAME') hist1.SetLineColor(kBlue) hist2.SetLineColor(kRed) hist1.SetLineWidth(2) hist2.SetLineWidth(2) hist1.SetLineStyle(1) hist2.SetLineStyle(2) hist1.Draw('HIST') hist2.Draw('HIST SAMES') gPad.Update() stats1 = hist1.GetListOfFunctions().FindObject('stats') stats2 = hist2.GetListOfFunctions().FindObject('stats') stats1.SetY1NDC(.74); stats1.SetY2NDC(.94) stats2.SetY1NDC(.50); stats2.SetY2NDC(.70) stats1.Draw() stats2.Draw() canvas.cd(2) gPad.SetPad('pad2','pad2',0,0,1,0.41,0,-1,0) gPad.SetTopMargin( 0.05 ); gPad.SetBottomMargin( 0.24 ) ratio = hist1.Clone('ratio') ratio.Divide(hist2) for i, (y1, y2, r) in enumerate(zip(hist1,hist2,ratio),0): if hist1.GetBinContent(i)==0 and hist2.GetBinContent(i)==0: ratio.SetBinContent(i,1) ratio.GetXaxis().SetTitle(var) ratio.GetXaxis().SetLabelSize(0.045) ratio.GetYaxis().SetLabelSize(0.045) ratio.GetXaxis().SetTitleSize(0.060) ratio.SetMinimum(0.2) ratio.SetMaximum(1.8) ratio.Draw() statsr = ratio.GetListOfFunctions().FindObject('stats') statsr.SetY1NDC(.65); statsr.SetY2NDC(.98) canvas.SaveAs(name) canvas.Close() gDirectory.Delete(hist1.GetName()) gDirectory.Delete(hist2.GetName()) gDirectory.Delete(ratio.GetName())
def createEff1D(histnum2D, histden2D): """Create 1D histogram of efficiency vs. pT for central and forward eta bins.""" etabins = { "|#eta| < 2.5": [(0, 5)], "|#eta| < 1.5": [(2, 3)], "1.5 < |#eta| < 2.5": [(1, 1), (4, 4)], } hists = [] for etatitle, bins in etabins.iteritems(): histnum = None histden = None for bin1, bin2 in bins: if histnum == None or histden == None: histnum = histnum2D.ProjectionX( "%s_%d" % (histnum2D.GetName(), bin1), bin1, bin2) histden = histden2D.ProjectionX( "%s_%d" % (histden2D.GetName(), bin1), bin1, bin2) else: histnum.Add( histnum2D.ProjectionX( "%s_%d" % (histnum2D.GetName(), bin1), bin1, bin2)) histden.Add( histden2D.ProjectionX( "%s_%d" % (histden2D.GetName(), bin1), bin1, bin2)) histnum.Sumw2() histnum.Divide(histden) histnum.SetTitle(etatitle) hists.append(histnum) gDirectory.Delete(histden.GetName()) #for i in xrange(0,histnum.GetXaxis().GetNbins()+1): # print i, histnum.GetBinContent(i) return hists
def normalizeSignal(self,S_exp,**kwargs): """Calculates normalization for a given expected signal yield.""" verbosity = kwargs.get('verbosity',0) cuts = [ ("%s && %s" % (baseline, category1)), ("%s && %s" % (baseline, category2)), ] cuts = kwargs.get('cuts',cuts) weight = kwargs.get('weight',"") (aa,bb) = kwargs.get('signalregion',(0,40)) N = 0; MC = 0 scale = 1 for i,cut in enumerate(cuts): cut = combineCuts("m_sv>0", cut) name = "m_sv_for_signal_renormalization_%d" % i hist = self.hist("m_sv",100,aa,bb,name=name,cuts=cut,weight=weight) N += hist.GetSumOfWeights() MC += hist.GetEntries() gDirectory.Delete(name) if N: scale = S_exp / N * self.scale printVerbose(">>> normalizeSignal: S_exp=%.4f, N=%.4f, MC=%.1f, old scale=%.4f, scale=%.4f" % (S_exp, N, MC, self.scale, scale), verbosity) printVerbose(">>> normalizeSignal: signalregion=(%.1f,%.1f)" % (aa,bb),verbosity) else: print warning("Could not find normalization for signal: no MC events in given signal region after cuts (%s)!" % cuts) self.setAllScales(scale) return scale
def calculateLumiAcceptance(self, cuts, **kwargs): """Calculates scale for a given expected signal yield, to divide out the luminosity and acceptance. This method only returns the scale, it does not rescale the signal.""" verbosity = kwargs.get('verbosity', 0) var = kwargs.get('var', "m_sv") weight = kwargs.get('weight', "") (a, b) = kwargs.get('signalregion', (0, 500)) scale = 1 N_tot = self.N name = "%s_for_LA" % var hist = self.hist(var, 100, a, b, name=name, cuts=cuts, weight=weight) (N, MC) = (hist.GetSumOfWeights(), hist.GetEntries()) gDirectory.Delete(name) #cuts = combineCuts("%s<%s && %s<%s"%(a,var,var,b), cuts) printVerbose(">>> calculateLA:", verbosity) printVerbose(">>> cuts=%s" % (cuts), verbosity) if N_tot and N and lumi: scale = N_tot / (N * lumi * 1000) printVerbose( ">>> N_tot=%.4f, N=%.4f, MC=%.1f, lumi=%s, current scale=%.4f, scale=%.4f" % (N_tot, N, MC, lumi, self.scale, scale), verbosity) #printVerbose(">>> signalregion=(%.1f,%.1f)" % (a,b),verbosity) else: print warning( "Could not find normalization for signal: N_tot=%s, N=%s, lumi=%s!" % (N_tot, N, lumi)) return scale
def compareMCProfiles(samples, channel, era, tag=""): """Compare MC profiles.""" print ">>> compareMCProfiles()" hname = 'pileup' htitle = 'MC average' outdir = ensuredir("plots") avehist = None hists = [] if tag and tag[0] != '_': tag = '_' + tag if 'pmx' in tag: htitle += " %s pre-mixing" % ("old" if "old" in tag else "new") # GET histograms for sample, fname in samples: print ">>> %s" % (fname) file, hist = gethist(fname, hname, retfile=True) hist.SetName(sample) hist.SetTitle(sample) hist.SetDirectory(0) if avehist == None: avehist = hist.Clone('average%s' % tag) avehist.SetTitle(htitle) avehist.SetDirectory(0) else: avehist.Add(hist) hist.Scale(1. / hist.Integral()) hists.append(hist) file.Close() # PLOT hists = [avehist] + hists colors = [kBlack] + linecolors avehist.Scale(1. / avehist.Integral()) pname = "%s/pileup_MC_%s%s" % (outdir, era, tag) xtitle = "Number of true interactions" plot = Plot(hists, ratio=True) plot.draw(xtitle=xtitle, ytitle="A.U.", rtitle="MC / Ave.", textsize=0.032, rmin=0.45, rmax=1.55, denom=2, colors=colors) plot.drawlegend('TTR', tsize=0.04, latex=False) plot.saveas(pname + ".png") plot.saveas(pname + ".pdf") plot.close(keep=True) for hist in hists: # clean memory if hist == avehist: continue if hist.GetDirectory(): gDirectory.Delete(hist.GetName()) else: hist.Delete() return avehist
def compareMCProfiles(indir, samples, channel, year, tag=""): """Compare MC profiles.""" print ">>> compareMCProfiles()" histname = 'pileup' outdir = ensureDirectory("plots") avehist = None hists = [] if tag and tag[0] != '_': tag = '_' + tag # GET histograms for subdir, samplename in samples: filename = "%s/%s/%s_%s.root" % (indir, subdir, samplename, channel) print ">>> %s" % (filename) file = TFile(filename, 'READ') if not file or file.IsZombie(): print ">>> Warning! compareMCProfiles: Could not open %s" % ( filename) continue hist = file.Get(histname) hist.SetName(samplename) hist.SetTitle(samplename) hist.SetDirectory(0) if not hist: print ">>> Warning! compareMCProfiles: Could not open histogram in %s" % ( filename) continue if avehist == None: avehist = hist.Clone('average%s' % tag) avehist.SetTitle('MC average') avehist.SetDirectory(0) avehist.Add(hist) hist.Scale(1. / hist.Integral()) hists.append(hist) file.Close() # PLOT hists = [avehist] + hists colors = [kBlack] + linecolors avehist.Scale(1. / avehist.Integral()) plotname = "%s/pileup_MC_%s%s" % (outdir, year, tag) drawHistsWithRatio(hists, plotname, xtitle="Number of true interactions", ytitle="A.U.", textsize=0.032, rmin=0.45, rmax=1.55, colors=colors) for hist in hists: if hist == avehist: continue if hist.GetDirectory(): gDirectory.Delete(hist.GetName()) else: hist.Delete() return avehist
def plotHists(hists,xtitle,plotname,header,ctexts=[ ],otext="",logy=False,y1=0.70): colors = [ kBlue, kRed, kGreen+2, kOrange, kMagenta+1 ] canvas = TCanvas('canvas','canvas',100,100,800,700) canvas.SetMargin(0.12,0.03,0.14,0.06 if otext else 0.03) textsize = 0.040 height = 1.28*(len(hists)+1)*textsize y1 legend = TLegend(0.65,y1,0.88,y1-height) legend.SetTextSize(textsize) legend.SetBorderSize(0) legend.SetFillStyle(0) legend.SetFillColor(0) legend.SetTextFont(62) legend.SetHeader(header) legend.SetTextFont(42) legend.SetMargin(0.2) latex = TLatex() latex.SetTextAlign(13) latex.SetTextFont(42) latex.SetNDC(True) frame = hists[0] frame.GetXaxis().SetTitle(xtitle) frame.GetYaxis().SetTitle("Fraction [%]") frame.GetXaxis().SetLabelSize(0.074) frame.GetYaxis().SetLabelSize(0.046) frame.GetXaxis().SetTitleSize(0.048) frame.GetYaxis().SetTitleSize(0.052) frame.GetXaxis().SetTitleOffset(1.38) frame.GetYaxis().SetTitleOffset(1.12) frame.GetXaxis().SetLabelOffset(0.009) frame.SetMaximum(1.25*max(h.GetMaximum() for h in hists)) if logy: canvas.SetLogy() frame.SetMinimum(1e-3) else: frame.SetMinimum(0) for i, hist in enumerate(hists): hist.Draw('HISTE0E1SAME') hist.SetLineWidth(2) hist.SetLineColor(colors[i%len(colors)]) legend.AddEntry(hist,hist.GetTitle(),'le') legend.Draw() for i, text in enumerate(ctexts): textsize = 0.024 #if i>0 else 0.044 latex.SetTextSize(textsize) latex.DrawLatex(0.14,0.98-canvas.GetTopMargin()-1.7*i*textsize,text) if otext: latex.SetTextSize(0.05) latex.SetTextAlign(31) latex.DrawLatex(1.-canvas.GetRightMargin(),1.-0.84*canvas.GetTopMargin(),otext) canvas.SaveAs(plotname+".png") canvas.SaveAs(plotname+".pdf") canvas.Close() for hist in hists: gDirectory.Delete(hist.GetName())
def normalizeSignal(self, S_exp, **kwargs): """Calculates normalization for a given expected signal yield.""" if not self.isSignal: print warning("normalizeSignal: Not a signal sample!") verbosity = kwargs.get('verbosity', 0) var = kwargs.get('var', "m_sv") cuts = [ ("%s && %s" % (baseline, category1)), ("%s && %s" % (baseline, category2)), ] cuts = kwargs.get('cuts', cuts) if not isinstance(cuts, list) and not isinstance(cuts, tuple): cuts = [cuts] (aa, bb) = kwargs.get('signalregion', (0, 40)) weight = kwargs.get('weight', "") channel = kwargs.get('channel', "mutau") #treeName = kwargs.get('treeName',"tree_%s"%channel) setScale = kwargs.get('setScale', True) N = 0 MC = 0 scale = 1 for i, cut in enumerate(cuts): #cut = combineCuts("%s<%s && %s<%s"%(aa,var,var,bb), cut) # remove over and underflow name = "m_sv_for_signal_renormalization_%d" % i hist = self.hist("m_sv", 100, aa, bb, name=name, cuts=cut, weight=weight, verbosity=verbosity) N += hist.GetSumOfWeights() MC += hist.GetEntries() gDirectory.Delete(name) printVerbose(">>> normalizeSignal: N=%s, MC=%s" % (N, MC), verbosity) if N: scale = S_exp / N * self.scale printVerbose( ">>> normalizeSignal: S_exp=%.4f, N=%.4f, MC=%.1f, old scale=%.4f, scale=%.4f" % (S_exp, N, MC, self.scale, scale), verbosity) #printVerbose(">>> normalizeSignal: signalregion=(%.1f,%.1f)" % (aa,bb),verbosity) else: print warning( "Could not find normalization for signal: no MC events (N=%s,MC=%s) in given signal region after cuts (%s)!" % (N, MC, cuts)) if setScale: self.setAllScales(scale) return scale
def calculateLumiAcceptance(self,cuts,**kwargs): """Calculates scale for a given expected signal yield, to divide out the luminosity and acceptance. This method only returns the scale, it does not rescale the signal.""" verbosity = kwargs.get('verbosity',0) weight = kwargs.get('weight',"") (a,b) = kwargs.get('range',(0,500)) scale = 1 N_tot = self.N name = "m_sv_for_LumiAcceptance" hist = self.hist("m_sv",100,a,b,name=name,cuts=cuts,weight=weight) (N,MC) = (hist.GetSumOfWeights(),hist.GetEntries()) gDirectory.Delete(name) if N_tot and N and lumi: scale = N_tot/(N*lumi) printVerbose(">>> normalizeSignal: N_tot=%.4f, N=%.4f, MC=%.1f, lumi=%s, current scale=%.4f, scale=%.4f" % (N_tot, N, MC, lumi, self.scale, scale), verbosity) printVerbose(">>> normalizeSignal: range=(%.1f,%.1f)" % (a,b),verbosity) else: print warning("Could not find normalization for signal: N_tot=%s, N=%s, lumi=%s!" % (N_tot,N,lumi)) return scale
def deletehist(*hists,**kwargs): """Completely remove histograms from memory.""" verbosity = LOG.getverbosity(kwargs) hists = unwraplistargs(hists) for hist in hists: hclass = hist.__class__.__name__ hname = hist.GetName() if hasattr(hist,'GetName') else None LOG.verb("deletehist: deleting %s %r"%(hclass,hname or hist),verbosity,3) #try: if hist: if hasattr(hist,'GetDirectory') and hist.GetDirectory()==None: hist.Delete() elif hname: gDirectory.Delete(hist.GetName()) else: LOG.warning("deletehist: %s %s has no name!"%(hclass,hist)) else: LOG.warning("deletehist: %s is already %s"%(hclass,hist)) #except AttributeError: # print ">>> AttributeError: " # raise AttributeError del hist
# QCD in SR = TF * (data - nonQCDBkg from CR) #---------------------------------------- qcdTFDirInFile = "%s/TF"%histDirInFile qcdShapeDirInFile = "%s/Shape"%histDirInFile transferFactor = 1.0 if sample =="QCD_DD" and "Semi" in ttbarDecayMode: toPrint("Determining QCD Transfer factor from CR", "") #transferFactor = getQCDTransFact(year, channel, nBJets, outputFile, qcdTFDirInFile) print "Transfer factor = ", transferFactor for hist in histogramsToMake: if not histogramInfo[hist][5]: continue toPrint("Determining QCD shape from CR", "") dataMinusOtherBkg = getShapeFromCR(year, channel, nJetSel, nBJets, histogramInfo[hist], outputFile, qcdShapeDirInFile) histograms.append(dataMinusOtherBkg) print histogramInfo[hist][1] histograms[-1].Scale(transferFactor) #----------------------------------- # Write final histograms in the file #----------------------------------- if not outputFile.GetDirectory(histDirInFile): outputFile.mkdir(histDirInFile) outputFile.cd(histDirInFile) for h in histograms: toPrint("Integral of Histogram %s = "%h.GetName(), h.Integral()) outputFile.cd(histDirInFile) gDirectory.Delete("%s;*"%(h.GetName())) h.Write() toPrint("Path of output root file", outFileFullPath) outputFile.Close()
if out > 0: hist.Scale(1. / hist.Integral()) ###hist.Draw('HISTE') ###canvas.SaveAs(histname+".png") ###canvas.SaveAs(histname+".pdf") ###canvas.Close() hists.append(hist) gStyle.SetOptTitle(False) canvas = TCanvas('canvas', 'canvas', 100, 100, 800, 600) canvas.SetTopMargin(0.03) legend = TLegend(0.7, 0.7, 0.85, 0.5) legend.SetTextSize(0.04) legend.SetBorderSize(0) legend.SetFillStyle(0) legend.SetFillColor(0) legend.SetTextFont(62) legend.SetHeader(trigger) legend.SetTextFont(42) plotname = "%s_nTau_%s_comparison" % (trigger, filter) hists[0].SetMaximum(1.18 * max(h.GetMaximum() for h in hists)) for hist in hists: hist.Draw('HISTSAME') legend.AddEntry(hist, hist.GetTitle(), 'l') legend.Draw() canvas.SaveAs(plotname + ".png") canvas.SaveAs(plotname + ".pdf") canvas.Close() for hist in hists: gDirectory.Delete(hist.GetName()) file.Close()
def main(): usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option( '-s', '--suffix', dest='input_suffix', help='suffix used to identify inputs from network training', default=None, type='string') parser.add_option('-j', '--json', dest='json', help='json file with list of variables', default=None, type='string') parser.add_option('-i', '--input', dest='input_file', help='input file', default=None, type='string') (opt, args) = parser.parse_args() jsonFile = open(opt.json, 'r') if opt.json == None: print 'input variable .json not defined!' sys.exit(1) if opt.input_suffix == None: print 'Input files suffix not defined!' sys.exit(1) if opt.input_file == None: print 'Input file not defined!' sys.exit(1) new_variable_list = json.load(jsonFile, encoding="utf-8").items() n_input_vars = 0 for key, value in new_variable_list: n_input_vars = n_input_vars + 1 input_file = opt.input_file classifier_suffix = opt.input_suffix classifier_parent_dir = '/afs/cern.ch/work/j/jthomasw/private/IHEP/ttHML/github/ttH_multilepton/DNN/Evaluation/V7-DNN_%s' % ( classifier_suffix) # Setup TMVA TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() reader = TMVA.Reader("Color:!Silent") # Check files exist if not isfile(input_file): print 'No such input file: %s' % input_file # Open files and load ttrees data_file = TFile.Open(input_file) data_tree = data_file.Get("syncTree") branches_tree = {} integer_branches_tree = {} for key, value in new_variable_list: if 'hadTop_BDT' in key: branches_tree[key] = array('f', [-999]) keyname = 'hadTop_BDT' data_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif 'Hj1_BDT' in key: branches_tree[key] = array('f', [-999]) keyname = 'Hj1_BDT' data_tree.SetBranchAddress(str(keyname), branches_tree[key]) elif ('n_fakeablesel_mu' in key) or ('n_fakeablesel_ele' in key) or ('Jet_numLoose' in key): integer_branches_tree[key] = array('I', [9999]) data_tree.SetBranchAddress(str(key), integer_branches_tree[key]) else: branches_tree[key] = array('f', [-999]) data_tree.SetBranchAddress(str(key), branches_tree[key]) branches_reader = {} # Register names of inputs with reader. Together with the name give the address of the local variable that carries the updated input variables during event loop. for key, value in new_variable_list: print 'Add variable name %s: ' % key branches_reader[key] = array('f', [-999]) reader.AddVariable(str(key), branches_reader[key]) event_number = array('f', [-999]) reader.AddSpectator('nEvent', event_number) # Book methods # First argument is user defined name. Doesn not have to be same as training name. # True type of method and full configuration are read from the weights file specified in the second argument. mva_weights_dir = '%s/weights/Factory_V7-DNN_%s_DNN.weights.xml' % ( classifier_parent_dir, classifier_suffix) print 'using weights file: ', mva_weights_dir reader.BookMVA('DNN', TString(mva_weights_dir)) if '/2L/' in input_file: analysis_region = '2L' elif '/ttWctrl/' in input_file: analysis_region = 'ttWctrl' elif '/JESDownttWctrl/' in input_file: analysis_region = 'JESDownttWctrl' elif '/JESUpttWctrl/' in input_file: analysis_region = 'JESUpttWctrl' elif '/ClosTTWctrl/' in input_file: analysis_region = 'ClosTTWctrl' elif '/ttZctrl/' in input_file: analysis_region = 'ttZctrl' elif '/Clos2LSS/' in input_file: analysis_region = 'Closure' elif '/JESDown2L/' in input_file: analysis_region = 'JESDown2L' elif '/JESUp2L/' in input_file: analysis_region = 'JESUp2L' time_suffix = str(datetime.now(pytz.utc)).split(' ') print time_suffix[0] #classifier_samples_dir = classifier_parent_dir+"/outputs" classifier_samples_dir = classifier_parent_dir + "/outputs-newbinning" #classifier_plots_dir = classifier_parent_dir+"/plots" classifier_plots_dir = classifier_parent_dir + "/plots-newbinning" if not os.path.exists(classifier_plots_dir): os.makedirs(classifier_plots_dir) if not os.path.exists(classifier_samples_dir): os.makedirs(classifier_samples_dir) analysis_region_samples_dir = '%s/%s' % (classifier_samples_dir, analysis_region) analysis_region_plots_dir = '%s/%s' % (classifier_plots_dir, analysis_region) if not os.path.exists(analysis_region_plots_dir): os.makedirs(analysis_region_plots_dir) if not os.path.exists(analysis_region_samples_dir): os.makedirs(analysis_region_samples_dir) output_suffix = input_file[input_file.rindex('/') + 1:] print 'output_suffix: ', output_suffix # Define outputs: files to store histograms/ttree with results from application of classifiers and any histos/trees themselves. output_file_name = '%s/Evaluated_%s_%s' % ( analysis_region_samples_dir, classifier_suffix, output_suffix) output_file = TFile.Open(output_file_name, 'RECREATE') output_tree = data_tree.CopyTree("") output_tree.SetName("output_tree") nEvents_check = output_tree.BuildIndex("nEvent", "run") print 'Copied %s events from original tree' % (nEvents_check) sample_nickname = '' if 'THQ_htt_2L' in input_file: sample_nickname = 'THQ_htt_2L' if 'THQ_hzz_2L' in input_file: sample_nickname = 'THQ_hzz_2L' if 'THW_hww_2L' in input_file: sample_nickname = 'THW_hww_2L' if 'TTH_hmm_2L' in input_file: sample_nickname = 'TTH_hmm_2L' if 'TTH_htt_2L' in input_file: sample_nickname = 'TTH_htt_2L' if 'TTH_hzz_2L' in input_file: sample_nickname = 'TTH_hzz_2L' if 'THQ_hww_2L' in input_file: sample_nickname = 'THQ_hww_2L' if 'THW_htt_2L' in input_file: sample_nickname = 'THW_htt_2L' if 'THW_hzz_2L' in input_file: sample_nickname = 'THW_hzz_2L' if 'TTH_hot_2L' in input_file: sample_nickname = 'TTH_hot_2L' if 'TTH_hww_2L' in input_file: sample_nickname = 'TTH_hww_2L' if 'TTWW_2L' in input_file: sample_nickname = 'TTWW_2L' if 'TTW_2L' in input_file: sample_nickname = 'TTW_2L' if 'TTZ_2L' in input_file: sample_nickname = 'TTZ_2L' if 'Conv_2L' in input_file: sample_nickname = 'Conv_2L' if 'EWK_2L' in input_file: sample_nickname = 'EWK_2L' if 'Fakes_2L' in input_file: sample_nickname = 'Fakes_2L' if 'Flips_2L' in input_file: sample_nickname = 'Flips_2L' if 'Rares_2L' in input_file: sample_nickname = 'Rares_2L' if 'TT_Clos' in input_file: sample_nickname = 'TT_Clos' if 'Data' in input_file: sample_nickname = 'Data' # Evaluate network and use max node response to categorise event. Only maximum node response will be plotted per event meaning each event will only contribute in the maximum nodes response histogram. network_evaluation(data_tree, new_variable_list, sample_nickname, branches_tree, integer_branches_tree, branches_reader, reader, True, output_tree) output_file.Write() gDirectory.Delete("syncTree;*") output_file.Close() print 'Job complete. Exiting.' sys.exit(0)
data_obs = dataHist.Clone("data_obs") rebinnedData = data_obs.Rebin(len(binning) - 1, "", binning) ## input to combine , open root file if template: myfile = TFile("%s%s.root" % (plotDirectory, myfilename), "update") # i have to get the nominal histogram from root file first and get the integration value if systematics == '': myDatahist = rebinnedData.Clone("nominal") mydataDir = "%s/data_obs/" % channel if myfile.GetDirectory(mydataDir): gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() else: gDirectory.mkdir(mydataDir) gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() # create directory only if it does not exist ### ele channel for iprocess in template_category.keys(): myfile.cd() mydir = "%s/%s/" % (channel, iprocess) #print "%s/%s/"%(channel,iprocess) if systematics == '':
def TreeLoopFromFile(self, fname, noCuts=False, cutOverride=None, CPweight=False, interference=0): # open file and get tree treeFile = TFile.Open(fname) theTree = treeFile.Get(self.pars.treeName) if not theTree: print 'failed to find tree %s in file %s' % (self.pars.treeName, fname) return # get the right cuts if cutOverride: theCuts = self.fullCuts(cutOverride) print 'override cuts:', theCuts elif noCuts: theCuts = '' else: theCuts = self.fullCuts() if gDirectory.Get('cuts_evtList'): gDirectory.Delete('cuts_evtList') theList = None # create fomulae for the variables of interest rowVs = [] for (i, v) in enumerate(self.pars.var): rowVs.append(TTreeFormula('v%i' % i, v, theTree)) extraDraw = '' varsRemaining = 4 - len(self.pars.var) ExtraDrawCP = False ExtraDrawInterf = False if CPweight: if hasattr(theTree, 'complexpolewtggH%i' % self.pars.mHiggs): extraDraw += ':(complexpolewtggH%i/avecomplexpolewtggH%i)' % \ (self.pars.mHiggs, self.pars.mHiggs) varsRemaining -= 1 ExtraDrawCP = True if interference == 1: extraDraw += ':interferencewtggH%i' % self.pars.mHiggs varsRemaining -= 1 ExtraDrawInterf = True elif interference == 2: extraDraw += ':interferencewt_upggH%i' % self.pars.mHiggs varsRemaining -= 1 ExtraDrawInterf = True elif interference == 3: extraDraw += ':interferencewt_downggH%i' % self.pars.mHiggs varsRemaining -= 1 ExtraDrawInterf = True if varsRemaining >= 0: if len(theCuts) > 0: theCuts = 'puwt*effwt*' + theCuts # print ':'.join(self.pars.var) + extraDraw # print 'weighted cuts:',theCuts Nsel = theTree.Draw(':'.join(self.pars.var) + extraDraw, theCuts, 'goff') else: # create an entry list which apply the cuts to the tree Nsel = theTree.Draw('>>cuts_evtList', theCuts, 'entrylist') theList = gDirectory.Get('cuts_evtList') # loop over the selected events calculate their weight and yield # the two variable values and the weight for each selected event. print "selected events:", Nsel if theList: while theTree.GetEntry(theList.Next()): # if self.pars.isElectron: # lep_pt = theTree.W_electron_pt # lep_eta = theTree.W_electron_eta # else: # lep_pt = theTree.W_muon_pt # lep_eta = theTree.W_muon_eta # jet_pt = [] # jet_eta = [] # for (idx, pt) in enumerate(theTree.JetPFCor_Pt): # if pt > 0: # jet_pt.append(pt) # jet_eta.append(theTree.JetPFCor_Eta[idx]) # effWgt = self.effWeight(lepton_pt = lep_pt, lepton_eta = lep_eta, # #jet_pt = jet_pt, jet_eta, # mt_pt = theTree.W_mt, mt_eta = lep_eta, # met_pt = theTree.event_met_pfmet, # met_eta = 0.) # if (hasattr(self.pars, 'btagVeto')) and (self.pars.btagVeto) and \ # self.btagVeto(theTree): # continue effWgt = theTree.puwt * theTree.effwt if CPweight: if hasattr(theTree, 'complexpolewtggH%i' % self.pars.mHiggs): cpw = getattr(theTree, 'complexpolewtggH%i' % self.pars.mHiggs) cpw /= getattr( theTree, 'avecomplexpolewtggH%i' % self.pars.mHiggs) else: cpw = HiggsCPWeight(self.pars.mHiggs, theTree.W_H_mass_gen) else: cpw = 1. if interference == 1: iwt = getattr(theTree, 'interferencewtggH%i' % self.pars.mHiggs) elif interference == 2: iwt = getattr(theTree, 'interferencewt_upggH%i' % self.pars.mHiggs) elif interference == 3: iwt = getattr( theTree, 'interferencewt_downggH%i' % self.pars.mHiggs) else: iwt = 1. row = [v.EvalInstance() for v in rowVs] yield (row, effWgt, cpw, iwt) else: for rowi in range(0, theTree.GetSelectedRows()): effWgt = theTree.GetW()[rowi] row = [] for vi in range(0, len(self.pars.var)): row.append(getattr(theTree, 'GetV%i' % (vi + 1))()[rowi]) cpw = 1. vi = len(self.pars.var) if ExtraDrawCP: cpw = getattr(theTree, 'GetV%i' % (vi + 1))()[rowi] vi += 1 iwt = 1. if ExtraDrawInterf: iwt = getattr(theTree, 'GetV%i' % (vi + 1))()[rowi] vi += 1 yield (row, effWgt, cpw, iwt) treeFile.Close() return
def plotMatches(tree, basebranch, trigger, WPs, plotname, header, ctexts): gStyle.SetOptTitle(True) hists = [] for i, wp in enumerate(WPs, 1): ###canvas = TCanvas('canvas','canvas',100,100,800,600) branch = basebranch + ("" if 'all' in wp else '_' + wp) histname = "%s_%s" % (trigger, branch) histtitle = "all (slimmed)" if wp == 'all' else wp #"%s, %s"%(trigger,wp) hist = TH1F(histname, histtitle, 8, -2, 6) hist.GetXaxis().SetTitle(branch) hist.GetYaxis().SetTitle("Fraction") for ibin in xrange(1, hist.GetXaxis().GetNbins() + 1): xbin = hist.GetBinLowEdge(ibin) if xbin == -2: hist.GetXaxis().SetBinLabel(ibin, "HLT not fired") elif xbin == -1: hist.GetXaxis().SetBinLabel(ibin, "No trig. obj.") elif xbin == 0: hist.GetXaxis().SetBinLabel(ibin, "No match") elif xbin == 1: hist.GetXaxis().SetBinLabel(ibin, "1 match") else: hist.GetXaxis().SetBinLabel(ibin, "%d matches" % xbin) hist.GetXaxis().SetLabelSize(0.074) hist.GetYaxis().SetLabelSize(0.046) hist.GetXaxis().SetTitleSize(0.046) hist.GetYaxis().SetTitleSize(0.052) hist.GetXaxis().SetTitleOffset(2.14) hist.GetYaxis().SetTitleOffset(0.98) hist.GetXaxis().SetLabelOffset(0.009) if len(branch) > 60: hist.GetXaxis().CenterTitle(True) hist.GetXaxis().SetTitleOffset(2.65) hist.GetXaxis().SetTitleSize(0.038) elif len(branch) > 40: hist.GetXaxis().CenterTitle(True) hist.GetXaxis().SetTitleOffset(2.16) hist.GetXaxis().SetTitleSize(0.044) hist.SetLineWidth(2) hist.SetLineColor(i) out = tree.Draw("%s >> %s" % (branch, histname), "trigger_%s" % trigger, 'gOff') if hist.Integral() > 0: hist.Scale(1. / hist.Integral()) else: print "Warning! Histogram '%s' is empty!" % hist.GetName() ###hist.Draw('HISTE') ###canvas.SaveAs(histname+".png") ###canvas.SaveAs(histname+".pdf") ###canvas.Close() hists.append(hist) gStyle.SetOptTitle(False) canvas = TCanvas('canvas', 'canvas', 100, 100, 800, 600) canvas.SetMargin(0.10, 0.09, 0.18, 0.03) textsize = 0.040 height = 1.28 * (len(hists) + 1) * textsize legend = TLegend(0.63, 0.70, 0.88, 0.70 - height) legend.SetTextSize(textsize) legend.SetBorderSize(0) legend.SetFillStyle(0) legend.SetFillColor(0) legend.SetTextFont(62) legend.SetHeader(header) legend.SetTextFont(42) legend.SetMargin(0.2) latex = TLatex() latex.SetTextAlign(13) latex.SetTextFont(42) latex.SetNDC(True) hists[0].SetMaximum(1.25 * max(h.GetMaximum() for h in hists)) for hist in hists: hist.Draw('HISTSAME') legend.AddEntry(hist, hist.GetTitle().capitalize(), 'l') legend.Draw() for i, text in enumerate(ctexts): textsize = 0.031 if i > 0 else 0.044 latex.SetTextSize(textsize) latex.DrawLatex(0.14, 0.95 - 1.7 * i * textsize, text) canvas.SaveAs(plotname + ".png") canvas.SaveAs(plotname + ".pdf") canvas.Close() for hist in hists: gDirectory.Delete(hist.GetName())
from ROOT import TFile, TTree, TH1F, gDirectory def printGDirectory(): print ">>> gDirectory.GetName()\n%s" % gDirectory.GetName() print ">>> gDirectory.pwd()" gDirectory.pwd() print ">>> gDirectory.ls()" gDirectory.ls() print print "\ndefault" printGDirectory() print ">>> creating a file with some contents" file = TFile("test.root","recreate") tree = TTree("tree","tree") hist = TH1F("hist","hist",100,0,100) dir1 = file.mkdir("dir1") printGDirectory() print ">>> gDirectory.Delete(\"hist\")" gDirectory.Delete("hist") printGDirectory() print ">>> dir1.cd()" dir1.cd() printGDirectory() print ">>> file.Close()" file.Close() printGDirectory()