Esempio n. 1
0
def main(args, opt):
    trees = {}
    for filename in os.listdir(args[0]):
        if not os.path.splitext(filename)[1] == '.root': continue
        isdata, pname, splitno = resolveFilename(filename)
        treefile = os.path.join(args[0], filename)
        tfile = ROOT.TFile.Open(treefile, 'READ')
        trees[pname] = tfile.Get(TREENAME)

    try:
        cachefile = open('genmlbhists.pck', 'r')
        hists = pickle.load(cachefile)
        print '>>> Read syst histos from cache (.svlsysthistos.pck)'
        cachefile.close()
    except IOError:
        hists = {}

        for tag, sel in MLBSELECTIONS:
            for pname in trees.keys():
                print '... processing', pname, tag
                hists[(pname, tag)] = getHistoFromTree(trees[pname],
                                                       sel=sel,
                                                       var='GenMlb',
                                                       hname="GenMlb_%s_%s" %
                                                       (pname, tag),
                                                       nbins=100,
                                                       xmin=0,
                                                       xmax=200,
                                                       titlex=MLBAXISTITLE)

        cachefile = open('genmlbhists.pck', 'w')
        pickle.dump(hists, cachefile, pickle.HIGHEST_PROTOCOL)
        print '>>> Dumped histos to cachefile (genmlbhists.pck)'
        cachefile.close()

    ROOT.gStyle.SetOptTitle(0)
    ROOT.gStyle.SetOptStat(0)
    ROOT.gROOT.SetBatch(1)

    for tag, _ in MLBSELECTIONS:
        plot = RatioPlot('genmlb')
        plot.normalized = False
        plot.add(hists[('TTJets_MSDecays_172v5', tag)], 'Nominal')
        plot.add(hists[('TTJets_MSDecays_scaleup', tag)], 'Q^{2} scale up')
        plot.add(hists[('TTJets_MSDecays_scaledown', tag)], 'Q^{2} scale down')
        plot.tag = "Generator level m_{lb} shape"
        if tag == 'cor':
            plot.subtag = "Correct combinations"
        else:
            plot.subtag = "Wrong combinations"
        plot.ratiotitle = 'Ratio wrt nominal'
        plot.titlex = MLBAXISTITLE
        plot.tagpos = (0.22, 0.85)
        plot.subtagpos = (0.22, 0.78)
        plot.legpos = (0.20, 0.55)
        plot.ratiorange = (0.85, 1.15)
        plot.colors = [ROOT.kBlue - 3, ROOT.kRed - 4, ROOT.kOrange - 3]
        plot.show("genmlb_scale_%s" % tag, opt.outDir)

    return 0
Esempio n. 2
0
def main(args, opt):
	trees = {}
	for filename in os.listdir(args[0]):
		if not os.path.splitext(filename)[1] == '.root': continue
		isdata, pname, splitno = resolveFilename(filename)
		treefile = os.path.join(args[0], filename)
		tfile = ROOT.TFile.Open(treefile,'READ')
		trees[pname] = tfile.Get(TREENAME)


	try:
		cachefile = open('genmlbhists.pck', 'r')
		hists = pickle.load(cachefile)
		print '>>> Read syst histos from cache (.svlsysthistos.pck)'
		cachefile.close()
	except IOError:
		hists = {}

		for tag,sel in MLBSELECTIONS:
			for pname in trees.keys():
				print '... processing', pname, tag
				hists[(pname,tag)] = getHistoFromTree(trees[pname],
				                                sel=sel,
				                                var='GenMlb',
			                                    hname="GenMlb_%s_%s"%(pname,tag),
		                                        nbins=100,xmin=0,xmax=200,
		                                        titlex=MLBAXISTITLE)

		cachefile = open('genmlbhists.pck', 'w')
		pickle.dump(hists, cachefile, pickle.HIGHEST_PROTOCOL)
		print '>>> Dumped histos to cachefile (genmlbhists.pck)'
		cachefile.close()


	ROOT.gStyle.SetOptTitle(0)
	ROOT.gStyle.SetOptStat(0)
	ROOT.gROOT.SetBatch(1)


	for tag,_ in MLBSELECTIONS:
		plot = RatioPlot('genmlb')
		plot.normalized = False
		plot.add(hists[('TTJets_MSDecays_172v5', tag)],     'Nominal')
		plot.add(hists[('TTJets_MSDecays_scaleup', tag)],   'Q^{2} scale up')
		plot.add(hists[('TTJets_MSDecays_scaledown', tag)], 'Q^{2} scale down')
		plot.tag = "Generator level m_{lb} shape"
		if tag == 'cor':
			plot.subtag = "Correct combinations"
		else:
			plot.subtag = "Wrong combinations"
		plot.ratiotitle = 'Ratio wrt nominal'
		plot.titlex = MLBAXISTITLE
		plot.tagpos    = (0.22, 0.85)
		plot.subtagpos = (0.22, 0.78)
		plot.legpos = (0.20, 0.55)
		plot.ratiorange = (0.85, 1.15)
		plot.colors = [ROOT.kBlue-3, ROOT.kRed-4, ROOT.kOrange-3]
		plot.show("genmlb_scale_%s"%tag, opt.outDir)

	return 0
