Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-19 23:20:32

0001 #!/bin/env python
0002 
0003 import argparse
0004 import ROOT as r
0005 from array import array
0006 import os
0007 import sys
0008 from math import sqrt
0009 
0010 sel_choices = ["base", "loweta", "xtr", "vtr", "none"]
0011 metric_choices = ["eff", "fakerate", "duplrate"]
0012 variable_choices = ["pt", "ptmtv", "ptlow", "eta", "phi", "dxy", "dz", "vxy"]
0013 objecttype_choices = ["TC", "pT5", "T5", "pT3", "pLS", "pT5_lower", "pT3_lower", "T5_lower"]
0014 #lowerObjectType = ["pT5_lower", "pT3_lower", "T5_lower"]
0015 
0016 r.gROOT.SetBatch(True)
0017 
0018 # Argument parsing
0019 parser = argparse.ArgumentParser(description="What are we wanting to graph?")
0020 # Input (input is a root file with numerator and denominator histograms
0021 parser.add_argument('inputs', nargs='+', help='input num_den_hist.root files')
0022 parser.add_argument('--tag'         , '-t' , dest='tag'         , type=str , default='v0'        , help='tag of the run [DEFAULT=v0]', required=True)
0023 # When more than two input files are provided it is to compare the two sets of performance plots
0024 # The provided input file must contain the TGraphAsymmError plots when comparing
0025 parser.add_argument('--metric'      , '-m' , dest='metric'      , type=str            , help='{}'.format(','.join(metric_choices), metric_choices[0]))
0026 parser.add_argument('--objecttype'  , '-o' , dest='objecttype'  , type=str            , help='{}'.format(','.join(objecttype_choices), objecttype_choices[0]), default=objecttype_choices[0])
0027 parser.add_argument('--selection'   , '-s' , dest='selection'   , type=str            , help='{}'.format(','.join(sel_choices), sel_choices[0]), default=sel_choices[0])
0028 parser.add_argument('--pdgid'       , '-g' , dest='pdgid'       , type=int            , help='pdgid (efficiency plots only)')
0029 parser.add_argument('--charge'      , '-c' , dest='charge'      , type=int            , help='charge (efficiency plots only) (0: both, 1: positive, -1:negative', default=0)
0030 parser.add_argument('--variable'    , '-v' , dest='variable'    , type=str            , help='{}'.format(','.join(variable_choices), variable_choices[0]))
0031 parser.add_argument('--individual'  , '-b' , dest='individual'  , action="store_true" , help='plot not the breakdown but individual')
0032 parser.add_argument('--yzoom'       , '-y' , dest='yzoom'       , action="store_true" , help='zoom in y')
0033 parser.add_argument('--xcoarse'     , '-x' , dest='xcoarse'     , action="store_true" , help='coarse in x')
0034 parser.add_argument('--sample_name' , '-S' , dest='sample_name' , type=str            , help='sample name in case one wants to override')
0035 parser.add_argument('--pt_cut'      ,        dest='pt_cut'      , type=float          , default=0.9, help='transverse momentum cut [DEFAULT=0.9]')
0036 parser.add_argument('--eta_cut'     ,        dest='eta_cut'     , type=float          , default=4.5, help='pseudorapidity cut [DEFAULT=4.5]')
0037 parser.add_argument('--compare'     , '-C' , dest='compare'     , action="store_true" , help='plot comparisons of input files')
0038 parser.add_argument('--comp_labels' , '-L' , dest='comp_labels' , type=str            , help='comma separated legend labels for comparison plots (e.g. reference,pT5_update')
0039 
0040 
0041 #______________________________________________________________________________________________________
0042 def main():
0043 
0044     # Plotting a set of performance plots
0045     #
0046     #   $ lst_plot_performance.py -t myRun1 INPUT.root
0047     #
0048     # where INPUT.root contains a set of numerator and denominator histograms named in following convention
0049     #
0050     #  Efficiency histogram:
0051     #
0052     #     Root__<OBJ>_<SEL>_<PDGID>_<CHARGE>_ef_numer_<VARIABLE>
0053     #     Root__<OBJ>_<SEL>_<PDGID>_<CHARGE>_ef_denom_<VARIABLE>
0054     #
0055     #     e.g. Root__pT5_loweta_321_-1_ef_numer_eta
0056     #
0057     #          OBJ=pT5
0058     #          SEL=loweta
0059     #          PDGID=Kaon
0060     #          CHARGE=-1
0061     #          METRIC=ef (efficiency)
0062     #          VARIABLE=eta
0063     #
0064     #  Fake rate or duplicate rate histogram:
0065     #
0066     #     Root__<OBJ>_fr_numer_<VARIABLE> (fake rate numerator)
0067     #     Root__<OBJ>_fr_denom_<VARIABLE> (fake rate denominator)
0068     #     Root__<OBJ>_dr_numer_<VARIABLE> (duplicate rate numerator)
0069     #     Root__<OBJ>_dr_denom_<VARIABLE> (duplicate rate denominator)
0070     #
0071     # Once ran, the output will be stored in performance/myRun1_<githash>
0072     # Also a ROOT file with the efficiency graphs will be saved to performance/myRun1_<githash>/efficiency.root
0073     #
0074 
0075     args = parser.parse_args()
0076 
0077     plot_standard_performance_plots(args)
0078 
0079 #______________________________________________________________________________________________________
0080 def plot(args):
0081 
0082     params = process_arguments_into_params(args)
0083 
0084     if params["metric"] == "eff":
0085         params["output_name"] = "{objecttype}_{selection}_{pdgid}_{charge}_{metric}_{variable}".format(**params)
0086     else:
0087         params["output_name"] = "{objecttype}_{metric}_{variable}".format(**params)
0088 
0089     if params["xcoarse"]:
0090         params["output_name"] += "coarse"
0091     if params["yzoom"]:
0092         params["output_name"] += "zoom"
0093     # if params["breakdown"]:
0094     #     params["output_name"] += "_breakdown"
0095     # if params["compare"]:
0096     #     params["output_name"] += "_breakdown"
0097     # Get histogram names
0098     if params["metric"] == "eff":
0099         params["denom"] = "Root__{objecttype}_{selection}_{pdgid}_{charge}_{metricsuffix}_denom_{variable}".format(**params)
0100         params["numer"] = "Root__{objecttype}_{selection}_{pdgid}_{charge}_{metricsuffix}_numer_{variable}".format(**params)
0101     else:
0102         params["denom"] = "Root__{objecttype}_{metricsuffix}_denom_{variable}".format(**params)
0103         params["numer"] = "Root__{objecttype}_{metricsuffix}_numer_{variable}".format(**params)
0104 
0105     # print(params["numer"])
0106     # print(params["denom"])
0107     # print(params["output_name"])
0108 
0109     #skip if histograms not found!
0110     if (not params["input_file"].GetListOfKeys().Contains(params["numer"])) or (not params["input_file"].GetListOfKeys().Contains(params["denom"])):
0111         return
0112 
0113     # Denom histogram
0114     denom = []
0115     denom.append(params["input_file"].Get(params["denom"]).Clone())
0116 
0117     # Numerator histograms
0118     numer = []
0119     numer.append(params["input_file"].Get(params["numer"]).Clone())
0120 
0121     breakdown_hist_types = ["pT5", "pT3", "T5", "pLS"]
0122     print("breakdown = ", params["breakdown"])
0123     if params["breakdown"]:
0124         for breakdown_hist_type in breakdown_hist_types:
0125             breakdown_histname = params["numer"].replace("TC", breakdown_hist_type)
0126             hist = params["input_file"].Get(breakdown_histname)
0127             numer.append(hist.Clone())
0128             denom.append(params["input_file"].Get(params["denom"]).Clone())
0129 
0130     if params["compare"]:
0131         for f in params["additional_input_files"]:
0132             hist = f.Get(params["numer"])
0133             numer.append(hist.Clone())
0134             hist = f.Get(params["denom"])
0135             denom.append(hist.Clone())
0136 
0137 
0138     if params["breakdown"]:
0139         params["legend_labels"] = ["TC" ,"pT5" ,"pT3" ,"T5" ,"pLS"]
0140     else:
0141         params["legend_labels"] = [args.objecttype]
0142 
0143     if params["compare"]:
0144         params["legend_labels"] = ["reference"]
0145         for i, f in enumerate(params["additional_input_files"]):
0146             params["legend_labels"].append("{i}".format(i=i))
0147         if params["comp_labels"]:
0148             params["legend_labels"] = params["comp_labels"]
0149 
0150 
0151     draw_ratio(
0152             numer, # numerator histogram(s)
0153             denom, # denominator histogram
0154             params,
0155             )
0156 
0157     DIR = os.environ["LSTPERFORMANCEWEBDIR"]
0158     perfwebpath = os.path.normpath("{}".format(DIR))
0159     os.system("cd {}; ln -sf {}/summary".format(params["output_dir"], perfwebpath))
0160     os.system("cd {}; ln -sf {}/compare".format(params["output_dir"], perfwebpath))
0161 
0162 #______________________________________________________________________________________________________
0163 def process_arguments_into_params(args):
0164 
0165     params = {}
0166 
0167     # parse metric
0168     if args.metric not in metric_choices:
0169         print("metric", args.metric)
0170         parser.print_help(sys.stderr)
0171         sys.exit(1)
0172     params["metric"] = args.metric
0173 
0174     # parse the selection type
0175     if args.selection not in sel_choices:
0176         print("selection", args.selection)
0177         parser.print_help(sys.stderr)
0178         sys.exit(1)
0179     params["selection"] = args.selection
0180 
0181     params["pdgid"] = args.pdgid
0182 
0183     params["charge"] = args.charge
0184 
0185     # parse the object type
0186     if args.objecttype not in objecttype_choices:
0187         print("objjecttype", args.objjecttype)
0188         parser.print_help(sys.stderr)
0189         sys.exit(1)
0190     params["objecttype"] = args.objecttype
0191 
0192     # parse variable
0193     if args.variable not in variable_choices:
0194         print("variable", args.variable)
0195         parser.print_help(sys.stderr)
0196         sys.exit(1)
0197     params["variable"] = args.variable
0198 
0199     if args.yzoom:
0200         params["yzoom"] = True
0201     else:
0202         params["yzoom"] = False
0203     if args.xcoarse:
0204         params["xcoarse"] = True
0205     else:
0206         params["xcoarse"] = False
0207 
0208     # Parse from input file
0209     root_file_name = args.inputs[0]
0210     f = r.TFile(root_file_name)
0211     params["input_file"] = f
0212 
0213     # git version hash
0214     git_hash = f.Get("githash").GetTitle()
0215     params["git_hash"] = git_hash
0216 
0217     params["pt_cut"] = args.pt_cut
0218     params["eta_cut"] = args.eta_cut
0219 
0220     # sample name
0221     sample_name = f.Get("input").GetTitle()
0222     if args.sample_name:
0223         sample_name = args.sample_name
0224     params["sample_name"] = sample_name
0225 
0226     if len(args.inputs) > 1: # if more than 1 save them to separate params
0227         params["additional_input_files"] = []
0228         params["additional_git_hashes"] = []
0229         params["additional_sample_names"] = []
0230         for i in args.inputs[1:]:
0231             params["additional_input_files"].append(r.TFile(i))
0232             params["additional_git_hashes"].append(params["additional_input_files"][-1].Get("githash").GetTitle())
0233             params["additional_sample_names"].append(params["additional_input_files"][-1].Get("input").GetTitle())
0234 
0235     if params["metric"] == "eff": params["metricsuffix"] = "ef"
0236     if params["metric"] == "duplrate": params["metricsuffix"] = "dr"
0237     if params["metric"] == "fakerate": params["metricsuffix"] = "fr"
0238 
0239     # If breakdown it must be object type of TC
0240     params["breakdown"] = args.breakdown
0241 #    if args.individual:
0242 #        params["breakdown"] = False
0243 #    else:
0244         # if params["objecttype"] != "TC":
0245         #     print("Warning! objecttype is set to \"TC\" because individual is False!")
0246         # params["objecttype"] = "TC"
0247 #        params["breakdown"] = True
0248 
0249     # If compare we compare the different files
0250     params["compare"] = False
0251     if args.compare:
0252         params["breakdown"] = False
0253         params["compare"] = True
0254 
0255     if args.comp_labels:
0256         params["comp_labels"] = args.comp_labels.split(",")
0257 
0258     # process tags
0259     tag_ = os.path.normpath(args.tag)
0260     bn = os.path.basename(tag_)
0261     dn = os.path.dirname(tag_)
0262     params["tagbase"] = bn
0263     params["tagdir"] = dn
0264     params["tag"] = args.tag
0265 
0266     # Create output_dir
0267     params["lstoutputdir"] = os.environ["LSTOUTPUTDIR"]
0268     params["output_dir"] = os.path.normpath("{lstoutputdir}/performance/{tagdir}/{tagbase}_{git_hash}-{sample_name}".format(**params))
0269     if params["compare"]:
0270         for gg, ii in zip(params["additional_git_hashes"], params["additional_sample_names"]):
0271             params["output_dir"] += "_{}-{}".format(gg, ii)
0272     os.system("mkdir -p {output_dir}/mtv/var".format(**params))
0273     os.system("mkdir -p {output_dir}/mtv/num".format(**params))
0274     os.system("mkdir -p {output_dir}/mtv/den".format(**params))
0275     os.system("mkdir -p {output_dir}/mtv/ratio".format(**params))
0276 
0277     # Output file
0278     params["output_file"] = r.TFile("{output_dir}/efficiency.root".format(**params), "update")
0279 
0280     # git version hash
0281     params["input_file"].Get("githash").Write("", r.TObject.kOverwrite)
0282 
0283     # sample name
0284     params["input_file"].Get("input").Write("", r.TObject.kOverwrite)
0285 
0286     # Obtain the number of events used
0287     params["nevts"] = str(int(f.Get("nevts").GetBinContent(1)))
0288 
0289     return params
0290 
0291 #______________________________________________________________________________________________________
0292 def draw_ratio(nums, dens, params):
0293 
0294     # Rebin if necessary
0295     if "scalar" in params["output_name"] and "ptscalar" not in params["output_name"]:
0296         for num in nums:
0297             num.Rebin(180)
0298         for den in dens:
0299             den.Rebin(180)
0300 
0301     # Rebin if necessary
0302     if "coarse" in params["output_name"] and "ptcoarse" not in params["output_name"]:
0303         for num in nums:
0304             num.Rebin(6)
0305         for den in dens:
0306             den.Rebin(6)
0307 
0308     # Deal with overflow bins for pt plots
0309     if "pt" in params["output_name"] or "vxy" in params["output_name"]:
0310         for num in nums:
0311             overFlowBin = num.GetBinContent(num.GetNbinsX() + 1)
0312             lastBin = num.GetBinContent(num.GetNbinsX())
0313             num.SetBinContent(num.GetNbinsX(), lastBin + overFlowBin)
0314             num.SetBinError(num.GetNbinsX(), sqrt(lastBin + overFlowBin))
0315         for den in dens:
0316             overFlowBin = den.GetBinContent(den.GetNbinsX() + 1)
0317             lastBin = den.GetBinContent(den.GetNbinsX())
0318             den.SetBinContent(den.GetNbinsX(), lastBin + overFlowBin)
0319             den.SetBinError(den.GetNbinsX(), sqrt(lastBin + overFlowBin))
0320 
0321     # Create efficiency graphs
0322     teffs = []
0323     effs = []
0324     for num, den in zip(nums, dens):
0325         teff = r.TEfficiency(num, den)
0326         eff = teff.CreateGraph()
0327         teffs.append(teff)
0328         effs.append(eff)
0329 
0330     # print(effs)
0331 
0332     #
0333     hist_name_suffix = ""
0334     if params["xcoarse"]:
0335         hist_name_suffix += "coarse"
0336     if params["yzoom"]:
0337         hist_name_suffix += "zoom"
0338 
0339     params["output_file"].cd()
0340     outputname = params["output_name"]
0341     for den in dens:
0342         den.Write(den.GetName() + hist_name_suffix, r.TObject.kOverwrite)
0343         # eff_den = r.TGraphAsymmErrors(den)
0344         # eff_den.SetName(outputname+"_den")
0345         # eff_den.Write("", r.TObject.kOverwrite)
0346     for num in nums:
0347         num.Write(num.GetName() + hist_name_suffix, r.TObject.kOverwrite)
0348         # eff_num = r.TGraphAsymmErrors(num)
0349         # eff_num.SetName(outputname+"_num")
0350         # eff_num.Write("", r.TObject.kOverwrite)
0351     for eff in effs:
0352         eff.SetName(outputname)
0353         eff.Write("", r.TObject.kOverwrite)
0354 
0355     draw_plot(effs, nums, dens, params)
0356 
0357 
0358 #______________________________________________________________________________________________________
0359 def parse_plot_name(output_name):
0360     if "fake" in output_name:
0361         rtnstr = ["Fake Rate of"]
0362     elif "dup" in output_name:
0363         rtnstr = ["Duplicate Rate of"]
0364     elif "inefficiency" in output_name:
0365         rtnstr = ["Inefficiency of"]
0366     else:
0367         rtnstr = ["Efficiency of"]
0368     if "MD_" in output_name:
0369         rtnstr.append("Mini-Doublet")
0370     elif "LS_" in output_name and "pLS" not in output_name:
0371         rtnstr.append("Line Segment")
0372     elif "pT4_" in output_name:
0373         rtnstr.append("Quadruplet w/ Pixel LS")
0374     elif "T4_" in output_name:
0375         rtnstr.append("Quadruplet w/o gap")
0376     elif "T4x_" in output_name:
0377         rtnstr.append("Quadruplet w/ gap")
0378     elif "pT3_" in output_name:
0379         rtnstr.append("Pixel Triplet")
0380     elif "pT5_" in output_name:
0381         rtnstr.append("Pixel Quintuplet")
0382     elif "T3_" in output_name:
0383         rtnstr.append("Triplet")
0384     elif "TCE_" in output_name:
0385         rtnstr.append("Extended Track")
0386     elif "pureTCE_" in output_name:
0387         rtnstr.append("Pure Extensions")
0388     elif "TC_" in output_name:
0389         rtnstr.append("Track Candidate")
0390     elif "T4s_" in output_name:
0391         rtnstr.append("Quadruplet w/ or w/o gap")
0392     elif "pLS_" in output_name:
0393         rtnstr.append("Pixel Line Segment")
0394     elif "T5_" in output_name:
0395         rtnstr.append("Quintuplet")
0396     return " ".join(rtnstr)
0397 
0398 #______________________________________________________________________________________________________
0399 def get_pdgidstr(pdgid):
0400     if abs(pdgid) == 0: return "All"
0401     elif abs(pdgid) == 11: return "Electron"
0402     elif abs(pdgid) == 13: return "Muon"
0403     elif abs(pdgid) == 211: return "Pion"
0404     elif abs(pdgid) == 321: return "Kaon"
0405 
0406 #______________________________________________________________________________________________________
0407 def get_chargestr(charge):
0408     if charge == 0: return "All"
0409     elif charge == 1: return "Positive"
0410     elif charge == -1: return "Negative"
0411 
0412 #______________________________________________________________________________________________________
0413 def set_label(eff, output_name, raw_number):
0414     if "phi" in output_name:
0415         title = "#phi"
0416     elif "_dz" in output_name:
0417         title = "z [cm]"
0418     elif "_dxy" in output_name:
0419         title = "d0 [cm]"
0420     elif "_vxy" in output_name:
0421         title = "r_{vertex} [cm]"
0422     elif "_pt" in output_name:
0423         title = "p_{T} [GeV]"
0424     elif "_hit" in output_name:
0425         title = "hits"
0426     elif "_lay" in output_name:
0427         title = "layers"
0428     else:
0429         title = "#eta"
0430     eff.GetXaxis().SetTitle(title)
0431     if "fakerate" in output_name:
0432         eff.GetYaxis().SetTitle("Fake Rate")
0433     elif "duplrate" in output_name:
0434         eff.GetYaxis().SetTitle("Duplicate Rate")
0435     elif "inefficiency" in output_name:
0436         eff.GetYaxis().SetTitle("Inefficiency")
0437     else:
0438         eff.GetYaxis().SetTitle("Efficiency")
0439     if raw_number:
0440         eff.GetYaxis().SetTitle("# of objects of interest")
0441     eff.GetXaxis().SetTitleSize(0.05)
0442     eff.GetYaxis().SetTitleSize(0.05)
0443     eff.GetXaxis().SetLabelSize(0.05)
0444     eff.GetYaxis().SetLabelSize(0.05)
0445 
0446 #______________________________________________________________________________________________________
0447 def draw_label(params):
0448     version_tag = params["git_hash"]
0449     sample_name = params["sample_name"]
0450     pdgidstr = get_pdgidstr(params["pdgid"])
0451     chargestr = get_chargestr(params["charge"])
0452     output_name = params["output_name"]
0453     n_events_processed = params["nevts"]
0454     ptcut = params["pt_cut"]
0455     etacut = params["eta_cut"]
0456     # Label
0457     t = r.TLatex()
0458 
0459     # Draw information about sample, git version, and types of particles
0460     t.SetTextAlign(11) # align bottom left corner of text
0461     t.SetTextColor(r.kBlack)
0462     t.SetTextSize(0.04)
0463     x = r.gPad.GetX1() + r.gPad.GetLeftMargin()
0464     y = r.gPad.GetY2() - r.gPad.GetTopMargin() + 0.09 + 0.03
0465     sample_name_label = "Sample:" + sample_name
0466     sample_name_label += "   Version tag:" + version_tag
0467     if n_events_processed:
0468         sample_name_label +="  N_{evt}:" + n_events_processed
0469     t.DrawLatexNDC(x,y,"#scale[0.9]{#font[42]{%s}}" % sample_name_label)
0470 
0471     x = r.gPad.GetX1() + r.gPad.GetLeftMargin()
0472     y = r.gPad.GetY2() - r.gPad.GetTopMargin() + 0.045 + 0.03
0473 
0474     etacutstr = "|#eta| < 4.5"
0475     if params["selection"] == "loweta":
0476         etacutstr = "|#eta| < 2.4"
0477     if params["selection"] == "xtr":
0478         etacutstr = "x-reg"
0479     if params["selection"] == "vtr":
0480         etacutstr = "not x-reg"
0481     if "eff" in output_name:
0482         if "_pt" in output_name:
0483             fiducial_label = "{etacutstr}, |Vtx_{{z}}| < 30 cm, |Vtx_{{xy}}| < 2.5 cm".format(etacutstr=etacutstr)
0484         elif "_eta" in output_name:
0485             fiducial_label = "p_{{T}} > {pt} GeV, |Vtx_{{z}}| < 30 cm, |Vtx_{{xy}}| < 2.5 cm".format(pt=ptcut)
0486         elif "_dz" in output_name:
0487             fiducial_label = "{etacutstr}, p_{{T}} > {pt} GeV, |Vtx_{{xy}}| < 2.5 cm".format(pt=ptcut, etacutstr=etacutstr)
0488         elif "_dxy" in output_name:
0489             fiducial_label = "{etacutstr}, p_{{T}} > {pt} GeV, |Vtx_{{z}}| < 30 cm".format(pt=ptcut, etacutstr=etacutstr)
0490         elif "_vxy" in output_name:
0491             fiducial_label = "{etacutstr}, p_{{T}} > {pt} GeV, |Vtx_{{z}}| < 30 cm".format(pt=ptcut, etacutstr=etacutstr)
0492         else:
0493             fiducial_label = "{etacutstr}, p_{{T}} > {pt} GeV, |Vtx_{{z}}| < 30 cm, |Vtx_{{xy}}| < 2.5 cm".format(pt=ptcut, etacutstr=etacutstr)
0494         particleselection = ((", Particle:" + pdgidstr) if pdgidstr else "" ) + ((", Charge:" + chargestr) if chargestr else "" )
0495         fiducial_label += particleselection
0496     # If fake rate or duplicate rate plot follow the following fiducial label rule
0497     elif "fakerate" in output_name or "duplrate" in output_name:
0498         if "_pt" in output_name:
0499             fiducial_label = "|#eta| < {eta}".format(eta=etacut)
0500         elif "_eta" in output_name:
0501             fiducial_label = "p_{{T}} > {pt} GeV".format(pt=ptcut)
0502         else:
0503             fiducial_label = "|#eta| < {eta}, p_{{T}} > {pt} GeV".format(pt=ptcut, eta=etacut)
0504     t.DrawLatexNDC(x,y,"#scale[0.9]{#font[42]{%s}}" % fiducial_label)
0505 
0506     # Draw CMS label
0507     cms_label = "Simulation"
0508     x = r.gPad.GetX1() + r.gPad.GetLeftMargin()
0509     y = r.gPad.GetY2() - r.gPad.GetTopMargin() + 0.005
0510     t.DrawLatexNDC(x,y,"#scale[1.25]{#font[61]{CMS}} #scale[1.1]{#font[52]{%s}}" % cms_label)
0511 
0512 #______________________________________________________________________________________________________
0513 def draw_plot(effs, nums, dens, params):
0514 
0515     legend_labels = params["legend_labels"]
0516     output_dir = params["output_dir"]
0517     output_name = params["output_name"]
0518     sample_name = params["sample_name"]
0519     version_tag = params["git_hash"]
0520     pdgidstr = get_pdgidstr(params["pdgid"])
0521     chargestr = get_chargestr(params["charge"])
0522 
0523     # Get Canvas
0524     c1 = r.TCanvas()
0525     c1.SetBottomMargin(0.15)
0526     c1.SetLeftMargin(0.15)
0527     c1.SetTopMargin(0.22)
0528     c1.SetRightMargin(0.15)
0529 
0530     # Set logx
0531     if "_pt" in output_name:
0532         c1.SetLogx()
0533 
0534     # Set title
0535     # print(output_name)
0536     # print(parse_plot_name(output_name))
0537     effs[0].SetTitle(parse_plot_name(output_name))
0538 
0539     # Draw the efficiency graphs
0540     colors = [1, 2, 3, 4, 6]
0541     markerstyles = [20, 26, 28, 24, 27]
0542     markersize = 1.2
0543     linewidth = 2
0544     for i, eff in enumerate(effs):
0545         if i == 0:
0546             eff.Draw("epa")
0547         else:
0548             eff.Draw("epsame")
0549         eff.SetMarkerStyle(markerstyles[i])
0550         eff.SetMarkerSize(markersize)
0551         eff.SetLineWidth(linewidth)
0552         eff.SetMarkerColor(colors[i])
0553         eff.SetLineColor(colors[i])
0554         set_label(eff, output_name, raw_number=False)
0555 
0556     nleg = len(legend_labels)
0557     legend = r.TLegend(0.15,0.75-nleg*0.04,0.25,0.75)
0558     for i, label in enumerate(legend_labels):
0559         legend.AddEntry(effs[i], label)
0560     legend.Draw("same")
0561 
0562     # Compute the yaxis_max
0563     yaxis_max = 0
0564     for eff in effs:
0565         for i in range(0, eff.GetN()):
0566             if yaxis_max < eff.GetY()[i]:
0567                 yaxis_max = eff.GetY()[i]
0568 
0569     # Compute the yaxis_min
0570     yaxis_min = 999
0571     for i in range(0, effs[0].GetN()):
0572         if yaxis_min > effs[0].GetY()[i] and effs[0].GetY()[i] != 0:
0573             yaxis_min = effs[0].GetY()[i]
0574 
0575     # Set Yaxis range
0576     effs[0].GetYaxis().SetRangeUser(0, 1.02)
0577     if "zoom" not in output_name:
0578         effs[0].GetYaxis().SetRangeUser(0, 1.02)
0579     else:
0580         if "fakerate" in output_name:
0581             effs[0].GetYaxis().SetRangeUser(0.0, yaxis_max * 1.1)
0582         elif "duplrate" in output_name:
0583             effs[0].GetYaxis().SetRangeUser(0.0, yaxis_max * 1.1)
0584         else:
0585             effs[0].GetYaxis().SetRangeUser(0.6, 1.02)
0586 
0587     # Set xaxis range
0588     if "_eta" in output_name:
0589         effs[0].GetXaxis().SetLimits(-4.5, 4.5)
0590 
0591     # Draw label
0592     draw_label(params)
0593 
0594     # Output file path
0595     output_fullpath = output_dir + "/mtv/var/" + output_name + ".pdf"
0596 
0597     # Save
0598     c1.SetGrid()
0599     c1.SaveAs("{}".format(output_fullpath))
0600     c1.SaveAs("{}".format(output_fullpath.replace(".pdf", ".png")))
0601     effs[0].SetName(output_name)
0602 
0603     for i, num in enumerate(nums):
0604         set_label(num, output_name, raw_number=True)
0605         num.Draw("hist")
0606         c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/num/").replace(".pdf", "_num{}.pdf".format(i))))
0607         c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/num/").replace(".pdf", "_num{}.png".format(i))))
0608 
0609     for i, den in enumerate(dens):
0610         set_label(den, output_name, raw_number=True)
0611         den.Draw("hist")
0612         c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/den/").replace(".pdf", "_den{}.pdf".format(i))))
0613         c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/den/").replace(".pdf", "_den{}.png".format(i))))
0614 
0615     # Double ratio if more than one nums are provided
0616     # Take the first num as the base
0617     if len(nums) > 1:
0618         base = nums[0].Clone()
0619         base.Divide(nums[0], dens[0], 1, 1, "B") #Binomial
0620         others = []
0621         for num, den in zip(nums[1:], dens[1:]):
0622             other = num.Clone()
0623             other.Divide(other, den, 1, 1, "B")
0624             others.append(other)
0625 
0626         # Take double ratio
0627         for other in others:
0628             other.Divide(base)
0629 
0630         for i, other in enumerate(others):
0631             other.Draw("ep")
0632             other.GetYaxis().SetTitle("{} / {}".format(legend_labels[i+1], legend_labels[0]))
0633             other.SetMarkerStyle(markerstyles[i+1])
0634             other.SetMarkerSize(markersize)
0635             other.SetMarkerColor(colors[i+1])
0636             other.SetLineWidth(linewidth)
0637             other.SetLineColor(colors[i+1])
0638             c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/ratio/").replace(".pdf", "_ratio{}.pdf".format(i))))
0639             c1.SaveAs("{}".format(output_fullpath.replace("/mtv/var/", "/mtv/ratio/").replace(".pdf", "_ratio{}.png".format(i))))
0640 
0641 #______________________________________________________________________________________________________
0642 def plot_standard_performance_plots(args):
0643 
0644     # Efficiency plots
0645     metrics = metric_choices
0646     yzooms = [False, True]
0647     variables = {
0648             "eff": ["pt", "ptlow", "ptmtv", "eta", "phi", "dxy", "dz", "vxy"],
0649             "fakerate": ["pt", "ptlow", "ptmtv", "eta", "phi"],
0650             "duplrate": ["pt", "ptlow", "ptmtv", "eta", "phi"],
0651             }
0652     sels = {
0653             "eff": ["base", "loweta"],
0654             "fakerate": ["none"],
0655             "duplrate": ["none"],
0656             }
0657     xcoarses = {
0658             "pt": [False],
0659             "ptlow": [False],
0660             "ptmtv": [False],
0661             "eta": [False, True],
0662             "phi": [False, True],
0663             "dxy": [False, True],
0664             "vxy": [False, True],
0665             "dz": [False, True],
0666             }
0667     types = objecttype_choices
0668     breakdowns = {
0669             "eff":{
0670                 "TC": [True, False],
0671                 "pT5": [False],
0672                 "pT3": [False],
0673                 "T5": [False],
0674                 "pLS": [False],
0675                 "pT5_lower":[False],
0676                 "pT3_lower":[False],
0677                 "T5_lower":[False],
0678                 },
0679             "fakerate":{
0680                 "TC": [True, False],
0681                 "pT5": [False],
0682                 "pT3": [False],
0683                 "T5": [False],
0684                 "pLS": [False],
0685                 "pT5_lower":[False],
0686                 "pT3_lower":[False],
0687                 "T5_lower":[False],
0688                 },
0689             "duplrate":{
0690                 "TC": [True, False],
0691                 "pT5": [False],
0692                 "pT3": [False],
0693                 "T5": [False],
0694                 "pLS": [False],
0695                 "pT5_lower":[False],
0696                 "pT3_lower":[False],
0697                 "T5_lower":[False],
0698             },
0699     }
0700     pdgids = {
0701             "eff": [0, 11, 13, 211, 321],
0702             "fakerate": [0],
0703             "duplrate": [0],
0704             }
0705     charges = {
0706             "eff":[0, 1, -1],
0707             "fakerate":[0],
0708             "duplrate":[0],
0709             }
0710 
0711     if args.metric:
0712         metrics = [args.metric]
0713 
0714     if args.objecttype:
0715         types = args.objecttype.split(',')
0716 
0717     if args.compare:
0718         types = args.objecttype.split(',')
0719 
0720     if args.selection:
0721         sels["eff"] = [args.selection]
0722 
0723     if args.pdgid != None:
0724         pdgids["eff"] = [args.pdgid]
0725         pdgids["fakerate"] = [args.pdgid]
0726         pdgids["duplrate"] = [args.pdgid]
0727 
0728     if args.charge != None:
0729         charges["eff"] = [args.charge]
0730         charges["fakerate"] = [args.charge]
0731         charges["duplrate"] = [args.charge]
0732 
0733     if args.variable:
0734         # dxy and dz are only in efficiency
0735         if args.variable != "dxy" and args.variable != "dz" and args.variable != "vxy":
0736             variables["eff"] = [args.variable]
0737             variables["fakerate"] = [args.variable]
0738             variables["suplrate"] = [args.variable]
0739         else:
0740             variables["eff"] = [args.variable]
0741             variables["fakerate"] = []
0742             variables["suplrate"] = []
0743 
0744     if args.individual:
0745         # Only eff / TC matters here
0746         breakdowns = {"eff":{"TC":[False], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]},
0747                 "fakerate": {"TC":[False], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]},
0748                 "duplrate": {"TC":[False], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]}}
0749 
0750 
0751     else:
0752         # Only eff / TC matters here
0753         breakdowns = {"eff":{"TC":[True], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]},
0754                 "fakerate": {"TC":[True], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]},
0755                 "duplrate": {"TC":[True], "pT5_lower":[False], "pT3_lower":[False], "T5_lower":[False]}}
0756     if args.yzoom:
0757         args.yzooms = [args.yzoom]
0758 
0759     if args.xcoarse:
0760         args.xcoarses = [args.xcoarse]
0761 
0762     for metric in metrics:
0763         for sel in sels[metric]:
0764             for variable in variables[metric]:
0765                 for yzoom in yzooms:
0766                     for xcoarse in xcoarses[variable]:
0767                         for typ in types:
0768                             print("type = ",typ)
0769                             for breakdown in breakdowns[metric][typ]:
0770                                 for pdgid in pdgids[metric]:
0771                                     for charge in charges[metric]:
0772                                         args.metric = metric
0773                                         args.objecttype = typ
0774                                         args.selection = sel
0775                                         args.pdgid = pdgid
0776                                         args.charge = charge
0777                                         args.variable = variable
0778                                         args.breakdown = breakdown
0779                                         args.yzoom = yzoom
0780                                         args.xcoarse = xcoarse
0781                                         print(args)
0782                                         plot(args)
0783 
0784 if __name__ == "__main__":
0785 
0786     main()
0787