Example #1
0
def plotAll(file):
    outDir = file[:-5] + '/'
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    outDir += "plotAll/"
    if not os.path.exists(outDir):
        os.mkdir(outDir)

    AtlasStyle.SetAtlasStyle()

    gROOT.ProcessLine("gErrorIgnoreLevel = 2000")  #Ignore TCanvas::Print info

    inFile = TFile.Open(file, "READ")
    keyList = [key.GetName()
               for key in inFile.GetListOfKeys()]  #List of top level objects
    dirList = [key for key in keyList
               if "Iteration" in key]  #List of all directories

    nomDir = [dir for dir in dirList if "Nominal" in dir]
    if (not len(nomDir) == 1):
        print "Error, nominal directories are ", nomDir
        return
    else:
        nomDir = inFile.Get(nomDir[0])

    c1 = TCanvas()

    ##################### Plot every single histogram  #################################

    print "Plotting All Histograms for "
    for dir in dirList:
        if not "Nominal" in dir:
            continue
        print "           ", dir
        if not os.path.exists(outDir + dir):
            os.mkdir(outDir + dir)
        if not os.path.exists(outDir + dir + '/eps'):
            os.mkdir(outDir + dir + '/eps')

        thisDir = inFile.Get(dir)
        histList = [key.GetName() for key in thisDir.GetListOfKeys()]
        ### Save all histograms ###
        for histName in histList:
            print dir, histName
            thisHist = thisDir.Get(histName)
            if ("Pt" in histName):
                thisHist.GetXaxis().SetRangeUser(0, 2000.)
            if (not "Nominal" in dir):
                nomHist = nomDir.Get(histName)
                thisHist.Add(nomHist, -1)
                thisHist.Divide(nomHist)
                thisHist.GetYaxis().SetTitle("Relative Difference w/ Nominal")
            if type(thisHist) == TH2F or type(thisHist) == TH3F:
                thisHist.Draw("colz")
            else:
                thisHist.Draw()
            c1.SaveAs(outDir + dir + "/" + thisHist.GetName() + ".png")
            c1.SaveAs(outDir + dir + "/eps/" + thisHist.GetName() + ".eps")
            c1.Clear()
    inFile.Close()
Example #2
0
def main():

    AtlasStyle.SetAtlasStyle()
    gROOT.SetBatch()
  
    parser = argparse.ArgumentParser(description='Error Analysis for TRT logs, input grafana mu dumps and ers log.')
    parser.add_argument('-d', '--dir', dest='dir', default='2018', type=str,  help="Directory of grafana mu dumps")
    parser.add_argument('-f', '--file', dest='log_file', default='', type=str, help="Log file from ERS.")
    parser.add_argument('-s', '--print_sb', dest='print_sb', default=False, action='store_true', help="print sb times")
    parser.add_argument('-n', '--non_sb',dest='non_sb', default=False, action='store_true', help="get errors for non-sb times")
    parser.add_argument('-t', '--text',dest='text_filter', default='rocketio problem', type=str, help="text to filter query")
    parser.add_argument('-b', '--date_beg',dest='date_beg', default='2015-04-01 00:00:00', type=str, help="date begin. Formatted yyyy-mm-dd hh:mm:ss format")
    parser.add_argument('-e', '--date_end',dest='date_end', default='2100-01-01 00:00:00', type=str, help="date end.  Formatted yyyy-mm-dd hh:mm:ss format")

    args = parser.parse_args()

    if args.print_sb:
        beg_sb, end_sb = getsb.get_sb_list(args.dir, args.print_sb)

    if args.log_file != '':
        beg_sb, end_sb = getsb.get_sb_list(args.dir, args.print_sb)
        dbhandler.add_to_db(beg_sb, end_sb, args.log_file)

    query_errors = dbhandler.get_query(args.non_sb, args.text_filter, args.date_beg, args.date_end)
    print "Query returned " + repr(len(query_errors)) + " results!"

    if('rocketio' in args.text_filter):
        parse_rocketio_errors(query_errors)
Example #3
0
import AtlasStyle

import ROOT

import BasicConfig
import utils

from datetime import date

if __name__ == '__main__':
    AtlasStyle.SetAtlasStyle()

    mass_list = range(600, 1801, 200)
    tfiles = [
        utils.open_tfile(
            BasicConfig.workdir +
            'SysUnc_ISR/SysUnc_ISR_{:04d}_weight.root'.format(mass))
        for mass in mass_list
    ]

    c = ROOT.TCanvas('c', 'c', 1000, 800)
    c.SetLogx()
    tf_out = ROOT.TFile(
        BasicConfig.workdir + 'SysUnc_ISR/SysUnc_ISR_weight.root', 'recreate')
    for ii, (tfile, mass) in enumerate(zip(tfiles, mass_list)):
        tfile.cd()
        c2 = ROOT.gROOT.FindObject('c2')
        hist = c2.GetPrimitive('ggSystem_SysUnc_ISR')
        hist.SetName('ggSystem_SysUnc_ISR_' + str(mass))
        utils.decorate_histogram(hist, BasicConfig.colors[ii])
        c.cd()
Example #4
0
def make_systematic_table():
    AtlasStyle.SetAtlasStyle()

    #directory = '/afs/cern.ch/work/k/kmotohas/DisplacedVertex/DV_xAODAnalysis/submitDir_LSF/mc/hist_DVPlusMETSys/'
    #directory = BasicConfig.workdir + 'hist_DVPlusMETSys/'
    directory = '/home/motohash/data/mc15_13TeV/DVPlusMETSys/'

    #tfile = TFile(BasicConfig.workdir + 'systTree.root')
    tfile = TFile(args.inputFile)
    key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()]
    print(len(key_list_all), key_list_all)
    regex = re.compile('Nominal|PRW|JET|MET.*')
    key_list = [key for key in key_list_all if re.match(regex, key)]
    print(len(key_list), key_list)
    tfile.Close()

    output_tfile = TFile('systematic_summary_SimpleMETFilter.root', 'recreate')

    #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402700, 402740)]
    tchains = [[dsid, [TChain(key, key + str(dsid)) for key in key_list]]
               for dsid in mc.parameters.keys()]
    #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402070, 402080)]
    #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402070, 402080)]

    #trees = [gROOT.FindObject(key) for key in key_list]
    #entries_list = [tree.GetEntriesFast() for tree in trees]

    gStyle.SetHistMinimumZero()

    for dsid, each_tchain in tchains:
        print('')
        print(dsid)
        #print glob(directory + 'systTree_mc15_13TeV.' + str(dsid) + '*.root')
        for tchain in each_tchain:
            for input in glob(directory + 'systTree_mc15_13TeV.' + str(dsid) +
                              '*.root'):
                #print(input)
                tchain.Add(input)

        #h_syst_diff = TH1F('syst_diff', ';Difference from Nominal', 101, -50.5, 50.5, 120, 0, 120)
        h_syst_diff = TH1F('syst_diff',
                           ';;(N_{shifted} - N_{nominal}) / N_{nominal}',
                           len(key_list) + 1, 0,
                           len(key_list) + 1)
        for ii, key in enumerate(key_list):
            h_syst_diff.GetXaxis().SetBinLabel(ii + 1, key)
        h_syst_diff.GetXaxis().SetBinLabel(
            len(key_list) + 1, 'ISR_Py2MG_SF_removed')
        h_syst_diff.SetMinimum(-0.3)
        h_syst_diff.SetMaximum(0.3)

        n_events_nominal = 0.
        for ii, tchain in enumerate(each_tchain):
            #print('')
            print('*** ' + key_list[ii])
            n_events_weighted = 0.
            n_events_weighted_noISR = 0.
            entries = tchain.GetEntries()
            if entries == 0:
                continue
            for entry in xrange(entries):
                ## get the next tree in the chain and verify
                ientry = tchain.LoadTree(entry)
                if ientry < 0:
                    break
                ## copy next entry into memory and verify
                nb = tchain.GetEntry(entry)
                if nb <= 0:
                    continue
                #if pass_event_cut(tchain, 7):
                #if pass_event_cut(tchain, 5) and tchain.MET > 220 and tchain.PassCut7:  # TODO
                if pass_event_cut(tchain, len(cut_flow) - 1):  # TODO
                    #if pass_event_cut(tchain, 5) and tchain.MET > 250 and tchain.PassCut7:  # TODO
                    #print(tree.McEventWeight, tree.PileupWeight)
                    n_events_weighted += tchain.McEventWeight * tchain.PileupWeight * tchain.ISRWeight
                    n_events_weighted_noISR += tchain.McEventWeight * tchain.PileupWeight
            if ii == 0:
                n_events_nominal = n_events_weighted
                if n_events_nominal < 1e-4:
                    h_syst_diff.SetBinContent(len(key_list) + 1, 0)
                else:
                    h_syst_diff.SetBinContent(
                        len(key_list) + 1,
                        float((n_events_weighted_noISR - n_events_nominal) /
                              n_events_nominal))
            diff = n_events_weighted - n_events_nominal
            #print(n_events_nominal, n_events_weighted, diff)
            if n_events_nominal < 1e-4:
                h_syst_diff.SetBinContent(ii + 1, 0)
            else:
                h_syst_diff.SetBinContent(ii + 1,
                                          float(diff / n_events_nominal))
            #h_syst_diff.SetBinError(ii+1, n_event_weighted-n_events_nominal)
        utils.decorate_histogram(h_syst_diff, kPink, fill_style=3001)
        h_syst_diff.SetBarWidth(0.9)
        h_syst_diff.GetXaxis().SetLabelSize(0.035)
        output_tfile.cd()
        h_syst_diff.SetName('systematic_table_' + str(dsid))
        h_syst_diff.Write()
