def test_systematics_doublesidedness(self):
     if check_sys:
         for sys in data.syss:
             if "up" in sys:
                 other = sys.replace("up", "down")
                 self.assertIn(other, data.syss,
                               "'%s' sys. uncert. is missing" % other)
             elif "down" in sys:
                 other = sys.replace("down", "up")
                 self.assertIn(other, data.syss,
                               "'%s' sys. uncert. is missing" % other)
 def test_systematics_doublesidedness(self):
     if check_sys:
         for sys in data.syss:
             if "up" in sys:
                 other = sys.replace("up", "down")
                 self.assertIn(other, data.syss,
                               "'%s' sys. uncert. is missing" % other)
             elif "down" in sys:
                 other = sys.replace("down", "up")
                 self.assertIn(other, data.syss,
                               "'%s' sys. uncert. is missing" % other)
Esempio n. 3
0
    def fetch(self):
        """Creates a series of physical system objects using a template and label substitutions.

        Returns:
            A list pf System objects of the appropriate type and with the appropriate
            properties, built by substituting placeholders in a template with the given values

        Raises:
            ValueError: Raised if the labels and the instance lists have mismatching lengths
        """

        super(InputSysTemplate, self).fetch()

        template = self.template.fetch()
        labels = self.labels.fetch()
        lsys = []
        for (k, v) in self.extra:
            if k == "instance":
                ins = v.fetch()
                sys = template
                if len(labels) != len(ins):
                    raise ValueError("Labels and instance length mismatch")
                for l in xrange(
                        len(ins)):  # string replacement within the template
                    sys = sys.replace(labels[l], ins[l])
                print "Generating system from template: \n", sys
                xsys = xml_parse_string(
                    sys)  # parses the string to an XML object
                isys = InputSystem()
                isys.parse(xsys.fields[0]
                           [1])  # parses the XML object into an InputSystem
                lsys.append(isys.fetch(
                ))  # fetches the generated System and appends to the list

        return lsys
Esempio n. 4
0
File: system.py Progetto: i-pi/i-pi
    def fetch(self):
        """Creates a series of physical system objects using a template and label substitutions.

        Returns:
            A list pf System objects of the appropriate type and with the appropriate
            properties, built by substituting placeholders in a template with the given values

        Raises:
            ValueError: Raised if the labels and the instance lists have mismatching lengths
        """

        super(InputSysTemplate, self).fetch()

        template = self.template.fetch()
        labels = self.labels.fetch()
        lsys = []
        for (k, v) in self.extra:
            if k == "instance":
                ins = v.fetch()
                sys = template
                if len(labels) != len(ins):
                    raise ValueError("Labels and instance length mismatch")
                for l in xrange(len(ins)):  # string replacement within the template
                    sys = sys.replace(labels[l], ins[l])
                print "Generating system from template: \n", sys
                xsys = xml_parse_string(sys)  # parses the string to an XML object
                isys = InputSystem()
                isys.parse(xsys.fields[0][1])  # parses the XML object into an InputSystem
                lsys.append(isys.fetch())     # fetches the generated System and appends to the list

        return lsys
Esempio n. 5
0
def getXmlWithDifSys(nounoId, modelList, name, cursor, sys):
    doc = minidom.parse("./SystemTree/" + sys)
    # get root element
    root = doc.documentElement
    mainpart = root.getElementsByTagName("model")

    for child in mainpart:
        if (child.hasAttribute("name")):
            if child.getAttribute("name") not in modelList:
                child.parentNode.removeChild(child)
            else:
                child.setAttribute(
                    "Chinese",
                    selectChinese(child.getAttribute("name"), cursor)[0])
    isdele = True
    while isdele:
        f = codecs.open("./" + nounoId + ".xml", 'w', 'utf-8')
        doc.writexml(f, addindent='  ', newl=' ', encoding='utf-8')
        f.close()
        doc = minidom.parse("./" + nounoId + ".xml")
        root = doc.documentElement
        isdele = False
        parts = root.getElementsByTagName("part")
        #print(len(parts))
        for part in parts:
            #print(len(part.childNodes))
            if len(part.childNodes) == 1:
                part.parentNode.removeChild(part)
                isdele = True
    f = codecs.open("./" + nounoId + "tmp.xml", 'w', 'utf-8')
    doc.writexml(f, addindent='  ', newl=' ', encoding='utf-8')
    f.close()
    sys = sys.replace(".xml", "")
    isExists = os.path.exists("./" + sys)
    if not isExists:
        os.makedirs("./" + sys)
    isExists = os.path.exists("./" + sys + "_1")
    if not isExists:
        os.makedirs("./" + sys + "_1")
    mainpart = root.getElementsByTagName("model")
    if len(mainpart) != 0:
        delblankline("./" + nounoId + "tmp.xml",
                     "./" + sys + "/" + nounoId + ".xml", name)
        # delblankline("./"+nounoId+"tmp.xml","./"+sys+"_1/"+nounoId+"_1.xml",name)
        # delblankline("./"+nounoId+"tmp.xml","./"+sys+"_1/"+nounoId+"_2.xml",name)
    os.remove("./" + nounoId + "tmp.xml")
    os.remove("./" + nounoId + ".xml")