Esempio n. 3
0
def makeHistos((url, name)):
	print "... processing", url
	tree = ROOT.TFile.Open(url,'READ').Get(TREENAME)
	histos = {}
	# selection = "SVMassWeight*(SVLCombRank>0 && %s)"%INCSEL
	selection = "(SVLCombRank>0 && %s)"%INCSEL
	# selection = "(SVLCombRank>0 && %s)"%EMSEL
	for varname, nbins, xmin, xmax, titlex in VARS:
		for ntk1,ntk2 in NTRKBINS:
			tksel = "(SVNtrk>=%d && SVNtrk<%d)"%(ntk1,ntk2)
			finalsel = "%s*%s" %(selection,tksel)
			histos[(varname, ntk1)] = getHistoFromTree(
				                              tree, sel=finalsel, var=varname,
			                                  hname='%s_%s_%d'%(name, varname, ntk1),
			                                  nbins=nbins, xmin=xmin, xmax=xmax,
			                                  titlex=titlex)
	return name,histos
Esempio n. 4
0
def makeHistos((url, name)):
    print "... processing", url
    tree = ROOT.TFile.Open(url, 'READ').Get(TREENAME)
    histos = {}
    # selection = "SVMassWeight*(SVLCombRank>0 && %s)"%INCSEL
    selection = "(SVLCombRank>0 && %s)" % INCSEL
    # selection = "(SVLCombRank>0 && %s)"%EMSEL
    for varname, nbins, xmin, xmax, titlex in VARS:
        for ntk1, ntk2 in NTRKBINS:
            tksel = "(SVNtrk>=%d && SVNtrk<%d)" % (ntk1, ntk2)
            finalsel = "%s*%s" % (selection, tksel)
            histos[(varname, ntk1)] = getHistoFromTree(tree,
                                                       sel=finalsel,
                                                       var=varname,
                                                       hname='%s_%s_%d' %
                                                       (name, varname, ntk1),
                                                       nbins=nbins,
                                                       xmin=xmin,
                                                       xmax=xmax,
                                                       titlex=titlex)
    return name, histos