Example #5
0
def fill_ntuple():
    print('*** starting fill_ntuple() ')
    AtlasStyle.SetAtlasStyle()

    #    # get key list
    #    tfile = TFile(BasicConfig.workdir + 'systTree.root')
    #    key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()]
    #    regex = re.compile('PRW|JET|MET.*')
    #    key_list = [key for key in key_list_all if re.match(regex, key)]
    #    tfile.Close()

    # start making ttree
    #output_tfile = TFile('rhadron_v06-00-05.root', 'recreate')
    output_tfile = TFile(args.outputFile, 'recreate')

    # initialize TTree
    tree = TTree('rhadron', 'tree of rhadron properties for limit setting')
    # leaf variables
    from array import array
    mass_gluino = array('f', [0.])
    delta_mass = array('f', [0.])
    ctau = array('f', [0.])
    eff = array('f', [0.])
    eff_stat_error = array('f', [0.])
    eff_syst_error = array('f', [0.])
    eff_syst_error_ISR = array('f', [0.])
    eff_syst_error_PRW = array('f', [0.])
    eff_syst_error_JET = array('f', [0.])
    eff_syst_error_MET = array('f', [0.])
    # set branch
    tree.Branch("mGluino", mass_gluino, 'mGluino/F')
    tree.Branch("deltaM", delta_mass, 'deltaM/F')
    tree.Branch("ctau", ctau, 'ctau/F')
    tree.Branch("eff", eff, 'eff/F')
    tree.Branch("effRelStatErr", eff_stat_error, 'effRelStatErr/F')
    tree.Branch("effRelSystErr", eff_syst_error, 'effRelSystErr/F')
    tree.Branch("effRelSystErrISR", eff_syst_error_ISR, 'effRelSystErrISR/F')
    tree.Branch("effRelSystErrPRW", eff_syst_error_PRW, 'effRelSystErrPRW/F')
    tree.Branch("effRelSystErrJET", eff_syst_error_JET, 'effRelSystErrJET/F')
    tree.Branch("effRelSystErrMET", eff_syst_error_MET, 'effRelSystErrMET/F')

    #directory = '/afs/cern.ch/work/k/kmotohas/DisplacedVertex/DV_xAODAnalysis/submitDir_LSF/mc/hist_DVPlusMETSys/'
    #directory = BasicConfig.workdir + 'hist_DVPlusMETSys/'
    #directory = '/home/motohash/data/mc15_13TeV/DVPlusMETSys/v06-00-05/'

    #tfile = TFile(args.referenceFile)
    tfile = TFile(args.inputFile)
    key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()]
    print(len(key_list_all), key_list_all)
    regex = re.compile('Nominal|PRW|JET|MET.*')
    key_list = [key for key in key_list_all if re.match(regex, key)]
    print(len(key_list), key_list)
    tfile.Close()
    #c = 299792458.  # [m/s]
    #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in range(402700, 402740)]
    #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in mc.parameters.keys()]
    #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in mc.parameters.keys()]
    dsids = [args.DSID]
    tchains = [[dsid, [TChain(key, key + str(dsid)) for key in key_list]]
               for dsid in dsids]

    cut_flow = [
        'Initial', 'Trigger', 'Filter', 'Cleaning', 'GRL', 'PV', 'NCB veto',
        'MET', 'DV Selection'
    ]
    #systematic_tables = TFile('systematic_summary_SimpleMETFilter.root', 'open')
    #table = TH1F()

    m_MET_min = 250.

    # loop over dsid
    try:
        for dsid, each_tchain in tchains:
            print('')
            print(dsid)
            #index = 0
            #for input in glob(directory + 'systTree_' + str(dsid) + '_*.root'):
            for tchain in each_tchain:
                #for input_file in glob(directory+'systTree_mc15_13TeV.' + str(dsid) + '*.root'):
                #    print(input_file)
                #    tchain.Add(input_file)
                tchain.Add(args.inputFile)

            mass_gluino[0] = mc.parameters[dsid]['g']
            delta_mass[0] = mass_gluino[0] - mc.parameters[dsid]['chi0']
            n_reweight_steps = 40
            xmin = 1.
            xmax = 10000.
            ratio = xmax / xmin
            bins = []
            for ii in range(n_reweight_steps):
                bins.append(
                    xmax *
                    10**(ii * TMath.Log10(xmax / xmin) / n_reweight_steps -
                         TMath.Log10(xmax / xmin)))
            #n_passed_w1 = [0. for _ in range(n_reweight_steps)]
            #n_passed = [0. for _ in range(n_reweight_steps)]
            from array import array
            limitsLifetime = array('d', bins)
            #
            tefficiency = [[
                TEfficiency('tefficiency_{0}_{1}_{2}'.format(key, step, dsid),
                            ';c#tau [mm]; Event-level efficiency',
                            len(limitsLifetime) - 1, limitsLifetime)
                for step in range(n_reweight_steps)
            ] for key in key_list]
            #h_syst_diff = [[TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1)
            #                for step in range(n_reweight_steps)] for key in key_list]
            h_syst_diff = [
                TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid),
                     ';;(N_{shifted} - N_{nominal}) / N_{nominal}',
                     len(key_list) + 1, 0,
                     len(key_list) + 1) for step in range(n_reweight_steps)
            ]

            for step in range(n_reweight_steps):
                for jj, key in enumerate(key_list):
                    h_syst_diff[step].GetXaxis().SetBinLabel(jj + 1, key)
                h_syst_diff[step].GetXaxis().SetBinLabel(
                    len(key_list) + 1, 'ISR_Py2MG_SF_removed')
            n_events_weighted = [[0. for _ in range(n_reweight_steps)]
                                 for key in key_list]
            n_events_weighted_noISR = [[0. for _ in range(n_reweight_steps)]
                                       for key in key_list]

            # loop over tchain of each systematic
            for ii, tchain in enumerate(each_tchain):
                entries = tchain.GetEntries()
                print('*** processed systs: {0} / {1}'.format(
                    ii, len(each_tchain)))
                #n_reweight_steps = 50
                #for step in range(n_reweight_steps):
                #    tefficiency.append(TEfficiency('tefficiency_'+str(step), ';c#tau [mm]; Event-level efficiency',
                #                                   len(limitsLifetime)-1, limitsLifetime))
                #    h_syst_diff.append(TH1F('syst_diff_'+str(step), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1))
                for step in range(n_reweight_steps):
                    tefficiency[ii][step].SetUseWeightedEvents()
                    #for jj, key in enumerate(key_list):
                    #     h_syst_diff[ii][step].GetXaxis().SetBinLabel(jj+1, key)
                    #h_syst_diff[ii][step].GetXaxis().SetBinLabel(len(key_list)+1, 'ISR_Py2MG_SF_removed')
                #    h_syst_diff[step].SetMinimum(-0.3)
                #    h_syst_diff[step].SetMaximum(0.3)
                if entries == 0:
                    continue
                for entry in range(entries):
                    #if entry % 1000 == 0:
                    #    print('* processed events: {0} / {1}'.format(entry, entries))
                    utils.show_progress(entry, entries)
                    #if entry == 605:
                    #    break
                    # get the next tree in the chain and verify
                    ientry = tchain.LoadTree(entry)
                    if ientry < 0:
                        break
                    # copy next entry into memory and verify
                    nb = tchain.GetEntry(entry)
                    if nb <= 0:
                        continue
                    event_weight = tchain.McEventWeight * tchain.PileupWeight * tchain.ISRWeight
                    ctau_MC = TMath.C(
                    ) * mc.parameters[dsid]['t'] * 1e-9  # [nm]->[m]
                    for step in range(n_reweight_steps):
                        #print(tchain.GetListOfBranches())
                        pass_all = pass_event_cut(tchain, len(cut_flow) - 1)
                        if pass_all:
                            matched = False
                            for idv in range(len(tchain.DV_x)):
                                matched = matched or match(
                                    tchain, idv, cut=1.0)
                            #print('pass_all is ', pass_all, ', matched is ', matched)
                            pass_all = pass_all and matched
                        target_ctau = xmax * 10**(
                            step * TMath.Log10(xmax / xmin) / n_reweight_steps
                            - TMath.Log10(xmax / xmin)) * 1e-3  # [mm]->[m]
                        #print(target_ctau)
                        lifetime_weight = get_lifetime_weight(
                            tchain, target_ctau, ctau_MC)
                        n_events_weighted[ii][
                            step] += event_weight * lifetime_weight
                        n_events_weighted_noISR[ii][
                            step] += tchain.McEventWeight * tchain.PileupWeight * lifetime_weight
                        #print(event_weight)
                        #print(event_weight*lifetime_weight)
                        #print(pass_all)
                        tefficiency[ii][step].FillWeighted(
                            pass_all, event_weight * lifetime_weight,
                            target_ctau * 1e3)
                # end of loop over entries of each TChain
            # end loop over tchain of each systematic
            for step in range(n_reweight_steps):
                n_events_nominal = [0. for _ in range(n_reweight_steps)]
                for ii in range(len(each_tchain)):
                    # if Nominal TTree, set syst diff of ISR as well
                    if ii == 0:
                        n_events_nominal[step] = n_events_weighted[ii][step]
                        if n_events_nominal[step] < 1e-4:
                            #h_syst_diff[ii][step].SetBinContent(len(key_list)+1, 0)
                            h_syst_diff[step].SetBinContent(
                                len(key_list) + 1, 0)
                        else:
                            #h_syst_diff[ii][step].SetBinContent(len(key_list)+1,
                            h_syst_diff[step].SetBinContent(
                                len(key_list) + 1,
                                float((n_events_weighted_noISR[ii][step] -
                                       n_events_nominal[step]) /
                                      n_events_nominal[step]))
                            #float((n_events_weighted[ii][step]-n_events_nominal[step])/n_events_nominal[step]))
                    diff = n_events_weighted[ii][step] - n_events_nominal[step]
                    #print(n_events_nominal, n_events_weighted, diff)
                    if n_events_nominal[step] < 1e-4:
                        #h_syst_diff[ii][step].SetBinContent(ii+1, 0)
                        h_syst_diff[step].SetBinContent(ii + 1, 0)
                    else:
                        #h_syst_diff[ii][step].SetBinContent(ii+1, float(diff/n_events_nominal[step]))
                        h_syst_diff[step].SetBinContent(
                            ii + 1, float(diff / n_events_nominal[step]))
                    #systematic_tables.GetObject('systematic_table_'+str(dsid), table)
                    #syst_up, syst_down = root_sum_squares(table, 'x')
                #systs = root_sum_squares(h_syst_diff[ii][step], 'x')
                systs = root_sum_squares(h_syst_diff[step], 'x')
                #eff_syst_error[0] = max(syst_up, syst_down)  # TODO
                #eff_syst_error[0] = (syst_up**2 + syst_down**2)**0.5

                ####    ############################
                eff_syst_error[0] = (systs[0]**2 + systs[1]**2)**0.5
                eff_syst_error_ISR[0] = systs[2]
                eff_syst_error_PRW[0] = systs[3]
                eff_syst_error_JET[0] = systs[4]
                eff_syst_error_MET[0] = systs[5]
                if eff_syst_error[0] > 1:
                    print('eff_syst_error[0] = ' + str(eff_syst_error[0]))
                    #eff_syst_error[0] = 1.
                #for step in range(n_reweight_steps):
                #for ct in bins:
                #    print(len(bins), bins)
                #print(n_total_w1[step], n_total[step])
                #sf =  n_total_w1[step] / n_total[step]
                #n_passed[step] *= sf
                #n_total[step] *= sf
                #eff_no_weight, stat_error_no_weight = utils.division_error_propagation(n_passed_w1[step], n_total_w1[step])
                #ctau[0] = TMath.Power(300, step/float(n_reweight_steps-1)) * 1e-3  # [mm]->[m]
                ct = bins[step]
                #print(ct)
                ctau[0] = ct * 1e-3  # [mm]->[m]
                #print(ctau[0])
                bin_ctau = tefficiency[0][step].GetPassedHistogram().FindBin(
                    ct)
                print(tefficiency[0][step].GetPassedHistogram().GetBinContent(
                    bin_ctau))
                print(tefficiency[0][step].GetTotalHistogram().GetBinContent(
                    bin_ctau))
                #print(bin_ctau)
                #print('ct', ct, 'bin_ctau', bin_ctau)
                eff[0] = tefficiency[0][step].GetEfficiency(bin_ctau)
                print(eff[0])
                abs_stat_error = (
                    tefficiency[0][step].GetEfficiencyErrorLow(bin_ctau)**2 +
                    tefficiency[0][step].GetEfficiencyErrorUp(bin_ctau)**
                    2)**0.5
                #eff[0], abs_stat_error = utils.binomial_ratio_and_error(n_passed[step], n_total[step])
                #if eff[0] < 1e-4:
                if eff[0] == 0:
                    eff_stat_error[
                        0] = 1.  # avoid zero division error and divergence
                    continue  # not fill values in tree if efficiency is too small
                else:
                    eff_stat_error[0] = abs_stat_error / eff[0]
                #if eff_stat_error[0] > 1:
                #    print(n_passed[step], n_total[step], abs_stat_error, eff[0], eff_stat_error[0])
                #    eff_stat_error[0] = 1.
                tree.Fill()
            # end loop over n_reweight_steps
    except KeyboardInterrupt:
        pass
    output_tfile.Write()
    output_tfile.Close()
Example #6
0
def create_cut_flow():
    AtlasStyle.SetAtlasStyle()

    #input_tfile = utils.open_tfile(BasicConfig.workdir + 'DVTree_NTuple_data15_13TeV.root')
    #tree = input_tfile.Get('DVTree_NTuple')
    input_tfile = utils.open_tfile(args.inputFile)
    tree = input_tfile.Get('Nominal')

    cut_flow = [
        'Initial', 'Trigger', 'Filter', 'Cleaning', 'GRL', 'PV', 'NCB veto',
        'MET', 'DV Selection'
    ]
    h_cut_flow = TH1F('cut_flow', ';;Number of Events', len(cut_flow), 0,
                      len(cut_flow))
    #h_cut_flow2 = TH1F('cut_flow2', ';;Number of Events', len(cut_flow), 0, len(cut_flow))
    for bin, cut in enumerate(cut_flow):
        h_cut_flow.GetXaxis().SetBinLabel(bin + 1, cut)
    #
    entries = tree.GetEntries()
    for entry in range(entries):
        #if entry % 10000 == 0:
        #    print('*** processed {0} out of {1}'.format(entry, entries))
        utils.show_progress(entry, entries)
        #if entry == 100000:
        #    break
        # get the next tree in the chain and verify
        ientry = tree.LoadTree(entry)
        if ientry < 0:
            break
        # copy next entry into memory and verify
        nb = tree.GetEntry(entry)
        if nb <= 0:
            continue
        event_weight = tree.McEventWeight * tree.PileupWeight * tree.ISRWeight
        for step, cut in enumerate(cut_flow):
            if step == 0:
                h_cut_flow.Fill(cut, event_weight)
                #h_cut_flow2.Fill(cut, event_weight)
            #elif step == 2:
            #    if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 2):
            #        h_cut_flow.Fill(cut, event_weight)
            #    if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 2):
            #        h_cut_flow2.Fill(cut, event_weight)
            #elif step == 6:
            #    if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 6):
            #        h_cut_flow.Fill(cut, event_weight)
            #    if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 6):
            #        h_cut_flow2.Fill(cut, event_weight)
            #elif step == 7:
            #    #have_signal_like_dv = False
            #    #for dv_index in range(len(tree.DV_passVtxCuts)):
            #    #    have_signal_like_dv = have_signal_like_dv or tree.DV_passVtxCuts[dv_index]
            #    #if pass_event_cut(tree, 7) and tree.MET > 220 and have_signal_like_dv:
            #    if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 7):
            #        h_cut_flow.Fill(cut, event_weight)
            #    if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 7):
            #        h_cut_flow2.Fill(cut, event_weight)
            elif pass_event_cut(tree, step):
                h_cut_flow.Fill(cut, event_weight)
                #h_cut_flow2.Fill(cut, event_weight)
    output = TFile('cut_flow.root', 'recreate')
    h_cut_flow.Write()
    output.Close()