Esempio n. 6
0
def print_latex_full():
    print '\\documentclass[11pt,oneside,a4paper]{article}'
    print '\\usepackage{rotating}'
    print '\\usepackage[usenames]{color}'
    print '\\begin{document}'

    for sys in lepsys + tausys + lepES + JES:
        print '\\begin{table}'
        print '\\centering'
        print '\\caption{\\bf{%s}}' % sys.replace('_', '\_')
        print '\\begin{tabular}{lrrrrrr}'
        print row('', [group.name for group in groups], fmt='latex')

        for region in [
                'SR_OS', 'SR_SS', 'WCR_OS', 'WCR_SS', 'QCD_OS', 'QCD_SS'
        ]:
            NormStatFull(sys, region)
        print '\\end{tabular}'
        print '\\end{table}'

    print '\\end{document}'
                sys_dict[sys].Add(tmp)
    elif proc == "Data" or proc == "Fit":
        print proc
        tmp = plotting.unroll2DHistograms([hists[proc][var]])[0]
        tmp.Print("all")
    else:
        tmp = plotting.unroll2DHistograms([hists[proc][var]])[0]
        tot.Add(tmp)
        if args.print_all:
            tmp.Print("all")
        if proc == 'QCD':
            qcdHist = tmp
print "Total MC"
tot.Print("all")

print "QCD fraction"
if qcdHist is not None:
    qcdFraction = qcdHist.Clone("qcdFraction")
    qcdFraction.Divide(tot)
    qcdFraction.Print("all")

# Compute the size of each systematic uncertainty,
# averaged across all analysis bins
for sys, hist in sys_dict.iteritems():
    if 'Up' in sys:
        downHist = sys_dict[sys.replace('Up', 'Down')]
        hist.Add(downHist, -1)
        hist.Divide(tot)
        print "Systematic:", sys.replace('Up', ''),
        print hist.Integral() / hist.GetNbinsX()
Esempio n. 8
0
def book_trees(select = ''):
    ordsam = OrderedDict(sorted(samples.items(), key=lambda x: x[1]['order']))

    sig_chain  = ROOT.TChain('sig_data')
    bkg_chain  = ROOT.TChain('bkg_data')
    
    for proc in ordsam:
        chainName = treename.replace('*',proc)
        if ordsam[proc].get("tree","") != "":
            chainName =  treename.replace('*',ordsam[proc].get("tree",""))
        chain      = ROOT.TChain(chainName)
        chainSysUp = []
        chainSysDw = []
        if type(samples[proc].get('name')) == type([]):
            for sam in samples[proc].get('name',[]):
                for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                    chain.Add(f)
                    
                    if 'signal' in str(samples[proc].get('label','')).lower():
                        print 'sig -->', f+'/'+treename.replace('*',proc)
                        sig_chain.Add(f+'/'+treename.replace('*',proc))
                    elif 'data' not in str(samples[proc].get('label','')).lower():
                        print 'bkg -->', f+'/'+treename.replace('*',proc)
                        bkg_chain.Add(f+'/'+treename.replace('*',proc))
            if proc != 'Data':
                for sys in treesUpSys:
                    print "debug::(",proc,")", sys, " == ", samples[proc].get('label')
                    chainUp = ROOT.TChain(sys.replace('*',proc))
                    for sam in samples[proc].get('name',[]):
                        for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                            chainUp.Add(f)
                        chainSysUp.append(chainUp)
                for sys in treesDwSys:
                    chainDw = ROOT.TChain(sys.replace('*',proc))
                    for sam in samples[proc].get('name',[]):
                        for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                            chainDw.Add(f)
                        chainSysDw.append(chainDw)
        else:
            sam = samples[proc].get('name')
            for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                chain.Add(f)
                if 'signal' in str(samples[proc].get('label','')).lower():
                    print 'sig -->', f+'/'+treename.replace('*',proc)
                    sig_chain.Add(f+'/'+treename.replace('*',proc))
                elif 'data' not in str(samples[proc].get('label','')).lower():
                    print 'bkg -->', f+'/'+treename.replace('*',proc)
                    bkg_chain.Add(f+'/'+treename.replace('*',proc))
            if samples[proc].get('label') != 'Data':
                for sys in treesUpSys:
                    chainUp = ROOT.TChain(sys.replace('*',proc))
                    for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                        chainUp.Add(f)
                    chainSysUp.append(chainUp)
                for sys in treesDwSys:
                    chainDw = ROOT.TChain(sys.replace('*',proc))
                    for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                        chainDw.Add(f)
                    chainSysDw.append(chainDw)
        # read systematic trees
        samples[proc].update({'_root_tree_'       : chain})
        samples[proc].update({'_root_tree_sysDw_' : chainSysDw})
        samples[proc].update({'_root_tree_sysUp_' : chainSysUp})
        
    logger.info('-- sig_chain.GetEntries() = %i' % sig_chain.GetEntries())
    logger.info('-- bkg_chain.GetEntries() = %i' % bkg_chain.GetEntries())
    return (sig_chain, bkg_chain)
