Example #1
0
def print_categories(var, categories = [], select=''):
    for cat in categories:
        heppi.variables.update(
            {
                "_entries_":{
                    "hist" : "(3,0,3)", 
	            "cut"  : "",
	            "blind": "",
	            "log"  : True, 
	            "norm" : False, 
	            "title": "entries"
                }
            }
        )
        cutflow = heppi.variable_cutflow('_entries_','')
        if len(cutflow)!=0:
            cutflow = heppi.variable_cutflow('_entries_',select)
        # === define the stack
        hbkg_sel = ROOT.TH1F('hbkg_sel','',3,0,3)
        hsig_sel = ROOT.TH1F('hsig_sel','',3,0,3)
        hspc_sel = ROOT.TH1F('hspc_sel','',3,0,3)

        hbkg_all = ROOT.TH1F('hbkg_all','',3,0,3)
        hsig_all = ROOT.TH1F('hsig_all','',3,0,3)
        hspc_all = ROOT.TH1F('hspc_all','',3,0,3)

        # === cutflow
        logger.info('\hline')
        logger.info('category( %i ) &%12.3f &&\\' % (categories.index(cat),cat))
        logger.info('\hline')
        logger.info('%12s &%12s &%12s &%12s \\' % ('process', 'total event', 'selection', 'efficiency'))
        ordsam = OrderedDict(sorted(heppi.samples.items(), key=lambda x: x[1]['order']))
        for proc in ordsam:
            tree  = heppi.samples[proc].get('_root_tree_')
            cutflow_sel = ''
            cutflow_all = ''
            if len(cutflow)!=0 :
                cutflow_sel = 'weight*(' + cutflow + ('&& %s > %f' % (var, cat)) +')'
                cutflow_all = 'weight*(' + cutflow + ')'
            else:
                cutflow_sel = 'weight*(' + ('%s > %f' % (var, cat)) +')'
                cutflow_all = 'weight'

            if 'Data' != proc:
                cutflow_all = cutflow_all.replace('weight','weight*%f' % (heppi.treeinfo.get('kfactor',1.0)))
                cutflow_sel = cutflow_sel.replace('weight','weight*%f' % (heppi.treeinfo.get('kfactor',1.0)))
            else:
                cutflow_all = cutflow_all.replace('weight*','')
                cutflow_sel = cutflow_sel.replace('weight*','')
                
            tree.Project(
                'h_sel_entries_' + proc + heppi.variables['_entries_']['hist'],
                '1',
                cutflow_sel
            )
            tree.Project(
                'h_all_entries_' + proc + heppi.variables['_entries_']['hist'],
                '1',
                cutflow_all
            )
            h_sel = ROOT.gDirectory.Get('h_sel_entries_'+proc)
            h_all = ROOT.gDirectory.Get('h_all_entries_'+proc)
            logger.info('%12s &%12.3f &%12.3f &%12.3f \\' % (proc,h_all.Integral(),
                                                           h_sel.Integral(),
                                                           100*float(h_sel.Integral())/float(h_all.Integral())))
            if heppi.samples[proc]['label']== 'background':
                hbkg_sel.Add(h_sel)
                hbkg_all.Add(h_all)
            if heppi.samples[proc]['label']== 'signal':
                hsig_sel.Add(h_sel)
                hsig_all.Add(h_all)
            if heppi.samples[proc]['label']== 'spectator':
                hspc_sel.Add(h_sel)
                hspc_all.Add(h_all)
                
        # -------------------------------------------------------------------
        logger.info('\hline')
        logger.info('%12s &%12.3f &%12.3f &%12.3f \\' % ('BKG ',
                                                         hbkg_all.Integral(),
                                                         hbkg_sel.Integral(),
                                                         100*float(hbkg_sel.Integral())/float(hbkg_all.Integral())))
        logger.info('%12s &%12.3f &%12.3f &%12.3f \\' % ('SIG',
                                                         hsig_all.Integral(),
                                                         hsig_sel.Integral(),
                                                         100*float(hsig_sel.Integral())/float(hsig_all.Integral())))
        logger.info('\hline')
        logger.info('S/\sqrt(S+B) = %12.3f' % (
            float(hsig_sel.Integral())/math.sqrt(hsig_sel.Integral()+hbkg_sel.Integral()) ))
        logger.info('\hline')