Example #7
0
def check_n_vertices_vs_met_threshold():
    AtlasStyle.SetAtlasStyle()

    #input_tfile = utils.open_tfile(BasicConfig.workdir + 'DVTree_NTuple_data15_13TeV.root')
    input_tfile = utils.open_tfile(args.inputFile)
    #tree = input_tfile.Get('DVTree_NTuple')
    tree = input_tfile.Get('Nominal')

    #bin_name = ['Base', 'Trigger', 'Filter', 'MET200', 'MET220', 'MET250']
    bin_name = ['Base', 'Trigger', 'Filter', 'MET250']
    h_nevents_cut = TH1F('nevents_cut', ';;Double Ratio', len(bin_name), 0,
                         len(bin_name))
    h_nevents_all = TH1F('nevents_all', ';;Double Ratio', len(bin_name), 0,
                         len(bin_name))
    h_ndvs_cut = {
        ntracks: TH1F('ndvs_cut_' + str(ntracks), ';;Double Ratio',
                      len(bin_name), 0, len(bin_name))
        for ntracks in range(2, 6)
    }
    h_ndvs_all = {
        ntracks: TH1F('ndvs_all_' + str(ntracks), ';;Double Ratio',
                      len(bin_name), 0, len(bin_name))
        for ntracks in range(2, 6)
    }
    for bin, name in enumerate(bin_name):
        h_nevents_cut.GetXaxis().SetBinLabel(bin + 1, name)
        h_nevents_all.GetXaxis().SetBinLabel(bin + 1, name)
        for ntracks in range(2, 6):
            h_ndvs_cut[ntracks].GetXaxis().SetBinLabel(bin + 1, name)
            h_ndvs_all[ntracks].GetXaxis().SetBinLabel(bin + 1, name)
    entries = tree.GetEntries()
    for entry in range(entries):
        utils.show_progress(entry, entries)
        #if entry == 1000000:
        #    break
        # get the next tree in the chain and verify
        ientry = tree.LoadTree(entry)
        if ientry < 0:
            break
        # copy next entry into memory and verify
        nb = tree.GetEntry(entry)
        if nb <= 0:
            continue
        if not utils.basic_event_selection(tree):
            continue
        # fill all
        for name in bin_name:
            h_nevents_all.Fill(name, 1.)
            for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
                if utils.basic_dv_selection(tree, dv_index):
                    if DV_nTracks < 6:
                        h_ndvs_all[DV_nTracks].Fill(name, 1.)
                    else:
                        h_ndvs_all[5].Fill(name, 1.)
        #
        h_nevents_cut.Fill('Base', 1.)
        for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
            if utils.basic_dv_selection(tree, dv_index):
                if DV_nTracks < 6:
                    h_ndvs_cut[DV_nTracks].Fill('Base', 1.)
                else:
                    h_ndvs_cut[5].Fill('Base', 1.)
        # Trigger
        if not tree.PassCut1:
            continue
        h_nevents_cut.Fill('Trigger', 1.)
        for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
            if utils.basic_dv_selection(tree, dv_index):
                if DV_nTracks < 6:
                    h_ndvs_cut[DV_nTracks].Fill('Trigger', 1.)
                else:
                    h_ndvs_cut[5].Fill('Trigger', 1.)
        # Filter
        if not tree.PassCut2:
            continue
        h_nevents_cut.Fill('Filter', 1.)
        for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
            if utils.basic_dv_selection(tree, dv_index):
                if DV_nTracks < 6:
                    h_ndvs_cut[DV_nTracks].Fill('Filter', 1.)
                else:
                    h_ndvs_cut[5].Fill('Filter', 1.)
        ##
        #if not tree.MET > 200:
        #    continue
        #h_nevents_cut.Fill('MET200', 1.)
        #for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
        #    if pass_base_dv_selection(tree, dv_index):
        #        if DV_nTracks < 6:
        #            h_ndvs_cut[DV_nTracks].Fill(name, 1.)
        #        else:
        #            h_ndvs_cut[5].Fill(name, 1.)
        ##
        #if not tree.MET > 220:
        #    continue
        #h_nevents_cut.Fill('MET220', 1.)
        #for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
        #    if pass_base_dv_selection(tree, dv_index):
        #        if DV_nTracks < 6:
        #            h_ndvs_cut[DV_nTracks].Fill(name, 1.)
        #        else:
        #            h_ndvs_cut[5].Fill(name, 1.)
        #
        if not tree.MET > 250:
            continue
        h_nevents_cut.Fill('MET250', 1.)
        for dv_index, DV_nTracks in enumerate(tree.DV_nTracks):
            if utils.basic_dv_selection(tree, dv_index):
                if DV_nTracks < 6:
                    h_ndvs_cut[DV_nTracks].Fill('MET250', 1.)
                else:
                    h_ndvs_cut[5].Fill('MET250', 1.)
    #
    output_tfile = TFile(args.outputFile, 'recreate')
    #
    #canvas = TCanvas('canvas', 'canvas', 1200, 800) #h_ndvs_all_clone = h_ndvs_all[2].Clone('unit')
    #h_ndvs_all_clone.Divide(h_ndvs_all[2])
    #h_ndvs_all_clone.SetMaximum(3)
    #h_ndvs_all_clone.SetMinimum(0)
    #h_ndvs_all_clone.Draw()
    #legend = TLegend(0.5, 0.6, 0.85, 0.85)
    h_nevents_cut.Write()
    h_nevents_all.Write()
    for DV_nTracks in range(2, 6):
        h_ndvs_cut[DV_nTracks].Write()
        h_ndvs_all[DV_nTracks].Write()
    #
    #    h_ndvs_cut[DV_nTracks].Sumw2()
    #    h_ndvs_cut[DV_nTracks].Divide(h_ndvs_all[DV_nTracks])
    #    h_ndvs_cut[DV_nTracks].Divide(h_nevents_cut)
    #    h_ndvs_cut[DV_nTracks].Multiply(h_nevents_all)
    #    utils.decorate_histogram(h_ndvs_cut[DV_nTracks], BasicConfig.colors[DV_nTracks])
    #    h_ndvs_cut[DV_nTracks].Draw('same,hist')
    #    legend.AddEntry(h_ndvs_cut[DV_nTracks],
    #                    '('+str(DV_nTracks)+'trk-DVs(cut)/2trk-DVs(all))/(Events(cut)/Events(all))', 'l')
    #utils.decorate_legend(legend)
    #legend.Draw()
    #utils.save_as(canvas, BasicConfig.plotdir + 'nVerts_met_dependency')
    #output = TFile('nVerts_met_dependency.root', 'recreate')
    #canvas.Write()
    output_tfile.Close()