Esempio n. 9
0
import sys as s, itertools as i
t,u,i2b,o,r=b'T',b'U',lambda i:i.to_bytes(1,'big'),[s.stdout.buffer.write,lambda s:0],range
A,e=s.argv,dict(zip((''.join(x).encode()for x in i.product(*('ACGT',)*4)),map(i2b,r(256))))
with open(A[2],'rb')as I:D,Rr=b''.join(I.read().strip().split()),lambda s:s.replace(t,u)
S,R,M,o,d=D[:-1],D[-1]&4,D[-1]&2,o[::-1]if A[1]=="d"else o,dict(zip(e.values(),e)).get
[o[0](e.get(q.replace(u,t)+(b'A'*(4-len(q)))))for q in(D[i: i+4] for i in r(0,len(D),4))]
o[0](i2b((len( [D[i: i+4] for i in r(0,len(D),4)][-1] )%4)|(4 if u in S else 0)))
o[1](b''.join(Rr(d(i2b(x)))if R else d(x)for x in S)[:-M if M else None]+b'\n')
Esempio n. 10
0
    counter = 0
    for mysys in allSys:
        print "===>", sys
        sys = mysys + "High"
        sys2 = sys
        if sys.find("photon") >= 0 or sys.find("GeneratorZ") >= 0 or sys.find(
                "Kappa") >= 0 or sys.find("QCD") >= 0 or sys.find(
                    "TopTuneA14") >= 0 or sys.find("TopRadiation") >= 0:
            sys2 = "Nom"

        if sys.find("QCD") >= 0:
            sys2 = "Nom"

        histHigh = createGraph(bkg, sys, region, bkg2, sys2, region2)
        histLow = createGraph(bkg, sys.replace("High", "Low"), region, bkg2,
                              sys2.replace("High", "Low"), region2)

        if doSym:
            newHists = Symmetrize(default, histLow, histHigh)
            histLow = newHists[0]
            histHigh = newHists[1]

        histHigh.SetLineStyle(2)
        histHigh.SetLineColor(colors[counter])
        histHigh.SetLineWidth(2)

        histLow.SetLineStyle(2)
        histLow.SetLineColor(colors[counter])
        histLow.SetLineWidth(2)
    # ***  Exit Scipt  ***
    # ********************
    # ********************

    import sys
    #sys.exit()

    #  Prepare table
    # ----------------------------------------------------------------------
    os.system('mkdir -p tablesQCD_ggH')

    for proc in sysMap:
        table = []
        headers = ['Systematic'] + [i + 1 for i in xrange(0, 17)]
        for sys in NPnames:
            line = [sys.replace('_', '\_')] + [
                formatCell(sysMap[proc], cat, sys) for cat in HG.CatLabels[:17]
            ]
            table.append(line)
        log = open('tablesQCD_ggH/table_%s1.tex' % proc, 'w+')
        print >> log, tabulate.tabulate(table,
                                        headers=headers,
                                        tablefmt='latex')
        print ''
        print tabulate.tabulate(table, headers=headers)

        table = []
        headers = ['Systematic'
                   ] + [i + 1 for i in xrange(17,
                                              len(HG.CatLabels) + 1)]
        for sys in NPnames:
Esempio n. 12
0
 def chSys(self, event):
     sys = self.useSystem.GetValue().lower().strip()
     sys = sys.replace("neuronales netz", "net")
     sys = sys.replace("hidden markov modell", "markov")
     self.system = sys
     print self.system
