Exemplo n.º 1
0
 def Save(self):
     # Save
     #if not os.path.isdir(self.GetOutPath()):
     #  print 'Creating directory: ', self.GetOutPath()
     gSystem.mkdir(self.GetOutPath(), True)
     self.canvas.Print(self.GetOutName() + '.pdf', 'pdf')
     self.canvas.Print(self.GetOutName() + '.png', 'png')
def parse_drawer_options(options):
    # Create outdir if necessary
    if not options.outdir.endswith("/"):
        options.outdir += "/"
    if gSystem.AccessPathName(options.outdir):
        gSystem.mkdir(options.outdir)

    # Make input file list
    if not options.infile.endswith(".root") and not options.infile.endswith(
            ".txt"):
        raise ValueError("infile must be .root file or .txt file")

    if options.infile.endswith(".root"):
        # Create a temporary file with one line
        with tempfile.NamedTemporaryFile() as infile:
            infile.write(options.infile)
            infile.flush()
            options.tfilecoll = TFileCollection("fc", "", infile.name)
    else:
        options.tfilecoll = TFileCollection("fc", "", options.infile)

    # Batch mode
    if options.batch:
        gROOT.SetBatch(True)

    # Trigger tower parameter space
    if options.tower != 99:
        ieta = options.tower / 8
        iphi = options.tower % 8
        options.etamin = -2.2 + (4.4 / 6) * ieta
        options.etamax = -2.2 + (4.4 / 6) * (ieta + 1)
        if iphi < 4:
            options.phimin = (2 * pi / 8) * iphi
            options.phimax = (2 * pi / 8) * (iphi + 1)
        else:
            options.phimin = -2 * pi - pi + (2 * pi / 8) * iphi
            options.phimax = -2 * pi - pi + (2 * pi / 8) * (iphi + 1)
        options.ptmin = 2.
        options.ptmax = 2000.
        options.vzmin = -15.
        options.vzmax = +15.
Exemplo n.º 3
0
def DrawPlot(fname,
             hname,
             outname,
             outpath="./SFplots/",
             inpath='InputFiles/',
             doErrors=True):
    if not outpath.endswith('/'): outpath += '/'
    if not inpath.endswith('/'): inpath += '/'
    if not fname.endswith('.root'): fname += '.root'
    f = TFile.Open(inpath + fname)
    h = f.Get(hname)
    c = TCanvas("c", "c", 10, 10, 1600, 1200)
    gStyle.SetOptStat(0)
    gStyle.SetPalette(1)
    gStyle.SetPaintTextFormat("1.2f")

    if doErrors: h.Draw("colz, text, errors")
    else: h.Draw("colz, text")
    if not os.path.isdir(outpath): gSystem.mkdir(outpath, 1)
    c.Print(outpath + outname + '.pdf', 'pdf')
    c.Print(outpath + outname + '.png', 'png')
def parse_drawer_options(options):
    # Create outdir if necessary
    if not options.outdir.endswith("/"):
        options.outdir += "/"
    if gSystem.AccessPathName(options.outdir):
        gSystem.mkdir(options.outdir)

    # Make input file list
    if not options.infile.endswith(".root") and not options.infile.endswith(".txt"):
        raise ValueError("infile must be .root file or .txt file")

    if options.infile.endswith(".root"):
        # Create a temporary file with one line
        with tempfile.NamedTemporaryFile() as infile:
            infile.write(options.infile)
            infile.flush()
            options.tfilecoll = TFileCollection("fc", "", infile.name)
    else:
        options.tfilecoll = TFileCollection("fc", "", options.infile)

    # Batch mode
    if options.batch:
        gROOT.SetBatch(True)

    # Trigger tower parameter space
    if options.tower != 99:
        ieta = options.tower/8
        iphi = options.tower%8
        options.etamin = -2.2 + (4.4/6) * ieta
        options.etamax = -2.2 + (4.4/6) * (ieta+1)
        if iphi < 6:
            options.phimin = -pi/2 + (2*pi/8) * iphi
            options.phimax = -pi/2 + (2*pi/8) * (iphi+1)
        else:
            options.phimin = -2*pi -pi/2 + (2*pi/8) * iphi
            options.phimax = -2*pi -pi/2 + (2*pi/8) * (iphi+1)
        options.ptmin = 2.
        options.ptmax = 2000.