Example #8
0
def plotFiles(filenames,
              variables,
              key,
              weight_plots=False,
              weight_plots_tx=True,
              plot_dict={},
              tex_dict={}):
    # plot a bunch of files and get stats of variables

    ROOT.gROOT.SetBatch(True)
    atlas.SetAtlasStyle()
    print filenames

    # check how many cv folds are represented in this list of files
    cv_folds = filter(
        lambda x: x.find('cv') != -1 and x.endswith('root') and x.find('train')
        != -1, filenames)
    # get the cv_xxx name and only return unique values
    cv_nums = list(
        set(
            map(lambda x: 'cv_' + x.split('_')[-1].replace('.root', ''),
                cv_folds)))
    print cv_nums
    # create a dictionary for the stats of each cv fold
    stats = {}
    # create a dict for the event counts of each cv fold
    event_counts = {}
    for c in cv_nums:
        stats[c] = {'Train': {}, 'Valid': {}}
        event_counts[c] = {'Train': {}, 'Valid': {}}
    # dict for the stats of the full dataset
    stats['Full'] = {}
    event_counts['Full'] = {}

    # add this to the end of the filenames to differentiate between weighted and not weighted
    weight_id = '_weighted' if weight_plots or weight_plots_tx else ''

    for i, f in enumerate(filenames):
        pt4_16 = False
        pt8_12 = False
        if f.find('400_1600') != -1 or f.find('4_16') != -1:
            pt4_16 = True
        elif f.find('800_1200') != -1 or f.find('8_12') != -1:
            pt8_12 = True
        # is this a test or train file?
        file_type = "Full"
        if f.lower().find('train') != -1:
            file_type = "Train"
        elif f.lower().find('test') != -1:
            file_type = 'Valid'
        # what cv split is it? look for cv_xyz.root
        cv_num = ''
        if file_type != 'Full':
            cv_num = 'cv_' + f.split('_')[-1].replace('.root', '')

        # open the file and get the tree
        f_open = ROOT.TFile.Open('folds/' + f)
        tree = f_open.Get('outputTree')
        leg = ROOT.TLegend(0.7, 0.60, 0.9, 0.50)
        leg.SetFillColor(ROOT.kWhite)
        c = ROOT.TCanvas(f)

        total_events = tree.GetEntries()
        signal_events = tree.GetEntries("label==1")
        bkg_events = tree.GetEntries("label==0")

        # this is the full dataset
        if cv_num != '':
            event_counts[cv_num][
                file_type] = '{0:15}  {1:10} {2:14}{3:10}'.format(
                    file_type + ' ' + cv_num, str(signal_events),
                    str(bkg_events), str(total_events))
        else:
            # this is a cv split
            event_counts['Full'] = '{0:15}  {1:10} {2:14}{3:10}'.format(
                file_type, str(signal_events), str(bkg_events),
                str(total_events))

        # create histograms for each variable
        hists = {}

        for v in variables:
            # set up the names for the different histograms
            ispt = False
            if v.find('pt') != -1:
                ispt = True
            hist_full_name = v
            hist_sig_name = 'sig_' + v
            hist_bkg_name = 'bkg_' + v

            # first get the full histogram to get an idea of the combined mean and rms
            if ispt:
                tree.Draw(v + '/1000>>' + v)
            else:
                tree.Draw(v + '>>' + v)
            # pull from the global space
            hist_full = ROOT.gDirectory.Get(hist_full_name).Clone()
            mean = '{0:.4f}'.format(float(hist_full.GetMean()))
            std = '{0:.4f}'.format(float(hist_full.GetRMS()))

            # for some of the variables the labels overlap with the distributions.  Visually inspecting this it doesn't
            # look like there is an easy fix.  I think maybe the best is to rebook the histogram and change the limits to
            # something higher on the x axis....
            # eec c2
            #

            # set up the variable expression that gets used in the Draw function
            if not ispt:
                varexp = v + ">>" + hist_sig_name
            else:
                varexp = v + "/1000>>" + hist_sig_name

            # cut string to select signal only.  An additional string can be added here to apply the weights.
            cutstring = '(label==1)'  #*(weight)'
            #if weight_plots:
            #cutstring += ('*atan(1/weight)')
            cutstring += '*(weight)'
            # create the signal histogram and retrieve it
            tree.Draw(varexp, cutstring)
            mult = 1.0
            xmax = -1
            addBins = 0
            if v.find('eec_c2_1') != -1 or v.find('mu12') != -1 or v.find(
                    'planarflow') != -1 or v.find(
                        'tauwta2tauwta1') != -1 or v.find(
                            'zcut12') != -1 or ispt:
                hist_sig_tmp = ROOT.gDirectory.Get(hist_sig_name).Clone()
                xmax = hist_sig_tmp.GetXaxis().GetXmax()
                width = float(hist_sig_tmp.GetXaxis().GetBinWidth(1))

                # tauwta21 and planarflow need more than 1.2...
                if v.find('tauwta2tauwta1') != -1:
                    mult = 1.65
                elif v.find('planarflow') != -1:
                    mult = 1.9
                elif ispt and pt4_16:
                    mult = 1.35
                else:
                    mult = 1.2
                addBins = float((xmax * mult - xmax)) / width
                hist_sig = ROOT.TH1F('hist_sig', 'hist_sig',
                                     int(hist_sig_tmp.GetNbinsX() + addBins),
                                     hist_sig_tmp.GetXaxis().GetXmin(),
                                     xmax * mult)  # how many bins? :(
                #fill it
                for n in xrange(1, hist_sig_tmp.GetNbinsX() + 1):
                    hist_sig.SetBinContent(n, hist_sig_tmp.GetBinContent(n))
            else:
                hist_sig = ROOT.gDirectory.Get(hist_sig_name).Clone()

            # stats
            sig_mean = '{0:.4f}'.format(float(hist_sig.GetMean()))
            sig_std = '{0:.4f}'.format(float(hist_sig.GetRMS()))
            # normalise
            if hist_sig.Integral() != 0:
                hist_sig.Scale(1 / hist_sig.Integral())
            # set some drawing options and titles
            hist_sig.SetLineColor(2)
            hist_sig.SetTitle('Signal')
            hist_sig.GetXaxis().SetTitle(v)
            # now get the background histogram
            cutstring = cutstring.replace('==1', '==0')
            #cutstring += ('*(weight)')
            #if weight_plots_tx:
            #    cutstring = cutstring.replace('*weight','*atan(1/weight)')
            tree.Draw(varexp.replace('sig', 'bkg'), cutstring)

            if v.find('eec_c2_1') != -1 or v.find('mu12') != -1 or v.find(
                    'planarflow') != -1 or v.find(
                        'tauwta2tauwta1') != -1 or v.find(
                            'zcut12') != -1 or ispt:
                hist_bkg_tmp = ROOT.gDirectory.Get(
                    hist_bkg_name).Clone()  # pull from global
                hist_bkg = ROOT.TH1F('hist_bkg', 'hist_bkg',
                                     int(hist_sig.GetNbinsX()),
                                     hist_sig.GetXaxis().GetXmin(),
                                     xmax * mult)
                for n in xrange(1, hist_bkg_tmp.GetNbinsX() + 1):
                    hist_bkg.SetBinContent(n, hist_bkg_tmp.GetBinContent(n))
            else:
                hist_bkg = ROOT.gDirectory.Get(
                    hist_bkg_name).Clone()  # pull from global
                # stats
            bkg_mean = '{0:.4f}'.format(float(hist_bkg.GetMean()))
            bkg_std = '{0:.4f}'.format(float(hist_bkg.GetRMS()))
            # normalise
            if hist_bkg.Integral() != 0:
                hist_bkg.Scale(1 / hist_bkg.Integral())
            # drawing options and titles
            hist_bkg.SetLineColor(4)
            hist_bkg.SetTitle('Background')
            hist_sig.GetXaxis().SetTitle(v)

            leg.Clear()
            # add the legend entries
            leg.AddEntry(hist_sig, 'Signal', 'l')
            leg.AddEntry(hist_bkg, 'Background', 'l')

            # find the maximum for when we draw them together on a single canvas
            sig_max = hist_sig.GetMaximum()
            bkg_max = hist_bkg.GetMaximum()
            max_val = max(sig_max, bkg_max)
            hist_sig.SetMaximum(max_val * 1.1)
            hist_bkg.SetMaximum(max_val * 1.1)

            if v.strip() in plot_dict.keys():
                hist_sig.GetXaxis().SetTitle(plot_dict[v])
                hist_bkg.GetXaxis().SetTitle(plot_dict[v])
                fnum = 'Full' if cv_num == '' else cv_num
                hist_sig.SetTitle('Fold ' + fnum + ': ' + plot_dict[v])
                hist_bkg.SetTitle('Fold ' + fnum + ': ' + plot_dict[v])

            hist_sig.Draw()
            hist_bkg.Draw('same')

            leg.Draw('same')

            # add the grooming algorithm too
            galg = ROOT.TLatex()
            galg.SetNDC()
            galg.SetTextFont(42)
            galg.SetTextSize(0.03)
            galg.SetTextColor(ROOT.kBlack)
            galg.DrawLatex(
                0.7, 0.71,
                "#splitline{anti-k_{t} R=1.0 jets}{#splitline{Trimmed}{f_{cut}=5%,R_{sub}=0.2}}"
            )

            scl = ROOT.TLatex()
            scl.SetNDC()
            scl.SetTextFont(42)
            scl.SetTextSize(0.03)
            scl.SetTextColor(ROOT.kBlack)
            scl.DrawLatex(0.7, 0.61, "Standardised")

            e = ROOT.TLatex()
            e.SetNDC()
            e.SetTextFont(42)
            e.SetTextSize(0.035)
            e.SetTextColor(ROOT.kBlack)
            e.DrawLatex(0.7, 0.88, "#sqrt{s}=13 TeV")

            m = ROOT.TLatex()
            m.SetNDC()
            m.SetTextFont(42)
            m.SetTextSize(0.035)
            m.SetTextColor(ROOT.kBlack)
            m.DrawLatex(0.7, 0.78, "68% mass window")
            # need to put the pt range on here....
            # okay, so this is nasty and poor form, but I'm super stressed and running out of time
            # to finish my thesis, so whatever.  The algorithm name should have the pt range in it in gev
            # At this point things are narrowed down to the point where we are only considering two
            # pt ranges: 400-1600 GeV or 800-1200 GeV, so just look for those.
            ptrange = ''
            if pt4_16:
                ptrange = '400<p_{T}^{Truth}<1600 GeV'
            elif pt8_12:
                ptrange = '800<p_{T}^{Truth}<1200 GeV'

            if ptrange != '':
                # draw it
                ptl = ROOT.TLatex()
                ptl.SetNDC()
                ptl.SetTextFont(42)
                ptl.SetTextSize(0.035)
                ptl.SetTextColor(ROOT.kBlack)
                ptl.DrawLatex(0.7, 0.83, ptrange)
                #"Internal Simulation");

            if cv_num == '':
                c.SaveAs('fold_plots/' + key + '_Full_' + v + weight_id +
                         '.pdf')
            else:
                c.SaveAs('fold_plots/' + key + '_Full_' + cv_num + '_' + v +
                         weight_id + '.pdf')
            # write the means and std to the stats file
            result = '{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'.format(
                file_type + ' ' + cv_num, str(mean), str(std), str(sig_mean),
                str(sig_std), str(bkg_mean), str(bkg_std))
            # check that this variable has a dictionary entry
            #if v not in stats[cv_num][file_type].keys():
            #stats[cv_num][file_type][v] = {}
            if cv_num != '':  # full dataset
                stats[cv_num][file_type][v] = result
            else:  # cv split
                stats['Full'][v] = result

    # now all of the stats can be written to file!
    # first write the combined stats
    combined_stats = open(
        'fold_stats/combined_stats_' + key + weight_id + '.txt', 'w')
    combined_stats.write('{0:15}  {1:10} {2:14}{3:10}'.format(
        'Sample', 'Signal', 'Background', 'Total') + '\n')
    print event_counts['Full']
    combined_stats.write(str(event_counts['Full']) + '\n')
    # write out all of the cv splits
    for cv in cv_nums:
        for f in ['Train', 'Valid']:
            combined_stats.write(str(event_counts[cv][f]) + '\n')
    combined_stats.write('\n' + '\n')
    # now start doing the variables
    combined_stats.write('\n{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'.
                         format('Variable', 'Mean', 'Std', 'Mean Sig',
                                'Std Sig', 'Mean Bkg', 'Std Bkg') + '\n\n')
    print 'stats full keys', stats['Full'].keys()
    print 'stats keys', stats.keys()
    print 'stats train keys', stats['cv_000']['Train'].keys()
    for v in variables:
        print v
        v = v.strip()
        if not v.strip() in stats['Full'].keys():
            continue
        if v.strip() in tex_dict.keys():
            combined_stats.write(tex_dict[v] + '\n')
        else:
            combined_stats.write(v + '\n')
        combined_stats.write(stats['Full'][v] + '\n')
        for c in cv_nums:
            combined_stats.write(str(stats[c]['Train'][v]) + '\n')
            combined_stats.write(str(stats[c]['Valid'][v]) + '\n')
        combined_stats.write('\n')
    combined_stats.close()

    # write the stats for each cv fold
    for cv in cv_nums:
        print cv
        stats_file = open('fold_stats/' + key + '_' + cv + weight_id + '.txt',
                          'w')
        stats_file.write('{0:15}  {1:10} {2:14}{3:10}'.format(
            'Sample', 'Signal', 'Background', 'Total') + '\n')
        stats_file.write(str(event_counts['Full']) + '\n')
        stats_file.write(str(event_counts[cv]['Train']) + '\n')
        stats_file.write(str(event_counts[cv]['Valid']) + '\n')

        stats_file.write('\n{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'.
                         format('Variable', 'Mean', 'Std', 'Mean Sig',
                                'Std Sig', 'Mean Bkg', 'Std Bkg') + '\n\n')
        # now write each variable
        for v in variables:
            if v.strip() in tex_dict.keys():
                stats_file.write(tex_dict[v] + '\n')
            else:
                stats_file.write(v + '\n')
            stats_file.write(stats['Full'][v] + '\n')
            stats_file.write(stats[cv]['Train'][v] + '\n')
            stats_file.write(stats[cv]['Valid'][v] + '\n')
            stats_file.write('\n')
        stats_file.close()