Esempio n. 13
0
                    "FitCRv8p2", "FailFitCRv8p2"
                ]
        for region in region_list:
            inFile_this.cd()
            hist_nominal = inFile_this.Get(region + "__fatJet2MassSD")
            outBinName = region.replace("SR",
                                        "").replace("Fail",
                                                    "fail").replace(vbdt, "")
            hist_nominal.SetName("histJet2Mass_" + outBinName + "_" +
                                 proc[idx])

            hists_sys = []
            for sys in systs:
                hist_Up = inFile_this.Get(region + sys + "Up__fatJet2MassSD")
                hist_Up.SetName("histJet2Mass_" + outBinName + "_" +
                                proc[idx] + "_" + sys.replace(vbdt, "") + "Up")
                hists_sys.append(hist_Up)
                hist_Down = inFile_this.Get(region + sys +
                                            "Down__fatJet2MassSD")
                hist_Down.SetName("histJet2Mass_" + outBinName + "_" +
                                  proc[idx] + "_" + sys.replace(vbdt, "") +
                                  "Down")
                hists_sys.append(hist_Down)

            hist_JMSUp = inFile_this.Get(region + "__fatJet2MassSD_JMSUp")
            hist_JMSUp.SetName("histJet2Mass_" + outBinName + "_" + proc[idx] +
                               "_JMSUp")
            hists_sys.append(hist_JMSUp)
            hist_JMSDown = inFile_this.Get(region + "__fatJet2MassSD_JMSDown")
            hist_JMSDown.SetName("histJet2Mass_" + outBinName + "_" +
                                 proc[idx] + "_JMSDown")
Esempio n. 14
0
htmpnom = copy.deepcopy(ffid.Get(varName).Clone("nom"))
htmpnom.SetLineColor(r.kBlack)
htmpnom.SetLineWidth(2)
htmpnom.GetXaxis().SetTitle(vl.varList[varName]['xaxis'])

for key in ffid.GetListOfKeys():
    if key.GetName() == varName or "nom" in key.GetName(
    ) or "Down" in key.GetName():
        continue
    sys = key.GetName().replace(varName + "_", "")
    if "Up" not in key.GetName() and "Down" not in key.GetName(): continue
    htmpup = copy.deepcopy(key.ReadObj().Clone(sys))
    htmpdw = copy.deepcopy(
        ffid.Get(key.GetName().replace("Up", "Down")).Clone(
            sys.replace("Up", "Down")))
    htmpup.SetLineColor(r.kBlue)
    htmpdw.SetLineColor(r.kRed)
    htmpup.SetLineWidth(2)
    htmpdw.SetLineWidth(2)

    c = r.TCanvas()
    htmpnom.Draw()
    htmpup.Draw("same")
    htmpdw.Draw("same")
    c.SaveAs('results/fiduchecks/{var}/{var}_{syst}.png'.format(
        var=varName, syst=sys.replace("Up", "")))
    del c, htmpup, htmpdw

