Beispiel #1
0
attr = 'attr/q1.json'
with open( query_path + attr, 'r' ) as f:
    faces = json.load( f )
age_range = [ (1,3), (3,8), (8,13), (13,20), (20,37), (37,66), (66,121) ]
age_cate = [ range(s, e) for s, e in age_range ]
v_label_dict = { (1, 1):0, (5, 1):1, (10, 1):2, (16, 1):3, (28, 1):4, (51, 1):5, (75, 1):6,
                 (1, 2):7, (5, 2):8, (10, 2):9, (16, 2):10, (28, 2):11, (51, 2):12, (75, 2):13 }

f_cen_lst = []
for idx, face in enumerate( faces ):   
    f_eye_l = face['faceLandmarks']['eyeLeftInner']
    f_eye_r = face['faceLandmarks']['eyeRightInner']
    f_cen = ( (f_eye_l['x'] + f_eye_r['x'])/2.0, (f_eye_l['y'] + f_eye_r['y'])/2.0 )
    f_gen = [ 1 if face['attributes']['gender'] else 0 ][0]
    f_age_s = face['attributes']['age']
    f_age = [ int(fn.median(a_c)) for a_c in age_cate if f_age_s in a_c ][0]
    v_label = v_label_dict[ f_age, f_gen ]
    G_q.add_node( idx, label=v_label )
    f_cen_lst.append( f_cen )
    
f_cen_combi = list( itertools.combinations( f_cen_lst, 2 ) )
for a, b in f_cen_combi:
    px_dist = fn.euclidean_dist( a, b )
    a_idx = f_cen_lst.index( a )
    b_idx = f_cen_lst.index( b )
    G_q.add_edge( a_idx, b_idx, weight=px_dist )

# Minimum Spanning Tree
G_q_mst = nx.minimum_spanning_tree( G_q )   
# Set initial number of Order Distance as 0 For MST edges
for v1, v2 in G_q_mst.edges():
Beispiel #2
0
    (10, 2): 9,
    (16, 2): 10,
    (28, 2): 11,
    (51, 2): 12,
    (75, 2): 13
}

f_cen_lst = []
for idx, face in enumerate(faces):
    f_eye_l = face['faceLandmarks']['eyeLeftInner']
    f_eye_r = face['faceLandmarks']['eyeRightInner']
    f_cen = ((f_eye_l['x'] + f_eye_r['x']) / 2.0,
             (f_eye_l['y'] + f_eye_r['y']) / 2.0)
    f_gen = [1 if face['attributes']['gender'] else 0][0]
    f_age_s = face['attributes']['age']
    f_age = [int(fn.median(a_c)) for a_c in age_cate if f_age_s in a_c][0]
    v_label = v_label_dict[f_age, f_gen]
    G_q.add_node(idx, label=v_label)
    f_cen_lst.append(f_cen)

f_cen_combi = list(itertools.combinations(f_cen_lst, 2))
for a, b in f_cen_combi:
    px_dist = fn.euclidean_dist(a, b)
    a_idx = f_cen_lst.index(a)
    b_idx = f_cen_lst.index(b)
    G_q.add_edge(a_idx, b_idx, weight=px_dist)