Exemplo n.º 5
0
channel  = "ZnunuHighPt"
region   = "TT"
massH    = 125
plotData = True
plotLog  = False
plotSig  = True
plotdir  = "plots_etc/"
kRed     = 632
kYellow  = 400
kBlue    = 600

TH1.SetDefaultSumw2(1)
gROOT.SetBatch(1)
if gSystem.AccessPathName(plotdir):
    gSystem.mkdir(plotdir)

class EventsJ11(object):
    pass


def Read(whatstep="Step4", cutallmc="", cutalldata=""):
    
    if whatstep == "Step4":
        indir    = "/uscms_data/d3/lpchbb/jiafu/ZnnH_postHCP/Step4_20130404/stitch/"
        #indir    = "/uscms_data/d3/lpchbb/jiafu/ZnnH_postHCP/Step4_20130314/stitch/"
        prefix   = "Step4_"
        suffix   = ".root"
        treename = "tree_%s_%s" % (channel, "ctrl")
        
        ZH = TChain(treename)
Exemplo n.º 6
0
def main():
    # Default conf file, if running with a different one just provide it as an argument
    configFile = 'plots.conf'
    args = sys.argv[1:]
    if len(args) >= 1:
        configFile = args[0]
    if os.path.exists(configFile):
        print('running with config file', configFile)
    else:
        print('you are trying to use a config file (' + configFile +
              ') that does not exist!')
        sys.exit(1)

    # Parse the conf file
    cfg = ConfigParser(dict_type=OrderedDict)
    cfg.optionxform = str

    cfg.read(configFile)

    # Set up plotting
    inputdir = cfg.get('setup', 'inputdir')
    outputdir = cfg.get('setup', 'outputdir')
    procs = cfg.get('setup', 'processes').replace(' ', '').split(',')
    treename = cfg.get('setup', 'treename')
    treename2d = cfg.get('setup', 'treename2d')
    comparetype = cfg.get('setup', 'comparetype')
    if not comparetype in ['processes', 'sels']:
        print('comparetype must be either "processes" or "sels"!')
        sys.exit(1)
    plotnames = cfg.get('plotting', 'plotnames').replace(' ', '').split(',')
    plotnames2d = cfg.get('plotting', 'plotnames2d').replace(' ',
                                                             '').split(',')
    effplotnames = cfg.get('plotting', 'effplotnames').replace(' ',
                                                               '').split(',')
    reversecutvars = cfg.get('plotting',
                             'reversecutvars').replace(' ', '').split(',')
    logvars = cfg.get('plotting', 'logvars').replace(' ', '').split(',')
    expr = {k: v for k, v in cfg['expressions'].items()}
    sel = {k: v for k, v in cfg['sels'].items()}
    sel2d = {k: v for k, v in cfg['sels2d'].items()}
    proclabels = {k: v for k, v in cfg['proclabels'].items()}
    plotlabels = {k: v for k, v in cfg['plotlabels'].items()}
    binning = {k: ast.literal_eval(v) for k, v in cfg['binning'].items()}
    colors = {k: st.colors[v] for k, v in cfg['colors'].items()}

    # ROOT setup stuff
    gROOT.SetBatch(True)

    st.SetTDRStyle()
    st.SetupColors()

    gStyle.SetLabelSize(0.03, "XYZ")

    gSystem.mkdir(outputdir, True)

    print('Making plots!')

    files = {
        proc: TFile.Open(inputdir + '/' + proc + '_tree.root')
        for proc in procs
    }

    # Loop over 1D variables to be plotted
    for n in plotnames:
        if n == '' or not n in binning:
            continue

        print('plotting', n)

        (outerloopitems,
         innerloopitems) = (procs, sel) if comparetype == 'sels' else (sel,
                                                                       procs)

        # Loop over outer loop items
        for x in outerloopitems:

            hists = []
            ymax = -1.

            infile = None
            tree = None
            selexp = ''

            if comparetype == 'processes':
                selexp = sel[x]
                print('with selection', selexp)

            # Loop over inner loop items
            for y in innerloopitems:
                if comparetype == 'sels':
                    infile = files[x]
                    selexp = sel[y]
                    print('with selection', selexp)
                else:
                    infile = files[y]

                tree = infile.FindObjectAny(treename)

                hist = TH1D('_'.join(['h', n, x, y]), '', binning[n][0],
                            binning[n][1], binning[n][2])
                hist.SetLineColor(colors[y])

                # Check if variable name corresponds to an expression
                if n in expr:
                    tree.Draw(expr[n] + '_'.join(['>>h', n, x, y]), selexp,
                              'histnorm')
                else:
                    tree.Draw(n + '_'.join(['>>h', n, x, y]), selexp,
                              'histnorm')

                # Histogram setup
                st.addOverFlow(hist)

                hist.GetXaxis().SetTitle(plotlabels[n])
                hist.GetXaxis().SetTitleSize(0.04)
                hist.GetYaxis().SetTitle("A.U.")
                hist.GetYaxis().SetTitleSize(0.05)
                hist.SetLineWidth(3)
                hist.SetLineColor(colors[y])
                if (hist.GetMaximum() > ymax):
                    ymax = hist.GetMaximum()
                tempmax = 0.0
                maxbin = 0
                for ibin in range(1, hist.GetNbinsX() + 1):
                    binc = hist.GetBinContent(ibin)
                    if binc > tempmax:
                        tempmax = binc
                        maxbin = hist.GetBinLowEdge(ibin)

                # Add this histogram to the list
                hists.append(hist)

            # Setup canvas
            c = st.MakeCanvas("c", "", 600, 600)
            leg = TLegend(0.65, 0.7, 0.95, 0.9)
            st.SetLegendStyle(leg)
            leg.SetTextSize(0.04)

            # Check if plotting in log scale
            logy = (n in logvars)
            if (logy):
                c.SetLogy()

            labels = []

            # Draw histograms and add each entry to the legend
            for hist in hists:
                if (logy):
                    hist.GetYaxis().SetRangeUser(1e-04, 15 * ymax)
                else:
                    hist.GetYaxis().SetRangeUser(0, 1.2 * ymax)

                hist.Draw('histsame')

                hname = str(hist.GetName())
                labelname = hname[hname.rindex('_') + 1:]

                leg.AddEntry(hist, proclabels[labelname], 'L')

            # Draw legend and save the plot
            leg.Draw('same')
            st.LDMX_lumi(c, 0, 'Simulation')

            c.SaveAs(outputdir + '/' + n + '_' + x +
                     ('_log.pdf' if logy else '.pdf'))

    # Loop over 2D plots
    for n in plotnames2d:
        if n == '':
            continue

        xvar = n[n.rindex('_') + 1:]
        yvar = n[0:n.index('_')]

        if not xvar in binning or not yvar in binning:
            continue

        print('plotting', yvar, 'vs', xvar)

        # Loop over processes
        for proc in procs:
            infile = files[proc]
            tree = infile.FindObjectAny(treename2d)

            c = st.MakeCanvas('c', '', 600, 600)

            # Loop over cut strings
            for seln in sel2d:
                selexp = sel2d[seln]
                print('with selection', selexp)

                hist = TH2D('_'.join(['h', n, proc, seln]), '',
                            binning[xvar][0], binning[xvar][1],
                            binning[xvar][2], binning[yvar][0],
                            binning[yvar][1], binning[yvar][2])

                c.cd()

                logx, logy = False, False

                if xvar in logvars:
                    logx = True
                if yvar in logvars:
                    logy = True

                if logx:
                    c.SetLogx()
                if logy:
                    c.SetLogy()

                c.SetLogz()
                c.SetLeftMargin(0.13)
                c.SetRightMargin(0.18)

                print('Drawing', expr[n])
                if n in expr:
                    #tree.Draw(expr[n]+'_'.join(['>>h',n,proc,seln]),selexp,'COLZnorm')
                    tree.Draw(expr[n] + '_'.join(['>>h', n, proc, seln]),
                              selexp, 'COLZ')
                else:
                    #tree.Draw(n+'_'.join(['>>h',n,proc,seln]),selexp,'COLZnorm')
                    tree.Draw(n + '_'.join(['>>h', n, proc, seln]), selexp,
                              'COLZ')

                # Histogram setup
                hist.GetXaxis().SetTitle(plotlabels[xvar])
                hist.GetXaxis().SetTitleSize(0.05)
                hist.GetYaxis().SetTitle(plotlabels[yvar])
                hist.GetYaxis().SetTitleSize(0.05)

                # Save plot
                c.SaveAs(outputdir + '/' + n + '_' + proc + '_' + seln +
                         ('_log.pdf' if logx or logy else '.pdf'))

    # Loop over efficiency variables to be plotted
    for n in effplotnames:
        if n == '' or not n in binning:
            continue

        print('plotting efficiency vs', n)

        (outerloopitems,
         innerloopitems) = (procs, sel) if comparetype == 'sels' else (sel,
                                                                       procs)
        # Loop over outer loop items
        for x in outerloopitems:

            hists = []
            effs = []

            infile = None
            tree = None
            selexp = ''

            if comparetype == 'sels':
                infile = files[x]
                tree = infile.FindObjectAny(treename)
            else:
                selexp = sel[x]
                print('with selection', selexp)

            isel = -1
            # Loop over inner loop items
            for y in innerloopitems:
                if comparetype == 'sels':
                    selexp = sel[y]
                    print('with selection', selexp)
                else:
                    infile = files[y]
                    tree = infile.FindObjectAny(treename)

                isel += 1

                hist = TH1D('_'.join(['h', n, x, y]), '', binning[n][0],
                            binning[n][1], binning[n][2])
                hist.SetLineColor(colors[y])
                hist.SetMarkerColor(colors[y])

                # Check if variable name corresponds to an expression
                if n in expr:
                    tree.Draw(expr[n] + '_'.join(['>>h', n, x, y]), selexp)
                else:
                    tree.Draw(n + '_'.join(['>>h', n, x, y]), selexp)

                # Histogram setup
                st.addOverFlow(hist)
                hist.GetXaxis().SetTitle(plotlabels[n])
                hists.append(hist)

            # Setup canvas
            c = st.MakeCanvas("c", "", 600, 600)
            leg = TLegend(0.65, 0.7, 0.95, 0.9)
            st.SetLegendStyle(leg)
            leg.SetTextSize(0.04)

            logy = (n in logvars)
            if (logy):
                c.SetLogy()

            if len(hists):
                xmin = hists[0].GetXaxis().GetXmin()
                xmax = hists[0].GetXaxis().GetXmax()
                c.DrawFrame(xmin, 1e-6 if logy else 0, xmax, 1.1)

            graphs = []
            emptyhist = None

            ihist = -1
            for hist in hists:
                ihist += 1

                hname = str(hist.GetName())
                labelname = hname[hname.rindex('_') + 1:]

                xmin = hist.GetXaxis().GetXmin()
                xmax = hist.GetXaxis().GetXmax()

                effgr = pt.computeEffVsCutGraph(hist, n in reversecutvars)
                effgr.SetLineWidth(3)
                effgr.SetLineColor(colors[labelname])
                effgr.SetMarkerColor(colors[labelname])
                effgr.GetXaxis().SetTitle(plotlabels[n])
                effgr.GetYaxis().SetTitle('Efficiency')
                effgr.GetXaxis().SetTitleSize(0.04)
                effgr.GetYaxis().SetTitleSize(0.05)
                effgr.GetHistogram().GetXaxis().SetLimits(xmin, xmax)
                effgr.GetHistogram().GetYaxis().SetRangeUser(
                    1e-6 if logy else 0.0, 1.1)
                c.cd()
                effgr.Draw('Csame')
                if ihist == 0:
                    emptyhist = effgr.GetHistogram()
                leg.AddEntry(effgr, proclabels[labelname], 'L')

                graphs.append(effgr)

            if emptyhist:
                emptyhist.Draw('AXIS')

            for graph in graphs:
                graph.Draw('Csame')

            c.cd()
            leg.Draw('same')
            st.LDMX_lumi(c, 0, 'Simulation')

            c.SaveAs(outputdir + '/eff_vs_' + n + '_' + y + '.pdf')

    for infile in files.values():
        infile.Close()
    chain.Add(f)