Example #9
0
def combinedPlotNominal(files, normalize, ratio):

    files = files.split(',')
    dataFile = [file for file in files if ".data." in file][0]
    files.remove(dataFile)
    files.insert(0, dataFile)
    if any("Pythia" in file for file in files):
        pythiaFile = [file for file in files if "Pythia" in file][0]
        files.remove(pythiaFile)
        files.insert(1, pythiaFile)

    if any("Sherpa" in file for file in files):
        sherpaFile = [file for file in files if "Sherpa" in file][0]
        files.remove(sherpaFile)
        files.insert(2, sherpaFile)
    # Save output to directory of first file
    outDir = dataFile[:-5] + '/'
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    if normalize:
        outDir += "combinedPlotNominal_normalized/"
    else:
        outDir += "combinedPlotNominal/"
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    if not os.path.exists(outDir + '/eps'):
        os.mkdir(outDir + '/eps')
    AtlasStyle.SetAtlasStyle()

    gROOT.ProcessLine("gErrorIgnoreLevel = 2000")  #Ignore TCanvas::Print info

    inFiles = []
    nomDirs = []
    for file in files:

        inFiles.append(TFile.Open(file, "READ"))
        keyList = [key.GetName() for key in inFiles[-1].GetListOfKeys()
                   ]  #List of top level objects
        dirList = [key for key in keyList
                   if "Iteration" in key]  #List of all directories

        nomDir = [dir for dir in dirList if "Nominal" in dir]
        if (not len(nomDir) == 1):
            print "Error, nominal directories are ", nomDir
            return
        else:
            nomDirs.append(inFiles[-1].Get(nomDir[0]))

    c1 = TCanvas()

    ################ Set Color Palate ####################3
    # Data, Herwig, Pythia8, Sherpa
    colors = [kBlack, kRed, kBlue, kViolet, kCyan]
    markers = [20, 21, 23, 22, 33, 34]
    #  colorMax = 240.
    #  colorMin = 0. #20.
    #  numInputs = len( nomDirs )
    #  colors = []
    #  if len(nomDirs) == 2:
    #    colors = [kBlack, kRed]
    #  else:
    #    for iDir, nomDir in enumerate(nomDirs):
    #      colorNum = int( colorMin+(colorMax-colorMin)*iDir/numInputs)
    #      colors.append( gStyle.GetColorPalette( colorNum ))

    ##################### Plot All Nominal #################################

    print "Plotting nominal hists "
    oneLine = TF1("zl1", "1", -10000, 10000)
    oneLine.SetTitle("")
    oneLine.SetLineWidth(1)
    oneLine.SetLineStyle(2)
    oneLine.SetLineColor(kBlack)

    histList = [key.GetName() for key in nomDirs[0].GetListOfKeys()]
    for histName in histList:
        if "prof_" in histName or "ptSlice" in histName:
            continue

        tmpHist = nomDirs[0].Get(histName)
        if not type(tmpHist) == TH1F and not type(
                tmpHist) == TH1D and not type(
                    tmpHist) == TGraphErrors:  #Can't draw bands if not 1D
            continue

        leg = TLegend(0.65, 0.72, 0.9, 0.93)
        leg.SetFillStyle(0)
        leg.SetTextFont(42)
        #!    leg = TLegend(0.83, 0.15, 0.99, 0.95)
        if ratio:
            pad1 = TPad("pad1", "", 0, 0.32, 1, 1)
            pad2 = TPad("pad2", "", 0, 0, 1, 0.32)
            #!      pad1 = TPad("pad1", "", 0, 0.3, 0.83, 1)
            #!      pad2 = TPad("pad2", "", 0, 0, 0.83, 0.3)
            pad1.SetBottomMargin(0.01)
            pad2.SetTopMargin(0)
            pad2.SetBottomMargin(0.45)
            pad1.Draw()
            pad2.Draw()
        else:
            #!      pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
            pad1.Draw()
        pad1.cd()

        nomHists = []
        drawString = ""
        maxVal = []
        maxBinX = []

        for iDir, nomDir in enumerate(nomDirs):
            thisFileStr = files[iDir].split('.')[2]
            if "all" in thisFileStr:
                thisFileStr = files[iDir].split('.')[1]

            nomHist = nomDir.Get(histName)
            nomHist.SetName(nomHist.GetName())
            if "Beta" in histName:
                nomHist.Rebin(2)
            if "alpha" in histName:
                nomHist.Rebin(4)
            if "ptAsym" in histName:
                nomHist.Rebin(4)

            if "Eta" in histName and not type(nomHist) == TGraphErrors:
                nomHist.Rebin(4)

            if ("recoilPt" in histName):
                for iBin in range(1, nomHist.GetNbinsX() + 1):
                    nomHist.SetBinContent(
                        iBin,
                        nomHist.GetBinContent(iBin) /
                        nomHist.GetBinWidth(iBin))

            if normalize and not type(
                    nomHist) == TGraphErrors and nomHist.Integral() > 0.:
                nomHist.Scale(1. / nomHist.Integral())

            if "Sherpa" in files[iDir] and "MJB" in histName:
                for iBin in range(31, nomHist.GetNbinsX() + 1):
                    nomHist.SetBinContent(iBin, 0)
                    nomHist.SetBinError(iBin, 0)

            nomHist.SetLineColor(colors[iDir])
            nomHist.SetMarkerColor(colors[iDir])
            nomHist.SetMarkerStyle(markers[iDir])
            thisEntry = leg.AddEntry(
                nomHist,
                thisFileStr.replace('d', 'D').replace(
                    'Sherpa', 'Sherpa 2.1').replace('Herwig', 'Herwig++'),
                "lp")
            thisEntry.SetTextColor(colors[iDir])
            #nomHist.SetMinimum(0.9)
            #      if( "jetPt" in histName or "jetEnergy" in histName):
            #        maxBinX.append(nomHist.FindLastBinAbove(0)+1)
            #        nomHist.SetMaximum(1.5*nomHist.GetMaximum())
            #        nomHist.SetMinimum(0.000101)
            #      else:
            nomHist.SetMaximum(1.5 * nomHist.GetMaximum())
            #nomHist.SetMinimum(0.000101)
            if ("MJB" in histName):
                nomHist.SetMinimum(0.0000101)
                #nomHist.GetXaxis().SetRangeUser( 500, 2800 ) #!!public
                #nomHist.SetMaximum(1.06) #!!public
                #nomHist.SetMinimum(0.9701) #!!public
                nomHist.GetXaxis().SetRangeUser(300, 2800)
                nomHist.SetMaximum(1.2)
                nomHist.SetMinimum(0.8999)
                nomHist.GetXaxis().SetMoreLogLabels(True)
                nomHist.GetYaxis().SetTitle(
                    "#LT p_{T}^{lead jet}/p_{T}^{recoil} #GT")
                nomHist.GetYaxis().SetTitleSize(0.09)
                nomHist.GetYaxis().SetTitleOffset(0.7)
                nomHist.GetYaxis().SetLabelSize(0.06)
                nomHist.GetYaxis().SetLabelOffset(0.01)
                nomHist.SetMarkerSize(.8)
                nomHist.SetLineWidth(1)
            elif ("Pt" in histName):
                nomHist.GetYaxis().SetTitle("AU")
                if ("jet0" in histName):
                    nomHist.GetXaxis().SetRangeUser(200, 2000)
                else:
                    nomHist.GetXaxis().SetRangeUser(0, 800)
            else:
                nomHist.GetYaxis().SetTitle("AU")
            if ("recoilPt" in histName):
                nomHist.GetYaxis().SetTitle("1/N dp_{T}^{recoil}/dN")
                nomHist.GetXaxis().SetRangeUser(300, 3000)
            if not type(nomHist) == TGraphErrors:
                #drawString = "histsamep"
                drawString = "psame"
            else:
                drawString = "apsame"
                nomHist.SetMarkerStyle(33)
                nomHist.SetMarkerSize(1.5)
                nomHist.SetLineWidth(4)

            nomHists.append(nomHist)
            maxVal.append(nomHist.GetMaximum())

        maxDir = maxVal.index(max(maxVal))
        nomHists[maxDir].Draw(drawString)
        if maxBinX:
            maxBinX = max(maxBinX)
            for iDir, nomDir in enumerate(nomDirs):
                nomHists[iDir].GetXaxis().SetRange(1, maxBinX)
        for iDir, nomDir in enumerate(nomDirs):
            nomHists[iDir].Draw(drawString)
        oneLine.Draw("same")
        for iDir, nomDir in enumerate(nomDirs):
            nomHists[iDir].Draw(drawString)
        nomHists[0].Draw(drawString)  ## Draw data on top

        if ratio:
            pad2.cd()
            ratioHists = []

            for iDir in range(1, len(nomDirs)):
                #ratioHists.append( nomHists[0].Clone() )
                #ratioHists[iDir-1].Add(nomHists[iDir], -1.)
                #ratioHists[iDir-1].Divide(nomHists[iDir])
                ratioHists.append(nomHists[iDir].Clone())
                ratioHists[iDir - 1].Divide(nomHists[0])

                ratioHists[iDir - 1].SetMarkerColor(colors[iDir])
                ratioHists[iDir - 1].SetMarkerStyle(markers[iDir])
                ratioHists[iDir - 1].SetLineColor(colors[iDir])
                if iDir == 1:
                    ratioHists[iDir - 1].SetMaximum(2)
                    ratioHists[iDir - 1].SetMinimum(0.5)
                    ratioHists[iDir - 1].GetXaxis().SetLabelOffset(.015)
                    ratioHists[iDir - 1].GetXaxis().SetTitleOffset(1.3)
                    ratioHists[iDir - 1].GetXaxis().SetLabelSize(0.13)
                    ratioHists[iDir - 1].GetXaxis().SetTitleSize(0.16)
                    #          ratioHists[iDir-1].GetXaxis().SetTitle(nomHists[0].GetXaxis().GetTitle());
                    ratioHists[iDir - 1].GetXaxis().SetMoreLogLabels()
                    ratioHists[iDir - 1].GetYaxis().SetLabelSize(0.13)
                    ratioHists[iDir - 1].GetYaxis().SetTitleSize(0.16)
                    ratioHists[iDir - 1].GetYaxis().SetLabelOffset(.01)
                    ratioHists[iDir - 1].GetYaxis().SetTitleOffset(0.37)
                    #ratioHists[iDir-1].GetYaxis().SetTitle("Significance")
                    ratioHists[iDir - 1].GetYaxis().SetTitle("   MC / Data")
                    ratioHists[iDir - 1].GetYaxis().SetNdivisions(7)
                    if ("Pt" in histName):
                        if ("jet0" in histName):
                            ratioHists[iDir - 1].GetXaxis().SetRangeUser(
                                200, 2000)
                        else:
                            ratioHists[iDir - 1].GetXaxis().SetRangeUser(
                                0, 800)
                    if ("MJB" in histName):
                        #ratioHists[iDir-1].GetXaxis().SetRangeUser( 500, 2800 ) #!!public
                        ratioHists[iDir - 1].GetXaxis().SetRangeUser(300, 2500)
                        ratioHists[iDir - 1].SetMaximum(1.05)
                        ratioHists[iDir - 1].SetMinimum(0.95)
                        ratioHists[iDir - 1].GetXaxis().SetTitle(
                            "p_{T}^{recoil} [GeV]")
                    if ("recoilPt" in histName):
                        ratioHists[iDir - 1].GetXaxis().SetRangeUser(300, 3000)
                        ratioHists[iDir - 1].GetXaxis().SetTitle(
                            "p_{T}^{recoil} [GeV]")

        #     if( "jetBeta" in histName):
        #       ratioHists[iDir-1].SetMaximum(1)
        #       ratioHists[iDir-1].SetMinimum(-1)

            ratioHists[0].Draw("p")
            oneLine.Draw("same")
            for iDir in range(1, len(nomDirs)):
                ratioHists[iDir - 1].Draw("psame")

        c1.cd()
        leg.Draw()
        AtlasStyle.ATLAS_LABEL(0.2, 0.88, 1, "    Preliminary")
        AtlasStyle.myText(0.2, 0.82, 1, "#sqrt{s} = 13 TeV, 3.3 fb^{-1}")
        AtlasStyle.myText(0.2, 0.76, 1, "Multijet Events")
        typeText = "anti-k_{t} R = 0.4"
        if "_LC_" in dataFile:
            typeText += ", LC+JES (in-situ)"
        else:
            typeText += ", EM+JES (in-situ)"
        AtlasStyle.myText(0.2, 0.7, 1, typeText)
        AtlasStyle.myText(0.2, 0.64, 1, "#left|#eta^{lead jet}#right| < 1.2")
        #    AtlasStyle.myText(0.1,0.75,1, "m_{jj} Correction")

        if "MJB" in histName:
            pad1.SetLogx()
            if ratio:
                pad2.SetLogx()
        else:
            pad1.SetLogx(0)
            if ratio:
                pad2.SetLogx(0)
        #if "Pt" in histName or "alpha" in histName:
        pad1.SetLogy()
        c1.SaveAs(outDir + nomHist.GetName() + "_logy.png")
        c1.SaveAs(outDir + "/eps/" + nomHist.GetName() + "_logy.eps")
        pad1.SetLogy(0)
        c1.SaveAs((outDir + nomHist.GetName() + ".png"))
        c1.SaveAs((outDir + "/eps/" + nomHist.GetName() + ".eps"))
        c1.SaveAs((outDir + "/eps/" + nomHist.GetName() + ".pdf"))
        c1.Clear()

    for inFile in inFiles:
        inFile.Close()
Example #10
0
def plotMJBvEta(file, binnings):

    if not "MJB" in file:
        print "Error:  plotMJBvEta.py must run on a file with  \"MJB\", but was given", file
        print "Exiting..."
        return

    binnings = binnings.split(',')
    for iBinning in range(len(binnings)):
        if not binnings[iBinning][0] == '_':
            binnings[iBinning] = '_' + binnings[iBinning]
    print "Binnings are ", binnings

    outDir = file[:-5] + '/'
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    outDir += "plotMJBvEta/"
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    AtlasStyle.SetAtlasStyle()

    gROOT.ProcessLine("gErrorIgnoreLevel = 2000")  #Ignore TCanvas::Print info

    inFile = TFile.Open(file, "READ")
    keyList = [key.GetName()
               for key in inFile.GetListOfKeys()]  #List of top level objects
    dirList = [key for key in keyList
               if "Iteration" in key]  #List of all directories

    #nomDir = [dir for dir in dirList if "NoCorr" in dir]
    nomDir = [dir for dir in dirList if "Nominal" in dir]
    if (not len(nomDir) == 1):
        print "Error, nominal directories are ", nomDir
        return
    else:
        nomDir = inFile.Get(nomDir[0])

    c1 = TCanvas()

    histList = [
        key.GetName() for key in nomDir.GetListOfKeys()
        if "MJB" in key.GetName()
    ]

    for binning in binnings:
        leg = TLegend(0.80, 0.15, 0.97, 0.95)
        pad1 = TPad("pad1", "", 0, 0, 0.85, 1)
        pad1.Draw()
        pad1.cd()
        hists = []
        counter = 0
        for histName in histList:
            if not binning in histName:
                continue
            counter += 1
            hists.append(nomDir.Get(histName))
            hists[-1].SetMarkerColor(
                gStyle.GetColorPalette(70 * (len(hists) - 1)))
            hists[-1].SetLineColor(
                gStyle.GetColorPalette(70 * (len(hists) - 1)))
            hists[-1].GetYaxis().SetRangeUser(0.9, 1.1)
            hists[-1].GetXaxis().SetMoreLogLabels(True)
            if ("DoubleMJB" in histName):
                hists[-1].GetXaxis().SetRangeUser(300, 2000)
            if (counter == 1):
                if type(hists[-1]) == TGraph:
                    hists[-1].Draw("apXl")
                else:
                    hists[-1].Draw()
                leg.AddEntry(hists[-1], "Nominal", "l")
            else:
                if type(hists[-1]) == TGraph:
                    hists[-1].Draw("same pXl")
                else:
                    hists[-1].Draw("same")
                leg.AddEntry(hists[-1], "Eta " + str(counter), "l")

        c1.cd()
        leg.Draw()
        pad1.SetLogx()
        c1.SaveAs(outDir + "/MJBvEta" + binning + ".png", "png")
        pad1.Clear()
        c1.Clear()

    inFile.Close()