Example #2
0
def draw_ROC(var ='dipho_dijet_MVA',
             label='VBF', title = 'variable', axistitle='',
             bkg = [], select='', categories=[]):
    #c = ROOT.TCanvas("c_ROC","ROC's BDT",600,600)
    #c.cd()
    histos = []
    histfilename = ('plots/histogram_stack_'  +
                    var + '_' + label + '_'
                    + heppi.selection['name'])
    cutflow = heppi.variable_cutflow(var,'')
    if len(cutflow)!=0:
        cutflow = heppi.variable_cutflow(var,select)
    # === define the stack
    hbkg = ROOT.TH1F('hbkg','',400,-1,1)
    hsig = ROOT.TH1F('hsig','',400,-1,1)
    hbkg.SetTitle(";" + heppi.variables[var]['title']+";entries")
    hsig.SetTitle(";" + heppi.variables[var]['title']+";entries")
    # === cutflow
    if len(cutflow)!=0 :
        cutflow = 'weight*(' + cutflow + ')'
    else:
        cutflow = 'weight'
    
    # === loop over the samples
    ordsam = OrderedDict(sorted(heppi.samples.items(), key=lambda x: x[1]['order']))
    for proc in ordsam:
        tree  = heppi.samples[proc].get('_root_tree_')#roof.Get(treename.replace('*',proc))
        histstr = '(%i,%f,%f)' %(int(400),-1, 1)
        if proc == 'Data':
            cutflow = cutflow.replace('weight*','').replace('weight','')
            
        tree.Project(
            'h_'+var + histstr,
            var,
            cutflow
        )
        if heppi.samples[proc]['label']!= 'Signal' and proc in bkg:
            print '--> have ', proc
            hbkg.Add(ROOT.gDirectory.Get('h_'+var))
        if heppi.samples[proc]['label']== 'signal':
            hsig.Add(ROOT.gDirectory.Get('h_'+var))
    
    hbkg.SetFillColorAlpha(122,0.6)
    hbkg.SetLineColor(1)
    hsig.SetLineColor(132)
    hsig.SetLineWidth(3)
    hbkg.GetYaxis().SetRangeUser(0,hbkg.GetMaximum() + hbkg.GetMaximum() * 0.5)
    
    roc   = ROOT.TGraph()
    roc.SetName ('fom_cat_'+label)
    roc.SetTitle( axistitle )
    catcut = []
    point = []
    cutpt = []
    roc.SetPoint (0,1,1)
    for ibin in range(0, hsig.GetNbinsX()+1):
        #print hbkg.Integral() 
        beff = hbkg.Integral(ibin,hsig.GetNbinsX())/hbkg.Integral() 
        seff = hsig.Integral(ibin,hsig.GetNbinsX())/hsig.Integral()
        #print beff, '  ', seff
        for cat in categories:
            if abs(hsig.GetBinCenter(int(ibin))-cat) < 0.005 :
                print 'diff :: val(', hsig.GetBinCenter(ibin),') ',abs(hsig.GetBinCenter(int(ibin))-cat)
                p = ROOT.TEllipse(beff,seff,0.013,0.013)
                p.SetFillColor(2)
                p.SetLineWidth(0)
                p.SetLineColor(2)
                point.append(p)
        roc.SetPoint (ibin+1,beff,seff)
    roc.SetPoint (hsig.GetNbinsX()+2,0,0)
    #roc.GetYaxis().SetLabelSize(25)
    roc.GetYaxis().SetTitleSize(25)
    roc.GetXaxis().SetTitleSize(25)
    roc.GetYaxis().SetTitleOffset (1)
    roc.GetXaxis().SetTitleOffset (1)
    roc.SetLineColor(129)
    roc.SetLineWidth(3)
    #roc.Draw('AL')
    roc.GetYaxis().SetRangeUser(0,1)
    roc.GetXaxis().SetRangeUser(0,1)
    #roc.Draw('AL')
    
    #c.Update()
    #
    #draw_labels("Integral = %1.3f" % (roc.Integral()+0.5),"bl",0.035)
    #for p in point[:-1]:
    #    p.Draw()
    ##raw_input()
    #c.SaveAs('plots/rocky_' + label + '.pdf')
    #c.SaveAs('plots/rocky_' + label + '.png')
    
    return roc