sections = {}
sections["makedataset"] = False
sections["makefits"]    = True
sections["makenewfits"] = False
sections["makefits2"]   = False
plotting = []
writing = []


imgdir = "figures_20140206/"
if not imgdir.endswith("/"):  imgdir += "/"
if gSystem.AccessPathName(imgdir):
    gSystem.mkdir(imgdir)


#_______________________________________________________________________________
# Classes/Functions

class Properties:
    """Stores stuff"""
    def __init__(self, x, y):
        self.x = x
        self.y = y

def CMS_label():
    old = (latex.GetTextFont(), latex.GetTextSize())
    latex.SetTextFont(42); latex.SetTextSize(0.026)
    latex.DrawLatex(0.665, 0.968, "Run2012D HLT_L1ETM40_v2")
Exemplo n.º 8
0
def create_k_folds(input_root_file_name, input_tree_name = None, k_folds = None):
	"""
	Inspect a .root ntuple and resample a TTree into k-folds for cross-validation.
	"""
	
	input_root_file = TFile(input_root_file_name, 'READ')
	
	# Check function arguments.
	if (not input_tree_name) or (not input_root_file.GetListOfKeys().FindObject(input_tree_name)):
		print "\nUnspecified or incorrect TTree name. Consider the file contents below.\n"
		input_root_file.ls()
		print "\n"
		return
	elif (k_folds == None):
		print "\nUnspecified number of folds."
		return
	else:
		input_tree = input_root_file.Get(input_tree_name)
		input_tree_n_entries = input_tree.GetEntriesFast()
	
	# Specify output directory, creating it if it doesn't exist.
	output_directory = 'k_folds/' 

	if (gSystem.AccessPathName(output_directory)):
		gSystem.mkdir(output_directory)
	
	# Determine the number of entries per fold, assuming the total number of entries is exactly divisible into k folds.
	# Create a list holding the entry indices specifying the endpoints for each fold.
	n_entries_per_fold = int(input_tree_n_entries / k_folds)
	range_list = range(0, n_entries_per_fold * (k_folds + 1), n_entries_per_fold)
	
	# If a remainder exists, we modify accordingly.
	n_entries_remainder = int(input_tree_n_entries % k_folds)
	
	if (n_entries_remainder != 0):
		for extra in range(0, len(range_list)):
			if (extra < n_entries_remainder):
				range_list[extra] += extra
			else:
				range_list[extra] += n_entries_remainder

	# Loop over each fold.	
	for k in range(0, k_folds):
	
		# Create output .root file for the current fold.	
		output_root_file_name = output_directory + 'CVFold_%s_of_%s.root' % (k+1, k_folds)
		output_root_file = TFile(output_root_file_name, 'RECREATE')

		# Copy an empty tree to hold the training sample.
		output_train_tree = input_tree.CloneTree(0)
		output_train_tree.SetName('%s_CV_Train' % input_tree.GetName())

		# Copy an empty tree to hold the "testing" sample.
		output_test_tree = input_tree.CloneTree(0)
		output_test_tree.SetName('%s_CV_Test' % input_tree.GetName())

		# Loop over the input tree entries, storing it for testing if it falls within the range
		# and training otherwise. I find this faster than conditionals within the loop.
		for entry in range(0, range_list[k]):
			input_tree.GetEntry(entry, 1) # The second argument 1 means get all branches.
			output_train_tree.Fill()
		for entry in range(range_list[k], range_list[k+1]):
			input_tree.GetEntry(entry, 1)
			output_test_tree.Fill()
		for entry in range(range_list[k+1], input_tree_n_entries):
			input_tree.GetEntry(entry, 1)
			output_train_tree.Fill()
	
		# Write the output trees and save the output file.
		output_train_tree.Write()
		output_test_tree.Write()
		output_root_file.Close()
	
	# Close the input file.
	input_root_file.Close()		

	return "Successfully split entries for %s-fold cross-validation." % k_folds