Example #11
0
def plotActualSysRatios(file):

    outDir = file[:-5] + '/'
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    outDir += "plotActualSysRatios/"
    if not os.path.exists(outDir):
        os.mkdir(outDir)
    AtlasStyle.SetAtlasStyle()

    gROOT.ProcessLine("gErrorIgnoreLevel = 2000")  #Ignore TCanvas::Print info

    inFile = TFile.Open(file, "READ")
    keyList = [key.GetName()
               for key in inFile.GetListOfKeys()]  #List of top level objects
    dirList = [key for key in keyList
               if "Iteration" in key]  #List of all directories

    #nomDir = [dir for dir in dirList if "NoCorr" in dir]
    nomDir = [dir for dir in dirList if "Nominal" in dir]
    if (not len(nomDir) == 1):
        print "Error, nominal directories are ", nomDir
        return
    else:
        nomDir = inFile.Get(nomDir[0])

    c1 = TCanvas()

    ##################### Plot Systematic Difference  #################################
    sysDirNameList = [
        dirName for dirName in dirList if not "Nominal" in dirName
    ]

    sysDirList = []
    for sysDirName in sysDirNameList:
        sysDirList.append(inFile.Get(sysDirName))

    ## This removes extra MJB systematics
    MJBsToUse = [
        "a40", "a20", "b15", "b05", "pta90", "pta70", "ptt30", "ptt20"
    ]
    sysDirList = [
        sysDir for sysDir in sysDirList
        if (not "MJB" in sysDir.GetName() or any(MJBtouse in sysDir.GetName()
                                                 for MJBtouse in MJBsToUse))
    ]
    sysDirList = [
        sysDir for sysDir in sysDirList if not "Nominal" in sysDir.GetName()
    ]

    ## Combine systematics in types ##

    #sysTypesToUse = ["Zjet_dPhi","Zjet_MC","Zjet_MuScale","Zjet_MuSmearID","Zjet_MuSmearMS","Zjet_KTerm","Zjet_Veto","Zjet_Stat1","Zjet_Stat2","Zjet_Stat3","Zjet_Stat4","Zjet_Stat5","Zjet_Stat6","Zjet_Stat7","Zjet_Stat8","Zjet_Stat9","Zjet_Stat10","Zjet_Stat11","Zjet_Stat12","Zjet_Stat13"]
    #sysTypesToUse = ["Gjet_dPhi","Gjet_Generator","Gjet_OOC","Gjet_Purity","Gjet_Veto","Gjet_Stat1","Gjet_Stat2","Gjet_Stat3","Gjet_Stat4","Gjet_Stat5","Gjet_Stat6","Gjet_Stat7","Gjet_Stat8","Gjet_Stat9","Gjet_Stat10","Gjet_Stat11","Gjet_Stat12","Gjet_Stat13","Gjet_Stat14","Gjet_Stat15"]

    sysTypesToUse = [
        "Zjet", "Gjet", "Flavor", "EtaIntercalibration", "PunchThrough",
        "Pileup", "MCType", "MJB"
    ]
    #sysTypesToUse = ["Zjet_Jvt", "Zjet_ElecESZee", "Zjet_ElecEsmear", "Gjet_Jvt", "Gjet_GamESZee", "LAr_Esmear"]
    #  sysTypesToUse = ["MJB_a", "MJB_b", "MJB_ptt", "MJB_pta", "MCType"]
    # SingleParticle, RelativeNonClosure, Pileup, BJES, PunchThrough

    sysTypes = []
    for sysType in sysTypesToUse:
        if any(sysType in sysDir for sysDir in sysDirNameList):
            sysTypes.append(sysType)

    if len(sysTypes) == 0:
        print "Error, found no systematics!!"
        exit(1)
    if len(sysTypes) > 1:
        sysTypes.append("All")
    if "All" in sysTypes:
        colorOffset = 240. / (len(sysTypes) - 1)
    else:
        colorOffset = 240. / len(sysTypes)

    #print "Plotting systematic differences "
    #print sysTypes

    histList = [key.GetName() for key in sysDirList[0].GetListOfKeys()]

    for histName in histList:
        if "prof_" in histName or "ptSlice" in histName:
            continue

        nomHist = sysDirList[0].Get(histName)
        nomHist.SetName(nomHist.GetName())

        if not type(nomHist) == TH1F and not type(
                nomHist) == TH1D:  #Can't draw bands if not 1D
            continue

        ## Get list of systematic histograms ##
        fullHistList = []
        for thisSysDir in sysDirList:
            fullHistList.append(thisSysDir.Get(histName))
            fullHistList[-1].SetDirectory(0)

        ### Setup Plot ###
        leg = TLegend(0.83, 0.15, 0.99, 0.95)
        pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
        pad1.Draw()
        pad1.cd()

        ## An empty histogram for plot settings ##
        settingsHist = nomHist.Clone()
        for iBin in range(1, settingsHist.GetNbinsX() + 1):
            settingsHist.SetBinContent(iBin, 0.)
            settingsHist.SetBinError(iBin, 0.)

        settingsHist.GetYaxis().SetTitle("Rel. Uncert. " +
                                         nomHist.GetYaxis().GetTitle())
        #settingsHist.GetYaxis().SetTitle( "Rel. Uncert. on "+settingsHist.GetYaxis().GetTitle())
        settingsHist.SetLineColor(kWhite)
        settingsHist.SetMarkerColor(kWhite)
        settingsHist.GetYaxis().SetRangeUser(-1., 2.)
        if ("MJB" in histName):
            settingsHist.GetXaxis().SetRangeUser(300, 3000)
            settingsHist.GetXaxis().SetMoreLogLabels(True)
            settingsHist.GetYaxis().SetRangeUser(-0.03, 0.03)

        settingsHist.Draw()

        sysHistList = []
        for iTopSys, topSysName in enumerate(sysTypes):

            if topSysName == 'All':
                subHistList = [
                    thisHist
                    for thisDir, thisHist in zip(sysDirList, fullHistList)
                    if any(otherTopName in thisDir.GetName()
                           for otherTopName in sysTypes)
                ]
            else:
                subHistList = [
                    thisHist
                    for thisDir, thisHist in zip(sysDirList, fullHistList)
                    if topSysName in thisDir.GetName()
                ]

            sysHistUp, sysHistDn = getCombinedSysHist(nomHist, subHistList,
                                                      topSysName)

            if topSysName == 'All':
                color = kBlack
            else:
                color = gStyle.GetColorPalette(int(colorOffset *
                                                   (iTopSys + 1)))

            sysHistUp.SetLineColor(color)
            sysHistUp.SetMarkerColor(color)
            sysHistUp.SetMarkerSize(0.8)
            sysHistDn.SetLineColor(color)
            sysHistDn.SetMarkerColor(color)
            sysHistDn.SetMarkerSize(0.8)

            sysHistUp.Draw("same hist lp")
            sysHistDn.Draw("same hist lp")
            if (topSysName == "EtaIntercalibration"):
                leg.AddEntry(sysHistUp, "#eta-inter", "lp")
            elif (topSysName == "PunchThrough"):
                leg.AddEntry(sysHistUp, "PunchTh", "lp")
            else:
                leg.AddEntry(sysHistUp, topSysName, "lp")

            # Save them in a list
            sysHistList.append(sysHistUp)
            sysHistList.append(sysHistDn)

        c1.cd()
        leg.Draw()
        if ("MJB" in histName):
            pad1.SetLogx()
        c1.SaveAs(outDir + nomHist.GetName() + "_fracDiff.png")
        c1.Clear()

    inFile.Close()
Example #12
0
def plotNominal(file, f_plotSys, f_addGagik):

  outDir = file[:-5]+'/'
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  outDir += "plotNominal/"
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  if not os.path.exists(outDir+'/eps'):
    os.mkdir(outDir+'/eps')
  AtlasStyle.SetAtlasStyle()

  gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info

  inFile = TFile.Open(file, "READ");
  keyList = [key.GetName() for key in inFile.GetListOfKeys()] #List of top level objects
  dirList = [key for key in keyList if "Iteration" in key] #List of all directories

  #nomDir = [dir for dir in dirList if "NoCorr" in dir]
  nomDir = [dir for dir in dirList if "Nominal" in dir]
  if( not len(nomDir) == 1):
    print "Error, nominal directories are ", nomDir
    return
  else:
    nomDir = inFile.Get( nomDir[0] )

  c1 = TCanvas()



##################### Plot All Nominal With Systematic Bands  #################################

  print "Plotting nominal hists "

  if(f_plotSys):
    sysDirNameList = [dir for dir in dirList if not "Nominal" in dir]
    sysDirList = []
    for sysDirName in sysDirNameList:
      sysDirList.append( inFile.Get(sysDirName) )

    ## Combine systematics in types ##
    #sysTypes = ["MJB_a", "MJB_b", "MJB_ptt", "MJB_pta", "Flavor", "EtaIntercalibration"]
    sysTypes = ["ZJ", "GJ", "MJB", "Flavor", "EtaIntercalibration", "All"]
    if "All" in sysTypes:
      colorOffset = 240./(len(sysTypes)-1)
    else:
      colorOffset = 240./len(sysTypes)

  histList = [key.GetName() for key in nomDir.GetListOfKeys()]
  for histName in histList:
    if "prof_" in histName or "ptSlice" in histName:
      continue

    nomHist = nomDir.Get( histName )
    nomHist.SetName(nomHist.GetName())

    if not type(nomHist) == TH1F and not type(nomHist) == TH1D and not type(nomHist) == TGraphErrors:  #Can't draw bands if not 1D
      continue

    leg = TLegend(0.83, 0.15, 0.99, 0.95)
    pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
    pad1.Draw()
    pad1.cd()
    leg.AddEntry( nomHist, "Nominal", "lp" )
    #nomHist.SetMinimum(0.9)
    if( "Pt" in histName or "Energy" in histName):
      nomHist.GetXaxis().SetRange(nomHist.FindFirstBinAbove(0), nomHist.FindLastBinAbove(0)+1 )
    nomHist.SetMaximum(1.5*nomHist.GetMaximum())
    nomHist.SetMinimum(0.0001)
    if( "MJB" in histName) :
      nomHist.GetXaxis().SetRangeUser( 300, 2500 )
      nomHist.SetMaximum(1.1)
      nomHist.SetMinimum(0.9)
      nomHist.GetXaxis().SetMoreLogLabels(True)
#    nomHist.SetMarkerSize(.75)
    if( "recoilPt" in histName):
      nomHist.GetYaxis().SetTitle("Entries / GeV")
      nomHist.GetXaxis().SetRangeUser( 500, 3100 )
      if( "recoilPt" in histName):
        for iBin in range(1, nomHist.GetNbinsX()+1):
          nomHist.SetBinContent(iBin, nomHist.GetBinContent(iBin)/ nomHist.GetBinWidth(iBin))
    nomHist.Draw()
    if not type(nomHist) == TGraphErrors:
      nomHist.Draw("p")
    else:
      nomHist.SetMarkerStyle(33)
      nomHist.SetMarkerSize(1.5)
      nomHist.SetLineWidth(4)
      nomHist.Draw("ap")

    if(f_plotSys):
      ## Get list of systematic histograms ##
      fullHistList = []
      for thisSysDir in sysDirList:
        fullHistList.append( thisSysDir.Get(histName) )
        fullHistList[-1].SetDirectory(0)

      ## Add systematic bands ##
      sysHistList = []
      for iTopSys, topSysName in enumerate(sysTypes):

        if topSysName == 'All':
          subHistList = [thisHist for thisName, thisHist in zip(sysDirNameList, fullHistList) if any(otherTopName in thisName for otherTopName in sysTypes) ]
        else:
          subHistList = [thisHist for thisName, thisHist in zip(sysDirNameList, fullHistList) if topSysName in thisName]

        sysHistUp, sysHistDn = plotSysRatios.getCombinedSysHist(nomHist, subHistList, topSysName)
        for iBin in range(1, nomHist.GetNbinsX()+1):
          sysHistUp.SetBinContent( iBin, nomHist.GetBinContent(iBin)*(1.+sysHistUp.GetBinContent(iBin)) )
          sysHistDn.SetBinContent( iBin, nomHist.GetBinContent(iBin)*(1.+sysHistDn.GetBinContent(iBin)) )

        if topSysName == 'All':
          color = kBlack
        else:
          color = gStyle.GetColorPalette(int(colorOffset*(iTopSys+1)))

        sysHistUp.SetLineColor(color)
        sysHistUp.SetMarkerColor(color)
        sysHistUp.SetMarkerSize(1.5)
        sysHistUp.SetMarkerStyle(34)
        sysHistDn.SetLineColor(color)
        sysHistDn.SetMarkerColor(color)
        sysHistDn.SetMarkerSize(1.5)
        sysHistDn.SetMarkerStyle(34)
        if( "Pt" in histName or "Energy" in histName):
          sysHistUp.GetXaxis().SetRange(sysHistUp.FindFirstBinAbove(0), sysHistUp.FindLastBinAbove(0)+1)
          sysHistDn.GetXaxis().SetRange(sysHistDn.FindFirstBinAbove(0), sysHistDn.FindLastBinAbove(0)+1)
        if not type(nomHist) == TGraphErrors:
          sysHistUp.Draw("same hist l")
          sysHistDn.Draw("same hist l")
        else:
          sysHistUp.SetMarkerStyle(4)
          sysHistUp.SetMarkerSize(1.2)
          sysHistUp.Draw("same l")
          sysHistDn.SetMarkerStyle(4)
          sysHistDn.SetMarkerSize(1.2)
          sysHistDn.Draw("same l")

        if( topSysName == "EtaIntercalibration"):
          leg.AddEntry( sysHistUp, "EIC", "lp")
        else:
          leg.AddEntry( sysHistUp, topSysName, "lp")

        # Save them in a list
        sysHistList.append( sysHistUp )
        sysHistList.append( sysHistDn )