# Minimum Spanning Tree
G_q_mst = nx.minimum_spanning_tree(G_q)
# Set initial number of Order Distance as 0 For MST edges
for v1, v2 in G_q_mst.edges():
Beispiel #3
0
def build_facegraph(json_obj):
    """
    # Build Face Graph from query image 
    """
    # create Graph of Query & BoFG
    G_q = nx.Graph()
    G_bofg = nx.Graph()
    # from Query
    faces = json_obj
    age_range = [(1, 3), (3, 8), (8, 13), (13, 20), (20, 37), (37, 66), (66, 85)]
    age_cate = [range(s, e) for s, e in age_range]
    v_label_dict = {
        (1, 1): 0,
        (5, 1): 1,
        (10, 1): 2,
        (16, 1): 3,
        (28, 1): 4,
        (51, 1): 5,
        (75, 1): 6,
        (1, 2): 7,
        (5, 2): 8,
        (10, 2): 9,
        (16, 2): 10,
        (28, 2): 11,
        (51, 2): 12,
        (75, 2): 13,
    }

    f_cen_lst = []
    for idx, face in enumerate(faces):
        f_eye_l = face["faceLandmarks"]["eyeLeftInner"]
        f_eye_r = face["faceLandmarks"]["eyeRightInner"]
        f_cen = ((f_eye_l["x"] + f_eye_r["x"]) / 2.0, (f_eye_l["y"] + f_eye_r["y"]) / 2.0)
        f_gen = [1 if face["attributes"]["gender"] else 0][0]
        f_age_s = face["attributes"]["age"]
        f_age = [int(fn.median(a_c)) for a_c in age_cate if f_age_s in a_c][0]
        v_label = v_label_dict[f_age, f_gen]
        G_q.add_node(idx, label=v_label)
        f_cen_lst.append(f_cen)

    f_cen_combi = list(itertools.combinations(f_cen_lst, 2))
    for a, b in f_cen_combi:
        px_dist = fn.euclidean_dist(a, b)
        a_idx = f_cen_lst.index(a)
        b_idx = f_cen_lst.index(b)
        G_q.add_edge(a_idx, b_idx, weight=px_dist)

    # Minimum Spanning Tree
    G_q_mst = nx.minimum_spanning_tree(G_q)
    # Set initial number of Order Distance as 0 For MST edges
    for v1, v2 in G_q_mst.edges():
        G_q_mst[v1][v2]["weight"] = 0
    # Assign Order Distance for All Edges
    G_q_od = nx.all_pairs_shortest_path_length(G_q_mst, cutoff=4)
    # Remove the edges visiting itself
    # Decrease Order Distances with -1
    # Copy MST's Order Distance to Edges Attrs. of Graph G
    for v1 in G_q_od.keys():
        for v2 in G_q_od[v1].keys():
            if v1 == v2:  # Remove Nodes self to self
                del G_q_od[v1][v2]
            else:  # Modify Weights of Other Nodes & Assign them to Graph G
                # Edge Label
                order_dist = G_q_od[v1][v2]
                order_dist -= 1
                G_q.add_edge(v1, v2, weight=order_dist)

    return G_q
Beispiel #4
0
 def Median(self):
     return median( self.__bins )
