ATLAS Offline Software
PlotSFuncertainty.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #!/usr/bin/env python
4 import ROOT
5 from array import array
6 import argparse, sys
7 from PlotUtils import PlotUtils, DiagnosticHisto
8 from CheckAppliedSFs import KnownWPs, getArgParser, getCalibReleasesAndWP
9 import gc
10 import os, math
11 
13  def __init__(self,
14  var_name = "", axis_title ="",
15  bins = -1, bmin = 0., bmax = 0.,
16  bdir = None,
17  calib = "",
18  wp = "",
19  sys = "SYS",
20  test_tree = None,
21  log_binning = False,
22  ):
23 
24  self.__muon_var = test_tree.GetLeaf("Muon_%s"%(var_name))
25  self.__norm = 1. if var_name.find("pt") == -1 else 1./1.e3
26 
27  self.__var_name = "%s%s_%s_%s"%("" if len(calib) == 0 else calib+"_", wp,var_name, sys)
28  self.__wp = wp
29  self.__sys = sys
30  self.__calib = calib
31  self.__nom_sf = test_tree.GetLeaf("%s%s_SF"%("" if len(calib) == 0 else "c%s_"%(calib), wp))
32  self.__1up_sf = test_tree.GetLeaf("%s%s_SF__MUON_EFF_%s_%s__1up"%("" if len(calib) == 0 else "c%s_"%(calib), wp,KnownWPs[wp],sys))
33  self.__1dn_sf = test_tree.GetLeaf("%s%s_SF__MUON_EFF_%s_%s__1down"%("" if len(calib) == 0 else "c%s_"%(calib), wp,KnownWPs[wp],sys))
35  name = "%s"%(self.__var_name),
36  axis_title = axis_title,
37  bins = bins, bmin = bmin, bmax = bmax,
38  bdir = bdir,
39  log_binning = log_binning)
41  name = "%s_1UP"%(self.__var_name),
42  axis_title = axis_title,
43  bins = bins, bmin = bmin, bmax = bmax,
44  bdir = bdir,
45  log_binning = log_binning )
47  name = "%s_1DOWN"%(self.__var_name),
48  axis_title = axis_title,
49  bins = bins, bmin = bmin, bmax = bmax,
50  bdir = bdir,
51  log_binning = log_binning )
52 
53  def fill(self):
54  self.__nom_histo.fill(value = self.__muon_var.GetValue()*self.__norm, weight= self.__nom_sf.GetValue())
55  self.__1up_histo.fill(value = self.__muon_var.GetValue()*self.__norm, weight= self.__1up_sf.GetValue())
56  self.__1dn_histo.fill(value = self.__muon_var.GetValue()*self.__norm, weight= self.__1dn_sf.GetValue())
57 
58 
59  def finalize(self):
60  self.__nom_histo.write()
61  self.__1dn_histo.write()
62  self.__1up_histo.write()
63 
64  def name(self):
65  return self.__var_name
66 
67  def get_nom_H1(self): return self.__nom_histo
68  def get_1up_H1(self): return self.__1up_histo
69  def get_1dn_H1(self): return self.__1dn_histo
70 
71  def get_wp(self): return self.__wp
72  def get_sys(self): return self.__sys
73  def get_calib(self): return self.__calib
74 
75 if __name__ == "__main__":
76  Options = getArgParser().parse_args()
77 
78  if not os.path.exists(Options.InputFile):
79  print 'ERROR: File %s does not exist!'%Options.InputFile
80  sys.exit(1)
81  infile = ROOT.TFile(Options.InputFile)
82 
83  tree = infile.Get("MuonEfficiencyTest")
84  calibReleases, WPs = getCalibReleasesAndWP(tree)
85 
86  if len(Options.WP)>0:
87  WPs = [wp for wp in Options.WP if wp in WPs]
88  print 'INFO: WPs given by user, only plot: %s'%(",".join(WPs))
89 
90 
91  ROOT.gROOT.Macro("rootlogon.C")
92  ROOT.gROOT.SetStyle("ATLAS")
93  ROOT.gROOT.SetBatch(1)
94  gc.disable()
95 
96  if os.path.isdir(Options.outDir) == False:
97  os.system("mkdir -p %s"%(Options.outDir))
98 
99  bonusname=Options.bonusname
100 
101  Histos = []
102 
103  for calib in calibReleases:
104  for wp in WPs:
105  for sys in ["SYS", "STAT"]:
106  Histos +=[
108  var_name = "pt",
109  axis_title ="p_{T} #mu(%s) [GeV]"%(wp),
110  calib = calib, log_binning = True,
111  bins = 25, bmin = 15, bmax = 1000,
112  wp =wp, sys = sys, test_tree = tree),
114  var_name = "eta",
115  axis_title ="#eta #mu(%s)"%(wp),
116  calib = calib,
117  bins =54 , bmin = -2.7, bmax = 2.7,
118  wp =wp, sys = sys, test_tree = tree),
120  var_name = "phi",
121  axis_title ="#phi (%s)"%(wp),
122  calib = calib,
123  bins =20 , bmin = -3.15, bmax = 3.15,
124  wp =wp, sys = sys, test_tree = tree) ]
125 
126  for i in range(tree.GetEntries()):
127  tree.GetEntry(i)
128  if i > 0 and i % 5000 == 0:
129  print "INFO: %d/%d events processed"%(i, tree.GetEntries())
130  #if math.fabs(tree.Muon_eta) <= 2.5 or tree.Muon_pt < 15.e3: continue
131  for H in Histos:
132  if tree.Muon_isHighPt == True or H.name().find("HighPt") == -1: H.fill()
133 
134  print "INFO: Histograms filled"
135  dummy = ROOT.TCanvas("dummy", "dummy", 800, 600)
136  dummy.SaveAs("%s/AllSystDep%s.pdf[" % (Options.outDir, bonusname))
137  for H in Histos:
138  pu = PlotUtils(status = Options.label)
139  pu.Prepare1PadCanvas(H.name())
140 
141  nom = H.get_nom_H1().TH1()
142  dn = H.get_1dn_H1().TH1()
143  up = H.get_1up_H1().TH1()
144 
145  up.SetLineColor(ROOT.kRed)
146  dn.SetLineColor(ROOT.kBlue)
147 
148  up.Divide(nom)
149  dn.Divide(nom)
150 
151 
152  up.SetTitle("+1#sigma %s"%("systematic" if H.get_sys() == "SYS" else "statistics"))
153  dn.SetTitle("-1#sigma %s"%("systematic" if H.get_sys() == "SYS" else "statistics"))
154 
155  nom.GetYaxis().SetTitle("Ratio to nominal")
156  pu.drawStyling(nom, 0.95,
157  1.05, TopPad = False)
158 
159  up.Draw("SAMEHIST")
160  dn.Draw("HISTSAME")
161 
162  pu.CreateLegend(0.7, 0.8, 0.9, 0.9)
163  pu.AddToLegend([up, dn])
164 
165  pu.DrawAtlas(0.2, 0.9)
166  pu.DrawSqrtS(0.2, 0.85)
167  pu.DrawTLatex(0.2,0.8, "%s, %s"%(H.get_wp(), H.get_calib()))
168 
169  pu.DrawLegend()
170 
171  pu.saveHisto("%s/SysCheck%s%s"%(Options.outDir, H.name(),bonusname) ,["pdf"])
172  pu.saveHisto("%s/AllSystDep%s" % (Options.outDir, bonusname),["pdf"])
173 
174  dummy.SaveAs("%s/AllSystDep%s.pdf]" % (Options.outDir, bonusname))
175 
176 
177 
PlotSFuncertainty.SystDependency.__muon_var
__muon_var
Direct access to the branch which are going to be compared.
Definition: PlotSFuncertainty.py:15
PlotUtils.PlotUtils
Definition: PlotUtils.py:97
PlotUtils.DiagnosticHisto
Definition: PlotUtils.py:18
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
PlotSFuncertainty.SystDependency.__sys
__sys
Definition: PlotSFuncertainty.py:20
PlotSFuncertainty.SystDependency.__norm
__norm
Definition: PlotSFuncertainty.py:16
PlotSFuncertainty.SystDependency.__nom_histo
__nom_histo
Definition: PlotSFuncertainty.py:25
PlotSFuncertainty.SystDependency.get_nom_H1
def get_nom_H1(self)
Definition: PlotSFuncertainty.py:67
PlotSFuncertainty.SystDependency.__wp
__wp
Definition: PlotSFuncertainty.py:19
python.ByteStreamConfig.write
def write
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:247
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
PlotSFuncertainty.SystDependency.__1dn_sf
__1dn_sf
Definition: PlotSFuncertainty.py:24
PlotSFuncertainty.SystDependency.fill
def fill(self)
Definition: PlotSFuncertainty.py:53
PlotSFuncertainty.SystDependency.__1up_histo
__1up_histo
Definition: PlotSFuncertainty.py:31
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
PlotSFuncertainty.SystDependency.__1up_sf
__1up_sf
Definition: PlotSFuncertainty.py:23
PlotSFuncertainty.SystDependency.get_sys
def get_sys(self)
Definition: PlotSFuncertainty.py:72
PlotSFuncertainty.SystDependency.get_wp
def get_wp(self)
Definition: PlotSFuncertainty.py:71
CheckAppliedSFs.getArgParser
def getArgParser()
Definition: CheckAppliedSFs.py:159
PlotSFuncertainty.SystDependency.get_calib
def get_calib(self)
Definition: PlotSFuncertainty.py:73
PlotSFuncertainty.SystDependency
Definition: PlotSFuncertainty.py:12
confTool.parse_args
def parse_args()
Definition: confTool.py:35
PlotSFuncertainty.SystDependency.__var_name
__var_name
Definition: PlotSFuncertainty.py:18
PlotSFuncertainty.SystDependency.finalize
def finalize(self)
Definition: PlotSFuncertainty.py:59
PlotSFuncertainty.SystDependency.get_1dn_H1
def get_1dn_H1(self)
Definition: PlotSFuncertainty.py:69
PlotSFuncertainty.SystDependency.__init__
def __init__(self, var_name="", axis_title="", bins=-1, bmin=0., bmax=0., bdir=None, calib="", wp="", sys="SYS", test_tree=None, log_binning=False)
Definition: PlotSFuncertainty.py:13
pickleTool.object
object
Definition: pickleTool.py:30
PlotSFuncertainty.SystDependency.__calib
__calib
Definition: PlotSFuncertainty.py:21
PlotSFuncertainty.SystDependency.name
def name(self)
Definition: PlotSFuncertainty.py:64
PlotSFuncertainty.SystDependency.__1dn_histo
__1dn_histo
Definition: PlotSFuncertainty.py:37
CheckAppliedSFs.getCalibReleasesAndWP
def getCalibReleasesAndWP(tree)
Definition: CheckAppliedSFs.py:172
PlotSFuncertainty.SystDependency.get_1up_H1
def get_1up_H1(self)
Definition: PlotSFuncertainty.py:68
PlotSFuncertainty.SystDependency.__nom_sf
__nom_sf
Definition: PlotSFuncertainty.py:22