#    ### Add MC for Data MJB! ###
#    if( "data" in file and "MJB" in histName ):
#      filePath = os.path.dirname( file )
#      if( "initial" in file):
#        mcFile = glob.glob( filePath+"/*mc14*MJB_initial.root")
#      elif( "final" in file):
#        mcFile = glob.glob( filePath+"/*mc14*MJB_final.root")
#      if len(mcFile) == 1:
#        mcFile = TFile.Open(mcFile[0], "READ")
#        mcKeyList = [key.GetName() for key in mcFile.GetListOfKeys()] #List of top level objects
#        mcDirList = [key for key in mcKeyList if "Iteration" in key] #List of all directories
#        #mcDir = [dir for dir in dirList if "NoCorr" in dir]
#        mcDir = [dir for dir in dirList if "Nominal" in dir]
#        if len(mcDir) == 1:
#          mcDir = mcFile.Get( mcDir[0] )
#          mcHist = mcDir.Get(histName)
#          mcHist.SetMarkerColor(kRed)
#          mcHist.SetLineColor(kRed)
#          mcHist.SetMarkerStyle(33)
#          mcHist.SetMarkerSize(1.3)
#          mcHist.Draw("same p")
#          leg.AddEntry(mcHist, "MC", "lp")
#
#    ### Add Gagik for DoubleMJB! ###
#    if( f_addGagik ):
#      if( "DoubleMJB" in histName ):
#        filePath = os.path.dirname( file )
#        gagikFile = TFile.Open(filePath+"/ThirdCycle.EM4.sherpa.1.v11.root", "READ")
#        gagikHist = gagikFile.Get("g_DoMC_fmean_vs_recoil")
#        gagikHist.SetMarkerColor(kRed)
#        gagikHist.SetLineColor(kRed)
#        gagikHist.SetMarkerStyle(33)
#        gagikHist.SetMarkerSize(1.3)
#        gagikHist.Draw("same p")
#        leg.AddEntry(gagikHist, "8 TeV", "lp")
#

    nomHist.Draw("same p")
    c1.cd()
    leg.Draw()
    AtlasStyle.ATLAS_LABEL(0.2,0.88, 1,"    Internal")
    AtlasStyle.myText(0.2,0.82,1, "#sqrt{s} = 13 TeV, 3.3 fb^{-1}")

    if "Pt" in histName or "alpha" in histName:
      pad1.SetLogy()
      c1.SaveAs(outDir+nomHist.GetName()+"_logy.png" )
      c1.SaveAs(outDir+'/eps/'+nomHist.GetName()+"_logy.eps" )
      pad1.SetLogy(0)
    if "MJB" in histName:
      pad1.SetLogx()
    c1.SaveAs((outDir+nomHist.GetName()+".png") )
    c1.SaveAs((outDir+'/eps/'+nomHist.GetName()+".eps") )
    c1.Clear()



  inFile.Close()
Example #13
0
def draw_cross_section_limit(tree, mass_g, flavor_of_sample='MET_TLJets'):
    AtlasStyle.SetAtlasStyle()
    entries = tree.GetEntries()
    current_delta_mass = 0
    upper_limits = []
    dM = []
    index = -1
    for entry in range(entries):
        tree.GetEntry(entry)
        if tree.mGluino == mass_g:
            if current_delta_mass != tree.deltaM:
                print('*** {0}, {1}'.format(tree.mGluino, tree.deltaM))
                upper_limits.append(TGraphErrors())
                dM.append(int(tree.deltaM))
                index += 1
                point = 0
            current_delta_mass = tree.deltaM
            #if current_delta_mass < 100:
            #    continue
            upper_limits[index].SetPoint(point, tree.ctau * 1e3, tree.xsUL)
            upper_limits[index].SetPointError(
                point, 0, tree.xsUL * tree.effRelStatErr +
                tree.xsUL * tree.effRelSystErr)
            point += 1
            print(tree.ctau, tree.xsUL)
    canvas = TCanvas('c', 'c', 1000, 800)
    canvas.SetLogx()
    canvas.SetLogy()
    #upper_limits[0].SetMinimum(0.8)
    #upper_limits[0].SetMinimum(0.05)
    #upper_limits[0].SetMaximum(30000)
    #upper_limits[0].GetXaxis().SetRangeUser(0.9, 310)
    #upper_limits[0].GetXaxis().SetTitle('c#tau [mm]')
    #upper_limits[0].GetYaxis().SetTitle('Cross Section [fb]')
    #upper_limits[0].Draw('A3')
    #if mass_g == 1400:
    #    upper_limits[1].RemovePoint(0)
    #upper_limits[0].RemovePoint(2)
    #upper_limits[1].RemovePoint(0)
    h_xs = TH1F('xs', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310)
    h_xs_line = TH1F('xs_line', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9,
                     310)
    print(mc.mass_xs_err[mass_g]['xs'] * 1e3)
    for bin in range(1, 1000 + 1):
        h_xs.SetBinContent(bin, mc.mass_xs_err[mass_g]['xs'] * 1e3)
        h_xs_line.SetBinContent(bin, mc.mass_xs_err[mass_g]['xs'] * 1e3)
        h_xs.SetBinError(
            bin, mc.mass_xs_err[mass_g]['xs'] * 1e3 *
            mc.mass_xs_err[mass_g]['xs_err'] * 0.01)
    h_xs.SetMarkerSize(0)
    h_xs.SetFillStyle(3001)
    h_xs.SetFillColor(kGray + 2)
    #h_xs.SetMinimum(0.8)
    h_xs.SetMinimum(0.05)
    h_xs.SetMaximum(30000)
    #h_xs.Draw('same,e2')
    h_xs.Draw('e2')
    h_xs_line.SetLineColor(kGray + 3)
    h_xs_line.SetLineStyle(2)
    h_xs_line.Draw('same')
    legend = TLegend(0.60, 0.75, 0.83, 0.90)
    for ii, upper_limit in enumerate(upper_limits):
        #upper_limit.RemovePoint(0)
        upper_limit.SetMarkerSize(0)
        upper_limit.SetFillStyle(3001)
        index = ii
        if dM[ii] == 130:
            index = 1
        elif dM[ii] == 80:
            index = 2
            continue
        elif dM[ii] == 50:
            index = 3
        elif dM[ii] == 30:
            index = 4
        upper_limit.SetFillColor(BasicConfig.colors[index + 1])
        upper_limit.SetLineColor(BasicConfig.colors[index + 1])
        upper_limit.Draw('3,same')
        #upper_limit.Draw('c,same')
        #if dM[ii] > 100:
        #    #legend.AddEntry(upper_limit, 'M_{#tilde{g}} = '+str(mass_g)+' GeV, #DeltaM = '+str(dM[ii])+' GeV', 'lf')
        #    legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf')
        legend.AddEntry(upper_limit, '#DeltaM = ' + str(dM[ii]) + ' GeV', 'lf')
    utils.decorate_legend(legend)
    legend.Draw()
    AtlasStyle.ATLASLabel(0.19, 0.87, 'Work in Progress')
    AtlasStyle.myText(0.20, 0.79, kBlack,
                      '#sqrt{s} = 13 TeV, #int L dt = 30 fb^{-1}', 0.035)
    AtlasStyle.myText(
        0.20, 0.73, kBlack,
        'Split-SUSY Model, M_{#tilde{g}} = ' + str(mass_g) + ' GeV', 0.032)
    #AtlasStyle.myText(0.20, 0.67, kBlack, 'M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.035)
    utils.save_as(
        canvas, BasicConfig.plotdir + 'xs_limit_mGluino' + str(mass_g) +
        flavor_of_sample)
Example #14
0
def draw_cross_section_limit_dM(tree, dM, flavor_of_sample='MET_TLJets'):
    AtlasStyle.SetAtlasStyle()
    entries = tree.GetEntries()
    current_mass = 0
    upper_limits = []
    mass_g = []
    index = -1
    point = 0
    for entry in range(entries):
        tree.GetEntry(entry)
        if tree.deltaM == dM or (dM == 'large'
                                 and tree.mGluino - tree.deltaM == 100):
            if current_mass != tree.mGluino:
                print('*** {0}, {1}'.format(tree.mGluino, tree.deltaM))
                upper_limits.append(TGraphErrors())
                mass_g.append(int(tree.mGluino))
                index += 1
                point = 0
            current_mass = tree.mGluino
            upper_limits[index].SetPoint(point, tree.ctau * 1e3, tree.xsUL)
            #upper_limits[index].SetPointError(point, 0, tree.xsUL*tree.effRelStatErr+tree.xsUL*tree.effRelSystErr)
            point += 1
            print(tree.ctau, tree.xsUL)
    canvas = TCanvas('c', 'c', 1000, 800)
    canvas.SetLogx()
    canvas.SetLogy()
    h_xs = TH1F('xs', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310)
    h_xs.GetYaxis().SetRangeUser(0.1, 100)
    h_xs.Draw()
    #h_xs_line = TH1F('xs_line', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310)
    #print(mc.mass_xs_err[mass_g]['xs'] * 1e3)
    legend = TLegend(0.60, 0.75, 0.83, 0.90)
    for ii, upper_limit in enumerate(upper_limits):
        #upper_limit.RemovePoint(0)
        upper_limit.SetMarkerSize(0)
        upper_limit.SetFillStyle(3001)
        index = ii
        #if dM[ii] == 130:
        #    index = 1
        #elif dM[ii] == 80:
        #    index = 2
        #    continue
        #elif dM[ii] == 50:
        #    index = 3
        #elif dM[ii] == 30:
        #    index = 4
        #print(upper_limit)
        upper_limit.SetFillColor(BasicConfig.colors[index + 1])
        upper_limit.SetLineColor(BasicConfig.colors[index + 1])
        upper_limit.Draw('lp,same')
        #upper_limit.Draw('c,same')
        #if dM[ii] > 100:
        #    #legend.AddEntry(upper_limit, 'M_{#tilde{g}} = '+str(mass_g)+' GeV, #DeltaM = '+str(dM[ii])+' GeV', 'lf')
        #    legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf')
        #legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf')
        legend.AddEntry(upper_limit, 'Mass G = ' + str(mass_g[ii]) + ' GeV',
                        'lf')
    utils.decorate_legend(legend)
    legend.Draw()
    AtlasStyle.ATLASLabel(0.19, 0.87, 'Work in Progress')
    AtlasStyle.myText(0.20, 0.79, kBlack,
                      '#sqrt{s} = 13 TeV, #int L dt = 30 fb^{-1}', 0.035)
    #AtlasStyle.myText(0.20, 0.73, kBlack, 'Split-SUSY Model, M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.032)
    #AtlasStyle.myText(0.20, 0.67, kBlack, 'M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.035)
    #if dM == 'large' and tree.mGluino - tree.deltaM == 100:
    #    print('test')
    utils.save_as(
        canvas, BasicConfig.plotdir + 'xs_limit_large_dM_' + flavor_of_sample)
Example #15
0
def plotSL(file, binning):

  if not "appended.root" in file:
    print "Error:  plotSL.py must run on a file ending in \"appended.root\", but was given", file
    print "Exiting..."
    return

  if not binning[0] == '_':
    binning = '_'+binning

  outDir = file[:-5]+'/'
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  outDir += "plotSL/"
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  AtlasStyle.SetAtlasStyle()

  gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info

  inFile = TFile.Open(file, "READ");
  keyList = [key.GetName() for key in inFile.GetListOfKeys()] #List of top level objects
  dirList = [key for key in keyList if "Iteration" in key] #List of all directories

  #nomDir = [dir for dir in dirList if "NoCorr" in dir]
  nomDir = [dir for dir in dirList if "Nominal" in dir]
  if( not len(nomDir) == 1):
    print "Error, nominal directories are ", nomDir
    return
  else:
    nomDir = inFile.Get( nomDir[0] )

  c1 = TCanvas()


  #################### New Sampling Layer Histograms ##########################
  numSamples = 24
  sampleName = ["PreSamplerB", "EMB1", "EMB2", "EMB3", "PreSamplerE", "EME1", "EME2", "HEC0", "HEC1", "HEC2", "HEC3", "TileBar0", "TileBar1", "TileBar2", "TileGap1", "TileGap2", "TileGap3", "TileExt0", "TileExt1", "TileExt2", "FCAL0", "FCAL1", "FCAL2", "MINIFCAL0", "MINIFCAL1", "MINIFCAL2", "MINIFCAL3"]

  print "Plotting new hists for "
  for dir in dirList:
    print "           ", dir
    if not os.path.exists(outDir+dir):
      os.mkdir( outDir+dir )

    thisDir = inFile.Get( dir )
    thisSample = []
    leg = TLegend(0.80, 0.15, 0.97, 0.95)
    pad1 = TPad("pad1", "", 0, 0, 0.85, 1)
    pad1.Draw()
    pad1.cd()
    ## Draw a ghost copy for it's axis first ##
    thisSample.append(thisDir.Get("prof_recoilPt_SamplingLayerPercent0"+binning) )
    thisSample[-1].Add( thisSample[-1], -1.)
    #for iBin in range(1, thisSample[-1].GetNbinsX()+1):
    #  thisSample[-1].SetBinError(iBin, 0.)

    #if("NoCorr" in dir):
    if("Nominal" in dir):
      thisSample[-1].GetYaxis().SetTitle("Percent of Recoil Energy per Layer")
      thisSample[-1].GetYaxis().SetRangeUser(0.004, 0.5)
    elif ("NoCorr" in dir):
      thisSample[-1].GetYaxis().SetTitle("Rel. Uncert. in % Recoil E per Layer")
      thisSample[-1].GetYaxis().SetRangeUser(-0.3, 0.3)
    else:
      thisSample[-1].GetYaxis().SetTitle("Rel. Uncert. in % Recoil E per Layer")
      thisSample[-1].GetYaxis().SetRangeUser(-0.1, 0.1)

    thisSample[-1].SetMarkerSize(0.)
    thisSample[-1].Draw()
    for iSample in range(0, numSamples):
      thisSample.append( thisDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning) )
      #thisSample[-1].GetXaxis().SetRangeUser(200., 2000.)
      thisSample[-1].SetLineColor( gStyle.GetColorPalette(10*iSample+1) )
      thisSample[-1].SetMarkerColor( gStyle.GetColorPalette(10*iSample+1) )
      thisSample[-1].SetMarkerSize(0.5)
      if(iSample%3 == 0):
        thisSample[-1].SetLineStyle(1)
      elif(iSample%3==1):
        thisSample[-1].SetLineStyle(9)
        thisSample[-1].SetLineWidth(3)
      else:
        thisSample[-1].SetLineStyle(2)

      #if( not "NoCorr" in dir):
      if( not "Nominal" in dir):
        nomSample = nomDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning)
        thisSample[-1].Add(nomSample, -1.)
        thisSample[-1].Divide(nomSample)