Beispiel #5
0
def analyse(Algorithm, plotbranches, plotreverselookup, plotconfig, trees, cutstring, hist, leg1, leg2, fileid, records, ptreweight = True, varpath = "", savePlots = True, mass_min = "0.0", mass_max = "1200000.0", scaleLumi = 1, nTracks='999'):
    '''
    Run through the Algorithm for a given mass range.  Returns the bkg rej at 50% signal eff.
    Keyword args:
    Algorithm -- Name of the algorithm.  Set in main, comes from config file.
    plotbranches -- the variables to be plotted.  Set in main.
    plotreverselookup -- Lookup for an algorithm from a variable stub
    plotconfig --- Dictionary of the plotting arguments for different variables.
    trees -- contains the actual data.
    cutstring -- basic selection cuts to be applied.
    hist -- Histograms that will be filled/ saved.
    leg1 -- TLegend for histograms
    leg2 -- TLegend for ROC curves
    fileid -- File identifier that gets used in the output file names
    ptreweight -- Reweight signal according to pT
    varpath -- output folder to save single variable plots to
    saveplots -- whether or not to write out plots to file
    mass_min -- Mass window minimum
    mass_max -- Mass window maximum
    scaleLumi -- luminosity scale factor
    nTracks -- cut on NTracks to apply.  Default is 999 which is essentially no cut.

    Returns:
    Background rejection at 50% signal efficiency using the ROC curve and variable used to achieve maximum rejection.
    '''

    # open log file
    #logfile = open(records,'w')
    cutflow = open(records.replace('.out','.cutflow'),'w')

    # canvas for histogram plots
    canv1 = TCanvas("canv1")
    canv1.Divide(5,5)
    # canvas for rejection ROC curves
    canv2 = TCanvas("canv2", "ROC curves showing 1-background rejection vs signal efficiency")
    # canvas for power roc curves
    power_canv = TCanvas('powercurves',"ROC curves showing background rejection power vs signal efficiency")
    power_canv.SetLogy()
    power_legend = TLegend(0.6,0.5,0.9,0.9); power_legend.SetFillColor(kWhite);

    tempCanv = TCanvas("temp")
    # reset hists
    for h in hist.keys():
        hist[h].Reset()
    
    global weightedxAOD
    global singleSidedROC

    global totalrejection
    # dict containing all of the ROC curves
    roc={}
    roc_nomw = {}
    bkgRejROC= {}
    bkgPowerROC = {}
    # bool that is set to false if no ROC curves are drawn - this will happen if any 
    # hist added to the roc is empty
    writeROC = False

    # dictionary containing the 50% signal eff bkg rejection power
    roc_rejection_scores = {}

    # dictionary holding all of the histograms without a mass cut
    hist_nomw = {}
    saveNoMassWindowPlots = savePlots
    hist_mass_nomw = {}

    # record the integral of pt for mw and nomw
    mw_int_pt = {}
    full_int_pt = {}

    # maximum rejection
    maxrej = 0
    maxrejvar = ''
    #set up the cutstring/ selection to cut on the correct jet masses
    cutstring_mass = cutstring+ " * (jet_" +Algorithm + "_m <= " +mass_max+ ")" + " * (jet_" +Algorithm + "_m > " +mass_min+ ") " 
    # loop through the indices and branchnames
    for index, branchname in enumerate(plotbranches):
        # add ROC dictionary entry
        roc[branchname] = TGraph()#Errors()
        roc[branchname].SetTitle(branchname)
        roc[branchname].SetName('roc_'+branchname)
        roc_nomw[branchname] = TGraph()#Errors()
        roc_nomw[branchname].SetTitle('No mass window ' + branchname)
        roc_nomw[branchname].SetName('roc_nomw_'+branchname)
        # add bkg rej power dictionary entry
        bkgRejROC[branchname] = TGraph()#Errors()
        bkgRejROC[branchname].SetTitle(branchname)
        bkgRejROC[branchname].SetName('bkgrej_roc_'+branchname)
        bkgPowerROC[branchname] = TGraph()#Errors()
        bkgPowerROC[branchname].SetTitle(branchname)
        bkgPowerROC[branchname].SetName('bkgpower_roc_'+branchname)
        # new canvas
        canv1.cd(index+1)

        # keep the integral when not applying mass window cuts, this
        # allows us to calculate the efficiency of the mass window cut
        signal_eff = 1.0
        bkg_eff = 1.0

        # setting the maximum of the plot - normally multiply by 1.2, but for some, like tauwta21 must be higher
        max_multiplier = 1.2
        if branchname.lower().find('tauwta2tauwta1') != -1:
            max_multiplier = 1.25


        # loop through the datatypes: signal and background
        for indexin, datatype in enumerate(trees):
            canv1.cd(index+1)
            histname =  datatype + "_" + branchname

            print "plotting " + datatype + branchname
            #logfile.write("plotting " + datatype + branchname+"\n")


            minxaxis = hist[histname].GetXaxis().GetXmin()
            maxxaxis = hist[histname].GetXaxis().GetXmax()
            # add the mc_weight and weighted number of events to the selection string
            # also make sure that the variable being plotted is within the bounds specified 
            # in the config file (the limits on the histogram)
            if not weightedxAOD:
                cutstringandweight = '*mc_event_weight*1./NEvents(mc_channel_number)'
            else:
                cutstringandweight = '*mc_event_weight*1./evt_nEvts'

            # add the cross section and filter efficiency for the background
            
            if datatype == 'bkg': 
                # no longer apply the k factor
                if not weightedxAOD:
                    cutstringandweight += '*filter_eff*xs'#*k_factor'
                else:
                    cutstringandweight += '*evt_filtereff*evt_xsec'#*evt_kfactor'
                hist[histname].SetMarkerStyle(21)
            # filter efficiency for signal
            elif datatype == 'sig':
                # we only apply pt rw now, so reset the cutstring
                cutstringandweight=''
                # apply pt reweighting to the signal
                if ptreweight:
                    cutstringandweight +='*SignalPtWeight3(jet_CamKt12Truth_pt)'
            
            hist[histname].Sumw2();
            # set up the tree.Draw() variable expression for the histogram
            varexp = branchname + '>>' + histname
            # apply the selection to the tree and store the output in the histogram
            if branchname.find('_m')==-1:
                trees[datatype].Draw(varexp,cutstring_mass+cutstringandweight)#+'*(nTracks<'+nTracks+')')
            else:
                trees[datatype].Draw(varexp,cutstring+cutstringandweight)#+'*(nTracks<'+nTracks+')')

            # if the histogram is not empty then normalise it
            
            #mw_int = hist[histname].Integral()

            if hist[histname].Integral() > 0.0:
                if scaleLumi != 1:
                    hist[histname].Scale(scaleLumi);
                else:
                    hist[histname].Scale(1.0/hist[histname].Integral());


            # set up the axes titles and colours/ styles
            hist[histname].SetLineStyle(1); hist[histname].SetFillStyle(0); hist[histname].SetMarkerSize(1);
            if (branchname.find('jet_')!=-1):
                hist[histname].SetXTitle(plotreverselookup[branchname.replace("jet_"+Algorithm,"")])
            else:
                hist[histname].SetXTitle(plotreverselookup[branchname])
            hist[histname].SetYTitle("Normalised Entries")

            #now get the same plot for no mass window cut to get the eff
            hist_full_massonly = hist[histname].Clone()
            hist_full_massonly.Reset()
            hist_full_massonly.SetName(histname+'_massonly')
            # need to store the variable in this histogram
            varexpfull = branchname + ' >>' + histname+'_massonly'
            # no nTracks cut here!!!!
            if branchname.find('_m')==-1:
                trees[datatype].Draw(varexpfull,cutstring_mass+cutstringandweight)
            else:
                trees[datatype].Draw(varexpfull,cutstring+cutstringandweight)
            # get the integral and normalise
            mw_int = hist_full_massonly.Integral()
            
            hist_full_massonly.Reset()
            hist_full_massonly.SetName(histname+'_full_massonly')
            # need to store the variable in this histogram
            varexpfull = branchname + ' >>' + histname+'_full_massonly'
            # no nTracks cut here!!!!
            trees[datatype].Draw(varexpfull,cutstring+cutstringandweight+"*(jet_" +Algorithm + "_m < 1200*1000)" + " * (jet_" +Algorithm + "_m > 0)")
            # get the integral and normalise
            full_int = hist_full_massonly.Integral()

            
            if histname.find('_pt') != -1:
                mw_int_pt[datatype] = mw_int
                full_int_pt[datatype] = full_int


            #now get the same plot for no mass window cut to get the eff
            hist_full = hist[histname].Clone()
            hist_full.Reset()
            hist_full.SetName(histname+'_full')
            # need to store the variable in this histogram
            varexpfull = branchname + ' >>' + histname+'_full'

            trees[datatype].Draw(varexpfull,cutstring+cutstringandweight+"*(jet_" +Algorithm + "_m < 1200*1000)" + " * (jet_" +Algorithm + "_m > 0)")#+'*(nTracks<'+nTracks+')')
            histInt = hist_full.Integral()
            # now scale
            if histInt != 0.0:
                if scaleLumi!=1:
                    hist_full.Scale(scaleLumi)
                else:
                    hist_full.Scale(1./histInt)
            # change the x title
            hist_full.SetXTitle(hist[histname].GetXaxis().GetTitle()+' (no mass window)')

            #save this histogram to the no mass window histo dictionary
            hist_nomw[histname+'_full'] = hist_full.Clone()

            if False:#datatype == 'sig':
                if full_int !=0:
                    signal_eff = float(mw_int/full_int)
                else:
                    signal_eff = 0.0
            if False:#else:
                if full_int != 0:
                    bkg_eff = float(mw_int/full_int)
                else:
                    bkg_eff = 0.0

        #Make ROC Curves before rebinning, but only if neither of the samples are zero
        if (hist["sig_" +branchname].Integral() != 0 and hist["bkg_" +branchname].Integral() != 0):

            if singleSidedROC == 'M':
                # check where the median is so we can correctly choose L or R cut.
                sig_median = fn.median(hist['sig_'+branchname])
                bkg_median = fn.median(hist['bkg_'+branchname])

                if sig_median > bkg_median:
                    side = 'R'
                else:
                    side = 'L'

                roc[branchname] = fn.RocCurve_SingleSided(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=side)

                #roc[branchname],hsigreg50,hcutval50,hsigreg25,hcutval25 = fn.RocCurve_SingleSided_WithUncer(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=side)
                bkgRejROC[branchname] = roc[branchname]
                bkgPowerROC[branchname] = fn.RocCurve_SingleSided(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=side,rejection=False)
                

            elif singleSidedROC == 'L' or singleSidedROC == 'R':
                roc[branchname] = fn.RocCurve_SingleSided(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=singleSidedROC)
                #roc[branchname],hsigreg50,hcutval50,hsigreg25,hcutval25 = fn.RocCurve_SingleSided_WithUncer(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=singleSidedROC)
                bkgRejROC[branchname] = roc[branchname]
                bkgPowerROC[branchname] = fn.RocCurve_SingleSided(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside=singleSidedROC,rejection=False)

            else:
                MakeROCBen(1, hist["sig_" +branchname], hist["bkg_" +branchname], roc[branchname], bkgRejROC[branchname], signal_eff, bkg_eff)
                bkgPowerROC[branchname] = fn.RocCurve_SingleSided(hist["sig_" +branchname], hist["bkg_" +branchname], signal_eff, bkg_eff, cutside='R',rejection=False)
            writeROC = True
            
        canv1.cd(index+1)
        pX = Double(0.5)
        pY = Double(0.0)

        # find the corresponding bkg rejection for the 50% signal efficiency point from bkg rejection power ROC curve
        # However, if we want the background rejection power for the mass variable we do not want to take 50% as we already have made
        # a cut on the mass to get it to 68%.
        # calculate error on this as well -> deltaX = X*(deltaY/Y)
        eval_roc = 1.0
        err_up = 1
        err_do = 1
        if not branchname.endswith("_m"):
            #eval_roc = roc[branchname].Eval(0.5)
            eval_roc = fn.GetBGRej50(roc[branchname])
            # get the bin for this point so that we can find the associated error in roc error tgraphs
            #rocBin = fn.findYValue(roc[branchname],Double(0.5), pY, 0.01, True, True)
            bin_num = roc[branchname].GetXaxis().FindBin(0.5)#roc_errUp[branchname].Eval(0.5)
            eval_rocup = eval_roc+roc[branchname].GetErrorX(bin_num)
            eval_rocdo = eval_roc-roc[branchname].GetErrorX(bin_num)
        else:
            #eval_roc = roc[branchname].Eval(0.68)
            eval_roc = fn.GetBGRej(roc[branchname], 0.68)
            #rocBin = fn.findYValue(roc[branchname],Double(0.68), pY, 0.01, True, True)
            bin_num = roc[branchname].GetXaxis().FindBin(0.68)#roc_errUp[branchname].Eval(0.5)
            eval_rocup = eval_roc+roc[branchname].GetErrorX(bin_num)
            eval_rocdo = eval_roc-roc[branchname].GetErrorX(bin_num)

        if eval_roc != 1:
            bkgrej = 1/(1-eval_roc)
        else:
            bkgrej = -1
        
        roc_rejection_scores[branchname] = bkgrej

        if (eval_rocup != 1):
            bkgrej_errUp = abs(bkgrej-1/(1-eval_rocup))
        else:
            bkgrej_errUp = -1
        if (eval_rocdo!= 1):
            bkgrej_errDo = abs(bkgrej-1/(1-eval_rocdo))
        else:
            bkgrej_errDo = -1

        # store a record of all background rejection values
        # want to store only the variable name, not the algorithm name, so string manipulation.  here it is stored as sig_jet_ALGO_variable.
        groups = branchname.split('_')
        j = '_'.join(groups[:2]), '_'.join(groups[2:])

        if not j[1] == 'pt':
            totalrejection.append([j[1], float(bkgrej), float(bkgrej_errUp), float(bkgrej_errDo)])

        if bkgrej > maxrej:
            maxrej = bkgrej
            maxrejvar = branchname

        # once the background rejection power has been calculated using the 200 bins the histograms can be rebinned.
            
        hist['sig_'+branchname].SetFillColor(4); hist['sig_'+branchname].SetLineColor(4); hist['sig_'+branchname].SetMarkerColor(4); hist['sig_'+branchname].Rebin(4);
        hist['bkg_'+branchname].SetFillColor(2); hist['bkg_'+branchname].SetLineColor(2);  hist['bkg_'+branchname].SetMarkerColor(2);  hist['bkg_'+branchname].Rebin(4);
        if saveNoMassWindowPlots:
            if singleSidedROC == 'M':
                # check where the median is so we can correctly choose L or R cut.
                sig_median = fn.median(hist_nomw['sig_'+branchname+'_full'])
                bkg_median = fn.median(hist_nomw['bkg_'+branchname+'_full'])

                if sig_median > bkg_median:
                    side = 'R'
                else:
                    side = 'L'
                    
                #roc_nomw[branchname],v1,v2,v3,v4 = fn.RocCurve_SingleSided_WithUncer(hist_nomw["sig_" +branchname+'_full'], hist_nomw["bkg_" +branchname+'_full'], 1,1, cutside=side)
                roc_nomw[branchname] = fn.RocCurve_SingleSided(hist_nomw["sig_" +branchname+'_full'], hist_nomw["bkg_" +branchname+'_full'], 1,1, cutside=side, rejection=True, debug_flag=False)

            elif singleSidedROC == 'L' or singleSidedROC == 'R':
                #roc_nomw[branchname],v1,v2,v3,v4 = fn.RocCurve_SingleSided_WithUncer(hist_nomw["sig_" +branchname+'_full'], hist_nomw["bkg_" +branchname+'_full'], 1,1, cutside=singleSidedROC)
                roc_nomw[branchname] = fn.RocCurve_SingleSided(hist_nomw["sig_" +branchname+'_full'], hist_nomw["bkg_" +branchname+'_full'], 1,1, cutside=singleSidedROC, rejection=True, debug_flag=False)

            else:
                MakeROCBen(1, hist_nomw["sig_" +branchname+'_full'], hist_nomw["bkg_" +branchname+'_full'], roc_nomw[branchname], TGraphErrors(), signal_eff, bkg_eff)
            canv1.cd(index+1)
            #roc_nomw[branchname].GetXaxis().SetTitle("Efficiency_{W jets}")
            #roc_nomw[branchname].GetYaxis().SetTitle("1 - Efficiency_{QCD jets}")
            hist_nomw['sig_'+branchname+'_full'].SetFillColor(4); hist_nomw['sig_'+branchname+'_full'].SetLineColor(4); hist_nomw['sig_'+branchname+'_full'].SetMarkerColor(4); 
            hist_nomw['bkg_'+branchname+'_full'].SetFillColor(2); hist_nomw['bkg_'+branchname+'_full'].SetLineColor(2);  hist_nomw['bkg_'+branchname+'_full'].SetMarkerColor(2);
            # resize the mass plots to be in a better range
            if branchname.endswith('_m'):
                hist['sig_'+branchname].SetAxisRange(0.0,300.0*1000.0)
                hist['bkg_'+branchname].SetAxisRange(0.0,300.0*1000.0)
                hist['sig_'+branchname].GetXaxis().SetLimits(0.0,300.0)
                hist['bkg_'+branchname].GetXaxis().SetLimits(0.0,300.0)
            else:
                hist_nomw['sig_'+branchname + '_full'].Rebin(4);
                hist_nomw['bkg_'+branchname + '_full'].Rebin(4);

        leg1.Clear()
        # add legend entries for bkg and signal histograms
        leg1.AddEntry(hist["sig_" + branchname],"W jets","l");    leg1.AddEntry(hist["bkg_" + branchname],"QCD jets","l");

        y_high = max(hist['sig_'+branchname].GetMaximum(), hist['bkg_'+branchname].GetMaximum())
        hist['bkg_'+branchname].SetMaximum(y_high*max_multiplier);hist['sig_'+branchname].SetMaximum(y_high*max_multiplier)
        # plot the maximum histogram
        #if (hist['sig_'+branchname].GetMaximum() > hist['bkg_'+branchname].GetMaximum()):
        fn.drawHists(hist['sig_' + branchname], hist['bkg_' + branchname])
        # else:
        #    fn.drawHists(hist['bkg_' + branchname], hist['sig_' + branchname])
        # change the coordinates to LHS of the canvas if we are plotting ThrustMaj or YFilt.
        offset_x = False
        if branchname.lower().find('thrustmaj')!=-1 or branchname.lower().find('yfilt')!=-1:
            offset_x = True
            leg1.SetX1NDC(0.25)
            leg1.SetX2NDC(0.35)
        else:
            leg1.SetX1NDC(0.8)
            leg1.SetX2NDC(0.9)
        leg1.Draw("same")

        # add correctly formatted text to the plot for the ATLAS collab text, energy, etc.
        fn.addLatex(fn.getAlgorithmString(),fn.getAlgorithmSettings(),fn.getPtRange(), fn.getE(), [fn.getNvtxLow(), fn.getNvtx()], offset_x)
        # save individual plots
        if savePlots:
            p = canv1.cd(index+1).Clone() 
            tempCanv.cd()
            p.SetPad(0,0,1,1) # resize
            p.Draw()
            tempCanv.SaveAs(varpath+branchname+".pdf")
            #tempCanv.SaveAs(varpath+branchname+".eps")
            del p

        # now save "no mass window" plots
        if saveNoMassWindowPlots:
            # resize the mass plots to be in a better range
            if branchname.find('_m') != -1:
                hist_nomw['sig_'+branchname+'_full'].SetAxisRange(0.0,300.0*1000.0)
                hist_nomw['sig_'+branchname+'_full'].GetXaxis().SetRangeUser(0.0,1000.0*300.0)
                hist_nomw['sig_'+branchname+'_full'].GetXaxis().SetLimits(0.0,300.0)
                hist_nomw['bkg_'+branchname+'_full'].SetAxisRange(0.0,300.0*1000.0)
                hist_nomw['bkg_'+branchname+'_full'].GetXaxis().SetRangeUser(0.0,1000.0*300.0)
                hist_nomw['bkg_'+branchname+'_full'].GetXaxis().SetLimits(0.0,300.0)
            tempCanv2 = TCanvas("tempnomw"+branchname)
            tempCanv2.cd()
            y_high = max(hist_nomw['sig_'+branchname+'_full'].GetMaximum(), hist_nomw['bkg_'+branchname+'_full'].GetMaximum())
            y_low = 0.0 #min(hist_nomw['sig_'+branchname+'_full'].GetMinimum(), hist_nomw['bkg_'+branchname+'_full'].GetMaximum())
            hist_nomw['bkg_'+branchname+'_full'].SetMaximum(y_high*max_multiplier);hist_nomw['sig_'+branchname+'_full'].SetMaximum(y_high*max_multiplier)
            #if (hist_nomw['sig_'+branchname+'_full'].GetMaximum() > hist_nomw['bkg_'+branchname+'_full'].GetMaximum()):
            fn.drawHists(hist_nomw['sig_' + branchname+'_full'], hist_nomw['bkg_' + branchname+'_full'])
            #else:
            #    fn.drawHists(hist_nomw['bkg_' + branchname+'_full'], hist_nomw['sig_' + branchname+'_full'])

            offset_x = False
            if branchname.lower().find('thrustmaj')!=-1 or branchname.lower().find('yfilt')!=-1:
                offset_x = True
                leg1.SetX1NDC(0.25)
                leg1.SetX2NDC(0.35)
            else:
                leg1.SetX1NDC(0.8)
                leg1.SetX2NDC(0.9)
            leg1.Draw("same")

            # if we are doing the mass plt we want to indicate the mass window
            if branchname.find('_m') == -1:
                mrange = []
            else:
                mrange = [float(mass_min), float(mass_max)]
                # if it is the mass variable then draw lines indicating the mass window
                line_min = TLine(float(mass_min)/1000.0, y_low, float(mass_min)/1000.0, y_high); line_min.SetLineColor(kBlack);
                line_max = TLine(float(mass_max)/1000.0, y_low, float(mass_max)/1000.0, y_high); line_max.SetLineColor(kBlack);
                line_min.Draw('same')
                line_max.Draw('same')

            # add the latex parts to the plot -> ATLAS, tev range, etc.
            fn.addLatex(fn.getAlgorithmString(),fn.getAlgorithmSettings(),fn.getPtRange(), fn.getE(), [fn.getNvtxLow(), fn.getNvtx()], offset_x, massrange = mrange)

            tempCanv2.SaveAs(varpath+branchname+"_noMW.pdf")
            #tempCanv2.SaveAs(varpath+branchname+"_noMW.eps")
            del tempCanv2

        
        canv1.cd(index+1)

    min_roc = 1.0
    for b in roc.keys():
        if b.find('_m') == -1 and b.find('_pt') == -1:
            n = roc[b].GetN()
            y = roc[b].GetY()
            locmin = TMath.LocMin(n,y)
            minval = y[locmin]
            min_roc = min(min_roc, minval)
            
    for branchidx, branchname in enumerate(roc.keys()):
        # plot the ROC curves
        canv2.cd()
        roc[branchname].GetXaxis().SetTitle("Efficiency_{W jets}")
        roc[branchname].GetYaxis().SetTitle("1 - Efficiency_{QCD jets}")
        roc[branchname].SetMinimum(min_roc*0.98)
        bkgPowerROC[branchname].GetXaxis().SetTitle("Efficiency_{W jets}")
        bkgPowerROC[branchname].GetYaxis().SetTitle("1/Efficiency_{QCD jets}")
    

        # only plot the roc and power curves if this is not a mass
        if branchname.find('_m') == -1 and branchname.find('_pt') == -1:
            roc[branchname].SetLineStyle(branchidx%10)
            if branchidx==0 and roc[branchname].Integral() != 0:
                roc[branchname].Draw("al")        
            elif roc[branchname].Integral() != 0:
                roc[branchname].SetLineColor(colours[branchidx])
                roc[branchname].Draw("same")
                # legend for the roc curve
            leg2.AddEntry(roc[branchname],branchname,"l");
            leg2.Draw("same")

            # plot the power curves
            power_canv.cd()
            bkgPowerROC[branchname].SetLineStyle(branchidx%10)
            if branchidx==0 and bkgPowerROC[branchname].Integral() != 0:
                bkgPowerROC[branchname].Draw("al")        
            elif bkgPowerROC[branchname].Integral() != 0:
                bkgPowerROC[branchname].SetLineColor(colours[branchidx])
                bkgPowerROC[branchname].Draw("same")
            power_legend.AddEntry(bkgPowerROC[branchname],branchname,'l')
            power_legend.Draw('same')

    # write out canv1 and roc curves on one page/ png each
    if savePlots:
        #write out the plots after cuts
        writePlots(Algorithm, fileid, canv1, canv2, writeROC, roc, power_canv)
        writePlotsToROOT(Algorithm, fileid, hist, roc, bkgRejROC, roc_nomw, roc_rejection_scores, recreate=True, power_curves = bkgPowerROC)
        #responseHists = resp.writeResponsePlots(weightedxAOD, Algorithm, plotconfig, trees, cutstring, fileid, ptreweight, varpath, mass_min, mass_max, scaleLumi)
        #responseHists = resp.writeResponsePlots(weightedxAOD, Algorithm, plotconfig, trees, cutstring, fileid, ptreweight, varpath, mass_min, mass_max, scaleLumi, applyMassWindow=False)
        #writePlotsToROOT(Algorithm, fileid, hist, recreate=False)

    # write out event counts for mass window cuts (not weighted)
    cutflow.write('Signal:\njet selection no mass window: '+'pt' +' '+ str(hist_nomw['sig_jet_'+Algorithm+'_pt_full'].GetEntries())+'\n')
    if 'sig' in full_int_pt.keys():
        cutflow.write('jet selection no mass window: '+'pt weighted '+ str(full_int_pt['sig'])+'\n')
    cutflow.write('mass window and jet selection: pt ' + str(hist['sig_jet_'+Algorithm+'_pt'].GetEntries())+'\n')
    if 'sig' in mw_int_pt.keys():
        cutflow.write('mass window and jet selection: pt weighted ' + str(mw_int_pt['sig'])+'\n')
    cutflow.write('Background:\njet selection no mass window: pt '+ str(hist_nomw['bkg_jet_'+Algorithm+'_pt_full'].GetEntries())+'\n')
    if 'bkg' in full_int_pt.keys():
        cutflow.write('jet selection no mass window: pt weighted '+ str(full_int_pt['bkg'])+'\n')
    cutflow.write('mass window and jet selection: pt ' + str(hist['bkg_jet_'+Algorithm+'_pt'].GetEntries())+'\n')
    if 'bkg' in mw_int_pt.keys():
        cutflow.write('mass window and jet selection: pt weighted ' + str(mw_int_pt['bkg'])+'\n')
    # close logfile
    cutflow.close()

    # return the variable with the maximum background rejection
    return maxrej, maxrejvar