ffid.Close()
del ffid, htmpnom
def systematics(sys, isShape=False, antiCorr=False):
    treeRead = True
    file = {}
    hist = {}
    tree = {}
    histUp = {}
    histDown = {}
    up = {}
    down = {}
    gUp = TGraph()
    gDown = TGraph()
    gUp.SetLineWidth(3)
    gDown.SetLineWidth(3)
    gUp.SetLineColor(632)
    gDown.SetLineColor(602)
    BTagAK4deepup = False
    BTagAK4deepdown = False
    BTagAK4deep = False
    BTagAK8deepup = False
    BTagAK8deepdown = False
    BTagAK8deep = False
    #    g.SetMarkerStyle(20)
    #    g.SetMarkerColor(418)
    #    g.SetMarkerSize(1.25)
    var = sys
    cut = 'nnbbSR'
    if var == 'BTagAK4Weight_deep_up':
        var = 'X_mass'
        BTagAK4deepup = True
    elif var == 'BTagAK4Weight_deep_down':
        var = 'X_mass'
        BTagAK4deepdown = True
    elif var == 'BTagAK4Weight_deep':
        var = 'X_mass'
        BTagAK4deep = True
    elif var == 'BTagAK8Weight_deep_up':
        var = 'X_mass'
        BTagAK8deepup = True
    elif var == 'BTagAK8Weight_deep_down':
        var = 'X_mass'
        BTagAK8deepdown = True
    elif var == 'BTagAK8Weight_deep':
        var = 'X_mass'
        BTagAK8deep = True
    upAvg, downAvg, upMin, downMin, upMax, downMax = 0., 0., 2., 2., 0., 0.
    for k in sorted(selection.keys(), key=len, reverse=True):
        if k in cut:
            cut = cut.replace(k, selection[k])
    for i, s in enumerate(sign):
        if '_MZ' in s: m = int((s.split('_MZ')[1]).split('_MA')[0])
        elif '_M' in s: m = int(s.split('_M')[1])
        else: m = 0
        if treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                min_value = variable[var]['min']
                max_value = variable[var]['max']
                title = variable[var]['title']
                if 'isZtoNN' in cut:
                    if var == 'MET':
                        min_value = 200
                        max_value = 2000
                    elif var == 'DPhi':
                        title = "#Delta #varphi (AK8 jet-#slash{E}_{T})"
                    elif var == 'VH_deltaR':
                        title = "#Delta R (#slash{E}_{T}, AK8 jet)"
                hist[s] = TH1F(
                    s, ";" + title + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], min_value, max_value)
            else:
                hist[s] = TH1F(s, ";" + variable[var]['title'],
                               len(variable[var]['bins']) - 1,
                               array('f', variable[var]['bins']))
            hist[s].Sumw2()
            cutstring = "(eventWeightLumi)" + ("*(" + cut + ")")
            if var == 'LeptonWeightUp':
                cutstring = "(eventWeightLumi * LeptonWeightUp/LeptonWeight)" + (
                    "*(" + cut + ")")
            elif var == 'LeptonWeightDown':
                cutstring = "(eventWeightLumi * LeptonWeightDown/LeptonWeight)" + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightUp':
                cutstring = "(eventWeightLumi * TriggerWeightUp/TriggerWeight)" + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightDown':
                cutstring = "(eventWeightLumi * TriggerWeightDown/TriggerWeight)" + (
                    "*(" + cut + ")")
            #division by BTagAk4Weight_deep is because the weighted samples are used
            elif BTagAK4deepup:
                cutstring = "(eventWeightLumi * BTagAK4Weight_deep_up/BTagAK4Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK4deepdown:
                cutstring = "(eventWeightLumi * BTagAK4Weight_deep_down/BTagAK4Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK8deep:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK8deepup:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep_up)" + (
                    "*(" + cut + ")")
            elif BTagAK8deepdown:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep_down)" + (
                    "*(" + cut + ")")
            tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        """
        for j, ss in enumerate(sample[s]['files']):
            file[ss] = TFile(NTUPLEDIR + ss + ".root", "READ")
            tmp = file[ss].Get("Sys/"+sys)
            if tmp == None: continue
            if not s in hist.keys(): hist[s] = tmp
            else:
                if antiCorr:
                    for x in range(1, hist[s].GetNbinsX()+1): hist[s].SetBinContent(x, hist[s].GetBinContent(x)+tmp.GetBinContent(tmp.GetNbinsX()+1-x))
                else: hist[s].Add( tmp )
            
            if isShape:
                tmp = file[ss].Get("Sys/"+sys+"_up")
                print "Sys/"+sys+"_up"
                if tmp == None: continue
                if not s in histUp.keys(): histUp[s] = tmp
                else: histUp[s].Add( tmp )
                #
                tmp = file[ss].Get("Sys/"+sys+"_down")
                if tmp == None: continue
                if not s in histDown.keys(): histDown[s] = tmp
                else: histDown[s].Add( tmp )
            
        if 'accept' in sys:
            norm = None
            for j, ss in enumerate(sample[s]['files']):
                tmp = file[ss].Get("Sys/PDF_scale")
                if tmp == None: continue
                if norm == None: norm = tmp
                else: norm.Add( tmp )
            hist[s].Divide(norm)
        """
        if (isShape):
            shape = TF1("shape", "gaus", 0, 5000)
            shapeUp = TF1("shapeUp", "gaus", 0, 5000)
            shapeDown = TF1("shapeDown", "gaus", 0, 5000)
            hist[s].Fit(shape, "Q0", "")
            histUp[s].Fit(shapeUp, "Q0", "")
            histDown[s].Fit(shapeDown, "Q0", "")
            if 'scale' in sys or 'unc' in sys:
                up[s] = histUp[s].GetMean() / hist[s].GetMean()
                down[s] = histDown[s].GetMean() / hist[s].GetMean()
#                up[s] = shapeUp.GetParameter(1)/shape.GetParameter(1)
#                down[s] = shapeDown.GetParameter(1)/shape.GetParameter(1)
            elif 'res' in sys:
                up[s] = histUp[s].GetRMS() / hist[s].GetRMS()
                down[s] = histDown[s].GetRMS() / hist[s].GetRMS()