Exemplo n.º 9
0
def RunPAF(samples,
           selection,
           xsec,
           nSumOfWeights,
           year,
           outname,
           nSlots=1,
           outpath='',
           options='',
           isamcatnlo=False,
           isData=False,
           nEvents=0,
           FirstEvent=0,
           workingdir=''):
    from ROOT import PAFProject, PAFIExecutionEnvironment, PAFSequentialEnvironment, PAFPROOFLiteEnvironment, PAFPoDEnvironment
    from ROOT import vector, TString, gSystem

    # PAF mode selection (based on number of slots)
    pafmode = PAFSequentialEnvironment()
    if nSlots <= 1: pafmode = PAFSequentialEnvironment()
    else: pafmode = PAFPROOFLiteEnvironment(nSlots)

    myProject = PAFProject(pafmode)

    myProject.AddDataFiles(samples)
    myProject.SetDefaultTreeName("Events")

    # Deal with first and last event
    if nEvents > 0: myProject.SetNEvents(nEvents)
    if FirstEvent > 0: myProject.SetFirstEvent(FirstEvent)
    if workingdir == "": workingdir = os.getcwd()

    # Set output file
    if outpath == '': outpath = workingdir + "/" + selection + "_temp"
    gSystem.mkdir(outpath, 1)
    myProject.SetOutputFile(outpath + "/" + outname + ".root")
    if verbose: print '## Output to: %s' % (outpath + "/" + outname + ".root")

    # Parameters for the analysis
    myProject.SetInputParam("sampleName", outname)
    myProject.SetInputParam("IsData", isData)
    myProject.SetInputParam("weight", xsec / nSumOfWeights)
    myProject.SetInputParam("IsMCatNLO", isamcatnlo)
    myProject.SetInputParam("selection", selection)
    myProject.SetInputParam("WorkingDir", workingdir)
    myProject.SetInputParam("xsec", xsec)
    myProject.SetInputParam("_options", options)
    myProject.SetInputParam("year", str(year))

    # Name of analysis class
    myProject.AddSelectorPackage("LeptonSelector")
    myProject.AddSelectorPackage("JetSelector")
    myProject.AddSelectorPackage("EventBuilder")

    # Analysis selector
    if selection != "":
        myProject.AddSelectorPackage(selection)
    #if selection == 'TopAnalysis':
    #  myProject.AddSelectorPackage("LepEffTop")

    # Additional packages
    myProject.AddPackage("Lepton")
    myProject.AddPackage("Jet")
    myProject.AddPackage("mt2")
    myProject.AddPackage("Functions")
    myProject.AddPackage("LeptonSF")
    myProject.AddPackage("BTagSFUtil")

    myProject.Run()