Beispiel #6
0
def build_facegraph(json_obj):
    """
    # Build Face Graph from query image 
    """
    # create Graph of Query & BoFG
    G_q = nx.Graph()
    G_bofg = nx.Graph()
    # from Query
    faces = json_obj
    age_range = [(1, 3), (3, 8), (8, 13), (13, 20), (20, 37), (37, 66),
                 (66, 85)]
    age_cate = [range(s, e) for s, e in age_range]
    v_label_dict = {
        (1, 1): 0,
        (5, 1): 1,
        (10, 1): 2,
        (16, 1): 3,
        (28, 1): 4,
        (51, 1): 5,
        (75, 1): 6,
        (1, 2): 7,
        (5, 2): 8,
        (10, 2): 9,
        (16, 2): 10,
        (28, 2): 11,
        (51, 2): 12,
        (75, 2): 13
    }

    f_cen_lst = []
    for idx, face in enumerate(faces):
        f_eye_l = face['faceLandmarks']['eyeLeftInner']
        f_eye_r = face['faceLandmarks']['eyeRightInner']
        f_cen = ((f_eye_l['x'] + f_eye_r['x']) / 2.0,
                 (f_eye_l['y'] + f_eye_r['y']) / 2.0)
        f_gen = [1 if face['attributes']['gender'] else 0][0]
        f_age_s = face['attributes']['age']
        f_age = [int(fn.median(a_c)) for a_c in age_cate if f_age_s in a_c][0]
        v_label = v_label_dict[f_age, f_gen]
        G_q.add_node(idx, label=v_label)
        f_cen_lst.append(f_cen)

    f_cen_combi = list(itertools.combinations(f_cen_lst, 2))
    for a, b in f_cen_combi:
        px_dist = fn.euclidean_dist(a, b)
        a_idx = f_cen_lst.index(a)
        b_idx = f_cen_lst.index(b)
        G_q.add_edge(a_idx, b_idx, weight=px_dist)

    # Minimum Spanning Tree
    G_q_mst = nx.minimum_spanning_tree(G_q)
    # Set initial number of Order Distance as 0 For MST edges
    for v1, v2 in G_q_mst.edges():
        G_q_mst[v1][v2]['weight'] = 0
    # Assign Order Distance for All Edges
    G_q_od = nx.all_pairs_shortest_path_length(G_q_mst, cutoff=4)
    # Remove the edges visiting itself
    # Decrease Order Distances with -1
    # Copy MST's Order Distance to Edges Attrs. of Graph G
    for v1 in G_q_od.keys():
        for v2 in G_q_od[v1].keys():
            if v1 == v2:  # Remove Nodes self to self
                del G_q_od[v1][v2]
            else:  # Modify Weights of Other Nodes & Assign them to Graph G
                # Edge Label
                order_dist = G_q_od[v1][v2]
                order_dist -= 1
                G_q.add_edge(v1, v2, weight=order_dist)

    return G_q