#                up[s] = shapeUp.GetParameter(2)/shape.GetParameter(2)
#                down[s] = shapeDown.GetParameter(2)/shape.GetParameter(2)
        else:
            up[s] = hist[s].GetBinContent(
                hist[s].FindBin(+1)) / hist[s].GetBinContent(
                    hist[s].FindBin(0))
            down[s] = hist[s].GetBinContent(
                hist[s].FindBin(-1)) / hist[s].GetBinContent(
                    hist[s].FindBin(0))
        gUp.SetPoint(i, m, up[s])
        gDown.SetPoint(i, m, down[s])
        #if mass < 1000: continue
        upAvg += up[s]
        downAvg += down[s]
        if abs(up[s]) > upMax: upMax = abs(up[s])
        if abs(up[s]) < upMin: upMin = abs(up[s])
        if abs(down[s]) > downMax: downMax = abs(down[s])
        if abs(down[s]) < downMin: downMin = abs(down[s])

    upAvg /= len(sign)
    downAvg /= len(sign)

    print " ---", sys, "--- | up: %.3f, down: %.3f, average: %.3f" % (
        upAvg, downAvg, abs(upAvg - 1 + 1. - downAvg) /
        2.), "|", "^{%.1f-%.1f}_{%.1f-%.1f}" % (100. * (1. - upMin), 100. *
                                                (1. - upMax), 100. *
                                                (downMin - 1.), 100. *
                                                (downMax - 1.))

    c1 = TCanvas("c1", "Signals", 800, 600)
    c1.cd()
    c1.GetPad(0).SetTicky(2)

    gUp.Draw("AL")
    gDown.Draw("SAME, L")
    gUp.GetYaxis().SetRangeUser(0.65, 1.35)
    gUp.GetXaxis().SetTitle("m_{X} (GeV)")
    gUp.GetYaxis().SetTitle("Uncertainty")

    leg = TLegend(0.5, 0.90 - 0.20, 0.9, 0.90)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetHeader(sys.replace('_', ' '))
    leg.AddEntry(gUp, "+1 s. d. (%.1f%%)" % (100. * (upAvg - 1.)), "l")
    leg.AddEntry(gDown, " -1 s. d. (%.1f%%)" % (100. * (1. - downAvg)), "l")
    leg.Draw()

    drawCMS(-1, "Simulation", False)
    c1.Update()

    filename = sys
    if sys.startswith('W_mass') or sys.startswith('Z_mass'):
        filename += "_" + sign[0][:3]
    #    c1.Print("plots/Systematics/"+filename+".png")
    #    c1.Print("plots/Systematics/"+filename+".pdf")
    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
    if 'doubleB' in sys or 'subjet' in sys or 'mass' in sys:
        print sys + " = {",
        for m in [
                800, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800,
                2000, 2500, 3000, 3500, 4000, 4500
        ]:
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
    if 'extr' in sys or 'tagging' in sys:
        print sys + " = {",
        for m in [800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]:
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
    if 'QCD_scale' in sys or 'PDF_scale' in sys:
        print sys + " = {",
        for m in range(800, 4500 + 1, 10):
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
Esempio n. 16
0
        else:
            if vbdt == "v24":
                region_list = ["SRv24Bin1", "SRv24Bin2",  "SRv24Bin3", "SRv24Bin4", "FailSRv24", "FitCRv24", "FailFitCRv24"]
            else:
                region_list = ["SRv8p2Bin1", "SRv8p2Bin2",  "SRv8p2Bin3", "FailSRv8p2", "FitCRv8p2", "FailFitCRv8p2"]

        for region in region_list:
            inFile_this.cd()
            hist_nominal = inFile_this.Get(region+"__fatJet2MassSD")
            outBinName=region.replace("SR",  "").replace("Fail", "fail").replace(vbdt, "")
            hist_nominal.SetName("histJet2Mass_"+outBinName+"_"+proc[idx])

            hists_sys = []
            for sys in systs:
                hist_Up = inFile_this.Get(region+sys+"Up__fatJet2MassSD")
                hist_Up.SetName("histJet2Mass_"+outBinName+"_"+proc[idx]+"_"+sys.replace(vbdt,"")+"Up")
                hists_sys.append(hist_Up)
                hist_Down = inFile_this.Get(region+sys+"Down__fatJet2MassSD")
                hist_Down.SetName("histJet2Mass_"+outBinName+"_"+proc[idx]+"_"+sys.replace(vbdt,"")+"Down")
                hists_sys.append(hist_Down)

            hist_JMSUp =  inFile_this.Get(region+"__fatJet2MassSD_JMSUp")
            hist_JMSUp.SetName("histJet2Mass_"+outBinName+"_"+proc[idx]+"_JMSUp")
            hists_sys.append(hist_JMSUp)
            hist_JMSDown =  inFile_this.Get(region+"__fatJet2MassSD_JMSDown")
            hist_JMSDown.SetName("histJet2Mass_"+outBinName+"_"+proc[idx]+"_JMSDown")
            hists_sys.append(hist_JMSDown)

            hist_JMRUp =  inFile_this.Get(region+"__fatJet2MassSD_JMRUp")
            hist_JMRUp.SetName("histJet2Mass_"+outBinName+"_"+proc[idx]+"_JMRUp")
            hists_sys.append(hist_JMRUp)
