babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
parameterize_efficiency.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import argparse
4 import ROOT
5 import array
6 
7 def ParameterizeEfficiency(out_file_path, docuts):
8  ROOT.TH1.SetDefaultSumw2()
9  c = ROOT.TChain("tree", "tree")
10  c.Add("/net/cms2/cms2r0/babymaker/babies/2017_01_21/mc/unprocessed/*_TTJets_*.root")
11 
12  btags = [("loose", 0.5426),
13  ("medium", 0.8484),
14  ("tight", 0.9535)]
15 
16  btags_deep = [("loose", 0.2219),
17  ("medium", 0.6324),
18  ("tight", 0.8958)]
19 
20  eta_cuts = [0., 1.2, 2.4]
21  pt_cuts = [30., 50., 70., 100., 140., 200., 300., 670., 1.e4]
22  flavor_cuts = [-0.5, 3.5, 4.5, 5.5]
23 
24  numerators = [ROOT.TH3D("btagEfficiency_"+btag[0], "btagEfficiency_"+btag[0],
25  len(eta_cuts)-1, array.array('d', eta_cuts),
26  len(pt_cuts)-1, array.array('d', pt_cuts),
27  len(flavor_cuts)-1, array.array('d', flavor_cuts))
28  for btag in btags]
29  numerators_deep = [ROOT.TH3D("btagEfficiency_deep_"+btag[0], "btagEfficiency_deep_"+btag[0],
30  len(eta_cuts)-1, array.array('d', eta_cuts),
31  len(pt_cuts)-1, array.array('d', pt_cuts),
32  len(flavor_cuts)-1, array.array('d', flavor_cuts))
33  for btag in btags_deep]
34 
35  denominator = numerators[0].Clone("btagEfficiency_denominator")
36 
37  entry = 0
38  num_entries = c.GetEntries()
39  for event in c:
40  if entry % 10000 == 0:
41  print "Completed", '{:.2f}'.format(100.*entry/num_entries)+"%"
42  entry = entry + 1
43  # if not (c.stitch and getattr(c,"pass")): continue # if using HT bins
44  if not (getattr(c,"pass")): continue
45  if docuts:
46  if (c.nleps<1 or c.st<=500. or c.met<=200. or c.njets<5):
47  continue
48  for ijet in xrange(len(c.jets_csv)):
49  if (c.jets_islep[ijet]): continue
50  flavor = abs(c.jets_hflavor[ijet])
51  pt = c.jets_pt[ijet]
52  eta = abs(c.jets_eta[ijet])
53  csv = c.jets_csv[ijet]
54  csvd = c.jets_csvd[ijet]
55 
56  denominator.Fill(eta, pt, flavor)
57  for inum in xrange(len(btags)):
58  if csv > btags[inum][1]:
59  numerators[inum].Fill(eta, pt, flavor)
60  for inum in xrange(len(btags_deep)):
61  if csvd > btags_deep[inum][1]:
62  numerators_deep[inum].Fill(eta, pt, flavor)
63 
64  for n in numerators:
65  n.Divide(denominator)
66  for n in numerators_deep:
67  n.Divide(denominator)
68 
69  out_file = ROOT.TFile(out_file_path, "recreate")
70  for n in numerators:
71  n.Write()
72  doc = ROOT.TNamed("Documentation: this file contains a parameterization in (eta, pt, flavor) for CSVv2 b-tagger", "")
73  doc.Write()
74  out_file.Close()
75 
76  out_file = ROOT.TFile(out_file_path.replace(".root","_deep.root"), "recreate")
77  for n in numerators_deep:
78  n.Write()
79  doc = ROOT.TNamed("Documentation: this file contains a parameterization in (eta, pt, flavor) for the deep CSV b-tagger", "")
80  doc.Write()
81  out_file.Close()
82 
83 if __name__ == "__main__":
84  parser = argparse.ArgumentParser(description="Computes b-tagging efficiency as a function of pT, eta, and flavor",
85  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
86  parser.add_argument("-o", "--out_file", metavar="OUT_FILE", default="btagEfficiency.root",
87  help="Save efficiences to %(metavar)s")
88  parser.add_argument("-c", "--docuts", action="store_true",
89  help="Use all available events, applying only basic filters")
90  args = parser.parse_args()
91 
92  ParameterizeEfficiency(args.out_file, args.docuts)
def ParameterizeEfficiency(out_file_path, docuts)