#        for iBin in range(1, thisSample[-1].GetNbinsX()+1):
#          thisSample[-1].SetBinError(iBin, 0.)

      #if("NoCorr" in dir):
      if("Nominal" in dir):
        thisSample[-1].GetYaxis().SetTitle("Percent of Recoil Energy per Layer")
        thisSample[-1].GetYaxis().SetRangeUser(0.004, 0.5)
      elif ("NoCorr" in dir):
        thisSample[-1].GetYaxis().SetTitle("Rel. Uncert. in % Recoil E per Layer")
        thisSample[-1].GetYaxis().SetRangeUser(-0.3, 0.3)
      else:
        thisSample[-1].GetYaxis().SetTitle("Rel. Uncert. in % Recoil E per Layer")
        thisSample[-1].GetYaxis().SetRangeUser(-0.1, 0.1)

      leg.AddEntry(thisSample[-1], sampleName[iSample], "l")
      #if( "NoCorr" in dir):
      if( "Nominal" in dir):
        thisSample[-1].Draw("same hist pe")
      else:
        thisSample[-1].Draw("same hist l")

    c1.cd()
    leg.Draw()
    c1.SaveAs(outDir+dir+"/SamplingEnergy_all"+binning+".png")
    #if( "NoCorr" in dir):
    if( "Nominal" in dir):
      pad1.SetLogy()
      c1.SaveAs(outDir+dir+"/SamplingEnergy_all"+binning+"_logy.png")
      pad1.SetLogy(0)
    c1.Clear()


  inFile.Close()
def plotSLDataMCRatio(dataFile, mcFile, binning):
  if not "appended.root" in mcFile or not "appended.root" in dataFile:
    print "Error:  plotSL.py must run on two files ending in \"appended.root\", but was given", mcFile, "and", dataFile
    print "Exiting..."
    return

  if not binning[0] == '_':
    binning = '_'+binning

  outDir = dataFile[:-5]+'/'
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  outDir += "plotSLDataMCRatio/"
  if not os.path.exists(outDir):
    os.mkdir(outDir)
  AtlasStyle.SetAtlasStyle()

  gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info

  mcFile = TFile.Open(mcFile, "READ");
  mcKeyList = [key.GetName() for key in mcFile.GetListOfKeys()] #List of top level objects
  #mcDir = [key for key in mcKeyList if "Iteration" in key and "NoCorr" in key] #List of all directories
  mcDir = [key for key in mcKeyList if "Iteration" in key and "Nominal" in key] #List of all directories
  mcDir = mcFile.Get( mcDir[0] )
  dataFile = TFile.Open(dataFile, "READ");
  dataKeyList = [key.GetName() for key in dataFile.GetListOfKeys()] #List of top level objects
  #dataDir = [key for key in dataKeyList if "Iteration" in key and "NoCorr" in key] #List of all directories
  dataDir = [key for key in dataKeyList if "Iteration" in key and "Nominal" in key] #List of all directories
  dataDir = dataFile.Get( dataDir[0] )

  c1 = TCanvas()
  numSamples = 24
  sampleName = ["PreSamplerB", "EMB1", "EMB2", "EMB3", "PreSamplerE", "EME1", "EME2", "HEC0", "HEC1", "HEC2", "HEC3", "TileBar0", "TileBar1", "TileBar2", "TileGap1", "TileGap2", "TileGap3", "TileExt0", "TileExt1", "TileExt2", "FCAL0", "FCAL1", "FCAL2", "MINIFCAL0", "MINIFCAL1", "MINIFCAL2", "MINIFCAL3"]

  sampleSubset = [0, 1, 2, 3, 11, 12, 13]

  #################### New Histograms ##########################
  print "Plotting sampling layer energy ratio "

  thisSample = []
  leg = TLegend(0.83, 0.15, 0.99, 0.95)
  pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
  pad1.SetRightMargin(0.1)
  pad1.Draw()
  pad1.cd()

  ## Draw a ghost copy for it's axis first ##
  thisSample.append(mcDir.Get("prof_recoilPt_SamplingLayerPercent0"+binning) )
  #thisSample[-1].Add(thisSample[-1], -1.)
  thisSample[-1].GetYaxis().SetTitle( "Rel. Uncert. of Recoil Energy per Layer" )
  thisSample[-1].GetYaxis().SetRangeUser(-0.2,0.2)
  thisSample[-1].SetLineColor(kWhite)
  thisSample[-1].SetMarkerColor(kWhite)
  thisSample[-1].Draw("hist l")

  for iSample in range(0, numSamples):
    thisSample.append( mcDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning) )
    thisSample[-1].SetName(thisSample[-1].GetName()+str(iSample))
    dataSample = dataDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning)
    thisSample[-1].Add(dataSample, -1.)
    thisSample[-1].Divide(dataSample)
    thisSample[-1].GetYaxis().SetRangeUser(-0.2,0.2)


    #thisSample[-1].GetXaxis().SetRangeUser(200., 2000.)
    thisSample[-1].SetLineColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerSize(0.5)
    if(iSample%3 == 0):
      thisSample[-1].SetLineStyle(1)
    elif(iSample%3==1):
      thisSample[-1].SetLineStyle(9)
      thisSample[-1].SetLineWidth(3)
    else:
      thisSample[-1].SetLineStyle(2)

    leg.AddEntry(thisSample[-1], sampleName[iSample], "l")
    thisSample[-1].Draw("same hist l")
  c1.cd()
  leg.Draw()
  c1.SaveAs(outDir+"Frac_SamplingEnergy_all"+binning+".png")
#  pad1.SetLogy()
#  c1.SaveAs(outDir+"SamplingEnergy_all"+binning+"_logy.png")
#  pad1.SetLogy(0)
  c1.Clear()

  ###### Do straight difference ##########
  thisSample = []
  leg = TLegend(0.83, 0.15, 0.99, 0.95)
  pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
  pad1.Draw()
  pad1.cd()

  ## Draw a ghost copy for it's axis first ##
  thisSample.append(mcDir.Get("prof_recoilPt_SamplingLayerPercent0"+binning) )
  thisSample[-1].Add(thisSample[-1], -1.)
  thisSample[-1].GetYaxis().SetTitle( "Difference in Recoil Energy per Layer" )
  thisSample[-1].GetYaxis().SetRangeUser(-0.1,0.1)
  thisSample[-1].SetLineColor(kWhite)
  thisSample[-1].SetMarkerColor(kWhite)
  thisSample[-1].Draw("hist l")

  for iSample in range(0, numSamples):
    thisSample.append( mcDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning) )
    thisSample[-1].SetName(thisSample[-1].GetName()+str(iSample))
    dataSample = dataDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning)
    thisSample[-1].Add(dataSample, -1.)
    thisSample[-1].GetYaxis().SetRangeUser(-0.1,0.1)

    #thisSample[-1].GetXaxis().SetRangeUser(200., 2000.)
    thisSample[-1].SetLineColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerSize(0.5)
    if(iSample%3 == 0):
      thisSample[-1].SetLineStyle(1)
    elif(iSample%3==1):
      thisSample[-1].SetLineStyle(9)
      thisSample[-1].SetLineWidth(3)
    else:
      thisSample[-1].SetLineStyle(2)

    leg.AddEntry(thisSample[-1], sampleName[iSample], "l")
    thisSample[-1].Draw("same hist l")
  c1.cd()
  leg.Draw()
  c1.SaveAs(outDir+"Diff_SamplingEnergy_all"+binning+".png")
  c1.Clear()


###################################### Subset of sample layers only ###################################################
  #################### New Histograms ##########################
  print "Plotting sampling layer energy ratio "

  thisSample = []
  leg = TLegend(0.83, 0.15, 0.99, 0.95)
  pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
  pad1.SetRightMargin(0.1)
  pad1.Draw()
  pad1.cd()

  ## Draw a ghost copy for it's axis first ##
  thisSample.append(mcDir.Get("prof_recoilPt_SamplingLayerPercent0"+binning) )
  #thisSample[-1].Add(thisSample[-1], -1.)
  thisSample[-1].GetYaxis().SetTitle( "Rel. Uncert. of Recoil Energy per Layer" )
  thisSample[-1].GetYaxis().SetRangeUser(-0.2,0.2)
  thisSample[-1].SetLineColor(kWhite)
  thisSample[-1].SetMarkerColor(kWhite)
  thisSample[-1].Draw("hist l")

  for iSample in sampleSubset:
    thisSample.append( mcDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning) )
    thisSample[-1].SetName(thisSample[-1].GetName()+str(iSample))
    dataSample = dataDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning)
    thisSample[-1].Add(dataSample, -1.)
    thisSample[-1].Divide(dataSample)
    thisSample[-1].GetYaxis().SetRangeUser(-0.2,0.2)


    #thisSample[-1].GetXaxis().SetRangeUser(200., 2000.)
    thisSample[-1].SetLineColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerSize(0.5)
    if(iSample%3 == 0):
      thisSample[-1].SetLineStyle(1)
    elif(iSample%3==1):
      thisSample[-1].SetLineStyle(9)
      thisSample[-1].SetLineWidth(3)
    else:
      thisSample[-1].SetLineStyle(2)

    leg.AddEntry(thisSample[-1], sampleName[iSample], "l")
    thisSample[-1].Draw("same hist l")
  c1.cd()
  leg.Draw()
  c1.SaveAs(outDir+"Frac_SubsetSamplingEnergy_all"+binning+".png")
#  pad1.SetLogy()
#  c1.SaveAs(outDir+"SamplingEnergy_all"+binning+"_logy.png")
#  pad1.SetLogy(0)
  c1.Clear()

  ###### Do straight difference ##########
  thisSample = []
  leg = TLegend(0.83, 0.15, 0.99, 0.95)
  pad1 = TPad("pad1", "", 0, 0, 0.83, 1)
  pad1.Draw()
  pad1.cd()

  ## Draw a ghost copy for it's axis first ##
  thisSample.append(mcDir.Get("prof_recoilPt_SamplingLayerPercent0"+binning) )
  thisSample[-1].Add(thisSample[-1], -1.)
  thisSample[-1].GetYaxis().SetTitle( "Difference in Recoil Energy per Layer" )
  thisSample[-1].GetYaxis().SetRangeUser(-0.1,0.1)
  thisSample[-1].SetLineColor(kWhite)
  thisSample[-1].SetMarkerColor(kWhite)
  thisSample[-1].Draw("hist l")

  for iSample in sampleSubset:
    thisSample.append( mcDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning) )
    thisSample[-1].SetName(thisSample[-1].GetName()+str(iSample))
    dataSample = dataDir.Get("prof_recoilPt_SamplingLayerPercent"+str(iSample)+binning)
    thisSample[-1].Add(dataSample, -1.)
    thisSample[-1].GetYaxis().SetRangeUser(-0.1,0.1)

    #thisSample[-1].GetXaxis().SetRangeUser(200., 2000.)
    thisSample[-1].SetLineColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerColor( gStyle.GetColorPalette(10*iSample+1) )
    thisSample[-1].SetMarkerSize(0.5)
    if(iSample%3 == 0):
      thisSample[-1].SetLineStyle(1)
    elif(iSample%3==1):
      thisSample[-1].SetLineStyle(9)
      thisSample[-1].SetLineWidth(3)
    else:
      thisSample[-1].SetLineStyle(2)

    leg.AddEntry(thisSample[-1], sampleName[iSample], "l")
    thisSample[-1].Draw("same hist l")
  c1.cd()
  leg.Draw()
  c1.SaveAs(outDir+"Diff_SubsetSamplingEnergy_all"+binning+".png")
  c1.Clear()