Esempio n. 17
0
                outBinName = region.replace("SR", "").replace("Fail",
                                                              "fail").replace(
                                                                  vbdt, "")
                hist_nominal.SetName("histJet2Mass_" + outBinName + "_" +
                                     proc[idx])

                hists_sys = []
                for sys in systs:
                    if idx == 0:
                        continue
                    print("hist_Up ", region + sys + "Up" + obs)
                    if idx == 1:
                        hist_Up = inFile_this.Get(histname)
                        hist_Up.SetName("histJet2Mass_" + outBinName + "_" +
                                        proc[idx] + "_" +
                                        sys.replace(vbdt, "") + "Up")
                        hist_Down = inFile_this.Get(histname)
                        hist_Down.SetName("histJet2Mass_" + outBinName + "_" +
                                          proc[idx] + "_" +
                                          sys.replace(vbdt, "") + "Down")

                    else:
                        hist_Up = inFile_this.Get(region + sys + "Up" + obs)
                        hist_Up.SetName("histJet2Mass_" + outBinName + "_" +
                                        proc[idx] + "_" +
                                        sys.replace(vbdt, "") + "Up")
                        hist_Down = inFile_this.Get(region + sys + "Down" +
                                                    obs)
                        hist_Down.SetName("histJet2Mass_" + outBinName + "_" +
                                          proc[idx] + "_" +
                                          sys.replace(vbdt, "") + "Down")
 def sys_naming(sys):
     if '_p' in sys:
         return 'sys_'+sys.replace('_p','')
     return ''
    allHist.append(default)

    counter=0
    for mysys in allSys:
        print "===>", sys
        sys=mysys+"High"
        sys2=sys
        if sys.find("photon")>=0 or  sys.find("GeneratorZ")>=0 or  sys.find("Kappa")>=0 or sys.find("QCD")>=0 or sys.find("TopTuneA14")>=0 or sys.find("TopRadiation")>=0:
            sys2="Nom"

        if sys.find("QCD")>=0:
            sys2="Nom"

        
        histHigh=createGraph(bkg,sys,region,bkg2,sys2,region2)
        histLow=createGraph(bkg,sys.replace("High","Low"),region,bkg2,sys2.replace("High","Low"),region2)
        
        if doSym:
            newHists=Symmetrize(default,histLow,histHigh)
            histLow=newHists[0]
            histHigh=newHists[1]


        histHigh.SetLineStyle(2)
        histHigh.SetLineColor(colors[counter])
        histHigh.SetLineWidth(2)

        histLow.SetLineStyle(2)
        histLow.SetLineColor(colors[counter])
        histLow.SetLineWidth(2)