Esempio n. 5
0
def main(args, options):
	os.system('mkdir -p %s'%options.outDir)
	try:
		treefiles = {} # procname -> filename
		for filename in os.listdir(args[0]):
			if not os.path.splitext(filename)[1] == '.root': continue
			procname = filename.split('_',1)[1][:-5]
			treefiles[procname] = os.path.join(args[0],filename)

	except OSError:
		print "Not a valid input directory: %s" % args[0]
		return -1

	except IndexError:
		print "Need to provide an input directory"
		return -1

	## Collect all the trees
	svltrees = {} # proc -> tree
	for proc in treefiles.keys():
		tfile = ROOT.TFile.Open(treefiles[proc], 'READ')
		if not filterUseless(treefiles[proc]): continue
		svltrees[proc] = tfile.Get(TREENAME)

	## Produce all the relevant histograms
	if not options.cached:
		masshistos = {}     # (selection tag, process) -> histo
		methistos  = {}     # (selection tag, process) -> histo
		fittertkhistos = {} # (selection tag, process) -> [h_ntk1, h_ntk2, ...]
		for tag,sel,_ in SELECTIONS:
			for proc, tree in svltrees.iteritems():
				if not filterUseless(treefiles[proc], sel): continue

				htag = ("%s_%s"%(tag, proc)).replace('.','')
				print ' ... processing %-30s %s htag=%s' % (proc, sel, htag)
				masshistos[(tag, proc)] = getHistoFromTree(tree, sel=sel,
					                               var='SVLMass',
		                                           hname="SVLMass_%s"%(htag),
		                                           titlex=MASSXAXISTITLE)

				methistos[(tag, proc)] =  getHistoFromTree(tree, sel=sel,
					                               var='MET',
		                                           hname="MET_%s"%(htag),
		                                           xmin=0,xmax=200,
		                                           titlex="Missing E_{T} [GeV]")

				fittertkhistos[(tag,proc)] = getNTrkHistos(tree, sel=sel,
									   tag=htag,
									   var='SVLMass',
									   titlex=MASSXAXISTITLE)

		cachefile = open(".svlqcdmasshistos.pck", 'w')
		pickle.dump(masshistos,     cachefile, pickle.HIGHEST_PROTOCOL)
		pickle.dump(methistos,      cachefile, pickle.HIGHEST_PROTOCOL)
		pickle.dump(fittertkhistos, cachefile, pickle.HIGHEST_PROTOCOL)
		cachefile.close()
	else:
		cachefile = open(".svlqcdmasshistos.pck", 'r')
		masshistos     = pickle.load(cachefile)
		methistos      = pickle.load(cachefile)
		fittertkhistos = pickle.load(cachefile)
		cachefile.close()


	#########################################################
	## Write out the histograms, make data/mc plots
	outputFileName = os.path.join(options.outDir,'qcd_DataMCHists.root')
	ofi = ROOT.TFile(outputFileName, 'recreate')
	ofi.cd()
	for hist in [h for h in masshistos.values() + methistos.values()]:
		hist.Write(hist.GetName())
	for hist in [h for hists in fittertkhistos.values() for h in hists]:
		hist.Write(hist.GetName())
	ofi.Write()
	ofi.Close()

	## Run the plotter to get scaled MET plots
	## Can then use those to subtract non-QCD backgrounds from data template
	## Overwrite some of the options
	options.filter = 'SVLMass,MET' ## only run SVLMass and MET plots
	# options.excludeProcesses = 'QCD'
	options.outFile = 'scaled_met_inputs.root'
	options.cutUnderOverFlow = True
	os.system('rm %s' % os.path.join(options.outDir, options.outFile))
	runPlotter(outputFileName, options)

	#########################################################
	## Build the actual templates from the single histograms
	templates = {} # selection tag -> template histo
	inputfile = openTFile(os.path.join(options.outDir, options.outFile))

	for tag,sel,_ in SELECTIONS:
		categories = ['MET', 'SVLMass']
		for x,_ in NTRKBINS:
			categories.append('SVLMass_tot_%d'%int(x))

		for category in categories:
			plotdirname = '%s_%s'%(category,tag)
			plotdir = inputfile.Get(plotdirname)
			h_bg = None
			h_data = None
			for tkey in plotdir.GetListOfKeys():
				key = tkey.GetName()
				if key.startswith('Graph_from'): continue
				if key.startswith('MC8TeV_QCD'): continue

				hist = inputfile.Get('%s/%s'%(plotdirname,key))
				if key.startswith('MC8TeV'):
					if not h_bg:
						h_bg = hist.Clone("%s_BGs" % tag)
					else:
						h_bg.Add(hist)

				if key.startswith('Data8TeV'):
					h_data = hist.Clone("%s_Data" % tag)

			## Determine a suitable output name
			histname = '%s_template'%tag
			if category == 'MET': histname = "%s_%s" % ('met',histname)
			if 'tot' in category:
				tkbin = int(category.split('_')[2])
				histname = "%s_%d" % (histname, tkbin)
			h_data_subtr = h_data.Clone(histname)

			## Now subtract the combined MC from the data
			h_data_subtr.Add(h_bg, -1)

			dicttag = tag
			if category == 'MET':
				dicttag = 'met_%s'%tag
			if '_tot_' in category:
				dicttag = '%s_%d' % (tag, tkbin)
			templates[dicttag] = h_data_subtr

	ofi = ROOT.TFile(os.path.join(options.outDir,'qcd_templates.root'),
		             'recreate')
	ofi.cd()
	for hist in templates.values(): hist.Write(hist.GetName())
	ofi.Write()
	ofi.Close()

	for key,hist in sorted(templates.iteritems()):
		print key


	#########################################################
	## Make a plot comparing the templates
	for tag,_,seltag in SELECTIONS:
		if not tag.endswith('_qcd'): continue
		templateplot = RatioPlot('qcdtemplates_%s'%tag)
		for key,hist in sorted(templates.iteritems()):
			if not tag in key: continue
			if key.startswith('met'): continue

			if key == tag:
				templateplot.add(hist, 'Inclusive')
				templateplot.reference = hist
			else:
				ntrkbin = int(key.rsplit('_',1)[1])
				templateplot.add(hist, 'N_{track} = %d'%ntrkbin)

		templateplot.tag = 'QCD templates'
		templateplot.subtag = seltag
		templateplot.tagpos    = (0.90, 0.85)
		templateplot.subtagpos = (0.90, 0.78)
		templateplot.legpos = (0.75, 0.25)
		templateplot.ratiotitle = 'Ratio wrt Inclusive'
		templateplot.extratext = '' #Work in Progress'
		templateplot.ratiorange = (0.2, 2.2)
		templateplot.colors = [ROOT.kBlack, ROOT.kBlue-8, ROOT.kAzure-2,
		                       ROOT.kCyan-3]
		templateplot.show("qcdtemplates_%s"%tag, options.outDir)
		templateplot.reset()


	return 0