Esempio n. 20
0
            hist_nominal = inFile_this.Get(region + "__fatJet2MassSD")
            outBinName = region.replace("SR",
                                        "").replace("Fail",
                                                    "fail").replace(vbdt, "")
            hist_nominal.SetName("histJet2Mass_" + outBinName + "_" +
                                 proc[idx])

            hists_sys = []
            for sys in systs:
                if idx == 0:
                    continue
                print("hist_Up ", region + sys + "Up__fatJet2MassSD")
                if idx == 1:
                    hist_Up = inFile_this.Get(region + "__fatJet2MassSD")
                    hist_Up.SetName("histJet2Mass_" + outBinName + "_" +
                                    proc[idx] + "_" + sys.replace(vbdt, "") +
                                    "Up")
                    hist_Down = inFile_this.Get(region + "__fatJet2MassSD")
                    hist_Down.SetName("histJet2Mass_" + outBinName + "_" +
                                      proc[idx] + "_" + sys.replace(vbdt, "") +
                                      "Down")

                else:
                    hist_Up = inFile_this.Get(region + sys +
                                              "Up__fatJet2MassSD")
                    hist_Up.SetName("histJet2Mass_" + outBinName + "_" +
                                    proc[idx] + "_" + sys.replace(vbdt, "") +
                                    "Up")
                    hist_Down = inFile_this.Get(region + sys +
                                                "Down__fatJet2MassSD")
                    hist_Down.SetName("histJet2Mass_" + outBinName + "_" +
Esempio n. 21
0
def book_trees(select = ''):
    ordsam = OrderedDict(sorted(samples.items(), key=lambda x: x[1]['order']))

    sig_chain  = ROOT.TChain('sig_data')
    bkg_chain  = ROOT.TChain('bkg_data')
    
    for proc in ordsam:
        chainName = treename.replace('*',proc)
        if ordsam[proc].get("tree","") != "":
            chainName =  treename.replace('*',ordsam[proc].get("tree",""))
        chain      = ROOT.TChain(chainName)
        chainSysUp = []
        chainSysDw = []
        sysReplace = proc
        if samples[proc].get("tree","") != "":
            sysReplace = samples[proc].get("tree","")
        if type(samples[proc].get('name')) == type([]):
            for sam in samples[proc].get('name',[]):
                for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                    chain.Add(f)
                    
                    if 'signal' in str(samples[proc].get('label','')).lower():
                        print 'sig -->', f+'/'+treename.replace('*',proc)
                        sig_chain.Add(f+'/'+treename.replace('*',proc))
                    elif 'data' not in str(samples[proc].get('label','')).lower():
                        print 'bkg -->', f+'/'+treename.replace('*',proc)
                        bkg_chain.Add(f+'/'+treename.replace('*',proc))
            if proc != 'Data' and samples[proc].get('dosysts',True):
                for sys in treesUpSys:
                    print "debug::(",proc,")", sys, " == ", samples[proc].get('label')
                    chainUp = ROOT.TChain(sys.replace('*',sysReplace))
                    for sam in samples[proc].get('name',[]):
                        for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                            chainUp.Add(f)
                        chainSysUp.append(chainUp)
                for sys in treesDwSys:
                    chainDw = ROOT.TChain(sys.replace('*',sysReplace))
                    for sam in samples[proc].get('name',[]):
                        for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                            chainDw.Add(f)
                        chainSysDw.append(chainDw)
        else:
            sam = samples[proc].get('name')
            for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                chain.Add(f)
                if 'signal' in str(samples[proc].get('label','')).lower():
                    print 'sig -->', f+'/'+treename.replace('*',proc)
                    sig_chain.Add(f+'/'+treename.replace('*',proc))
                elif 'data' not in str(samples[proc].get('label','')).lower():
                    print 'bkg -->', f+'/'+treename.replace('*',proc)
                    bkg_chain.Add(f+'/'+treename.replace('*',proc))
            if samples[proc].get('label') != 'Data':
                for sys in treesUpSys:
                    chainUp = ROOT.TChain(sys.replace('*',sysReplace))
                    for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                        chainUp.Add(f)
                    chainSysUp.append(chainUp)
                for sys in treesDwSys:
                    chainDw = ROOT.TChain(sys.replace('*',sysReplace))
                    for f in glob.glob( sampledir + '/*'+ sam +'*.root'):
                        chainDw.Add(f)
                    chainSysDw.append(chainDw)
        # read systematic trees
        samples[proc].update({'_root_tree_'       : chain})
        samples[proc].update({'_root_tree_sysDw_' : chainSysDw})
        samples[proc].update({'_root_tree_sysUp_' : chainSysUp})
        
    logger.info('-- sig_chain.GetEntries() = {0:d}'.format(sig_chain.GetEntries()))
    logger.info('-- bkg_chain.GetEntries() = {0:d}'.format(bkg_chain.GetEntries()))
    return (sig_chain, bkg_chain)
Esempio n. 22
0
        io.save_pickle(os.path.join(out_dir, "models"), R_models)

        front_sorted = paretto.sort_results_by_accuracy(R_models)
        models = [val[0] for val in front_sorted]
        print(models)

        sys = sb.SystemBuilder(verbose=False)
        trigger_train_dataset = os.path.join(data_path, "train_trigger_0")
        test_train_dataset = os.path.join(data_path, "test_trigger_0")

        # Model 1
        for im, m in enumerate(models):

            smallClassifier = make.make_classifier("small", m, "trigger")
            sys.replace("small", smallClassifier)

            small_dict = io.read_pickle(m)
            test_images = len(small_dict['test']['gt'])
            train_images = len(small_dict['train']['gt'])

            # For different thresholds
            for th in np.arange(0 + step_th, 1, step_th):

                # Data
                source = make.make_source(trigger_train_dataset,
                                          test_train_dataset, 2)
                data = make.make_data("trigger_data",
                                      train_images,
                                      test_images,
                                      source=source)