Esempio n. 6
0
def main(args, options):
    os.system('mkdir -p %s' % options.outDir)
    try:
        treefiles = {}  # procname -> filename
        for filename in os.listdir(args[0]):
            if not os.path.splitext(filename)[1] == '.root': continue
            procname = filename.split('_', 1)[1][:-5]
            treefiles[procname] = os.path.join(args[0], filename)

    except OSError:
        print "Not a valid input directory: %s" % args[0]
        return -1

    except IndexError:
        print "Need to provide an input directory"
        return -1

    ## Collect all the trees
    svltrees = {}  # proc -> tree
    for proc in treefiles.keys():
        tfile = ROOT.TFile.Open(treefiles[proc], 'READ')
        if not filterUseless(treefiles[proc]): continue
        svltrees[proc] = tfile.Get(TREENAME)

    ## Produce all the relevant histograms
    if not options.cached:
        masshistos = {}  # (selection tag, process) -> histo
        methistos = {}  # (selection tag, process) -> histo
        fittertkhistos = {
        }  # (selection tag, process) -> [h_ntk1, h_ntk2, ...]
        for tag, sel, _ in SELECTIONS:
            for proc, tree in svltrees.iteritems():
                if not filterUseless(treefiles[proc], sel): continue

                htag = ("%s_%s" % (tag, proc)).replace('.', '')
                print ' ... processing %-30s %s htag=%s' % (proc, sel, htag)
                masshistos[(tag, proc)] = getHistoFromTree(
                    tree,
                    sel=sel,
                    var='SVLMass',
                    hname="SVLMass_%s" % (htag),
                    titlex=MASSXAXISTITLE)

                methistos[(tag, proc)] = getHistoFromTree(
                    tree,
                    sel=sel,
                    var='MET',
                    hname="MET_%s" % (htag),
                    xmin=0,
                    xmax=200,
                    titlex="Missing E_{T} [GeV]")

                fittertkhistos[(tag,
                                proc)] = getNTrkHistos(tree,
                                                       sel=sel,
                                                       tag=htag,
                                                       var='SVLMass',
                                                       titlex=MASSXAXISTITLE)

        cachefile = open(".svlqcdmasshistos.pck", 'w')
        pickle.dump(masshistos, cachefile, pickle.HIGHEST_PROTOCOL)
        pickle.dump(methistos, cachefile, pickle.HIGHEST_PROTOCOL)
        pickle.dump(fittertkhistos, cachefile, pickle.HIGHEST_PROTOCOL)
        cachefile.close()
    else:
        cachefile = open(".svlqcdmasshistos.pck", 'r')
        masshistos = pickle.load(cachefile)
        methistos = pickle.load(cachefile)
        fittertkhistos = pickle.load(cachefile)
        cachefile.close()

    #########################################################
    ## Write out the histograms, make data/mc plots
    outputFileName = os.path.join(options.outDir, 'qcd_DataMCHists.root')
    ofi = ROOT.TFile(outputFileName, 'recreate')
    ofi.cd()
    for hist in [h for h in masshistos.values() + methistos.values()]:
        hist.Write(hist.GetName())
    for hist in [h for hists in fittertkhistos.values() for h in hists]:
        hist.Write(hist.GetName())
    ofi.Write()
    ofi.Close()

    ## Run the plotter to get scaled MET plots
    ## Can then use those to subtract non-QCD backgrounds from data template
    ## Overwrite some of the options
    options.filter = 'SVLMass,MET'  ## only run SVLMass and MET plots
    # options.excludeProcesses = 'QCD'
    options.outFile = 'scaled_met_inputs.root'
    options.cutUnderOverFlow = True
    os.system('rm %s' % os.path.join(options.outDir, options.outFile))
    runPlotter(outputFileName, options)

    #########################################################
    ## Build the actual templates from the single histograms
    templates = {}  # selection tag -> template histo
    inputfile = openTFile(os.path.join(options.outDir, options.outFile))

    for tag, sel, _ in SELECTIONS:
        categories = ['MET', 'SVLMass']
        for x, _ in NTRKBINS:
            categories.append('SVLMass_tot_%d' % int(x))

        for category in categories:
            plotdirname = '%s_%s' % (category, tag)
            plotdir = inputfile.Get(plotdirname)
            h_bg = None
            h_data = None
            for tkey in plotdir.GetListOfKeys():
                key = tkey.GetName()
                if key.startswith('Graph_from'): continue
                if key.startswith('MC8TeV_QCD'): continue

                hist = inputfile.Get('%s/%s' % (plotdirname, key))
                if key.startswith('MC8TeV'):
                    if not h_bg:
                        h_bg = hist.Clone("%s_BGs" % tag)
                    else:
                        h_bg.Add(hist)

                if key.startswith('Data8TeV'):
                    h_data = hist.Clone("%s_Data" % tag)

            ## Determine a suitable output name
            histname = '%s_template' % tag
            if category == 'MET': histname = "%s_%s" % ('met', histname)
            if 'tot' in category:
                tkbin = int(category.split('_')[2])
                histname = "%s_%d" % (histname, tkbin)
            h_data_subtr = h_data.Clone(histname)

            ## Now subtract the combined MC from the data
            h_data_subtr.Add(h_bg, -1)

            dicttag = tag
            if category == 'MET':
                dicttag = 'met_%s' % tag
            if '_tot_' in category:
                dicttag = '%s_%d' % (tag, tkbin)
            templates[dicttag] = h_data_subtr

    ofi = ROOT.TFile(os.path.join(options.outDir, 'qcd_templates.root'),
                     'recreate')
    ofi.cd()
    for hist in templates.values():
        hist.Write(hist.GetName())
    ofi.Write()
    ofi.Close()

    for key, hist in sorted(templates.iteritems()):
        print key

    #########################################################
    ## Make a plot comparing the templates
    for tag, _, seltag in SELECTIONS:
        if not tag.endswith('_qcd'): continue
        templateplot = RatioPlot('qcdtemplates_%s' % tag)
        for key, hist in sorted(templates.iteritems()):
            if not tag in key: continue
            if key.startswith('met'): continue

            if key == tag:
                templateplot.add(hist, 'Inclusive')
                templateplot.reference = hist
            else:
                ntrkbin = int(key.rsplit('_', 1)[1])
                templateplot.add(hist, 'N_{track} = %d' % ntrkbin)

        templateplot.tag = 'QCD templates'
        templateplot.subtag = seltag
        templateplot.tagpos = (0.90, 0.85)
        templateplot.subtagpos = (0.90, 0.78)
        templateplot.legpos = (0.75, 0.25)
        templateplot.ratiotitle = 'Ratio wrt Inclusive'
        templateplot.extratext = ''  #Work in Progress'
        templateplot.ratiorange = (0.2, 2.2)
        templateplot.colors = [
            ROOT.kBlack, ROOT.kBlue - 8, ROOT.kAzure - 2, ROOT.kCyan - 3
        ]
        templateplot.show("qcdtemplates_%s" % tag, options.outDir)
        templateplot.reset()

    return 0